summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/ntp.cpp
authortille <tille>2002-06-25 20:10:42 (UTC)
committer tille <tille>2002-06-25 20:10:42 (UTC)
commit4c671979117dc8ec75c91ce543c7039730530a63 (patch) (unidiff)
tree69995a8c917fba569814fd0475e7819a95d65ac5 /noncore/settings/netsystemtime/ntp.cpp
parentaf4a3940dd672423da28b54e5d955cc5d33cecda (diff)
downloadopie-4c671979117dc8ec75c91ce543c7039730530a63.zip
opie-4c671979117dc8ec75c91ce543c7039730530a63.tar.gz
opie-4c671979117dc8ec75c91ce543c7039730530a63.tar.bz2
working
Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp77
1 files changed, 51 insertions, 26 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index 87b20ec..4a7959e 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -3,7 +3,9 @@
3#include <qregexp.h> 3#include <qregexp.h>
4#include <qtable.h> 4#include <qtable.h>
5#include <qlabel.h> 5#include <qlabel.h>
6#include <qsocket.h>
6#include <qlineedit.h> 7#include <qlineedit.h>
8#include <qspinbox.h>
7#include <qmultilineedit.h> 9#include <qmultilineedit.h>
8#include <opie/oprocess.h> 10#include <opie/oprocess.h>
9#include <qpe/config.h> 11#include <qpe/config.h>
@@ -22,32 +24,35 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
22{ 24{
23 Config cfg("ntp",Config::User); 25 Config cfg("ntp",Config::User);
24 cfg.setGroup("settings"); 26 cfg.setGroup("settings");
25 _maxOffset = cfg.readNumEntry("maxOffset",5); 27 // _maxOffset = cfg.readNumEntry("maxOffset",5);
26 _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); 28// _minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
27 LineEditNtpServer->setText( cfg.readEntry("ntpServer") ); 29 SpinBoxMinDelay->setValue( cfg.readNumEntry("minLookupDiff",10) );
30 SpinBoxMinDelay->setMinValue( 7 );
31 LineEditNtpSrv->setText( cfg.readEntry("ntpServer", tr("time.fu-berlin.de")) );
28 ntpProcess = new OProcess( ); 32 ntpProcess = new OProcess( );
33
34 QSocket *ntpSock = new QSocket( this );
35 ntpSock->connectToHost( LineEditNtpSrv->text() ,123);
36 connect( ntpSock, SIGNAL( error(int) ),
37 SLOT(slotCheckNtp(int)) );
38
29 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), 39 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
30 this, SLOT(getNtpOutput(OProcess*,char*,int))); 40 this, SLOT(getNtpOutput(OProcess*,char*,int)));
31 connect ( ntpProcess, SIGNAL(processExited(OProcess*)), 41 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
32 this, SLOT(ntpFinished(OProcess*))); 42 this, SLOT(ntpFinished(OProcess*)));
33 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); 43 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
34 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); 44 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime()));
35 // _nextCorrection = new QTimer( this ); 45
36// connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) ); 46// slotRunNtp();
37 slotRunNtp();
38 readLookups(); 47 readLookups();
39} 48}
40 49
41Ntp::~Ntp() 50Ntp::~Ntp()
42{ 51{
43
44}
45
46void Ntp::accept()
47{
48 Config cfg("ntp",Config::User); 52 Config cfg("ntp",Config::User);
49 cfg.setGroup("settings"); 53 cfg.setGroup("settings");
50 cfg.writeEntry("ntpServer", LineEditNtpServer->text()); 54 cfg.writeEntry("ntpServer", LineEditNtpSrv->text());
55 cfg.writeEntry( "minLookupDiff", SpinBoxMinDelay->value() );
51} 56}
52 57
53 58
@@ -55,7 +60,7 @@ void Ntp::slotRunNtp()
55{ 60{
56 TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); 61 TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
57 ntpProcess->clearArguments(); 62 ntpProcess->clearArguments();
58 *ntpProcess << "ntpdate" << LineEditNtpServer->text(); 63 *ntpProcess << "ntpdate" << LineEditNtpSrv->text();
59 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); 64 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
60 if ( !ret ) { 65 if ( !ret ) {
61 qDebug("Error while executing ntp"); 66 qDebug("Error while executing ntp");
@@ -69,7 +74,7 @@ void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
69 lineStr=lineStr.left(buflen); 74 lineStr=lineStr.left(buflen);
70 if (lineStr!=lineStrOld) 75 if (lineStr!=lineStrOld)
71 { 76 {
72 // outPut->append(lineStr); 77 MultiLineEditntpOutPut->append(lineStr);
73 _ntpOutput += lineStr; 78 _ntpOutput += lineStr;
74 } 79 }
75 lineStrOld = lineStr; 80 lineStrOld = lineStr;
@@ -90,7 +95,7 @@ void Ntp::ntpFinished(OProcess*)
90 if (timeShift == 0.0) return; 95 if (timeShift == 0.0) return;
91 int secsSinceLast = time - lastLookup; 96 int secsSinceLast = time - lastLookup;
92 TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); 97 TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
93 if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff) 98 if ( lastLookup > 0 && secsSinceLast > 60*24* SpinBoxMinDelay->value())
94 { 99 {
95 lookupCount++; 100 lookupCount++;
96 cfg.writeEntry("count",lookupCount); 101 cfg.writeEntry("count",lookupCount);
@@ -151,12 +156,12 @@ void Ntp::readLookups()
151 qDebug("lookupCount = %i",lookupCount); 156 qDebug("lookupCount = %i",lookupCount);
152 TableLookups->setNumCols( 3 ); 157 TableLookups->setNumCols( 3 );
153 TableLookups->setNumRows( lookupCount); 158 TableLookups->setNumRows( lookupCount);
154 TableLookups->horizontalHeader()->setLabel(2,"secsSinceLast"); 159 TableLookups->horizontalHeader()->setLabel(1,"secsSinceLast");
155 TableLookups->horizontalHeader()->setLabel(1,"timeShift"); 160 TableLookups->horizontalHeader()->setLabel(2,"timeShift");
156 TableLookups->horizontalHeader()->setLabel(0,"shift/s"); 161 TableLookups->horizontalHeader()->setLabel(0,"shift/s");
157 int cw = TableLookups->width()/4; 162 int cw = 50;//TableLookups->width()/4;
158 qDebug("column width %i",cw); 163 qDebug("column width %i",cw);
159 TableLookups->setColumnWidth( 0, cw ); 164 TableLookups->setColumnWidth( 0, cw+30 );
160 TableLookups->setColumnWidth( 1, cw ); 165 TableLookups->setColumnWidth( 1, cw );
161 TableLookups->setColumnWidth( 2, cw ); 166 TableLookups->setColumnWidth( 2, cw );
162 for (int i=0; i < lookupCount; i++) 167 for (int i=0; i < lookupCount; i++)
@@ -164,13 +169,13 @@ void Ntp::readLookups()
164 cfg.setGroup("lookup_"+QString::number(i)); 169 cfg.setGroup("lookup_"+QString::number(i));
165 last = cfg.readEntry("secsSinceLast",0).toFloat(); 170 last = cfg.readEntry("secsSinceLast",0).toFloat();
166 shift = QString(cfg.readEntry("timeShift",0)).toFloat(); 171 shift = QString(cfg.readEntry("timeShift",0)).toFloat();
167 qDebug("%i last %f",i,last); 172 // qDebug("%i last %f",i,last);
168 qDebug("%i shift %f",i,shift); 173 // qDebug("%i shift %f",i,shift);
169 shiftPerSec = shift / last; 174 shiftPerSec = shift / last;
170 _shiftPerSec += shiftPerSec; 175 _shiftPerSec += shiftPerSec;
171 TableLookups->setText( i,0,QString::number(shiftPerSec)); 176 TableLookups->setText( i,0,QString::number(shiftPerSec));
172 TableLookups->setText( i,1,QString::number(shift)); 177 TableLookups->setText( i,2,QString::number(shift));
173 TableLookups->setText( i,2,QString::number(last)); 178 TableLookups->setText( i,1,QString::number(last));
174 } 179 }
175 _shiftPerSec /= lookupCount+1; 180 _shiftPerSec /= lookupCount+1;
176 TextLabelShift->setText(QString::number(_shiftPerSec)); 181 TextLabelShift->setText(QString::number(_shiftPerSec));
@@ -185,7 +190,27 @@ void Ntp::preditctTime()
185 setenv( "TZ", tz->currentZone(), 1 ); 190 setenv( "TZ", tz->currentZone(), 1 );
186 int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); 191 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
187 int corr = int((now - lastTime) * _shiftPerSec); 192 int corr = int((now - lastTime) * _shiftPerSec);
188 QDateTime dt = QDateTime::currentDateTime().addSecs(corr); 193 predictedTime = QDateTime::currentDateTime().addSecs(corr);
189 setTime(dt); 194 TextLabelPredTime->setText(predictedTime.toString());
190 TextLabelPredTime->setText(dt.toString()); 195 TextLabelMainPredTime->setText(predictedTime.toString());
196}
197
198void Ntp::setPredictTime()
199{
200 preditctTime();
201 setTime( predictedTime );
202}
203
204void Ntp::slotCheckNtp(int i)
205{
206 if (i == 0)
207 {
208 TextLabelMainPredTime->hide();
209 ButtonSetTime->setText( tr("Get time from network") );
210 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) );
211 }else{
212 preditctTime();
213 ButtonSetTime->setText( tr("Predict time") );
214 connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) );
215 };
191} 216}