summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/settime.cpp
Unidiff
Diffstat (limited to 'noncore/settings/netsystemtime/settime.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/settime.cpp459
1 files changed, 459 insertions, 0 deletions
diff --git a/noncore/settings/netsystemtime/settime.cpp b/noncore/settings/netsystemtime/settime.cpp
new file mode 100644
index 0000000..dac5bd4
--- a/dev/null
+++ b/noncore/settings/netsystemtime/settime.cpp
@@ -0,0 +1,459 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "settime.h"
22
23#include <qpe/alarmserver.h>
24#include <qpe/qpeapplication.h>
25#include <qpe/config.h>
26#include <qpe/datebookdb.h>
27#include <qpe/datebookmonth.h>
28#include <qpe/global.h>
29#include <qpe/resource.h>
30#include <qpe/timeconversion.h>
31#include <qpe/tzselect.h>
32#include <qpe/timestring.h>
33#include <qpe/qpedialog.h>
34#if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP)
35#include <qpe/qcopenvelope_qws.h>
36#endif
37
38#include <qbuttongroup.h>
39#include <qcheckbox.h>
40#include <qlabel.h>
41#include <qlayout.h>
42#include <qradiobutton.h>
43#include <qspinbox.h>
44#include <qtoolbutton.h>
45#include <qwindowsystem_qws.h>
46#include <qcombobox.h>
47
48#include <sys/time.h>
49#include <time.h>
50#include <stdlib.h>
51#include <stdio.h>
52
53
54SetDateTime::SetDateTime(QWidget *parent, const char *name, WFlags f )
55 : NtpBase( parent, name, f )
56{
57 // setCaption( tr("Set System Time") );
58
59// QVBoxLayout *vb = new QVBoxLayout( this, 5 );
60 QVBoxLayout *vb = new QVBoxLayout( FrameSystemTime, 5 );
61 QVBoxLayout *vb2 = new QVBoxLayout( FrameSetTime, 5 );
62
63 QHBoxLayout *hb = new QHBoxLayout( vb, -1, "timezone layout" );
64
65 QLabel *lblZone = new QLabel( tr( "Time Zone" ), FrameSystemTime, "timezone label" );
66 lblZone->setMaximumSize( lblZone->sizeHint() );
67 hb->addWidget( lblZone );
68
69 tz = new TimeZoneSelector( FrameSystemTime, "Timezone choices" );
70 tz->setMinimumSize( tz->sizeHint() );
71 hb->addWidget( tz );
72
73 timeButton = new SetTime( FrameSetTime );
74 vb2->addWidget( timeButton );
75
76 QHBoxLayout *db = new QHBoxLayout( vb2 );
77 QLabel *dateLabel = new QLabel( tr("Date"), FrameSetTime );
78 db->addWidget( dateLabel, 1 );
79 dateButton = new DateButton( TRUE, FrameSetTime );
80 db->addWidget( dateButton, 2 );
81
82 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
83 vb2->addItem( spacer );
84
85 QFrame *hline = new QFrame( FrameSystemTime );
86 hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
87 vb->addWidget( hline );
88
89 Config config("qpe");
90 config.setGroup( "Time" );
91
92 QHBoxLayout *hb1 = new QHBoxLayout( vb );
93
94 QLabel *l = new QLabel( tr("Time format"), FrameSystemTime );
95 // l->setAlignment( AlignRight | AlignVCenter );
96 hb1->addWidget( l, 1 );
97
98
99 ampmCombo = new QComboBox( FrameSystemTime );
100 ampmCombo->insertItem( tr("24 hour"), 0 );
101 ampmCombo->insertItem( tr("12 hour"), 1 );
102 hb1->addWidget( ampmCombo, 2 );
103
104 int show12hr = config.readBoolEntry("AMPM") ? 1 : 0;
105 ampmCombo->setCurrentItem( show12hr );
106 timeButton->show12hourTime( show12hr );
107
108 connect(ampmCombo, SIGNAL(activated(int)),
109 timeButton, SLOT(show12hourTime(int)));
110
111
112
113 QHBoxLayout *hb2 = new QHBoxLayout( vb );
114 l = new QLabel( tr("Weeks start on" ), FrameSystemTime );
115 //l->setAlignment( AlignRight | AlignVCenter );
116 hb2->addWidget( l, 1 );
117
118 weekStartCombo = new QComboBox( FrameSystemTime );
119 weekStartCombo->insertItem( tr("Sunday"), 0 );
120 weekStartCombo->insertItem( tr("Monday"), 1 );
121
122 hb2->addWidget( weekStartCombo, 2 );
123 int startMonday = config.readBoolEntry("MONDAY") ? 1 : 0;
124 dateButton->setWeekStartsMonday( startMonday );
125 weekStartCombo->setCurrentItem( startMonday );
126
127 connect( weekStartCombo, SIGNAL( activated(int)),
128 dateButton, SLOT(setWeekStartsMonday(int)));
129
130
131 QHBoxLayout *hb3 = new QHBoxLayout( vb );
132 l = new QLabel( tr("Date format" ), FrameSystemTime );
133 hb3->addWidget( l, 1 );
134 dateFormatCombo = new QComboBox( FrameSystemTime );
135 hb3->addWidget( dateFormatCombo, 2 );
136
137 config.setGroup( "Date" );
138 DateFormat df(QChar(config.readEntry("Separator", "/")[0]),
139 (DateFormat::Order)config .readNumEntry("ShortOrder", DateFormat::DayMonthYear),
140 (DateFormat::Order)config.readNumEntry("LongOrder", DateFormat::DayMonthYear));
141
142 int currentdf = 0;
143 date_formats[0] = DateFormat('/', DateFormat::MonthDayYear);
144 dateFormatCombo->insertItem( tr( date_formats[0].toNumberString()) );
145 date_formats[1] = DateFormat('.', DateFormat::DayMonthYear);
146 if (df == date_formats[1])
147 currentdf = 1;
148 dateFormatCombo->insertItem( tr( date_formats[1].toNumberString() ) );
149 date_formats[2] = DateFormat('-', DateFormat::YearMonthDay,
150 DateFormat::DayMonthYear);
151 if (df == date_formats[2])
152 currentdf = 2;
153 dateFormatCombo->insertItem( tr( date_formats[2].toNumberString() ) ); //ISO8601
154 date_formats[3] = DateFormat('/', DateFormat::DayMonthYear);
155 if (df == date_formats[3])
156 currentdf = 3;
157 dateFormatCombo->insertItem( tr( date_formats[3].toNumberString() ) );
158
159 dateFormatCombo->setCurrentItem( currentdf );
160 dateButton->setDateFormat( df );
161
162 connect( dateFormatCombo, SIGNAL( activated(int)),
163 SLOT(formatChanged(int)));
164
165 QHBoxLayout *hb4 = new QHBoxLayout( vb );
166 l = new QLabel( tr("Applet format" ), FrameSystemTime );
167 hb4->addWidget( l, 1 );
168
169 clockAppletCombo = new QComboBox( FrameSystemTime );
170 clockAppletCombo->insertItem( tr("hh:mm"), 0 );
171 clockAppletCombo->insertItem( tr("D/M hh:mm"), 1 );
172 clockAppletCombo->insertItem( tr("M/D hh:mm"), 2 );
173
174 hb4->addWidget( clockAppletCombo, 2 );
175 int clockApplet = config.readNumEntry("ClockApplet",0);
176 clockAppletCombo->setCurrentItem( clockApplet );
177
178 vb->addStretch( 0 );
179
180 QObject::connect( tz, SIGNAL( signalNewTz( const QString& ) ),
181 timeButton, SLOT( slotTzChange( const QString& ) ) );
182 QObject::connect( tz, SIGNAL( signalNewTz( const QString& ) ),
183 SLOT( tzChange( const QString& ) ) );
184 QObject::connect( PushButtonSetManualTime, SLOT(clicked()),
185 SLOT(commitTime()));
186// dl = new QPEDialogListener(this);
187}
188
189void SetDateTime::accept()
190{
191
192 Config config("qpe");
193 config.setGroup( "Time" );
194 config.writeEntry( "AMPM", ampmCombo->currentItem() );
195 config.writeEntry( "MONDAY", weekStartCombo->currentItem() );
196 config.setGroup( "Date" );
197 DateFormat df = date_formats[dateFormatCombo->currentItem()];
198 config.writeEntry( "Separator", QString(df.separator()));
199 config.writeEntry( "ShortOrder", df.shortOrder());
200 config.writeEntry( "LongOrder", df.longOrder());
201 config.writeEntry( "ClockApplet", clockAppletCombo->currentItem() );
202
203 Config lconfig("locale");
204 lconfig.setGroup( "Location" );
205 lconfig.writeEntry( "Timezone", tz->currentZone() );
206
207}
208
209void SetDateTime::commitTime()
210{
211 Config cfg("ntp",Config::User);
212 cfg.setGroup("lookups");
213 int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
214 cfg.writeEntry("time", time);
215 tz->setFocus();
216 // really turn off the screensaver before doing anything
217 {
218 // Needs to be encased in { } so that it deconstructs and sends
219 QCopEnvelope disableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" );
220 disableScreenSaver << 0 << 0 << 0;
221 }
222 // Need to process the QCOP event generated above before proceeding
223 qApp->processEvents();
224
225 // before we progress further, set our TZ!
226 setenv( "TZ", tz->currentZone(), 1 );
227 // now set the time...
228 QDateTime dt( dateButton->date(), timeButton->time() );
229
230 if ( dt.isValid() ) {
231 struct timeval myTv;
232 myTv.tv_sec = TimeConversion::toUTC( dt );
233 myTv.tv_usec = 0;
234
235 if ( myTv.tv_sec != -1 )
236 ::settimeofday( &myTv, 0 );
237 Global::writeHWClock();
238 // since time has changed quickly load in the datebookdb
239 // to allow the alarm server to get a better grip on itself
240 // (example re-trigger alarms for when we travel back in time)
241 DateBookDB db;
242 } else {
243 qWarning( "Invalid date/time" );
244 }
245 // set the timezone for everyone else...
246 QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" );
247 setTimeZone << tz->currentZone();
248
249 // AM/PM setting and notify time changed
250 QCopEnvelope setClock( "QPE/System", "clockChange(bool)" );
251 setClock << ampmCombo->currentItem();
252
253 // Notify everyone what day we prefer to start the week on.
254 QCopEnvelope setWeek( "QPE/System", "weekChange(bool)" );
255 setWeek << weekStartCombo->currentItem();
256
257 // Notify everyone what date format to use
258 QCopEnvelope setDateFormat( "QPE/System", "setDateFormat(DateFormat)" );
259 setDateFormat << date_formats[dateFormatCombo->currentItem()];
260
261 // Restore screensaver
262 QCopEnvelope enableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" );
263 enableScreenSaver << -1 << -1 << -1;
264
265 // QDialog::accept();
266}
267
268void SetDateTime::done(int r)
269{
270// QDialog::done(r);
271 close();
272}
273
274void SetDateTime::tzChange( const QString &tz )
275{
276 // set the TZ get the time and leave gracefully...
277 QString strSave;
278 strSave = getenv( "TZ" );
279 setenv( "TZ", tz, 1 );
280
281 QDate d = QDate::currentDate();
282 // reset the time.
283 if ( !strSave.isNull() ) {
284 setenv( "TZ", strSave, 1 );
285 }
286 dateButton->setDate( d );
287}
288
289void SetDateTime::formatChanged(int i)
290{
291 dateButton->setDateFormat(date_formats[i]);
292}
293
294static const int ValueAM = 0;
295static const int ValuePM = 1;
296
297
298
299SetTime::SetTime( QWidget *parent, const char *name )
300 : QWidget( parent, name )
301{
302 use12hourTime = FALSE;
303
304 QTime currTime = QTime::currentTime();
305 hour = currTime.hour();
306 minute = currTime.minute();
307
308 QHBoxLayout *hb2 = new QHBoxLayout( this );
309 hb2->setSpacing( 3 );
310
311 QLabel *l = new QLabel( tr("Hour"), this );
312 // l->setAlignment( AlignRight | AlignVCenter );
313 hb2->addWidget( l );
314
315 sbHour = new QSpinBox( this );
316 sbHour->setMinimumWidth( 30 );
317 if(use12hourTime) {
318 sbHour->setMinValue(1);
319 sbHour->setMaxValue( 12 );
320 int show_hour = hour;
321 if (hour > 12)
322 show_hour -= 12;
323 if (show_hour == 0)
324 show_hour = 12;
325
326 sbHour->setValue( show_hour );
327 } else {
328 sbHour->setMinValue( 0 );
329 sbHour->setMaxValue( 23 );
330 sbHour->setValue( hour );
331 }
332 sbHour->setWrapping(TRUE);
333 connect( sbHour, SIGNAL(valueChanged(int)), this, SLOT(hourChanged(int)) );
334 hb2->addWidget( sbHour );
335
336 hb2->addStretch( 1 );
337
338 l = new QLabel( tr("Minute"), this );
339 //l->setAlignment( AlignRight | AlignVCenter );
340 hb2->addWidget( l );
341
342 sbMin = new QSpinBox( this );
343 sbMin->setMinValue( 0 );
344 sbMin->setMaxValue( 59 );
345 sbMin->setWrapping(TRUE);
346 sbMin->setValue( minute );
347 sbMin->setMinimumWidth( 30 );
348 connect( sbMin, SIGNAL(valueChanged(int)), this, SLOT(minuteChanged(int)) );
349 hb2->addWidget( sbMin );
350
351 hb2->addStretch( 1 );
352
353 ampm = new QComboBox( this );
354 ampm->insertItem( tr("AM"), ValueAM );
355 ampm->insertItem( tr("PM"), ValuePM );
356 connect( ampm, SIGNAL(activated(int)), this, SLOT(checkedPM(int)) );
357 hb2->addWidget( ampm );
358
359 hb2->addStretch( 1 );
360
361}
362
363QTime SetTime::time() const
364{
365 return QTime( hour, minute, 0 );
366}
367
368void SetTime::hourChanged( int value )
369{
370 if(use12hourTime) {
371 int realhour = value;
372 if (realhour == 12)
373 realhour = 0;
374 if (ampm->currentItem() == ValuePM )
375 realhour += 12;
376 hour = realhour;
377 } else
378 hour = value;
379}
380
381void SetTime::minuteChanged( int value )
382{
383 minute = value;
384}
385
386void SetTime::show12hourTime( int on )
387{
388 use12hourTime = on;
389 ampm->setEnabled(on);
390
391 int show_hour = hour;
392 if ( on ) {
393 /* this might change the value of hour */
394 sbHour->setMinValue(1);
395 sbHour->setMaxValue( 12 );
396
397 /* so use one we saved earlier */
398 if (show_hour >= 12) {
399 show_hour -= 12;
400 ampm->setCurrentItem( ValuePM );
401 } else {
402 ampm->setCurrentItem( ValueAM );
403 }
404 if (show_hour == 0)
405 show_hour = 12;
406
407 } else {
408 sbHour->setMinValue( 0 );
409 sbHour->setMaxValue( 23 );
410 }
411
412
413 sbHour->setValue( show_hour );
414}
415
416void SetTime::checkedPM( int c )
417{
418 int show_hour = sbHour->value();
419 if (show_hour == 12)
420 show_hour = 0;
421
422 if ( c == ValuePM )
423 show_hour += 12;
424
425 hour = show_hour;
426}
427
428void SetTime::slotTzChange( const QString &tz )
429{
430 // set the TZ get the time and leave gracefully...
431 QString strSave;
432 strSave = getenv( "TZ" );
433 setenv( "TZ", tz, 1 );
434
435 QTime t = QTime::currentTime();
436 // reset the time.
437 if ( !strSave.isNull() ) {
438 setenv( "TZ", strSave, 1 );
439 }
440
441 // just set the spinboxes and let it propage through
442 if(use12hourTime) {
443 int show_hour = t.hour();
444 if (t.hour() >= 12) {
445 show_hour -= 12;
446 ampm->setCurrentItem( ValuePM );
447 } else {
448 ampm->setCurrentItem( ValueAM );
449 }
450 if (show_hour == 0)
451 show_hour = 12;
452 sbHour->setValue( show_hour );
453 } else {
454 sbHour->setValue( t.hour() );
455 }
456 sbMin->setValue( t.minute() );
457}
458
459