-rw-r--r-- | example/main.cpp | 4 | ||||
-rw-r--r-- | example/netsystemtime.pro | 10 | ||||
-rw-r--r-- | example/ntp.cpp | 130 | ||||
-rw-r--r-- | example/ntp.h | 35 | ||||
-rw-r--r-- | example/ntpbase.ui | 322 | ||||
-rw-r--r-- | example/opie-netsystemtime.control | 8 |
6 files changed, 507 insertions, 2 deletions
diff --git a/example/main.cpp b/example/main.cpp index b705c44..2b23751 100644 --- a/example/main.cpp +++ b/example/main.cpp | |||
@@ -1,12 +1,12 @@ | |||
1 | #include "example.h" | 1 | #include "ntp.h" |
2 | #include <qpe/qpeapplication.h> | 2 | #include <qpe/qpeapplication.h> |
3 | 3 | ||
4 | int main( int argc, char ** argv ) | 4 | int main( int argc, char ** argv ) |
5 | { | 5 | { |
6 | QPEApplication a( argc, argv ); | 6 | QPEApplication a( argc, argv ); |
7 | 7 | ||
8 | Example mw; | 8 | Ntp mw; |
9 | a.showMainWidget( &mw ); | 9 | a.showMainWidget( &mw ); |
10 | 10 | ||
11 | return a.exec(); | 11 | return a.exec(); |
12 | } | 12 | } |
diff --git a/example/netsystemtime.pro b/example/netsystemtime.pro new file mode 100644 index 0000000..b98e45d --- a/dev/null +++ b/example/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 | ||
8 | LIBS += -lqpe -lopie | ||
9 | INTERFACES= ntpbase.ui | ||
10 | TARGET = ../../bin/netsystemtime | ||
diff --git a/example/ntp.cpp b/example/ntp.cpp new file mode 100644 index 0000000..5357cae --- a/dev/null +++ b/example/ntp.cpp | |||
@@ -0,0 +1,130 @@ | |||
1 | #include "ntp.h" | ||
2 | #include <qpushbutton.h> | ||
3 | #include <qregexp.h> | ||
4 | #include <qlabel.h> | ||
5 | #include <qlineedit.h> | ||
6 | #include <qmultilineedit.h> | ||
7 | #include <opie/oprocess.h> | ||
8 | #include <qpe/config.h> | ||
9 | #include <qpe/global.h> | ||
10 | #include <qpe/timeconversion.h> | ||
11 | #include <qpe/tzselect.h> | ||
12 | #include <qpe/timestring.h> | ||
13 | #include <qpe/qpedialog.h> | ||
14 | #include <sys/time.h> | ||
15 | #include <time.h> | ||
16 | #include <stdlib.h> | ||
17 | |||
18 | |||
19 | Ntp::Ntp( QWidget* parent, const char* name, WFlags fl ) | ||
20 | : NtpBase( parent, name, fl ) | ||
21 | { | ||
22 | Config cfg("ntp",Config::User); | ||
23 | cfg.setGroup("settings"); | ||
24 | _maxOffset = cfg.readNumEntry("maxOffset",5); | ||
25 | _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); | ||
26 | LineEditNtpServer->setText( cfg.readEntry("ntpServer") ); | ||
27 | ntpProcess = new OProcess( ); | ||
28 | connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), | ||
29 | this, SLOT(getNtpOutput(OProcess*,char*,int))); | ||
30 | connect ( ntpProcess, SIGNAL(processExited(OProcess*)), | ||
31 | this, SLOT(ntpFinished(OProcess*))); | ||
32 | connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); | ||
33 | _nextCorrection = new QTimer( this ); | ||
34 | connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) ); | ||
35 | slotRunNtp(); | ||
36 | } | ||
37 | |||
38 | Ntp::~Ntp() | ||
39 | { | ||
40 | Config cfg("ntp",Config::User); | ||
41 | cfg.setGroup("settings"); | ||
42 | cfg.writeEntry("ntpServer", LineEditNtpServer->text()); | ||
43 | } | ||
44 | |||
45 | |||
46 | void Ntp::slotRunNtp() | ||
47 | { | ||
48 | TextLabelStartTime->setText(QDateTime::currentDateTime().toString()); | ||
49 | ntpProcess->clearArguments(); | ||
50 | *ntpProcess << "ntpdate" << LineEditNtpServer->text(); | ||
51 | bool ret = ntpProcess->start(OProcess::NotifyOnExit,OProcess::AllOutput); | ||
52 | if ( !ret ) { | ||
53 | qDebug("Error while executing ntp"); | ||
54 | outPut->append("\nError while executing\n\n"); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen) | ||
59 | { | ||
60 | QString lineStr, lineStrOld; | ||
61 | lineStr = buffer; | ||
62 | lineStr=lineStr.left(buflen); | ||
63 | if (lineStr!=lineStrOld) | ||
64 | { | ||
65 | outPut->append(lineStr); | ||
66 | _ntpOutput += lineStr; | ||
67 | } | ||
68 | lineStrOld = lineStr; | ||
69 | } | ||
70 | |||
71 | void Ntp::ntpFinished(OProcess*) | ||
72 | { | ||
73 | Config cfg("ntp",Config::User); | ||
74 | cfg.setGroup("lookups"); | ||
75 | int lastLookup = cfg.readNumEntry("time",0); | ||
76 | int lookupCount = cfg.readNumEntry("count",-1); | ||
77 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
78 | cfg.writeEntry("time", time); | ||
79 | |||
80 | float timeShift = getTimeShift(); | ||
81 | int secsSinceLast = time - lastLookup; | ||
82 | TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); | ||
83 | if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff) | ||
84 | { | ||
85 | lookupCount++; | ||
86 | cfg.writeEntry("count",lookupCount); | ||
87 | cfg.setGroup("lookup_"+QString::number(lookupCount)); | ||
88 | _shiftPerSec = timeShift / secsSinceLast; | ||
89 | float nextCorr = _maxOffset / _shiftPerSec; | ||
90 | 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); | ||
91 | cfg.writeEntry("secsSinceLast",secsSinceLast); | ||
92 | cfg.writeEntry("timeShift",QString::number(timeShift)); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | void Ntp::correctClock() | ||
97 | { | ||
98 | qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | ||
99 | Config cfg("ntp",Config::User); | ||
100 | cfg.setGroup("correction"); | ||
101 | int lastTime = cfg.readNumEntry("time",0); | ||
102 | int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); | ||
103 | int corr = int((now - lastTime) * _shiftPerSec); | ||
104 | outPut->append( "time will be shifted by "+QString::number(corr)+ "secs"); | ||
105 | struct timeval myTv; | ||
106 | myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) ); | ||
107 | myTv.tv_usec = 0; | ||
108 | |||
109 | if ( myTv.tv_sec != -1 ) | ||
110 | ::settimeofday( &myTv, 0 ); | ||
111 | Global::writeHWClock(); | ||
112 | cfg.writeEntry("time",now); | ||
113 | qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); | ||
114 | } | ||
115 | |||
116 | float Ntp::getTimeShift() | ||
117 | { | ||
118 | QString _offset = "offset"; | ||
119 | QString _sec = "sec"; | ||
120 | QRegExp _reOffset = QRegExp(_offset); | ||
121 | QRegExp _reEndOffset = QRegExp(_sec); | ||
122 | int posOffset = _reOffset.match( _ntpOutput ); | ||
123 | int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); | ||
124 | posOffset += _offset.length() + 1; | ||
125 | QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); | ||
126 | qDebug("%s", _ntpOutput.latin1()); | ||
127 | qDebug("diff = >%s<",diff.latin1()); | ||
128 | TextLabelTimeShift->setText(diff); | ||
129 | return diff.toFloat(); | ||
130 | } \ No newline at end of file | ||
diff --git a/example/ntp.h b/example/ntp.h new file mode 100644 index 0000000..c9c2e9b --- a/dev/null +++ b/example/ntp.h | |||
@@ -0,0 +1,35 @@ | |||
1 | #ifndef NTP_H | ||
2 | #define NTP_H | ||
3 | #include "ntpbase.h" | ||
4 | #include <qdatetime.h> | ||
5 | #include <qtimer.h> | ||
6 | |||
7 | class OProcess; | ||
8 | class QString; | ||
9 | |||
10 | |||
11 | class Ntp : public NtpBase | ||
12 | { | ||
13 | Q_OBJECT | ||
14 | |||
15 | public: | ||
16 | Ntp( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | ||
17 | ~Ntp(); | ||
18 | |||
19 | OProcess *ntpProcess; | ||
20 | |||
21 | private: | ||
22 | QString _ntpOutput; | ||
23 | int _maxOffset; | ||
24 | float _shiftPerSec; | ||
25 | QTimer *_nextCorrection; | ||
26 | float getTimeShift(); | ||
27 | int _minLookupDiff; | ||
28 | private slots: | ||
29 | void slotRunNtp(); | ||
30 | void getNtpOutput(OProcess *proc, char *buffer, int buflen); | ||
31 | void ntpFinished(OProcess*); | ||
32 | void correctClock(); | ||
33 | }; | ||
34 | |||
35 | #endif | ||
diff --git a/example/ntpbase.ui b/example/ntpbase.ui new file mode 100644 index 0000000..eb09e4a --- a/dev/null +++ b/example/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/example/opie-netsystemtime.control b/example/opie-netsystemtime.control new file mode 100644 index 0000000..e6f5e2d --- a/dev/null +++ b/example/opie-netsystemtime.control | |||
@@ -0,0 +1,8 @@ | |||
1 | Files: bin/netsystemtime apps/Settings/ntpdatetime.desktop | ||
2 | Priority: optional | ||
3 | Section: opie/settings | ||
4 | Maintainer: Patrick S. Vogt <tille@handhelds.org> | ||
5 | Architecture: arm | ||
6 | Version: $QPE_VERSION-$SUB_VERSION | ||
7 | Depends: opie-base ($QPE_VERSION) | ||
8 | Description: ntp ( Network Time Protocol) gui | ||