Move remaining usage of dbus to glibmm in specific_linux

This commit is contained in:
Ilya Fedin 2021-03-02 03:22:27 +04:00 committed by John Preston
parent 580a12ad7f
commit a2f8546033

View file

@ -44,22 +44,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtCore/QProcess> #include <QtCore/QProcess>
#include <QtGui/QWindow> #include <QtGui/QWindow>
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusReply>
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
#include <private/qguiapplication_p.h> #include <private/qguiapplication_p.h>
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <glibmm.h> #include <glibmm.h>
#include <giomm.h> #include <giomm.h>
// must be after undefs
#include "base/platform/linux/base_linux_glibmm_helper.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <cstdlib> #include <cstdlib>
@ -83,6 +73,8 @@ constexpr auto kHandlerTypeName = "x-scheme-handler/tg"_cs;
constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs; constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs;
constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs; constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs;
constexpr auto kXDGDesktopPortalKDEService = "org.freedesktop.impl.portal.desktop.kde"_cs;
constexpr auto kIBusPortalService = "org.freedesktop.portal.IBus"_cs;
constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs; constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs;
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
@ -206,17 +198,49 @@ PortalAutostart::PortalAutostart(bool start, bool silent) {
} }
bool IsXDGDesktopPortalPresent() { bool IsXDGDesktopPortalPresent() {
static const auto Result = QDBusInterface( static const auto Result = [&] {
kXDGDesktopPortalService.utf16(), try {
kXDGDesktopPortalObjectPath.utf16()).isValid(); const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION);
const auto serviceRegistered = base::Platform::DBus::NameHasOwner(
connection,
std::string(kXDGDesktopPortalService));
const auto serviceActivatable = ranges::contains(
base::Platform::DBus::ListActivatableNames(connection),
Glib::ustring(std::string(kXDGDesktopPortalService)));
return serviceRegistered || serviceActivatable;
} catch (...) {
}
return false;
}();
return Result; return Result;
} }
bool IsXDGDesktopPortalKDEPresent() { bool IsXDGDesktopPortalKDEPresent() {
static const auto Result = QDBusInterface( static const auto Result = [&] {
qsl("org.freedesktop.impl.portal.desktop.kde"), try {
kXDGDesktopPortalObjectPath.utf16()).isValid(); const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION);
const auto serviceRegistered = base::Platform::DBus::NameHasOwner(
connection,
std::string(kXDGDesktopPortalKDEService));
const auto serviceActivatable = ranges::contains(
base::Platform::DBus::ListActivatableNames(connection),
Glib::ustring(std::string(kXDGDesktopPortalKDEService)));
return serviceRegistered || serviceActivatable;
} catch (...) {
}
return false;
}();
return Result; return Result;
} }
@ -229,11 +253,11 @@ bool IsIBusPortalPresent() {
const auto serviceRegistered = base::Platform::DBus::NameHasOwner( const auto serviceRegistered = base::Platform::DBus::NameHasOwner(
connection, connection,
"org.freedesktop.portal.IBus"); std::string(kIBusPortalService));
const auto serviceActivatable = ranges::contains( const auto serviceActivatable = ranges::contains(
base::Platform::DBus::ListActivatableNames(connection), base::Platform::DBus::ListActivatableNames(connection),
"org.freedesktop.portal.IBus"); Glib::ustring(std::string(kIBusPortalService)));
return serviceRegistered || serviceActivatable; return serviceRegistered || serviceActivatable;
} catch (...) { } catch (...) {
@ -247,28 +271,31 @@ bool IsIBusPortalPresent() {
uint FileChooserPortalVersion() { uint FileChooserPortalVersion() {
static const auto Result = [&]() -> uint { static const auto Result = [&]() -> uint {
auto message = QDBusMessage::createMethodCall( try {
kXDGDesktopPortalService.utf16(), const auto connection = Gio::DBus::Connection::get_sync(
kXDGDesktopPortalObjectPath.utf16(), Gio::DBus::BusType::BUS_TYPE_SESSION);
kPropertiesInterface.utf16(),
qsl("Get"));
message.setArguments({ auto reply = connection->call_sync(
qsl("org.freedesktop.portal.FileChooser"), std::string(kXDGDesktopPortalObjectPath),
qsl("version") std::string(kPropertiesInterface),
}); "Get",
base::Platform::MakeGlibVariant(std::tuple{
Glib::ustring("org.freedesktop.portal.FileChooser"),
Glib::ustring("version"),
}),
std::string(kXDGDesktopPortalService));
const QDBusReply<QVariant> reply = QDBusConnection::sessionBus().call( return base::Platform::GlibVariantCast<uint>(
message); base::Platform::GlibVariantCast<Glib::VariantBase>(
reply.get_child(0)));
if (reply.isValid()) { } catch (const Glib::Error &e) {
return reply.value().toUInt(); LOG(("Error getting FileChooser portal version: %1")
.arg(QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Error getting FileChooser portal version: %1")
.arg(QString::fromStdString(e.what())));
} }
LOG(("Error getting FileChooser portal version: %1: %2")
.arg(reply.error().name())
.arg(reply.error().message()));
return 0; return 0;
}(); }();