-rw-r--r-- | noncore/settings/netsystemtime/ntp.cpp | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp index 111f7d4..6e74e32 100644 --- a/noncore/settings/netsystemtime/ntp.cpp +++ b/noncore/settings/netsystemtime/ntp.cpp | |||
@@ -1,376 +1,377 @@ | |||
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 <qtimer.h> | 4 | #include <qtimer.h> |
5 | #include <qtable.h> | 5 | #include <qtable.h> |
6 | #include <qlabel.h> | 6 | #include <qlabel.h> |
7 | #include <qsocket.h> | 7 | #include <qsocket.h> |
8 | #include <qlineedit.h> | 8 | #include <qlineedit.h> |
9 | #include <qspinbox.h> | 9 | #include <qspinbox.h> |
10 | #include <qcheckbox.h> | 10 | #include <qcheckbox.h> |
11 | #include <qtabwidget.h> | 11 | #include <qtabwidget.h> |
12 | #include <qlayout.h> | 12 | #include <qlayout.h> |
13 | #include <qmessagebox.h> | 13 | #include <qmessagebox.h> |
14 | #include <qmultilineedit.h> | 14 | #include <qmultilineedit.h> |
15 | #include <opie/oprocess.h> | 15 | #include <opie/oprocess.h> |
16 | #include <qpe/qpeapplication.h> | 16 | #include <qpe/qpeapplication.h> |
17 | #include <qpe/config.h> | 17 | #include <qpe/config.h> |
18 | #include <qpe/global.h> | 18 | #include <qpe/global.h> |
19 | #include <qpe/timeconversion.h> | 19 | #include <qpe/timeconversion.h> |
20 | #include <qpe/tzselect.h> | 20 | #include <qpe/tzselect.h> |
21 | #include <qpe/timestring.h> | 21 | #include <qpe/timestring.h> |
22 | #include <qpe/qpedialog.h> | 22 | #include <qpe/qpedialog.h> |
23 | #include <qpe/datebookdb.h> | 23 | #include <qpe/datebookdb.h> |
24 | #include <qpe/qcopenvelope_qws.h> | 24 | #include <qpe/qcopenvelope_qws.h> |
25 | #include <sys/time.h> | 25 | #include <sys/time.h> |
26 | #include <time.h> | 26 | #include <time.h> |
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | 28 | ||
29 | 29 | ||
30 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | 30 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) |
31 | : SetDateTime( parent, name, fl ) | 31 | : SetDateTime( parent, name, fl ) |
32 | { | 32 | { |
33 | Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File); | 33 | Config ntpSrvs(QPEApplication::qpeDir()+"etc/ntpservers",Config::File); |
34 | ntpSrvs.setGroup("servers"); | 34 | ntpSrvs.setGroup("servers"); |
35 | int srvCount = ntpSrvs.readNumEntry("count", 0 ); | 35 | int srvCount = ntpSrvs.readNumEntry("count", 0 ); |
36 | for (int i = 0; i < srvCount; i++) | 36 | for (int i = 0; i < srvCount; i++) |
37 | { | 37 | { |
38 | ntpSrvs.setGroup(QString::number(i)); | 38 | ntpSrvs.setGroup(QString::number(i)); |
39 | ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") ); | 39 | ComboNtpSrv->insertItem( ntpSrvs.readEntry("name") ); |
40 | } | 40 | } |
41 | if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de")); | 41 | if ( srvCount==0 ) ComboNtpSrv->insertItem(tr("time.fu-berlin.de")); |
42 | 42 | ||
43 | Config cfg("ntp",Config::User); | 43 | Config cfg("ntp",Config::User); |
44 | cfg.setGroup("settings"); | 44 | cfg.setGroup("settings"); |
45 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",720) ); | 45 | SpinBoxMinLookupDelay->setValue( cfg.readNumEntry("minLookupDiff",720) ); |
46 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",1440) ); | 46 | SpinBoxNtpDelay->setValue( cfg.readNumEntry("ntpRefreshFreq",1440) ); |
47 | ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); | 47 | ComboNtpSrv->setCurrentItem( cfg.readNumEntry("ntpServer", 0) ); |
48 | 48 | ||
49 | //make tab order | 49 | //make tab order |
50 | 50 | ||
51 | TabWidgetMain->removePage( tabMain ); | 51 | TabWidgetMain->removePage( tabMain ); |
52 | TabWidgetMain->removePage( tabManualSetTime ); | 52 | TabWidgetMain->removePage( tabManualSetTime ); |
53 | TabWidgetMain->removePage( TabSettings ); | 53 | TabWidgetMain->removePage( TabSettings ); |
54 | TabWidgetMain->removePage( tabPredict ); | 54 | TabWidgetMain->removePage( tabPredict ); |
55 | TabWidgetMain->removePage( tabNtp ); | 55 | TabWidgetMain->removePage( tabNtp ); |
56 | 56 | ||
57 | TabWidgetMain->insertTab( tabMain, tr( "Main" ) ); | 57 | TabWidgetMain->insertTab( tabMain, tr( "Main" ) ); |
58 | TabWidgetMain->insertTab( tabManualSetTime, tr( "Manual" ) ); | 58 | TabWidgetMain->insertTab( tabManualSetTime, tr( "Manual" ) ); |
59 | TabWidgetMain->insertTab( TabSettings, tr( "Settings" ) ); | 59 | TabWidgetMain->insertTab( TabSettings, tr( "Settings" ) ); |
60 | TabWidgetMain->insertTab( tabPredict, tr( "Predict" ) ); | 60 | TabWidgetMain->insertTab( tabPredict, tr( "Predict" ) ); |
61 | TabWidgetMain->insertTab( tabNtp, tr( "NTP" ) ); | 61 | TabWidgetMain->insertTab( tabNtp, tr( "NTP" ) ); |
62 | NtpBaseLayout->addWidget( TabWidgetMain, 0, 0 ); | 62 | NtpBaseLayout->addWidget( TabWidgetMain, 0, 0 ); |
63 | 63 | ||
64 | 64 | ||
65 | 65 | ||
66 | bool advMode = cfg.readBoolEntry("advancedFeatures", false ); | 66 | bool advMode = cfg.readBoolEntry("advancedFeatures", false ); |
67 | showAdvancedFeatures(advMode); | 67 | showAdvancedFeatures(advMode); |
68 | CheckBoxAdvSettings->setChecked( advMode ); | 68 | CheckBoxAdvSettings->setChecked( advMode ); |
69 | connect( CheckBoxAdvSettings, SIGNAL( toggled( bool ) ), | 69 | connect( CheckBoxAdvSettings, SIGNAL( toggled( bool ) ), |
70 | SLOT( showAdvancedFeatures( bool ) ) ); | 70 | SLOT( showAdvancedFeatures( bool ) ) ); |
71 | 71 | ||
72 | makeChannel(); | 72 | makeChannel(); |
73 | 73 | ||
74 | ntpTimer = new QTimer(this); | 74 | ntpTimer = new QTimer(this); |
75 | 75 | ||
76 | ntpProcess = new OProcess( ); | 76 | ntpProcess = new OProcess( ); |
77 | connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), | 77 | connect( SpinBoxNtpDelay, SIGNAL( valueChanged(int) ), |
78 | SLOT(slotNtpDelayChanged(int)) ); | 78 | SLOT(slotNtpDelayChanged(int)) ); |
79 | 79 | ||
80 | ntpSock = new QSocket( this ); | 80 | ntpSock = new QSocket( this ); |
81 | connect( ntpSock, SIGNAL( error(int) ), | 81 | connect( ntpSock, SIGNAL( error(int) ), |
82 | SLOT(slotCheckNtp(int)) ); | 82 | SLOT(slotCheckNtp(int)) ); |
83 | slotProbeNtpServer(); | 83 | slotProbeNtpServer(); |
84 | 84 | ||
85 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), | 85 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), |
86 | this, SLOT(getNtpOutput(OProcess*,char*,int))); | 86 | this, SLOT(getNtpOutput(OProcess*,char*,int))); |
87 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), | 87 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), |
88 | this, SLOT(ntpFinished(OProcess*))); | 88 | this, SLOT(ntpFinished(OProcess*))); |
89 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); | 89 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); |
90 | connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); | 90 | connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); |
91 | connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime())); | 91 | connect(PushButtonSetPredTime, SIGNAL(clicked()), this, SLOT(setPredictTime())); |
92 | slotCheckNtp(-1); | 92 | slotCheckNtp(-1); |
93 | readLookups(); | 93 | readLookups(); |
94 | } | 94 | } |
95 | 95 | ||
96 | Ntp::~Ntp() | 96 | Ntp::~Ntp() |
97 | { | 97 | { |
98 | delete ntpProcess; | 98 | delete ntpProcess; |
99 | Config ntpSrvs("/etc/ntpservers",Config::File); | 99 | Config ntpSrvs("/etc/ntpservers",Config::File); |
100 | ntpSrvs.setGroup("servers"); | 100 | ntpSrvs.setGroup("servers"); |
101 | int srvCount = ComboNtpSrv->count(); | 101 | int srvCount = ComboNtpSrv->count(); |
102 | ntpSrvs.writeEntry("count", srvCount); | 102 | ntpSrvs.writeEntry("count", srvCount); |
103 | for (int i = 0; i < srvCount; i++){ | 103 | for (int i = 0; i < srvCount; i++){ |
104 | ntpSrvs.setGroup(QString::number(i)); | 104 | ntpSrvs.setGroup(QString::number(i)); |
105 | ntpSrvs.writeEntry( "name", ComboNtpSrv->text(i) ); | 105 | ntpSrvs.writeEntry( "name", ComboNtpSrv->text(i) ); |
106 | } | 106 | } |
107 | Config cfg("ntp",Config::User); | 107 | Config cfg("ntp",Config::User); |
108 | cfg.setGroup("settings"); | 108 | cfg.setGroup("settings"); |
109 | cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem()); | 109 | cfg.writeEntry("ntpServer", ComboNtpSrv->currentItem()); |
110 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); | 110 | cfg.writeEntry( "minLookupDiff", SpinBoxMinLookupDelay->value() ); |
111 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); | 111 | cfg.writeEntry( "ntpRefreshFreq", SpinBoxNtpDelay->value() ); |
112 | cfg.writeEntry( "advancedFeatures", CheckBoxAdvSettings->isChecked() ); | 112 | cfg.writeEntry( "advancedFeatures", CheckBoxAdvSettings->isChecked() ); |
113 | } | 113 | } |
114 | 114 | ||
115 | bool Ntp::ntpDelayElapsed() | 115 | bool Ntp::ntpDelayElapsed() |
116 | { | 116 | { |
117 | Config cfg("ntp",Config::User); | 117 | Config cfg("ntp",Config::User); |
118 | cfg.setGroup("lookups"); | 118 | cfg.setGroup("lookups"); |
119 | _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); | 119 | _lookupDiff = TimeConversion::toUTC(QDateTime::currentDateTime()) - cfg.readNumEntry("time",0); |
120 | if (_lookupDiff < 0) return true; | 120 | if (_lookupDiff < 0) return true; |
121 | int i =_lookupDiff - (SpinBoxNtpDelay->value()*60); | 121 | int i =_lookupDiff - (SpinBoxNtpDelay->value()*60); |
122 | return i > -60; | 122 | return i > -60; |
123 | } | 123 | } |
124 | 124 | ||
125 | QString Ntp::getNtpServer() | 125 | QString Ntp::getNtpServer() |
126 | { | 126 | { |
127 | return ComboNtpSrv->currentText(); | 127 | return ComboNtpSrv->currentText(); |
128 | } | 128 | } |
129 | 129 | ||
130 | void Ntp::slotRunNtp() | 130 | void Ntp::slotRunNtp() |
131 | { | 131 | { |
132 | if ( !ntpDelayElapsed() && CheckBoxAdvSettings->isChecked() ) | 132 | if ( !ntpDelayElapsed() && CheckBoxAdvSettings->isChecked() ) |
133 | { | 133 | { |
134 | switch ( | 134 | switch ( |
135 | QMessageBox::warning(this, tr("Run NTP?"), | 135 | QMessageBox::warning(this, tr("Run NTP?"), |
136 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ | 136 | tr("You asked for a delay of ")+SpinBoxNtpDelay->text()+tr(" minutes, but only ")+ |
137 | QString::number(_lookupDiff/60)+tr(" minutes elapsed since last lookup.")+ | 137 | QString::number(_lookupDiff/60)+tr(" minutes elapsed since last lookup.")+ |
138 | "<br>"+tr("Rerun NTP?"), | 138 | "<br>"+tr("Rerun NTP?"), |
139 | QMessageBox::Ok,QMessageBox::Cancel) | 139 | QMessageBox::Ok,QMessageBox::Cancel) |
140 | ) { | 140 | ) { |
141 | case QMessageBox::Ok: break; | 141 | case QMessageBox::Ok: break; |
142 | case QMessageBox::Cancel: return; | 142 | case QMessageBox::Cancel: return; |
143 | default: return; | 143 | default: return; |
144 | } | 144 | } |
145 | } | 145 | } |
146 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); | 146 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); |
147 | ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() ); | 147 | ntpOutPut( tr("Running:")+"\nntpdate "+getNtpServer() ); |
148 | 148 | ||
149 | ntpProcess->clearArguments(); | 149 | ntpProcess->clearArguments(); |
150 | *ntpProcess << "ntpdate" << getNtpServer(); | 150 | *ntpProcess << "ntpdate" << getNtpServer(); |
151 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); | 151 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); |
152 | if ( !ret ) { | 152 | if ( !ret ) { |
153 | QMessageBox::critical(this, tr("ntp error"), | 153 | QMessageBox::critical(this, tr("ntp error"), |
154 | tr("Error while getting time form network!")); | 154 | tr("Error while getting time form network!")); |
155 | qDebug("Error while executing ntpdate"); | 155 | qDebug("Error while executing ntpdate"); |
156 | ntpOutPut( tr("Error while executing ntpdate")); | 156 | ntpOutPut( tr("Error while executing ntpdate")); |
157 | } | 157 | } |
158 | } | 158 | } |
159 | 159 | ||
160 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | 160 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) |
161 | { | 161 | { |
162 | if (! proc ) qDebug("Ntp::getNtpOutput OProcess is null"); | 162 | if (! proc ) qDebug("Ntp::getNtpOutput OProcess is null"); |
163 | QString lineStr, lineStrOld; | 163 | QString lineStr, lineStrOld; |
164 | lineStr = buffer; | 164 | lineStr = buffer; |
165 | lineStr=lineStr.left(buflen); | 165 | lineStr=lineStr.left(buflen); |
166 | if (lineStr!=lineStrOld) | 166 | if (lineStr!=lineStrOld) |
167 | { | 167 | { |
168 | ntpOutPut(lineStr); | 168 | ntpOutPut(lineStr); |
169 | _ntpOutput += lineStr; | 169 | _ntpOutput += lineStr; |
170 | } | 170 | } |
171 | lineStrOld = lineStr; | 171 | lineStrOld = lineStr; |
172 | } | 172 | } |
173 | 173 | ||
174 | void Ntp::ntpFinished(OProcess *p) | 174 | void Ntp::ntpFinished(OProcess *p) |
175 | { | 175 | { |
176 | qDebug("p->exitStatus() %i",p->exitStatus()); | 176 | qDebug("p->exitStatus() %i",p->exitStatus()); |
177 | if (p->exitStatus()!=0 || !p->normalExit()) | 177 | if (p->exitStatus()!=0 || !p->normalExit()) |
178 | { | 178 | { |
179 | QMessageBox::critical(this, tr("ntp error"), | 179 | if ( isVisible() ) { |
180 | QMessageBox::critical(this, tr("ntp error"), | ||
180 | tr("Error while getting time form\n server")+ | 181 | tr("Error while getting time form\n server")+ |
181 | getNtpServer()+"\n"+ | 182 | getNtpServer()+"\n"+ |
182 | _ntpOutput ); | 183 | _ntpOutput ); |
183 | // TabWidgetMain->setCurrentPage( 1 ); | 184 | TabWidgetMain->showPage( tabManualSetTime ); |
184 | TabWidgetMain->showPage( tabManualSetTime ); | 185 | } |
185 | 186 | ||
186 | return; | 187 | return; |
187 | } | 188 | } |
188 | 189 | ||
189 | Global::writeHWClock(); | 190 | Global::writeHWClock(); |
190 | // since time has changed quickly load in the datebookdb | 191 | // since time has changed quickly load in the datebookdb |
191 | // to allow the alarm server to get a better grip on itself | 192 | // to allow the alarm server to get a better grip on itself |
192 | // (example re-trigger alarms for when we travel back in time) | 193 | // (example re-trigger alarms for when we travel back in time) |
193 | DateBookDB db; | 194 | DateBookDB db; |
194 | 195 | ||
195 | // QCopEnvelope timeApplet( "QPE/TaskBar", "reloadApplets()" ); | 196 | // QCopEnvelope timeApplet( "QPE/TaskBar", "reloadApplets()" ); |
196 | // timeApplet << ""; | 197 | // timeApplet << ""; |
197 | 198 | ||
198 | Config cfg("ntp",Config::User); | 199 | Config cfg("ntp",Config::User); |
199 | cfg.setGroup("lookups"); | 200 | cfg.setGroup("lookups"); |
200 | int lastLookup = cfg.readNumEntry("time",0); | 201 | int lastLookup = cfg.readNumEntry("time",0); |
201 | int lookupCount = cfg.readNumEntry("count",0); | 202 | int lookupCount = cfg.readNumEntry("count",0); |
202 | bool lastNtp = cfg.readBoolEntry("lastNtp",false); | 203 | bool lastNtp = cfg.readBoolEntry("lastNtp",false); |
203 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 204 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
204 | cfg.writeEntry("time", time); | 205 | cfg.writeEntry("time", time); |
205 | 206 | ||
206 | float timeShift = getTimeShift(); | 207 | float timeShift = getTimeShift(); |
207 | if (timeShift == 0.0) return; | 208 | if (timeShift == 0.0) return; |
208 | int secsSinceLast = time - lastLookup; | 209 | int secsSinceLast = time - lastLookup; |
209 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); | 210 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); |
210 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); | 211 | TextLabelTimeShift->setText(QString::number(timeShift)+tr(" seconds")); |
211 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) | 212 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60* SpinBoxMinLookupDelay->value()) |
212 | { | 213 | { |
213 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | 214 | cfg.setGroup("lookup_"+QString::number(lookupCount)); |
214 | lookupCount++; | 215 | lookupCount++; |
215 | _shiftPerSec = timeShift / secsSinceLast; | 216 | _shiftPerSec = timeShift / secsSinceLast; |
216 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); | 217 | qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); |
217 | cfg.writeEntry("secsSinceLast",secsSinceLast); | 218 | cfg.writeEntry("secsSinceLast",secsSinceLast); |
218 | cfg.writeEntry("timeShift",QString::number(timeShift)); | 219 | cfg.writeEntry("timeShift",QString::number(timeShift)); |
219 | cfg.setGroup("lookups"); | 220 | cfg.setGroup("lookups"); |
220 | cfg.writeEntry("count",lookupCount); | 221 | cfg.writeEntry("count",lookupCount); |
221 | cfg.writeEntry("lastNtp",true); | 222 | cfg.writeEntry("lastNtp",true); |
222 | } | 223 | } |
223 | } | 224 | } |
224 | 225 | ||
225 | 226 | ||
226 | float Ntp::getTimeShift() | 227 | float Ntp::getTimeShift() |
227 | { | 228 | { |
228 | QString _offset = "offset"; | 229 | QString _offset = "offset"; |
229 | QString _sec = "sec"; | 230 | QString _sec = "sec"; |
230 | QRegExp _reOffset = QRegExp(_offset); | 231 | QRegExp _reOffset = QRegExp(_offset); |
231 | QRegExp _reEndOffset = QRegExp(_sec); | 232 | QRegExp _reEndOffset = QRegExp(_sec); |
232 | int posOffset = _reOffset.match( _ntpOutput ); | 233 | int posOffset = _reOffset.match( _ntpOutput ); |
233 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); | 234 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); |
234 | posOffset += _offset.length() + 1; | 235 | posOffset += _offset.length() + 1; |
235 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); | 236 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); |
236 | qDebug("%s", _ntpOutput.latin1()); | 237 | qDebug("%s", _ntpOutput.latin1()); |
237 | qDebug("diff = >%s<",diff.latin1()); | 238 | qDebug("diff = >%s<",diff.latin1()); |
238 | return diff.toFloat(); | 239 | return diff.toFloat(); |
239 | } | 240 | } |
240 | 241 | ||
241 | void Ntp::readLookups() | 242 | void Ntp::readLookups() |
242 | { | 243 | { |
243 | Config cfg("ntp",Config::User); | 244 | Config cfg("ntp",Config::User); |
244 | cfg.setGroup("lookups"); | 245 | cfg.setGroup("lookups"); |
245 | int lookupCount = cfg.readNumEntry("count",0); | 246 | int lookupCount = cfg.readNumEntry("count",0); |
246 | float last, shift, shiftPerSec; | 247 | float last, shift, shiftPerSec; |
247 | qDebug("lookupCount = %i",lookupCount); | 248 | qDebug("lookupCount = %i",lookupCount); |
248 | TableLookups->setNumCols( 3 ); | 249 | TableLookups->setNumCols( 3 ); |
249 | TableLookups->setNumRows( lookupCount); | 250 | TableLookups->setNumRows( lookupCount); |
250 | TableLookups->horizontalHeader()->setLabel(1,tr("last [h]")); | 251 | TableLookups->horizontalHeader()->setLabel(1,tr("last [h]")); |
251 | TableLookups->horizontalHeader()->setLabel(2,tr("offset [s]")); | 252 | TableLookups->horizontalHeader()->setLabel(2,tr("offset [s]")); |
252 | TableLookups->horizontalHeader()->setLabel(0,tr("shift [s/h]")); | 253 | TableLookups->horizontalHeader()->setLabel(0,tr("shift [s/h]")); |
253 | int cw = TableLookups->width()/4; | 254 | int cw = TableLookups->width()/4; |
254 | qDebug("column width %i",cw); | 255 | qDebug("column width %i",cw); |
255 | cw = 50; | 256 | cw = 50; |
256 | TableLookups->setColumnWidth( 0, cw+30 ); | 257 | TableLookups->setColumnWidth( 0, cw+30 ); |
257 | TableLookups->setColumnWidth( 1, cw ); | 258 | TableLookups->setColumnWidth( 1, cw ); |
258 | TableLookups->setColumnWidth( 2, cw ); | 259 | TableLookups->setColumnWidth( 2, cw ); |
259 | TableLookups->sortColumn(0, false, true ); | 260 | TableLookups->sortColumn(0, false, true ); |
260 | // TableLookups->setSorting( true ); | 261 | // TableLookups->setSorting( true ); |
261 | _shiftPerSec = 0; | 262 | _shiftPerSec = 0; |
262 | for (int i=0; i < lookupCount; i++) | 263 | for (int i=0; i < lookupCount; i++) |
263 | { | 264 | { |
264 | cfg.setGroup("lookup_"+QString::number(i)); | 265 | cfg.setGroup("lookup_"+QString::number(i)); |
265 | last = cfg.readEntry("secsSinceLast",0).toFloat(); | 266 | last = cfg.readEntry("secsSinceLast",0).toFloat(); |
266 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); | 267 | shift = QString(cfg.readEntry("timeShift",0)).toFloat(); |
267 | shiftPerSec = shift / last; | 268 | shiftPerSec = shift / last; |
268 | qDebug("%i shift %f",i,shiftPerSec); | 269 | qDebug("%i shift %f",i,shiftPerSec); |
269 | _shiftPerSec += shiftPerSec; | 270 | _shiftPerSec += shiftPerSec; |
270 | TableLookups->setText( i,0,QString::number(shiftPerSec*60*60)); | 271 | TableLookups->setText( i,0,QString::number(shiftPerSec*60*60)); |
271 | TableLookups->setText( i,2,QString::number(shift)); | 272 | TableLookups->setText( i,2,QString::number(shift)); |
272 | TableLookups->setText( i,1,QString::number(last/(60*60))); | 273 | TableLookups->setText( i,1,QString::number(last/(60*60))); |
273 | } | 274 | } |
274 | _shiftPerSec /= lookupCount; | 275 | _shiftPerSec /= lookupCount; |
275 | TextLabelShift->setText(QString::number(_shiftPerSec*60*60)+tr(" s/h")); | 276 | TextLabelShift->setText(QString::number(_shiftPerSec*60*60)+tr(" s/h")); |
276 | } | 277 | } |
277 | 278 | ||
278 | void Ntp::preditctTime() | 279 | void Ntp::preditctTime() |
279 | { | 280 | { |
280 | Config cfg("ntp",Config::User); | 281 | Config cfg("ntp",Config::User); |
281 | cfg.setGroup("lookups"); | 282 | cfg.setGroup("lookups"); |
282 | int lastTime = cfg.readNumEntry("time",0); | 283 | int lastTime = cfg.readNumEntry("time",0); |
283 | cfg.writeEntry("lastNtp",true); | 284 | cfg.writeEntry("lastNtp",true); |
284 | setenv( "TZ", tz->currentZone(), 1 ); | 285 | setenv( "TZ", tz->currentZone(), 1 ); |
285 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 286 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
286 | int corr = int((now - lastTime) * _shiftPerSec); | 287 | int corr = int((now - lastTime) * _shiftPerSec); |
287 | TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); | 288 | TextLabelEstimatedShift->setText(QString::number(corr)+tr(" seconds")); |
288 | predictedTime = QDateTime::currentDateTime().addSecs(corr); | 289 | predictedTime = QDateTime::currentDateTime().addSecs(corr); |
289 | TextLabelPredTime->setText(predictedTime.toString()); | 290 | TextLabelPredTime->setText(predictedTime.toString()); |
290 | TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); | 291 | TextLabelMainPredTime->setText(tr("Predicted time:")+"<br><b>"+predictedTime.toString()+"</b>"); |
291 | } | 292 | } |
292 | 293 | ||
293 | void Ntp::setPredictTime() | 294 | void Ntp::setPredictTime() |
294 | { | 295 | { |
295 | preditctTime(); | 296 | preditctTime(); |
296 | setTime( predictedTime ); | 297 | setTime( predictedTime ); |
297 | } | 298 | } |
298 | 299 | ||
299 | void Ntp::slotCheckNtp(int i) | 300 | void Ntp::slotCheckNtp(int i) |
300 | { | 301 | { |
301 | if (i == 0) | 302 | if (i == 0) |
302 | { | 303 | { |
303 | TextLabelMainPredTime->hide(); | 304 | TextLabelMainPredTime->hide(); |
304 | ButtonSetTime->setText( tr("Get time from network") ); | 305 | ButtonSetTime->setText( tr("Get time from network") ); |
305 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); | 306 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(slotRunNtp()) ); |
306 | if ( ntpDelayElapsed() ) | 307 | if ( ntpDelayElapsed() ) |
307 | { | 308 | { |
308 | slotRunNtp(); | 309 | slotRunNtp(); |
309 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); | 310 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotProbeNtpServer()) ); |
310 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); | 311 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotRunNtp()) ); |
311 | }else{ | 312 | }else{ |
312 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); | 313 | disconnect(ntpTimer, SIGNAL( timeout() ), this, SLOT(slotRunNtp()) ); |
313 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 314 | connect(ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
314 | } | 315 | } |
315 | }else{ | 316 | }else{ |
316 | preditctTime(); | 317 | preditctTime(); |
317 | ButtonSetTime->setText( tr("Set predicted time: ")+predictedTime.toString() ); | 318 | ButtonSetTime->setText( tr("Set predicted time: ")+predictedTime.toString() ); |
318 | if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer()); | 319 | if (i>0)ntpOutPut(tr("Could not connect to server ")+getNtpServer()); |
319 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); | 320 | connect( ButtonSetTime, SIGNAL(clicked()), SLOT(setPredictTime()) ); |
320 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); | 321 | connect( ntpTimer, SIGNAL( timeout() ), SLOT(slotProbeNtpServer()) ); |
321 | } | 322 | } |
322 | } | 323 | } |
323 | 324 | ||
324 | void Ntp::slotProbeNtpServer() | 325 | void Ntp::slotProbeNtpServer() |
325 | { | 326 | { |
326 | ntpSock->connectToHost( getNtpServer() ,123); | 327 | ntpSock->connectToHost( getNtpServer() ,123); |
327 | } | 328 | } |
328 | 329 | ||
329 | void Ntp::slotNtpDelayChanged(int delay) | 330 | void Ntp::slotNtpDelayChanged(int delay) |
330 | { | 331 | { |
331 | ntpTimer->changeInterval( delay*1000*60 ); | 332 | ntpTimer->changeInterval( delay*1000*60 ); |
332 | } | 333 | } |
333 | 334 | ||
334 | void Ntp::ntpOutPut(QString out) | 335 | void Ntp::ntpOutPut(QString out) |
335 | { | 336 | { |
336 | 337 | ||
337 | MultiLineEditntpOutPut->append(out); | 338 | MultiLineEditntpOutPut->append(out); |
338 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); | 339 | MultiLineEditntpOutPut->setCursorPosition(MultiLineEditntpOutPut->numLines() + 1,0,FALSE); |
339 | } | 340 | } |
340 | 341 | ||
341 | 342 | ||
342 | void Ntp::makeChannel() | 343 | void Ntp::makeChannel() |
343 | { | 344 | { |
344 | channel = new QCopChannel( "QPE/Application/netsystemtime", this ); | 345 | channel = new QCopChannel( "QPE/Application/netsystemtime", this ); |
345 | connect( channel, SIGNAL(received(const QCString&, const QByteArray&)), | 346 | connect( channel, SIGNAL(received(const QCString&, const QByteArray&)), |
346 | this, SLOT(receive(const QCString&, const QByteArray&)) ); | 347 | this, SLOT(receive(const QCString&, const QByteArray&)) ); |
347 | } | 348 | } |
348 | 349 | ||
349 | 350 | ||
350 | 351 | ||
351 | void Ntp::receive(const QCString &msg, const QByteArray &arg) | 352 | void Ntp::receive(const QCString &msg, const QByteArray &arg) |
352 | { | 353 | { |
353 | qDebug("QCop(Ntp) "+msg+" "+QCString(arg)); | 354 | qDebug("QCop(Ntp) "+msg+" "+QCString(arg)); |
354 | if ( msg == "ntpLookup(QString)" ) | 355 | if ( msg == "ntpLookup(QString)" ) |
355 | { | 356 | { |
356 | slotRunNtp(); | 357 | slotRunNtp(); |
357 | } | 358 | } |
358 | if ( msg == "setPredictedTime(QString)" ) | 359 | if ( msg == "setPredictedTime(QString)" ) |
359 | { | 360 | { |
360 | setPredictTime(); | 361 | setPredictTime(); |
361 | }else{ | 362 | }else{ |
362 | qDebug("Ntp::receive: Huh what do ya want"); | 363 | qDebug("Ntp::receive: Huh what do ya want"); |
363 | } | 364 | } |
364 | } | 365 | } |
365 | 366 | ||
366 | void Ntp::setDocument(const QString &fileName) | 367 | void Ntp::setDocument(const QString &fileName) |
367 | { | 368 | { |
368 | qDebug("Ntp::setDocument( %s )",fileName.latin1()); | 369 | qDebug("Ntp::setDocument( %s )",fileName.latin1()); |
369 | } | 370 | } |
370 | 371 | ||
371 | void Ntp::showAdvancedFeatures(bool advMode) | 372 | void Ntp::showAdvancedFeatures(bool advMode) |
372 | { | 373 | { |
373 | if (advMode) { | 374 | if (advMode) { |
374 | 375 | ||
375 | TabWidgetMain->addTab( tabPredict, tr( "Predict" ) ); | 376 | TabWidgetMain->addTab( tabPredict, tr( "Predict" ) ); |
376 | TabWidgetMain->addTab( tabNtp, tr( "NTP" ) ); | 377 | TabWidgetMain->addTab( tabNtp, tr( "NTP" ) ); |