• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm/lib

  • sources
  • kde-4.12
  • kdepim
  • kalarm
  • lib
synchtimer.cpp
Go to the documentation of this file.
1 /*
2  * synchtimer.cpp - timers which synchronize to time boundaries
3  * Program: kalarm
4  * Copyright © 2004,2005,2007-2009 by David Jarvie <djarvie@kde.org>
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 #include "kalarm.h"
22 #include "synchtimer.moc"
23 #include <kdebug.h>
24 #include <QTimer>
25 
26 
27 /*=============================================================================
28 = Class: SynchTimer
29 = Virtual base class for application-wide timer synchronized to a time boundary.
30 =============================================================================*/
31 
32 SynchTimer::SynchTimer()
33 {
34  mTimer = new QTimer(this);
35  mTimer->setSingleShot(true);
36 }
37 
38 SynchTimer::~SynchTimer()
39 {
40  delete mTimer;
41  mTimer = 0;
42 }
43 
44 /******************************************************************************
45 * Connect to the timer. The timer is started if necessary.
46 */
47 void SynchTimer::connecT(QObject* receiver, const char* member)
48 {
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())
55  {
56  connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
57  start();
58  }
59 }
60 
61 /******************************************************************************
62 * Disconnect from the timer. The timer is stopped if no longer needed.
63 */
64 void SynchTimer::disconnecT(QObject* receiver, const char* member)
65 {
66  if (mTimer)
67  {
68  mTimer->disconnect(receiver, member);
69  if (member)
70  {
71  int i = mConnections.indexOf(Connection(receiver, member));
72  if (i >= 0)
73  mConnections.removeAt(i);
74  }
75  else
76  {
77  for (int i = 0; i < mConnections.count(); )
78  {
79  if (mConnections[i].receiver == receiver)
80  mConnections.removeAt(i);
81  else
82  ++i;
83  }
84  }
85  if (mConnections.isEmpty())
86  {
87  mTimer->disconnect();
88  mTimer->stop();
89  }
90  }
91 }
92 
93 
94 /*=============================================================================
95 = Class: MinuteTimer
96 = Application-wide timer synchronized to the minute boundary.
97 =============================================================================*/
98 
99 MinuteTimer* MinuteTimer::mInstance = 0;
100 
101 MinuteTimer* MinuteTimer::instance()
102 {
103  if (!mInstance)
104  mInstance = new MinuteTimer;
105  return mInstance;
106 }
107 
108 /******************************************************************************
109 * Called when the timer triggers, or to start the timer.
110 * Timers can under some circumstances wander off from the correct trigger time,
111 * so rather than setting a 1 minute interval, calculate the correct next
112 * interval each time it triggers.
113 */
114 void MinuteTimer::slotTimer()
115 {
116  kDebug();
117  int interval = 62 - QTime::currentTime().second();
118  mTimer->start(interval * 1000); // execute a single shot
119 }
120 
121 
122 /*=============================================================================
123 = Class: DailyTimer
124 = Application-wide timer synchronized to midnight.
125 =============================================================================*/
126 
127 QList<DailyTimer*> DailyTimer::mFixedTimers;
128 
129 DailyTimer::DailyTimer(const QTime& timeOfDay, bool fixed)
130  : mTime(timeOfDay),
131  mFixed(fixed)
132 {
133  if (fixed)
134  mFixedTimers.append(this);
135 }
136 
137 DailyTimer::~DailyTimer()
138 {
139  if (mFixed)
140  mFixedTimers.removeAt(mFixedTimers.indexOf(this));
141 }
142 
143 DailyTimer* DailyTimer::fixedInstance(const QTime& timeOfDay, bool create)
144 {
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;
149 }
150 
151 /******************************************************************************
152 * Disconnect from the timer signal which triggers at the given fixed time of day.
153 * If there are no remaining connections to that timer, it is destroyed.
154 */
155 void DailyTimer::disconnect(const QTime& timeOfDay, QObject* receiver, const char* member)
156 {
157  DailyTimer* timer = fixedInstance(timeOfDay, false);
158  if (!timer)
159  return;
160  timer->disconnecT(receiver, member);
161  if (!timer->hasConnections())
162  delete timer;
163 }
164 
165 /******************************************************************************
166 * Change the time at which the variable timer triggers.
167 */
168 void DailyTimer::changeTime(const QTime& newTimeOfDay, bool triggerMissed)
169 {
170  if (mFixed)
171  return;
172  if (mTimer->isActive())
173  {
174  mTimer->stop();
175  bool triggerNow = false;
176  if (triggerMissed)
177  {
178  QTime now = QTime::currentTime();
179  if (now >= newTimeOfDay && now < mTime)
180  {
181  // The trigger time is now earlier and it has already arrived today.
182  // Trigger a timer event immediately.
183  triggerNow = true;
184  }
185  }
186  mTime = newTimeOfDay;
187  if (triggerNow)
188  mTimer->start(0); // trigger immediately
189  else
190  start();
191  }
192  else
193  mTime = newTimeOfDay;
194 }
195 
196 /******************************************************************************
197 * Initialise the timer to trigger at the specified time.
198 * This will either be today or tomorrow, depending on whether the trigger time
199 * has already passed.
200 */
201 void DailyTimer::start()
202 {
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.
208  bool today;
209  if (mLastDate.isValid())
210  today = (mLastDate < now.date());
211  else
212  today = (now.time() < mTime);
213  QDateTime next;
214  if (today)
215  next = QDateTime(now.date(), mTime);
216  else
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;
221 }
222 
223 /******************************************************************************
224 * Called when the timer triggers.
225 * Set the timer to trigger again tomorrow at the specified time.
226 * Note that if daylight savings time changes occur, this will not be 24 hours
227 * from now.
228 */
229 void DailyTimer::slotTimer()
230 {
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;
238 }
239 
240 // vim: et sw=4:
DailyTimer
DailyTimer is an application-wide timer synchronized to a specified time of day, local time...
Definition: synchtimer.h:115
SynchTimer::mTimer
QTimer * mTimer
Definition: synchtimer.h:58
DailyTimer::slotTimer
virtual void slotTimer()
Definition: synchtimer.cpp:229
SynchTimer::start
virtual void start()=0
SynchTimer::SynchTimer
SynchTimer()
Definition: synchtimer.cpp:32
MinuteTimer::instance
static MinuteTimer * instance()
Definition: synchtimer.cpp:101
QObject
DailyTimer::DailyTimer
DailyTimer(const QTime &, bool fixed)
Construct an instance.
Definition: synchtimer.cpp:129
MinuteTimer::slotTimer
virtual void slotTimer()
Definition: synchtimer.cpp:114
SynchTimer::~SynchTimer
virtual ~SynchTimer()
Definition: synchtimer.cpp:38
DailyTimer::start
virtual void start()
Definition: synchtimer.cpp:201
SynchTimer::disconnecT
void disconnecT(QObject *receiver, const char *member=0)
Definition: synchtimer.cpp:64
MinuteTimer
MinuteTimer is an application-wide timer synchronized to the minute boundary.
Definition: synchtimer.h:76
DailyTimer::fixedInstance
static DailyTimer * fixedInstance(const QTime &timeOfDay, bool create=true)
Return the instance which triggers at the specified fixed time of day, optionally creating a new inst...
Definition: synchtimer.cpp:143
SynchTimer::hasConnections
bool hasConnections() const
Definition: synchtimer.h:56
DailyTimer::disconnect
static void disconnect(const QTime &timeOfDay, QObject *receiver, const char *member=0)
Disconnect from the timer signal which triggers at the given fixed time of day.
Definition: synchtimer.cpp:155
DailyTimer::~DailyTimer
virtual ~DailyTimer()
Definition: synchtimer.cpp:137
MinuteTimer::MinuteTimer
MinuteTimer()
Definition: synchtimer.h:96
SynchTimer::connecT
void connecT(QObject *receiver, const char *member)
Definition: synchtimer.cpp:47
SynchTimer::Connection
Definition: synchtimer.h:43
DailyTimer::changeTime
void changeTime(const QTime &newTimeOfDay, bool triggerMissed=true)
Change the time at which this variable timer triggers.
Definition: synchtimer.cpp:168
SynchTimer::slotTimer
virtual void slotTimer()=0
QList< DailyTimer * >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal