kalarm/lib

synchtimer.h
1/*
2 * synchtimer.h - timers which synchronise to time boundaries
3 * Program: kalarm
4 * Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SYNCHTIMER_H
22#define SYNCHTIMER_H
23
24/* @file synchtimer.h - timers which synchronise to time boundaries */
25
26#include <tqobject.h>
27#include <tqvaluelist.h>
28#include <tqcstring.h>
29#include <tqdatetime.h>
30class TQTimer;
31
37class SynchTimer : public TQObject
38{
39 TQ_OBJECT
40
41 public:
42 virtual ~SynchTimer();
43
44 struct Connection
45 {
46 Connection() { }
47 Connection(TQObject* r, const char* s) : receiver(r), slot(s) { }
48 bool operator==(const Connection& c) const { return receiver == c.receiver && slot == c.slot; }
49 TQObject* receiver;
50 const TQCString slot;
51 };
52 protected:
53 SynchTimer();
54 virtual void start() = 0;
55 void connecT(TQObject* receiver, const char* member);
56 void disconnecT(TQObject* receiver, const char* member = 0);
57 bool hasConnections() const { return !mConnections.isEmpty(); }
58
59 TQTimer* mTimer;
60
61 protected slots:
62 virtual void slotTimer() = 0;
63
64 private slots:
65 void slotReceiverGone(TQObject* r) { disconnecT(r); }
66
67 private:
68 SynchTimer(const SynchTimer&); // prohibit copying
69 TQValueList<Connection> mConnections; // list of current clients
70};
71
72
77class MinuteTimer : public SynchTimer
78{
79 TQ_OBJECT
80
81 public:
82 virtual ~MinuteTimer() { mInstance = 0; }
87 static void connect(TQObject* receiver, const char* member)
88 { instance()->connecT(receiver, member); }
94 static void disconnect(TQObject* receiver, const char* member = 0)
95 { if (mInstance) mInstance->disconnecT(receiver, member); }
96
97 protected:
98 MinuteTimer() : SynchTimer() { }
99 static MinuteTimer* instance();
100 virtual void start() { slotTimer(); }
101
102 protected slots:
103 virtual void slotTimer();
104
105 private:
106 static MinuteTimer* mInstance; // the one and only instance
107};
108
109
117class DailyTimer : public SynchTimer
118{
119 TQ_OBJECT
120
121 public:
122 virtual ~DailyTimer();
129 static void connect(const TQTime& timeOfDay, TQObject* receiver, const char* member)
130 { fixedInstance(timeOfDay)->connecT(receiver, member); }
138 static void disconnect(const TQTime& timeOfDay, TQObject* receiver, const char* member = 0);
145 void changeTime(const TQTime& newTimeOfDay, bool triggerMissed = true);
147 TQTime timeOfDay() const { return mTime; }
148
149 protected:
155 DailyTimer(const TQTime&, bool fixed);
163 static DailyTimer* fixedInstance(const TQTime& timeOfDay, bool create = true);
164 virtual void start();
165
166 protected slots:
167 virtual void slotTimer();
168
169 private:
170 static TQValueList<DailyTimer*> mFixedTimers; // list of timers whose trigger time is fixed
171 TQTime mTime;
172 TQDate mLastDate; // the date on which the timer was last triggered
173 bool mFixed; // the time at which the timer triggers cannot be changed
174};
175
176
182{
183 public:
188 static void connect(TQObject* receiver, const char* member)
189 { DailyTimer::connect(TQTime(0,0), receiver, member); }
195 static void disconnect(TQObject* receiver, const char* member = 0)
196 { DailyTimer::disconnect(TQTime(0,0), receiver, member); }
197
198};
199
200#endif // SYNCHTIMER_H
201
DailyTimer is an application-wide timer synchronised to a specified time of day, local time.
Definition: synchtimer.h:118
TQTime timeOfDay() const
Return the current time of day at which this variable timer triggers.
Definition: synchtimer.h:147
static void disconnect(const TQTime &timeOfDay, TQObject *receiver, const char *member=0)
Disconnect from the timer signal which triggers at the given fixed time of day.
Definition: synchtimer.cpp:150
static DailyTimer * fixedInstance(const TQTime &timeOfDay, bool create=true)
Return the instance which triggers at the specified fixed time of day, optionally creating a new inst...
Definition: synchtimer.cpp:138
static void connect(const TQTime &timeOfDay, TQObject *receiver, const char *member)
Connect to the timer signal which triggers at the given fixed time of day.
Definition: synchtimer.h:129
DailyTimer(const TQTime &, bool fixed)
Construct an instance.
Definition: synchtimer.cpp:124
void changeTime(const TQTime &newTimeOfDay, bool triggerMissed=true)
Change the time at which this variable timer triggers.
Definition: synchtimer.cpp:163
MidnightTimer is an application-wide timer synchronised to midnight, local time.
Definition: synchtimer.h:182
static void connect(TQObject *receiver, const char *member)
Connect to the timer signal.
Definition: synchtimer.h:188
static void disconnect(TQObject *receiver, const char *member=0)
Disconnect from the timer signal.
Definition: synchtimer.h:195
MinuteTimer is an application-wide timer synchronised to the minute boundary.
Definition: synchtimer.h:78
static void disconnect(TQObject *receiver, const char *member=0)
Disconnect from the timer signal.
Definition: synchtimer.h:94
static void connect(TQObject *receiver, const char *member)
Connect to the timer signal.
Definition: synchtimer.h:87
SynchTimer is a virtual base class for application-wide timers synchronised to a time boundary.
Definition: synchtimer.h:38