Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 77 |
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 | |||
@@ -1,12 +1,14 @@ | |||
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 <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> |
10 | #include <qpe/global.h> | 12 | #include <qpe/global.h> |
11 | #include <qpe/timeconversion.h> | 13 | #include <qpe/timeconversion.h> |
12 | #include <qpe/tzselect.h> | 14 | #include <qpe/tzselect.h> |
@@ -19,46 +21,49 @@ | |||
19 | 21 | ||
20 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | 22 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) |
21 | : SetDateTime( parent, name, fl ) | 23 | : SetDateTime( parent, name, 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 | ||
41 | Ntp::~Ntp() | 50 | Ntp::~Ntp() |
42 | { | 51 | { |
43 | |||
44 | } | ||
45 | |||
46 | void 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 | ||
54 | void Ntp::slotRunNtp() | 59 | 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"); |
62 | } | 67 | } |
63 | } | 68 | } |
64 | 69 | ||
@@ -66,13 +71,13 @@ void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | |||
66 | { | 71 | { |
67 | QString lineStr, lineStrOld; | 72 | QString lineStr, lineStrOld; |
68 | lineStr = buffer; | 73 | lineStr = buffer; |
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; |
76 | } | 81 | } |
77 | 82 | ||
78 | void Ntp::ntpFinished(OProcess*) | 83 | void Ntp::ntpFinished(OProcess*) |
@@ -87,13 +92,13 @@ void Ntp::ntpFinished(OProcess*) | |||
87 | cfg.writeEntry("time", time); | 92 | cfg.writeEntry("time", time); |
88 | 93 | ||
89 | float timeShift = getTimeShift(); | 94 | float timeShift = getTimeShift(); |
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); |
97 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | 102 | cfg.setGroup("lookup_"+QString::number(lookupCount)); |
98 | _shiftPerSec = timeShift / secsSinceLast; | 103 | _shiftPerSec = timeShift / secsSinceLast; |
99 | // float nextCorr = _maxOffset / _shiftPerSec; | 104 | // float nextCorr = _maxOffset / _shiftPerSec; |
@@ -148,32 +153,32 @@ void Ntp::readLookups() | |||
148 | cfg.setGroup("lookups"); | 153 | cfg.setGroup("lookups"); |
149 | int lookupCount = cfg.readNumEntry("count",-1); | 154 | int lookupCount = cfg.readNumEntry("count",-1); |
150 | float last, shift, shiftPerSec; | 155 | float last, shift, shiftPerSec; |
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++) |
163 | { | 168 | { |
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)); |
177 | } | 182 | } |
178 | 183 | ||
179 | void Ntp::preditctTime() | 184 | void Ntp::preditctTime() |
@@ -182,10 +187,30 @@ void Ntp::preditctTime() | |||
182 | Config cfg("ntp",Config::User); | 187 | Config cfg("ntp",Config::User); |
183 | cfg.setGroup("lookups"); | 188 | cfg.setGroup("lookups"); |
184 | int lastTime = cfg.readNumEntry("time",0); | 189 | int lastTime = cfg.readNumEntry("time",0); |
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 | |||
198 | void Ntp::setPredictTime() | ||
199 | { | ||
200 | preditctTime(); | ||
201 | setTime( predictedTime ); | ||
202 | } | ||
203 | |||
204 | void 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 | } |