From 37a8afaddf477a9d22b811f91da3065e72a36e88 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Tue, 2 Mar 2021 14:20:51 +0400 Subject: [PATCH] Move remaining usage of dbus to glibmm in notifications_manager_linux --- .../linux/notifications_manager_linux.cpp | 264 ++++++++++-------- 1 file changed, 153 insertions(+), 111 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 67c819f530..719f297ac9 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -20,13 +20,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include -#include -#include -#include -#include -#include -#include -#include #include #include @@ -71,112 +64,158 @@ bool GetServiceRegistered() { } void GetServerInformation( - Fn)> callback) { - using ServerInformationReply = QDBusPendingReply< - QString, - QString, - QString, - QString>; + Fn &)> callback) { + try { + const auto connection = Gio::DBus::Connection::get_sync( + Gio::DBus::BusType::BUS_TYPE_SESSION); - const auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("GetServerInformation")); + connection->call( + std::string(kObjectPath), + std::string(kInterface), + "GetServerInformation", + {}, + [=](const Glib::RefPtr &result) { + try { + auto reply = connection->call_finish(result); - const auto async = QDBusConnection::sessionBus().asyncCall(message); - auto watcher = new QDBusPendingCallWatcher(async); + const auto name = base::Platform::GlibVariantCast< + Glib::ustring>(reply.get_child(0)); - const auto finished = [=](QDBusPendingCallWatcher *call) { - const ServerInformationReply reply = *call; + const auto vendor = base::Platform::GlibVariantCast< + Glib::ustring>(reply.get_child(1)); - if (reply.isValid()) { - crl::on_main([=] { - callback(ServerInformation{ - reply.argumentAt<0>(), - reply.argumentAt<1>(), - QVersionNumber::fromString(reply.argumentAt<2>()), - QVersionNumber::fromString(reply.argumentAt<3>()), - }); - }); - } else { - LOG(("Native Notification Error: %1: %2") - .arg(reply.error().name()) - .arg(reply.error().message())); + const auto version = base::Platform::GlibVariantCast< + Glib::ustring>(reply.get_child(2)); - crl::on_main([=] { callback(std::nullopt); }); - } + const auto specVersion = base::Platform::GlibVariantCast< + Glib::ustring>(reply.get_child(3)); - call->deleteLater(); - }; + crl::on_main([=] { + callback(ServerInformation{ + QString::fromStdString(name), + QString::fromStdString(vendor), + QVersionNumber::fromString( + QString::fromStdString(version)), + QVersionNumber::fromString( + QString::fromStdString(specVersion)), + }); + }); - QObject::connect(watcher, &QDBusPendingCallWatcher::finished, finished); + return; + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } catch (const std::exception &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + + crl::on_main([=] { callback({}); }); + }, + std::string(kService)); + + return; + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + + crl::on_main([=] { callback(std::nullopt); }); } -void GetCapabilities(Fn callback) { - const auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("GetCapabilities")); +void GetCapabilities(Fn callback) { + try { + const auto connection = Gio::DBus::Connection::get_sync( + Gio::DBus::BusType::BUS_TYPE_SESSION); - const auto async = QDBusConnection::sessionBus().asyncCall(message); - auto watcher = new QDBusPendingCallWatcher(async); + connection->call( + std::string(kObjectPath), + std::string(kInterface), + "GetCapabilities", + {}, + [=](const Glib::RefPtr &result) { + try { + auto reply = connection->call_finish(result); - const auto finished = [=](QDBusPendingCallWatcher *call) { - const QDBusPendingReply reply = *call; + QStringList value; + ranges::transform( + base::Platform::GlibVariantCast< + std::vector>(reply.get_child(0)), + ranges::back_inserter(value), + QString::fromStdString); - if (reply.isValid()) { - crl::on_main([=] { callback(reply.value()); }); - } else { - LOG(("Native Notification Error: %1: %2") - .arg(reply.error().name()) - .arg(reply.error().message())); + crl::on_main([=] { + callback(value); + }); - crl::on_main([=] { callback({}); }); - } + return; + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } catch (const std::exception &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + + crl::on_main([=] { callback({}); }); + }, + std::string(kService)); - call->deleteLater(); - }; - - QObject::connect(watcher, &QDBusPendingCallWatcher::finished, finished); + return; + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + + crl::on_main([=] { callback({}); }); } void GetInhibitionSupported(Fn callback) { - auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kPropertiesInterface.utf16(), - qsl("Get")); + try { + const auto connection = Gio::DBus::Connection::get_sync( + Gio::DBus::BusType::BUS_TYPE_SESSION); - message.setArguments({ - kInterface.utf16(), - qsl("Inhibited") - }); + connection->call( + std::string(kObjectPath), + std::string(kPropertiesInterface), + "Get", + base::Platform::MakeGlibVariant(std::tuple{ + Glib::ustring(std::string(kInterface)), + Glib::ustring("Inhibited"), + }), + [=](const Glib::RefPtr &result) { + try { + connection->call_finish(result); - const auto async = QDBusConnection::sessionBus().asyncCall(message); - auto watcher = new QDBusPendingCallWatcher(async); + crl::on_main([=] { + callback(true); + }); - static const auto DontLogErrors = { - QDBusError::NoError, - QDBusError::InvalidArgs, - QDBusError::UnknownProperty, - }; + return; + } catch (const Glib::Error &e) { + static const auto DontLogErrors = { + "org.freedesktop.DBus.Error.InvalidArgs", + "org.freedesktop.DBus.Error.UnknownMethod", + }; - const auto finished = [=](QDBusPendingCallWatcher *call) { - const auto error = QDBusPendingReply(*call).error(); + const auto errorName = Gio::DBus::ErrorUtils::get_remote_error(e); + if (!ranges::contains(DontLogErrors, errorName)) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + } + + crl::on_main([=] { callback(false); }); + }, + std::string(kService)); - if (!ranges::contains(DontLogErrors, error.type())) { - LOG(("Native Notification Error: %1: %2") - .arg(error.name()) - .arg(error.message())); - } - - crl::on_main([=] { callback(!error.isValid()); }); - call->deleteLater(); - }; - - QObject::connect(watcher, &QDBusPendingCallWatcher::finished, finished); + return; + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } + + crl::on_main([=] { callback(false); }); } bool Inhibited() { @@ -186,28 +225,31 @@ bool Inhibited() { return false; } - auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kPropertiesInterface.utf16(), - qsl("Get")); + try { + const auto connection = Gio::DBus::Connection::get_sync( + Gio::DBus::BusType::BUS_TYPE_SESSION); - message.setArguments({ - kInterface.utf16(), - qsl("Inhibited") - }); + auto reply = connection->call_sync( + std::string(kObjectPath), + std::string(kPropertiesInterface), + "Get", + base::Platform::MakeGlibVariant(std::tuple{ + Glib::ustring(std::string(kInterface)), + Glib::ustring("Inhibited"), + }), + std::string(kService)); - const QDBusReply reply = QDBusConnection::sessionBus().call( - message); - - if (reply.isValid()) { - return reply.value().toBool(); + return base::Platform::GlibVariantCast( + base::Platform::GlibVariantCast( + reply.get_child(0))); + } catch (const Glib::Error &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); + } catch (const std::exception &e) { + LOG(("Native Notification Error: %1").arg( + QString::fromStdString(e.what()))); } - LOG(("Native Notification Error: %1: %2") - .arg(reply.error().name()) - .arg(reply.error().message())); - return false; } @@ -691,12 +733,12 @@ void Create(Window::Notifications::System *system) { } }; - GetServerInformation([=](std::optional result) { + GetServerInformation([=](const std::optional &result) { CurrentServerInformation = result; oneReady(); }); - GetCapabilities([=](QStringList result) { + GetCapabilities([=](const QStringList &result) { CurrentCapabilities = result; oneReady(); });