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.cpp50
1 files changed, 29 insertions, 21 deletions
diff --git a/noncore/settings/netsystemtime/ntp.cpp b/noncore/settings/netsystemtime/ntp.cpp
index ddb1372..87b20ec 100644
--- a/noncore/settings/netsystemtime/ntp.cpp
+++ b/noncore/settings/netsystemtime/ntp.cpp
@@ -23,26 +23,26 @@ Ntp::Ntp( QWidget* parent, const char* name, WFlags fl )
23 Config cfg("ntp",Config::User); 23 Config cfg("ntp",Config::User);
24 cfg.setGroup("settings"); 24 cfg.setGroup("settings");
25 _maxOffset = cfg.readNumEntry("maxOffset",5); 25 _maxOffset = cfg.readNumEntry("maxOffset",5);
26 _minLookupDiff = cfg.readNumEntry("minLookupDiff",10); 26 _minLookupDiff = cfg.readNumEntry("minLookupDiff",10);
27 LineEditNtpServer->setText( cfg.readEntry("ntpServer") ); 27 LineEditNtpServer->setText( cfg.readEntry("ntpServer") );
28 ntpProcess = new OProcess( ); 28 ntpProcess = new OProcess( );
29 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)), 29 connect ( ntpProcess, SIGNAL(receivedStdout(OProcess*,char*,int)),
30 this, SLOT(getNtpOutput(OProcess*,char*,int))); 30 this, SLOT(getNtpOutput(OProcess*,char*,int)));
31 connect ( ntpProcess, SIGNAL(processExited(OProcess*)), 31 connect ( ntpProcess, SIGNAL(processExited(OProcess*)),
32 this, SLOT(ntpFinished(OProcess*))); 32 this, SLOT(ntpFinished(OProcess*)));
33 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp())); 33 connect(runNtp, SIGNAL(clicked()), this, SLOT(slotRunNtp()));
34 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime())); 34 connect(PushButtonPredict, SIGNAL(clicked()), this, SLOT(preditctTime()));
35 _nextCorrection = new QTimer( this ); 35 // _nextCorrection = new QTimer( this );
36 connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) ); 36// connect( _nextCorrection, SIGNAL(timeout()), SLOT(correctClock()) );
37 slotRunNtp(); 37 slotRunNtp();
38 readLookups(); 38 readLookups();
39} 39}
40 40
41Ntp::~Ntp() 41Ntp::~Ntp()
42{ 42{
43 43
44} 44}
45 45
46void Ntp::accept() 46void Ntp::accept()
47{ 47{
48 Config cfg("ntp",Config::User); 48 Config cfg("ntp",Config::User);
@@ -74,60 +74,66 @@ void Ntp::getNtpOutput(OProcess *proc, char *buffer, int buflen)
74 } 74 }
75 lineStrOld = lineStr; 75 lineStrOld = lineStr;
76} 76}
77 77
78void Ntp::ntpFinished(OProcess*) 78void Ntp::ntpFinished(OProcess*)
79{ 79{
80 Config cfg("ntp",Config::User); 80 Config cfg("ntp",Config::User);
81 cfg.setGroup("lookups"); 81 cfg.setGroup("lookups");
82 int lastLookup = cfg.readNumEntry("time",0); 82 int lastLookup = cfg.readNumEntry("time",0);
83 int lookupCount = cfg.readNumEntry("count",-1); 83 int lookupCount = cfg.readNumEntry("count",-1);
84 int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); 84 int time = TimeConversion::toUTC( QDateTime::currentDateTime() );
85 cfg.writeEntry("time", time); 85 cfg.writeEntry("time", time);
86 cfg.setGroup("correction");
87 cfg.writeEntry("time", time);
86 88
87 float timeShift = getTimeShift(); 89 float timeShift = getTimeShift();
88 if (timeShift == 0.0) return; 90 if (timeShift == 0.0) return;
89 int secsSinceLast = time - lastLookup; 91 int secsSinceLast = time - lastLookup;
90 TextLabelNewTime->setText(QDateTime::currentDateTime().toString()); 92 TextLabelNewTime->setText(QDateTime::currentDateTime().toString());
91 if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff) 93 if ( lastLookup > 0 && secsSinceLast > 60*_minLookupDiff)
92 { 94 {
93 lookupCount++; 95 lookupCount++;
94 cfg.writeEntry("count",lookupCount); 96 cfg.writeEntry("count",lookupCount);
95 cfg.setGroup("lookup_"+QString::number(lookupCount)); 97 cfg.setGroup("lookup_"+QString::number(lookupCount));
96 _shiftPerSec = timeShift / secsSinceLast; 98 _shiftPerSec = timeShift / secsSinceLast;
97// float nextCorr = _maxOffset / _shiftPerSec; 99// float nextCorr = _maxOffset / _shiftPerSec;
98 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec); 100 qDebug("secs since last lookup %i", secsSinceLast);qDebug("timeshift since last lookup %f", timeShift);qDebug("timeshift since per sec %f", _shiftPerSec);
99 cfg.writeEntry("secsSinceLast",secsSinceLast); 101 cfg.writeEntry("secsSinceLast",secsSinceLast);
100 cfg.writeEntry("timeShift",QString::number(timeShift)); 102 cfg.writeEntry("timeShift",QString::number(timeShift));
101 } 103 }
102} 104}
103 105
104void Ntp::correctClock() 106//void Ntp::correctClock()
105{ 107//{
106 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); 108 //qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
107 Config cfg("ntp",Config::User); 109 //Config cfg("ntp",Config::User);
108 cfg.setGroup("correction"); 110// cfg.setGroup("correction");
109 int lastTime = cfg.readNumEntry("time",0); 111 // int lastTime = cfg.readNumEntry("time",0);
110 int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); 112// if ( lastTime == 0 )
111 int corr = int((now - lastTime) * _shiftPerSec); 113// {
112 struct timeval myTv; 114 // return;
113 myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) ); 115// }
114 myTv.tv_usec = 0; 116// int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
115 117// int corr = int((now - lastTime) * _shiftPerSec);
116 if ( myTv.tv_sec != -1 ) 118// struct timeval myTv;
117 ::settimeofday( &myTv, 0 ); 119// myTv.tv_sec = TimeConversion::toUTC( QDateTime::currentDateTime().addSecs(corr) );
118 Global::writeHWClock(); 120// myTv.tv_usec = 0;
119 cfg.writeEntry("time",now); 121//
120 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); 122// if ( myTv.tv_sec != -1 )
121} 123// ::settimeofday( &myTv, 0 );
124// Global::writeHWClock();
125// cfg.writeEntry("time",now);
126 //qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
127//}
122 128
123float Ntp::getTimeShift() 129float Ntp::getTimeShift()
124{ 130{
125 QString _offset = "offset"; 131 QString _offset = "offset";
126 QString _sec = "sec"; 132 QString _sec = "sec";
127 QRegExp _reOffset = QRegExp(_offset); 133 QRegExp _reOffset = QRegExp(_offset);
128 QRegExp _reEndOffset = QRegExp(_sec); 134 QRegExp _reEndOffset = QRegExp(_sec);
129 int posOffset = _reOffset.match( _ntpOutput ); 135 int posOffset = _reOffset.match( _ntpOutput );
130 int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset ); 136 int posEndOffset = _reEndOffset.match( _ntpOutput, posOffset );
131 posOffset += _offset.length() + 1; 137 posOffset += _offset.length() + 1;
132 QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1); 138 QString diff = _ntpOutput.mid(posOffset, posEndOffset-posOffset-1);
133 qDebug("%s", _ntpOutput.latin1()); 139 qDebug("%s", _ntpOutput.latin1());
@@ -170,14 +176,16 @@ void Ntp::readLookups()
170 TextLabelShift->setText(QString::number(_shiftPerSec)); 176 TextLabelShift->setText(QString::number(_shiftPerSec));
171} 177}
172 178
173void Ntp::preditctTime() 179void Ntp::preditctTime()
174{ 180{
175 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1()); 181 qDebug("current time: %s",QDateTime::currentDateTime().toString().latin1());
176 Config cfg("ntp",Config::User); 182 Config cfg("ntp",Config::User);
177 cfg.setGroup("lookups"); 183 cfg.setGroup("lookups");
178 int lastTime = cfg.readNumEntry("time",0); 184 int lastTime = cfg.readNumEntry("time",0);
179 setenv( "TZ", tz->currentZone(), 1 ); 185 setenv( "TZ", tz->currentZone(), 1 );
180 int now = TimeConversion::toUTC( QDateTime::currentDateTime() ); 186 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
181 int corr = int((now - lastTime) * _shiftPerSec); 187 int corr = int((now - lastTime) * _shiftPerSec);
182 TextLabelPredTime->setText(QDateTime::currentDateTime().addSecs(corr).toString()); 188 QDateTime dt = QDateTime::currentDateTime().addSecs(corr);
189 setTime(dt);
190 TextLabelPredTime->setText(dt.toString());
183} 191}