Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 58 |
1 files changed, 34 insertions, 24 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp index 364609d..bcbf7a1 100644 --- a/noncore/settings/netsystemtime/ntp.cpp +++ b/noncore/settings/netsystemtime/ntp.cpp | |||
@@ -41,13 +41,13 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | |||
41 | // _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); | 41 | // _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); |
42 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",41) ); | 42 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",41) ); |
43 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",42) ); | 43 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",42) ); |
44 | ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); | 44 | ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); |
45 | 45 | ||
46 | ntpTimer = new QTimer(this); | 46 | ntpTimer = new QTimer(this); |
47 | ntpTimer->start(SpinBoxNtpDelay->value()*100); | 47 | ntpTimer->start(SpinBoxNtpDelay->value()*1000*60); |
48 | 48 | ||
49 | ntpProcess = new OProcess( ); | 49 | ntpProcess = new OProcess( ); |
50 | connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), | 50 | connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), |
51 | SLOT(slotNtpDelayChanged(int)) ); | 51 | SLOT(slotNtpDelayChanged(int)) ); |
52 | 52 | ||
53 | ntpSock = new QSocket( this ); | 53 | ntpSock = new QSocket( this ); |
@@ -82,51 +82,60 @@ Ntp::~Ntp() | |||
82 | cfg.setGroup("settings"); | 82 | cfg.setGroup("settings"); |
83 | cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem()); | 83 | cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem()); |
84 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); | 84 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); |
85 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); | 85 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); |
86 | } | 86 | } |
87 | 87 | ||
88 | 88 | bool Ntp::ntpDelayElapsed() | |
89 | void Ntp::slotRunNtp() | ||
90 | { | 89 | { |
91 | Config cfg("ntp",Config::User); | 90 | Config cfg("ntp",Config::User); |
92 | cfg.setGroup("lookups"); | 91 | cfg.setGroup("lookups"); |
93 | int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | 92 | _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); |
93 | return (_lookupDiff - (SpinBoxNtpDelay->value()*60)) > -60; | ||
94 | } | ||
95 | |||
96 | QString Ntp::getNtpServer() | ||
97 | { | ||
98 | return ComboNtpSrv->currentText(); | ||
99 | } | ||
94 | 100 | ||
95 | if ( lookupDiff < SpinBoxNtpDelay->value()*60 ) | 101 | void Ntp::slotRunNtp() |
102 | { | ||
103 | if ( !ntpDelayElapsed() ) | ||
96 | { | 104 | { |
97 | switch ( | 105 | switch ( |
98 | QMessageBox::warning(this, tr("Run ntp?"), | 106 | QMessageBox::warning(this, tr("Run NTP?"), |
99 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ | 107 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ |
100 | QString::number(lookupDiff%60)+tr(" minutes elapsed since last loopup.")+ | 108 | QString::number(_lookupDiff/60)+tr(" minutes elapsed since last loopup.")+ |
101 | "<br>"+tr("Rerun ntp?"), | 109 | "<br>"+tr("Rerun NTP?"), |
102 | QMessageBox::Ok,QMessageBox::Cancel) | 110 | QMessageBox::Ok,QMessageBox::Cancel) |
103 | ) { | 111 | ) { |
104 | case QMessageBox::Ok: break; | 112 | case QMessageBox::Ok: break; |
105 | case QMessageBox::Cancel: return; | 113 | case QMessageBox::Cancel: return; |
106 | default: return; | 114 | default: return; |
107 | } | 115 | } |
108 | } | 116 | } |
109 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); | 117 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); |
110 | MultiLineEditntpOutPut->append( "\n"+tr("Running:")+"\nntpdate "+ ComboNtpSrv->currentText()+"\n"); | 118 | ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() ); |
111 | ntpProcess->clearArguments(); | 119 | ntpProcess->clearArguments(); |
112 | *ntpProcess << "ntpdate" << ComboNtpSrv->currentText(); | 120 | *ntpProcess << "ntpdate" << getNtpServer(); |
113 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); | 121 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); |
114 | if ( !ret ) { | 122 | if ( !ret ) { |
115 | qDebug("Error while executing ntp"); | 123 | qDebug("Error while executing ntpdate"); |
124 | ntpOutPut( tr("Error while executing ntpdate")); | ||
116 | } | 125 | } |
117 | } | 126 | } |
118 | 127 | ||
119 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | 128 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) |
120 | { | 129 | { |
121 | QString lineStr, lineStrOld; | 130 | QString lineStr, lineStrOld; |
122 | lineStr = buffer; | 131 | lineStr = buffer; |
123 | lineStr=lineStr.left(buflen); | 132 | lineStr=lineStr.left(buflen); |
124 | if (lineStr!=lineStrOld) | 133 | if (lineStr!=lineStrOld) |
125 | { | 134 | { |
126 | MultiLineEditntpOutPut->append(lineStr); | 135 | ntpOutPut(lineStr); |
127 | _ntpOutput += lineStr; | 136 | _ntpOutput += lineStr; |
128 | } | 137 | } |
129 | lineStrOld = lineStr; | 138 | lineStrOld = lineStr; |
130 | } | 139 | } |
131 | 140 | ||
132 | void Ntp::ntpFinished(OProcess*) | 141 | void Ntp::ntpFinished(OProcess*) |
@@ -148,13 +157,12 @@ void Ntp::ntpFinished(OProcess*) | |||
148 | if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) | 157 | if ( lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) |
149 | { | 158 | { |
150 | lookupCount++; | 159 | lookupCount++; |
151 | cfg.writeEntry("count",lookupCount); | 160 | cfg.writeEntry("count",lookupCount); |
152 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | 161 | cfg.setGroup("lookup_"+QString::number(lookupCount)); |
153 | _shiftPerSec = timeShift / secsSinceLast; | 162 | _shiftPerSec = timeShift / secsSinceLast; |
154 | // float nextCorr = _maxOffset / _shiftPerSec; | ||
155 | 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); |
156 | cfg.writeEntry("secsSinceLast",secsSinceLast); | 164 | cfg.writeEntry("secsSinceLast",secsSinceLast); |
157 | cfg.writeEntry("timeShift",QString::number(timeShift)); | 165 | cfg.writeEntry("timeShift",QString::number(timeShift)); |
158 | } | 166 | } |
159 | } | 167 | } |
160 | 168 | ||
@@ -209,13 +217,12 @@ void Ntp::readLookups() | |||
209 | _shiftPerSec /= lookupCount+1; | 217 | _shiftPerSec /= lookupCount+1; |
210 | TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); | 218 | TextLabelShift->setText(QString::number(_shiftPerSec)+tr(" seconds")); |
211 | } | 219 | } |
212 | 220 | ||
213 | void Ntp::preditctTime() | 221 | void Ntp::preditctTime() |
214 | { | 222 | { |
215 | qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | ||
216 | Config cfg("ntp",Config::User); | 223 | Config cfg("ntp",Config::User); |
217 | cfg.setGroup("lookups"); | 224 | cfg.setGroup("lookups"); |
218 | int lastTime = cfg.readNumEntry("time",0); | 225 | int lastTime = cfg.readNumEntry("time",0); |
219 | setenv( "TZ", tz->currentZone(), 1 ); | 226 | setenv( "TZ", tz->currentZone(), 1 ); |
220 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 227 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
221 | int corr = int((now - lastTime) * _shiftPerSec); | 228 | int corr = int((now - lastTime) * _shiftPerSec); |
@@ -235,38 +242,41 @@ void Ntp::slotCheckNtp(int i) | |||
235 | { | 242 | { |
236 | if (i == 0) | 243 | if (i == 0) |
237 | { | 244 | { |
238 | TextLabelMainPredTime->hide(); | 245 | TextLabelMainPredTime->hide(); |
239 | ButtonSetTime->setText( tr("Get time from network") ); | 246 | ButtonSetTime->setText( tr("Get time from network") ); |
240 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); | 247 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); |
241 | Config cfg("ntp",Config::User); | 248 | if ( ntpDelayElapsed() ) |
242 | cfg.setGroup("lookups"); | ||
243 | int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | ||
244 | if ( lookupDiff > SpinBoxNtpDelay->value()*60 ) | ||
245 | { | 249 | { |
250 | slotRunNtp(); | ||
246 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); | 251 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); |
247 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); | 252 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); |
248 | }else{ | 253 | }else{ |
249 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); | 254 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); |
250 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 255 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
251 | } | 256 | } |
252 | }else{ | 257 | }else{ |
253 | preditctTime(); | 258 | preditctTime(); |
254 | 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()); | ||
255 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); | 261 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); |
256 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 262 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
257 | } | 263 | } |
264 | //ntpTimer->start(SpinBoxNtpDelay->value()*1000*60); | ||
258 | } | 265 | } |
259 | 266 | ||
260 | void Ntp::slotProbeNtpServer() | 267 | void Ntp::slotProbeNtpServer() |
261 | { | 268 | { |
262 | Config cfg("ntp",Config::User); | 269 | if ( ntpDelayElapsed() ) |
263 | cfg.setGroup("lookups"); | 270 | ntpSock->connectToHost( getNtpServer() ,123); |
264 | int lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | ||
265 | if ( lookupDiff > SpinBoxNtpDelay->value()*60 ) | ||
266 | ntpSock->connectToHost( ComboNtpSrv->currentText() ,123); | ||
267 | } | 271 | } |
268 | 272 | ||
269 | void Ntp::slotNtpDelayChanged(int delay) | 273 | void Ntp::slotNtpDelayChanged(int delay) |
270 | { | 274 | { |
271 | ntpTimer->changeInterval( delay*100 ); | 275 | ntpTimer->changeInterval( delay*1000*60 ); |
276 | } | ||
277 | |||
278 | void Ntp::ntpOutPut(QString out) | ||
279 | { | ||
280 | MultiLineEditntpOutPut->append(out); | ||
281 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); | ||
272 | } | 282 | } |