From ba5d9eda2d5e28227a15eb4b41deae91e8f5bbf9 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Tue, 10 Mar 2020 12:36:06 +0400 Subject: [PATCH] Minimize dbus requests --- .../linux/notifications_manager_linux.cpp | 55 +++++++++++++++---- .../platform/linux/specific_linux.cpp | 20 ++++++- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 8a34da6fe..6b9057437 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -31,7 +31,9 @@ constexpr auto kObjectPath = "/org/freedesktop/Notifications"_cs; constexpr auto kInterface = kService; constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs; -std::vector GetServerInformation() { +bool InhibitedNotSupported = false; + +std::vector ComputeServerInformation() { std::vector serverInformation; const auto message = QDBusMessage::createMethodCall( @@ -58,7 +60,12 @@ std::vector GetServerInformation() { return serverInformation; } -QStringList GetCapabilities() { +std::vector GetServerInformation() { + static const auto ServerInformation = ComputeServerInformation(); + return ServerInformation; +} + +QStringList ComputeCapabilities() { const auto message = QDBusMessage::createMethodCall( kService.utf16(), kObjectPath.utf16(), @@ -77,6 +84,11 @@ QStringList GetCapabilities() { return {}; } +QStringList GetCapabilities() { + static const auto Capabilities = ComputeCapabilities(); + return Capabilities; +} + bool Inhibited() { auto message = QDBusMessage::createMethodCall( kService.utf16(), @@ -92,9 +104,20 @@ bool Inhibited() { const QDBusReply reply = QDBusConnection::sessionBus().call( message); + constexpr auto notSupportedErrors = { + QDBusError::ServiceUnknown, + QDBusError::InvalidArgs, + }; + if (reply.isValid()) { return reply.value().toBool(); - } else if (reply.error().type() != QDBusError::InvalidArgs) { + } else if (ranges::contains(notSupportedErrors, reply.error().type())) { + InhibitedNotSupported = true; + } else { + if (reply.error().type() == QDBusError::AccessDenied) { + InhibitedNotSupported = true; + } + LOG(("Native notification error: %1").arg(reply.error().message())); } @@ -380,7 +403,9 @@ const QDBusArgument &operator>>( bool SkipAudio() { #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION - if (Supported()) { + if (Supported() + && GetCapabilities().contains(qsl("inhibitions")) + && !InhibitedNotSupported) { return Inhibited(); } #endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION @@ -390,7 +415,9 @@ bool SkipAudio() { bool SkipToast() { #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION - if (Supported()) { + if (Supported() + && GetCapabilities().contains(qsl("inhibitions")) + && !InhibitedNotSupported) { return Inhibited(); } #endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION @@ -427,14 +454,22 @@ Manager::Private::Private(not_null manager, Type type) const auto capabilities = GetCapabilities(); if (!serverInformation.empty()) { - LOG(("Notification daemon product name: %1").arg(serverInformation[0])); - LOG(("Notification daemon vendor name: %1").arg(serverInformation[1])); - LOG(("Notification daemon version: %1").arg(serverInformation[2])); - LOG(("Notification daemon specification version: %1").arg(serverInformation[3])); + LOG(("Notification daemon product name: %1") + .arg(serverInformation[0])); + + LOG(("Notification daemon vendor name: %1") + .arg(serverInformation[1])); + + LOG(("Notification daemon version: %1") + .arg(serverInformation[2])); + + LOG(("Notification daemon specification version: %1") + .arg(serverInformation[3])); } if (!capabilities.isEmpty()) { - LOG(("Notification daemon capabilities: %1").arg(capabilities.join(", "))); + LOG(("Notification daemon capabilities: %1") + .arg(capabilities.join(", "))); } } diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 7f3e22615..b9c5d3f0b 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -359,6 +359,12 @@ std::optional LastUserInputTime() { // TODO: a fallback pure-X11 implementation, this one covers only major DEs on X11 and Wayland // an example: https://stackoverflow.com/q/9049087 #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION + static auto NotSupported = false; + + if (NotSupported) { + return std::nullopt; + } + static const auto message = QDBusMessage::createMethodCall( qsl("org.freedesktop.ScreenSaver"), qsl("/org/freedesktop/ScreenSaver"), @@ -368,10 +374,20 @@ std::optional LastUserInputTime() { const QDBusReply reply = QDBusConnection::sessionBus().call( message); + constexpr auto notSupportedErrors = { + QDBusError::ServiceUnknown, + QDBusError::NotSupported, + }; + if (reply.isValid()) { return (crl::now() - static_cast(reply.value())); - } else if (reply.error().type() != QDBusError::ServiceUnknown - && reply.error().type() != QDBusError::NotSupported) { + } else if (ranges::contains(notSupportedErrors, reply.error().type())) { + NotSupported = true; + } else { + if (reply.error().type() == QDBusError::AccessDenied) { + NotSupported = true; + } + LOG(("Unable to get last user input time: %1: %2") .arg(reply.error().name()) .arg(reply.error().message()));