kalarm/lib
synchtimer.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
00022 #include <QTimer>
00023 #include <kdebug.h>
00024 #include "synchtimer.moc"
00025
00026
00027
00028
00029
00030
00031
00032 SynchTimer::SynchTimer()
00033 {
00034 mTimer = new QTimer(this);
00035 mTimer->setSingleShot(true);
00036 }
00037
00038 SynchTimer::~SynchTimer()
00039 {
00040 delete mTimer;
00041 mTimer = 0;
00042 }
00043
00044
00045
00046
00047 void SynchTimer::connecT(QObject* receiver, const char* member)
00048 {
00049 Connection connection(receiver, member);
00050 if (mConnections.contains(connection))
00051 return;
00052 connect(mTimer, SIGNAL(timeout()), receiver, member);
00053 mConnections.append(connection);
00054 if (!mTimer->isActive())
00055 {
00056 connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
00057 start();
00058 }
00059 }
00060
00061
00062
00063
00064 void SynchTimer::disconnecT(QObject* receiver, const char* member)
00065 {
00066 if (mTimer)
00067 {
00068 mTimer->disconnect(receiver, member);
00069 if (member)
00070 {
00071 mConnections.removeAt(mConnections.indexOf(Connection(receiver, member)));
00072 }
00073 else
00074 {
00075 for (int i = 0; i < mConnections.count(); )
00076 {
00077 if (mConnections[i].receiver == receiver)
00078 mConnections.removeAt(i);
00079 else
00080 ++i;
00081 }
00082 }
00083 if (mConnections.isEmpty())
00084 {
00085 mTimer->disconnect();
00086 mTimer->stop();
00087 }
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 MinuteTimer* MinuteTimer::mInstance = 0;
00098
00099 MinuteTimer* MinuteTimer::instance()
00100 {
00101 if (!mInstance)
00102 mInstance = new MinuteTimer;
00103 return mInstance;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 void MinuteTimer::slotTimer()
00113 {
00114 kDebug();
00115 int interval = 62 - QTime::currentTime().second();
00116 mTimer->start(interval * 1000);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 QList<DailyTimer*> DailyTimer::mFixedTimers;
00126
00127 DailyTimer::DailyTimer(const QTime& timeOfDay, bool fixed)
00128 : mTime(timeOfDay),
00129 mFixed(fixed)
00130 {
00131 if (fixed)
00132 mFixedTimers.append(this);
00133 }
00134
00135 DailyTimer::~DailyTimer()
00136 {
00137 if (mFixed)
00138 mFixedTimers.removeAt(mFixedTimers.indexOf(this));
00139 }
00140
00141 DailyTimer* DailyTimer::fixedInstance(const QTime& timeOfDay, bool create)
00142 {
00143 for (int i = 0, end = mFixedTimers.count(); i < end; ++i)
00144 if (mFixedTimers[i]->mTime == timeOfDay)
00145 return mFixedTimers[i];
00146 return create ? new DailyTimer(timeOfDay, true) : 0;
00147 }
00148
00149
00150
00151
00152
00153 void DailyTimer::disconnect(const QTime& timeOfDay, QObject* receiver, const char* member)
00154 {
00155 DailyTimer* timer = fixedInstance(timeOfDay, false);
00156 if (!timer)
00157 return;
00158 timer->disconnecT(receiver, member);
00159 if (!timer->hasConnections())
00160 delete timer;
00161 }
00162
00163
00164
00165
00166 void DailyTimer::changeTime(const QTime& newTimeOfDay, bool triggerMissed)
00167 {
00168 if (mFixed)
00169 return;
00170 if (mTimer->isActive())
00171 {
00172 mTimer->stop();
00173 bool triggerNow = false;
00174 if (triggerMissed)
00175 {
00176 QTime now = QTime::currentTime();
00177 if (now >= newTimeOfDay && now < mTime)
00178 {
00179
00180
00181 triggerNow = true;
00182 }
00183 }
00184 mTime = newTimeOfDay;
00185 if (triggerNow)
00186 mTimer->start(0);
00187 else
00188 start();
00189 }
00190 else
00191 mTime = newTimeOfDay;
00192 }
00193
00194
00195
00196
00197
00198
00199 void DailyTimer::start()
00200 {
00201
00202 QDateTime now = QDateTime::currentDateTime();
00203
00204
00205
00206 bool today;
00207 if (mLastDate.isValid())
00208 today = (mLastDate < now.date());
00209 else
00210 today = (now.time() < mTime);
00211 QDateTime next;
00212 if (today)
00213 next = QDateTime(now.date(), mTime);
00214 else
00215 next = QDateTime(now.date().addDays(1), mTime);
00216 uint interval = next.toTime_t() - now.toTime_t();
00217 mTimer->start(interval * 1000);
00218 kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227 void DailyTimer::slotTimer()
00228 {
00229
00230 QDateTime now = QDateTime::currentDateTime();
00231 mLastDate = now.date();
00232 QDateTime next = QDateTime(mLastDate.addDays(1), mTime);
00233 uint interval = next.toTime_t() - now.toTime_t();
00234 mTimer->start(interval * 1000);
00235 kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
00236 }