summaryrefslogtreecommitdiff
path: root/library/alarmserver.cpp
Side-by-side diff
Diffstat (limited to 'library/alarmserver.cpp') (more/less context) (show whitespace changes)
-rw-r--r--library/alarmserver.cpp9
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,224 +1,223 @@
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
-#include <qdir.h>
-
+#include "alarmserver.h"
+#include <qpe/global.h>
#include <qpe/qpeapplication.h>
-
#include <qpe/qcopenvelope_qws.h>
-#include "alarmserver.h"
#include <qpe/timeconversion.h>
+#include <qdir.h>
+
#include <sys/types.h>
#include <sys/stat.h>
-
#include <stdlib.h>
#include <unistd.h>
#undef USE_ATD // not used anymore -- we run opie-alarm on suspend/resume
struct timerEventItem
{
time_t UTCtime;
QCString channel, message;
int data;
bool operator==( const timerEventItem &right ) const
{
return ( UTCtime == right.UTCtime
&& channel == right.channel
&& message == right.message
&& data == right.data );
}
};
class TimerReceiverObject : public QObject
{
public:
TimerReceiverObject()
{ }
~TimerReceiverObject()
{ }
void resetTimer();
void setTimerEventItem();
void deleteTimer();
protected:
void timerEvent( QTimerEvent *te );
#ifdef USE_ATD
private:
QString atfilename;
#endif
};
TimerReceiverObject *timerEventReceiver = NULL;
QList<timerEventItem> timerEventList;
timerEventItem *nearestTimerEvent = NULL;
// set the timer to go off on the next event in the list
void setNearestTimerEvent()
{
nearestTimerEvent = NULL;
QListIterator<timerEventItem> it( timerEventList );
if ( *it )
nearestTimerEvent = *it;
for ( ; *it; ++it )
if ( (*it)->UTCtime < nearestTimerEvent->UTCtime )
nearestTimerEvent = *it;
if (nearestTimerEvent)
timerEventReceiver->resetTimer();
else
timerEventReceiver->deleteTimer();
}
//store current state to file
//Simple implementation. Should run on a timer.
static void saveState()
{
QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" );
if ( timerEventList.isEmpty() ) {
unlink( savefilename );
return ;
}
QFile savefile(savefilename + ".new");
if ( savefile.open(IO_WriteOnly) ) {
QDataStream ds( &savefile );
//save
QListIterator<timerEventItem> it( timerEventList );
for ( ; *it; ++it ) {
ds << it.current()->UTCtime;
ds << it.current()->channel;
ds << it.current()->message;
ds << it.current()->data;
}
savefile.close();
unlink( savefilename );
QDir d;
d.rename(savefilename + ".new", savefilename);
}
}
/*!
Sets up the alarm server. Restoring to previous state (session management).
*/
void AlarmServer::initialize()
{
//read autosave file and put events in timerEventList
QString savefilename = Global::applicationFileName( "AlarmServer", "saveFile" );
QFile savefile(savefilename);
if ( savefile.open(IO_ReadOnly) ) {
QDataStream ds( &savefile );
while ( !ds.atEnd() ) {
timerEventItem *newTimerEventItem = new timerEventItem;
ds >> newTimerEventItem->UTCtime;
ds >> newTimerEventItem->channel;
ds >> newTimerEventItem->message;
ds >> newTimerEventItem->data;
timerEventList.append( newTimerEventItem );
}
savefile.close();
if (!timerEventReceiver)
timerEventReceiver = new TimerReceiverObject;
setNearestTimerEvent();
}
}
#ifdef USE_ATD
static const char* atdir = "/var/spool/at/";
static bool triggerAtd( bool writeHWClock = FALSE )
{
QFile trigger(QString(atdir) + "trigger");
if ( trigger.open(IO_WriteOnly | IO_Raw) ) {
if ( trigger.writeBlock("\n", 2) != 2 ) {
QMessageBox::critical( 0, QObject::tr( "Out of Space" ),
QObject::tr( "Unable to schedule alarm.\nFree some memory and try again." ) );
trigger.close();
QFile::remove
( trigger.name() );
return FALSE;
}
return TRUE;
}
return FALSE;
}
#else
static bool writeResumeAt ( time_t wakeup )
{
FILE *fp = ::fopen ( "/var/run/resumeat", "w" );
if ( fp ) {
::fprintf ( fp, "%d\n", (int) wakeup );
::fclose ( fp );
}
else
qWarning ( "Failed to write wakeup time to /var/run/resumeat" );
return ( fp );
}
#endif
void TimerReceiverObject::deleteTimer()
{
#ifdef USE_ATD
if ( !atfilename.isEmpty() ) {
unlink( atfilename );
atfilename = QString::null;
triggerAtd( FALSE );
}
#else
writeResumeAt ( 0 );
#endif
}
void TimerReceiverObject::resetTimer()
{
const int maxsecs = 2147000;
QDateTime nearest = TimeConversion::fromUTC(nearestTimerEvent->UTCtime);
QDateTime now = QDateTime::currentDateTime();
if ( nearest < now )
nearest = now;
int secs = TimeConversion::secsTo( now, nearest );
if ( secs > maxsecs ) {
// too far for millisecond timing
secs = maxsecs;
}
// System timer (needed so that we wake from deep sleep),
// from the Epoch in seconds.
//