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 QObject
00038 {
00039         Q_OBJECT
00040     public:
00041         virtual ~SynchTimer();
00042 
00043         struct Connection
00044         {
00045             Connection() { }
00046             Connection(TQObject* r, const char* s) : receiver(r), slot(s) { }
00047             bool operator==(const Connection& c) const  { return receiver == c.receiver && slot == c.slot; }
00048             TQObject*       receiver;
00049             const TQCString slot;
00050         };
00051     protected:
00052         SynchTimer();
00053         virtual void        start() = 0;
00054         void                connecT(TQObject* receiver, const char* member);
00055         void                disconnecT(TQObject* receiver, const char* member = 0);
00056         bool                hasConnections() const   { return !mConnections.isEmpty(); }
00057 
00058         TQTimer*             mTimer;
00059 
00060     protected slots:
00061         virtual void        slotTimer() = 0;
00062 
00063     private slots:
00064         void                slotReceiverGone(TQObject* r)  { disconnecT(r); }
00065 
00066     private:
00067         SynchTimer(const SynchTimer&);   // prohibit copying
00068         TQValueList<Connection> mConnections;  // list of current clients
00069 };
00070 
00071 
00076 class MinuteTimer : public SynchTimer
00077 {
00078         Q_OBJECT
00079     public:
00080         virtual ~MinuteTimer()  { mInstance = 0; }
00085         static void connect(TQObject* receiver, const char* member)
00086                            { instance()->connecT(receiver, member); }
00092         static void disconnect(TQObject* receiver, const char* member = 0)
00093                            { if (mInstance) mInstance->disconnecT(receiver, member); }
00094 
00095     protected:
00096         MinuteTimer() : SynchTimer() { }
00097         static MinuteTimer* instance();
00098         virtual void        start()    { slotTimer(); }
00099 
00100     protected slots:
00101         virtual void slotTimer();
00102 
00103     private:
00104         static MinuteTimer* mInstance;     // the one and only instance
00105 };
00106 
00107 
00115 class DailyTimer : public SynchTimer
00116 {
00117         Q_OBJECT
00118     public:
00119         virtual ~DailyTimer();
00126         static void connect(const TQTime& timeOfDay, TQObject* receiver, const char* member)
00127                            { fixedInstance(timeOfDay)->connecT(receiver, member); }
00135         static void disconnect(const TQTime& timeOfDay, TQObject* receiver, const char* member = 0);
00142         void changeTime(const TQTime& newTimeOfDay, bool triggerMissed = true);
00144         TQTime timeOfDay() const  { return mTime; }
00145 
00146     protected:
00152         DailyTimer(const TQTime&, bool fixed);
00160         static DailyTimer* fixedInstance(const TQTime& timeOfDay, bool create = true);
00161         virtual void start();
00162 
00163     protected slots:
00164         virtual void slotTimer();
00165 
00166     private:
00167         static TQValueList<DailyTimer*>  mFixedTimers;   // list of timers whose trigger time is fixed
00168         TQTime  mTime;
00169         TQDate  mLastDate;  // the date on which the timer was last triggered
00170         bool   mFixed;     // the time at which the timer triggers cannot be changed
00171 };
00172 
00173 
00178 class MidnightTimer
00179 {
00180     public:
00185         static void connect(TQObject* receiver, const char* member)
00186                            { DailyTimer::connect(TQTime(0,0), receiver, member); }
00192         static void disconnect(TQObject* receiver, const char* member = 0)
00193                            { DailyTimer::disconnect(TQTime(0,0), receiver, member); }
00194 
00195 };
00196 
00197 #endif // SYNCHTIMER_H
00198