-rw-r--r-- | library/alarmserver.cpp | 5 |
1 files changed, 0 insertions, 5 deletions
diff --git a/library/alarmserver.cpp b/library/alarmserver.cpp index 6f6f32d..48ab9c1 100644 --- a/library/alarmserver.cpp +++ b/library/alarmserver.cpp | |||
@@ -1,285 +1,280 @@ | |||
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 <qdir.h> |
22 | #include <qfile.h> | ||
23 | #include <qmessagebox.h> | ||
24 | #include <qtextstream.h> | ||
25 | 22 | ||
26 | 23 | ||
27 | #include <qpe/qpeapplication.h> | 24 | #include <qpe/qpeapplication.h> |
28 | #include "global.h" | ||
29 | #include "resource.h" | ||
30 | 25 | ||
31 | #include <qpe/qcopenvelope_qws.h> | 26 | #include <qpe/qcopenvelope_qws.h> |
32 | #include "alarmserver.h" | 27 | #include "alarmserver.h" |
33 | #include <qpe/timeconversion.h> | 28 | #include <qpe/timeconversion.h> |
34 | 29 | ||
35 | #include <sys/types.h> | 30 | #include <sys/types.h> |
36 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
37 | 32 | ||
38 | #include <stdlib.h> | 33 | #include <stdlib.h> |
39 | #include <unistd.h> | 34 | #include <unistd.h> |
40 | 35 | ||
41 | 36 | ||
42 | #undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume | 37 | #undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume |
43 | 38 | ||
44 | 39 | ||
45 | struct timerEventItem | 40 | struct timerEventItem |
46 | { | 41 | { |
47 | time_t UTCtime; | 42 | time_t UTCtime; |
48 | QCString channel, message; | 43 | QCString channel, message; |
49 | int data; | 44 | int data; |
50 | bool operator==( const timerEventItem &right ) const | 45 | bool operator==( const timerEventItem &right ) const |
51 | { | 46 | { |
52 | return ( UTCtime == right.UTCtime | 47 | return ( UTCtime == right.UTCtime |
53 | && channel == right.channel | 48 | && channel == right.channel |
54 | && message == right.message | 49 | && message == right.message |
55 | && data == right.data ); | 50 | && data == right.data ); |
56 | } | 51 | } |
57 | }; | 52 | }; |
58 | 53 | ||
59 | class TimerReceiverObject : public QObject | 54 | class TimerReceiverObject : public QObject |
60 | { | 55 | { |
61 | public: | 56 | public: |
62 | TimerReceiverObject() | 57 | TimerReceiverObject() |
63 | { } | 58 | { } |
64 | ~TimerReceiverObject() | 59 | ~TimerReceiverObject() |
65 | { } | 60 | { } |
66 | void resetTimer(); | 61 | void resetTimer(); |
67 | void setTimerEventItem(); | 62 | void setTimerEventItem(); |
68 | void deleteTimer(); | 63 | void deleteTimer(); |
69 | protected: | 64 | protected: |
70 | void timerEvent( QTimerEvent *te ); | 65 | void timerEvent( QTimerEvent *te ); |
71 | 66 | ||
72 | #ifdef USE_ATD | 67 | #ifdef USE_ATD |
73 | private: | 68 | private: |
74 | QString atfilename; | 69 | QString atfilename; |
75 | #endif | 70 | #endif |
76 | }; | 71 | }; |
77 | 72 | ||
78 | TimerReceiverObject *timerEventReceiver = NULL; | 73 | TimerReceiverObject *timerEventReceiver = NULL; |
79 | QList<timerEventItem> timerEventList; | 74 | QList<timerEventItem> timerEventList; |
80 | timerEventItem *nearestTimerEvent = NULL; | 75 | timerEventItem *nearestTimerEvent = NULL; |
81 | 76 | ||
82 | 77 | ||
83 | // set the timer to go off on the next event in the list | 78 | // set the timer to go off on the next event in the list |
84 | void setNearestTimerEvent() | 79 | void setNearestTimerEvent() |
85 | { | 80 | { |
86 | nearestTimerEvent = NULL; | 81 | nearestTimerEvent = NULL; |
87 | QListIterator<timerEventItem> it( timerEventList ); | 82 | QListIterator<timerEventItem> it( timerEventList ); |
88 | if ( *it ) | 83 | if ( *it ) |
89 | nearestTimerEvent = *it; | 84 | nearestTimerEvent = *it; |
90 | for ( ; *it; ++it ) | 85 | for ( ; *it; ++it ) |
91 | if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) | 86 | if ( (*it)->UTCtime < nearestTimerEvent->UTCtime ) |
92 | nearestTimerEvent = *it; | 87 | nearestTimerEvent = *it; |
93 | if (nearestTimerEvent) | 88 | if (nearestTimerEvent) |
94 | timerEventReceiver->resetTimer(); | 89 | timerEventReceiver->resetTimer(); |
95 | else | 90 | else |
96 | timerEventReceiver->deleteTimer(); | 91 | timerEventReceiver->deleteTimer(); |
97 | } | 92 | } |
98 | 93 | ||
99 | 94 | ||
100 | //store current state to file | 95 | //store current state to file |
101 | //Simple implementation. Should run on a timer. | 96 | //Simple implementation. Should run on a timer. |
102 | 97 | ||
103 | static void saveState() | 98 | static void saveState() |
104 | { | 99 | { |
105 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); | 100 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); |
106 | if ( timerEventList.isEmpty() ) { | 101 | if ( timerEventList.isEmpty() ) { |
107 | unlink( savefilename ); | 102 | unlink( savefilename ); |
108 | return ; | 103 | return ; |
109 | } | 104 | } |
110 | 105 | ||
111 | QFile savefile(savefilename + ".new"); | 106 | QFile savefile(savefilename + ".new"); |
112 | if ( savefile.open(IO_WriteOnly) ) { | 107 | if ( savefile.open(IO_WriteOnly) ) { |
113 | QDataStream ds( &savefile ); | 108 | QDataStream ds( &savefile ); |
114 | 109 | ||
115 | //save | 110 | //save |
116 | 111 | ||
117 | QListIterator<timerEventItem> it( timerEventList ); | 112 | QListIterator<timerEventItem> it( timerEventList ); |
118 | for ( ; *it; ++it ) { | 113 | for ( ; *it; ++it ) { |
119 | ds << it.current()->UTCtime; | 114 | ds << it.current()->UTCtime; |
120 | ds << it.current()->channel; | 115 | ds << it.current()->channel; |
121 | ds << it.current()->message; | 116 | ds << it.current()->message; |
122 | ds << it.current()->data; | 117 | ds << it.current()->data; |
123 | } | 118 | } |
124 | 119 | ||
125 | 120 | ||
126 | savefile.close(); | 121 | savefile.close(); |
127 | unlink( savefilename ); | 122 | unlink( savefilename ); |
128 | QDir d; | 123 | QDir d; |
129 | d.rename(savefilename + ".new", savefilename); | 124 | d.rename(savefilename + ".new", savefilename); |
130 | 125 | ||
131 | } | 126 | } |
132 | } | 127 | } |
133 | 128 | ||
134 | /*! | 129 | /*! |
135 | Sets up the alarm server. Restoring to previous state (session management). | 130 | Sets up the alarm server. Restoring to previous state (session management). |
136 | */ | 131 | */ |
137 | void AlarmServer::initialize() | 132 | void AlarmServer::initialize() |
138 | { | 133 | { |
139 | //read autosave file and put events in timerEventList | 134 | //read autosave file and put events in timerEventList |
140 | 135 | ||
141 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); | 136 | QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); |
142 | 137 | ||
143 | QFile savefile(savefilename); | 138 | QFile savefile(savefilename); |
144 | if ( savefile.open(IO_ReadOnly) ) { | 139 | if ( savefile.open(IO_ReadOnly) ) { |
145 | QDataStream ds( &savefile ); | 140 | QDataStream ds( &savefile ); |
146 | while ( !ds.atEnd() ) { | 141 | while ( !ds.atEnd() ) { |
147 | timerEventItem *newTimerEventItem = new timerEventItem; | 142 | timerEventItem *newTimerEventItem = new timerEventItem; |
148 | ds >> newTimerEventItem->UTCtime; | 143 | ds >> newTimerEventItem->UTCtime; |
149 | ds >> newTimerEventItem->channel; | 144 | ds >> newTimerEventItem->channel; |
150 | ds >> newTimerEventItem->message; | 145 | ds >> newTimerEventItem->message; |
151 | ds >> newTimerEventItem->data; | 146 | ds >> newTimerEventItem->data; |
152 | timerEventList.append( newTimerEventItem ); | 147 | timerEventList.append( newTimerEventItem ); |
153 | } | 148 | } |
154 | savefile.close(); | 149 | savefile.close(); |
155 | if (!timerEventReceiver) | 150 | if (!timerEventReceiver) |
156 | timerEventReceiver = new TimerReceiverObject; | 151 | timerEventReceiver = new TimerReceiverObject; |
157 | setNearestTimerEvent(); | 152 | setNearestTimerEvent(); |
158 | } | 153 | } |
159 | } | 154 | } |
160 | 155 | ||
161 | 156 | ||
162 | #ifdef USE_ATD | 157 | #ifdef USE_ATD |
163 | 158 | ||
164 | static const char* atdir = "/var/spool/at/"; | 159 | static const char* atdir = "/var/spool/at/"; |
165 | 160 | ||
166 | static bool triggerAtd( bool writeHWClock = FALSE ) | 161 | static bool triggerAtd( bool writeHWClock = FALSE ) |
167 | { | 162 | { |
168 | QFile trigger(QString(atdir) + "trigger"); | 163 | QFile trigger(QString(atdir) + "trigger"); |
169 | if ( trigger.open(IO_WriteOnly | IO_Raw) ) { | 164 | if ( trigger.open(IO_WriteOnly | IO_Raw) ) { |
170 | if ( trigger.writeBlock("\n", 2) != 2 ) { | 165 | if ( trigger.writeBlock("\n", 2) != 2 ) { |
171 | QMessageBox::critical( 0, QObject::tr( "Out of Space" ), | 166 | QMessageBox::critical( 0, QObject::tr( "Out of Space" ), |
172 | QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); | 167 | QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); |
173 | trigger.close(); | 168 | trigger.close(); |
174 | QFile::remove | 169 | QFile::remove |
175 | ( trigger.name() ); | 170 | ( trigger.name() ); |
176 | return FALSE; | 171 | return FALSE; |
177 | } | 172 | } |
178 | return TRUE; | 173 | return TRUE; |
179 | } | 174 | } |
180 | return FALSE; | 175 | return FALSE; |
181 | } | 176 | } |
182 | 177 | ||
183 | #else | 178 | #else |
184 | 179 | ||
185 | static bool writeResumeAt ( time_t wakeup ) | 180 | static bool writeResumeAt ( time_t wakeup ) |
186 | { | 181 | { |
187 | FILE *fp = ::fopen ( "/var/run/resumeat", "w" ); | 182 | FILE *fp = ::fopen ( "/var/run/resumeat", "w" ); |
188 | 183 | ||
189 | if ( fp ) { | 184 | if ( fp ) { |
190 | ::fprintf ( fp, "%d\n", (int) wakeup ); | 185 | ::fprintf ( fp, "%d\n", (int) wakeup ); |
191 | ::fclose ( fp ); | 186 | ::fclose ( fp ); |
192 | } | 187 | } |
193 | else | 188 | else |
194 | qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); | 189 | qWarning ( "Failed to write wakeup time to /var/run/resumeat" ); |
195 | 190 | ||
196 | return ( fp ); | 191 | return ( fp ); |
197 | } | 192 | } |
198 | 193 | ||
199 | #endif | 194 | #endif |
200 | 195 | ||
201 | void TimerReceiverObject::deleteTimer() | 196 | void TimerReceiverObject::deleteTimer() |
202 | { | 197 | { |
203 | #ifdef USE_ATD | 198 | #ifdef USE_ATD |
204 | if ( !atfilename.isEmpty() ) { | 199 | if ( !atfilename.isEmpty() ) { |
205 | unlink( atfilename ); | 200 | unlink( atfilename ); |
206 | atfilename = QString::null; | 201 | atfilename = QString::null; |
207 | triggerAtd( FALSE ); | 202 | triggerAtd( FALSE ); |
208 | } | 203 | } |
209 | #else | 204 | #else |
210 | writeResumeAt ( 0 ); | 205 | writeResumeAt ( 0 ); |
211 | #endif | 206 | #endif |
212 | } | 207 | } |
213 | 208 | ||
214 | void TimerReceiverObject::resetTimer() | 209 | void TimerReceiverObject::resetTimer() |
215 | { | 210 | { |
216 | const int maxsecs = 2147000; | 211 | const int maxsecs = 2147000; |
217 | QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); | 212 | QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); |
218 | QDateTime now = QDateTime::currentDateTime(); | 213 | QDateTime now = QDateTime::currentDateTime(); |
219 | if ( nearest < now ) | 214 | if ( nearest < now ) |
220 | nearest = now; | 215 | nearest = now; |
221 | int secs = TimeConversion::secsTo( now, nearest ); | 216 | int secs = TimeConversion::secsTo( now, nearest ); |
222 | if ( secs > maxsecs ) { | 217 | if ( secs > maxsecs ) { |
223 | // too far for millisecond timing | 218 | // too far for millisecond timing |
224 | secs = maxsecs; | 219 | secs = maxsecs; |
225 | } | 220 | } |
226 | 221 | ||
227 | // System timer (needed so that we wake from deep sleep), | 222 | // System timer (needed so that we wake from deep sleep), |
228 | // from the Epoch in seconds. | 223 | // from the Epoch in seconds. |
229 | // | 224 | // |
230 | int at_secs = TimeConversion::toUTC(nearest); | 225 | int at_secs = TimeConversion::toUTC(nearest); |
231 | // qDebug("reset timer to %d seconds from Epoch",at_secs); | 226 | // qDebug("reset timer to %d seconds from Epoch",at_secs); |
232 | 227 | ||
233 | #ifdef USE_ATD | 228 | #ifdef USE_ATD |
234 | 229 | ||
235 | QString fn = atdir + QString::number(at_secs) + "." | 230 | QString fn = atdir + QString::number(at_secs) + "." |
236 | + QString::number(getpid()); | 231 | + QString::number(getpid()); |
237 | if ( fn != atfilename ) { | 232 | if ( fn != atfilename ) { |
238 | QFile atfile(fn + ".new"); | 233 | QFile atfile(fn + ".new"); |
239 | if ( atfile.open(IO_WriteOnly | IO_Raw) ) { | 234 | if ( atfile.open(IO_WriteOnly | IO_Raw) ) { |
240 | int total_written; | 235 | int total_written; |
241 | 236 | ||
242 | // just wake up and delete the at file | 237 | // just wake up and delete the at file |
243 | QString cmd = "#!/bin/sh\nrm " + fn; | 238 | QString cmd = "#!/bin/sh\nrm " + fn; |
244 | total_written = atfile.writeBlock(cmd.latin1(), cmd.length()); | 239 | total_written = atfile.writeBlock(cmd.latin1(), cmd.length()); |
245 | if ( total_written != int(cmd.length()) ) { | 240 | if ( total_written != int(cmd.length()) ) { |
246 | QMessageBox::critical( 0, tr("Out of Space"), | 241 | QMessageBox::critical( 0, tr("Out of Space"), |
247 | tr("Unable to schedule alarm.\n" | 242 | tr("Unable to schedule alarm.\n" |
248 | "Please free up space and try again") ); | 243 | "Please free up space and try again") ); |
249 | atfile.close(); | 244 | atfile.close(); |
250 | QFile::remove | 245 | QFile::remove |
251 | ( atfile.name() ); | 246 | ( atfile.name() ); |
252 | return ; | 247 | return ; |
253 | } | 248 | } |
254 | atfile.close(); | 249 | atfile.close(); |
255 | unlink( atfilename ); | 250 | unlink( atfilename ); |
256 | QDir d; | 251 | QDir d; |
257 | d.rename(fn + ".new", fn); | 252 | d.rename(fn + ".new", fn); |
258 | chmod(fn.latin1(), 0755); | 253 | chmod(fn.latin1(), 0755); |
259 | atfilename = fn; | 254 | atfilename = fn; |
260 | triggerAtd( FALSE ); | 255 | triggerAtd( FALSE ); |
261 | } | 256 | } |
262 | else { | 257 | else { |
263 | qWarning("Cannot open atd file %s", fn.latin1()); | 258 | qWarning("Cannot open atd file %s", fn.latin1()); |
264 | } | 259 | } |
265 | } | 260 | } |
266 | #else | 261 | #else |
267 | writeResumeAt ( at_secs ); | 262 | writeResumeAt ( at_secs ); |
268 | 263 | ||
269 | #endif | 264 | #endif |
270 | 265 | ||
271 | // Qt timers (does the actual alarm) | 266 | // Qt timers (does the actual alarm) |
272 | // from now in milliseconds | 267 | // from now in milliseconds |
273 | // | 268 | // |
274 | qDebug("AlarmServer waiting %d seconds", secs); | 269 | qDebug("AlarmServer waiting %d seconds", secs); |
275 | startTimer( 1000 * secs + 500 ); | 270 | startTimer( 1000 * secs + 500 ); |
276 | } | 271 | } |
277 | 272 | ||
278 | void TimerReceiverObject::timerEvent( QTimerEvent * ) | 273 | void TimerReceiverObject::timerEvent( QTimerEvent * ) |
279 | { | 274 | { |
280 | bool needSave = FALSE; | 275 | bool needSave = FALSE; |
281 | killTimers(); | 276 | killTimers(); |
282 | if (nearestTimerEvent) { | 277 | if (nearestTimerEvent) { |
283 | if ( nearestTimerEvent->UTCtime | 278 | if ( nearestTimerEvent->UTCtime |
284 | <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { | 279 | <= TimeConversion::toUTC(QDateTime::currentDateTime()) ) { |
285 | #ifndef QT_NO_COP | 280 | #ifndef QT_NO_COP |