2 * synchtimer.h - timers which synchronize to time boundaries
4 * Copyright © 2004,2005 by David Jarvie <djarvie@kde.org>
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.
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.
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.
24 /* @file synchtimer.h - timers which synchronize to time boundaries */
32 /** SynchTimer is a virtual base class for application-wide timers synchronized
35 * @author David Jarvie <djarvie@kde.org>
37 class SynchTimer
: public QObject
41 virtual ~SynchTimer();
46 Connection(QObject
* r
, const char* s
) : receiver(r
), slot(s
) { }
47 bool operator==(const Connection
& c
) const { return receiver
== c
.receiver
&& slot
== c
.slot
; }
53 virtual void start() = 0;
54 void connecT(QObject
* receiver
, const char* member
);
55 void disconnecT(QObject
* receiver
, const char* member
= Q_NULLPTR
);
56 bool hasConnections() const { return !mConnections
.isEmpty(); }
61 virtual void slotTimer() = 0;
64 void slotReceiverGone(QObject
* r
) { disconnecT(r
); }
67 SynchTimer(const SynchTimer
&); // prohibit copying
68 QList
<Connection
> mConnections
; // list of current clients
72 /** MinuteTimer is an application-wide timer synchronized to the minute boundary.
74 * @author David Jarvie <djarvie@kde.org>
76 class MinuteTimer
: public SynchTimer
80 virtual ~MinuteTimer() { mInstance
= Q_NULLPTR
; }
81 /** Connect to the timer signal.
82 * @param receiver Receiving object.
83 * @param member Slot to activate.
85 static void connect(QObject
* receiver
, const char* member
)
86 { instance()->connecT(receiver
, member
); }
87 /** Disconnect from the timer signal.
88 * @param receiver Receiving object.
89 * @param member Slot to disconnect. If null, all slots belonging to
90 * @p receiver will be disconnected.
92 static void disconnect(QObject
* receiver
, const char* member
= Q_NULLPTR
)
93 { if (mInstance
) mInstance
->disconnecT(receiver
, member
); }
96 MinuteTimer() : SynchTimer() { }
97 static MinuteTimer
* instance();
98 void start() Q_DECL_OVERRIDE
{ slotTimer(); }
101 void slotTimer() Q_DECL_OVERRIDE
;
104 static MinuteTimer
* mInstance
; // the one and only instance
108 /** DailyTimer is an application-wide timer synchronized to a specified time of day, local time.
110 * Daily timers come in two flavors: fixed, which can only be accessed through static methods,
111 * and variable, whose time can be adjusted and which are accessed through non-static methods.
113 * @author David Jarvie <djarvie@kde.org>
115 class DailyTimer
: public SynchTimer
119 virtual ~DailyTimer();
120 /** Connect to the timer signal which triggers at the given fixed time of day.
121 * A new timer is created if necessary.
122 * @param timeOfDay Time at which the timer is to trigger.
123 * @param receiver Receiving object.
124 * @param member Slot to activate.
126 static void connect(const QTime
& timeOfDay
, QObject
* receiver
, const char* member
)
127 { fixedInstance(timeOfDay
)->connecT(receiver
, member
); }
128 /** Disconnect from the timer signal which triggers at the given fixed time of day.
129 * If there are no remaining connections to that timer, it is destroyed.
130 * @param timeOfDay Time at which the timer triggers.
131 * @param receiver Receiving object.
132 * @param member Slot to disconnect. If null, all slots belonging to
133 * @p receiver will be disconnected.
135 static void disconnect(const QTime
& timeOfDay
, QObject
* receiver
, const char* member
= Q_NULLPTR
);
136 /** Change the time at which this variable timer triggers.
137 * @param newTimeOfDay New time at which the timer should trigger.
138 * @param triggerMissed If true, and if @p newTimeOfDay < @p oldTimeOfDay, and if the current
139 * time is between @p newTimeOfDay and @p oldTimeOfDay, the timer will be
140 * triggered immediately so as to avoid missing today's trigger.
142 void changeTime(const QTime
& newTimeOfDay
, bool triggerMissed
= true);
143 /** Return the current time of day at which this variable timer triggers. */
144 QTime
timeOfDay() const { return mTime
; }
147 /** Construct an instance.
148 * The constructor is protected to ensure that for variable timers, only derived classes
149 * can construct instances. This ensures that multiple timers are not created for the same
152 DailyTimer(const QTime
&, bool fixed
);
153 /** Return the instance which triggers at the specified fixed time of day,
154 * optionally creating a new instance if necessary.
155 * @param timeOfDay Time at which the timer triggers.
156 * @param create If true, create a new instance if none already exists
158 * @return The instance for @p timeOfDay, or 0 if it does not exist.
160 static DailyTimer
* fixedInstance(const QTime
& timeOfDay
, bool create
= true);
161 void start() Q_DECL_OVERRIDE
;
164 void slotTimer() Q_DECL_OVERRIDE
;
167 static QList
<DailyTimer
*> mFixedTimers
; // list of timers whose trigger time is fixed
169 QDate mLastDate
; // the date on which the timer was last triggered
170 bool mFixed
; // the time at which the timer triggers cannot be changed
174 /** MidnightTimer is an application-wide timer synchronized to midnight, local time.
176 * @author David Jarvie <djarvie@kde.org>
181 /** Connect to the timer signal.
182 * @param receiver Receiving object.
183 * @param member Slot to activate.
185 static void connect(QObject
* receiver
, const char* member
)
186 { DailyTimer::connect(QTime(0,0), receiver
, member
); }
187 /** Disconnect from the timer signal.
188 * @param receiver Receiving object.
189 * @param member Slot to disconnect. If null, all slots belonging to
190 * @p receiver will be disconnected.
192 static void disconnect(QObject
* receiver
, const char* member
= Q_NULLPTR
)
193 { DailyTimer::disconnect(QTime(0,0), receiver
, member
); }
197 #endif // SYNCHTIMER_H