-rw-r--r-- | noncore/settings/netsystemtime/mainwindow.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/settings/netsystemtime/mainwindow.cpp b/noncore/settings/netsystemtime/mainwindow.cpp index 01d9a98..1f9f8c3 100644 --- a/noncore/settings/netsystemtime/mainwindow.cpp +++ b/noncore/settings/netsystemtime/mainwindow.cpp | |||
@@ -1,416 +1,416 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the OPIE Project | 2 | This file is part of the OPIE Project |
3 | =. | 3 | =. |
4 | .=l. Copyright (c) 2002 OPIE team <opie@handhelds.org?> | 4 | .=l. Copyright (c) 2002 OPIE team <opie@handhelds.org?> |
5 | .>+-= | 5 | .>+-= |
6 | _;:, .> :=|. This file is free software; you can | 6 | _;:, .> :=|. This file is free software; you can |
7 | .> <`_, > . <= redistribute it and/or modify it under | 7 | .> <`_, > . <= redistribute it and/or modify it under |
8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public | 8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public |
9 | .="- .-=="i, .._ License as published by the Free Software | 9 | .="- .-=="i, .._ License as published by the Free Software |
10 | - . .-<_> .<> Foundation; either version 2 of the License, | 10 | - . .-<_> .<> Foundation; either version 2 of the License, |
11 | ._= =} : or (at your option) any later version. | 11 | ._= =} : or (at your option) any later version. |
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This file is distributed in the hope that | 13 | .i_,=:_. -<s. This file is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #include "mainwindow.h" | 29 | #include "mainwindow.h" |
30 | #include "timetabwidget.h" | 30 | #include "timetabwidget.h" |
31 | #include "formattabwidget.h" | 31 | #include "formattabwidget.h" |
32 | #include "settingstabwidget.h" | 32 | #include "settingstabwidget.h" |
33 | #include "ntptabwidget.h" | 33 | #include "ntptabwidget.h" |
34 | #include "predicttabwidget.h" | 34 | #include "predicttabwidget.h" |
35 | 35 | ||
36 | #include <qpe/config.h> | 36 | #include <qpe/config.h> |
37 | #include <qpe/datebookdb.h> | 37 | #include <qpe/datebookdb.h> |
38 | #include <qpe/qpeapplication.h> | 38 | #include <qpe/qpeapplication.h> |
39 | #include <qpe/qpedialog.h> | 39 | #include <qpe/qpedialog.h> |
40 | 40 | ||
41 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) | 41 | #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) |
42 | #include <qpe/qcopenvelope_qws.h> | 42 | #include <qpe/qcopenvelope_qws.h> |
43 | #endif | 43 | #endif |
44 | 44 | ||
45 | #include <qlayout.h> | 45 | #include <qlayout.h> |
46 | #include <qmessagebox.h> | 46 | #include <qmessagebox.h> |
47 | #include <qsocket.h> | 47 | #include <qsocket.h> |
48 | #include <qstring.h> | 48 | #include <qstring.h> |
49 | #include <qtimer.h> | 49 | #include <qtimer.h> |
50 | 50 | ||
51 | using namespace Opie::Ui; | 51 | using namespace Opie::Ui; |
52 | using namespace Opie::Core; | 52 | using namespace Opie::Core; |
53 | MainWindow::MainWindow( QWidget *parent , const char *name, bool modal, WFlags f ) | 53 | MainWindow::MainWindow( QWidget *parent , const char *name, bool modal, WFlags f ) |
54 | : QDialog( parent, name, modal, f ) | 54 | : QDialog( parent, name, modal, f ) |
55 | { | 55 | { |
56 | setCaption( tr( "SystemTime" ) ); | 56 | setCaption( tr( "SystemTime" ) ); |
57 | 57 | ||
58 | QVBoxLayout *layout = new QVBoxLayout( this ); | 58 | QVBoxLayout *layout = new QVBoxLayout( this ); |
59 | layout->setMargin( 2 ); | 59 | layout->setMargin( 2 ); |
60 | layout->setSpacing( 4 ); | 60 | layout->setSpacing( 4 ); |
61 | 61 | ||
62 | // Create main tabbed control | 62 | // Create main tabbed control |
63 | mainWidget = new OTabWidget( this ); | 63 | mainWidget = new OTabWidget( this ); |
64 | 64 | ||
65 | // Default object pointers to null | 65 | // Default object pointers to null |
66 | ntpProcess = 0x0; | 66 | ntpProcess = 0x0; |
67 | ntpTab = 0x0; | 67 | ntpTab = 0x0; |
68 | 68 | ||
69 | // Add tab widgets | 69 | // Add tab widgets |
70 | mainWidget->addTab( timeTab = new TimeTabWidget( mainWidget ), "netsystemtime/DateTime", tr( "Time" ) ); | 70 | mainWidget->addTab( timeTab = new TimeTabWidget( mainWidget ), "netsystemtime/DateTime", tr( "Time" ) ); |
71 | mainWidget->addTab( formatTab = new FormatTabWidget( mainWidget ), "netsystemtime/formattab", tr( "Format" ) ); | 71 | mainWidget->addTab( formatTab = new FormatTabWidget( mainWidget ), "netsystemtime/formattab", tr( "Format" ) ); |
72 | mainWidget->addTab( settingsTab = new SettingsTabWidget( mainWidget ), "SettingsIcon", tr( "Settings" ) ); | 72 | mainWidget->addTab( settingsTab = new SettingsTabWidget( mainWidget ), "SettingsIcon", tr( "Settings" ) ); |
73 | mainWidget->addTab( predictTab = new PredictTabWidget( mainWidget ), "netsystemtime/predicttab", tr( "Predict" ) ); | 73 | mainWidget->addTab( predictTab = new PredictTabWidget( mainWidget ), "netsystemtime/predicttab", tr( "Predict" ) ); |
74 | Config config( "ntp" ); | 74 | Config config( "ntp" ); |
75 | config.setGroup( "settings" ); | 75 | config.setGroup( "settings" ); |
76 | slotDisplayNTPTab( config.readBoolEntry( "displayNtpTab", false ) ); | 76 | slotDisplayNTPTab( config.readBoolEntry( "displayNtpTab", false ) ); |
77 | slotDisplayPredictTab( config.readBoolEntry( "displayPredictTab", false ) ); | 77 | slotDisplayPredictTab( config.readBoolEntry( "displayPredictTab", false ) ); |
78 | 78 | ||
79 | mainWidget->setCurrentTab( tr( "Time" ) ); | 79 | mainWidget->setCurrentTab( tr( "Time" ) ); |
80 | layout->addWidget( mainWidget ); | 80 | layout->addWidget( mainWidget ); |
81 | 81 | ||
82 | connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)), | 82 | connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)), |
83 | this, SLOT(slotQCopReceive(const QCString&,const QByteArray&)) ); | 83 | this, SLOT(slotQCopReceive(const QCString&,const QByteArray&)) ); |
84 | 84 | ||
85 | 85 | ||
86 | // Create NTP socket | 86 | // Create NTP socket |
87 | ntpSock = new QSocket( this ); | 87 | ntpSock = new QSocket( this ); |
88 | connect( ntpSock, SIGNAL(error(int)),SLOT(slotCheckNtp(int)) ); | 88 | connect( ntpSock, SIGNAL(error(int)),SLOT(slotCheckNtp(int)) ); |
89 | slotProbeNTPServer(); | 89 | slotProbeNTPServer(); |
90 | 90 | ||
91 | // Create timer for automatic time lookups | 91 | // Create timer for automatic time lookups |
92 | ntpTimer = new QTimer( this ); | 92 | ntpTimer = new QTimer( this ); |
93 | 93 | ||
94 | // Connect everything together | 94 | // Connect everything together |
95 | connect( timeTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); | 95 | connect( timeTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); |
96 | connect( timeTab, SIGNAL(tzChanged(const QString&)), predictTab, SLOT(slotTZChanged(const QString&)) ); | 96 | connect( timeTab, SIGNAL(tzChanged(const QString&)), predictTab, SLOT(slotTZChanged(const QString&)) ); |
97 | connect( timeTab, SIGNAL(getPredictedTime()), predictTab, SLOT(slotSetPredictedTime()) ); | 97 | connect( timeTab, SIGNAL(getPredictedTime()), predictTab, SLOT(slotSetPredictedTime()) ); |
98 | connect( formatTab, SIGNAL(show12HourTime(int)), timeTab, SLOT(slotUse12HourTime(int)) ); | 98 | connect( formatTab, SIGNAL(show12HourTime(int)), timeTab, SLOT(slotUse12HourTime(int)) ); |
99 | connect( formatTab, SIGNAL(dateFormatChanged(const DateFormat&)), | 99 | connect( formatTab, SIGNAL(dateFormatChanged(const DateFormat&)), |
100 | timeTab, SLOT(slotDateFormatChanged(const DateFormat&)) ); | 100 | timeTab, SLOT(slotDateFormatChanged(const DateFormat&)) ); |
101 | connect( formatTab, SIGNAL(weekStartChanged(int)), timeTab, SLOT(slotWeekStartChanged(int)) ); | 101 | connect( formatTab, SIGNAL(weekStartChanged(int)), timeTab, SLOT(slotWeekStartChanged(int)) ); |
102 | connect( settingsTab, SIGNAL(ntpDelayChanged(int)), this, SLOT(slotNTPDelayChanged(int)) ); | 102 | connect( settingsTab, SIGNAL(ntpDelayChanged(int)), this, SLOT(slotNTPDelayChanged(int)) ); |
103 | connect( settingsTab, SIGNAL(displayNTPTab(bool)), this, SLOT(slotDisplayNTPTab(bool)) ); | 103 | connect( settingsTab, SIGNAL(displayNTPTab(bool)), this, SLOT(slotDisplayNTPTab(bool)) ); |
104 | connect( settingsTab, SIGNAL(displayPredictTab(bool)), this, SLOT(slotDisplayPredictTab(bool)) ); | 104 | connect( settingsTab, SIGNAL(displayPredictTab(bool)), this, SLOT(slotDisplayPredictTab(bool)) ); |
105 | connect( predictTab, SIGNAL(setTime(const QDateTime&)), this, SLOT(slotSetTime(const QDateTime&)) ); | 105 | connect( predictTab, SIGNAL(setTime(const QDateTime&)), this, SLOT(slotSetTime(const QDateTime&)) ); |
106 | 106 | ||
107 | // Do initial time server check | 107 | // Do initial time server check |
108 | slotNTPDelayChanged( config.readNumEntry( "ntpRefreshFreq", 1440 ) ); | 108 | slotNTPDelayChanged( config.readNumEntry( "ntpRefreshFreq", 1440 ) ); |
109 | slotCheckNtp( -1 ); | 109 | slotCheckNtp( -1 ); |
110 | 110 | ||
111 | // Display app | 111 | // Display app |
112 | //showMaximized(); | 112 | //showMaximized(); |
113 | (void)new QPEDialogListener(this); | 113 | (void)new QPEDialogListener(this); |
114 | } | 114 | } |
115 | 115 | ||
116 | MainWindow::~MainWindow() | 116 | MainWindow::~MainWindow() |
117 | { | 117 | { |
118 | if ( ntpProcess ) | 118 | if ( ntpProcess ) |
119 | delete ntpProcess; | 119 | delete ntpProcess; |
120 | } | 120 | } |
121 | 121 | ||
122 | void MainWindow::accept() | 122 | void MainWindow::accept() |
123 | { | 123 | { |
124 | // Turn off the screensaver (Note: needs to be encased in { } so that it deconstructs and sends) | 124 | // Turn off the screensaver (Note: needs to be encased in { } so that it deconstructs and sends) |
125 | { | 125 | { |
126 | QCopEnvelope disableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); | 126 | QCopEnvelope disableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); |
127 | disableScreenSaver << 0 << 0 << 0; | 127 | disableScreenSaver << 0 << 0 << 0; |
128 | } | 128 | } |
129 | 129 | ||
130 | // Update the systemtime | 130 | // Update the systemtime |
131 | timeTab->saveSettings( true ); | 131 | timeTab->saveSettings( true ); |
132 | 132 | ||
133 | // Save format options | 133 | // Save format options |
134 | formatTab->saveSettings( true ); | 134 | formatTab->saveSettings( true ); |
135 | 135 | ||
136 | // Save settings options | 136 | // Save settings options |
137 | settingsTab->saveSettings(); | 137 | settingsTab->saveSettings(); |
138 | 138 | ||
139 | // Since time has changed quickly load in the DateBookDB to allow the alarm server to get a better | 139 | // Since time has changed quickly load in the DateBookDB to allow the alarm server to get a better |
140 | // grip on itself (example re-trigger alarms for when we travel back in time). | 140 | // grip on itself (example re-trigger alarms for when we travel back in time). |
141 | DateBookDB db; | 141 | DateBookDB db; |
142 | 142 | ||
143 | // Turn back on the screensaver | 143 | // Turn back on the screensaver |
144 | QCopEnvelope enableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); | 144 | QCopEnvelope enableScreenSaver( "QPE/System", "setScreenSaverIntervals(int,int,int)" ); |
145 | enableScreenSaver << -1 << -1 << -1; | 145 | enableScreenSaver << -1 << -1 << -1; |
146 | 146 | ||
147 | // Exit app | 147 | // Exit app |
148 | qApp->quit(); | 148 | qApp->quit(); |
149 | } | 149 | } |
150 | 150 | ||
151 | void MainWindow::reject() | 151 | void MainWindow::reject() |
152 | { | 152 | { |
153 | // Reset time settings | 153 | // Reset time settings |
154 | timeTab->saveSettings( false ); | 154 | timeTab->saveSettings( false ); |
155 | 155 | ||
156 | // Send notifications but do not save settings | 156 | // Send notifications but do not save settings |
157 | formatTab->saveSettings( false ); | 157 | formatTab->saveSettings( false ); |
158 | 158 | ||
159 | // Exit app | 159 | // Exit app |
160 | qApp->quit(); | 160 | qApp->quit(); |
161 | } | 161 | } |
162 | 162 | ||
163 | void MainWindow::runNTP() | 163 | void MainWindow::runNTP() |
164 | { | 164 | { |
165 | if ( !ntpDelayElapsed() && ntpInteractive ) | 165 | if ( !ntpDelayElapsed() && ntpInteractive ) |
166 | { | 166 | { |
167 | QString msg = tr( "You asked for a delay of %1 minutes, but only %2 minutes elapsed since last lookup.<br>Continue?" ).arg( QString::number( ntpDelay ) ).arg( QString::number( _lookupDiff / 60 ) ); | 167 | QString msg = tr( "You asked for a delay of %1 minutes, but only %2 minutes elapsed since last lookup.<br>Continue?" ).arg( QString::number( ntpDelay ) ).arg( QString::number( _lookupDiff / 60 ) ); |
168 | 168 | ||
169 | switch ( | 169 | switch ( |
170 | QMessageBox::warning( this, tr( "Continue?" ), msg, QMessageBox::Yes, QMessageBox::No ) | 170 | QMessageBox::warning( this, tr( "Continue?" ), msg, QMessageBox::Yes, QMessageBox::No ) |
171 | ) | 171 | ) |
172 | { | 172 | { |
173 | case QMessageBox::Yes: break; | 173 | case QMessageBox::Yes: break; |
174 | case QMessageBox::No: return; | 174 | case QMessageBox::No: return; |
175 | default: return; | 175 | default: return; |
176 | } | 176 | } |
177 | } | 177 | } |
178 | 178 | ||
179 | QString srv = settingsTab->ntpServer(); | 179 | QString srv = settingsTab->ntpServer(); |
180 | 180 | ||
181 | // Send information to time server tab if enabled | 181 | // Send information to time server tab if enabled |
182 | if ( ntpTabEnabled ) | 182 | if ( ntpTabEnabled ) |
183 | { | 183 | { |
184 | ntpTab->setStartTime( QDateTime::currentDateTime().toString() ); | 184 | ntpTab->setStartTime( QDateTime::currentDateTime().toString() ); |
185 | QString output = tr( "Running:\nntpdate " ); | 185 | QString output = tr( "Running:\nntpdate " ); |
186 | output.append( srv ); | 186 | output.append( srv ); |
187 | ntpTab->addNtpOutput( output ); | 187 | ntpTab->addNtpOutput( output ); |
188 | ntpTab->setNTPBtnEnabled( false ); | 188 | ntpTab->setNTPBtnEnabled( false ); |
189 | } | 189 | } |
190 | 190 | ||
191 | // Disable set time buttons & change app caption to indicate time update is happening | 191 | // Disable set time buttons & change app caption to indicate time update is happening |
192 | timeTab->setNTPBtnEnabled( false ); | 192 | timeTab->setNTPBtnEnabled( false ); |
193 | setCaption( tr( "Retrieving time from network..." ) ); | 193 | setCaption( tr( "Retrieving time from network..." ) ); |
194 | 194 | ||
195 | if ( !ntpProcess ) | 195 | if ( !ntpProcess ) |
196 | { | 196 | { |
197 | ntpProcess = new OProcess(); | 197 | ntpProcess = new OProcess(); |
198 | connect( ntpProcess, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)), | 198 | connect( ntpProcess, SIGNAL(receivedStdout(Opie::Core::OProcess*,char*,int)), |
199 | this, SLOT(slotNtpOutput(Opie::Core::OProcess*,char*,int)) ); | 199 | this, SLOT(slotNtpOutput(Opie::Core::OProcess*,char*,int)) ); |
200 | connect( ntpProcess, SIGNAL(processExited(Opie::Core::OProcess*)), | 200 | connect( ntpProcess, SIGNAL(processExited(Opie::Core::OProcess*)), |
201 | this, SLOT(slotNtpFinished(Opie::Core::OProcess*)) ); | 201 | this, SLOT(slotNtpFinished(Opie::Core::OProcess*)) ); |
202 | } | 202 | } |
203 | 203 | ||
204 | else | 204 | else |
205 | ntpProcess->clearArguments(); | 205 | ntpProcess->clearArguments(); |
206 | 206 | ||
207 | *ntpProcess << "ntpdate" << srv; | 207 | *ntpProcess << "ntpdate" << srv; |
208 | bool ret = ntpProcess->start( OProcess::NotifyOnExit, OProcess::AllOutput ); | 208 | bool ret = ntpProcess->start( OProcess::NotifyOnExit, OProcess::AllOutput ); |
209 | if ( !ret ) | 209 | if ( !ret ) |
210 | { | 210 | { |
211 | QMessageBox::critical( this, tr( "Error" ), tr( "Error while getting time from network." ) ); | 211 | QMessageBox::critical( this, tr( "Error" ), tr( "Error while getting time from network." ) ); |
212 | if ( ntpTabEnabled ) | 212 | if ( ntpTabEnabled ) |
213 | { | 213 | { |
214 | ntpTab->addNtpOutput( tr( "Error while executing ntpdate" ) ); | 214 | ntpTab->addNtpOutput( tr( "Error while executing ntpdate" ) ); |
215 | ntpTab->setNTPBtnEnabled( true ); | 215 | ntpTab->setNTPBtnEnabled( true ); |
216 | } | 216 | } |
217 | 217 | ||
218 | // Re-enable set time buttons & change app caption to indicate time update is happening | 218 | // Re-enable set time buttons & change app caption to indicate time update is happening |
219 | timeTab->setNTPBtnEnabled( true ); | 219 | timeTab->setNTPBtnEnabled( true ); |
220 | setCaption( tr( "SystemTime" ) ); | 220 | setCaption( tr( "SystemTime" ) ); |
221 | } | 221 | } |
222 | } | 222 | } |
223 | 223 | ||
224 | bool MainWindow::ntpDelayElapsed() | 224 | bool MainWindow::ntpDelayElapsed() |
225 | { | 225 | { |
226 | // Determine if time elapsed is greater than time delay | 226 | // Determine if time elapsed is greater than time delay |
227 | Config config( "ntp" ); | 227 | Config config( "ntp" ); |
228 | config.setGroup( "lookups" ); | 228 | config.setGroup( "lookups" ); |
229 | _lookupDiff = TimeConversion::toUTC( QDateTime::currentDateTime() ) - config.readNumEntry( "time", 0 ); | 229 | _lookupDiff = TimeConversion::toUTC( QDateTime::currentDateTime() ) - config.readNumEntry( "time", 0 ); |
230 | if ( _lookupDiff < 0 ) | 230 | if ( _lookupDiff < 0 ) |
231 | return true; | 231 | return true; |
232 | return ( _lookupDiff - ( ntpDelay * 60) ) > 0; | 232 | return ( _lookupDiff - ( ntpDelay * 60) ) > 0; |
233 | } | 233 | } |
234 | 234 | ||
235 | void MainWindow::slotSetTime( const QDateTime &dt ) | 235 | void MainWindow::slotSetTime( const QDateTime &dt ) |
236 | { | 236 | { |
237 | timeTab->setDateTime( dt ); | 237 | timeTab->setDateTime( dt ); |
238 | } | 238 | } |
239 | 239 | ||
240 | void MainWindow::slotQCopReceive( const QCString &msg, const QByteArray & ) | 240 | void MainWindow::slotQCopReceive( const QCString &msg, const QByteArray & ) |
241 | { | 241 | { |
242 | if ( msg == "ntpLookup(QString)" ) | 242 | if ( msg == "ntpLookup(QString)" ) |
243 | { | 243 | { |
244 | ntpInteractive = false; | 244 | ntpInteractive = false; |
245 | runNTP(); | 245 | runNTP(); |
246 | } | 246 | } |
247 | if ( msg == "setPredictedTime(QString)" ) | 247 | if ( msg == "setPredictedTime(QString)" ) |
248 | { | 248 | { |
249 | //setPredictTime(); | 249 | //setPredictTime(); |
250 | } | 250 | } |
251 | } | 251 | } |
252 | 252 | ||
253 | void MainWindow::slotDisplayNTPTab( bool display ) | 253 | void MainWindow::slotDisplayNTPTab( bool display ) |
254 | { | 254 | { |
255 | ntpTabEnabled = display; | 255 | ntpTabEnabled = display; |
256 | 256 | ||
257 | // Create widget if it hasn't needed | 257 | // Create widget if it hasn't needed |
258 | if ( display && !ntpTab ) | 258 | if ( display && !ntpTab ) |
259 | { | 259 | { |
260 | ntpTab = new NTPTabWidget( mainWidget ); | 260 | ntpTab = new NTPTabWidget( mainWidget ); |
261 | connect( ntpTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); | 261 | connect( ntpTab, SIGNAL(getNTPTime()), this, SLOT(slotGetNTPTime()) ); |
262 | } | 262 | } |
263 | 263 | ||
264 | // Display/hide tab | 264 | // Display/hide tab |
265 | display ? mainWidget->addTab( ntpTab, "netsystemtime/ntptab", tr( "Time Server" ) ) | 265 | display ? mainWidget->addTab( ntpTab, "netsystemtime/ntptab", tr( "Time Server" ) ) |
266 | : mainWidget->removePage( ntpTab ); | 266 | : mainWidget->removePage( ntpTab ); |
267 | } | 267 | } |
268 | 268 | ||
269 | void MainWindow::slotDisplayPredictTab( bool display ) | 269 | void MainWindow::slotDisplayPredictTab( bool display ) |
270 | { | 270 | { |
271 | predictTabEnabled = display; | 271 | predictTabEnabled = display; |
272 | 272 | ||
273 | // Create widget if it hasn't needed | 273 | // Create widget if it hasn't needed |
274 | if ( display && !predictTab ) | 274 | if ( display && !predictTab ) |
275 | { | 275 | { |
276 | } | 276 | } |
277 | // Display/hide tab | 277 | // Display/hide tab |
278 | display ? mainWidget->addTab( predictTab, "netsystemtime/predicttab", tr( "Predict" ) ) | 278 | display ? mainWidget->addTab( predictTab, "netsystemtime/predicttab", tr( "Predict" ) ) |
279 | : mainWidget->removePage( predictTab ); | 279 | : mainWidget->removePage( predictTab ); |
280 | } | 280 | } |
281 | 281 | ||
282 | void MainWindow::slotGetNTPTime() | 282 | void MainWindow::slotGetNTPTime() |
283 | { | 283 | { |
284 | ntpInteractive = true; | 284 | ntpInteractive = true; |
285 | runNTP(); | 285 | runNTP(); |
286 | } | 286 | } |
287 | 287 | ||
288 | void MainWindow::slotTimerGetNTPTime() | 288 | void MainWindow::slotTimerGetNTPTime() |
289 | { | 289 | { |
290 | ntpInteractive = false; | 290 | ntpInteractive = false; |
291 | runNTP(); | 291 | runNTP(); |
292 | } | 292 | } |
293 | 293 | ||
294 | void MainWindow::slotProbeNTPServer() | 294 | void MainWindow::slotProbeNTPServer() |
295 | { | 295 | { |
296 | ntpSock->connectToHost( settingsTab->ntpServer(), 123 ); | 296 | ntpSock->connectToHost( settingsTab->ntpServer(), 123 ); |
297 | } | 297 | } |
298 | 298 | ||
299 | void MainWindow::slotNtpOutput( OProcess *, char *buffer, int buflen ) | 299 | void MainWindow::slotNtpOutput( OProcess *, char *buffer, int buflen ) |
300 | { | 300 | { |
301 | QString output = QString( buffer ).left( buflen ); | 301 | QString output = QString( buffer ).left( buflen ); |
302 | ntpOutput.append( output ); | 302 | ntpOutput.append( output ); |
303 | 303 | ||
304 | if ( ntpTabEnabled ) | 304 | if ( ntpTabEnabled ) |
305 | ntpTab->addNtpOutput( output ); | 305 | ntpTab->addNtpOutput( output ); |
306 | } | 306 | } |
307 | 307 | ||
308 | void MainWindow::slotNtpFinished( OProcess *p ) | 308 | void MainWindow::slotNtpFinished( OProcess *p ) |
309 | { | 309 | { |
310 | QString output; | 310 | QString output; |
311 | QDateTime dt = QDateTime::currentDateTime(); | 311 | QDateTime dt = QDateTime::currentDateTime(); |
312 | 312 | ||
313 | // Re-enable set time buttons & change app caption to indicate time update is happening | 313 | // Re-enable set time buttons & change app caption to indicate time update is happening |
314 | if ( ntpTabEnabled ) | 314 | if ( ntpTabEnabled ) |
315 | ntpTab->setNTPBtnEnabled( true ); | 315 | ntpTab->setNTPBtnEnabled( true ); |
316 | timeTab->setNTPBtnEnabled( true ); | 316 | timeTab->setNTPBtnEnabled( true ); |
317 | setCaption( tr( "SystemTime" ) ); | 317 | setCaption( tr( "SystemTime" ) ); |
318 | 318 | ||
319 | // Verify run was successful | 319 | // Verify run was successful |
320 | if ( p->exitStatus() != 0 || !p->normalExit() ) | 320 | if ( p->exitStatus() != 0 ) |
321 | { | 321 | { |
322 | if ( isVisible() && ntpInteractive ) | 322 | if ( isVisible() && ntpInteractive ) |
323 | { | 323 | { |
324 | output = tr( "Error while getting time from\n server: " ); | 324 | output = tr( "Error while getting time from\n server: " ); |
325 | output.append( settingsTab->ntpServer() ); | 325 | output.append( settingsTab->ntpServer() ); |
326 | QMessageBox::critical(this, tr( "Error" ), output ); | 326 | QMessageBox::critical(this, tr( "Error" ), output ); |
327 | } | 327 | } |
328 | // slotCheckNtp(-1); | 328 | // slotCheckNtp(-1); |
329 | return; | 329 | return; |
330 | } | 330 | } |
331 | 331 | ||
332 | // Set controls on time tab to new time value | 332 | // Set controls on time tab to new time value |
333 | timeTab->setDateTime( dt ); | 333 | timeTab->setDateTime( dt ); |
334 | 334 | ||
335 | // Write out lookup information | 335 | // Write out lookup information |
336 | Config config( "ntp" ); | 336 | Config config( "ntp" ); |
337 | config.setGroup( "lookups" ); | 337 | config.setGroup( "lookups" ); |
338 | int lastLookup = config.readNumEntry( "time", 0 ); | 338 | int lastLookup = config.readNumEntry( "time", 0 ); |
339 | int lookupCount = config.readNumEntry( "count", 0 ); | 339 | int lookupCount = config.readNumEntry( "count", 0 ); |
340 | bool lastNtp = config.readBoolEntry( "lastNtp", false ); | 340 | bool lastNtp = config.readBoolEntry( "lastNtp", false ); |
341 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); | 341 | int time = TimeConversion::toUTC( QDateTime::currentDateTime() ); |
342 | config.writeEntry( "time", time ); | 342 | config.writeEntry( "time", time ); |
343 | 343 | ||
344 | // Calculate new time/time shift | 344 | // Calculate new time/time shift |
345 | QString _offset = "offset"; | 345 | QString _offset = "offset"; |
346 | QString _sec = "sec"; | 346 | QString _sec = "sec"; |
347 | QRegExp _reOffset = QRegExp( _offset ); | 347 | QRegExp _reOffset = QRegExp( _offset ); |
348 | QRegExp _reEndOffset = QRegExp( _sec ); | 348 | QRegExp _reEndOffset = QRegExp( _sec ); |
349 | int posOffset = _reOffset.match( ntpOutput ); | 349 | int posOffset = _reOffset.match( ntpOutput ); |
350 | int posEndOffset = _reEndOffset.match( ntpOutput, posOffset ); | 350 | int posEndOffset = _reEndOffset.match( ntpOutput, posOffset ); |
351 | posOffset += _offset.length() + 1; | 351 | posOffset += _offset.length() + 1; |
352 | QString diff = ntpOutput.mid( posOffset, posEndOffset - posOffset - 1 ); | 352 | QString diff = ntpOutput.mid( posOffset, posEndOffset - posOffset - 1 ); |
353 | 353 | ||
354 | float timeShift = diff.toFloat(); | 354 | float timeShift = diff.toFloat(); |
355 | if ( timeShift == 0.0 ) | 355 | if ( timeShift == 0.0 ) |
356 | return; | 356 | return; |
357 | int secsSinceLast = time - lastLookup; | 357 | int secsSinceLast = time - lastLookup; |
358 | output = tr( "%1 seconds").arg(QString::number( timeShift )); | 358 | output = tr( "%1 seconds").arg(QString::number( timeShift )); |
359 | 359 | ||
360 | // Display information on time server tab | 360 | // Display information on time server tab |
361 | if ( ntpTabEnabled ) | 361 | if ( ntpTabEnabled ) |
362 | { | 362 | { |
363 | ntpTab->setTimeShift( output ); | 363 | ntpTab->setTimeShift( output ); |
364 | ntpTab->setNewTime( dt.toString() ); | 364 | ntpTab->setNewTime( dt.toString() ); |
365 | } | 365 | } |
366 | 366 | ||
367 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60 * ntpDelay ) | 367 | if ( lastNtp && lastLookup > 0 && secsSinceLast > 60 * ntpDelay ) |
368 | { | 368 | { |
369 | QString grpname = QString( "lookup_" ).append( QString::number( lookupCount ) ); | 369 | QString grpname = QString( "lookup_" ).append( QString::number( lookupCount ) ); |
370 | config.setGroup( grpname ); | 370 | config.setGroup( grpname ); |
371 | lookupCount++; | 371 | lookupCount++; |
372 | predictTab->setShiftPerSec( (int)(timeShift / secsSinceLast) ); | 372 | predictTab->setShiftPerSec( (int)(timeShift / secsSinceLast) ); |
373 | config.writeEntry( "secsSinceLast", secsSinceLast ); | 373 | config.writeEntry( "secsSinceLast", secsSinceLast ); |
374 | config.writeEntry( "timeShift", QString::number( timeShift ) ); | 374 | config.writeEntry( "timeShift", QString::number( timeShift ) ); |
375 | config.setGroup( "lookups" ); | 375 | config.setGroup( "lookups" ); |
376 | config.writeEntry( "count", lookupCount ); | 376 | config.writeEntry( "count", lookupCount ); |
377 | config.writeEntry( "lastNtp", true ); | 377 | config.writeEntry( "lastNtp", true ); |
378 | } | 378 | } |
379 | } | 379 | } |
380 | 380 | ||
381 | void MainWindow::slotNTPDelayChanged( int delay ) | 381 | void MainWindow::slotNTPDelayChanged( int delay ) |
382 | { | 382 | { |
383 | ntpTimer->changeInterval( delay * 1000 * 60 ); | 383 | ntpTimer->changeInterval( delay * 1000 * 60 ); |
384 | ntpDelay = delay; | 384 | ntpDelay = delay; |
385 | } | 385 | } |
386 | 386 | ||
387 | void MainWindow::slotCheckNtp( int i ) | 387 | void MainWindow::slotCheckNtp( int i ) |
388 | { | 388 | { |
389 | if ( i == 0 ) | 389 | if ( i == 0 ) |
390 | { | 390 | { |
391 | if ( ntpDelayElapsed() ) | 391 | if ( ntpDelayElapsed() ) |
392 | { | 392 | { |
393 | runNTP(); | 393 | runNTP(); |
394 | disconnect( ntpTimer, SIGNAL(timeout()), this, SLOT(slotProbeNTPServer()) ); | 394 | disconnect( ntpTimer, SIGNAL(timeout()), this, SLOT(slotProbeNTPServer()) ); |
395 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotTimerGetNTPTime()) ); | 395 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotTimerGetNTPTime()) ); |
396 | } | 396 | } |
397 | else | 397 | else |
398 | { | 398 | { |
399 | disconnect(ntpTimer, SIGNAL(timeout()), this, SLOT(slotTimerGetNTPTime()) ); | 399 | disconnect(ntpTimer, SIGNAL(timeout()), this, SLOT(slotTimerGetNTPTime()) ); |
400 | connect(ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); | 400 | connect(ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); |
401 | } | 401 | } |
402 | } | 402 | } |
403 | else | 403 | else |
404 | { | 404 | { |
405 | predictTab->slotPredictTime(); | 405 | predictTab->slotPredictTime(); |
406 | if ( i > 0 ) | 406 | if ( i > 0 ) |
407 | { | 407 | { |
408 | QString output = tr( "Could not connect to server " ); | 408 | QString output = tr( "Could not connect to server " ); |
409 | output.append( settingsTab->ntpServer() ); | 409 | output.append( settingsTab->ntpServer() ); |
410 | ntpOutput.append( output ); | 410 | ntpOutput.append( output ); |
411 | if ( ntpTabEnabled ) | 411 | if ( ntpTabEnabled ) |
412 | ntpTab->addNtpOutput( output ); | 412 | ntpTab->addNtpOutput( output ); |
413 | } | 413 | } |
414 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); | 414 | connect( ntpTimer, SIGNAL(timeout()), SLOT(slotProbeNTPServer()) ); |
415 | } | 415 | } |
416 | } | 416 | } |