summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/ntp.cpp
Unidiff
Diffstat (limited to 'noncore/settings/netsystemtime/ntp.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/netsystemtime/ntp.cpp146
1 files changed, 146 insertions, 0 deletions
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}