Diffstat (limited to 'noncore/settings/netsystemtime/mainwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/mainwindow.cpp | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/noncore/settings/netsystemtime/mainwindow.cpp b/noncore/settings/netsystemtime/mainwindow.cpp new file mode 100644 index 0000000..306117a --- a/dev/null +++ b/noncore/settings/netsystemtime/mainwindow.cpp | |||
@@ -0,0 +1,404 @@ | |||
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 "mainwindow.h" | ||
30 | #include "timetabwidget.h" | ||
31 | #include "formattabwidget.h" | ||
32 | #include "settingstabwidget.h" | ||
33 | #include "ntptabwidget.h" | ||
34 | #include "predicttabwidget.h" | ||
35 | #include "systemtimeconfig.h" | ||
36 | |||
37 | #include <opie/oprocess.h> | ||
38 | #include <opie/otabwidget.h> | ||
39 | |||
40 | #include <qpe/config.h> | ||
41 | #include <qpe/datebookdb.h> | ||
42 | #include <qpe/qpeapplication.h> | ||
43 | |||
44 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | ||
45 | #include <qpe/qcopenvelope_qws.h> | ||
46 | #endif | ||
47 | |||
48 | #include <qlayout.h> | ||
49 | #include <qmessagebox.h> | ||
50 | #include <qsocket.h> | ||
51 | #include <qstring.h> | ||
52 | #include <qtimer.h> | ||
53 | |||
54 | MainWindow::MainWindow() | ||
55 | : QDialog( 0x0, 0x0, TRUE, 0 ) | ||
56 | { | ||
57 | setCaption( tr( "SystemTime" ) ); | ||
58 | |||
59 | QVBoxLayout *layout = new QVBoxLayout( this ); | ||
60 | layout->setMargin( 2 ); | ||
61 | layout->setSpacing( 4 ); | ||
62 | |||
63 | // Create main tabbed control | ||
64 | mainWidget = new OTabWidget( this ); | ||
65 | |||
66 | // Default object pointers to null | ||
67 | ntpProcess = 0x0; | ||
68 | ntpTab = 0x0; | ||
69 | |||
70 | // Add tab widgets | ||
71 | mainWidget->addTab( timeTab = new TimeTabWidget( mainWidget ), "netsystemtime/DateTime", tr( "Time" ) ); | ||
72 | mainWidget->addTab( formatTab = new FormatTabWidget( mainWidget ), "netsystemtime/formattab", tr( "Format" ) ); | ||
73 | mainWidget->addTab( settingsTab = new SettingsTabWidget( mainWidget ), "SettingsIcon", tr( "Settings" ) ); | ||
74 | mainWidget->addTab( predictTab = new PredictTabWidget( mainWidget ), "netsystemtime/predicttab", tr( "Predict" ) ); | ||
75 | Config config( "ntp" ); | ||
76 | config.setGroup( "settings" ); | ||
77 | slotDisplayNTPTab( config.readBoolEntry( "displayNtpTab", FALSE ) ); | ||
78 | slotDisplayPredictTab( config.readBoolEntry( "displayPredictTab", FALSE ) ); | ||
79 | |||
80 | mainWidget->setCurrentTab( tr( "Time" ) ); | ||
81 | layout->addWidget( mainWidget ); | ||
82 | |||
83 | // Create QCOP channel | ||
84 | QCopChannel *channel = new QCopChannel( "QPE/Application/netsystemtime", this ); | ||
85 | connect( channel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
86 | this, SLOT(slotQCopReceive(const QCString&, const QByteArray&)) ); | ||
87 | |||
88 | // Create NTP socket | ||
89 | ntpSock = new QSocket( this ); | ||
90 | connect( ntpSock, SIGNAL(error(int)),SLOT(slotCheckNtp(int)) ); | ||
91 | slotProbeNTPServer(); | ||
92 | |||
93 | // Create timer for automatic time lookups | ||
94 | ntpTimer = new QTimer( this ); | ||
95 | |||
96 | // Connect everything together | ||
97 | connect( timeTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); | ||
98 | connect( timeTab, SIGNAL(tzChanged(const QString &)), predictTab, SLOT(slotTZChanged(const QString &)) ); | ||
99 | connect( timeTab, SIGNAL(getPredictedTime()), predictTab, SLOT(slotSetPredictedTime()) ); | ||
100 | connect( formatTab, SIGNAL(show12HourTime(int)), timeTab, SLOT(slotUse12HourTime( int )) ); | ||
101 | connect( formatTab, SIGNAL(dateFormatChanged(const DateFormat &)), | ||
102 | timeTab, SLOT(slotDateFormatChanged(const DateFormat &)) ); | ||
103 | connect( formatTab, SIGNAL(weekStartChanged(int)), timeTab, SLOT(slotWeekStartChanged(int)) ); | ||
104 | connect( settingsTab, SIGNAL(ntpDelayChanged(int)), this, SLOT(slotNTPDelayChanged(int)) ); | ||
105 | connect( settingsTab, SIGNAL(displayNTPTab(bool)), this, SLOT(slotDisplayNTPTab(bool)) ); | ||
106 | connect( settingsTab, SIGNAL(displayPredictTab(bool)), this, SLOT(slotDisplayPredictTab(bool)) ); | ||
107 | connect( predictTab, SIGNAL(setTime(const QDateTime &)), this, SLOT(slotSetTime(const QDateTime &)) ); | ||
108 | |||
109 | // Do initial time server check | ||
110 | slotNTPDelayChanged( config.readNumEntry( "ntpRefreshFreq", 1440 ) ); | ||
111 | slotCheckNtp( -1 ); | ||
112 | |||
113 | // Display app | ||
114 | showMaximized(); | ||
115 | } | ||
116 | |||
117 | MainWindow::~MainWindow() | ||
118 | { | ||
119 | if ( ntpProcess ) | ||
120 | delete ntpProcess; | ||
121 | } | ||
122 | |||
123 | void MainWindow::accept() | ||
124 | { | ||
125 | // Turn off the screensaver (Note: needs to be encased in { } so that it deconstructs and sends) | ||
126 | { | ||
127 | QCopEnvelope disableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); | ||
128 | disableScreenSaver << 0 << 0 << 0; | ||
129 | } | ||
130 | |||
131 | // Update the systemtime | ||
132 | timeTab->saveSettings( TRUE ); | ||
133 | |||
134 | // Save format options | ||
135 | formatTab->saveSettings( TRUE ); | ||
136 | |||
137 | // Save settings options | ||
138 | settingsTab->saveSettings(); | ||
139 | |||
140 | // Since time has changed quickly load in the DateBookDB to allow the alarm server to get a better | ||
141 | // grip on itself (example re-trigger alarms for when we travel back in time). | ||
142 | DateBookDB db; | ||
143 | |||
144 | // Turn back on the screensaver | ||
145 | QCopEnvelope enableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); | ||
146 | enableScreenSaver << -1 << -1 << -1; | ||
147 | |||
148 | // Exit app | ||
149 | qApp->quit(); | ||
150 | } | ||
151 | |||
152 | void MainWindow::reject() | ||
153 | { | ||
154 | // Reset time settings | ||
155 | timeTab->saveSettings( FALSE ); | ||
156 | |||
157 | // Send notifications but do not save settings | ||
158 | formatTab->saveSettings( FALSE ); | ||
159 | |||
160 | // Exit app | ||
161 | qApp->quit(); | ||
162 | } | ||
163 | |||
164 | void MainWindow::runNTP() | ||
165 | { | ||
166 | if ( !ntpDelayElapsed() && ntpInteractive ) | ||
167 | { | ||
168 | QString msg = tr( "You asked for a delay of " ); | ||
169 | msg.append( QString::number( ntpDelay ) ); | ||
170 | msg.append( tr( " minutes, but only " ) ); | ||
171 | msg.append( QString::number( _lookupDiff / 60 ) ); | ||
172 | msg.append( tr(" minutes elapsed since last lookup.<br>Continue?") ); | ||
173 | |||
174 | switch ( | ||
175 | QMessageBox::warning( this, tr( "Continue?" ), msg, QMessageBox::Yes, QMessageBox::No ) | ||
176 | ) | ||
177 | { | ||
178 | case QMessageBox::Yes: break; | ||
179 | case QMessageBox::No: return; | ||
180 | default: return; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | QString srv = settingsTab->ntpServer(); | ||
185 | |||
186 | // Send information to time server tab if enabled | ||
187 | if ( ntpTabEnabled ) | ||
188 | { | ||
189 | ntpTab->setStartTime( QDateTime::currentDateTime().toString() ); | ||
190 | QString output = tr( "Running:\nntpdate " ); | ||
191 | output.append( srv ); | ||
192 | ntpTab->addNtpOutput( output ); | ||
193 | } | ||
194 | |||
195 | if ( !ntpProcess ) | ||
196 | { | ||
197 | ntpProcess = new OProcess(); | ||
198 | connect( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), | ||
199 | this, SLOT(slotNtpOutput(OProcess*,char*,int)) ); | ||
200 | connect( ntpProcess, SIGNAL(processExited(OProcess*)), | ||
201 | this, SLOT(slotNtpFinished(OProcess*)) ); | ||
202 | } | ||
203 | |||
204 | else | ||
205 | ntpProcess->clearArguments(); | ||
206 | |||
207 | *ntpProcess << "ntpdate" << srv; | ||
208 | bool ret = ntpProcess->start( OProcess::NotifyOnExit, OProcess::AllOutput ); | ||
209 | if ( !ret ) | ||
210 | { | ||
211 | QMessageBox::critical( this, tr( "Error" ), tr( "Error while getting time from network." ) ); | ||
212 | if ( ntpTabEnabled ) | ||
213 | ntpTab->addNtpOutput( tr( "Error while executing ntpdate" ) ); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | bool MainWindow::ntpDelayElapsed() | ||
218 | { | ||
219 | // Determine if time elapsed is greater than time delay | ||
220 | Config config( "ntp" ); | ||
221 | config.setGroup( "lookups" ); | ||
222 | _lookupDiff = TimeConversion::toUTC( QDateTime::currentDateTime() ) - config.readNumEntry( "time", 0 ); | ||
223 | if ( _lookupDiff < 0 ) | ||
224 | return true; | ||
225 | return ( _lookupDiff - ( ntpDelay * 60) ) > 0; | ||
226 | } | ||
227 | |||
228 | void MainWindow::slotSetTime( const QDateTime &dt ) | ||
229 | { | ||
230 | timeTab->setDateTime( dt ); | ||
231 | } | ||
232 | |||
233 | void MainWindow::slotQCopReceive( const QCString &msg, const QByteArray & ) | ||
234 | { | ||
235 | if ( msg == "ntpLookup(QString)" ) | ||
236 | { | ||
237 | ntpInteractive = false; | ||
238 | runNTP(); | ||
239 | } | ||
240 | if ( msg == "setPredictedTime(QString)" ) | ||
241 | { | ||
242 | //setPredictTime(); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | void MainWindow::slotDisplayNTPTab( bool display ) | ||
247 | { | ||
248 | ntpTabEnabled = display; | ||
249 | |||
250 | // Create widget if it hasn't needed | ||
251 | if ( display && !ntpTab ) | ||
252 | { | ||
253 | ntpTab = new NTPTabWidget( mainWidget ); | ||
254 | connect( ntpTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); | ||
255 | } | ||
256 | |||
257 | // Display/hide tab | ||
258 | display ? mainWidget->addTab( ntpTab, "netsystemtime/ntptab", tr( "Time Server" ) ) | ||
259 | : mainWidget->removePage( ntpTab ); | ||
260 | } | ||
261 | |||
262 | void MainWindow::slotDisplayPredictTab( bool display ) | ||
263 | { | ||
264 | predictTabEnabled = display; | ||
265 | |||
266 | // Create widget if it hasn't needed | ||
267 | if ( display && !predictTab ) | ||
268 | { | ||
269 | } | ||
270 | // Display/hide tab | ||
271 | display ? mainWidget->addTab( predictTab, "netsystemtime/predicttab", tr( "Predict" ) ) | ||
272 | : mainWidget->removePage( predictTab ); | ||
273 | } | ||
274 | |||
275 | void MainWindow::slotGetNTPTime() | ||
276 | { | ||
277 | ntpInteractive = TRUE; | ||
278 | runNTP(); | ||
279 | } | ||
280 | |||
281 | void MainWindow::slotTimerGetNTPTime() | ||
282 | { | ||
283 | ntpInteractive = FALSE; | ||
284 | runNTP(); | ||
285 | } | ||
286 | |||
287 | void MainWindow::slotProbeNTPServer() | ||
288 | { | ||
289 | ntpSock->connectToHost( settingsTab->ntpServer(), 123 ); | ||
290 | } | ||
291 | |||
292 | void MainWindow::slotNtpOutput( OProcess *, char *buffer, int buflen ) | ||
293 | { | ||
294 | QString output = QString( buffer ).left( buflen ); | ||
295 | ntpOutput.append( output ); | ||
296 | |||
297 | if ( ntpTabEnabled ) | ||
298 | ntpTab->addNtpOutput( output ); | ||
299 | } | ||
300 | |||
301 | void MainWindow::slotNtpFinished( OProcess *p ) | ||
302 | { | ||
303 | QString output; | ||
304 | QDateTime dt = QDateTime::currentDateTime(); | ||
305 | |||
306 | // Verify run was successful | ||
307 | if ( p->exitStatus() != 0 || !p->normalExit() ) | ||
308 | { | ||
309 | if ( isVisible() && ntpInteractive ) | ||
310 | { | ||
311 | output = tr( "Error while getting time from\n server: " ); | ||
312 | output.append( settingsTab->ntpServer() ); | ||
313 | QMessageBox::critical(this, tr( "Error" ), output ); | ||
314 | } | ||
315 | // slotCheckNtp(-1); | ||
316 | return; | ||
317 | } | ||
318 | |||
319 | // Set controls on time tab to new time value | ||
320 | timeTab->setDateTime( dt ); | ||
321 | |||
322 | // Write out lookup information | ||
323 | Config config( "ntp" ); | ||
324 | config.setGroup( "lookups" ); | ||
325 | int lastLookup = config.readNumEntry( "time", 0 ); | ||
326 | int lookupCount = config.readNumEntry( "count", 0 ); | ||
327 | bool lastNtp = config.readBoolEntry( "lastNtp", FALSE ); | ||
328 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
329 | config.writeEntry( "time", time ); | ||
330 | |||
331 | // Calculate new time/time shift | ||
332 | QString _offset = "offset"; | ||
333 | QString _sec = "sec"; | ||
334 | QRegExp _reOffset = QRegExp( _offset ); | ||
335 | QRegExp _reEndOffset = QRegExp( _sec ); | ||
336 | int posOffset = _reOffset.match( ntpOutput ); | ||
337 | int posEndOffset = _reEndOffset.match( ntpOutput, posOffset ); | ||
338 | posOffset += _offset.length() + 1; | ||
339 | QString diff = ntpOutput.mid( posOffset, posEndOffset - posOffset - 1 ); | ||
340 | |||
341 | float timeShift = diff.toFloat(); | ||
342 | if ( timeShift == 0.0 ) | ||
343 | return; | ||
344 | int secsSinceLast = time - lastLookup; | ||
345 | output = QString::number( timeShift ); | ||
346 | output.append( tr( " seconds" ) ); | ||
347 | |||
348 | // Display information on time server tab | ||
349 | if ( ntpTabEnabled ) | ||
350 | { | ||
351 | ntpTab->setTimeShift( output ); | ||
352 | ntpTab->setNewTime( dt.toString() ); | ||
353 | } | ||
354 | |||
355 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60 * ntpDelay ) | ||
356 | { | ||
357 | QString grpname = QString( "lookup_" ).append( QString::number( lookupCount ) ); | ||
358 | config.setGroup( grpname ); | ||
359 | lookupCount++; | ||
360 | predictTab->setShiftPerSec( timeShift / secsSinceLast ); | ||
361 | config.writeEntry( "secsSinceLast", secsSinceLast ); | ||
362 | config.writeEntry( "timeShift", QString::number( timeShift ) ); | ||
363 | config.setGroup( "lookups" ); | ||
364 | config.writeEntry( "count", lookupCount ); | ||
365 | config.writeEntry( "lastNtp", TRUE ); | ||
366 | } | ||
367 | } | ||
368 | |||
369 | void MainWindow::slotNTPDelayChanged( int delay ) | ||
370 | { | ||
371 | ntpTimer->changeInterval( delay * 1000 * 60 ); | ||
372 | ntpDelay = delay; | ||
373 | } | ||
374 | |||
375 | void MainWindow::slotCheckNtp( int i ) | ||
376 | { | ||
377 | if ( i == 0 ) | ||
378 | { | ||
379 | if ( ntpDelayElapsed() ) | ||
380 | { | ||
381 | runNTP(); | ||
382 | disconnect( ntpTimer, SIGNAL(timeout()), this, SLOT(slotProbeNTPServer()) ); | ||
383 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotTimerGetNTPTime()) ); | ||
384 | } | ||
385 | else | ||
386 | { | ||
387 | disconnect(ntpTimer, SIGNAL(timeout()), this, SLOT(slotTimerGetNTPTime()) ); | ||
388 | connect(ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); | ||
389 | } | ||
390 | } | ||
391 | else | ||
392 | { | ||
393 | predictTab->slotPredictTime(); | ||
394 | if ( i > 0 ) | ||
395 | { | ||
396 | QString output = tr( "Could not connect to server " ); | ||
397 | output.append( settingsTab->ntpServer() ); | ||
398 | ntpOutput.append( output ); | ||
399 | if ( ntpTabEnabled ) | ||
400 | ntpTab->addNtpOutput( output ); | ||
401 | } | ||
402 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); | ||
403 | } | ||
404 | } | ||