Commit 805afd9e authored by Volker Krause's avatar Volker Krause
Browse files

Create KNotification instances on demand

This makes all methods in AlarmNotification safe to call at any time,
independent on whether the KNotification object has already self-destructed
or not. It also avoids creating KNotification objects for still suspended
alarms.

This is an intermediate step to unify the handling of active and suspended
alarms in NotificationHandler, so that we can correctly restore active
alarms after a restart as well, and can finally clean up AlarmNotification
objects properly once dismissed.
parent f786ad66
Pipeline #104989 passed with stage
in 5 minutes and 18 seconds
......@@ -9,35 +9,38 @@
#include <KLocalizedString>
#include <QDebug>
AlarmNotification::AlarmNotification(NotificationHandler *handler, const QString &uid)
AlarmNotification::AlarmNotification(const QString &uid)
: m_uid{uid}
, m_remind_at{QDateTime()}
, m_notification_handler{handler}
{
}
AlarmNotification::~AlarmNotification()
{
delete m_notification;
}
void AlarmNotification::send(NotificationHandler *handler)
{
if (m_notification) {
return; // already active
}
m_notification = new KNotification(QStringLiteral("alarm"));
m_notification->setText(m_text);
m_notification->setActions({i18n("Remind in 5 mins"), i18n("Dismiss")});
connect(m_notification, &KNotification::action1Activated, this, &AlarmNotification::suspend);
// dismiss both with the explicit action and just closing the notification
// there is no signal for explicit closing though, we only can observe that
// indirectly from not having received a different signal before closed()
connect(m_notification, &KNotification::closed, this, &AlarmNotification::dismiss);
connect(this, &AlarmNotification::suspend, m_notification_handler, [this]() {
m_notification_handler->suspend(this);
disconnect(this, &AlarmNotification::dismiss, m_notification_handler, nullptr);
QObject::connect(m_notification, &KNotification::closed, handler, [this, handler]() {
handler->dismiss(this);
});
connect(this, &AlarmNotification::dismiss, m_notification_handler, [this]() {
m_notification_handler->dismiss(this);
QObject::connect(m_notification, &KNotification::action1Activated, handler, [this, handler]() {
handler->suspend(this);
QObject::disconnect(m_notification, &KNotification::closed, handler, nullptr);
});
}
AlarmNotification::~AlarmNotification()
{
delete m_notification;
}
void AlarmNotification::send() const
{
m_notification->sendEvent();
}
......@@ -48,12 +51,12 @@ QString AlarmNotification::uid() const
QString AlarmNotification::text() const
{
return m_notification->text();
return m_text;
}
void AlarmNotification::setText(const QString &alarmText)
{
m_notification->setText(alarmText);
m_text = alarmText;
}
QDateTime AlarmNotification::remindAt() const
......
......@@ -16,17 +16,16 @@ class NotificationHandler;
* @brief The alarm notification that should be displayed. It is a wrapper of a KNotification enhanced with alarm properties, like uid and remind time
*
*/
class AlarmNotification : public QObject
class AlarmNotification
{
Q_OBJECT
public:
explicit AlarmNotification(NotificationHandler *handler, const QString &uid);
~AlarmNotification() override;
explicit AlarmNotification(const QString &uid);
~AlarmNotification();
/**
* @brief Sends the notification to be displayed
*/
void send() const;
void send(NotificationHandler *handler);
/**
* @return The uid of the Incidence of the alarm of the notification
......@@ -53,21 +52,9 @@ public:
*/
void setRemindAt(const QDateTime &remindAtDt);
Q_SIGNALS:
/**
* @brief Signal that should be emitted when the user clicks to the Dismiss action button of the KNotification displayed
*/
void dismiss();
/**
* @brief Signal that should be emitted when the user clicks to the Suspend action button of the KNotification displayed
*/
void suspend();
private:
QPointer<KNotification> m_notification;
QString m_uid;
QString m_text;
QDateTime m_remind_at;
NotificationHandler *m_notification_handler;
};
......@@ -24,7 +24,7 @@ NotificationHandler::~NotificationHandler() = default;
void NotificationHandler::addActiveNotification(const QString &uid, const QString &text)
{
AlarmNotification *notification = new AlarmNotification(this, uid);
AlarmNotification *notification = new AlarmNotification(uid);
notification->setText(text);
m_active_notifications[notification->uid()] = notification;
}
......@@ -32,7 +32,7 @@ void NotificationHandler::addActiveNotification(const QString &uid, const QStrin
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;
AlarmNotification *notification = new AlarmNotification(this, uid);
AlarmNotification *notification = new AlarmNotification(uid);
notification->setText(txt);
notification->setRemindAt(remindTime);
m_suspended_notifications[notification->uid()] = notification;
......@@ -44,7 +44,7 @@ void NotificationHandler::sendSuspendedNotifications()
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();
suspItr.value()->send(this);
suspItr = m_suspended_notifications.erase(suspItr);
} else {
suspItr++;
......@@ -56,7 +56,7 @@ void NotificationHandler::sendActiveNotifications()
{
for (const auto &n : qAsConst(m_active_notifications)) {
qDebug() << "sendNotifications:\tSending notification for alarm" << n->uid();
n->send();
n->send(this);
}
}
......@@ -78,7 +78,7 @@ void NotificationHandler::dismiss(AlarmNotification *const notification)
void NotificationHandler::suspend(AlarmNotification *const notification)
{
AlarmNotification *suspendedNotification = new AlarmNotification(this, notification->uid());
AlarmNotification *suspendedNotification = new AlarmNotification(notification->uid());
suspendedNotification->setText(notification->text());
suspendedNotification->setRemindAt(QDateTime(QDateTime::currentDateTime()).addSecs(m_suspend_seconds));
......
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