Move NotificationServiceWatcher to notifications manager

This commit is contained in:
Ilya Fedin 2021-07-18 07:26:42 +04:00 committed by John Preston
parent 4ea065fdf1
commit 9b1d967967
5 changed files with 39 additions and 119 deletions

View file

@ -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

View file

@ -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 <glibmm.h>
#include <giomm.h>
namespace Platform {
namespace internal {
namespace {
constexpr auto kService = "org.freedesktop.Notifications"_cs;
auto Activatable() {
static const auto Result = []() -> std::optional<bool> {
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<Gio::DBus::Connection> dbusConnection;
uint signalId = 0;
};
NotificationServiceWatcher::NotificationServiceWatcher()
: _private(std::make_unique<Private>()) {
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

View file

@ -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> _private;
};
} // namespace internal
} // namespace Platform

View file

@ -49,6 +49,43 @@ bool InhibitionSupported = false;
std::optional<ServerInformation> CurrentServerInformation;
QStringList CurrentCapabilities;
std::unique_ptr<base::Platform::DBus::ServiceWatcher> 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<base::Platform::DBus::ServiceWatcher>(
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<void()> 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())

View file

@ -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<internal::NotificationServiceWatcher> 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<internal::NotificationServiceWatcher>();
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