Diffstat (limited to 'noncore/settings/netsystemtime/timetabwidget.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/timetabwidget.cpp | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/noncore/settings/netsystemtime/timetabwidget.cpp b/noncore/settings/netsystemtime/timetabwidget.cpp new file mode 100644 index 0000000..6f24462 --- a/dev/null +++ b/noncore/settings/netsystemtime/timetabwidget.cpp | |||
@@ -0,0 +1,292 @@ | |||
1 | /* | ||
2 | This file is part of the OPIE Project | ||
3 | =. | ||
4 | .=l. Copyright (c) 2002 OPIE team <opie@handhelds.org?> | ||
5 | .>+-= | ||
6 | _;:, .> :=|. This file is free software; you can | ||
7 | .> <`_, > . <= redistribute it and/or modify it under | ||
8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public | ||
9 | .="- .-=="i, .._ License as published by the Free Software | ||
10 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
11 | ._= =} : or (at your option) any later version. | ||
12 | .%`+i> _;_. | ||
13 | .i_,=:_. -<s. This file is distributed in the hope that | ||
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
15 | : .. .:, . . . without even the implied warranty of | ||
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | ||
18 | ..}^=.= = ; Public License for more details. | ||
19 | ++= -. .` .: | ||
20 | : = ...= . :.=- You should have received a copy of the GNU | ||
21 | -. .:....=;==+<; General Public License along with this file; | ||
22 | -_. . . )=. = see the file COPYING. If not, write to the | ||
23 | -- :-=` Free Software Foundation, Inc., | ||
24 | 59 Temple Place - Suite 330, | ||
25 | Boston, MA 02111-1307, USA. | ||
26 | |||
27 | */ | ||
28 | |||
29 | #include "timetabwidget.h" | ||
30 | |||
31 | #include <qpe/config.h> | ||
32 | #include <qpe/datebookmonth.h> | ||
33 | #include <qpe/global.h> | ||
34 | #include <qpe/resource.h> | ||
35 | #include <qpe/tzselect.h> | ||
36 | |||
37 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | ||
38 | #include <qpe/qcopenvelope_qws.h> | ||
39 | #endif | ||
40 | |||
41 | #include <qcombobox.h> | ||
42 | #include <qdatetime.h> | ||
43 | #include <qframe.h> | ||
44 | #include <qlabel.h> | ||
45 | #include <qlayout.h> | ||
46 | #include <qpushbutton.h> | ||
47 | #include <qscrollview.h> | ||
48 | #include <qspinbox.h> | ||
49 | |||
50 | #include <stdlib.h> | ||
51 | #include <sys/time.h> | ||
52 | |||
53 | static const int ValueAM = 0; | ||
54 | static const int ValuePM = 1; | ||
55 | |||
56 | TimeTabWidget::TimeTabWidget( QWidget *parent ) | ||
57 | : QWidget( parent, 0x0, 0 ) | ||
58 | { | ||
59 | // Synchronize HW clock to systemtime | ||
60 | // This app will update systemtime | ||
61 | // - if Cancel is clicked, will reset systemtime to HW clock's time | ||
62 | // - if Ok is clicked, will leave systemtime as is | ||
63 | system("/sbin/hwclock --systohc --utc"); | ||
64 | |||
65 | QVBoxLayout *tmpvb = new QVBoxLayout( this ); | ||
66 | QScrollView *sv = new QScrollView( this ); | ||
67 | tmpvb->addWidget( sv, 0, 0 ); | ||
68 | sv->setResizePolicy( QScrollView::AutoOneFit ); | ||
69 | sv->setFrameStyle( QFrame::NoFrame ); | ||
70 | QWidget *container = new QWidget( sv->viewport() ); | ||
71 | sv->addChild( container ); | ||
72 | |||
73 | QGridLayout *layout = new QGridLayout( container ); | ||
74 | layout->setMargin( 2 ); | ||
75 | layout->setSpacing( 4 ); | ||
76 | |||
77 | // Hours | ||
78 | layout->addMultiCellWidget( new QLabel( tr( "Hour" ), container ), 1, 1, 0, 1 ); | ||
79 | sbHour = new QSpinBox( container ); | ||
80 | sbHour->setWrapping( TRUE ); | ||
81 | layout->addMultiCellWidget( sbHour, 2, 2, 0, 1 ); | ||
82 | |||
83 | // Minutes | ||
84 | layout->addMultiCellWidget( new QLabel( tr( "Minute" ), container ), 1, 1, 2, 3 ); | ||
85 | sbMin = new QSpinBox( container ); | ||
86 | sbMin->setWrapping( TRUE ); | ||
87 | sbMin->setMinValue( 0 ); | ||
88 | sbMin->setMaxValue( 59 ); | ||
89 | layout->addMultiCellWidget( sbMin, 2, 2, 2, 3 ); | ||
90 | |||
91 | // AM/PM | ||
92 | cbAmpm = new QComboBox( container ); | ||
93 | cbAmpm->insertItem( tr( "AM" ), ValueAM ); | ||
94 | cbAmpm->insertItem( tr( "PM" ), ValuePM ); | ||
95 | layout->addMultiCellWidget( cbAmpm, 2, 2, 4, 5 ); | ||
96 | |||
97 | // Date | ||
98 | layout->addWidget( new QLabel( tr( "Date" ), container ), 4, 0 ); | ||
99 | btnDate = new DateButton( TRUE, container ); | ||
100 | layout->addMultiCellWidget( btnDate, 4, 4, 1, 5 ); | ||
101 | |||
102 | // Timezone | ||
103 | layout->addMultiCellWidget( new QLabel( tr( "Time zone" ), container ), 6, 6, 0, 1 ); | ||
104 | selTimeZone = new TimeZoneSelector( container ); | ||
105 | connect( selTimeZone, SIGNAL(signalNewTz(const QString &)), this, SLOT(slotTZChanged(const QString &)) ); | ||
106 | layout->addMultiCellWidget( selTimeZone, 6, 6, 2, 5 ); | ||
107 | |||
108 | // Space filler | ||
109 | layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 7, 0 ); | ||
110 | |||
111 | // Set NTP time button | ||
112 | QPushButton *pb = new QPushButton( Resource::loadPixmap( "netsystemtime/ntptab" ), | ||
113 | tr( "Get time from the network" ), container ); | ||
114 | connect( pb, SIGNAL(clicked()), this, SIGNAL(getNTPTime()) ); | ||
115 | layout->addMultiCellWidget( pb, 8, 8, 0, 5 ); | ||
116 | |||
117 | // Set predicted time button | ||
118 | pb = new QPushButton( Resource::loadPixmap( "netsystemtime/predicttab" ), tr( "Set predicted time" ), | ||
119 | container ); | ||
120 | connect( pb, SIGNAL(clicked()), this, SIGNAL(getPredictedTime()) ); | ||
121 | layout->addMultiCellWidget( pb, 9, 9, 0, 5 ); | ||
122 | |||
123 | // Space filler at bottom of widget | ||
124 | layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 10, 0 ); | ||
125 | |||
126 | // Initialize values | ||
127 | Config config( "locale" ); | ||
128 | config.setGroup( "Location" ); | ||
129 | selTimeZone->setCurrentZone( config.readEntry( "Timezone", "America/New_York" ) ); | ||
130 | use12HourTime = FALSE; | ||
131 | setDateTime( QDateTime::currentDateTime() ); | ||
132 | } | ||
133 | |||
134 | TimeTabWidget::~TimeTabWidget() | ||
135 | { | ||
136 | } | ||
137 | |||
138 | void TimeTabWidget::saveSettings( bool commit ) | ||
139 | { | ||
140 | if ( commit ) | ||
141 | { | ||
142 | // Set timezone and announce to world | ||
143 | QString tz = selTimeZone->currentZone(); | ||
144 | Config config("locale"); | ||
145 | config.setGroup( "Location" ); | ||
146 | config.writeEntry( "Timezone", tz ); | ||
147 | setenv( "TZ", tz, 1 ); | ||
148 | QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" ); | ||
149 | setTimeZone << tz; | ||
150 | |||
151 | // If controls have a valid date & time, update systemtime | ||
152 | int hour = sbHour->value(); | ||
153 | if ( use12HourTime && cbAmpm->currentItem() == ValuePM ) | ||
154 | hour += 12; | ||
155 | QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) ); | ||
156 | setSystemTime( dt ); | ||
157 | } | ||
158 | else | ||
159 | { | ||
160 | // Reset systemtime to hardware clock (i.e. undo any changes made by this app) | ||
161 | system("/sbin/hwclock --hctosys --utc"); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | void TimeTabWidget::setDateTime( const QDateTime &dt ) | ||
166 | { | ||
167 | // Set time | ||
168 | QTime t = dt.time(); | ||
169 | if( use12HourTime ) | ||
170 | { | ||
171 | int show_hour = t.hour(); | ||
172 | if ( t.hour() >= 12 ) | ||
173 | { | ||
174 | show_hour -= 12; | ||
175 | cbAmpm->setCurrentItem( ValuePM ); | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | cbAmpm->setCurrentItem( ValueAM ); | ||
180 | } | ||
181 | if ( show_hour == 0 ) | ||
182 | show_hour = 12; | ||
183 | sbHour->setValue( show_hour ); | ||
184 | } | ||
185 | else | ||
186 | { | ||
187 | sbHour->setValue( t.hour() ); | ||
188 | } | ||
189 | sbMin->setValue( t.minute() ); | ||
190 | |||
191 | // Set date | ||
192 | btnDate->setDate( dt.date() ); | ||
193 | } | ||
194 | |||
195 | void TimeTabWidget::setSystemTime( const QDateTime &dt ) | ||
196 | { | ||
197 | // Set system clock | ||
198 | if ( dt.isValid() ) | ||
199 | { | ||
200 | struct timeval myTv; | ||
201 | int t = TimeConversion::toUTC( dt ); | ||
202 | myTv.tv_sec = t; | ||
203 | myTv.tv_usec = 0; | ||
204 | |||
205 | if ( myTv.tv_sec != -1 ) | ||
206 | ::settimeofday( &myTv, 0 ); | ||
207 | } | ||
208 | } | ||
209 | |||
210 | void TimeTabWidget::slotUse12HourTime( int i ) | ||
211 | { | ||
212 | use12HourTime = (i == 1); | ||
213 | |||
214 | cbAmpm->setEnabled( use12HourTime ); | ||
215 | |||
216 | int show_hour = sbHour->value(); | ||
217 | |||
218 | if ( use12HourTime ) | ||
219 | { | ||
220 | sbHour->setMinValue( 1 ); | ||
221 | sbHour->setMaxValue( 12 ); | ||
222 | |||
223 | if ( show_hour >= 12 ) | ||
224 | { | ||
225 | show_hour -= 12; | ||
226 | cbAmpm->setCurrentItem( ValuePM ); | ||
227 | } | ||
228 | else | ||
229 | { | ||
230 | cbAmpm->setCurrentItem( ValueAM ); | ||
231 | } | ||
232 | if ( show_hour == 0 ) | ||
233 | show_hour = 12; | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | sbHour->setMinValue( 0 ); | ||
238 | sbHour->setMaxValue( 23 ); | ||
239 | |||
240 | if ( cbAmpm->currentItem() == ValuePM ) | ||
241 | { | ||
242 | show_hour += 12; | ||
243 | if ( show_hour == 24 ) | ||
244 | show_hour = 0; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | sbHour->setValue( show_hour ); | ||
249 | } | ||
250 | |||
251 | void TimeTabWidget::slotDateFormatChanged( const DateFormat &df ) | ||
252 | { | ||
253 | btnDate->setDateFormat( df ); | ||
254 | } | ||
255 | |||
256 | void TimeTabWidget::slotWeekStartChanged( int monday ) | ||
257 | { | ||
258 | btnDate->setWeekStartsMonday( monday ); | ||
259 | } | ||
260 | |||
261 | void TimeTabWidget::slotTZChanged( const QString &newtz ) | ||
262 | { | ||
263 | // If controls have a valid date & time, update systemtime | ||
264 | int hour = sbHour->value(); | ||
265 | if ( use12HourTime && cbAmpm->currentItem() == ValuePM ) | ||
266 | hour += 12; | ||
267 | QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) ); | ||
268 | setSystemTime( dt ); | ||
269 | QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" ); | ||
270 | setTimeZone << newtz; | ||
271 | |||
272 | // Set system timezone | ||
273 | QString currtz = getenv( "TZ" ); | ||
274 | setenv( "TZ", newtz, 1 ); | ||
275 | |||
276 | // Get new date/time | ||
277 | hour = sbHour->value(); | ||
278 | if ( use12HourTime && cbAmpm->currentItem() == ValuePM ) | ||
279 | hour += 12; | ||
280 | dt = QDateTime::currentDateTime(); | ||
281 | |||
282 | // Reset system timezone | ||
283 | if ( !currtz.isNull() ) | ||
284 | { | ||
285 | setenv( "TZ", currtz, 1 ); | ||
286 | } | ||
287 | |||
288 | // Set controls to new time | ||
289 | setDateTime( dt ); | ||
290 | |||
291 | emit tzChanged( newtz ); | ||
292 | } | ||