Move NotificationServiceWatcher to glibmm

This commit is contained in:
Ilya Fedin 2021-03-01 22:40:09 +04:00 committed by John Preston
parent 82f92cffd3
commit d77df9905f
2 changed files with 42 additions and 24 deletions

View file

@ -10,9 +10,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/application.h" #include "core/application.h"
#include "main/main_domain.h" #include "main/main_domain.h"
#include "window/notifications_manager.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 "base/platform/linux/base_linux_dbus_utilities.h"
#include <QtDBus/QDBusConnection> #include <glibmm.h>
#include <giomm.h>
namespace Platform { namespace Platform {
namespace internal { namespace internal {
@ -40,27 +42,43 @@ bool IsNotificationServiceActivatable() {
} // namespace } // namespace
NotificationServiceWatcher::NotificationServiceWatcher() class NotificationServiceWatcher::Private {
: _dbusWatcher( public:
kNotificationService.utf16(), Glib::RefPtr<Gio::DBus::Connection> dbusConnection;
QDBusConnection::sessionBus(), uint signalId = 0;
QDBusServiceWatcher::WatchForOwnerChange) { };
const auto signal = &QDBusServiceWatcher::serviceOwnerChanged;
QObject::connect(&_dbusWatcher, signal, [=](
const QString &service,
const QString &oldOwner,
const QString &newOwner) {
crl::on_main([=] {
if (!Core::App().domain().started()) {
return;
} else if (IsNotificationServiceActivatable()
&& newOwner.isEmpty()) {
return;
}
Core::App().notifications().createManager(); 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(kNotificationService),
[](
const Glib::ustring &service,
const Glib::ustring &oldOwner,
const Glib::ustring &newOwner) {
if (!Core::App().domain().started()
|| (IsNotificationServiceActivatable()
&& 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 internal

View file

@ -7,17 +7,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#pragma once #pragma once
#include <QtDBus/QDBusServiceWatcher>
namespace Platform { namespace Platform {
namespace internal { namespace internal {
class NotificationServiceWatcher { class NotificationServiceWatcher {
public: public:
NotificationServiceWatcher(); NotificationServiceWatcher();
~NotificationServiceWatcher();
private: private:
QDBusServiceWatcher _dbusWatcher; class Private;
const std::unique_ptr<Private> _private;
}; };
} // namespace internal } // namespace internal