From 47a1461b3a250e079ab99a1075974ccea909216a Mon Sep 17 00:00:00 2001 From: tille Date: Wed, 26 Jun 2002 14:27:43 +0000 Subject: inlc. timer for ntp update and ntpservers combo --- 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 @@ #include "ntp.h" #include #include +#include #include #include #include @@ -23,19 +24,36 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) : SetDateTime( parent, name, fl ) { + + Config ntpSrvs("/etc/ntpservers",Config::File); + ntpSrvs.setGroup("servers"); + int srvCount = ntpSrvs.readNumEntry("count", 0 ); + for (int i = 0; i < srvCount; i++) + { + ntpSrvs.setGroup(QString::number(i)); + ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") ); + } + if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de")); + Config cfg("ntp",Config::User); cfg.setGroup("settings"); // _maxOffset = cfg.readNumEntry("maxOffset",5); // _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); - SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",60) ); - SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",60) ); - LineEditNtpSrv->setText( cfg.readEntry("ntpServer", tr("time.fu-berlin.de")) ); + SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",41) ); + SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",42) ); + ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); + + ntpTimer = new QTimer(this); + ntpTimer->start(SpinBoxNtpDelay->value()*100); + ntpProcess = new OProcess( ); + connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), + SLOT(slotNtpDelayChanged(int)) ); - QSocket *ntpSock = new QSocket( this ); - ntpSock->connectToHost( LineEditNtpSrv->text() ,123); + ntpSock = new QSocket( this ); connect( ntpSock, SIGNAL( error(int) ), SLOT(slotCheckNtp(int)) ); + slotProbeNtpServer(); connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), this, SLOT(getNtpOutput(OProcess*,char*,int))); @@ -43,16 +61,26 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) this, SLOT(ntpFinished(OProcess*))); connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); + connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime())); slotCheckNtp(-1); -// slotRunNtp(); readLookups(); } Ntp::~Ntp() { + delete ntpProcess; + Config ntpSrvs("/etc/ntpservers",Config::File); + ntpSrvs.setGroup("servers"); + int srvCount = ComboNtpSrv->count(); + ntpSrvs.writeEntry("count", srvCount); + for (int i = 0; i < srvCount; i++) + { + ntpSrvs.setGroup(QString::number(i)); + ntpSrvs.writeEntry( "name", ComboNtpSrv->text(i) ); + } Config cfg("ntp",Config::User); cfg.setGroup("settings"); - cfg.writeEntry("ntpServer", LineEditNtpSrv->text()); + cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem()); cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); } @@ -79,8 +107,9 @@ void Ntp::slotRunNtp() } } TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); + MultiLineEditntpOutPut->append( "\n"+tr("Running:")+"\nntpdate "+ ComboNtpSrv->currentText()+"\n"); ntpProcess->clearArguments(); - *ntpProcess << "ntpdate" << LineEditNtpSrv->text(); + *ntpProcess << "ntpdate" << ComboNtpSrv->currentText(); bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); if ( !ret ) { qDebug("Error while executing ntp"); @@ -190,6 +219,7 @@ void Ntp::preditctTime() setenv( "TZ", tz->currentZone(), 1 ); int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); int corr = int((now - lastTime) * _shiftPerSec); + TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); predictedTime = QDateTime::currentDateTime().addSecs(corr); TextLabelPredTime->setText(predictedTime.toString()); TextLabelMainPredTime->setText(tr("Predicted time:")+"
"+predictedTime.toString()+""); @@ -208,9 +238,35 @@ void Ntp::slotCheckNtp(int i) TextLabelMainPredTime->hide(); ButtonSetTime->setText( tr("Get time from network") ); connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); + Config cfg("ntp",Config::User); + cfg.setGroup("lookups"); + int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); + if ( lookupDiff > SpinBoxNtpDelay->value()*60 ) + { + disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); + connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); + }else{ + disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); + connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); + } }else{ preditctTime(); ButtonSetTime->setText( tr("Set predicted time") ); connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); - }; + connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); + } +} + +void Ntp::slotProbeNtpServer() +{ + Config cfg("ntp",Config::User); + cfg.setGroup("lookups"); + int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); + if ( lookupDiff > SpinBoxNtpDelay->value()*60 ) + ntpSock->connectToHost( ComboNtpSrv->currentText() ,123); +} + +void Ntp::slotNtpDelayChanged(int delay) +{ + ntpTimer->changeInterval( delay*100 ); } diff --git a/noncore/settings/netsystemtime/ntp.h b/noncore/settings/netsystemtime/ntp.h index 284ae27..7cc309f 100644 --- a/noncore/settings/netsystemtime/ntp.h +++ b/noncore/settings/netsystemtime/ntp.h @@ -5,7 +5,8 @@ class OProcess; class QString; - +class QTimer; +class QSocket; class Ntp : public SetDateTime { @@ -23,6 +24,8 @@ private: QString _ntpOutput; float _shiftPerSec; OProcess *ntpProcess; + QTimer *ntpTimer; + QSocket *ntpSock; float getTimeShift(); void readLookups(); @@ -33,6 +36,8 @@ private slots: void preditctTime(); void slotCheckNtp(int); void setPredictTime(); + void slotProbeNtpServer(); + void slotNtpDelayChanged(int); }; #endif diff --git a/noncore/settings/netsystemtime/ntpbase.ui b/noncore/settings/netsystemtime/ntpbase.ui index 8d32849..4e10fd2 100644 --- a/noncore/settings/netsystemtime/ntpbase.ui +++ b/noncore/settings/netsystemtime/ntpbase.ui @@ -11,8 +11,8 @@ 0 0 - 330 - 419 + 331 + 426 @@ -109,7 +109,7 @@ text - get time from network + Get time from network @@ -287,11 +287,11 @@ margin - 2 + 5 spacing - 2 + 6 QTable @@ -308,22 +308,11 @@ 2 - - QPushButton - - name - PushButtonPredict - - - text - predict time - - QLayoutWidget name - Layout5 + Layout9 @@ -345,17 +334,39 @@ nan + + QLabel + + name + TextLabel4 + + + text + Esimated Shift: + + QLabel name - TextLabelPredTime + TextLabelEstimatedShift text nan + + QLabel + + name + TextLabel3_2 + + + text + Predicted Time: + + QLabel @@ -367,19 +378,58 @@ Mean shift: - + QLabel name - TextLabel3_2 + TextLabelPredTime text - Pred. Time: + nan + + QLayoutWidget + + name + Layout11 + + + + margin + 0 + + + spacing + 6 + + + QPushButton + + name + PushButtonSetPredTime + + + text + Set predicted time + + + + QPushButton + + name + PushButtonPredict + + + text + Predict time + + + + @@ -486,10 +536,14 @@ - QLineEdit + QComboBox name - LineEditNtpSrv + ComboNtpSrv + + + editable + true @@ -609,8 +663,15 @@ TextLabel1_2_2 + sizePolicy + + 1 + 1 + + + text - Wait for + Wait for @@ -632,7 +693,7 @@ text - NTP syncronises the clock with the network + NTP tries to syncronises the clock with the network. alignment -- cgit v0.9.0.2