summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/main.cpp12
-rw-r--r--noncore/settings/netsystemtime/netsystemtime.pro10
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp146
-rw-r--r--noncore/settings/netsystemtime/ntp.h37
-rw-r--r--noncore/settings/netsystemtime/ntpbase.ui322
-rw-r--r--noncore/settings/netsystemtime/opie-netsystemtime.control8
6 files changed, 535 insertions, 0 deletions
diff --git a/noncore/settings/netsystemtime/main.cpp b/noncore/settings/netsystemtime/main.cpp
new file mode 100644
index 0000000..2b23751
--- a/dev/null
+++ b/noncore/settings/netsystemtime/main.cpp
@@ -0,0 +1,12 @@
1#include "ntp.h"
2#include <qpe/qpeapplication.h>
3
4int main( int argc, char ** argv )
5{
6 QPEApplication a( argc, argv );
7
8 Ntp mw;
9 a.showMainWidget( &mw );
10
11 return a.exec();
12}
diff --git a/noncore/settings/netsystemtime/netsystemtime.pro b/noncore/settings/netsystemtime/netsystemtime.pro
new file mode 100644
index 0000000..b98e45d
--- a/dev/null
+++ b/noncore/settings/netsystemtime/netsystemtime.pro
@@ -0,0 +1,10 @@
1 TEMPLATE= app
2 CONFIG = qt warn_on debug
3 #CONFIG = qt warn_on release
4 HEADERS = ntp.h
5 SOURCES = main.cpp ntp.cpp
6 INCLUDEPATH+= $(OPIEDIR)/include
7 DEPENDPATH+= $(OPIEDIR)/include
8LIBS += -lqpe -lopie
9 INTERFACES= ntpbase.ui
10 TARGET = ../../bin/netsystemtime
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
new file mode 100644
index 0000000..c2b64f0
--- a/dev/null
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -0,0 +1,146 @@
1#include "ntp.h"
2#include <qpushbutton.h>
3#include <qregexp.h>
4#include <qtable.h>
5#include <qlabel.h>
6#include <qlineedit.h>
7#include <qmultilineedit.h>
8#include <opie/oprocess.h>
9#include <qpe/config.h>
10#include <qpe/global.h>
11#include <qpe/timeconversion.h>
12#include <qpe/tzselect.h>
13#include <qpe/timestring.h>
14#include <qpe/qpedialog.h>
15#include <sys/time.h>
16#include <time.h>
17#include <stdlib.h>
18
19
20Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
21 : NtpBase( parent, name, fl )
22{
23 Config cfg("ntp",Config::User);
24 cfg.setGroup("settings");
25 _maxOffset = cfg.readNumEntry("maxOffset",5);
26 _minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
27 LineEditNtpServer->setText( cfg.readEntry("ntpServer") );
28 ntpProcess = new OProcess( );
29 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
30 this, SLOT(getNtpOutput(OProcess*,char*,int)));
31 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
32 this, SLOT(ntpFinished(OProcess*)));
33 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
34 _nextCorrection = new QTimer( this );
35 connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) );
36 slotRunNtp();
37 readLookups();
38}
39
40Ntp::~Ntp()
41{
42 Config cfg("ntp",Config::User);
43 cfg.setGroup("settings");
44 cfg.writeEntry("ntpServer", LineEditNtpServer->text());
45}
46
47
48void Ntp::slotRunNtp()
49{
50 TextLabelStartTime->setText(QDateTime::currentDateTime().toString());
51 ntpProcess->clearArguments();
52 *ntpProcess << "ntpdate" << LineEditNtpServer->text();
53 bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput);
54 if ( !ret ) {
55 qDebug("Error while executing ntp");
56 outPut->append("\nError while executing\n\n");
57 }
58}
59
60void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
61{
62 QString lineStr, lineStrOld;
63 lineStr = buffer;
64 lineStr=lineStr.left(buflen);
65 if (lineStr!=lineStrOld)
66 {
67 outPut->append(lineStr);
68 _ntpOutput += lineStr;
69 }
70 lineStrOld = lineStr;
71}
72
73void Ntp::ntpFinished(OProcess*)
74{
75 Config cfg("ntp",Config::User);
76 cfg.setGroup("lookups");
77 int lastLookup = cfg.readNumEntry("time",0);
78 int lookupCount = cfg.readNumEntry("count",-1);
79 int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
80 cfg.writeEntry("time", time);
81
82 float timeShift = getTimeShift();
83 if (timeShift = 0.0) return;
84 int secsSinceLast = time - lastLookup;
85 TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
86 if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff)
87 {
88 lookupCount++;
89 cfg.writeEntry("count",lookupCount);
90 cfg.setGroup("lookup_"+QString::number(lookupCount));
91 _shiftPerSec = timeShift / secsSinceLast;
92 float nextCorr = _maxOffset / _shiftPerSec;
93 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);qDebug("in %f secs the time will be shifted by %i", nextCorr,_maxOffset);
94 cfg.writeEntry("secsSinceLast",secsSinceLast);
95 cfg.writeEntry("timeShift",QString::number(timeShift));
96 }
97}
98
99void Ntp::correctClock()
100{
101 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
102 Config cfg("ntp",Config::User);
103 cfg.setGroup("correction");
104 int lastTime = cfg.readNumEntry("time",0);
105 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
106 int corr = int((now - lastTime) * _shiftPerSec);
107 outPut->append( "time will be shifted by "+QString::number(corr)+ "secs");
108 struct timeval myTv;
109 myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) );
110 myTv.tv_usec = 0;
111
112 if ( myTv.tv_sec != -1 )
113 ::settimeofday( &myTv, 0 );
114 Global::writeHWClock();
115 cfg.writeEntry("time",now);
116 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
117}
118
119float Ntp::getTimeShift()
120{
121 QString _offset = "offset";
122 QString _sec = "sec";
123 QRegExp _reOffset = QRegExp(_offset);
124 QRegExp _reEndOffset = QRegExp(_sec);
125 int posOffset = _reOffset.match( _ntpOutput );
126 int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset );
127 posOffset += _offset.length() + 1;
128 QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1);
129 qDebug("%s", _ntpOutput.latin1());
130 qDebug("diff = >%s<",diff.latin1());
131 TextLabelTimeShift->setText(diff);
132 return diff.toFloat();
133}
134
135void Ntp::readLookups()
136{
137 Config cfg("ntp",Config::User);
138 cfg.setGroup("lookups");
139 int lookupCount = cfg.readNumEntry("count",-1);
140 for (int i=0; i < lookupCount; i++)
141 {
142 cfg.setGroup("lookup_"+QString::number(i));
143 TableLookups->setText( 1,i,cfg.readEntry("secsSinceLast",0));
144 TableLookups->setText( 2,i,cfg.readEntry("timeShift",0));
145 }
146}
diff --git a/noncore/settings/netsystemtime/ntp.h b/noncore/settings/netsystemtime/ntp.h
new file mode 100644
index 0000000..4dc04b1
--- a/dev/null
+++ b/noncore/settings/netsystemtime/ntp.h
@@ -0,0 +1,37 @@
1#ifndef NTP_H
2#define NTP_H
3#include "ntpbase.h"
4#include <qdatetime.h>
5#include <qtimer.h>
6
7class OProcess;
8class QString;
9
10
11class Ntp : public NtpBase
12{
13 Q_OBJECT
14
15public:
16 Ntp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
17 ~Ntp();
18
19 OProcess *ntpProcess;
20
21private:
22 QString _ntpOutput;
23 int _maxOffset;
24 float _shiftPerSec;
25 QTimer *_nextCorrection;
26 int _minLookupDiff;
27
28 float getTimeShift();
29 void readLookups();
30private slots:
31 void slotRunNtp();
32 void getNtpOutput(OProcess *proc, char *buffer, int buflen);
33 void ntpFinished(OProcess*);
34 void correctClock();
35};
36
37#endif
diff --git a/noncore/settings/netsystemtime/ntpbase.ui b/noncore/settings/netsystemtime/ntpbase.ui
new file mode 100644
index 0000000..eb09e4a
--- a/dev/null
+++ b/noncore/settings/netsystemtime/ntpbase.ui
@@ -0,0 +1,322 @@
1<!DOCTYPE UI><UI>
2<class>NtpBase</class>
3<widget>
4 <class>QWidget</class>
5 <property stdset="1">
6 <name>name</name>
7 <cstring>NtpBase</cstring>
8 </property>
9 <property stdset="1">
10 <name>geometry</name>
11 <rect>
12 <x>0</x>
13 <y>0</y>
14 <width>296</width>
15 <height>337</height>
16 </rect>
17 </property>
18 <property stdset="1">
19 <name>caption</name>
20 <string>Network Time</string>
21 </property>
22 <property>
23 <name>layoutMargin</name>
24 </property>
25 <property>
26 <name>layoutSpacing</name>
27 </property>
28 <grid>
29 <property stdset="1">
30 <name>margin</name>
31 <number>2</number>
32 </property>
33 <property stdset="1">
34 <name>spacing</name>
35 <number>1</number>
36 </property>
37 <widget row="0" column="0" >
38 <class>QTabWidget</class>
39 <property stdset="1">
40 <name>name</name>
41 <cstring>TabWidgetMain</cstring>
42 </property>
43 <property>
44 <name>layoutMargin</name>
45 </property>
46 <property>
47 <name>layoutSpacing</name>
48 </property>
49 <widget>
50 <class>QWidget</class>
51 <property stdset="1">
52 <name>name</name>
53 <cstring>tabNtp</cstring>
54 </property>
55 <attribute>
56 <name>title</name>
57 <string>NTP</string>
58 </attribute>
59 <grid>
60 <property stdset="1">
61 <name>margin</name>
62 <number>3</number>
63 </property>
64 <property stdset="1">
65 <name>spacing</name>
66 <number>3</number>
67 </property>
68 <widget row="1" column="0" >
69 <class>QPushButton</class>
70 <property stdset="1">
71 <name>name</name>
72 <cstring>runNtp</cstring>
73 </property>
74 <property stdset="1">
75 <name>text</name>
76 <string>get time from network</string>
77 </property>
78 </widget>
79 <widget row="0" column="0" >
80 <class>QFrame</class>
81 <property stdset="1">
82 <name>name</name>
83 <cstring>FrameNtp</cstring>
84 </property>
85 <property stdset="1">
86 <name>frameShape</name>
87 <enum>StyledPanel</enum>
88 </property>
89 <property stdset="1">
90 <name>frameShadow</name>
91 <enum>Raised</enum>
92 </property>
93 <property>
94 <name>layoutMargin</name>
95 </property>
96 <grid>
97 <property stdset="1">
98 <name>margin</name>
99 <number>11</number>
100 </property>
101 <property stdset="1">
102 <name>spacing</name>
103 <number>6</number>
104 </property>
105 <widget row="0" column="0" >
106 <class>QLayoutWidget</class>
107 <property stdset="1">
108 <name>name</name>
109 <cstring>Layout4</cstring>
110 </property>
111 <vbox>
112 <property stdset="1">
113 <name>margin</name>
114 <number>0</number>
115 </property>
116 <property stdset="1">
117 <name>spacing</name>
118 <number>6</number>
119 </property>
120 <widget>
121 <class>QLabel</class>
122 <property stdset="1">
123 <name>name</name>
124 <cstring>TextLabel1</cstring>
125 </property>
126 <property stdset="1">
127 <name>autoMask</name>
128 <bool>false</bool>
129 </property>
130 <property stdset="1">
131 <name>text</name>
132 <string>Start Time:</string>
133 </property>
134 </widget>
135 <widget>
136 <class>QLabel</class>
137 <property stdset="1">
138 <name>name</name>
139 <cstring>TextLabelStartTime</cstring>
140 </property>
141 <property stdset="1">
142 <name>text</name>
143 <string></string>
144 </property>
145 </widget>
146 <widget>
147 <class>QLabel</class>
148 <property stdset="1">
149 <name>name</name>
150 <cstring>TextLabel3</cstring>
151 </property>
152 <property stdset="1">
153 <name>text</name>
154 <string>Time Shift:</string>
155 </property>
156 </widget>
157 <widget>
158 <class>QLabel</class>
159 <property stdset="1">
160 <name>name</name>
161 <cstring>TextLabelTimeShift</cstring>
162 </property>
163 <property stdset="1">
164 <name>text</name>
165 <string></string>
166 </property>
167 </widget>
168 <widget>
169 <class>QLabel</class>
170 <property stdset="1">
171 <name>name</name>
172 <cstring>TextLabel5</cstring>
173 </property>
174 <property stdset="1">
175 <name>text</name>
176 <string>New Time:</string>
177 </property>
178 </widget>
179 <widget>
180 <class>QLabel</class>
181 <property stdset="1">
182 <name>name</name>
183 <cstring>TextLabelNewTime</cstring>
184 </property>
185 <property stdset="1">
186 <name>text</name>
187 <string></string>
188 </property>
189 </widget>
190 <spacer>
191 <property>
192 <name>name</name>
193 <cstring>Spacer1</cstring>
194 </property>
195 <property stdset="1">
196 <name>orientation</name>
197 <enum>Vertical</enum>
198 </property>
199 <property stdset="1">
200 <name>sizeType</name>
201 <enum>Expanding</enum>
202 </property>
203 <property>
204 <name>sizeHint</name>
205 <size>
206 <width>20</width>
207 <height>20</height>
208 </size>
209 </property>
210 </spacer>
211 </vbox>
212 </widget>
213 <widget row="1" column="0" >
214 <class>QLayoutWidget</class>
215 <property stdset="1">
216 <name>name</name>
217 <cstring>Layout6</cstring>
218 </property>
219 <hbox>
220 <property stdset="1">
221 <name>margin</name>
222 <number>0</number>
223 </property>
224 <property stdset="1">
225 <name>spacing</name>
226 <number>6</number>
227 </property>
228 <widget>
229 <class>QLabel</class>
230 <property stdset="1">
231 <name>name</name>
232 <cstring>TextLabel7</cstring>
233 </property>
234 <property stdset="1">
235 <name>text</name>
236 <string>NTP Server:</string>
237 </property>
238 </widget>
239 <widget>
240 <class>QLineEdit</class>
241 <property stdset="1">
242 <name>name</name>
243 <cstring>LineEditNtpServer</cstring>
244 </property>
245 </widget>
246 </hbox>
247 </widget>
248 </grid>
249 </widget>
250 </grid>
251 </widget>
252 <widget>
253 <class>QWidget</class>
254 <property stdset="1">
255 <name>name</name>
256 <cstring>tab</cstring>
257 </property>
258 <attribute>
259 <name>title</name>
260 <string>Auto Correction</string>
261 </attribute>
262 <widget>
263 <class>QTable</class>
264 <property stdset="1">
265 <name>name</name>
266 <cstring>TableLookups</cstring>
267 </property>
268 <property stdset="1">
269 <name>geometry</name>
270 <rect>
271 <x>10</x>
272 <y>10</y>
273 <width>270</width>
274 <height>220</height>
275 </rect>
276 </property>
277 <property stdset="1">
278 <name>numRows</name>
279 <number>2</number>
280 </property>
281 <property stdset="1">
282 <name>numCols</name>
283 <number>2</number>
284 </property>
285 </widget>
286 </widget>
287 <widget>
288 <class>QWidget</class>
289 <property stdset="1">
290 <name>name</name>
291 <cstring>TabDebug</cstring>
292 </property>
293 <attribute>
294 <name>title</name>
295 <string>Debug</string>
296 </attribute>
297 <grid>
298 <property stdset="1">
299 <name>margin</name>
300 <number>3</number>
301 </property>
302 <property stdset="1">
303 <name>spacing</name>
304 <number>6</number>
305 </property>
306 <widget row="0" column="0" >
307 <class>QMultiLineEdit</class>
308 <property stdset="1">
309 <name>name</name>
310 <cstring>outPut</cstring>
311 </property>
312 <property stdset="1">
313 <name>wordWrap</name>
314 <enum>WidgetWidth</enum>
315 </property>
316 </widget>
317 </grid>
318 </widget>
319 </widget>
320 </grid>
321</widget>
322</UI>
diff --git a/noncore/settings/netsystemtime/opie-netsystemtime.control b/noncore/settings/netsystemtime/opie-netsystemtime.control
new file mode 100644
index 0000000..e6f5e2d
--- a/dev/null
+++ b/noncore/settings/netsystemtime/opie-netsystemtime.control
@@ -0,0 +1,8 @@
1Files: bin/netsystemtime apps/Settings/ntpdatetime.desktop
2Priority: optional
3Section: opie/settings
4Maintainer: Patrick S. Vogt <tille@handhelds.org>
5Architecture: arm
6Version: $QPE_VERSION-$SUB_VERSION
7Depends: opie-base ($QPE_VERSION)
8Description: ntp ( Network Time Protocol) gui