Commit 95ecb639 authored by Volker Krause's avatar Volker Krause
Browse files

Unify handling of active and suspended notifications

Besides simplifying the code this fixes a number of issues:
- AlarmNotifications are now properly deleted
- Suspended notifications are not triggered immediately after a restart,
  but at the correct  time.
- Active notifications that haven't explicitly been dismissed or suspended
  yet are also restored after a restart.
parent ab7c65c7
Pipeline #105239 passed with stage
in 5 minutes and 15 seconds
......@@ -17,7 +17,9 @@ AlarmNotification::AlarmNotification(const QString &uid)
AlarmNotification::~AlarmNotification()
{
delete m_notification;
// don't delete immediately, in case we end up here as a result
// of a signal from m_notification itself
m_notification->deleteLater();
}
void AlarmNotification::send(NotificationHandler *handler)
......
......@@ -118,11 +118,11 @@ void KalendarAlarmClient::restoreSuspendedFromConfig()
KConfigGroup suspendedAlarm(&suspendedGroup, s);
QString uid = suspendedAlarm.readEntry("UID");
QString txt = suspendedAlarm.readEntry("Text");
QDateTime remindAt = QDateTime::fromString(suspendedAlarm.readEntry("RemindAt"), QStringLiteral("yyyy,M,d,HH,m,s"));
qDebug() << "restoreSuspendedFromConfig: Restoring alarm" << uid << "," << txt << "," << remindAt.toString();
QDateTime remindAt = suspendedAlarm.readEntry("RemindAt", QDateTime());
qDebug() << "restoreSuspendedFromConfig: Restoring alarm" << uid << "," << txt << "," << remindAt;
if (!(uid.isEmpty() && remindAt.isValid() && !(txt.isEmpty()))) {
m_notificationHandler->addSuspendedNotification(uid, txt, remindAt);
if (!uid.isEmpty() && remindAt.isValid() && !txt.isEmpty()) {
m_notificationHandler->addNotification(uid, txt, remindAt);
}
}
}
......@@ -132,17 +132,17 @@ void KalendarAlarmClient::flushSuspendedToConfig()
KConfigGroup suspendedGroup(KSharedConfig::openConfig(), "Suspended");
suspendedGroup.deleteGroup();
const auto suspendedNotifications = m_notificationHandler->suspendedNotifications();
const auto notifications = m_notificationHandler->activeNotifications();
if (suspendedNotifications.isEmpty()) {
qDebug() << "flushSuspendedToConfig: No suspended notification exists, nothing to write to config";
if (notifications.isEmpty()) {
qDebug() << "flushSuspendedToConfig: No pending notification exists, nothing to write to config";
KSharedConfig::openConfig()->sync();
return;
}
for (const auto &s : suspendedNotifications) {
qDebug() << "flushSuspendedToConfig: Flushing suspended alarm" << s->uid() << " to config";
for (const auto &s : notifications) {
qDebug() << "flushSuspendedToConfig: Flushing alarm" << s->uid() << s->remindAt() << " to config";
KConfigGroup notificationGroup(&suspendedGroup, s->uid());
notificationGroup.writeEntry("UID", s->uid());
notificationGroup.writeEntry("Text", s->text());
......@@ -208,17 +208,17 @@ void KalendarAlarmClient::checkAlarms()
if (incidence && incidence->type() == KCalendarCore::Incidence::TypeTodo && !incidence->dtStart().isValid()) {
auto todo = incidence.staticCast<KCalendarCore::Todo>();
timeText = i18n("Task due at %1", QLocale::system().toString(todo->dtDue().time(), QLocale::NarrowFormat));
m_notificationHandler->addActiveNotification(uid, QLatin1String("%1\n%2").arg(timeText, incidence->summary()));
m_notificationHandler->addNotification(uid, QLatin1String("%1\n%2").arg(timeText, incidence->summary()), mLastChecked);
} else if (incidence) {
QString incidenceString = incidence->type() == KCalendarCore::Incidence::TypeTodo ? i18n("Task") : i18n("Event");
timeText = i18nc("Event starts at 10:00",
"%1 starts at %2",
incidenceString,
QLocale::system().toString(incidence->dtStart().time(), QLocale::NarrowFormat));
m_notificationHandler->addActiveNotification(uid, QLatin1String("%1\n%2").arg(timeText, incidence->summary()));
m_notificationHandler->addNotification(uid, QLatin1String("%1\n%2").arg(timeText, incidence->summary()), mLastChecked);
} else {
QLocale::system().toString(alarm->time(), QLocale::NarrowFormat);
m_notificationHandler->addActiveNotification(uid, QLatin1String("%1\n%2").arg(timeText, alarm->text()));
m_notificationHandler->addNotification(uid, QLatin1String("%1\n%2").arg(timeText, alarm->text()), mLastChecked);
}
}
......
......@@ -13,8 +13,6 @@
NotificationHandler::NotificationHandler(QObject *parent)
: QObject(parent)
, m_active_notifications{QHash<QString, AlarmNotification *>()}
, m_suspended_notifications{QHash<QString, AlarmNotification *>()}
{
KConfigGroup generalGroup(KSharedConfig::openConfig(), "General");
m_suspend_seconds = generalGroup.readEntry("SuspendSeconds", 60 * 5); // 5 minutes
......@@ -22,78 +20,44 @@ NotificationHandler::NotificationHandler(QObject *parent)
NotificationHandler::~NotificationHandler() = default;
void NotificationHandler::addActiveNotification(const QString &uid, const QString &text)
void NotificationHandler::addNotification(const QString &uid, const QString &txt, const QDateTime &remindTime)
{
AlarmNotification *notification = new AlarmNotification(uid);
notification->setText(text);
m_active_notifications[notification->uid()] = notification;
}
void NotificationHandler::addSuspendedNotification(const QString &uid, const QString &txt, const QDateTime &remindTime)
{
qDebug() << "addSuspendedNotification:\tAdding notification to suspended list, uid:" << uid << "text:" << txt << "remindTime:" << remindTime;
if (m_notifications.contains(uid)) {
return;
}
qDebug() << "Adding notification, uid:" << uid << "text:" << txt << "remindTime:" << remindTime;
AlarmNotification *notification = new AlarmNotification(uid);
notification->setText(txt);
notification->setRemindAt(remindTime);
m_suspended_notifications[notification->uid()] = notification;
}
void NotificationHandler::sendSuspendedNotifications()
{
auto suspItr = m_suspended_notifications.begin();
while (suspItr != m_suspended_notifications.end()) {
if (suspItr.value()->remindAt() < QDateTime::currentDateTime()) {
qDebug() << "sendNotifications:\tSending notification for suspended alarm" << suspItr.value()->uid() << ", text is" << suspItr.value()->text();
suspItr.value()->send(this);
suspItr = m_suspended_notifications.erase(suspItr);
} else {
suspItr++;
}
}
}
void NotificationHandler::sendActiveNotifications()
{
for (const auto &n : qAsConst(m_active_notifications)) {
qDebug() << "sendNotifications:\tSending notification for alarm" << n->uid();
n->send(this);
}
m_notifications[notification->uid()] = notification;
}
void NotificationHandler::sendNotifications()
{
qDebug() << "\nsendNotifications:\tLooking for notifications, total Active:" << m_active_notifications.count()
<< ", total Suspended:" << m_suspended_notifications.count();
qDebug() << "Looking for notifications, total:" << m_notifications.count();
sendSuspendedNotifications();
sendActiveNotifications();
for (auto it = m_notifications.begin(); it != m_notifications.end(); ++it) {
if (it.value()->remindAt() <= QDateTime::currentDateTime()) {
qDebug() << "Sending notification for alarm" << it.value()->uid() << ", text is" << it.value()->text();
it.value()->send(this);
}
}
}
void NotificationHandler::dismiss(AlarmNotification *const notification)
void NotificationHandler::dismiss(AlarmNotification *notification)
{
m_active_notifications.remove(notification->uid());
qDebug() << "\ndismiss:\tAlarm" << notification->uid() << "dismissed";
qDebug() << "Alarm" << notification->uid() << "dismissed";
m_notifications.remove(notification->uid());
delete notification;
}
void NotificationHandler::suspend(AlarmNotification *const notification)
void NotificationHandler::suspend(AlarmNotification *notification)
{
AlarmNotification *suspendedNotification = new AlarmNotification(notification->uid());
suspendedNotification->setText(notification->text());
suspendedNotification->setRemindAt(QDateTime(QDateTime::currentDateTime()).addSecs(m_suspend_seconds));
m_suspended_notifications[notification->uid()] = suspendedNotification;
m_active_notifications.remove(notification->uid());
qDebug() << "\nsuspend\t:Alarm " << notification->uid() << "suspended";
qDebug() << ":Alarm " << notification->uid() << "suspended";
notification->setRemindAt(QDateTime(QDateTime::currentDateTime()).addSecs(m_suspend_seconds));
}
QHash<QString, AlarmNotification *> NotificationHandler::activeNotifications() const
{
return m_active_notifications;
}
QHash<QString, AlarmNotification *> NotificationHandler::suspendedNotifications() const
{
return m_suspended_notifications;
return m_notifications;
}
......@@ -29,46 +29,30 @@ public:
void sendNotifications();
/**
* @brief Creates an alarm notification object for the Incidence with \p uid. It sets the text to be displayed according to \p text. It adds this alarm
* notification to the internal list of active notifications (the list of notifications that should be sent at the next check).
* @brief Creates an alarm notification object for the Incidence with \p uid. It sets the text to be displayed according to \p text.
*/
void addActiveNotification(const QString &uid, const QString &text);
/**
* @brief Creates an alarm notification object for the Incidence with \p uid. It sets the text to be displayed according to \p text. It adds this alarm
* notification to the internal list of suspended notifications.
*/
void addSuspendedNotification(const QString &uid, const QString &text, const QDateTime &remindTime);
void addNotification(const QString &uid, const QString &text, const QDateTime &remindTime);
/**
* @return The list of active notifications. It is the set of notification that should be sent at the next check
*/
QHash<QString, AlarmNotification *> activeNotifications() const;
/**
* @return The list of suspended notifications
*/
QHash<QString, AlarmNotification *> suspendedNotifications() const;
public Q_SLOTS:
/**
* @brief Dismisses any further notification display for the alarm \p notification.
*
*/
void dismiss(AlarmNotification *const notification);
void dismiss(AlarmNotification *notification);
/**
* @brief Suspends the display of the alarm \p notification, by removing it from the list of active and putting it to the list of suspended notifications.
* Remind time is set according to configuration.
*/
void suspend(AlarmNotification *const notification);
void suspend(AlarmNotification *notification);
private:
void sendActiveNotifications();
void sendSuspendedNotifications();
QHash<QString, AlarmNotification *> m_active_notifications;
QHash<QString, AlarmNotification *> m_suspended_notifications;
QHash<QString, AlarmNotification *> m_notifications;
int m_suspend_seconds;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment