-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp index bcbf7a1..b2f5332 100644 --- a/noncore/settings/netsystemtime/ntp.cpp +++ b/noncore/settings/netsystemtime/ntp.cpp | |||
@@ -141,142 +141,141 @@ void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | |||
141 | void Ntp::ntpFinished(OProcess*) | 141 | void Ntp::ntpFinished(OProcess*) |
142 | { | 142 | { |
143 | Config cfg("ntp",Config::User); | 143 | Config cfg("ntp",Config::User); |
144 | cfg.setGroup("lookups"); | 144 | cfg.setGroup("lookups"); |
145 | int lastLookup = cfg.readNumEntry("time",0); | 145 | int lastLookup = cfg.readNumEntry("time",0); |
146 | int lookupCount = cfg.readNumEntry("count",-1); | 146 | int lookupCount = cfg.readNumEntry("count",-1); |
147 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 147 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
148 | cfg.writeEntry("time", time); | 148 | cfg.writeEntry("time", time); |
149 | cfg.setGroup("correction"); | 149 | cfg.setGroup("correction"); |
150 | cfg.writeEntry("time", time); | 150 | cfg.writeEntry("time", time); |
151 | 151 | ||
152 | float timeShift = getTimeShift(); | 152 | float timeShift = getTimeShift(); |
153 | if (timeShift == 0.0) return; | 153 | if (timeShift == 0.0) return; |
154 | int secsSinceLast = time - lastLookup; | 154 | int secsSinceLast = time - lastLookup; |
155 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); | 155 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); |
156 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); | 156 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); |
157 | if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) | 157 | if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) |
158 | { | 158 | { |
159 | lookupCount++; | 159 | lookupCount++; |
160 | cfg.writeEntry("count",lookupCount); | 160 | cfg.writeEntry("count",lookupCount); |
161 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | 161 | cfg.setGroup("lookup_"+QString::number(lookupCount)); |
162 | _shiftPerSec = timeShift / secsSinceLast; | 162 | _shiftPerSec = timeShift / secsSinceLast; |
163 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); | 163 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); |
164 | cfg.writeEntry("secsSinceLast",secsSinceLast); | 164 | cfg.writeEntry("secsSinceLast",secsSinceLast); |
165 | cfg.writeEntry("timeShift",QString::number(timeShift)); | 165 | cfg.writeEntry("timeShift",QString::number(timeShift)); |
166 | } | 166 | } |
167 | } | 167 | } |
168 | 168 | ||
169 | 169 | ||
170 | float Ntp::getTimeShift() | 170 | float Ntp::getTimeShift() |
171 | { | 171 | { |
172 | QString _offset = "offset"; | 172 | QString _offset = "offset"; |
173 | QString _sec = "sec"; | 173 | QString _sec = "sec"; |
174 | QRegExp _reOffset = QRegExp(_offset); | 174 | QRegExp _reOffset = QRegExp(_offset); |
175 | QRegExp _reEndOffset = QRegExp(_sec); | 175 | QRegExp _reEndOffset = QRegExp(_sec); |
176 | int posOffset = _reOffset.match( _ntpOutput ); | 176 | int posOffset = _reOffset.match( _ntpOutput ); |
177 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); | 177 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); |
178 | posOffset += _offset.length() + 1; | 178 | posOffset += _offset.length() + 1; |
179 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); | 179 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); |
180 | qDebug("%s", _ntpOutput.latin1()); | 180 | qDebug("%s", _ntpOutput.latin1()); |
181 | qDebug("diff = >%s<",diff.latin1()); | 181 | qDebug("diff = >%s<",diff.latin1()); |
182 | return diff.toFloat(); | 182 | return diff.toFloat(); |
183 | } | 183 | } |
184 | 184 | ||
185 | void Ntp::readLookups() | 185 | void Ntp::readLookups() |
186 | { | 186 | { |
187 | Config cfg("ntp",Config::User); | 187 | Config cfg("ntp",Config::User); |
188 | cfg.setGroup("lookups"); | 188 | cfg.setGroup("lookups"); |
189 | int lookupCount = cfg.readNumEntry("count",-1); | 189 | int lookupCount = cfg.readNumEntry("count",-1); |
190 | float last, shift, shiftPerSec; | 190 | float last, shift, shiftPerSec; |
191 | qDebug("lookupCount = %i",lookupCount); | 191 | qDebug("lookupCount = %i",lookupCount); |
192 | TableLookups->setNumCols( 3 ); | 192 | TableLookups->setNumCols( 3 ); |
193 | TableLookups->setNumRows( lookupCount); | 193 | TableLookups->setNumRows( lookupCount); |
194 | TableLookups->horizontalHeader()->setLabel(1,"secsSinceLast"); | 194 | TableLookups->horizontalHeader()->setLabel(1,"secsSinceLast"); |
195 | TableLookups->horizontalHeader()->setLabel(2,"timeShift"); | 195 | TableLookups->horizontalHeader()->setLabel(2,"timeShift"); |
196 | TableLookups->horizontalHeader()->setLabel(0,"shift/s"); | 196 | TableLookups->horizontalHeader()->setLabel(0,"shift/s"); |
197 | int cw = 50;//TableLookups->width()/4; | 197 | int cw = 50;//TableLookups->width()/4; |
198 | qDebug("column width %i",cw); | 198 | qDebug("column width %i",cw); |
199 | TableLookups->setColumnWidth( 0, cw+30 ); | 199 | TableLookups->setColumnWidth( 0, cw+30 ); |
200 | TableLookups->setColumnWidth( 1, cw ); | 200 | TableLookups->setColumnWidth( 1, cw ); |
201 | TableLookups->setColumnWidth( 2, cw ); | 201 | TableLookups->setColumnWidth( 2, cw ); |
202 | TableLookups->sortColumn(0, false, true ); | 202 | TableLookups->sortColumn(0, false, true ); |
203 | // TableLookups->setSorting( true ); | 203 | // TableLookups->setSorting( true ); |
204 | for (int i=0; i < lookupCount; i++) | 204 | for (int i=0; i < lookupCount; i++) |
205 | { | 205 | { |
206 | cfg.setGroup("lookup_"+QString::number(i)); | 206 | cfg.setGroup("lookup_"+QString::number(i)); |
207 | last = cfg.readEntry("secsSinceLast",0).toFloat(); | 207 | last = cfg.readEntry("secsSinceLast",0).toFloat(); |
208 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); | 208 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); |
209 | // qDebug("%i last %f",i,last); | 209 | // qDebug("%i last %f",i,last); |
210 | // qDebug("%i shift %f",i,shift); | 210 | // qDebug("%i shift %f",i,shift); |
211 | shiftPerSec = shift / last; | 211 | shiftPerSec = shift / last; |
212 | _shiftPerSec += shiftPerSec; | 212 | _shiftPerSec += shiftPerSec; |
213 | TableLookups->setText( i,0,QString::number(shiftPerSec)); | 213 | TableLookups->setText( i,0,QString::number(shiftPerSec)); |
214 | TableLookups->setText( i,2,QString::number(shift)); | 214 | TableLookups->setText( i,2,QString::number(shift)); |
215 | TableLookups->setText( i,1,QString::number(last)); | 215 | TableLookups->setText( i,1,QString::number(last)); |
216 | } | 216 | } |
217 | _shiftPerSec /= lookupCount+1; | 217 | _shiftPerSec /= lookupCount+1; |
218 | TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); | 218 | TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); |
219 | } | 219 | } |
220 | 220 | ||
221 | void Ntp::preditctTime() | 221 | void Ntp::preditctTime() |
222 | { | 222 | { |
223 | Config cfg("ntp",Config::User); | 223 | Config cfg("ntp",Config::User); |
224 | cfg.setGroup("lookups"); | 224 | cfg.setGroup("lookups"); |
225 | int lastTime = cfg.readNumEntry("time",0); | 225 | int lastTime = cfg.readNumEntry("time",0); |
226 | setenv( "TZ", tz->currentZone(), 1 ); | 226 | setenv( "TZ", tz->currentZone(), 1 ); |
227 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 227 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
228 | int corr = int((now - lastTime) * _shiftPerSec); | 228 | int corr = int((now - lastTime) * _shiftPerSec); |
229 | TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); | 229 | TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); |
230 | predictedTime = QDateTime::currentDateTime().addSecs(corr); | 230 | predictedTime = QDateTime::currentDateTime().addSecs(corr); |
231 | TextLabelPredTime->setText(predictedTime.toString()); | 231 | TextLabelPredTime->setText(predictedTime.toString()); |
232 | TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); | 232 | TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); |
233 | } | 233 | } |
234 | 234 | ||
235 | void Ntp::setPredictTime() | 235 | void Ntp::setPredictTime() |
236 | { | 236 | { |
237 | preditctTime(); | 237 | preditctTime(); |
238 | setTime( predictedTime ); | 238 | setTime( predictedTime ); |
239 | } | 239 | } |
240 | 240 | ||
241 | void Ntp::slotCheckNtp(int i) | 241 | void Ntp::slotCheckNtp(int i) |
242 | { | 242 | { |
243 | if (i == 0) | 243 | if (i == 0) |
244 | { | 244 | { |
245 | TextLabelMainPredTime->hide(); | 245 | TextLabelMainPredTime->hide(); |
246 | ButtonSetTime->setText( tr("Get time from network") ); | 246 | ButtonSetTime->setText( tr("Get time from network") ); |
247 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); | 247 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); |
248 | if ( ntpDelayElapsed() ) | 248 | if ( ntpDelayElapsed() ) |
249 | { | 249 | { |
250 | slotRunNtp(); | 250 | slotRunNtp(); |
251 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); | 251 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); |
252 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); | 252 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); |
253 | }else{ | 253 | }else{ |
254 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); | 254 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); |
255 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 255 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
256 | } | 256 | } |
257 | }else{ | 257 | }else{ |
258 | preditctTime(); | 258 | preditctTime(); |
259 | ButtonSetTime->setText( tr("Set predicted time") ); | 259 | ButtonSetTime->setText( tr("Set predicted time") ); |
260 | if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer()); | 260 | if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer()); |
261 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); | 261 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); |
262 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 262 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
263 | } | 263 | } |
264 | //ntpTimer->start(SpinBoxNtpDelay->value()*1000*60); | 264 | //ntpTimer->start(SpinBoxNtpDelay->value()*1000*60); |
265 | } | 265 | } |
266 | 266 | ||
267 | void Ntp::slotProbeNtpServer() | 267 | void Ntp::slotProbeNtpServer() |
268 | { | 268 | { |
269 | if ( ntpDelayElapsed() ) | 269 | ntpSock->connectToHost( getNtpServer() ,123); |
270 | ntpSock->connectToHost( getNtpServer() ,123); | ||
271 | } | 270 | } |
272 | 271 | ||
273 | void Ntp::slotNtpDelayChanged(int delay) | 272 | void Ntp::slotNtpDelayChanged(int delay) |
274 | { | 273 | { |
275 | ntpTimer->changeInterval( delay*1000*60 ); | 274 | ntpTimer->changeInterval( delay*1000*60 ); |
276 | } | 275 | } |
277 | 276 | ||
278 | void Ntp::ntpOutPut(QString out) | 277 | void Ntp::ntpOutPut(QString out) |
279 | { | 278 | { |
280 | MultiLineEditntpOutPut->append(out); | 279 | MultiLineEditntpOutPut->append(out); |
281 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); | 280 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); |
282 | } | 281 | } |