summaryrefslogtreecommitdiff
path: root/library/alarmserver.cpp
Unidiff
Diffstat (limited to 'library/alarmserver.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/alarmserver.cpp11
1 files changed, 1 insertions, 10 deletions
diff --git a/library/alarmserver.cpp b/library/alarmserver.cpp
index 7e6e515..5e4dd18 100644
--- a/library/alarmserver.cpp
+++ b/library/alarmserver.cpp
@@ -95,138 +95,129 @@ static void saveState()
95 QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); 95 QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" );
96 if ( timerEventList.isEmpty() ) { 96 if ( timerEventList.isEmpty() ) {
97 unlink( savefilename ); 97 unlink( savefilename );
98 return; 98 return;
99 } 99 }
100 100
101 QFile savefile(savefilename+".new"); 101 QFile savefile(savefilename+".new");
102 if ( savefile.open(IO_WriteOnly) ) { 102 if ( savefile.open(IO_WriteOnly) ) {
103 QDataStream ds( &savefile ); 103 QDataStream ds( &savefile );
104 104
105 //save 105 //save
106 106
107 QListIterator<timerEventItem> it( timerEventList ); 107 QListIterator<timerEventItem> it( timerEventList );
108 for ( ; *it; ++it ) { 108 for ( ; *it; ++it ) {
109 ds << it.current()->UTCtime; 109 ds << it.current()->UTCtime;
110 ds << it.current()->channel; 110 ds << it.current()->channel;
111 ds << it.current()->message; 111 ds << it.current()->message;
112 ds << it.current()->data; 112 ds << it.current()->data;
113 } 113 }
114 114
115 115
116 savefile.close(); 116 savefile.close();
117 unlink( savefilename ); 117 unlink( savefilename );
118 QDir d; d.rename(savefilename+".new",savefilename); 118 QDir d; d.rename(savefilename+".new",savefilename);
119 119
120 } 120 }
121} 121}
122 122
123/*! 123/*!
124 Sets up the alarm server. Restoring to previous state (session management). 124 Sets up the alarm server. Restoring to previous state (session management).
125 */ 125 */
126void AlarmServer::initialize() 126void AlarmServer::initialize()
127{ 127{
128 //read autosave file and put events in timerEventList 128 //read autosave file and put events in timerEventList
129 129
130 QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" ); 130 QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" );
131 131
132 QFile savefile(savefilename); 132 QFile savefile(savefilename);
133 if ( savefile.open(IO_ReadOnly) ) { 133 if ( savefile.open(IO_ReadOnly) ) {
134 QDataStream ds( &savefile ); 134 QDataStream ds( &savefile );
135 while ( !ds.atEnd() ) { 135 while ( !ds.atEnd() ) {
136 timerEventItem *newTimerEventItem = new timerEventItem; 136 timerEventItem *newTimerEventItem = new timerEventItem;
137 ds >> newTimerEventItem->UTCtime; 137 ds >> newTimerEventItem->UTCtime;
138 ds >> newTimerEventItem->channel; 138 ds >> newTimerEventItem->channel;
139 ds >> newTimerEventItem->message; 139 ds >> newTimerEventItem->message;
140 ds >> newTimerEventItem->data; 140 ds >> newTimerEventItem->data;
141 timerEventList.append( newTimerEventItem ); 141 timerEventList.append( newTimerEventItem );
142 } 142 }
143 savefile.close(); 143 savefile.close();
144 if (!timerEventReceiver) 144 if (!timerEventReceiver)
145 timerEventReceiver = new TimerReceiverObject; 145 timerEventReceiver = new TimerReceiverObject;
146 setNearestTimerEvent(); 146 setNearestTimerEvent();
147 } 147 }
148} 148}
149 149
150 150
151 151
152 152
153static const char* atdir = "/var/spool/at/"; 153static const char* atdir = "/var/spool/at/";
154 154
155static bool triggerAtd( bool writeHWClock = FALSE ) 155static bool triggerAtd( bool writeHWClock = FALSE )
156{ 156{
157 QFile trigger(QString(atdir) + "trigger"); 157 QFile trigger(QString(atdir) + "trigger");
158 if ( trigger.open(IO_WriteOnly|IO_Raw) ) { 158 if ( trigger.open(IO_WriteOnly|IO_Raw) ) {
159 159 if ( trigger.writeBlock("\n",2) != 2 ) {
160 const char* data =
161#ifdef QT_QWS_SHARP
162 //custom atd only writes HW Clock if we write a 'W'
163 ( writeHWClock ) ? "W\n" :
164#endif
165 data = "\n";
166 int len = strlen(data);
167 int total_written = trigger.writeBlock(data,len);
168 if ( total_written != len ) {
169 QMessageBox::critical( 0, QObject::tr( "Out of Space" ), 160 QMessageBox::critical( 0, QObject::tr( "Out of Space" ),
170 QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) ); 161 QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) );
171 trigger.close(); 162 trigger.close();
172 QFile::remove( trigger.name() ); 163 QFile::remove( trigger.name() );
173 return FALSE; 164 return FALSE;
174 } 165 }
175 return TRUE; 166 return TRUE;
176 } 167 }
177 return FALSE; 168 return FALSE;
178} 169}
179 170
180void TimerReceiverObject::deleteTimer() 171void TimerReceiverObject::deleteTimer()
181{ 172{
182 if ( !atfilename.isEmpty() ) { 173 if ( !atfilename.isEmpty() ) {
183 unlink( atfilename ); 174 unlink( atfilename );
184 atfilename = QString::null; 175 atfilename = QString::null;
185 triggerAtd( FALSE ); 176 triggerAtd( FALSE );
186 } 177 }
187} 178}
188 179
189void TimerReceiverObject::resetTimer() 180void TimerReceiverObject::resetTimer()
190{ 181{
191 const int maxsecs = 2147000; 182 const int maxsecs = 2147000;
192 int total_written; 183 int total_written;
193 QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime); 184 QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime);
194 QDateTime now = QDateTime::currentDateTime(); 185 QDateTime now = QDateTime::currentDateTime();
195 if ( nearest < now ) 186 if ( nearest < now )
196 nearest = now; 187 nearest = now;
197 int secs = TimeConversion::secsTo( now, nearest ); 188 int secs = TimeConversion::secsTo( now, nearest );
198 if ( secs > maxsecs ) { 189 if ( secs > maxsecs ) {
199 // too far for millisecond timing 190 // too far for millisecond timing
200 secs = maxsecs; 191 secs = maxsecs;
201 } 192 }
202 193
203 // System timer (needed so that we wake from deep sleep), 194 // System timer (needed so that we wake from deep sleep),
204 // from the Epoch in seconds. 195 // from the Epoch in seconds.
205 // 196 //
206 int at_secs = TimeConversion::toUTC(nearest); 197 int at_secs = TimeConversion::toUTC(nearest);
207 // qDebug("reset timer to %d seconds from Epoch",at_secs); 198 // qDebug("reset timer to %d seconds from Epoch",at_secs);
208 QString fn = atdir + QString::number(at_secs) + "." 199 QString fn = atdir + QString::number(at_secs) + "."
209 + QString::number(getpid()); 200 + QString::number(getpid());
210 if ( fn != atfilename ) { 201 if ( fn != atfilename ) {
211 QFile atfile(fn+".new"); 202 QFile atfile(fn+".new");
212 if ( atfile.open(IO_WriteOnly|IO_Raw) ) { 203 if ( atfile.open(IO_WriteOnly|IO_Raw) ) {
213 // just wake up and delete the at file 204 // just wake up and delete the at file
214 QString cmd = "#!/bin/sh\nrm " + fn; 205 QString cmd = "#!/bin/sh\nrm " + fn;
215 total_written = atfile.writeBlock(cmd.latin1(),cmd.length()); 206 total_written = atfile.writeBlock(cmd.latin1(),cmd.length());
216 if ( total_written != int(cmd.length()) ) { 207 if ( total_written != int(cmd.length()) ) {
217 QMessageBox::critical( 0, tr("Out of Space"), 208 QMessageBox::critical( 0, tr("Out of Space"),
218 tr("Unable to schedule alarm.\n" 209 tr("Unable to schedule alarm.\n"
219 "Please free up space and try again") ); 210 "Please free up space and try again") );
220 atfile.close(); 211 atfile.close();
221 QFile::remove( atfile.name() ); 212 QFile::remove( atfile.name() );
222 return; 213 return;
223 } 214 }
224 atfile.close(); 215 atfile.close();
225 unlink( atfilename ); 216 unlink( atfilename );
226 QDir d; d.rename(fn+".new",fn); 217 QDir d; d.rename(fn+".new",fn);
227 chmod(fn.latin1(),0755); 218 chmod(fn.latin1(),0755);
228 atfilename = fn; 219 atfilename = fn;
229 triggerAtd( FALSE ); 220 triggerAtd( FALSE );
230 } else { 221 } else {
231 qWarning("Cannot open atd file %s",fn.latin1()); 222 qWarning("Cannot open atd file %s",fn.latin1());
232 } 223 }