summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/ntp.cpp
Unidiff
Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp74
1 files changed, 65 insertions, 9 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index 52fc6be..364609d 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -1,67 +1,95 @@
1#include "ntp.h" 1#include "ntp.h"
2#include <qpushbutton.h> 2#include <qpushbutton.h>
3#include <qregexp.h> 3#include <qregexp.h>
4#include <qtimer.h>
4#include <qtable.h> 5#include <qtable.h>
5#include <qlabel.h> 6#include <qlabel.h>
6#include <qsocket.h> 7#include <qsocket.h>
7#include <qlineedit.h> 8#include <qlineedit.h>
8#include <qspinbox.h> 9#include <qspinbox.h>
9#include <qmessagebox.h> 10#include <qmessagebox.h>
10#include <qmultilineedit.h> 11#include <qmultilineedit.h>
11#include <opie/oprocess.h> 12#include <opie/oprocess.h>
12#include <qpe/config.h> 13#include <qpe/config.h>
13#include <qpe/global.h> 14#include <qpe/global.h>
14#include <qpe/timeconversion.h> 15#include <qpe/timeconversion.h>
15#include <qpe/tzselect.h> 16#include <qpe/tzselect.h>
16#include <qpe/timestring.h> 17#include <qpe/timestring.h>
17#include <qpe/qpedialog.h> 18#include <qpe/qpedialog.h>
18#include <sys/time.h> 19#include <sys/time.h>
19#include <time.h> 20#include <time.h>
20#include <stdlib.h> 21#include <stdlib.h>
21 22
22 23
23Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) 24Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
24 : SetDateTime( parent, name, fl ) 25 : SetDateTime( parent, name, fl )
25{ 26{
27
28 Config ntpSrvs("/etc/ntpservers",Config::File);
29 ntpSrvs.setGroup("servers");
30 int srvCount = ntpSrvs.readNumEntry("count", 0 );
31 for (int i = 0; i < srvCount; i++)
32 {
33 ntpSrvs.setGroup(QString::number(i));
34 ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") );
35 }
36 if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de"));
37
26 Config cfg("ntp",Config::User); 38 Config cfg("ntp",Config::User);
27 cfg.setGroup("settings"); 39 cfg.setGroup("settings");
28 // _maxOffset = cfg.readNumEntry("maxOffset",5); 40 // _maxOffset = cfg.readNumEntry("maxOffset",5);
29// _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); 41// _minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
30 SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",60) ); 42 SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",41) );
31 SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",60) ); 43 SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",42) );
32 LineEditNtpSrv->setText( cfg.readEntry("ntpServer", tr("time.fu-berlin.de")) ); 44 ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) );
45
46 ntpTimer = new QTimer(this);
47 ntpTimer->start(SpinBoxNtpDelay->value()*100);
48
33 ntpProcess = new OProcess( ); 49 ntpProcess = new OProcess( );
50 connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ),
51 SLOT(slotNtpDelayChanged(int)) );
34 52
35 QSocket *ntpSock = new QSocket( this ); 53 ntpSock = new QSocket( this );
36 ntpSock->connectToHost( LineEditNtpSrv->text() ,123);
37 connect( ntpSock, SIGNAL( error(int) ), 54 connect( ntpSock, SIGNAL( error(int) ),
38 SLOT(slotCheckNtp(int)) ); 55 SLOT(slotCheckNtp(int)) );
56 slotProbeNtpServer();
39 57
40 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), 58 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
41 this, SLOT(getNtpOutput(OProcess*,char*,int))); 59 this, SLOT(getNtpOutput(OProcess*,char*,int)));
42 connect ( ntpProcess, SIGNAL(processExited(OProcess*)), 60 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
43 this, SLOT(ntpFinished(OProcess*))); 61 this, SLOT(ntpFinished(OProcess*)));
44 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); 62 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
45 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); 63 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime()));
64 connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime()));
46 slotCheckNtp(-1); 65 slotCheckNtp(-1);
47// slotRunNtp();
48 readLookups(); 66 readLookups();
49} 67}
50 68
51Ntp::~Ntp() 69Ntp::~Ntp()
52{ 70{
71 delete ntpProcess;
72 Config ntpSrvs("/etc/ntpservers",Config::File);
73 ntpSrvs.setGroup("servers");
74 int srvCount = ComboNtpSrv->count();
75 ntpSrvs.writeEntry("count", srvCount);
76 for (int i = 0; i < srvCount; i++)
77 {
78 ntpSrvs.setGroup(QString::number(i));
79 ntpSrvs.writeEntry( "name", ComboNtpSrv->text(i) );
80 }
53 Config cfg("ntp",Config::User); 81 Config cfg("ntp",Config::User);
54 cfg.setGroup("settings"); 82 cfg.setGroup("settings");
55 cfg.writeEntry("ntpServer", LineEditNtpSrv->text()); 83 cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem());
56 cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); 84 cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() );
57 cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); 85 cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() );
58} 86}
59 87
60 88
61void Ntp::slotRunNtp() 89void Ntp::slotRunNtp()
62{ 90{
63 Config cfg("ntp",Config::User); 91 Config cfg("ntp",Config::User);
64 cfg.setGroup("lookups"); 92 cfg.setGroup("lookups");
65 int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); 93 int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0);
66 94
67 if ( lookupDiff < SpinBoxNtpDelay->value()*60 ) 95 if ( lookupDiff < SpinBoxNtpDelay->value()*60 )
@@ -70,26 +98,27 @@ void Ntp::slotRunNtp()
70 QMessageBox::warning(this, tr("Run ntp?"), 98 QMessageBox::warning(this, tr("Run ntp?"),
71 tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ 99 tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+
72 QString::number(lookupDiff%60)+tr(" minutes elapsed since last loopup.")+ 100 QString::number(lookupDiff%60)+tr(" minutes elapsed since last loopup.")+
73 "<br>"+tr("Rerun ntp?"), 101 "<br>"+tr("Rerun ntp?"),
74 QMessageBox::Ok,QMessageBox::Cancel) 102 QMessageBox::Ok,QMessageBox::Cancel)
75 ) { 103 ) {
76 case QMessageBox::Ok: break; 104 case QMessageBox::Ok: break;
77 case QMessageBox::Cancel: return; 105 case QMessageBox::Cancel: return;
78 default: return; 106 default: return;
79 } 107 }
80 } 108 }
81 TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); 109 TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
110 MultiLineEditntpOutPut->append( "\n"+tr("Running:")+"\nntpdate "+ ComboNtpSrv->currentText()+"\n");
82 ntpProcess->clearArguments(); 111 ntpProcess->clearArguments();
83 *ntpProcess << "ntpdate" << LineEditNtpSrv->text(); 112 *ntpProcess << "ntpdate" << ComboNtpSrv->currentText();
84 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); 113 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
85 if ( !ret ) { 114 if ( !ret ) {
86 qDebug("Error while executing ntp"); 115 qDebug("Error while executing ntp");
87 } 116 }
88} 117}
89 118
90void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) 119void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
91{ 120{
92 QString lineStr, lineStrOld; 121 QString lineStr, lineStrOld;
93 lineStr = buffer; 122 lineStr = buffer;
94 lineStr=lineStr.left(buflen); 123 lineStr=lineStr.left(buflen);
95 if (lineStr!=lineStrOld) 124 if (lineStr!=lineStrOld)
@@ -181,36 +210,63 @@ void Ntp::readLookups()
181 TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); 210 TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds"));
182} 211}
183 212
184void Ntp::preditctTime() 213void Ntp::preditctTime()
185{ 214{
186 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); 215 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
187 Config cfg("ntp",Config::User); 216 Config cfg("ntp",Config::User);
188 cfg.setGroup("lookups"); 217 cfg.setGroup("lookups");
189 int lastTime = cfg.readNumEntry("time",0); 218 int lastTime = cfg.readNumEntry("time",0);
190 setenv( "TZ", tz->currentZone(), 1 ); 219 setenv( "TZ", tz->currentZone(), 1 );
191 int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); 220 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
192 int corr = int((now - lastTime) * _shiftPerSec); 221 int corr = int((now - lastTime) * _shiftPerSec);
222 TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds"));
193 predictedTime = QDateTime::currentDateTime().addSecs(corr); 223 predictedTime = QDateTime::currentDateTime().addSecs(corr);
194 TextLabelPredTime->setText(predictedTime.toString()); 224 TextLabelPredTime->setText(predictedTime.toString());
195 TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); 225 TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>");
196} 226}
197 227
198void Ntp::setPredictTime() 228void Ntp::setPredictTime()
199{ 229{
200 preditctTime(); 230 preditctTime();
201 setTime( predictedTime ); 231 setTime( predictedTime );
202} 232}
203 233
204void Ntp::slotCheckNtp(int i) 234void Ntp::slotCheckNtp(int i)
205{ 235{
206 if (i == 0) 236 if (i == 0)
207 { 237 {
208 TextLabelMainPredTime->hide(); 238 TextLabelMainPredTime->hide();
209 ButtonSetTime->setText( tr("Get time from network") ); 239 ButtonSetTime->setText( tr("Get time from network") );
210 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); 240 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) );
241 Config cfg("ntp",Config::User);
242 cfg.setGroup("lookups");
243 int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0);
244 if ( lookupDiff > SpinBoxNtpDelay->value()*60 )
245 {
246 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) );
247 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) );
248 }else{
249 disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) );
250 connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
251 }
211 }else{ 252 }else{
212 preditctTime(); 253 preditctTime();
213 ButtonSetTime->setText( tr("Set predicted time") ); 254 ButtonSetTime->setText( tr("Set predicted time") );
214 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); 255 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) );
215 }; 256 connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
257 }
258}
259
260void Ntp::slotProbeNtpServer()
261{
262 Config cfg("ntp",Config::User);
263 cfg.setGroup("lookups");
264 int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0);
265 if ( lookupDiff > SpinBoxNtpDelay->value()*60 )
266 ntpSock->connectToHost( ComboNtpSrv->currentText() ,123);
267}
268
269void Ntp::slotNtpDelayChanged(int delay)
270{
271 ntpTimer->changeInterval( delay*100 );
216} 272}