Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 446 |
1 files changed, 0 insertions, 446 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp deleted file mode 100644 index f653cd0..0000000 --- a/noncore/settings/netsystemtime/ntp.cpp +++ b/dev/null | |||
@@ -1,446 +0,0 @@ | |||
1 | #include "ntp.h" | ||
2 | #include <qpushbutton.h> | ||
3 | #include <qregexp.h> | ||
4 | #include <qtimer.h> | ||
5 | #include <qtable.h> | ||
6 | #include <qlabel.h> | ||
7 | #include <qsocket.h> | ||
8 | #include <qlineedit.h> | ||
9 | #include <qspinbox.h> | ||
10 | #include <qcheckbox.h> | ||
11 | #include <qtabwidget.h> | ||
12 | #include <qlayout.h> | ||
13 | #include <qmessagebox.h> | ||
14 | #include <qmultilineedit.h> | ||
15 | #include <opie/oprocess.h> | ||
16 | #include <qpe/qpeapplication.h> | ||
17 | #include <qpe/config.h> | ||
18 | #include <qpe/global.h> | ||
19 | #include <qpe/timeconversion.h> | ||
20 | #include <qpe/tzselect.h> | ||
21 | #include <qpe/timestring.h> | ||
22 | #include <qpe/qpedialog.h> | ||
23 | #include <qpe/datebookdb.h> | ||
24 | #include <qpe/datebookmonth.h> | ||
25 | #include <qpe/qcopenvelope_qws.h> | ||
26 | #include <sys/time.h> | ||
27 | #include <time.h> | ||
28 | #include <stdlib.h> | ||
29 | |||
30 | |||
31 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | ||
32 | : SetDateTime( parent, name, fl ) | ||
33 | { | ||
34 | _interactive = false; | ||
35 | Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File); | ||
36 | ntpSrvs.setGroup("servers"); | ||
37 | int srvCount = ntpSrvs.readNumEntry("count", 0 ); | ||
38 | for (int i = 0; i < srvCount; i++) | ||
39 | { | ||
40 | ntpSrvs.setGroup(QString::number(i)); | ||
41 | ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") ); | ||
42 | } | ||
43 | if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de")); | ||
44 | |||
45 | Config cfg("ntp",Config::User); | ||
46 | cfg.setGroup("settings"); | ||
47 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",720) ); | ||
48 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",1440) ); | ||
49 | ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); | ||
50 | |||
51 | //make tab order | ||
52 | |||
53 | TabWidgetMain->removePage( tabMain ); | ||
54 | TabWidgetMain->removePage( tabManualSetTime ); | ||
55 | TabWidgetMain->removePage( TabSettings ); | ||
56 | TabWidgetMain->removePage( tabPredict ); | ||
57 | TabWidgetMain->removePage( tabNtp ); | ||
58 | |||
59 | TabWidgetMain->insertTab( tabMain, tr( "Main" ) ); | ||
60 | TabWidgetMain->insertTab( TabSettings, tr( "Settings" ) ); | ||
61 | TabWidgetMain->insertTab( tabPredict, tr( "Predict" ) ); | ||
62 | TabWidgetMain->insertTab( tabNtp, tr( "NTP" ) ); | ||
63 | NtpBaseLayout->addWidget( TabWidgetMain, 0, 0 ); | ||
64 | |||
65 | |||
66 | |||
67 | bool advMode = cfg.readBoolEntry("advancedFeatures", false ); | ||
68 | showAdvancedFeatures(advMode); | ||
69 | CheckBoxAdvSettings->setChecked( advMode ); | ||
70 | connect( CheckBoxAdvSettings, SIGNAL( toggled( bool ) ), | ||
71 | SLOT( showAdvancedFeatures( bool ) ) ); | ||
72 | |||
73 | makeChannel(); | ||
74 | |||
75 | ntpTimer = new QTimer(this); | ||
76 | |||
77 | ntpProcess = new OProcess( ); | ||
78 | connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), | ||
79 | SLOT(slotNtpDelayChanged(int)) ); | ||
80 | |||
81 | ntpSock = new QSocket( this ); | ||
82 | connect( ntpSock, SIGNAL( error(int) ),SLOT(slotCheckNtp(int)) ); | ||
83 | slotProbeNtpServer(); | ||
84 | |||
85 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), | ||
86 | this, SLOT(getNtpOutput(OProcess*,char*,int))); | ||
87 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), | ||
88 | this, SLOT(ntpFinished(OProcess*))); | ||
89 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotButtonRunNtp())); | ||
90 | connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); | ||
91 | connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime())); | ||
92 | slotCheckNtp(-1); | ||
93 | readLookups(); | ||
94 | } | ||
95 | |||
96 | Ntp::~Ntp() | ||
97 | { | ||
98 | delete ntpProcess; | ||
99 | } | ||
100 | |||
101 | void Ntp::saveConfig(){ | ||
102 | int srvCount = ComboNtpSrv->count(); | ||
103 | bool serversChanged = true; | ||
104 | int curSrv = ComboNtpSrv->currentItem(); | ||
105 | QString edit = ComboNtpSrv->currentText(); | ||
106 | for (int i = 0; i < srvCount; i++){ | ||
107 | if ( edit == ComboNtpSrv->text(i)) serversChanged = false; | ||
108 | } | ||
109 | if (serversChanged){ | ||
110 | Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File); | ||
111 | ntpSrvs.setGroup("servers"); | ||
112 | ntpSrvs.writeEntry("count", ++srvCount); | ||
113 | ntpSrvs.setGroup("0"); | ||
114 | ntpSrvs.writeEntry( "name", edit ); | ||
115 | curSrv = 0; | ||
116 | for (int i = 1; i < srvCount; i++){ | ||
117 | qDebug("ntpSrvs[%i/%i]=%s",i,srvCount,ComboNtpSrv->text(i).latin1()); | ||
118 | ntpSrvs.setGroup(QString::number(i)); | ||
119 | ntpSrvs.writeEntry( "name", ComboNtpSrv->text(i-1) ); | ||
120 | } | ||
121 | } | ||
122 | Config cfg("ntp",Config::User); | ||
123 | cfg.setGroup("settings"); | ||
124 | cfg.writeEntry("ntpServer", curSrv ); | ||
125 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); | ||
126 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); | ||
127 | cfg.writeEntry( "advancedFeatures", CheckBoxAdvSettings->isChecked() ); | ||
128 | } | ||
129 | |||
130 | bool Ntp::ntpDelayElapsed() | ||
131 | { | ||
132 | Config cfg("ntp",Config::User); | ||
133 | cfg.setGroup("lookups"); | ||
134 | _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | ||
135 | if (_lookupDiff < 0) return true; | ||
136 | int i =_lookupDiff - (SpinBoxNtpDelay->value()*60); | ||
137 | return i > -60; | ||
138 | } | ||
139 | |||
140 | QString Ntp::getNtpServer() | ||
141 | { | ||
142 | return ComboNtpSrv->currentText(); | ||
143 | } | ||
144 | |||
145 | void Ntp::slotButtonRunNtp() | ||
146 | { | ||
147 | _interactive = true; | ||
148 | slotRunNtp(); | ||
149 | } | ||
150 | |||
151 | void Ntp::slotTimerRunNtp() | ||
152 | { | ||
153 | _interactive = false; | ||
154 | slotRunNtp(); | ||
155 | } | ||
156 | |||
157 | |||
158 | void Ntp::slotRunNtp() | ||
159 | { | ||
160 | if ( !ntpDelayElapsed() && CheckBoxAdvSettings->isChecked() ) | ||
161 | { | ||
162 | switch ( | ||
163 | QMessageBox::warning(this, tr("Run NTP?"), | ||
164 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ | ||
165 | QString::number(_lookupDiff/60)+tr(" minutes elapsed since last lookup.")+ | ||
166 | "<br>"+tr("Rerun NTP?"), | ||
167 | QMessageBox::Ok,QMessageBox::Cancel) | ||
168 | ) { | ||
169 | case QMessageBox::Ok: break; | ||
170 | case QMessageBox::Cancel: return; | ||
171 | default: return; | ||
172 | } | ||
173 | } | ||
174 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); | ||
175 | ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() ); | ||
176 | |||
177 | ntpProcess->clearArguments(); | ||
178 | *ntpProcess << "ntpdate" << getNtpServer(); | ||
179 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); | ||
180 | if ( !ret ) { | ||
181 | QMessageBox::critical(this, tr("ntp error"), | ||
182 | tr("Error while getting time form network!")); | ||
183 | qDebug("Error while executing ntpdate"); | ||
184 | ntpOutPut( tr("Error while executing ntpdate")); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | ||
189 | { | ||
190 | if (! proc ) qDebug("Ntp::getNtpOutput OProcess is null"); | ||
191 | QString lineStr, lineStrOld; | ||
192 | lineStr = buffer; | ||
193 | lineStr=lineStr.left(buflen); | ||
194 | if (lineStr!=lineStrOld) | ||
195 | { | ||
196 | ntpOutPut(lineStr); | ||
197 | _ntpOutput += lineStr; | ||
198 | } | ||
199 | lineStrOld = lineStr; | ||
200 | } | ||
201 | |||
202 | void Ntp::ntpFinished(OProcess *p) | ||
203 | { | ||
204 | qDebug("p->exitStatus() %i",p->exitStatus()); | ||
205 | if (p->exitStatus()!=0 || !p->normalExit()) | ||
206 | { | ||
207 | if ( isVisible() && _interactive ){ | ||
208 | QMessageBox::critical(this, tr("ntp error"),tr("Error while getting time form\n server")+getNtpServer()+"\n"+_ntpOutput ); | ||
209 | } | ||
210 | // slotCheckNtp(-1); | ||
211 | return; | ||
212 | } | ||
213 | |||
214 | Config cfg("ntp",Config::User); | ||
215 | cfg.setGroup("lookups"); | ||
216 | int lastLookup = cfg.readNumEntry("time",0); | ||
217 | int lookupCount = cfg.readNumEntry("count",0); | ||
218 | bool lastNtp = cfg.readBoolEntry("lastNtp",false); | ||
219 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
220 | cfg.writeEntry("time", time); | ||
221 | |||
222 | float timeShift = getTimeShift(); | ||
223 | if (timeShift == 0.0) return; | ||
224 | int secsSinceLast = time - lastLookup; | ||
225 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); | ||
226 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); | ||
227 | |||
228 | dateButton->setDate( QDate::currentDate() ); | ||
229 | timeButton->setTime( QDateTime::currentDateTime() ); | ||
230 | |||
231 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) | ||
232 | { | ||
233 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | ||
234 | lookupCount++; | ||
235 | _shiftPerSec = timeShift / secsSinceLast; | ||
236 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); | ||
237 | cfg.writeEntry("secsSinceLast",secsSinceLast); | ||
238 | cfg.writeEntry("timeShift",QString::number(timeShift)); | ||
239 | cfg.setGroup("lookups"); | ||
240 | cfg.writeEntry("count",lookupCount); | ||
241 | cfg.writeEntry("lastNtp",true); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | |||
246 | float Ntp::getTimeShift() | ||
247 | { | ||
248 | QString _offset = "offset"; | ||
249 | QString _sec = "sec"; | ||
250 | QRegExp _reOffset = QRegExp(_offset); | ||
251 | QRegExp _reEndOffset = QRegExp(_sec); | ||
252 | int posOffset = _reOffset.match( _ntpOutput ); | ||
253 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); | ||
254 | posOffset += _offset.length() + 1; | ||
255 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); | ||
256 | qDebug("%s", _ntpOutput.latin1()); | ||
257 | qDebug("diff = >%s<",diff.latin1()); | ||
258 | return diff.toFloat(); | ||
259 | } | ||
260 | |||
261 | void Ntp::readLookups() | ||
262 | { | ||
263 | Config cfg("ntp",Config::User); | ||
264 | cfg.setGroup("lookups"); | ||
265 | int lookupCount = cfg.readNumEntry("count",0); | ||
266 | float last, shift, shiftPerSec; | ||
267 | qDebug("lookupCount = %i",lookupCount); | ||
268 | TableLookups->setNumCols( 3 ); | ||
269 | TableLookups->setNumRows( lookupCount); | ||
270 | TableLookups->horizontalHeader()->setLabel(1,tr("last [h]")); | ||
271 | TableLookups->horizontalHeader()->setLabel(2,tr("offset [s]")); | ||
272 | TableLookups->horizontalHeader()->setLabel(0,tr("shift [s/h]")); | ||
273 | int cw = TableLookups->width()/4; | ||
274 | qDebug("column width %i",cw); | ||
275 | cw = 50; | ||
276 | TableLookups->setColumnWidth( 0, cw+30 ); | ||
277 | TableLookups->setColumnWidth( 1, cw ); | ||
278 | TableLookups->setColumnWidth( 2, cw ); | ||
279 | TableLookups->sortColumn(0, false, true ); | ||
280 | // TableLookups->setSorting( true ); | ||
281 | _shiftPerSec = 0; | ||
282 | for (int i=0; i < lookupCount; i++) | ||
283 | { | ||
284 | cfg.setGroup("lookup_"+QString::number(i)); | ||
285 | last = cfg.readEntry("secsSinceLast",0).toFloat(); | ||
286 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); | ||
287 | shiftPerSec = shift / last; | ||
288 | qDebug("%i shift %f",i,shiftPerSec); | ||
289 | _shiftPerSec += shiftPerSec; | ||
290 | TableLookups->setText( i,0,QString::number(shiftPerSec*60*60)); | ||
291 | TableLookups->setText( i,2,QString::number(shift)); | ||
292 | TableLookups->setText( i,1,QString::number(last/(60*60))); | ||
293 | } | ||
294 | _shiftPerSec /= lookupCount; | ||
295 | TextLabelShift->setText(QString::number(_shiftPerSec*60*60)+tr(" s/h")); | ||
296 | } | ||
297 | |||
298 | void Ntp::preditctTime() | ||
299 | { | ||
300 | Config cfg("ntp",Config::User); | ||
301 | cfg.setGroup("lookups"); | ||
302 | int lastTime = cfg.readNumEntry("time",0); | ||
303 | cfg.writeEntry("lastNtp",true); | ||
304 | setenv( "TZ", tz->currentZone(), 1 ); | ||
305 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
306 | int corr = int((now - lastTime) * _shiftPerSec); | ||
307 | TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); | ||
308 | predictedTime = QDateTime::currentDateTime().addSecs(corr); | ||
309 | TextLabelPredTime->setText(predictedTime.toString()); | ||
310 | // TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); | ||
311 | } | ||
312 | |||
313 | void Ntp::setPredictTime() | ||
314 | { | ||
315 | qDebug("Ntp::setPredictTime"); | ||
316 | preditctTime(); | ||
317 | timeButton->setTime( predictedTime ); | ||
318 | } | ||
319 | |||
320 | void Ntp::slotCheckNtp(int i) | ||
321 | { | ||
322 | qDebug(" Ntp::slotCheckNtp(%i)",i); | ||
323 | if (i == 0) | ||
324 | { | ||
325 | // TextLabelMainPredTime->hide(); | ||
326 | ButtonSetTime->setText( tr("Get time from network") ); | ||
327 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotButtonRunNtp()) ); | ||
328 | if ( ntpDelayElapsed() ) | ||
329 | { | ||
330 | slotRunNtp(); | ||
331 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); | ||
332 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotTimerRunNtp()) ); | ||
333 | }else{ | ||
334 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotTimerRunNtp()) ); | ||
335 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | ||
336 | } | ||
337 | }else{ | ||
338 | preditctTime(); | ||
339 | ButtonSetTime->setText( tr("Set predicted time: ")+predictedTime.toString() ); | ||
340 | if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer()); | ||
341 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); | ||
342 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | ||
343 | } | ||
344 | } | ||
345 | |||
346 | void Ntp::slotProbeNtpServer() | ||
347 | { | ||
348 | ntpSock->connectToHost( getNtpServer() ,123); | ||
349 | } | ||
350 | |||
351 | void Ntp::slotNtpDelayChanged(int delay) | ||
352 | { | ||
353 | ntpTimer->changeInterval( delay*1000*60 ); | ||
354 | } | ||
355 | |||
356 | void Ntp::ntpOutPut(QString out) | ||
357 | { | ||
358 | |||
359 | MultiLineEditntpOutPut->append(out); | ||
360 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); | ||
361 | } | ||
362 | |||
363 | |||
364 | void Ntp::makeChannel() | ||
365 | { | ||
366 | channel = new QCopChannel( "QPE/Application/netsystemtime", this ); | ||
367 | connect( channel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
368 | this, SLOT(receive(const QCString&, const QByteArray&)) ); | ||
369 | } | ||
370 | |||
371 | |||
372 | |||
373 | void Ntp::receive(const QCString &msg, const QByteArray &arg) | ||
374 | { | ||
375 | qDebug("QCop(Ntp) "+msg+" "+QCString(arg)); | ||
376 | if ( msg == "ntpLookup(QString)" ) | ||
377 | { | ||
378 | _interactive = false; | ||
379 | slotRunNtp(); | ||
380 | } | ||
381 | if ( msg == "setPredictedTime(QString)" ) | ||
382 | { | ||
383 | setPredictTime(); | ||
384 | }else{ | ||
385 | qDebug("Ntp::receive: Huh what do ya want"); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | void Ntp::setDocument(const QString &fileName) | ||
390 | { | ||
391 | qDebug("Ntp::setDocument( %s )",fileName.latin1()); | ||
392 | } | ||
393 | |||
394 | void Ntp::showAdvancedFeatures(bool advMode) | ||
395 | { | ||
396 | if (advMode) { | ||
397 | |||
398 | if ( tabNtp->isVisible() ) { | ||
399 | TabWidgetMain->addTab( tabPredict, tr( "Predict" ) ); | ||
400 | TabWidgetMain->addTab( tabNtp, tr( "NTP" ) ); | ||
401 | } | ||
402 | TextLabel1_2_2->show(); | ||
403 | TextLabel2_3->show(); | ||
404 | TextLabel3_3_2->show(); | ||
405 | TextLabel1_2->show(); | ||
406 | SpinBoxMinLookupDelay->show(); | ||
407 | TextLabel2->show(); | ||
408 | TextLabel3_3->show(); | ||
409 | SpinBoxNtpDelay->show(); | ||
410 | Line1->show(); | ||
411 | }else{ | ||
412 | TabWidgetMain->removePage( tabPredict ); | ||
413 | TabWidgetMain->removePage( tabNtp ); | ||
414 | TextLabel1_2_2->hide(); | ||
415 | TextLabel2_3->hide(); | ||
416 | TextLabel3_3_2->hide(); | ||
417 | TextLabel1_2->hide(); | ||
418 | SpinBoxMinLookupDelay->hide(); | ||
419 | TextLabel2->hide(); | ||
420 | TextLabel3_3->hide(); | ||
421 | SpinBoxNtpDelay->hide(); | ||
422 | Line1->hide(); | ||
423 | }; | ||
424 | TabWidgetMain->show(); | ||
425 | } | ||
426 | |||
427 | |||
428 | void Ntp::accept( ){ | ||
429 | qDebug("saving"); | ||
430 | //SetTimeDate | ||
431 | commitTime(); | ||
432 | writeSettings(); | ||
433 | updateSystem(); | ||
434 | // Ntp | ||
435 | saveConfig(); | ||
436 | qApp->quit(); | ||
437 | } | ||
438 | |||
439 | void Ntp::reject( ){ | ||
440 | qDebug("_oldTimeZone %s",_oldTimeZone.latin1()); | ||
441 | if (!_oldTimeZone.isEmpty()){ | ||
442 | qDebug("reverting timezone"); | ||
443 | tzChange(_oldTimeZone); | ||
444 | commitTime(); | ||
445 | } | ||
446 | } | ||