diff --git a/CMakeLists.txt b/CMakeLists.txt index 59842f2..d702f18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,11 +44,11 @@ else() find_library(COCOA Cocoa REQUIRED) list(APPEND TRAY_SOURCES "${CMAKE_SOURCE_DIR}/src/tray_darwin.m") else() - find_package(Qt6 QUIET COMPONENTS Widgets) + find_package(Qt6 QUIET COMPONENTS Widgets DBus) if(Qt6_FOUND) set(TRAY_QT_VERSION 6) else() - find_package(Qt5 REQUIRED COMPONENTS Widgets) + find_package(Qt5 REQUIRED COMPONENTS Widgets DBus) set(TRAY_QT_VERSION 5) endif() set(CMAKE_AUTOMOC ON) @@ -74,9 +74,9 @@ else() else() list(APPEND TRAY_DEFINITIONS TRAY_QT=1) if(TRAY_QT_VERSION EQUAL 6) - list(APPEND TRAY_EXTERNAL_LIBRARIES Qt6::Widgets) + list(APPEND TRAY_EXTERNAL_LIBRARIES Qt6::Widgets Qt6::DBus) else() - list(APPEND TRAY_EXTERNAL_LIBRARIES Qt5::Widgets) + list(APPEND TRAY_EXTERNAL_LIBRARIES Qt5::Widgets Qt5::DBus) endif() endif() endif() diff --git a/src/tray_linux.cpp b/src/tray_linux.cpp index 87c4e62..a911554 100644 --- a/src/tray_linux.cpp +++ b/src/tray_linux.cpp @@ -12,9 +12,12 @@ // Qt includes #include #include +#include +#include #include #include #include +#include namespace { std::unique_ptr g_app; // NOSONAR(cpp:S5421) - mutable state, not const @@ -22,6 +25,22 @@ namespace { int g_loop_result = 0; // NOSONAR(cpp:S5421) - mutable state, not const bool g_app_owned = false; // NOSONAR(cpp:S5421) - mutable state, not const bool g_exit_pending = false; // NOSONAR(cpp:S5421) - mutable state, not const + uint g_notification_id = 0; // NOSONAR(cpp:S5421) - tracks last D-Bus notification ID for proper cleanup + + void close_notification() { + if (g_notification_id == 0) { + return; + } + QDBusInterface iface( + QStringLiteral("org.freedesktop.Notifications"), + QStringLiteral("/org/freedesktop/Notifications"), + QStringLiteral("org.freedesktop.Notifications") + ); + if (iface.isValid()) { + iface.call(QStringLiteral("CloseNotification"), g_notification_id); + } + g_notification_id = 0; + } QMenu *build_menu(struct tray_menu *m, QWidget *parent) { auto *menu = new QMenu(parent); // NOSONAR(cpp:S5025) - submenus owned by parent via Qt; top-level deleted manually @@ -51,6 +70,7 @@ namespace { } void destroy_tray() { + close_notification(); if (g_tray_icon != nullptr) { g_tray_icon->hide(); QMenu *menu = g_tray_icon->contextMenu(); @@ -60,6 +80,17 @@ namespace { delete menu; // NOSONAR(cpp:S5025) - QSystemTrayIcon does not own the context menu } } + + void destroy_app() { + if (g_app_owned && g_app) { + // Destroy QApplication here (during active program execution) rather than letting + // the unique_ptr destructor run at static-destruction time. At static-destruction + // time, Qt's lazily-initialized D-Bus statics have already been destroyed (LIFO + // order), so calling QApplication::~QApplication() then would crash. + g_app.reset(); + g_app_owned = false; + } + } } // namespace extern "C" { @@ -73,6 +104,7 @@ extern "C" { destroy_tray(); g_loop_result = 0; + g_exit_pending = false; g_tray_icon = new QSystemTrayIcon(); // NOSONAR(cpp:S5025) - raw pointer; deleted in destroy_tray() before QApplication @@ -87,8 +119,20 @@ extern "C" { } int tray_loop(int blocking) { + if (g_exit_pending) { + g_exit_pending = false; + destroy_tray(); + destroy_app(); + return g_loop_result; + } + if (blocking) { QApplication::exec(); + if (g_exit_pending) { + g_exit_pending = false; + destroy_tray(); + destroy_app(); + } } else { QApplication::processEvents(); } @@ -116,17 +160,40 @@ extern "C" { const QString text = tray->notification_text != nullptr ? QString::fromUtf8(tray->notification_text) : QString(); QObject::disconnect(g_tray_icon, &QSystemTrayIcon::messageClicked, nullptr, nullptr); + close_notification(); if (!text.isEmpty()) { const QString title = tray->notification_title != nullptr ? QString::fromUtf8(tray->notification_title) : QString(); + const char *icon_path = tray->notification_icon != nullptr ? tray->notification_icon : tray->icon; + const QString icon = icon_path != nullptr ? QString::fromUtf8(icon_path) : QString(); if (tray->notification_cb != nullptr) { void (*cb)() = tray->notification_cb; QObject::connect(g_tray_icon, &QSystemTrayIcon::messageClicked, [cb]() { cb(); }); } - g_tray_icon->showMessage(title, text, QSystemTrayIcon::Information, 5000); - } else { - g_tray_icon->showMessage(QString(), QString(), QSystemTrayIcon::NoIcon, 0); + QDBusInterface iface( + QStringLiteral("org.freedesktop.Notifications"), + QStringLiteral("/org/freedesktop/Notifications"), + QStringLiteral("org.freedesktop.Notifications") + ); + if (iface.isValid()) { + QDBusReply reply = iface.call( + QStringLiteral("Notify"), + QStringLiteral("tray"), + static_cast(0), + icon, + title, + text, + QStringList(), + QVariantMap(), + 5000 + ); + if (reply.isValid()) { + g_notification_id = reply.value(); + } + } else { + g_tray_icon->showMessage(title, text, QSystemTrayIcon::Information, 5000); + } } } @@ -142,12 +209,9 @@ extern "C" { void tray_exit(void) { g_loop_result = -1; - destroy_tray(); - - if (g_app_owned) { + g_exit_pending = true; + if (g_app_owned && QApplication::instance() != nullptr) { QApplication::quit(); - g_app_owned = false; - g_app.reset(); } }