Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp index b2f192a..52fc6be 100644 --- a/noncore/settings/netsystemtime/ntp.cpp +++ b/noncore/settings/netsystemtime/ntp.cpp | |||
@@ -1,216 +1,216 @@ | |||
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 <qsocket.h> |
7 | #include <qlineedit.h> | 7 | #include <qlineedit.h> |
8 | #include <qspinbox.h> | 8 | #include <qspinbox.h> |
9 | #include <qmessagebox.h> | ||
9 | #include <qmultilineedit.h> | 10 | #include <qmultilineedit.h> |
10 | #include <opie/oprocess.h> | 11 | #include <opie/oprocess.h> |
11 | #include <qpe/config.h> | 12 | #include <qpe/config.h> |
12 | #include <qpe/global.h> | 13 | #include <qpe/global.h> |
13 | #include <qpe/timeconversion.h> | 14 | #include <qpe/timeconversion.h> |
14 | #include <qpe/tzselect.h> | 15 | #include <qpe/tzselect.h> |
15 | #include <qpe/timestring.h> | 16 | #include <qpe/timestring.h> |
16 | #include <qpe/qpedialog.h> | 17 | #include <qpe/qpedialog.h> |
17 | #include <sys/time.h> | 18 | #include <sys/time.h> |
18 | #include <time.h> | 19 | #include <time.h> |
19 | #include <stdlib.h> | 20 | #include <stdlib.h> |
20 | 21 | ||
21 | 22 | ||
22 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | 23 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) |
23 | : SetDateTime( parent, name, fl ) | 24 | : SetDateTime( parent, name, fl ) |
24 | { | 25 | { |
25 | Config cfg("ntp",Config::User); | 26 | Config cfg("ntp",Config::User); |
26 | cfg.setGroup("settings"); | 27 | cfg.setGroup("settings"); |
27 | // _maxOffset = cfg.readNumEntry("maxOffset",5); | 28 | // _maxOffset = cfg.readNumEntry("maxOffset",5); |
28 | // _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); | 29 | // _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); |
29 | SpinBoxMinDelay->setValue( cfg.readNumEntry("minLookupDiff",10) ); | 30 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",60) ); |
30 | SpinBoxMinDelay->setMinValue( 7 ); | 31 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",60) ); |
31 | LineEditNtpSrv->setText( cfg.readEntry("ntpServer", tr("time.fu-berlin.de")) ); | 32 | LineEditNtpSrv->setText( cfg.readEntry("ntpServer", tr("time.fu-berlin.de")) ); |
32 | ntpProcess = new OProcess( ); | 33 | ntpProcess = new OProcess( ); |
33 | 34 | ||
34 | QSocket *ntpSock = new QSocket( this ); | 35 | QSocket *ntpSock = new QSocket( this ); |
35 | ntpSock->connectToHost( LineEditNtpSrv->text() ,123); | 36 | ntpSock->connectToHost( LineEditNtpSrv->text() ,123); |
36 | connect( ntpSock, SIGNAL( error(int) ), | 37 | connect( ntpSock, SIGNAL( error(int) ), |
37 | SLOT(slotCheckNtp(int)) ); | 38 | SLOT(slotCheckNtp(int)) ); |
38 | 39 | ||
39 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), | 40 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), |
40 | this, SLOT(getNtpOutput(OProcess*,char*,int))); | 41 | this, SLOT(getNtpOutput(OProcess*,char*,int))); |
41 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), | 42 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), |
42 | this, SLOT(ntpFinished(OProcess*))); | 43 | this, SLOT(ntpFinished(OProcess*))); |
43 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); | 44 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); |
44 | connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); | 45 | connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); |
45 | slotCheckNtp(-1); | 46 | slotCheckNtp(-1); |
46 | // slotRunNtp(); | 47 | // slotRunNtp(); |
47 | readLookups(); | 48 | readLookups(); |
48 | } | 49 | } |
49 | 50 | ||
50 | Ntp::~Ntp() | 51 | Ntp::~Ntp() |
51 | { | 52 | { |
52 | Config cfg("ntp",Config::User); | 53 | Config cfg("ntp",Config::User); |
53 | cfg.setGroup("settings"); | 54 | cfg.setGroup("settings"); |
54 | cfg.writeEntry("ntpServer", LineEditNtpSrv->text()); | 55 | cfg.writeEntry("ntpServer", LineEditNtpSrv->text()); |
55 | cfg.writeEntry( "minLookupDiff", SpinBoxMinDelay->value() ); | 56 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); |
57 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); | ||
56 | } | 58 | } |
57 | 59 | ||
58 | 60 | ||
59 | void Ntp::slotRunNtp() | 61 | void Ntp::slotRunNtp() |
60 | { | 62 | { |
63 | Config cfg("ntp",Config::User); | ||
64 | cfg.setGroup("lookups"); | ||
65 | int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | ||
66 | |||
67 | if ( lookupDiff < SpinBoxNtpDelay->value()*60 ) | ||
68 | { | ||
69 | switch ( | ||
70 | QMessageBox::warning(this, tr("Run ntp?"), | ||
71 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ | ||
72 | QString::number(lookupDiff%60)+tr(" minutes elapsed since last loopup.")+ | ||
73 | "<br>"+tr("Rerun ntp?"), | ||
74 | QMessageBox::Ok,QMessageBox::Cancel) | ||
75 | ) { | ||
76 | case QMessageBox::Ok: break; | ||
77 | case QMessageBox::Cancel: return; | ||
78 | default: return; | ||
79 | } | ||
80 | } | ||
61 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); | 81 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); |
62 | ntpProcess->clearArguments(); | 82 | ntpProcess->clearArguments(); |
63 | *ntpProcess << "ntpdate" << LineEditNtpSrv->text(); | 83 | *ntpProcess << "ntpdate" << LineEditNtpSrv->text(); |
64 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); | 84 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); |
65 | if ( !ret ) { | 85 | if ( !ret ) { |
66 | qDebug("Error while executing ntp"); | 86 | qDebug("Error while executing ntp"); |
67 | } | 87 | } |
68 | } | 88 | } |
69 | 89 | ||
70 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | 90 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) |
71 | { | 91 | { |
72 | QString lineStr, lineStrOld; | 92 | QString lineStr, lineStrOld; |
73 | lineStr = buffer; | 93 | lineStr = buffer; |
74 | lineStr=lineStr.left(buflen); | 94 | lineStr=lineStr.left(buflen); |
75 | if (lineStr!=lineStrOld) | 95 | if (lineStr!=lineStrOld) |
76 | { | 96 | { |
77 | MultiLineEditntpOutPut->append(lineStr); | 97 | MultiLineEditntpOutPut->append(lineStr); |
78 | _ntpOutput += lineStr; | 98 | _ntpOutput += lineStr; |
79 | } | 99 | } |
80 | lineStrOld = lineStr; | 100 | lineStrOld = lineStr; |
81 | } | 101 | } |
82 | 102 | ||
83 | void Ntp::ntpFinished(OProcess*) | 103 | void Ntp::ntpFinished(OProcess*) |
84 | { | 104 | { |
85 | Config cfg("ntp",Config::User); | 105 | Config cfg("ntp",Config::User); |
86 | cfg.setGroup("lookups"); | 106 | cfg.setGroup("lookups"); |
87 | int lastLookup = cfg.readNumEntry("time",0); | 107 | int lastLookup = cfg.readNumEntry("time",0); |
88 | int lookupCount = cfg.readNumEntry("count",-1); | 108 | int lookupCount = cfg.readNumEntry("count",-1); |
89 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 109 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
90 | cfg.writeEntry("time", time); | 110 | cfg.writeEntry("time", time); |
91 | cfg.setGroup("correction"); | 111 | cfg.setGroup("correction"); |
92 | cfg.writeEntry("time", time); | 112 | cfg.writeEntry("time", time); |
93 | 113 | ||
94 | float timeShift = getTimeShift(); | 114 | float timeShift = getTimeShift(); |
95 | if (timeShift == 0.0) return; | 115 | if (timeShift == 0.0) return; |
96 | int secsSinceLast = time - lastLookup; | 116 | int secsSinceLast = time - lastLookup; |
97 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); | 117 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); |
98 | if ( lastLookup > 0 && secsSinceLast > 60*24* SpinBoxMinDelay->value()) | 118 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); |
119 | if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) | ||
99 | { | 120 | { |
100 | lookupCount++; | 121 | lookupCount++; |
101 | cfg.writeEntry("count",lookupCount); | 122 | cfg.writeEntry("count",lookupCount); |
102 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | 123 | cfg.setGroup("lookup_"+QString::number(lookupCount)); |
103 | _shiftPerSec = timeShift / secsSinceLast; | 124 | _shiftPerSec = timeShift / secsSinceLast; |
104 | // float nextCorr = _maxOffset / _shiftPerSec; | 125 | // float nextCorr = _maxOffset / _shiftPerSec; |
105 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); | 126 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); |
106 | cfg.writeEntry("secsSinceLast",secsSinceLast); | 127 | cfg.writeEntry("secsSinceLast",secsSinceLast); |
107 | cfg.writeEntry("timeShift",QString::number(timeShift)); | 128 | cfg.writeEntry("timeShift",QString::number(timeShift)); |
108 | } | 129 | } |
109 | } | 130 | } |
110 | 131 | ||
111 | //void Ntp::correctClock() | ||
112 | //{ | ||
113 | //qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | ||
114 | //Config cfg("ntp",Config::User); | ||
115 | // cfg.setGroup("correction"); | ||
116 | // int lastTime = cfg.readNumEntry("time",0); | ||
117 | // if ( lastTime == 0 ) | ||
118 | // { | ||
119 | // return; | ||
120 | // } | ||
121 | // int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
122 | // int corr = int((now - lastTime) * _shiftPerSec); | ||
123 | // struct timeval myTv; | ||
124 | // myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) ); | ||
125 | // myTv.tv_usec = 0; | ||
126 | // | ||
127 | // if ( myTv.tv_sec != -1 ) | ||
128 | // ::settimeofday( &myTv, 0 ); | ||
129 | // Global::writeHWClock(); | ||
130 | // cfg.writeEntry("time",now); | ||
131 | //qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | ||
132 | //} | ||
133 | 132 | ||
134 | float Ntp::getTimeShift() | 133 | float Ntp::getTimeShift() |
135 | { | 134 | { |
136 | QString _offset = "offset"; | 135 | QString _offset = "offset"; |
137 | QString _sec = "sec"; | 136 | QString _sec = "sec"; |
138 | QRegExp _reOffset = QRegExp(_offset); | 137 | QRegExp _reOffset = QRegExp(_offset); |
139 | QRegExp _reEndOffset = QRegExp(_sec); | 138 | QRegExp _reEndOffset = QRegExp(_sec); |
140 | int posOffset = _reOffset.match( _ntpOutput ); | 139 | int posOffset = _reOffset.match( _ntpOutput ); |
141 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); | 140 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); |
142 | posOffset += _offset.length() + 1; | 141 | posOffset += _offset.length() + 1; |
143 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); | 142 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); |
144 | qDebug("%s", _ntpOutput.latin1()); | 143 | qDebug("%s", _ntpOutput.latin1()); |
145 | qDebug("diff = >%s<",diff.latin1()); | 144 | qDebug("diff = >%s<",diff.latin1()); |
146 | TextLabelTimeShift->setText(diff); | ||
147 | return diff.toFloat(); | 145 | return diff.toFloat(); |
148 | } | 146 | } |
149 | 147 | ||
150 | void Ntp::readLookups() | 148 | void Ntp::readLookups() |
151 | { | 149 | { |
152 | Config cfg("ntp",Config::User); | 150 | Config cfg("ntp",Config::User); |
153 | cfg.setGroup("lookups"); | 151 | cfg.setGroup("lookups"); |
154 | int lookupCount = cfg.readNumEntry("count",-1); | 152 | int lookupCount = cfg.readNumEntry("count",-1); |
155 | float last, shift, shiftPerSec; | 153 | float last, shift, shiftPerSec; |
156 | qDebug("lookupCount = %i",lookupCount); | 154 | qDebug("lookupCount = %i",lookupCount); |
157 | TableLookups->setNumCols( 3 ); | 155 | TableLookups->setNumCols( 3 ); |
158 | TableLookups->setNumRows( lookupCount); | 156 | TableLookups->setNumRows( lookupCount); |
159 | TableLookups->horizontalHeader()->setLabel(1,"secsSinceLast"); | 157 | TableLookups->horizontalHeader()->setLabel(1,"secsSinceLast"); |
160 | TableLookups->horizontalHeader()->setLabel(2,"timeShift"); | 158 | TableLookups->horizontalHeader()->setLabel(2,"timeShift"); |
161 | TableLookups->horizontalHeader()->setLabel(0,"shift/s"); | 159 | TableLookups->horizontalHeader()->setLabel(0,"shift/s"); |
162 | int cw = 50;//TableLookups->width()/4; | 160 | int cw = 50;//TableLookups->width()/4; |
163 | qDebug("column width %i",cw); | 161 | qDebug("column width %i",cw); |
164 | TableLookups->setColumnWidth( 0, cw+30 ); | 162 | TableLookups->setColumnWidth( 0, cw+30 ); |
165 | TableLookups->setColumnWidth( 1, cw ); | 163 | TableLookups->setColumnWidth( 1, cw ); |
166 | TableLookups->setColumnWidth( 2, cw ); | 164 | TableLookups->setColumnWidth( 2, cw ); |
165 | TableLookups->sortColumn(0, false, true ); | ||
166 | // TableLookups->setSorting( true ); | ||
167 | for (int i=0; i < lookupCount; i++) | 167 | for (int i=0; i < lookupCount; i++) |
168 | { | 168 | { |
169 | cfg.setGroup("lookup_"+QString::number(i)); | 169 | cfg.setGroup("lookup_"+QString::number(i)); |
170 | last = cfg.readEntry("secsSinceLast",0).toFloat(); | 170 | last = cfg.readEntry("secsSinceLast",0).toFloat(); |
171 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); | 171 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); |
172 | // qDebug("%i last %f",i,last); | 172 | // qDebug("%i last %f",i,last); |
173 | // qDebug("%i shift %f",i,shift); | 173 | // qDebug("%i shift %f",i,shift); |
174 | shiftPerSec = shift / last; | 174 | shiftPerSec = shift / last; |
175 | _shiftPerSec += shiftPerSec; | 175 | _shiftPerSec += shiftPerSec; |
176 | TableLookups->setText( i,0,QString::number(shiftPerSec)); | 176 | TableLookups->setText( i,0,QString::number(shiftPerSec)); |
177 | TableLookups->setText( i,2,QString::number(shift)); | 177 | TableLookups->setText( i,2,QString::number(shift)); |
178 | TableLookups->setText( i,1,QString::number(last)); | 178 | TableLookups->setText( i,1,QString::number(last)); |
179 | } | 179 | } |
180 | _shiftPerSec /= lookupCount+1; | 180 | _shiftPerSec /= lookupCount+1; |
181 | TextLabelShift->setText(QString::number(_shiftPerSec)); | 181 | TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); |
182 | } | 182 | } |
183 | 183 | ||
184 | void Ntp::preditctTime() | 184 | void Ntp::preditctTime() |
185 | { | 185 | { |
186 | qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | 186 | qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); |
187 | Config cfg("ntp",Config::User); | 187 | Config cfg("ntp",Config::User); |
188 | cfg.setGroup("lookups"); | 188 | cfg.setGroup("lookups"); |
189 | int lastTime = cfg.readNumEntry("time",0); | 189 | int lastTime = cfg.readNumEntry("time",0); |
190 | setenv( "TZ", tz->currentZone(), 1 ); | 190 | setenv( "TZ", tz->currentZone(), 1 ); |
191 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 191 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
192 | int corr = int((now - lastTime) * _shiftPerSec); | 192 | int corr = int((now - lastTime) * _shiftPerSec); |
193 | predictedTime = QDateTime::currentDateTime().addSecs(corr); | 193 | predictedTime = QDateTime::currentDateTime().addSecs(corr); |
194 | TextLabelPredTime->setText(predictedTime.toString()); | 194 | TextLabelPredTime->setText(predictedTime.toString()); |
195 | TextLabelMainPredTime->setText(predictedTime.toString()); | 195 | TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); |
196 | } | 196 | } |
197 | 197 | ||
198 | void Ntp::setPredictTime() | 198 | void Ntp::setPredictTime() |
199 | { | 199 | { |
200 | preditctTime(); | 200 | preditctTime(); |
201 | setTime( predictedTime ); | 201 | setTime( predictedTime ); |
202 | } | 202 | } |
203 | 203 | ||
204 | void Ntp::slotCheckNtp(int i) | 204 | void Ntp::slotCheckNtp(int i) |
205 | { | 205 | { |
206 | if (i == 0) | 206 | if (i == 0) |
207 | { | 207 | { |
208 | TextLabelMainPredTime->hide(); | 208 | TextLabelMainPredTime->hide(); |
209 | ButtonSetTime->setText( tr("Get time from network") ); | 209 | ButtonSetTime->setText( tr("Get time from network") ); |
210 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); | 210 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); |
211 | }else{ | 211 | }else{ |
212 | preditctTime(); | 212 | preditctTime(); |
213 | ButtonSetTime->setText( tr("Predict time") ); | 213 | ButtonSetTime->setText( tr("Set predicted time") ); |
214 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); | 214 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); |
215 | }; | 215 | }; |
216 | } | 216 | } |