summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp228
1 files changed, 115 insertions, 113 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index f60e38d..40805a0 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -28,22 +28,22 @@
28 28
29 29
30Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) 30Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
31 : SetDateTime( parent, name, fl ) 31 : SetDateTime( parent, name, fl )
32{ 32{
33 Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File); 33 Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File);
34 ntpSrvs.setGroup("servers"); 34 ntpSrvs.setGroup("servers");
35 int srvCount = ntpSrvs.readNumEntry("count", 0 ); 35 int srvCount = ntpSrvs.readNumEntry("count", 0 );
36 for (int i = 0; i < srvCount; i++) 36 for (int i = 0; i < srvCount; i++)
37 { 37 {
38 ntpSrvs.setGroup(QString::number(i)); 38 ntpSrvs.setGroup(QString::number(i));
39 ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") ); 39 ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") );
40 } 40 }
41 if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de")); 41 if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de"));
42 42
43 Config cfg("ntp",Config::User); 43 Config cfg("ntp",Config::User);
44 cfg.setGroup("settings"); 44 cfg.setGroup("settings");
45 SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",720) ); 45 SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",720) );
46 SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",1440) ); 46 SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",1440) );
47 ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); 47 ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) );
48 48
49 bool advMode = cfg.readBoolEntry("advancedFeatures", false ); 49 bool advMode = cfg.readBoolEntry("advancedFeatures", false );
@@ -56,19 +56,19 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
56 56
57 ntpTimer = new QTimer(this); 57 ntpTimer = new QTimer(this);
58 58
59 ntpProcess = new OProcess( ); 59 ntpProcess = new OProcess( );
60 connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), 60 connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ),
61 SLOT(slotNtpDelayChanged(int)) ); 61 SLOT(slotNtpDelayChanged(int)) );
62 62
63 ntpSock = new QSocket( this ); 63 ntpSock = new QSocket( this );
64 connect( ntpSock, SIGNAL( error(int) ), 64 connect( ntpSock, SIGNAL( error(int) ),
65 SLOT(slotCheckNtp(int)) ); 65 SLOT(slotCheckNtp(int)) );
66 slotProbeNtpServer(); 66 slotProbeNtpServer();
67 67
68 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), 68 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
69 this, SLOT(getNtpOutput(OProcess*,char*,int))); 69 this, SLOT(getNtpOutput(OProcess*,char*,int)));
70 connect ( ntpProcess, SIGNAL(processExited(OProcess*)), 70 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
71 this, SLOT(ntpFinished(OProcess*))); 71 this, SLOT(ntpFinished(OProcess*)));
72 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); 72 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
73 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); 73 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime()));
74 connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime())); 74 connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime()));
@@ -97,42 +97,44 @@ Ntp::~Ntp()
97 97
98bool Ntp::ntpDelayElapsed() 98bool Ntp::ntpDelayElapsed()
99{ 99{
100 Config cfg("ntp",Config::User); 100 Config cfg("ntp",Config::User);
101 cfg.setGroup("lookups"); 101 cfg.setGroup("lookups");
102 _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); 102 _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0);
103 return (_lookupDiff - (SpinBoxNtpDelay->value()*60)) > -60; 103 if (_lookupDiff < 0) return true;
104 int i =_lookupDiff - (SpinBoxNtpDelay->value()*60);
105 return i > -60;
104} 106}
105 107
106QString Ntp::getNtpServer() 108QString Ntp::getNtpServer()
107{ 109{
108 return ComboNtpSrv->currentText(); 110 return ComboNtpSrv->currentText();
109} 111}
110 112
111void Ntp::slotRunNtp() 113void Ntp::slotRunNtp()
112{ 114{
113 if ( !ntpDelayElapsed() ) 115 if ( !ntpDelayElapsed() )
114 { 116 {
115 switch ( 117 switch (
116 QMessageBox::warning(this, tr("Run NTP?"), 118 QMessageBox::warning(this, tr("Run NTP?"),
117 tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ 119 tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+
118 QString::number(_lookupDiff/60)+tr(" minutes elapsed since last lookup.")+ 120 QString::number(_lookupDiff/60)+tr(" minutes elapsed since last lookup.")+
119 "<br>"+tr("Rerun NTP?"), 121 "<br>"+tr("Rerun NTP?"),
120 QMessageBox::Ok,QMessageBox::Cancel) 122 QMessageBox::Ok,QMessageBox::Cancel)
121 ) { 123 ) {
122 case QMessageBox::Ok: break; 124 case QMessageBox::Ok: break;
123 case QMessageBox::Cancel: return; 125 case QMessageBox::Cancel: return;
124 default: return; 126 default: return;
125 } 127 }
126 } 128 }
127 TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); 129 TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
128 ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() ); 130 ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() );
129 131
130 ntpProcess->clearArguments(); 132 ntpProcess->clearArguments();
131 *ntpProcess << "ntpdate" << getNtpServer(); 133 *ntpProcess << "ntpdate" << getNtpServer();
132 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); 134 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
133 if ( !ret ) { 135 if ( !ret ) {
134 qDebug("Error while executing ntpdate"); 136 qDebug("Error while executing ntpdate");
135 ntpOutPut( tr("Error while executing ntpdate")); 137 ntpOutPut( tr("Error while executing ntpdate"));
136 } 138 }
137} 139}
138 140
@@ -142,21 +144,21 @@ void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
142 lineStr = buffer; 144 lineStr = buffer;
143 lineStr=lineStr.left(buflen); 145 lineStr=lineStr.left(buflen);
144 if (lineStr!=lineStrOld) 146 if (lineStr!=lineStrOld)
145 { 147 {
146 ntpOutPut(lineStr); 148 ntpOutPut(lineStr);
147 _ntpOutput += lineStr; 149 _ntpOutput += lineStr;
148 } 150 }
149 lineStrOld = lineStr; 151 lineStrOld = lineStr;
150} 152}
151 153
152void Ntp::ntpFinished(OProcess *p) 154void Ntp::ntpFinished(OProcess *p)
153{ 155{
154 //qDebug("p->exitStatus() %i",p->exitStatus()); 156 //qDebug("p->exitStatus() %i",p->exitStatus());
155 if (p->exitStatus()!=0 || !p->normalExit()) 157 if (p->exitStatus()!=0 || !p->normalExit())
156 { 158 {
157 slotProbeNtpServer(); 159 slotProbeNtpServer();
158 return; 160 return;
159 } 161 }
160 162
161 Global::writeHWClock(); 163 Global::writeHWClock();
162 // since time has changed quickly load in the datebookdb 164 // since time has changed quickly load in the datebookdb
@@ -164,10 +166,10 @@ void Ntp::ntpFinished(OProcess *p)
164 // (example re-trigger alarms for when we travel back in time) 166 // (example re-trigger alarms for when we travel back in time)
165 DateBookDB db; 167 DateBookDB db;
166 168
167// QCopEnvelope timeApplet( "QPE/TaskBar", "reloadApplets()" ); 169 // QCopEnvelope timeApplet( "QPE/TaskBar", "reloadApplets()" );
168// timeApplet << ""; 170 // timeApplet << "";
169 171
170 Config cfg("ntp",Config::User); 172 Config cfg("ntp",Config::User);
171 cfg.setGroup("lookups"); 173 cfg.setGroup("lookups");
172 int lastLookup = cfg.readNumEntry("time",0); 174 int lastLookup = cfg.readNumEntry("time",0);
173 int lookupCount = cfg.readNumEntry("count",0); 175 int lookupCount = cfg.readNumEntry("count",0);
@@ -177,27 +179,27 @@ void Ntp::ntpFinished(OProcess *p)
177 179
178 float timeShift = getTimeShift(); 180 float timeShift = getTimeShift();
179 if (timeShift == 0.0) return; 181 if (timeShift == 0.0) return;
180 int secsSinceLast = time - lastLookup; 182 int secsSinceLast = time - lastLookup;
181 TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); 183 TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
182 TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); 184 TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds"));
183 if ( lastNtp && lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) 185 if ( lastNtp && lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value())
184 { 186 {
185 cfg.setGroup("lookup_"+QString::number(lookupCount)); 187 cfg.setGroup("lookup_"+QString::number(lookupCount));
186 lookupCount++; 188 lookupCount++;
187 _shiftPerSec = timeShift / secsSinceLast; 189 _shiftPerSec = timeShift / secsSinceLast;
188 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); 190 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);
189 cfg.writeEntry("secsSinceLast",secsSinceLast); 191 cfg.writeEntry("secsSinceLast",secsSinceLast);
190 cfg.writeEntry("timeShift",QString::number(timeShift)); 192 cfg.writeEntry("timeShift",QString::number(timeShift));
191 cfg.setGroup("lookups"); 193 cfg.setGroup("lookups");
192 cfg.writeEntry("count",lookupCount); 194 cfg.writeEntry("count",lookupCount);
193 cfg.writeEntry("lastNtp",true); 195 cfg.writeEntry("lastNtp",true);
194 } 196 }
195} 197}
196 198
197 199
198float Ntp::getTimeShift() 200float Ntp::getTimeShift()
199{ 201{
200 QString _offset = "offset"; 202 QString _offset = "offset";
201 QString _sec = "sec"; 203 QString _sec = "sec";
202 QRegExp _reOffset = QRegExp(_offset); 204 QRegExp _reOffset = QRegExp(_offset);
203 QRegExp _reEndOffset = QRegExp(_sec); 205 QRegExp _reEndOffset = QRegExp(_sec);
@@ -212,7 +214,7 @@ float Ntp::getTimeShift()
212 214
213void Ntp::readLookups() 215void Ntp::readLookups()
214{ 216{
215 Config cfg("ntp",Config::User); 217 Config cfg("ntp",Config::User);
216 cfg.setGroup("lookups"); 218 cfg.setGroup("lookups");
217 int lookupCount = cfg.readNumEntry("count",0); 219 int lookupCount = cfg.readNumEntry("count",0);
218 float last, shift, shiftPerSec; 220 float last, shift, shiftPerSec;
@@ -229,73 +231,73 @@ void Ntp::readLookups()
229 TableLookups->setColumnWidth( 1, cw ); 231 TableLookups->setColumnWidth( 1, cw );
230 TableLookups->setColumnWidth( 2, cw ); 232 TableLookups->setColumnWidth( 2, cw );
231 TableLookups->sortColumn(0, false, true ); 233 TableLookups->sortColumn(0, false, true );
232 // TableLookups->setSorting( true ); 234 // TableLookups->setSorting( true );
233 _shiftPerSec = 0; 235 _shiftPerSec = 0;
234 for (int i=0; i < lookupCount; i++) 236 for (int i=0; i < lookupCount; i++)
235 { 237 {
236 cfg.setGroup("lookup_"+QString::number(i)); 238 cfg.setGroup("lookup_"+QString::number(i));
237 last = cfg.readEntry("secsSinceLast",0).toFloat(); 239 last = cfg.readEntry("secsSinceLast",0).toFloat();
238 shift = QString(cfg.readEntry("timeShift",0)).toFloat(); 240 shift = QString(cfg.readEntry("timeShift",0)).toFloat();
239 shiftPerSec = shift / last; 241 shiftPerSec = shift / last;
240 qDebug("%i shift %f",i,shiftPerSec); 242 qDebug("%i shift %f",i,shiftPerSec);
241 _shiftPerSec += shiftPerSec; 243 _shiftPerSec += shiftPerSec;
242 TableLookups->setText( i,0,QString::number(shiftPerSec*60*60)); 244 TableLookups->setText( i,0,QString::number(shiftPerSec*60*60));
243 TableLookups->setText( i,2,QString::number(shift)); 245 TableLookups->setText( i,2,QString::number(shift));
244 TableLookups->setText( i,1,QString::number(last/(60*60))); 246 TableLookups->setText( i,1,QString::number(last/(60*60)));
245 } 247 }
246 _shiftPerSec /= lookupCount; 248 _shiftPerSec /= lookupCount;
247 TextLabelShift->setText(QString::number(_shiftPerSec*60*60)+tr(" s/h")); 249 TextLabelShift->setText(QString::number(_shiftPerSec*60*60)+tr(" s/h"));
248} 250}
249 251
250void Ntp::preditctTime() 252void Ntp::preditctTime()
251{ 253{
252 Config cfg("ntp",Config::User); 254 Config cfg("ntp",Config::User);
253 cfg.setGroup("lookups"); 255 cfg.setGroup("lookups");
254 int lastTime = cfg.readNumEntry("time",0); 256 int lastTime = cfg.readNumEntry("time",0);
255 cfg.writeEntry("lastNtp",true); 257 cfg.writeEntry("lastNtp",true);
256 setenv( "TZ", tz->currentZone(), 1 ); 258 setenv( "TZ", tz->currentZone(), 1 );
257 int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); 259 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
258 int corr = int((now - lastTime) * _shiftPerSec); 260 int corr = int((now - lastTime) * _shiftPerSec);
259 TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); 261 TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds"));
260 predictedTime = QDateTime::currentDateTime().addSecs(corr); 262 predictedTime = QDateTime::currentDateTime().addSecs(corr);
261 TextLabelPredTime->setText(predictedTime.toString()); 263 TextLabelPredTime->setText(predictedTime.toString());
262 TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); 264 TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>");
263} 265}
264 266
265void Ntp::setPredictTime() 267void Ntp::setPredictTime()
266{ 268{
267 preditctTime(); 269 preditctTime();
268 setTime( predictedTime ); 270 setTime( predictedTime );
269} 271}
270 272
271void Ntp::slotCheckNtp(int i) 273void Ntp::slotCheckNtp(int i)
272{ 274{
273 if (i == 0) 275 if (i == 0)
274 { 276 {
275 TextLabelMainPredTime->hide(); 277 TextLabelMainPredTime->hide();
276 ButtonSetTime->setText( tr("Get time from network") ); 278 ButtonSetTime->setText( tr("Get time from network") );
277 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); 279 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) );
278 if ( ntpDelayElapsed() ) 280 if ( ntpDelayElapsed() )
279 { 281 {
280 slotRunNtp(); 282 slotRunNtp();
281 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); 283 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) );
282 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); 284 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) );
283 }else{ 285 }else{
284 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); 286 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) );
285 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); 287 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
288 }
289 }else{
290 preditctTime();
291 ButtonSetTime->setText( tr("Set predicted time: ")+predictedTime.toString() );
292 if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer());
293 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) );
294 connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
286 } 295 }
287 }else{
288 preditctTime();
289 ButtonSetTime->setText( tr("Set predicted time: ")+predictedTime.toString() );
290 if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer());
291 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) );
292 connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
293 }
294} 296}
295 297
296void Ntp::slotProbeNtpServer() 298void Ntp::slotProbeNtpServer()
297{ 299{
298 ntpSock->connectToHost( getNtpServer() ,123); 300 ntpSock->connectToHost( getNtpServer() ,123);
299} 301}
300 302
301void Ntp::slotNtpDelayChanged(int delay) 303void Ntp::slotNtpDelayChanged(int delay)
@@ -305,33 +307,33 @@ void Ntp::slotNtpDelayChanged(int delay)
305 307
306void Ntp::ntpOutPut(QString out) 308void Ntp::ntpOutPut(QString out)
307{ 309{
308 MultiLineEditntpOutPut->append(out); 310 MultiLineEditntpOutPut->append(out);
309 MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); 311 MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE);
310} 312}
311 313
312 314
313void Ntp::makeChannel() 315void Ntp::makeChannel()
314 { 316 {
315 channel = new QCopChannel( "QPE/Application/netsystemtime", this ); 317 channel = new QCopChannel( "QPE/Application/netsystemtime", this );
316 connect( channel, SIGNAL(received(const QCString&, const QByteArray&)), 318 connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
317 this, SLOT(receive(const QCString&, const QByteArray&)) ); 319 this, SLOT(receive(const QCString&, const QByteArray&)) );
318} 320}
319 321
320 322
321 323
322void Ntp::receive(const QCString &msg, const QByteArray &arg) 324void Ntp::receive(const QCString &msg, const QByteArray &arg)
323{ 325{
324 qDebug("QCop(Ntp) "+msg+" "+QCString(arg)); 326 qDebug("QCop(Ntp) "+msg+" "+QCString(arg));
325 if ( msg == "ntpLookup(QString)" ) 327 if ( msg == "ntpLookup(QString)" )
326 { 328 {
327 slotRunNtp(); 329 slotRunNtp();
328 } 330 }
329 if ( msg == "setPredictedTime(QString)" ) 331 if ( msg == "setPredictedTime(QString)" )
330 { 332 {
331 setPredictTime(); 333 setPredictTime();
332 }else{ 334 }else{
333 qDebug("Ntp::receive: Huh what do ya want"); 335 qDebug("Ntp::receive: Huh what do ya want");
334 } 336 }
335} 337}
336 338
337void Ntp::setDocument(const QString &fileName) 339void Ntp::setDocument(const QString &fileName)