File indexing completed on 2024-05-12 05:21:22

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 2002 Adriaan de Groot <groot@kde.org>
0005   SPDX-FileCopyrightText: 2004 Cornelius Schumacher <schumacher@kde.org>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0008 */
0009 
0010 #include "datechecker.h"
0011 
0012 #include <QTimer>
0013 
0014 DateChecker::DateChecker(QObject *parent)
0015     : QObject(parent)
0016 {
0017     enableRollover(FollowMonth);
0018 }
0019 
0020 DateChecker::~DateChecker() = default;
0021 
0022 void DateChecker::enableRollover(RolloverType r)
0023 {
0024     switch (r) {
0025     case None:
0026         if (mUpdateTimer) {
0027             mUpdateTimer->stop();
0028             delete mUpdateTimer;
0029             mUpdateTimer = nullptr;
0030         }
0031         break;
0032     case FollowDay:
0033     case FollowMonth:
0034         if (!mUpdateTimer) {
0035             mUpdateTimer = new QTimer(this);
0036             connect(mUpdateTimer, &QTimer::timeout, this, &DateChecker::possiblyPastMidnight);
0037         }
0038         mUpdateTimer->setSingleShot(true);
0039         mUpdateTimer->start(0);
0040         mLastDayChecked = QDate::currentDate();
0041         break;
0042     }
0043     mUpdateRollover = r;
0044 }
0045 
0046 void DateChecker::passedMidnight()
0047 {
0048     QDate today = QDate::currentDate();
0049 
0050     if (today.month() != mLastDayChecked.month()) {
0051         if (mUpdateRollover == FollowMonth) {
0052             Q_EMIT monthPassed(today);
0053         }
0054     }
0055     Q_EMIT dayPassed(today);
0056 }
0057 
0058 void DateChecker::possiblyPastMidnight()
0059 {
0060     if (mLastDayChecked != QDate::currentDate()) {
0061         passedMidnight();
0062         mLastDayChecked = QDate::currentDate();
0063     }
0064     // Set the timer to go off 1 second after midnight
0065     // or after 8 minutes, whichever comes first.
0066     if (mUpdateTimer) {
0067         const QTime now = QTime::currentTime();
0068         const QTime midnight = QTime(23, 59, 59);
0069         const int msecsWait = qMin(480000, now.msecsTo(midnight) + 2000);
0070 
0071         mUpdateTimer->stop();
0072         mUpdateTimer->start(msecsWait);
0073     }
0074 }
0075 
0076 #include "moc_datechecker.cpp"