From 3bb0ee17cdbdf70f213864bbbc7ffe0b1e700f32 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 18 Jan 2023 02:14:41 +0100 Subject: [PATCH] NotificationsManager: Group notifications per room Instead of issuing a new notification for every message, bundle them by room. It looks like the code was originally designed to do that and somehow we forgot along the way. It also fixes the leaking in m_notifications as we were never cleaning after it. --- src/notificationsmanager.cpp | 32 +++++++++++++++++++------------- src/notificationsmanager.h | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/notificationsmanager.cpp b/src/notificationsmanager.cpp index b60988bad..194821b08 100644 --- a/src/notificationsmanager.cpp +++ b/src/notificationsmanager.cpp @@ -55,18 +55,27 @@ void NotificationsManager::postNotification(NeoChatRoom *room, const QString &replyEventId, bool canReply) { - QPixmap img; - img.convertFromImage(icon); - KNotification *notification = new KNotification("message"); - - if (sender == room->displayName()) { - notification->setTitle(sender); - } else { - notification->setTitle(i18n("%1 (%2)", sender, room->displayName())); + const QString roomId = room->id(); + KNotification *notification = m_notifications.value(roomId); + if (!notification) { + notification = new KNotification("message"); + m_notifications.insert(roomId, notification); + connect(notification, &KNotification::closed, this, [this, roomId] { + m_notifications.remove(roomId); + }); } - notification->setText(text.toHtmlEscaped()); - notification->setPixmap(img); + QString entry; + if (sender == room->displayName()) { + notification->setTitle(sender); + entry = text.toHtmlEscaped(); + } else { + notification->setTitle(room->displayName()); + entry = i18n("%1: %2", sender, text.toHtmlEscaped()); + } + + notification->setText(entry + '\n' + notification->text()); + notification->setPixmap(QPixmap::fromImage(icon)); notification->setDefaultAction(i18n("Open NeoChat in this room")); connect(notification, &KNotification::defaultActivated, this, [=]() { @@ -96,10 +105,7 @@ void NotificationsManager::postNotification(NeoChatRoom *room, } notification->setHint(QStringLiteral("x-kde-origin-name"), room->localUser()->id()); - notification->sendEvent(); - - m_notifications.insert(room->id(), notification); } void NotificationsManager::postInviteNotification(NeoChatRoom *room, const QString &title, const QString &sender, const QImage &icon) diff --git a/src/notificationsmanager.h b/src/notificationsmanager.h index cb4839c7f..2b11c011d 100644 --- a/src/notificationsmanager.h +++ b/src/notificationsmanager.h @@ -184,7 +184,7 @@ public: private: NotificationsManager(QObject *parent = nullptr); - QMultiMap m_notifications; + QHash m_notifications; QHash> m_invitations; bool m_globalNotificationsEnabled = false;