From 919ec2a5e3ceb1f9b09c69301e491f4fe100968f Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Sun, 29 Jan 2023 13:46:00 +0800 Subject: [PATCH 1/2] feat: clean all notifications if exit on linux --- src/ChatPage.cpp | 8 ++++++++ src/ChatPage.h | 4 ++++ src/TrayIcon.cpp | 10 +++++++++- src/notifications/Manager.h | 3 +++ src/notifications/ManagerLinux.cpp | 9 +++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index cf44ae00..349f8fde 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -1640,3 +1640,11 @@ ChatPage::isRoomActive(const QString &room_id) return QGuiApplication::focusWindow() && QGuiApplication::focusWindow()->isActive() && MainWindow::instance()->windowForRoom(room_id) == QGuiApplication::focusWindow(); } + +#if defined (Q_OS_LINUX) +void +ChatPage::removeAllNotifications() +{ + notificationsManager->closeAllNotifications(); +} +#endif diff --git a/src/ChatPage.h b/src/ChatPage.h index 0ac39faa..e6375be8 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -84,6 +84,10 @@ public: return pushrules; } +#if defined(Q_OS_LINUX) + void removeAllNotifications(); +#endif + public slots: bool handleMatrixUri(QString uri); bool handleMatrixUri(const QUrl &uri); diff --git a/src/TrayIcon.cpp b/src/TrayIcon.cpp index 1e33ac8d..76ccb8fc 100644 --- a/src/TrayIcon.cpp +++ b/src/TrayIcon.cpp @@ -15,6 +15,9 @@ #include "TrayIcon.h" +#if defined(Q_OS_LINUX) +#include "ChatPage.h" +#endif #if defined(Q_OS_MAC) #include #endif @@ -119,7 +122,12 @@ TrayIcon::TrayIcon(const QString &filename, QWindow *parent) quitAction_ = new QAction(tr("Quit"), this); connect(viewAction_, &QAction::triggered, parent, &QWindow::show); - connect(quitAction_, &QAction::triggered, this, QApplication::quit); + connect(quitAction_, &QAction::triggered, this, [=] { +#if defined(Q_OS_LINUX) + ChatPage::instance()->removeAllNotifications(); +#endif + QApplication::quit(); + }); menu->addAction(viewAction_); menu->addAction(quitAction_); diff --git a/src/notifications/Manager.h b/src/notifications/Manager.h index bf7af29b..70a03345 100644 --- a/src/notifications/Manager.h +++ b/src/notifications/Manager.h @@ -55,6 +55,9 @@ public slots: #if defined(NHEKO_DBUS_SYS) public: void closeNotifications(QString roomId); +#if defined(Q_OS_LINUX) + void closeAllNotifications(); +#endif private: QDBusInterface dbus; diff --git a/src/notifications/ManagerLinux.cpp b/src/notifications/ManagerLinux.cpp index 294df4db..c694d88f 100644 --- a/src/notifications/ManagerLinux.cpp +++ b/src/notifications/ManagerLinux.cpp @@ -271,3 +271,12 @@ NotificationsManager::notificationClosed(uint id, uint reason) Q_UNUSED(reason); notificationIds.remove(id); } + +void +NotificationsManager::closeAllNotifications() +{ + for (auto id : notificationIds.keys()) { + closeNotification(id); + notificationIds.remove(id); + } +} From 0e2cc65548880a5446a23dfc4d1098378934f58a Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Wed, 1 Feb 2023 09:54:01 +0800 Subject: [PATCH 2/2] chore: Adjust about function closeAllNotification Log: maek closeAllNotification on all platform, move closeAllNotification action to aboutToQuit --- src/ChatPage.cpp | 4 ++-- src/ChatPage.h | 2 -- src/TrayIcon.cpp | 10 +--------- src/main.cpp | 1 + 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 349f8fde..c0705cc6 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -1641,10 +1641,10 @@ ChatPage::isRoomActive(const QString &room_id) MainWindow::instance()->windowForRoom(room_id) == QGuiApplication::focusWindow(); } -#if defined (Q_OS_LINUX) void ChatPage::removeAllNotifications() { +#if defined (Q_OS_LINUX) notificationsManager->closeAllNotifications(); -} #endif +} diff --git a/src/ChatPage.h b/src/ChatPage.h index e6375be8..ffe70496 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -84,9 +84,7 @@ public: return pushrules; } -#if defined(Q_OS_LINUX) void removeAllNotifications(); -#endif public slots: bool handleMatrixUri(QString uri); diff --git a/src/TrayIcon.cpp b/src/TrayIcon.cpp index 76ccb8fc..1e33ac8d 100644 --- a/src/TrayIcon.cpp +++ b/src/TrayIcon.cpp @@ -15,9 +15,6 @@ #include "TrayIcon.h" -#if defined(Q_OS_LINUX) -#include "ChatPage.h" -#endif #if defined(Q_OS_MAC) #include #endif @@ -122,12 +119,7 @@ TrayIcon::TrayIcon(const QString &filename, QWindow *parent) quitAction_ = new QAction(tr("Quit"), this); connect(viewAction_, &QAction::triggered, parent, &QWindow::show); - connect(quitAction_, &QAction::triggered, this, [=] { -#if defined(Q_OS_LINUX) - ChatPage::instance()->removeAllNotifications(); -#endif - QApplication::quit(); - }); + connect(quitAction_, &QAction::triggered, this, QApplication::quit); menu->addAction(viewAction_); menu->addAction(quitAction_); diff --git a/src/main.cpp b/src/main.cpp index 779c7800..1ecb014d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -350,6 +350,7 @@ main(int argc, char *argv[]) w.show(); QObject::connect(&app, &QApplication::aboutToQuit, &w, [&w]() { + ChatPage::instance()->removeAllNotifications(); w.saveCurrentWindowSize(); if (http::client() != nullptr) { nhlog::net()->debug("shutting down all I/O threads & open connections");