From 9b1d9679672fac1cf85fe2fdd3aa705d5de4b1ed Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Sun, 18 Jul 2021 07:26:42 +0400 Subject: [PATCH] Move NotificationServiceWatcher to notifications manager --- Telegram/CMakeLists.txt | 4 - .../linux_notification_service_watcher.cpp | 84 ------------------- .../linux_notification_service_watcher.h | 24 ------ .../linux/notifications_manager_linux.cpp | 39 +++++++++ .../platform/linux/specific_linux.cpp | 7 -- 5 files changed, 39 insertions(+), 119 deletions(-) delete mode 100644 Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.cpp delete mode 100644 Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 7dad8ebb2..b79769ec7 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -902,8 +902,6 @@ PRIVATE platform/linux/linux_gtk_integration.h platform/linux/linux_gtk_open_with_dialog.cpp platform/linux/linux_gtk_open_with_dialog.h - platform/linux/linux_notification_service_watcher.cpp - platform/linux/linux_notification_service_watcher.h platform/linux/linux_wayland_integration_dummy.cpp platform/linux/linux_wayland_integration.cpp platform/linux/linux_wayland_integration.h @@ -1203,8 +1201,6 @@ if (DESKTOP_APP_DISABLE_DBUS_INTEGRATION) platform/linux/linux_gsd_media_keys.h platform/linux/linux_mpris_support.cpp platform/linux/linux_mpris_support.h - platform/linux/linux_notification_service_watcher.cpp - platform/linux/linux_notification_service_watcher.h platform/linux/linux_xdp_file_dialog.cpp platform/linux/linux_xdp_file_dialog.h platform/linux/linux_xdp_open_with_dialog.cpp diff --git a/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.cpp b/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.cpp deleted file mode 100644 index f0dc477bd..000000000 --- a/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* -This file is part of Telegram Desktop, -the official desktop application for the Telegram messaging service. - -For license and copyright information please follow this link: -https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL -*/ -#include "platform/linux/linux_notification_service_watcher.h" - -#include "core/application.h" -#include "main/main_domain.h" -#include "window/notifications_manager.h" -#include "base/platform/linux/base_linux_glibmm_helper.h" -#include "base/platform/linux/base_linux_dbus_utilities.h" - -#include -#include - -namespace Platform { -namespace internal { -namespace { - -constexpr auto kService = "org.freedesktop.Notifications"_cs; - -auto Activatable() { - static const auto Result = []() -> std::optional { - try { - const auto connection = Gio::DBus::Connection::get_sync( - Gio::DBus::BusType::BUS_TYPE_SESSION); - - return ranges::contains( - base::Platform::DBus::ListActivatableNames(connection), - Glib::ustring(std::string(kService))); - } catch (...) { - } - - return std::nullopt; - }(); - - return Result; -} - -} // namespace - -class NotificationServiceWatcher::Private { -public: - Glib::RefPtr dbusConnection; - uint signalId = 0; -}; - -NotificationServiceWatcher::NotificationServiceWatcher() -: _private(std::make_unique()) { - try { - _private->dbusConnection = Gio::DBus::Connection::get_sync( - Gio::DBus::BusType::BUS_TYPE_SESSION); - - _private->signalId = base::Platform::DBus::RegisterServiceWatcher( - _private->dbusConnection, - std::string(kService), - []( - const Glib::ustring &service, - const Glib::ustring &oldOwner, - const Glib::ustring &newOwner) { - if (!Core::App().domain().started() - || (Activatable().value_or(true) && newOwner.empty())) { - return; - } - - crl::on_main([] { - Core::App().notifications().createManager(); - }); - }); - } catch (...) { - } -} - -NotificationServiceWatcher::~NotificationServiceWatcher() { - if (_private->dbusConnection && _private->signalId != 0) { - _private->dbusConnection->signal_unsubscribe(_private->signalId); - } -} - -} // namespace internal -} // namespace Platform diff --git a/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.h b/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.h deleted file mode 100644 index 034b49c7b..000000000 --- a/Telegram/SourceFiles/platform/linux/linux_notification_service_watcher.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -This file is part of Telegram Desktop, -the official desktop application for the Telegram messaging service. - -For license and copyright information please follow this link: -https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL -*/ -#pragma once - -namespace Platform { -namespace internal { - -class NotificationServiceWatcher { -public: - NotificationServiceWatcher(); - ~NotificationServiceWatcher(); - -private: - class Private; - const std::unique_ptr _private; -}; - -} // namespace internal -} // namespace Platform diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 82955b01c..4ecb073d9 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -49,6 +49,43 @@ bool InhibitionSupported = false; std::optional CurrentServerInformation; QStringList CurrentCapabilities; +std::unique_ptr CreateServiceWatcher() { + try { + const auto connection = Gio::DBus::Connection::get_sync( + Gio::DBus::BusType::BUS_TYPE_SESSION); + + const auto activatable = [&] { + try { + return ranges::contains( + base::Platform::DBus::ListActivatableNames(connection), + Glib::ustring(std::string(kService))); + } catch (...) { + // avoid service restart loop in sandboxed environments + return true; + } + }(); + + return std::make_unique( + connection, + std::string(kService), + [=]( + const Glib::ustring &service, + const Glib::ustring &oldOwner, + const Glib::ustring &newOwner) { + if (activatable && newOwner.empty()) { + return; + } + + crl::on_main([] { + Core::App().notifications().createManager(); + }); + }); + } catch (...) { + } + + return nullptr; +} + void StartServiceAsync(Fn callback) { try { const auto connection = Gio::DBus::Connection::get_sync( @@ -712,6 +749,8 @@ bool ByDefault() { } void Create(Window::Notifications::System *system) { + static const auto ServiceWatcher = CreateServiceWatcher(); + const auto managerSetter = [=] { using ManagerType = Window::Notifications::ManagerType; if ((Core::App().settings().nativeNotifications() && Supported()) diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 2fa0be985..6040295fb 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -27,7 +27,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION #include "base/platform/linux/base_linux_dbus_utilities.h" #include "base/platform/linux/base_linux_xdp_utilities.h" -#include "platform/linux/linux_notification_service_watcher.h" #include "platform/linux/linux_xdp_file_dialog.h" #include "platform/linux/linux_gsd_media_keys.h" #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION @@ -84,8 +83,6 @@ constexpr auto kSnapcraftSettingsObjectPath = "/io/snapcraft/Settings"_cs; constexpr auto kSnapcraftSettingsInterface = kSnapcraftSettingsService; #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION -std::unique_ptr NSWInstance; - void PortalAutostart(bool start, bool silent) { if (cExeName().isEmpty()) { return; @@ -964,15 +961,11 @@ void start() { crl::async(SetDarkMode); #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION - NSWInstance = std::make_unique(); FileDialog::XDP::Start(); #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION } void finish() { -#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION - NSWInstance = nullptr; -#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION } } // namespace ThirdParty