-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 74 | ||||
-rw-r--r-- | noncore/settings/netsystemtime/ntp.h | 7 | ||||
-rw-r--r-- | noncore/settings/netsystemtime/ntpbase.ui | 109 |
3 files changed, 156 insertions, 34 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,107 +1,136 @@ #include "ntp.h" #include <qpushbutton.h> #include <qregexp.h> +#include <qtimer.h> #include <qtable.h> #include <qlabel.h> #include <qsocket.h> #include <qlineedit.h> #include <qspinbox.h> #include <qmessagebox.h> #include <qmultilineedit.h> #include <opie/oprocess.h> #include <qpe/config.h> #include <qpe/global.h> #include <qpe/timeconversion.h> #include <qpe/tzselect.h> #include <qpe/timestring.h> #include <qpe/qpedialog.h> #include <sys/time.h> #include <time.h> #include <stdlib.h> 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))); connect ( ntpProcess, SIGNAL(processExited(OProcess*)), 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() ); } void Ntp::slotRunNtp() { Config cfg("ntp",Config::User); cfg.setGroup("lookups"); int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); if ( lookupDiff < SpinBoxNtpDelay->value()*60 ) { switch ( QMessageBox::warning(this, tr("Run ntp?"), tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ QString::number(lookupDiff%60)+tr(" minutes elapsed since last loopup.")+ "<br>"+tr("Rerun ntp?"), QMessageBox::Ok,QMessageBox::Cancel) ) { case QMessageBox::Ok: break; case QMessageBox::Cancel: return; default: return; } } 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"); } } void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) { QString lineStr, lineStrOld; lineStr = buffer; lineStr=lineStr.left(buflen); if (lineStr!=lineStrOld) { MultiLineEditntpOutPut->append(lineStr); _ntpOutput += lineStr; } lineStrOld = lineStr; } void Ntp::ntpFinished(OProcess*) { Config cfg("ntp",Config::User); cfg.setGroup("lookups"); int lastLookup = cfg.readNumEntry("time",0); @@ -169,48 +198,75 @@ void Ntp::readLookups() cfg.setGroup("lookup_"+QString::number(i)); last = cfg.readEntry("secsSinceLast",0).toFloat(); shift = QString(cfg.readEntry("timeShift",0)).toFloat(); // qDebug("%i last %f",i,last); // qDebug("%i shift %f",i,shift); shiftPerSec = shift / last; _shiftPerSec += shiftPerSec; TableLookups->setText( i,0,QString::number(shiftPerSec)); TableLookups->setText( i,2,QString::number(shift)); TableLookups->setText( i,1,QString::number(last)); } _shiftPerSec /= lookupCount+1; TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); } void Ntp::preditctTime() { qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); Config cfg("ntp",Config::User); cfg.setGroup("lookups"); int lastTime = cfg.readNumEntry("time",0); 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:")+"<br><b>"+predictedTime.toString()+"</b>"); } void Ntp::setPredictTime() { preditctTime(); setTime( predictedTime ); } void Ntp::slotCheckNtp(int i) { if (i == 0) { 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 @@ -1,38 +1,43 @@ #ifndef NTP_H #define NTP_H #include "settime.h" #include <qdatetime.h> class OProcess; class QString; - +class QTimer; +class QSocket; class Ntp : public SetDateTime { Q_OBJECT public: Ntp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); ~Ntp(); protected: QDateTime predictedTime; private: QString _ntpOutput; float _shiftPerSec; OProcess *ntpProcess; + QTimer *ntpTimer; + QSocket *ntpSock; float getTimeShift(); void readLookups(); private slots: void slotRunNtp(); void getNtpOutput(OProcess *proc, char *buffer, int buflen); void ntpFinished(OProcess*); 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 @@ -1,39 +1,39 @@ <!DOCTYPE UI><UI> <class>NtpBase</class> <widget> <class>QWidget</class> <property stdset="1"> <name>name</name> <cstring>NtpBase</cstring> </property> <property stdset="1"> <name>geometry</name> <rect> <x>0</x> <y>0</y> - <width>330</width> - <height>419</height> + <width>331</width> + <height>426</height> </rect> </property> <property stdset="1"> <name>caption</name> <string>Network Time</string> </property> <property> <name>layoutMargin</name> </property> <property> <name>layoutSpacing</name> </property> <grid> <property stdset="1"> <name>margin</name> <number>2</number> </property> <property stdset="1"> <name>spacing</name> <number>2</number> </property> <widget row="0" column="0" > <class>QTabWidget</class> <property stdset="1"> @@ -88,49 +88,49 @@ <name>name</name> <cstring>tabNtp</cstring> </property> <attribute> <name>title</name> <string>NTP</string> </attribute> <grid> <property stdset="1"> <name>margin</name> <number>1</number> </property> <property stdset="1"> <name>spacing</name> <number>2</number> </property> <widget row="1" column="0" > <class>QPushButton</class> <property stdset="1"> <name>name</name> <cstring>runNtp</cstring> </property> <property stdset="1"> <name>text</name> - <string>get time from network</string> + <string>Get time from network</string> </property> </widget> <widget row="0" column="0" > <class>QFrame</class> <property stdset="1"> <name>name</name> <cstring>FrameNtp</cstring> </property> <property stdset="1"> <name>frameShape</name> <enum>StyledPanel</enum> </property> <property stdset="1"> <name>frameShadow</name> <enum>Raised</enum> </property> <property> <name>layoutMargin</name> </property> <property> <name>layoutSpacing</name> </property> <grid> <property stdset="1"> @@ -266,141 +266,191 @@ <enum>WidgetWidth</enum> </property> <property stdset="1"> <name>text</name> <string></string> </property> </widget> </grid> </widget> </grid> </widget> <widget> <class>QWidget</class> <property stdset="1"> <name>name</name> <cstring>tab</cstring> </property> <attribute> <name>title</name> <string>Predict</string> </attribute> <grid> <property stdset="1"> <name>margin</name> - <number>2</number> + <number>5</number> </property> <property stdset="1"> <name>spacing</name> - <number>2</number> + <number>6</number> </property> <widget row="0" column="0" > <class>QTable</class> <property stdset="1"> <name>name</name> <cstring>TableLookups</cstring> </property> <property stdset="1"> <name>numRows</name> <number>2</number> </property> <property stdset="1"> <name>numCols</name> <number>2</number> </property> </widget> - <widget row="2" column="0" > - <class>QPushButton</class> - <property stdset="1"> - <name>name</name> - <cstring>PushButtonPredict</cstring> - </property> - <property stdset="1"> - <name>text</name> - <string>predict time</string> - </property> - </widget> <widget row="1" column="0" > <class>QLayoutWidget</class> <property stdset="1"> <name>name</name> - <cstring>Layout5</cstring> + <cstring>Layout9</cstring> </property> <grid> <property stdset="1"> <name>margin</name> <number>0</number> </property> <property stdset="1"> <name>spacing</name> <number>6</number> </property> <widget row="0" column="1" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>TextLabelShift</cstring> </property> <property stdset="1"> <name>text</name> <string>nan</string> </property> </widget> + <widget row="1" column="0" > + <class>QLabel</class> + <property stdset="1"> + <name>name</name> + <cstring>TextLabel4</cstring> + </property> + <property stdset="1"> + <name>text</name> + <string>Esimated Shift:</string> + </property> + </widget> <widget row="1" column="1" > <class>QLabel</class> <property stdset="1"> <name>name</name> - <cstring>TextLabelPredTime</cstring> + <cstring>TextLabelEstimatedShift</cstring> </property> <property stdset="1"> <name>text</name> <string>nan</string> </property> </widget> + <widget row="2" column="0" > + <class>QLabel</class> + <property stdset="1"> + <name>name</name> + <cstring>TextLabel3_2</cstring> + </property> + <property stdset="1"> + <name>text</name> + <string>Predicted Time:</string> + </property> + </widget> <widget row="0" column="0" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>Mean_shift_label</cstring> </property> <property stdset="1"> <name>text</name> <string>Mean shift:</string> </property> </widget> - <widget row="1" column="0" > + <widget row="2" column="1" > <class>QLabel</class> <property stdset="1"> <name>name</name> - <cstring>TextLabel3_2</cstring> + <cstring>TextLabelPredTime</cstring> </property> <property stdset="1"> <name>text</name> - <string>Pred. Time:</string> + <string>nan</string> </property> </widget> </grid> </widget> + <widget row="2" column="0" > + <class>QLayoutWidget</class> + <property stdset="1"> + <name>name</name> + <cstring>Layout11</cstring> + </property> + <hbox> + <property stdset="1"> + <name>margin</name> + <number>0</number> + </property> + <property stdset="1"> + <name>spacing</name> + <number>6</number> + </property> + <widget> + <class>QPushButton</class> + <property stdset="1"> + <name>name</name> + <cstring>PushButtonSetPredTime</cstring> + </property> + <property stdset="1"> + <name>text</name> + <string>Set predicted time</string> + </property> + </widget> + <widget> + <class>QPushButton</class> + <property stdset="1"> + <name>name</name> + <cstring>PushButtonPredict</cstring> + </property> + <property stdset="1"> + <name>text</name> + <string>Predict time</string> + </property> + </widget> + </hbox> + </widget> </grid> </widget> <widget> <class>QWidget</class> <property stdset="1"> <name>name</name> <cstring>TabSettings</cstring> </property> <attribute> <name>title</name> <string>Settings</string> </attribute> <grid> <property stdset="1"> <name>margin</name> <number>2</number> </property> <property stdset="1"> <name>spacing</name> <number>2</number> </property> <widget row="0" column="0" > <class>QFrame</class> <property stdset="1"> @@ -465,52 +515,56 @@ <property stdset="1"> <name>text</name> <string>as</string> </property> </widget> <widget row="1" column="0" rowspan="1" colspan="2" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>TextLabel1_3</cstring> </property> <property stdset="1"> <name>text</name> <string>NTP server to get the time from the network.</string> </property> <property stdset="1"> <name>alignment</name> <set>WordBreak|AlignVCenter|AlignLeft</set> </property> <property> <name>wordwrap</name> </property> </widget> <widget row="0" column="1" > - <class>QLineEdit</class> + <class>QComboBox</class> <property stdset="1"> <name>name</name> - <cstring>LineEditNtpSrv</cstring> + <cstring>ComboNtpSrv</cstring> + </property> + <property stdset="1"> + <name>editable</name> + <bool>true</bool> </property> </widget> </grid> </widget> <spacer row="3" column="0" > <property> <name>name</name> <cstring>Spacer2</cstring> </property> <property stdset="1"> <name>orientation</name> <enum>Vertical</enum> </property> <property stdset="1"> <name>sizeType</name> <enum>Expanding</enum> </property> <property> <name>sizeHint</name> <size> <width>20</width> <height>20</height> </size> </property> @@ -588,72 +642,79 @@ </grid> </widget> <widget row="1" column="0" > <class>QLayoutWidget</class> <property stdset="1"> <name>name</name> <cstring>Layout7_2</cstring> </property> <grid> <property stdset="1"> <name>margin</name> <number>0</number> </property> <property stdset="1"> <name>spacing</name> <number>6</number> </property> <widget row="0" column="0" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>TextLabel1_2_2</cstring> </property> <property stdset="1"> + <name>sizePolicy</name> + <sizepolicy> + <hsizetype>1</hsizetype> + <vsizetype>1</vsizetype> + </sizepolicy> + </property> + <property stdset="1"> <name>text</name> <string>Wait for</string> </property> </widget> <widget row="0" column="2" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>TextLabel2_3</cstring> </property> <property stdset="1"> <name>text</name> <string>minutes until</string> </property> </widget> <widget row="1" column="0" rowspan="1" colspan="3" > <class>QLabel</class> <property stdset="1"> <name>name</name> <cstring>TextLabel3_3_2</cstring> </property> <property stdset="1"> <name>text</name> - <string>NTP syncronises the clock with the network</string> + <string>NTP tries to syncronises the clock with the network.</string> </property> <property stdset="1"> <name>alignment</name> <set>WordBreak|AlignVCenter|AlignLeft</set> </property> <property> <name>wordwrap</name> </property> </widget> <widget row="0" column="1" > <class>QSpinBox</class> <property stdset="1"> <name>name</name> <cstring>SpinBoxNtpDelay</cstring> </property> <property stdset="1"> <name>maxValue</name> <number>9999999</number> </property> <property stdset="1"> <name>minValue</name> <number>0</number> </property> </widget> |