diff --git a/src/libsnore/notification/notification.cpp b/src/libsnore/notification/notification.cpp index 64ee1a4..6d59343 100644 --- a/src/libsnore/notification/notification.cpp +++ b/src/libsnore/notification/notification.cpp @@ -67,12 +67,12 @@ const Icon &Notification::icon() const return d->m_icon; } -const int &Notification::timeout() const +int Notification::timeout() const { return d->m_timeout; } -Notification Notification::old() const +Notification &Notification::old() const { return d->m_toReplace; } diff --git a/src/libsnore/notification/notification.h b/src/libsnore/notification/notification.h index 2fd7498..c41ea38 100644 --- a/src/libsnore/notification/notification.h +++ b/src/libsnore/notification/notification.h @@ -41,7 +41,6 @@ class SnorePlugin; class SNORE_EXPORT Notification { - friend class NotificationData; public: /** * The reason why the Notification was closed. @@ -161,7 +160,7 @@ public: * A timeout of 0 means the notification isSticky and will stay visible until dismissed by the user, if supported by the backend. * @see isSticky */ - const int &timeout() const; + int timeout() const; /** * @@ -255,7 +254,7 @@ public: * @return the old notification to be replaced * @see isUpdate */ - Notification old() const; + Notification &old() const; /** * @@ -284,6 +283,7 @@ public: private: QExplicitlySharedDataPointer d; + friend class NotificationData; }; } diff --git a/src/libsnore/notification/notification_p.cpp b/src/libsnore/notification/notification_p.cpp index b5685f9..76819a7 100644 --- a/src/libsnore/notification/notification_p.cpp +++ b/src/libsnore/notification/notification_p.cpp @@ -81,15 +81,6 @@ void NotificationData::setCloseReason(Snore::Notification::CloseReasons r) m_closeReason = r; } -void NotificationData::setTimeoutTimer(QTimer *timer) -{ - if (m_timeoutTimer) { - m_timeoutTimer->stop(); - m_timeoutTimer->deleteLater(); - } - m_timeoutTimer.reset(timer); -} - QString NotificationData::resolveMarkup(const QString &string, Utils::MARKUP_FLAGS flags) { if (!m_hints.value("use-markup").toBool()) { diff --git a/src/libsnore/notification/notification_p.h b/src/libsnore/notification/notification_p.h index 15015e8..9a9679c 100644 --- a/src/libsnore/notification/notification_p.h +++ b/src/libsnore/notification/notification_p.h @@ -32,8 +32,6 @@ class SnorePlugin; class SNORE_EXPORT NotificationData : public QSharedData { - - friend class Notification; public: NotificationData(const Application &application, const Alert &alert, const QString &title, const QString &text, const Icon &icon, int timeout, Notification::Prioritys priority); @@ -46,8 +44,6 @@ public: void setCloseReason(Notification::CloseReasons r); - void setTimeoutTimer(QTimer *timer); - QString resolveMarkup(const QString &string, Utils::MARKUP_FLAGS flags); void setBroadcasted(); @@ -97,6 +93,8 @@ private: static uint notificationCount; static uint m_idCount; + friend class Notification; + friend class SnoreCorePrivate; }; } diff --git a/src/libsnore/plugins/snorebackend.cpp b/src/libsnore/plugins/snorebackend.cpp index f4a36d3..b169d22 100644 --- a/src/libsnore/plugins/snorebackend.cpp +++ b/src/libsnore/plugins/snorebackend.cpp @@ -62,9 +62,11 @@ SnoreBackend::~SnoreBackend() void SnoreBackend::requestCloseNotification(Notification notification, Notification::CloseReasons reason) { - if (canCloseNotification() && notification.isValid()) { - closeNotification(notification, reason); - slotCloseNotification(notification); + if (notification.isValid()) { + if (canCloseNotification()) { + slotCloseNotification(notification); + closeNotification(notification, reason); + } } } @@ -78,7 +80,7 @@ void SnoreBackend::closeNotification(Notification n, Notification::CloseReasons n.old().removeActiveIn(this); } n.data()->setCloseReason(reason); - snoreDebug(SNORE_DEBUG) << n; + snoreDebug(SNORE_DEBUG) << n << reason; emit notificationClosed(n); } @@ -139,29 +141,6 @@ void SnoreBackend::slotNotificationDisplayed(Notification notification) { notification.addActiveIn(this); SnoreCorePrivate::instance()->slotNotificationDisplayed(notification); - - if (notification.isSticky()) { - return; - } - - QTimer *timer = new QTimer(this); - timer->setSingleShot(true); - notification.data()->setTimeoutTimer(timer); - - if (notification.isUpdate()) { - notification.old().data()->setTimeoutTimer(nullptr); - } - timer->setInterval(notification.timeout() * 1000); - //dont use a capture for notification, as it can leak - uint id = notification.id(); - connect(timer, &QTimer::timeout, [id]() { - Notification notification = SnoreCore::instance().getActiveNotificationByID(id); - if (notification.isValid()) { - snoreDebug(SNORE_DEBUG) << notification; - SnoreCore::instance().requestCloseNotification(notification, Notification::TIMED_OUT); - } - }); - timer->start(); } void SnoreBackend::slotNotificationActionInvoked(Notification notification, const Action &action) diff --git a/src/libsnore/snore.cpp b/src/libsnore/snore.cpp index d7f30a3..1d2ea48 100644 --- a/src/libsnore/snore.cpp +++ b/src/libsnore/snore.cpp @@ -129,6 +129,7 @@ void SnoreCore::broadcastNotification(Notification notification) } } notification.data()->setBroadcasted(); + d->startNotificationTimeoutTimer(notification); emit d->notify(notification); } @@ -185,8 +186,14 @@ bool SnoreCore::setPrimaryNotificationBackend(const QString &backend) void SnoreCore::requestCloseNotification(Notification n, Notification::CloseReasons r) { Q_D(SnoreCore); + if (d->m_notificationBackend) { d->m_notificationBackend->requestCloseNotification(n, r); + } else { + if (n.isValid()) { + n.data()->setCloseReason(r); + emit notificationClosed(n); + } } } diff --git a/src/libsnore/snore_p.cpp b/src/libsnore/snore_p.cpp index 1e0574e..16abb09 100644 --- a/src/libsnore/snore_p.cpp +++ b/src/libsnore/snore_p.cpp @@ -228,6 +228,42 @@ void SnoreCorePrivate::slotAboutToQuit() } } +void SnoreCorePrivate::startNotificationTimeoutTimer(Notification notification) +{ + Q_Q(SnoreCore); + if (notification.isSticky()) { + return; + } + + if (notification.data()->m_timeoutTimer) { + stopNotificationTimeoutTimer(notification); + } + QTimer *timer = new QTimer(); + notification.data()->m_timeoutTimer.reset(timer); + timer->setSingleShot(true); + + if (notification.isUpdate()) { + stopNotificationTimeoutTimer(notification.old()); + } + timer->setInterval(notification.timeout() * 1000); + connect(timer, &QTimer::timeout, [q, notification]() { + snoreDebug(SNORE_DEBUG) << notification; + q->requestCloseNotification(notification, Notification::TIMED_OUT); + }); + timer->start(); +} + +void SnoreCorePrivate::stopNotificationTimeoutTimer(Notification ¬ification) +{ + if (notification.data()->m_timeout) { + notification.data()->m_timeoutTimer->stop(); + notification.data()->m_timeoutTimer->deleteLater(); + notification.data()->m_timeoutTimer.reset(nullptr); + } +} + +///Startup code + static void loadTranslator() { auto installTranslator = [](const QString & locale) { diff --git a/src/libsnore/snore_p.h b/src/libsnore/snore_p.h index 309bb00..49211b3 100644 --- a/src/libsnore/snore_p.h +++ b/src/libsnore/snore_p.h @@ -69,6 +69,8 @@ public: */ void setDefaultSettingsValueIntern(const QString &key, const QVariant &value); + void startNotificationTimeoutTimer(Notification notification); + void stopNotificationTimeoutTimer(Notification ¬ification); private Q_SLOTS: //TODO: find a better solutinon for the slots in this section friend class Snore::SnoreBackend;