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,6 +1,7 @@
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>
@@ -23,19 +24,36 @@
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)));
@@ -43,16 +61,26 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
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}
@@ -79,8 +107,9 @@ void Ntp::slotRunNtp()
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");
@@ -190,6 +219,7 @@ void Ntp::preditctTime()
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>");
@@ -208,9 +238,35 @@ void Ntp::slotCheckNtp(int i)
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}