kalarm/lib

synchtimer.h

00001 /*
00002  *  synchtimer.h  -  timers which synchronise to time boundaries
00003  *  Program:  kalarm
00004  *  Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #ifndef SYNCHTIMER_H
00022 #define SYNCHTIMER_H
00023 
00024 /* @file synchtimer.h - timers which synchronise to time boundaries */
00025 
00026 #include <tqobject.h>
00027 #include <tqvaluelist.h>
00028 #include <tqcstring.h>
00029 #include <tqdatetime.h>
00030 class TQTimer;
00031 
00037 class SynchTimer : public TQObject
00038 {
00039         Q_OBJECT
00040   
00041     public:
00042         virtual ~SynchTimer();
00043 
00044         struct Connection
00045         {
00046             Connection() { }
00047             Connection(TQObject* r, const char* s) : receiver(r), slot(s) { }
00048             bool operator==(const Connection& c) const  { return receiver == c.receiver && slot == c.slot; }
00049             TQObject*       receiver;
00050             const TQCString slot;
00051         };
00052     protected:
00053         SynchTimer();
00054         virtual void        start() = 0;
00055         void                connecT(TQObject* receiver, const char* member);
00056         void                disconnecT(TQObject* receiver, const char* member = 0);
00057         bool                hasConnections() const   { return !mConnections.isEmpty(); }
00058 
00059         TQTimer*             mTimer;
00060 
00061     protected slots:
00062         virtual void        slotTimer() = 0;
00063 
00064     private slots:
00065         void                slotReceiverGone(TQObject* r)  { disconnecT(r); }
00066 
00067     private:
00068         SynchTimer(const SynchTimer&);   // prohibit copying
00069         TQValueList<Connection> mConnections;  // list of current clients
00070 };
00071 
00072 
00077 class MinuteTimer : public SynchTimer
00078 {
00079         Q_OBJECT
00080   
00081     public:
00082         virtual ~MinuteTimer()  { mInstance = 0; }
00087         static void connect(TQObject* receiver, const char* member)
00088                            { instance()->connecT(receiver, member); }
00094         static void disconnect(TQObject* receiver, const char* member = 0)
00095                            { if (mInstance) mInstance->disconnecT(receiver, member); }
00096 
00097     protected:
00098         MinuteTimer() : SynchTimer() { }
00099         static MinuteTimer* instance();
00100         virtual void        start()    { slotTimer(); }
00101 
00102     protected slots:
00103         virtual void slotTimer();
00104 
00105     private:
00106         static MinuteTimer* mInstance;     // the one and only instance
00107 };
00108 
00109 
00117 class DailyTimer : public SynchTimer
00118 {
00119         Q_OBJECT
00120   
00121     public:
00122         virtual ~DailyTimer();
00129         static void connect(const TQTime& timeOfDay, TQObject* receiver, const char* member)
00130                            { fixedInstance(timeOfDay)->connecT(receiver, member); }
00138         static void disconnect(const TQTime& timeOfDay, TQObject* receiver, const char* member = 0);
00145         void changeTime(const TQTime& newTimeOfDay, bool triggerMissed = true);
00147         TQTime timeOfDay() const  { return mTime; }
00148 
00149     protected:
00155         DailyTimer(const TQTime&, bool fixed);
00163         static DailyTimer* fixedInstance(const TQTime& timeOfDay, bool create = true);
00164         virtual void start();
00165 
00166     protected slots:
00167         virtual void slotTimer();
00168 
00169     private:
00170         static TQValueList<DailyTimer*>  mFixedTimers;   // list of timers whose trigger time is fixed
00171         TQTime  mTime;
00172         TQDate  mLastDate;  // the date on which the timer was last triggered
00173         bool   mFixed;     // the time at which the timer triggers cannot be changed
00174 };
00175 
00176 
00181 class MidnightTimer
00182 {
00183     public:
00188         static void connect(TQObject* receiver, const char* member)
00189                            { DailyTimer::connect(TQTime(0,0), receiver, member); }
00195         static void disconnect(TQObject* receiver, const char* member = 0)
00196                            { DailyTimer::disconnect(TQTime(0,0), receiver, member); }
00197 
00198 };
00199 
00200 #endif // SYNCHTIMER_H
00201