Fix Bug 361605 - kmail crash on double click email
[kdepim.git] / korganizer / datechecker.cpp
blobc8ed22b1164c54d2e7d73972b52be08b4ff3b37f
1 /*
2 This file is part of KOrganizer.
4 Copyright (c) 2002 Adriaan de Groot <groot@kde.org>
5 Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 As a special exception, permission is given to link this program
22 with any edition of Qt, and distribute the resulting executable,
23 without including the source code for Qt in the source distribution.
26 #include "datechecker.h"
28 #include <QTimer>
30 DateChecker::DateChecker(QObject *parent) : QObject(parent), mUpdateTimer(Q_NULLPTR)
32 enableRollover(FollowMonth);
35 DateChecker::~DateChecker()
39 void DateChecker::enableRollover(RolloverType r)
41 switch (r) {
42 case None:
43 if (mUpdateTimer) {
44 mUpdateTimer->stop();
45 delete mUpdateTimer;
46 mUpdateTimer = Q_NULLPTR;
48 break;
49 case FollowDay:
50 case FollowMonth:
51 if (!mUpdateTimer) {
52 mUpdateTimer = new QTimer(this);
53 connect(mUpdateTimer, &QTimer::timeout, this, &DateChecker::possiblyPastMidnight);
55 mUpdateTimer->setSingleShot(true);
56 mUpdateTimer->start(0);
57 mLastDayChecked = QDate::currentDate();
59 mUpdateRollover = r;
62 void DateChecker::passedMidnight()
64 QDate today = QDate::currentDate();
66 if (today.month() != mLastDayChecked.month()) {
67 if (mUpdateRollover == FollowMonth) {
68 Q_EMIT monthPassed(today);
71 Q_EMIT dayPassed(today);
74 void DateChecker::possiblyPastMidnight()
76 if (mLastDayChecked != QDate::currentDate()) {
77 passedMidnight();
78 mLastDayChecked = QDate::currentDate();
80 // Set the timer to go off 1 second after midnight
81 // or after 8 minutes, whichever comes first.
82 if (mUpdateTimer) {
83 QTime now = QTime::currentTime();
84 QTime midnight = QTime(23, 59, 59);
85 int msecsWait = qMin(480000, now.msecsTo(midnight) + 2000);
87 mUpdateTimer->stop();
88 mUpdateTimer->start(msecsWait);