Move remaining usage of dbus to glibmm in notifications_manager_linux

This commit is contained in:
Ilya Fedin 2021-03-02 14:20:51 +04:00 committed by John Preston
parent b08c33cf8a
commit 37a8afaddf

View file

@ -20,13 +20,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include <QtCore/QVersionNumber> #include <QtCore/QVersionNumber>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusPendingCall>
#include <QtDBus/QDBusPendingCallWatcher>
#include <QtDBus/QDBusPendingReply>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusError>
#include <glibmm.h> #include <glibmm.h>
#include <giomm.h> #include <giomm.h>
@ -71,112 +64,158 @@ bool GetServiceRegistered() {
} }
void GetServerInformation( void GetServerInformation(
Fn<void(std::optional<ServerInformation>)> callback) { Fn<void(const std::optional<ServerInformation> &)> callback) {
using ServerInformationReply = QDBusPendingReply< try {
QString, const auto connection = Gio::DBus::Connection::get_sync(
QString, Gio::DBus::BusType::BUS_TYPE_SESSION);
QString,
QString>;
const auto message = QDBusMessage::createMethodCall( connection->call(
kService.utf16(), std::string(kObjectPath),
kObjectPath.utf16(), std::string(kInterface),
kInterface.utf16(), "GetServerInformation",
qsl("GetServerInformation")); {},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try {
auto reply = connection->call_finish(result);
const auto async = QDBusConnection::sessionBus().asyncCall(message); const auto name = base::Platform::GlibVariantCast<
auto watcher = new QDBusPendingCallWatcher(async); Glib::ustring>(reply.get_child(0));
const auto finished = [=](QDBusPendingCallWatcher *call) { const auto vendor = base::Platform::GlibVariantCast<
const ServerInformationReply reply = *call; Glib::ustring>(reply.get_child(1));
if (reply.isValid()) { const auto version = base::Platform::GlibVariantCast<
crl::on_main([=] { Glib::ustring>(reply.get_child(2));
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()));
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<void(QStringList)> callback) { void GetCapabilities(Fn<void(const QStringList &)> callback) {
const auto message = QDBusMessage::createMethodCall( try {
kService.utf16(), const auto connection = Gio::DBus::Connection::get_sync(
kObjectPath.utf16(), Gio::DBus::BusType::BUS_TYPE_SESSION);
kInterface.utf16(),
qsl("GetCapabilities"));
const auto async = QDBusConnection::sessionBus().asyncCall(message); connection->call(
auto watcher = new QDBusPendingCallWatcher(async); std::string(kObjectPath),
std::string(kInterface),
"GetCapabilities",
{},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try {
auto reply = connection->call_finish(result);
const auto finished = [=](QDBusPendingCallWatcher *call) { QStringList value;
const QDBusPendingReply<QStringList> reply = *call; ranges::transform(
base::Platform::GlibVariantCast<
std::vector<Glib::ustring>>(reply.get_child(0)),
ranges::back_inserter(value),
QString::fromStdString);
if (reply.isValid()) { crl::on_main([=] {
crl::on_main([=] { callback(reply.value()); }); callback(value);
} else { });
LOG(("Native Notification Error: %1: %2")
.arg(reply.error().name())
.arg(reply.error().message()));
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(); return;
}; } catch (const Glib::Error &e) {
LOG(("Native Notification Error: %1").arg(
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, finished); QString::fromStdString(e.what())));
}
crl::on_main([=] { callback({}); });
} }
void GetInhibitionSupported(Fn<void(bool)> callback) { void GetInhibitionSupported(Fn<void(bool)> callback) {
auto message = QDBusMessage::createMethodCall( try {
kService.utf16(), const auto connection = Gio::DBus::Connection::get_sync(
kObjectPath.utf16(), Gio::DBus::BusType::BUS_TYPE_SESSION);
kPropertiesInterface.utf16(),
qsl("Get"));
message.setArguments({ connection->call(
kInterface.utf16(), std::string(kObjectPath),
qsl("Inhibited") std::string(kPropertiesInterface),
}); "Get",
base::Platform::MakeGlibVariant(std::tuple{
Glib::ustring(std::string(kInterface)),
Glib::ustring("Inhibited"),
}),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try {
connection->call_finish(result);
const auto async = QDBusConnection::sessionBus().asyncCall(message); crl::on_main([=] {
auto watcher = new QDBusPendingCallWatcher(async); callback(true);
});
static const auto DontLogErrors = { return;
QDBusError::NoError, } catch (const Glib::Error &e) {
QDBusError::InvalidArgs, static const auto DontLogErrors = {
QDBusError::UnknownProperty, "org.freedesktop.DBus.Error.InvalidArgs",
}; "org.freedesktop.DBus.Error.UnknownMethod",
};
const auto finished = [=](QDBusPendingCallWatcher *call) { const auto errorName = Gio::DBus::ErrorUtils::get_remote_error(e);
const auto error = QDBusPendingReply<QVariant>(*call).error(); 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())) { return;
LOG(("Native Notification Error: %1: %2") } catch (const Glib::Error &e) {
.arg(error.name()) LOG(("Native Notification Error: %1").arg(
.arg(error.message())); QString::fromStdString(e.what())));
} }
crl::on_main([=] { callback(!error.isValid()); }); crl::on_main([=] { callback(false); });
call->deleteLater();
};
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, finished);
} }
bool Inhibited() { bool Inhibited() {
@ -186,28 +225,31 @@ bool Inhibited() {
return false; return false;
} }
auto message = QDBusMessage::createMethodCall( try {
kService.utf16(), const auto connection = Gio::DBus::Connection::get_sync(
kObjectPath.utf16(), Gio::DBus::BusType::BUS_TYPE_SESSION);
kPropertiesInterface.utf16(),
qsl("Get"));
message.setArguments({ auto reply = connection->call_sync(
kInterface.utf16(), std::string(kObjectPath),
qsl("Inhibited") std::string(kPropertiesInterface),
}); "Get",
base::Platform::MakeGlibVariant(std::tuple{
Glib::ustring(std::string(kInterface)),
Glib::ustring("Inhibited"),
}),
std::string(kService));
const QDBusReply<QVariant> reply = QDBusConnection::sessionBus().call( return base::Platform::GlibVariantCast<bool>(
message); base::Platform::GlibVariantCast<Glib::VariantBase>(
reply.get_child(0)));
if (reply.isValid()) { } catch (const Glib::Error &e) {
return reply.value().toBool(); 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; return false;
} }
@ -691,12 +733,12 @@ void Create(Window::Notifications::System *system) {
} }
}; };
GetServerInformation([=](std::optional<ServerInformation> result) { GetServerInformation([=](const std::optional<ServerInformation> &result) {
CurrentServerInformation = result; CurrentServerInformation = result;
oneReady(); oneReady();
}); });
GetCapabilities([=](QStringList result) { GetCapabilities([=](const QStringList &result) {
CurrentCapabilities = result; CurrentCapabilities = result;
oneReady(); oneReady();
}); });