Make sure notification times out even so there is no working backend

This commit is contained in:
Patrick von Reth 2015-07-15 12:23:01 +02:00
parent 5112904d32
commit 9f3cdc6f41
8 changed files with 58 additions and 45 deletions

View File

@ -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;
}

View File

@ -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<NotificationData> d;
friend class NotificationData;
};
}

View File

@ -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()) {

View File

@ -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;
};
}

View File

@ -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)

View File

@ -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);
}
}
}

View File

@ -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 &notification)
{
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) {

View File

@ -69,6 +69,8 @@ public:
*/
void setDefaultSettingsValueIntern(const QString &key, const QVariant &value);
void startNotificationTimeoutTimer(Notification notification);
void stopNotificationTimeoutTimer(Notification &notification);
private Q_SLOTS:
//TODO: find a better solutinon for the slots in this section
friend class Snore::SnoreBackend;