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