1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* synchtimer.cpp - timers which synchronise to time boundaries
* Program: kalarm
* Copyright (C) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kalarm.h"
#include <tqtimer.h>
#include <kdebug.h>
#include "synchtimer.moc"
/*=============================================================================
= Class: SynchTimer
= Virtual base class for application-wide timer synchronised to a time boundary.
=============================================================================*/
SynchTimer::SynchTimer()
{
mTimer = new TQTimer(this, "mTimer");
}
SynchTimer::~SynchTimer()
{
delete mTimer;
mTimer = 0;
}
/******************************************************************************
* Connect to the timer. The timer is started if necessary.
*/
void SynchTimer::connecT(TQObject* receiver, const char* member)
{
Connection connection(receiver, member);
if (mConnections.tqfind(connection) != mConnections.end())
return; // the slot is already connected, so ignore request
connect(mTimer, TQT_SIGNAL(timeout()), receiver, member);
mConnections.append(connection);
if (!mTimer->isActive())
{
connect(mTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimer()));
start();
}
}
/******************************************************************************
* Disconnect from the timer. The timer is stopped if no longer needed.
*/
void SynchTimer::disconnecT(TQObject* receiver, const char* member)
{
if (mTimer)
{
mTimer->disconnect(receiver, member);
if (member)
mConnections.remove(Connection(receiver, member));
else
{
for (TQValueList<Connection>::Iterator it = mConnections.begin(); it != mConnections.end(); )
{
if ((*it).receiver == receiver)
it = mConnections.remove(it);
else
++it;
}
}
if (mConnections.isEmpty())
{
mTimer->disconnect();
mTimer->stop();
}
}
}
/*=============================================================================
= Class: MinuteTimer
= Application-wide timer synchronised to the minute boundary.
=============================================================================*/
MinuteTimer* MinuteTimer::mInstance = 0;
MinuteTimer* MinuteTimer::instance()
{
if (!mInstance)
mInstance = new MinuteTimer;
return mInstance;
}
/******************************************************************************
* Called when the timer triggers, or to start the timer.
* Timers can under some circumstances wander off from the correct trigger time,
* so rather than setting a 1 minute interval, calculate the correct next
* interval each time it triggers.
*/
void MinuteTimer::slotTimer()
{
kdDebug(5950) << "MinuteTimer::slotTimer()" << endl;
int interval = 62 - TQTime::currentTime().second();
mTimer->start(interval * 1000, true); // execute a single shot
}
/*=============================================================================
= Class: DailyTimer
= Application-wide timer synchronised to midnight.
=============================================================================*/
TQValueList<DailyTimer*> DailyTimer::mFixedTimers;
DailyTimer::DailyTimer(const TQTime& timeOfDay, bool fixed)
: mTime(timeOfDay),
mFixed(fixed)
{
if (fixed)
mFixedTimers.append(this);
}
DailyTimer::~DailyTimer()
{
if (mFixed)
mFixedTimers.remove(this);
}
DailyTimer* DailyTimer::fixedInstance(const TQTime& timeOfDay, bool create)
{
for (TQValueList<DailyTimer*>::Iterator it = mFixedTimers.begin(); it != mFixedTimers.end(); ++it)
if ((*it)->mTime == timeOfDay)
return *it;
return create ? new DailyTimer(timeOfDay, true) : 0;
}
/******************************************************************************
* Disconnect from the timer signal which triggers at the given fixed time of day.
* If there are no remaining connections to that timer, it is destroyed.
*/
void DailyTimer::disconnect(const TQTime& timeOfDay, TQObject* receiver, const char* member)
{
DailyTimer* timer = fixedInstance(timeOfDay, false);
if (!timer)
return;
timer->disconnecT(receiver, member);
if (!timer->hasConnections())
delete timer;
}
/******************************************************************************
* Change the time at which the variable timer triggers.
*/
void DailyTimer::changeTime(const TQTime& newTimeOfDay, bool triggerMissed)
{
if (mFixed)
return;
if (mTimer->isActive())
{
mTimer->stop();
bool triggerNow = false;
if (triggerMissed)
{
TQTime now = TQTime::currentTime();
if (now >= newTimeOfDay && now < mTime)
{
// The trigger time is now earlier and it has already arrived today.
// Trigger a timer event immediately.
triggerNow = true;
}
}
mTime = newTimeOfDay;
if (triggerNow)
mTimer->start(0, true); // trigger immediately
else
start();
}
else
mTime = newTimeOfDay;
}
/******************************************************************************
* Initialise the timer to trigger at the specified time.
* This will either be today or tomorrow, depending on whether the trigger time
* has already passed.
*/
void DailyTimer::start()
{
// TIMEZONE = local time
TQDateTime now = TQDateTime::tqcurrentDateTime();
// Find out whether to trigger today or tomorrow.
// In preference, use the last trigger date to determine this, since
// that will avoid possible errors due to daylight savings time changes.
bool today;
if (mLastDate.isValid())
today = (mLastDate < now.date());
else
today = (now.time() < mTime);
TQDateTime next;
if (today)
next = TQDateTime(now.date(), mTime);
else
next = TQDateTime(now.date().addDays(1), mTime);
uint interval = next.toTime_t() - now.toTime_t();
mTimer->start(interval * 1000, true); // execute a single shot
kdDebug(5950) << "DailyTimer::start(at " << mTime.hour() << ":" << mTime.minute() << "): interval = " << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60 << endl;
}
/******************************************************************************
* Called when the timer triggers.
* Set the timer to trigger again tomorrow at the specified time.
* Note that if daylight savings time changes occur, this will not be 24 hours
* from now.
*/
void DailyTimer::slotTimer()
{
// TIMEZONE = local time
TQDateTime now = TQDateTime::tqcurrentDateTime();
mLastDate = now.date();
TQDateTime next = TQDateTime(mLastDate.addDays(1), mTime);
uint interval = next.toTime_t() - now.toTime_t();
mTimer->start(interval * 1000, true); // execute a single shot
kdDebug(5950) << "DailyTimer::slotTimer(at " << mTime.hour() << ":" << mTime.minute() << "): interval = " << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60 << endl;
}
|