Skip to content
Snippets Groups Projects
0001-Support-updating-Snore-notifications.patch 4.61 KiB
Newer Older
From be14d07c405b49628be6aa0eb450d896ddd3b54f Mon Sep 17 00:00:00 2001
hluk's avatar
hluk committed
From: Lukas Holecek <hluk@email.cz>
Date: Sun, 15 Nov 2020 19:06:53 +0100
Subject: [PATCH] Support updating Snore notifications

---
 src/notifybysnore.cpp | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp
index 9356bd3..b023238 100644
hluk's avatar
hluk committed
--- a/src/notifybysnore.cpp
+++ b/src/notifybysnore.cpp
@@ -60,7 +60,7 @@ NotifyBySnore::NotifyBySnore(QObject *parent)
hluk's avatar
hluk committed
     m_server.listen(QString::number(qHash(qApp->applicationDirPath())));
     connect(&m_server, &QLocalServer::newConnection, this, [this]() {
         QLocalSocket *responseSocket = m_server.nextPendingConnection();
-        connect(responseSocket, &QLocalSocket::readyRead, [this, responseSocket]() {
hluk's avatar
hluk committed
+        connect(responseSocket, &QLocalSocket::readyRead, this, [this, responseSocket](){
             const QByteArray rawNotificationResponse = responseSocket->readAll();
             responseSocket->deleteLater();
 
@@ -169,9 +169,20 @@ void NotifyBySnore::notifyDeferred(KNotification *notification)
hluk's avatar
hluk committed
 
     // handle the icon for toast notification
     const QString iconPath = m_iconDir.path() + QLatin1Char('/') + QString::number(notification->id());
+    const bool deleteNewIcon = !QFile::exists(iconPath);
     const bool hasIcon = (notification->pixmap().isNull()) ? qApp->windowIcon().pixmap(1024, 1024).save(iconPath, "PNG") //
hluk's avatar
hluk committed
                                                            : notification->pixmap().save(iconPath, "PNG");
     if (hasIcon) {
+        qCDebug(LOG_KNOTIFICATIONS) << "Created temporary icon" << iconPath;
+        if (deleteNewIcon) {
+            connect(notification, &KNotification::destroyed, [iconPath](){
+                if (QFile::remove(iconPath)) {
+                    qCDebug(LOG_KNOTIFICATIONS) << "Deleted temporary icon" << iconPath;
+                } else {
+                    qCDebug(LOG_KNOTIFICATIONS) << "Failed to delete temporary icon" << iconPath;
+                }
+            });
+        }
         snoretoastArgsList << QStringLiteral("-p") << iconPath;
     }
 
@@ -189,19 +200,20 @@ void NotifyBySnore::notifyDeferred(KNotification *notification)
         const auto data = snoretoastProcess->readAllStandardOutput();
         qCDebug(LOG_KNOTIFICATIONS) << "SnoreToast process stdout:" << snoretoastArgsList << data;
hluk's avatar
hluk committed
     });
-    connect(snoretoastProcess, &QProcess::errorOccurred, this, [this, snoretoastProcess, snoretoastArgsList, iconPath](QProcess::ProcessError error) {
hluk's avatar
hluk committed
+    QPointer<KNotification> maybeNotification = notification;
+    connect(snoretoastProcess, &QProcess::errorOccurred, this, [this, snoretoastProcess, maybeNotification, snoretoastArgsList, iconPath](QProcess::ProcessError error) {
         qCWarning(LOG_KNOTIFICATIONS) << "SnoreToast process errored:" << snoretoastArgsList << error;
         snoretoastProcess->deleteLater();
-        QFile::remove(iconPath);
+        close(maybeNotification);
hluk's avatar
hluk committed
     });
     connect(snoretoastProcess,
             QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
             this,
hluk's avatar
hluk committed
-            [this, snoretoastProcess, snoretoastArgsList, iconPath](int exitCode, QProcess::ExitStatus exitStatus) {
+            [this, snoretoastProcess, maybeNotification, snoretoastArgsList, iconPath](int exitCode, QProcess::ExitStatus exitStatus) {
                 qCDebug(LOG_KNOTIFICATIONS) << "SnoreToast process finished:" << snoretoastArgsList;
                 qCDebug(LOG_KNOTIFICATIONS) << "code:" << exitCode << "status:" << exitStatus;
                 snoretoastProcess->deleteLater();
-                QFile::remove(iconPath);
+                close(maybeNotification);
hluk's avatar
hluk committed
 
     qCDebug(LOG_KNOTIFICATIONS) << "SnoreToast process starting:" << snoretoastArgsList;
@@ -210,6 +222,9 @@ void NotifyBySnore::notifyDeferred(KNotification *notification)
hluk's avatar
hluk committed
 
 void NotifyBySnore::close(KNotification *notification)
 {
+    if (notification == nullptr)
+        return;
+
     qCDebug(LOG_KNOTIFICATIONS) << "Requested to close notification with ID:" << notification->id();
     if (m_notifications.constFind(notification->id()) == m_notifications.constEnd()) {
         qCWarning(LOG_KNOTIFICATIONS) << "Couldn't find the notification in m_notifications. Nothing to close.";
@@ -228,7 +243,5 @@ void NotifyBySnore::close(KNotification *notification)
hluk's avatar
hluk committed
 
 void NotifyBySnore::update(KNotification *notification, KNotifyConfig *config)
 {
-    Q_UNUSED(notification);
-    Q_UNUSED(config);
-    qCWarning(LOG_KNOTIFICATIONS) << "updating a notification is not supported yet.";
+    notify(notification, config);
 }
-- 
2.30.2