32
32
SynchTimer::SynchTimer()
34
mTimer = new QTimer(this);
35
mTimer->setSingleShot(true);
34
mTimer = new QTimer(this);
35
mTimer->setSingleShot(true);
38
38
SynchTimer::~SynchTimer()
44
44
/******************************************************************************
47
47
void SynchTimer::connecT(QObject* receiver, const char* member)
49
Connection connection(receiver, member);
50
if (mConnections.contains(connection))
51
return; // the slot is already connected, so ignore request
52
connect(mTimer, SIGNAL(timeout()), receiver, member);
53
mConnections.append(connection);
54
if (!mTimer->isActive())
56
connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
49
Connection connection(receiver, member);
50
if (mConnections.contains(connection))
51
return; // the slot is already connected, so ignore request
52
connect(mTimer, SIGNAL(timeout()), receiver, member);
53
mConnections.append(connection);
54
if (!mTimer->isActive())
56
connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
61
61
/******************************************************************************
64
64
void SynchTimer::disconnecT(QObject* receiver, const char* member)
68
mTimer->disconnect(receiver, member);
71
int i = mConnections.indexOf(Connection(receiver, member));
73
mConnections.removeAt(i);
77
for (int i = 0; i < mConnections.count(); )
79
if (mConnections[i].receiver == receiver)
80
mConnections.removeAt(i);
85
if (mConnections.isEmpty())
68
mTimer->disconnect(receiver, member);
71
int i = mConnections.indexOf(Connection(receiver, member));
73
mConnections.removeAt(i);
77
for (int i = 0; i < mConnections.count(); )
79
if (mConnections[i].receiver == receiver)
80
mConnections.removeAt(i);
85
if (mConnections.isEmpty())
114
114
void MinuteTimer::slotTimer()
117
int interval = 62 - QTime::currentTime().second();
118
mTimer->start(interval * 1000); // execute a single shot
117
int interval = 62 - QTime::currentTime().second();
118
mTimer->start(interval * 1000); // execute a single shot
127
127
QList<DailyTimer*> DailyTimer::mFixedTimers;
129
129
DailyTimer::DailyTimer(const QTime& timeOfDay, bool fixed)
134
mFixedTimers.append(this);
134
mFixedTimers.append(this);
137
137
DailyTimer::~DailyTimer()
140
mFixedTimers.removeAt(mFixedTimers.indexOf(this));
140
mFixedTimers.removeAt(mFixedTimers.indexOf(this));
143
143
DailyTimer* DailyTimer::fixedInstance(const QTime& timeOfDay, bool create)
145
for (int i = 0, end = mFixedTimers.count(); i < end; ++i)
146
if (mFixedTimers[i]->mTime == timeOfDay)
147
return mFixedTimers[i];
148
return create ? new DailyTimer(timeOfDay, true) : 0;
145
for (int i = 0, end = mFixedTimers.count(); i < end; ++i)
146
if (mFixedTimers[i]->mTime == timeOfDay)
147
return mFixedTimers[i];
148
return create ? new DailyTimer(timeOfDay, true) : 0;
151
151
/******************************************************************************
155
155
void DailyTimer::disconnect(const QTime& timeOfDay, QObject* receiver, const char* member)
157
DailyTimer* timer = fixedInstance(timeOfDay, false);
160
timer->disconnecT(receiver, member);
161
if (!timer->hasConnections())
157
DailyTimer* timer = fixedInstance(timeOfDay, false);
160
timer->disconnecT(receiver, member);
161
if (!timer->hasConnections())
165
165
/******************************************************************************
168
168
void DailyTimer::changeTime(const QTime& newTimeOfDay, bool triggerMissed)
172
if (mTimer->isActive())
175
bool triggerNow = false;
178
QTime now = QTime::currentTime();
179
if (now >= newTimeOfDay && now < mTime)
181
// The trigger time is now earlier and it has already arrived today.
182
// Trigger a timer event immediately.
186
mTime = newTimeOfDay;
188
mTimer->start(0); // trigger immediately
193
mTime = newTimeOfDay;
172
if (mTimer->isActive())
175
bool triggerNow = false;
178
QTime now = QTime::currentTime();
179
if (now >= newTimeOfDay && now < mTime)
181
// The trigger time is now earlier and it has already arrived today.
182
// Trigger a timer event immediately.
186
mTime = newTimeOfDay;
188
mTimer->start(0); // trigger immediately
193
mTime = newTimeOfDay;
196
196
/******************************************************************************
201
201
void DailyTimer::start()
203
// TIMEZONE = local time
204
QDateTime now = QDateTime::currentDateTime();
205
// Find out whether to trigger today or tomorrow.
206
// In preference, use the last trigger date to determine this, since
207
// that will avoid possible errors due to daylight savings time changes.
209
if (mLastDate.isValid())
210
today = (mLastDate < now.date());
212
today = (now.time() < mTime);
215
next = QDateTime(now.date(), mTime);
217
next = QDateTime(now.date().addDays(1), mTime);
218
uint interval = next.toTime_t() - now.toTime_t();
219
mTimer->start(interval * 1000); // execute a single shot
220
kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
203
// TIMEZONE = local time
204
QDateTime now = QDateTime::currentDateTime();
205
// Find out whether to trigger today or tomorrow.
206
// In preference, use the last trigger date to determine this, since
207
// that will avoid possible errors due to daylight savings time changes.
209
if (mLastDate.isValid())
210
today = (mLastDate < now.date());
212
today = (now.time() < mTime);
215
next = QDateTime(now.date(), mTime);
217
next = QDateTime(now.date().addDays(1), mTime);
218
uint interval = next.toTime_t() - now.toTime_t();
219
mTimer->start(interval * 1000); // execute a single shot
220
kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
223
223
/******************************************************************************
229
229
void DailyTimer::slotTimer()
231
// TIMEZONE = local time
232
QDateTime now = QDateTime::currentDateTime();
233
mLastDate = now.date();
234
QDateTime next = QDateTime(mLastDate.addDays(1), mTime);
235
uint interval = next.toTime_t() - now.toTime_t();
236
mTimer->start(interval * 1000); // execute a single shot
237
kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
231
// TIMEZONE = local time
232
QDateTime now = QDateTime::currentDateTime();
233
mLastDate = now.date();
234
QDateTime next = QDateTime(mLastDate.addDays(1), mTime);
235
uint interval = next.toTime_t() - now.toTime_t();
236
mTimer->start(interval * 1000); // execute a single shot
237
kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;