-rw-r--r-- | library/alarmserver.cpp | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/library/alarmserver.cpp b/library/alarmserver.cpp index 48ab9c1..ba7b015 100644 --- a/library/alarmserver.cpp +++ b/library/alarmserver.cpp | |||
@@ -1,507 +1,506 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of the Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include <qdir.h> | 21 | #include "alarmserver.h" |
22 | |||
23 | 22 | ||
23 | #include <qpe/global.h> | ||
24 | #include <qpe/qpeapplication.h> | 24 | #include <qpe/qpeapplication.h> |
25 | |||
26 | #include <qpe/qcopenvelope_qws.h> | 25 | #include <qpe/qcopenvelope_qws.h> |
27 | #include "alarmserver.h" | ||
28 | #include <qpe/timeconversion.h> | 26 | #include <qpe/timeconversion.h> |
29 | 27 | ||
28 | #include <qdir.h> | ||
29 | |||
30 | #include <sys/types.h> | 30 | #include <sys/types.h> |
31 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
32 | |||
33 | #include <stdlib.h> | 32 | #include <stdlib.h> |
34 | #include <unistd.h> | 33 | #include <unistd.h> |
35 | 34 | ||
36 | 35 | ||
37 | #undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume | 36 | #undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume |
38 | 37 | ||
39 | 38 | ||
40 | struct timerEventItem | 39 | struct timerEventItem |
41 | { | 40 | { |
42 | time_t UTCtime; | 41 | time_t UTCtime; |
43 | QCString channel, message; | 42 | QCString channel, message; |
44 | int data; | 43 | int data; |
45 | bool operator==( const timerEventItem &right ) const | 44 | bool operator==( const timerEventItem &right ) const |
46 | { | 45 | { |
47 | return ( UTCtime == right.UTCtime | 46 | return ( UTCtime == right.UTCtime |
48 | && channel == right.channel | 47 | && channel == right.channel |
49 | && message == right.message | 48 | && message == right.message |
50 | && data == right.data ); | 49 | && data == right.data ); |
51 | } | 50 | } |
52 | }; | 51 | }; |
53 | 52 | ||
54 | class TimerReceiverObject : public QObject | 53 | class TimerReceiverObject : public QObject |
55 | { | 54 | { |
56 | public: | 55 | public: |
57 | TimerReceiverObject() | 56 | TimerReceiverObject() |
58 | { } | 57 | { } |
59 | ~TimerReceiverObject() | 58 | ~TimerReceiverObject() |
60 | { } | 59 | { } |
61 | void resetTimer(); | 60 | void resetTimer(); |
62 | void setTimerEventItem(); | 61 | void setTimerEventItem(); |
63 | void deleteTimer(); | 62 | void deleteTimer(); |
64 | protected: | 63 | protected: |
65 | void timerEvent( QTimerEvent *te ); | 64 | void timerEvent( QTimerEvent *te ); |
66 | 65 | ||
67 | #ifdef USE_ATD | 66 | #ifdef USE_ATD |
68 | private: | 67 | private: |
69 | QString atfilename; | 68 | QString atfilename; |
70 | #endif | 69 | #endif |
71 | }; | 70 | }; |
72 | 71 | ||
73 | TimerReceiverObject *timerEventReceiver = NULL; | 72 | TimerReceiverObject *timerEventReceiver = NULL; |
74 | QList<timerEventItem> timerEventList; | 73 | QList<timerEventItem> timerEventList; |
75 | timerEventItem *nearestTimerEvent = NULL; | 74 | timerEventItem *nearestTimerEvent = NULL; |
76 | 75 | ||
77 | 76 | ||
78 | // set the timer to go off on the next event in the list | 77 | // set the timer to go off on the next event in the list |
79 | void setNearestTimerEvent() | 78 | void setNearestTimerEvent() |
80 | { | 79 | { |
81 | nearestTimerEvent = NULL; | 80 | nearestTimerEvent = NULL; |
82 | QListIterator<timerEventItem> it( timerEventList ); | 81 | QListIterator<timerEventItem> it( timerEventList ); |
83 | if ( *it ) | 82 | if ( *it ) |
84 | nearestTimerEvent = *it; | 83 | nearestTimerEvent = *it; |
85 | for ( ; *it; ++it ) | 84 | for ( ; *it; ++it ) |
86 | if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) | 85 | if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) |
87 | nearestTimerEvent = *it; | 86 | nearestTimerEvent = *it; |
88 | if (nearestTimerEvent) | 87 | if (nearestTimerEvent) |
89 | timerEventReceiver->resetTimer(); | 88 | timerEventReceiver->resetTimer(); |
90 | else | 89 | else |
91 | timerEventReceiver->deleteTimer(); | 90 | timerEventReceiver->deleteTimer(); |
92 | } | 91 | } |
93 | 92 | ||
94 | 93 | ||
95 | //store current state to file | 94 | //store current state to file |
96 | //Simple implementation. Should run on a timer. | 95 | //Simple implementation. Should run on a timer. |
97 | 96 | ||
98 | static void saveState() | 97 | static void saveState() |
99 | { | 98 | { |
100 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); | 99 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); |
101 | if ( timerEventList.isEmpty() ) { | 100 | if ( timerEventList.isEmpty() ) { |
102 | unlink( savefilename ); | 101 | unlink( savefilename ); |
103 | return ; | 102 | return ; |
104 | } | 103 | } |
105 | 104 | ||
106 | QFile savefile(savefilename + ".new"); | 105 | QFile savefile(savefilename + ".new"); |
107 | if ( savefile.open(IO_WriteOnly) ) { | 106 | if ( savefile.open(IO_WriteOnly) ) { |
108 | QDataStream ds( &savefile ); | 107 | QDataStream ds( &savefile ); |
109 | 108 | ||
110 | //save | 109 | //save |
111 | 110 | ||
112 | QListIterator<timerEventItem> it( timerEventList ); | 111 | QListIterator<timerEventItem> it( timerEventList ); |
113 | for ( ; *it; ++it ) { | 112 | for ( ; *it; ++it ) { |
114 | ds << it.current()->UTCtime; | 113 | ds << it.current()->UTCtime; |
115 | ds << it.current()->channel; | 114 | ds << it.current()->channel; |
116 | ds << it.current()->message; | 115 | ds << it.current()->message; |
117 | ds << it.current()->data; | 116 | ds << it.current()->data; |
118 | } | 117 | } |
119 | 118 | ||
120 | 119 | ||
121 | savefile.close(); | 120 | savefile.close(); |
122 | unlink( savefilename ); | 121 | unlink( savefilename ); |
123 | QDir d; | 122 | QDir d; |
124 | d.rename(savefilename + ".new", savefilename); | 123 | d.rename(savefilename + ".new", savefilename); |
125 | 124 | ||
126 | } | 125 | } |
127 | } | 126 | } |
128 | 127 | ||
129 | /*! | 128 | /*! |
130 | Sets up the alarm server. Restoring to previous state (session management). | 129 | Sets up the alarm server. Restoring to previous state (session management). |
131 | */ | 130 | */ |
132 | void AlarmServer::initialize() | 131 | void AlarmServer::initialize() |
133 | { | 132 | { |
134 | //read autosave file and put events in timerEventList | 133 | //read autosave file and put events in timerEventList |
135 | 134 | ||
136 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); | 135 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); |
137 | 136 | ||
138 | QFile savefile(savefilename); | 137 | QFile savefile(savefilename); |
139 | if ( savefile.open(IO_ReadOnly) ) { | 138 | if ( savefile.open(IO_ReadOnly) ) { |
140 | QDataStream ds( &savefile ); | 139 | QDataStream ds( &savefile ); |
141 | while ( !ds.atEnd() ) { | 140 | while ( !ds.atEnd() ) { |
142 | timerEventItem *newTimerEventItem = new timerEventItem; | 141 | timerEventItem *newTimerEventItem = new timerEventItem; |
143 | ds >> newTimerEventItem->UTCtime; | 142 | ds >> newTimerEventItem->UTCtime; |
144 | ds >> newTimerEventItem->channel; | 143 | ds >> newTimerEventItem->channel; |
145 | ds >> newTimerEventItem->message; | 144 | ds >> newTimerEventItem->message; |
146 | ds >> newTimerEventItem->data; | 145 | ds >> newTimerEventItem->data; |
147 | timerEventList.append( newTimerEventItem ); | 146 | timerEventList.append( newTimerEventItem ); |
148 | } | 147 | } |
149 | savefile.close(); | 148 | savefile.close(); |
150 | if (!timerEventReceiver) | 149 | if (!timerEventReceiver) |
151 | timerEventReceiver = new TimerReceiverObject; | 150 | timerEventReceiver = new TimerReceiverObject; |
152 | setNearestTimerEvent(); | 151 | setNearestTimerEvent(); |
153 | } | 152 | } |
154 | } | 153 | } |
155 | 154 | ||
156 | 155 | ||
157 | #ifdef USE_ATD | 156 | #ifdef USE_ATD |
158 | 157 | ||
159 | static const char* atdir = "/var/spool/at/"; | 158 | static const char* atdir = "/var/spool/at/"; |
160 | 159 | ||
161 | static bool triggerAtd( bool writeHWClock = FALSE ) | 160 | static bool triggerAtd( bool writeHWClock = FALSE ) |
162 | { | 161 | { |
163 | QFile trigger(QString(atdir) + "trigger"); | 162 | QFile trigger(QString(atdir) + "trigger"); |
164 | if ( trigger.open(IO_WriteOnly | IO_Raw) ) { | 163 | if ( trigger.open(IO_WriteOnly | IO_Raw) ) { |
165 | if ( trigger.writeBlock("\n", 2) != 2 ) { | 164 | if ( trigger.writeBlock("\n", 2) != 2 ) { |
166 | QMessageBox::critical( 0, QObject::tr( "Out of Space" ), | 165 | QMessageBox::critical( 0, QObject::tr( "Out of Space" ), |
167 | QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); | 166 | QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); |
168 | trigger.close(); | 167 | trigger.close(); |
169 | QFile::remove | 168 | QFile::remove |
170 | ( trigger.name() ); | 169 | ( trigger.name() ); |
171 | return FALSE; | 170 | return FALSE; |
172 | } | 171 | } |
173 | return TRUE; | 172 | return TRUE; |
174 | } | 173 | } |
175 | return FALSE; | 174 | return FALSE; |
176 | } | 175 | } |
177 | 176 | ||
178 | #else | 177 | #else |
179 | 178 | ||
180 | static bool writeResumeAt ( time_t wakeup ) | 179 | static bool writeResumeAt ( time_t wakeup ) |
181 | { | 180 | { |
182 | FILE *fp = ::fopen ( "/var/run/resumeat", "w" ); | 181 | FILE *fp = ::fopen ( "/var/run/resumeat", "w" ); |
183 | 182 | ||
184 | if ( fp ) { | 183 | if ( fp ) { |
185 | ::fprintf ( fp, "%d\n", (int) wakeup ); | 184 | ::fprintf ( fp, "%d\n", (int) wakeup ); |
186 | ::fclose ( fp ); | 185 | ::fclose ( fp ); |
187 | } | 186 | } |
188 | else | 187 | else |
189 | qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); | 188 | qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); |
190 | 189 | ||
191 | return ( fp ); | 190 | return ( fp ); |
192 | } | 191 | } |
193 | 192 | ||
194 | #endif | 193 | #endif |
195 | 194 | ||
196 | void TimerReceiverObject::deleteTimer() | 195 | void TimerReceiverObject::deleteTimer() |
197 | { | 196 | { |
198 | #ifdef USE_ATD | 197 | #ifdef USE_ATD |
199 | if ( !atfilename.isEmpty() ) { | 198 | if ( !atfilename.isEmpty() ) { |
200 | unlink( atfilename ); | 199 | unlink( atfilename ); |
201 | atfilename = QString::null; | 200 | atfilename = QString::null; |
202 | triggerAtd( FALSE ); | 201 | triggerAtd( FALSE ); |
203 | } | 202 | } |
204 | #else | 203 | #else |
205 | writeResumeAt ( 0 ); | 204 | writeResumeAt ( 0 ); |
206 | #endif | 205 | #endif |
207 | } | 206 | } |
208 | 207 | ||
209 | void TimerReceiverObject::resetTimer() | 208 | void TimerReceiverObject::resetTimer() |
210 | { | 209 | { |
211 | const int maxsecs = 2147000; | 210 | const int maxsecs = 2147000; |
212 | QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); | 211 | QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); |
213 | QDateTime now = QDateTime::currentDateTime(); | 212 | QDateTime now = QDateTime::currentDateTime(); |
214 | if ( nearest < now ) | 213 | if ( nearest < now ) |
215 | nearest = now; | 214 | nearest = now; |
216 | int secs = TimeConversion::secsTo( now, nearest ); | 215 | int secs = TimeConversion::secsTo( now, nearest ); |
217 | if ( secs > maxsecs ) { | 216 | if ( secs > maxsecs ) { |
218 | // too far for millisecond timing | 217 | // too far for millisecond timing |
219 | secs = maxsecs; | 218 | secs = maxsecs; |
220 | } | 219 | } |
221 | 220 | ||
222 | // System timer (needed so that we wake from deep sleep), | 221 | // System timer (needed so that we wake from deep sleep), |
223 | // from the Epoch in seconds. | 222 | // from the Epoch in seconds. |
224 | // | 223 | // |
225 | int at_secs = TimeConversion::toUTC(nearest); | 224 | int at_secs = TimeConversion::toUTC(nearest); |
226 | // qDebug("reset timer to %d seconds from Epoch",at_secs); | 225 | // qDebug("reset timer to %d seconds from Epoch",at_secs); |
227 | 226 | ||
228 | #ifdef USE_ATD | 227 | #ifdef USE_ATD |
229 | 228 | ||
230 | QString fn = atdir + QString::number(at_secs) + "." | 229 | QString fn = atdir + QString::number(at_secs) + "." |
231 | + QString::number(getpid()); | 230 | + QString::number(getpid()); |
232 | if ( fn != atfilename ) { | 231 | if ( fn != atfilename ) { |
233 | QFile atfile(fn + ".new"); | 232 | QFile atfile(fn + ".new"); |
234 | if ( atfile.open(IO_WriteOnly | IO_Raw) ) { | 233 | if ( atfile.open(IO_WriteOnly | IO_Raw) ) { |
235 | int total_written; | 234 | int total_written; |
236 | 235 | ||
237 | // just wake up and delete the at file | 236 | // just wake up and delete the at file |
238 | QString cmd = "#!/bin/sh\nrm " + fn; | 237 | QString cmd = "#!/bin/sh\nrm " + fn; |
239 | total_written = atfile.writeBlock(cmd.latin1(), cmd.length()); | 238 | total_written = atfile.writeBlock(cmd.latin1(), cmd.length()); |
240 | if ( total_written != int(cmd.length()) ) { | 239 | if ( total_written != int(cmd.length()) ) { |
241 | QMessageBox::critical( 0, tr("Out of Space"), | 240 | QMessageBox::critical( 0, tr("Out of Space"), |
242 | tr("Unable to schedule alarm.\n" | 241 | tr("Unable to schedule alarm.\n" |
243 | "Please free up space and try again") ); | 242 | "Please free up space and try again") ); |
244 | atfile.close(); | 243 | atfile.close(); |
245 | QFile::remove | 244 | QFile::remove |
246 | ( atfile.name() ); | 245 | ( atfile.name() ); |
247 | return ; | 246 | return ; |
248 | } | 247 | } |
249 | atfile.close(); | 248 | atfile.close(); |
250 | unlink( atfilename ); | 249 | unlink( atfilename ); |
251 | QDir d; | 250 | QDir d; |
252 | d.rename(fn + ".new", fn); | 251 | d.rename(fn + ".new", fn); |
253 | chmod(fn.latin1(), 0755); | 252 | chmod(fn.latin1(), 0755); |
254 | atfilename = fn; | 253 | atfilename = fn; |
255 | triggerAtd( FALSE ); | 254 | triggerAtd( FALSE ); |
256 | } | 255 | } |
257 | else { | 256 | else { |
258 | qWarning("Cannot open atd file %s", fn.latin1()); | 257 | qWarning("Cannot open atd file %s", fn.latin1()); |
259 | } | 258 | } |
260 | } | 259 | } |
261 | #else | 260 | #else |
262 | writeResumeAt ( at_secs ); | 261 | writeResumeAt ( at_secs ); |
263 | 262 | ||
264 | #endif | 263 | #endif |
265 | 264 | ||
266 | // Qt timers (does the actual alarm) | 265 | // Qt timers (does the actual alarm) |
267 | // from now in milliseconds | 266 | // from now in milliseconds |
268 | // | 267 | // |
269 | qDebug("AlarmServer waiting %d seconds", secs); | 268 | qDebug("AlarmServer waiting %d seconds", secs); |
270 | startTimer( 1000 * secs + 500 ); | 269 | startTimer( 1000 * secs + 500 ); |
271 | } | 270 | } |
272 | 271 | ||
273 | void TimerReceiverObject::timerEvent( QTimerEvent * ) | 272 | void TimerReceiverObject::timerEvent( QTimerEvent * ) |
274 | { | 273 | { |
275 | bool needSave = FALSE; | 274 | bool needSave = FALSE; |
276 | killTimers(); | 275 | killTimers(); |
277 | if (nearestTimerEvent) { | 276 | if (nearestTimerEvent) { |
278 | if ( nearestTimerEvent->UTCtime | 277 | if ( nearestTimerEvent->UTCtime |
279 | <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { | 278 | <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { |
280 | #ifndef QT_NO_COP | 279 | #ifndef QT_NO_COP |
281 | QCopEnvelope e( nearestTimerEvent->channel, | 280 | QCopEnvelope e( nearestTimerEvent->channel, |
282 | nearestTimerEvent->message ); | 281 | nearestTimerEvent->message ); |
283 | e << TimeConversion::fromUTC( nearestTimerEvent->UTCtime ) | 282 | e << TimeConversion::fromUTC( nearestTimerEvent->UTCtime ) |
284 | << nearestTimerEvent->data; | 283 | << nearestTimerEvent->data; |
285 | #endif | 284 | #endif |
286 | 285 | ||
287 | timerEventList.remove( nearestTimerEvent ); | 286 | timerEventList.remove( nearestTimerEvent ); |
288 | needSave = TRUE; | 287 | needSave = TRUE; |
289 | } | 288 | } |
290 | setNearestTimerEvent(); | 289 | setNearestTimerEvent(); |
291 | } | 290 | } |
292 | else { | 291 | else { |
293 | resetTimer(); | 292 | resetTimer(); |
294 | } | 293 | } |
295 | if ( needSave ) | 294 | if ( needSave ) |
296 | saveState(); | 295 | saveState(); |
297 | } | 296 | } |
298 | 297 | ||
299 | /*! | 298 | /*! |
300 | \class AlarmServer alarmserver.h | 299 | \class AlarmServer alarmserver.h |
301 | \brief The AlarmServer class allows alarms to be scheduled and unscheduled. | 300 | \brief The AlarmServer class allows alarms to be scheduled and unscheduled. |
302 | 301 | ||
303 | Applications can schedule alarms with addAlarm() and can | 302 | Applications can schedule alarms with addAlarm() and can |
304 | unschedule alarms with deleteAlarm(). When the time for an alarm | 303 | unschedule alarms with deleteAlarm(). When the time for an alarm |
305 | to go off is reached the specified \link qcop.html QCop\endlink | 304 | to go off is reached the specified \link qcop.html QCop\endlink |
306 | message is sent on the specified channel (optionally with | 305 | message is sent on the specified channel (optionally with |
307 | additional data). | 306 | additional data). |
308 | 307 | ||
309 | Scheduling an alarm using this class is important (rather just using | 308 | Scheduling an alarm using this class is important (rather just using |
310 | a QTimer) since the machine may be asleep and needs to get woken up using | 309 | a QTimer) since the machine may be asleep and needs to get woken up using |
311 | the Linux kernel which implements this at the kernel level to minimize | 310 | the Linux kernel which implements this at the kernel level to minimize |
312 | battery usage while asleep. | 311 | battery usage while asleep. |
313 | 312 | ||
314 | A small example on how to use AlarmServer. | 313 | A small example on how to use AlarmServer. |
315 | 314 | ||
316 | First we need to connect a slot the AppMessage QCOP call. appMessage | 315 | First we need to connect a slot the AppMessage QCOP call. appMessage |
317 | will be emitted if QPE/Application/appname gets called. | 316 | will be emitted if QPE/Application/appname gets called. |
318 | 317 | ||
319 | \code | 318 | \code |
320 | TestApp::TestApp(QWidget *parent, const char* name, WFlags fl ) | 319 | TestApp::TestApp(QWidget *parent, const char* name, WFlags fl ) |
321 | : QMainWindow(parent,name,fl){ | 320 | : QMainWindow(parent,name,fl){ |
322 | connect(qApp,SIGNAL(appMessage(const QCString&,const QByteArray&)), | 321 | connect(qApp,SIGNAL(appMessage(const QCString&,const QByteArray&)), |
323 | this,SLOT(slotAppMessage(const QCString&,const QByteArray&))); | 322 | this,SLOT(slotAppMessage(const QCString&,const QByteArray&))); |
324 | } | 323 | } |
325 | \endcode | 324 | \endcode |
326 | 325 | ||
327 | To add / delete an alarm, you can use the static method AlarmServer::addAlarm and | 326 | To add / delete an alarm, you can use the static method AlarmServer::addAlarm and |
328 | AlarmServer::deleteAlarm. Note that an old (expired) alarm will automatically be deleted | 327 | AlarmServer::deleteAlarm. Note that an old (expired) alarm will automatically be deleted |
329 | from the alarmserver list, but a change in timing will have the effect, that both | 328 | from the alarmserver list, but a change in timing will have the effect, that both |
330 | alarms will be emitted. So if you change an Alarm be sure to delete the old one! | 329 | alarms will be emitted. So if you change an Alarm be sure to delete the old one! |
331 | @see addAlarm | 330 | @see addAlarm |
332 | 331 | ||
333 | \code | 332 | \code |
334 | QDateTime oldDt = oldAlarmDateTime(); | 333 | QDateTime oldDt = oldAlarmDateTime(); |
335 | QPEApplication::execDialog(ourDlg); | 334 | QPEApplication::execDialog(ourDlg); |
336 | QDateTime newDt = ourDlg->dateTime(); | 335 | QDateTime newDt = ourDlg->dateTime(); |
337 | if(newDt == oldDt ) return; | 336 | if(newDt == oldDt ) return; |
338 | @slash* code is missing for unsetting an alarm *@slash | 337 | @slash* code is missing for unsetting an alarm *@slash |
339 | 338 | ||
340 | AlarmServer::deleteAlarm(oldDt,"QPE/Application/appname","checkAlarm(QDateTime,int)",0); | 339 | AlarmServer::deleteAlarm(oldDt,"QPE/Application/appname","checkAlarm(QDateTime,int)",0); |
341 | AlarmServer::addAlarm( newDt,"QPE/AlarmServer/appname","checkAlarm(QDateTime,int)",0); | 340 | AlarmServer::addAlarm( newDt,"QPE/AlarmServer/appname","checkAlarm(QDateTime,int)",0); |
342 | 341 | ||
343 | \endcode | 342 | \endcode |
344 | 343 | ||
345 | Now once the Alarm is emitted you need to check the appMessage and then do what you want. | 344 | Now once the Alarm is emitted you need to check the appMessage and then do what you want. |
346 | \code | 345 | \code |
347 | void TestApp::slotAppMessage(const QCString& str, const QByteArray& ar ){ | 346 | void TestApp::slotAppMessage(const QCString& str, const QByteArray& ar ){ |
348 | QDataStream stream(ar,IO_ReadOnly); | 347 | QDataStream stream(ar,IO_ReadOnly); |
349 | if(str == "checkAlarm(QDateTime,int)" ){ | 348 | if(str == "checkAlarm(QDateTime,int)" ){ |
350 | QDateTime dt; | 349 | QDateTime dt; |
351 | int a; | 350 | int a; |
352 | stream >> dt >> a; | 351 | stream >> dt >> a; |
353 | // fire up alarm | 352 | // fire up alarm |
354 | } | 353 | } |
355 | } | 354 | } |
356 | \endcode | 355 | \endcode |
357 | 356 | ||
358 | \ingroup qtopiaemb | 357 | \ingroup qtopiaemb |
359 | \sa QCopEnvelope | 358 | \sa QCopEnvelope |
360 | @see QPEApplication::appMessage(const QCString&,const QByteArray&) | 359 | @see QPEApplication::appMessage(const QCString&,const QByteArray&) |
361 | @see OPimMainWindow | 360 | @see OPimMainWindow |
362 | @see ODevice::alarmSound() | 361 | @see ODevice::alarmSound() |
363 | @see Sound::soundAlarm() | 362 | @see Sound::soundAlarm() |
364 | */ | 363 | */ |
365 | 364 | ||
366 | /*! | 365 | /*! |
367 | Schedules an alarm to go off at (or soon after) time \a when. When | 366 | Schedules an alarm to go off at (or soon after) time \a when. When |
368 | the alarm goes off, the \link qcop.html QCop\endlink \a message will | 367 | the alarm goes off, the \link qcop.html QCop\endlink \a message will |
369 | be sent to \a channel, with \a data as a parameter. | 368 | be sent to \a channel, with \a data as a parameter. |
370 | 369 | ||
371 | If this function is called with exactly the same data as a previous | 370 | If this function is called with exactly the same data as a previous |
372 | call the subsequent call is ignored, so there is only ever one alarm | 371 | call the subsequent call is ignored, so there is only ever one alarm |
373 | with a given set of parameters. | 372 | with a given set of parameters. |
374 | 373 | ||
375 | Once an alarm is emitted. The \a channel with a \a message will be emitted | 374 | Once an alarm is emitted. The \a channel with a \a message will be emitted |
376 | and data will be send. | 375 | and data will be send. |
377 | The QDateTime and int are the two parameters included in the QCOP message. | 376 | The QDateTime and int are the two parameters included in the QCOP message. |
378 | You can specify channel, message and the integer parameter. QDateTime will be | 377 | You can specify channel, message and the integer parameter. QDateTime will be |
379 | the datetime of the QCop call. | 378 | the datetime of the QCop call. |
380 | 379 | ||
381 | @param when The QDateTime of the alarm | 380 | @param when The QDateTime of the alarm |
382 | @param channel The channel which gets called once the alarm is emitted | 381 | @param channel The channel which gets called once the alarm is emitted |
383 | @param message The message to be send to the channel | 382 | @param message The message to be send to the channel |
384 | @param data Additional data as integer | 383 | @param data Additional data as integer |
385 | 384 | ||
386 | @see QCopChannel | 385 | @see QCopChannel |
387 | \sa deleteAlarm() | 386 | \sa deleteAlarm() |
388 | */ | 387 | */ |
389 | void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, | 388 | void AlarmServer::addAlarm ( QDateTime when, const QCString& channel, |
390 | const QCString& message, int data) | 389 | const QCString& message, int data) |
391 | { | 390 | { |
392 | if ( qApp->type() == QApplication::GuiServer ) { | 391 | if ( qApp->type() == QApplication::GuiServer ) { |
393 | bool needSave = FALSE; | 392 | bool needSave = FALSE; |
394 | // Here we are the server so either it has been directly called from | 393 | // Here we are the server so either it has been directly called from |
395 | // within the server or it has been sent to us from a client via QCop | 394 | // within the server or it has been sent to us from a client via QCop |
396 | if (!timerEventReceiver) | 395 | if (!timerEventReceiver) |
397 | timerEventReceiver = new TimerReceiverObject; | 396 | timerEventReceiver = new TimerReceiverObject; |
398 | 397 | ||
399 | timerEventItem *newTimerEventItem = new timerEventItem; | 398 | timerEventItem *newTimerEventItem = new timerEventItem; |
400 | newTimerEventItem->UTCtime = TimeConversion::toUTC( when ); | 399 | newTimerEventItem->UTCtime = TimeConversion::toUTC( when ); |
401 | newTimerEventItem->channel = channel; | 400 | newTimerEventItem->channel = channel; |
402 | newTimerEventItem->message = message; | 401 | newTimerEventItem->message = message; |
403 | newTimerEventItem->data = data; | 402 | newTimerEventItem->data = data; |
404 | // explore the case of already having the event in here... | 403 | // explore the case of already having the event in here... |
405 | QListIterator<timerEventItem> it( timerEventList ); | 404 | QListIterator<timerEventItem> it( timerEventList ); |
406 | for ( ; *it; ++it ) | 405 | for ( ; *it; ++it ) |
407 | if ( *(*it) == *newTimerEventItem ) | 406 | if ( *(*it) == *newTimerEventItem ) |
408 | return ; | 407 | return ; |
409 | // if we made it here, it is okay to add the item... | 408 | // if we made it here, it is okay to add the item... |
410 | timerEventList.append( newTimerEventItem ); | 409 | timerEventList.append( newTimerEventItem ); |
411 | needSave = TRUE; | 410 | needSave = TRUE; |
412 | // quicker than using setNearestTimerEvent() | 411 | // quicker than using setNearestTimerEvent() |
413 | if ( nearestTimerEvent ) { | 412 | if ( nearestTimerEvent ) { |
414 | if (newTimerEventItem->UTCtime < nearestTimerEvent->UTCtime) { | 413 | if (newTimerEventItem->UTCtime < nearestTimerEvent->UTCtime) { |
415 | nearestTimerEvent = newTimerEventItem; | 414 | nearestTimerEvent = newTimerEventItem; |
416 | timerEventReceiver->killTimers(); | 415 | timerEventReceiver->killTimers(); |
417 | timerEventReceiver->resetTimer(); | 416 | timerEventReceiver->resetTimer(); |
418 | } | 417 | } |
419 | } | 418 | } |
420 | else { | 419 | else { |
421 | nearestTimerEvent = newTimerEventItem; | 420 | nearestTimerEvent = newTimerEventItem; |
422 | timerEventReceiver->resetTimer(); | 421 | timerEventReceiver->resetTimer(); |
423 | } | 422 | } |
424 | if ( needSave ) | 423 | if ( needSave ) |
425 | saveState(); | 424 | saveState(); |
426 | } | 425 | } |
427 | else { | 426 | else { |
428 | #ifndef QT_NO_COP | 427 | #ifndef QT_NO_COP |
429 | QCopEnvelope e( "QPE/System", "addAlarm(QDateTime,QCString,QCString,int)" ); | 428 | QCopEnvelope e( "QPE/System", "addAlarm(QDateTime,QCString,QCString,int)" ); |
430 | e << when << channel << message << data; | 429 | e << when << channel << message << data; |
431 | #endif | 430 | #endif |
432 | 431 | ||
433 | } | 432 | } |
434 | } | 433 | } |
435 | 434 | ||
436 | /*! | 435 | /*! |
437 | Deletes previously scheduled alarms which match \a when, \a channel, | 436 | Deletes previously scheduled alarms which match \a when, \a channel, |
438 | \a message, and \a data. | 437 | \a message, and \a data. |
439 | 438 | ||
440 | Passing null values for \a when, \a channel, or for the \link | 439 | Passing null values for \a when, \a channel, or for the \link |
441 | qcop.html QCop\endlink \a message, acts as a wildcard meaning "any". | 440 | qcop.html QCop\endlink \a message, acts as a wildcard meaning "any". |
442 | Similarly, passing -1 for \a data indicates "any". | 441 | Similarly, passing -1 for \a data indicates "any". |
443 | 442 | ||
444 | If there is no matching alarm, nothing happens. | 443 | If there is no matching alarm, nothing happens. |
445 | 444 | ||
446 | \sa addAlarm() | 445 | \sa addAlarm() |
447 | 446 | ||
448 | */ | 447 | */ |
449 | void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QCString& message, int data) | 448 | void AlarmServer::deleteAlarm (QDateTime when, const QCString& channel, const QCString& message, int data) |
450 | { | 449 | { |
451 | if ( qApp->type() == QApplication::GuiServer) { | 450 | if ( qApp->type() == QApplication::GuiServer) { |
452 | bool needSave = FALSE; | 451 | bool needSave = FALSE; |
453 | if ( timerEventReceiver != NULL ) { | 452 | if ( timerEventReceiver != NULL ) { |
454 | timerEventReceiver->killTimers(); | 453 | timerEventReceiver->killTimers(); |
455 | 454 | ||
456 | // iterate over the list of events | 455 | // iterate over the list of events |
457 | QListIterator<timerEventItem> it( timerEventList ); | 456 | QListIterator<timerEventItem> it( timerEventList ); |
458 | time_t deleteTime = TimeConversion::toUTC( when ); | 457 | time_t deleteTime = TimeConversion::toUTC( when ); |
459 | for ( ; *it; ++it ) { | 458 | for ( ; *it; ++it ) { |
460 | // if its a match, delete it | 459 | // if its a match, delete it |
461 | if ( ( (*it)->UTCtime == deleteTime || when.isNull() ) | 460 | if ( ( (*it)->UTCtime == deleteTime || when.isNull() ) |
462 | && ( channel.isNull() || (*it)->channel == channel ) | 461 | && ( channel.isNull() || (*it)->channel == channel ) |
463 | && ( message.isNull() || (*it)->message == message ) | 462 | && ( message.isNull() || (*it)->message == message ) |
464 | && ( data == -1 || (*it)->data == data ) ) { | 463 | && ( data == -1 || (*it)->data == data ) ) { |
465 | // if it's first, then we need to update the timer | 464 | // if it's first, then we need to update the timer |
466 | if ( (*it) == nearestTimerEvent ) { | 465 | if ( (*it) == nearestTimerEvent ) { |
467 | timerEventList.remove(*it); | 466 | timerEventList.remove(*it); |
468 | setNearestTimerEvent(); | 467 | setNearestTimerEvent(); |
469 | } | 468 | } |
470 | else { | 469 | else { |
471 | timerEventList.remove(*it); | 470 | timerEventList.remove(*it); |
472 | } | 471 | } |
473 | needSave = TRUE; | 472 | needSave = TRUE; |
474 | } | 473 | } |
475 | } | 474 | } |
476 | if ( nearestTimerEvent ) | 475 | if ( nearestTimerEvent ) |
477 | timerEventReceiver->resetTimer(); | 476 | timerEventReceiver->resetTimer(); |
478 | } | 477 | } |
479 | if ( needSave ) | 478 | if ( needSave ) |
480 | saveState(); | 479 | saveState(); |
481 | } | 480 | } |
482 | else { | 481 | else { |
483 | #ifndef QT_NO_COP | 482 | #ifndef QT_NO_COP |
484 | QCopEnvelope e( "QPE/System", "deleteAlarm(QDateTime,QCString,QCString,int)" ); | 483 | QCopEnvelope e( "QPE/System", "deleteAlarm(QDateTime,QCString,QCString,int)" ); |
485 | e << when << channel << message << data; | 484 | e << when << channel << message << data; |
486 | #endif | 485 | #endif |
487 | 486 | ||
488 | } | 487 | } |
489 | } | 488 | } |
490 | 489 | ||
491 | /*! | 490 | /*! |
492 | The implementation depends on the mode of AlarmServer. If the AlarmServer | 491 | The implementation depends on the mode of AlarmServer. If the AlarmServer |
493 | uses atd the current system time will be written to the hardware clock. | 492 | uses atd the current system time will be written to the hardware clock. |
494 | If the AlarmServer relies on opie-alarm the time will be written once the | 493 | If the AlarmServer relies on opie-alarm the time will be written once the |
495 | device gets suspended. opie-alarm is used by the Zaurus, iPAQs and SIMpad | 494 | device gets suspended. opie-alarm is used by the Zaurus, iPAQs and SIMpad |
496 | */ | 495 | */ |
497 | void Global::writeHWClock() | 496 | void Global::writeHWClock() |
498 | { | 497 | { |
499 | #ifdef USE_ATD | 498 | #ifdef USE_ATD |
500 | if ( !triggerAtd( TRUE ) ) { | 499 | if ( !triggerAtd( TRUE ) ) { |
501 | // atd not running? set it ourselves | 500 | // atd not running? set it ourselves |
502 | system("/sbin/hwclock --systohc"); // ##### UTC? | 501 | system("/sbin/hwclock --systohc"); // ##### UTC? |
503 | } | 502 | } |
504 | #else | 503 | #else |
505 | // hwclock is written on suspend | 504 | // hwclock is written on suspend |
506 | #endif | 505 | #endif |
507 | } | 506 | } |