File indexing completed on 2024-05-05 05:28:19

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Dimitris Kardarakos <dimkard@posteo.net>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "notificationhandler.h"
0008 #include "alarmnotification.h"
0009 #include <KLocalizedString>
0010 #include <KSharedConfig>
0011 #include <KConfigGroup>
0012 #include <QDebug>
0013 
0014 NotificationHandler::NotificationHandler(QObject *parent) : QObject(parent), m_active_notifications {QHash<QString, AlarmNotification*>()}, m_suspended_notifications {QHash<QString, AlarmNotification*>()}
0015 {
0016     KConfigGroup generalGroup(KSharedConfig::openConfig(), QStringLiteral("General"));
0017     m_suspend_seconds = generalGroup.readEntry("SuspendSeconds", 60);
0018 }
0019 
0020 NotificationHandler::~NotificationHandler() = default;
0021 
0022 void NotificationHandler::addActiveNotification(const QString &uid, const QString &text)
0023 {
0024     AlarmNotification *notification = new AlarmNotification(this, uid);
0025     notification->setText(text);
0026     m_active_notifications[notification->uid()] = notification;
0027 }
0028 
0029 void NotificationHandler::addSuspendedNotification(const QString &uid, const QString &txt, const QDateTime &remindTime)
0030 {
0031     qDebug() << "addSuspendedNotification:\tAdding notification to suspended list, uid:" << uid << "text:" << txt << "remindTime:" << remindTime;
0032     AlarmNotification *notification = new AlarmNotification(this, uid);
0033     notification->setText(txt);
0034     notification->setRemindAt(remindTime);
0035     m_suspended_notifications[notification->uid()] = notification;
0036 }
0037 
0038 void NotificationHandler::sendSuspendedNotifications()
0039 {
0040     auto suspItr = m_suspended_notifications.begin();
0041     while (suspItr != m_suspended_notifications.end()) {
0042         if (suspItr.value()->remindAt() < m_period.to) {
0043             qDebug() << "sendNotifications:\tSending notification for suspended alarm" <<  suspItr.value()->uid() << ", text is" << suspItr.value()->text();
0044             suspItr.value()->send();
0045             suspItr = m_suspended_notifications.erase(suspItr);
0046         } else {
0047             suspItr++;
0048         }
0049     }
0050 }
0051 
0052 void NotificationHandler::sendActiveNotifications()
0053 {
0054     for (const auto &n : qAsConst(m_active_notifications)) {
0055         qDebug() << "sendNotifications:\tSending notification for alarm" <<  n->uid();
0056         n->send();
0057     }
0058 }
0059 
0060 void NotificationHandler::sendNotifications()
0061 {
0062     qDebug() << "\nsendNotifications:\tLooking for notifications, total Active:" << m_active_notifications.count() << ", total Suspended:" << m_suspended_notifications.count();
0063 
0064     sendSuspendedNotifications();
0065     sendActiveNotifications();
0066 }
0067 
0068 void NotificationHandler::dismiss(AlarmNotification *const notification)
0069 {
0070     m_active_notifications.remove(notification->uid());
0071 
0072     qDebug() << "\ndismiss:\tAlarm" << notification->uid() << "dismissed";
0073     Q_EMIT scheduleAlarmCheck();
0074 }
0075 
0076 void NotificationHandler::suspend(AlarmNotification *const notification)
0077 {
0078     AlarmNotification *suspendedNotification = new AlarmNotification(this, notification->uid());
0079     suspendedNotification->setText(notification->text());
0080     suspendedNotification->setRemindAt(QDateTime(QDateTime::currentDateTime()).addSecs(m_suspend_seconds));
0081 
0082     m_suspended_notifications[notification->uid()] = suspendedNotification;
0083     m_active_notifications.remove(notification->uid());
0084 
0085     qDebug() << "\nsuspend\t:Alarm " << notification->uid() << "suspended";
0086 
0087     Q_EMIT scheduleAlarmCheck();
0088 }
0089 
0090 FilterPeriod NotificationHandler::period() const
0091 {
0092     return m_period;
0093 }
0094 
0095 void NotificationHandler::setPeriod(const FilterPeriod &checkPeriod)
0096 {
0097     m_period = checkPeriod;
0098 }
0099 
0100 QHash<QString, AlarmNotification *> NotificationHandler::activeNotifications() const
0101 {
0102     return m_active_notifications;
0103 }
0104 
0105 QHash<QString, AlarmNotification *> NotificationHandler::suspendedNotifications() const
0106 {
0107     return m_suspended_notifications;
0108 }
0109 
0110 QDateTime NotificationHandler::firstSuspended() const
0111 {
0112     if (m_suspended_notifications.isEmpty()) {
0113         return QDateTime {};
0114     }
0115 
0116     auto firstAlarmTime = m_suspended_notifications.values().first()->remindAt();
0117 
0118     for (const auto &s : qAsConst(m_suspended_notifications)) {
0119         auto alarmTime = s->remindAt();
0120         if (alarmTime < firstAlarmTime) {
0121             firstAlarmTime = alarmTime;
0122         }
0123     }
0124 
0125     return firstAlarmTime;
0126 }