summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp21
-rw-r--r--noncore/settings/netsystemtime/ntp.h3
2 files changed, 19 insertions, 5 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index b2f5332..6a5c062 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -16,55 +16,54 @@
#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",41) );
SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",42) );
ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) );
ntpTimer = new QTimer(this);
+ processTimer = new QTimer(this);
ntpTimer->start(SpinBoxNtpDelay->value()*1000*60);
ntpProcess = new OProcess( );
connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ),
SLOT(slotNtpDelayChanged(int)) );
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);
readLookups();
}
Ntp::~Ntp()
{
@@ -95,72 +94,81 @@ bool Ntp::ntpDelayElapsed()
QString Ntp::getNtpServer()
{
return ComboNtpSrv->currentText();
}
void Ntp::slotRunNtp()
{
if ( !ntpDelayElapsed() )
{
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());
ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() );
+ connect( processTimer, SIGNAL( timeout() ), SLOT(slotTimeoutNtpProcess()) );
+ processTimer->start(2*1000*60, true);
+
ntpProcess->clearArguments();
*ntpProcess << "ntpdate" << getNtpServer();
bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
if ( !ret ) {
qDebug("Error while executing ntpdate");
ntpOutPut( tr("Error while executing ntpdate"));
}
}
void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
{
QString lineStr, lineStrOld;
lineStr = buffer;
lineStr=lineStr.left(buflen);
if (lineStr!=lineStrOld)
{
ntpOutPut(lineStr);
_ntpOutput += lineStr;
}
lineStrOld = lineStr;
}
-void Ntp::ntpFinished(OProcess*)
+void Ntp::ntpFinished(OProcess *p)
{
+ if (!p->normalExit())
+ {
+ slotProbeNtpServer();
+ return;
+ }
+ processTimer->stop();
Config cfg("ntp",Config::User);
cfg.setGroup("lookups");
int lastLookup = cfg.readNumEntry("time",0);
int lookupCount = cfg.readNumEntry("count",-1);
int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
cfg.writeEntry("time", time);
cfg.setGroup("correction");
cfg.writeEntry("time", time);
float timeShift = getTimeShift();
if (timeShift == 0.0) return;
int secsSinceLast = time - lastLookup;
TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds"));
if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value())
{
lookupCount++;
cfg.writeEntry("count",lookupCount);
cfg.setGroup("lookup_"+QString::number(lookupCount));
_shiftPerSec = timeShift / secsSinceLast;
qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);
cfg.writeEntry("secsSinceLast",secsSinceLast);
cfg.writeEntry("timeShift",QString::number(timeShift));
}
@@ -240,42 +248,47 @@ void Ntp::setPredictTime()
void Ntp::slotCheckNtp(int i)
{
if (i == 0)
{
TextLabelMainPredTime->hide();
ButtonSetTime->setText( tr("Get time from network") );
connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) );
if ( ntpDelayElapsed() )
{
slotRunNtp();
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") );
if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer());
connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) );
connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) );
}
-// ntpTimer->start(SpinBoxNtpDelay->value()*1000*60);
}
void Ntp::slotProbeNtpServer()
{
ntpSock->connectToHost( getNtpServer() ,123);
}
void Ntp::slotNtpDelayChanged(int delay)
{
ntpTimer->changeInterval( delay*1000*60 );
}
void Ntp::ntpOutPut(QString out)
{
MultiLineEditntpOutPut->append(out);
MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE);
}
+
+void Ntp::slotTimeoutNtpProcess()
+{
+ ntpProcess->kill();
+ slotProbeNtpServer();
+}
diff --git a/noncore/settings/netsystemtime/ntp.h b/noncore/settings/netsystemtime/ntp.h
index c78dc55..d166973 100644
--- a/noncore/settings/netsystemtime/ntp.h
+++ b/noncore/settings/netsystemtime/ntp.h
@@ -4,44 +4,45 @@
#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;
int _lookupDiff;
OProcess *ntpProcess;
- QTimer *ntpTimer;
+ QTimer *ntpTimer, *processTimer;
QSocket *ntpSock;
float getTimeShift();
void readLookups();
void ntpOutPut(QString);
bool ntpDelayElapsed();
QString getNtpServer();
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);
+ void slotTimeoutNtpProcess();
};
#endif