From e4ac70090e2a0564b0bede53ce88525b511ea609 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Sat, 1 Aug 2020 11:03:17 +0000 Subject: [PATCH] Use GDBus to send notifications to avoid Q_OBJECT --- Telegram/CMakeLists.txt | 32 +- .../linux/notifications_manager_linux.cpp | 344 +++++++++++------- .../linux/notifications_manager_linux.h | 71 ---- 3 files changed, 234 insertions(+), 213 deletions(-) diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index b0e6dc4f0..7554fd08d 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -157,32 +157,38 @@ if (LINUX) endif() endif() +if (LINUX AND (NOT TDESKTOP_DISABLE_GTK_INTEGRATION OR NOT DESKTOP_APP_DISABLE_DBUS_INTEGRATION)) + find_package(PkgConfig REQUIRED) + pkg_check_modules(GLIB2 REQUIRED IMPORTED_TARGET glib-2.0) + pkg_check_modules(GOBJECT REQUIRED IMPORTED_TARGET gobject-2.0) + pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) + + target_link_libraries(Telegram + PRIVATE + PkgConfig::GLIB2 + PkgConfig::GOBJECT + PkgConfig::GIO + ) + + target_compile_options(Telegram PRIVATE -Wno-register) +endif() + if (LINUX AND NOT TDESKTOP_DISABLE_GTK_INTEGRATION) find_package(PkgConfig REQUIRED) - target_compile_options(Telegram PRIVATE -Wno-register) if (DESKTOP_APP_USE_PACKAGED AND NOT DESKTOP_APP_USE_PACKAGED_LAZY) - pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11) - pkg_check_modules(GOBJECT2 REQUIRED IMPORTED_TARGET gobject-2.0) - pkg_check_modules(GLIB2 REQUIRED IMPORTED_TARGET glib-2.0) pkg_check_modules(GTK3 REQUIRED IMPORTED_TARGET gtk+-3.0) + pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11) target_link_libraries(Telegram PRIVATE - PkgConfig::X11 - PkgConfig::GOBJECT2 - PkgConfig::GLIB2 PkgConfig::GTK3 + PkgConfig::X11 ) else() pkg_search_module(GTK REQUIRED gtk+-2.0 gtk+-3.0) - target_link_libraries(Telegram - PRIVATE - X11 - gobject-2.0 - glib-2.0 - ) target_include_directories(Telegram PRIVATE ${GTK_INCLUDE_DIRS}) + target_link_libraries(Telegram PRIVATE X11) endif() endif() diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 996dec475..b6343df49 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -19,10 +19,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION #include +#include #include #include #include -#include + +extern "C" { +#undef signals +#include +#define signals public +} // extern "C" #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION namespace Platform { @@ -31,10 +37,13 @@ namespace Notifications { #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION namespace { +constexpr auto kDBusTimeout = 30000; constexpr auto kService = "org.freedesktop.Notifications"_cs; constexpr auto kObjectPath = "/org/freedesktop/Notifications"_cs; constexpr auto kInterface = kService; constexpr auto kPropertiesInterface = "org.freedesktop.DBus.Properties"_cs; +constexpr auto kImageDataType = "(iiibii@ay)"_cs; +constexpr auto kNotifyArgsType = "(susssasa{sv}i)"_cs; bool NotificationsSupported = false; bool InhibitedNotSupported = false; @@ -53,7 +62,11 @@ void ComputeSupported(bool wait = false) { watcher, &QDBusPendingCallWatcher::finished, [=](QDBusPendingCallWatcher *call) { - QDBusPendingReply reply = *call; + QDBusPendingReply< + QString, + QString, + QString, + QString> reply = *call; if (reply.isValid()) { NotificationsSupported = true; @@ -203,7 +216,57 @@ QString GetImageKey(const QVersionNumber &specificationVersion) { return QString(); } -} // namespace +class NotificationData { +public: + using NotificationId = Window::Notifications::Manager::NotificationId; + + NotificationData( + const base::weak_ptr &manager, + const QString &title, + const QString &subtitle, + const QString &msg, + NotificationId id, + bool hideReplyButton); + + NotificationData(const NotificationData &other) = delete; + NotificationData &operator=(const NotificationData &other) = delete; + NotificationData(NotificationData &&other) = delete; + NotificationData &operator=(NotificationData &&other) = delete; + + ~NotificationData(); + + bool show(); + void close(); + void setImage(const QString &imagePath); + + void notificationClosed(uint id, uint reason); + void actionInvoked(uint id, const QString &actionName); + void notificationReplied(uint id, const QString &text); + +private: + GDBusProxy *_dbusProxy; + base::weak_ptr _manager; + + QString _title; + QString _body; + std::vector _actions; + base::flat_map _hints; + QString _imageKey; + QImage _image; + + uint _notificationId = 0; + NotificationId _id; + + static void signalEmitted( + GDBusProxy *proxy, + gchar *sender_name, + gchar *signal_name, + GVariant *parameters, + NotificationData *notificationData); + +}; + +using Notification = std::shared_ptr; NotificationData::NotificationData( const base::weak_ptr &manager, @@ -212,7 +275,15 @@ NotificationData::NotificationData( const QString &msg, NotificationId id, bool hideReplyButton) -: _dbusConnection(QDBusConnection::sessionBus()) +: _dbusProxy(g_dbus_proxy_new_for_bus_sync( + G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + nullptr, + kService.utf8(), + kObjectPath.utf8(), + kInterface.utf8(), + nullptr, + nullptr)) , _manager(manager) , _title(title) , _imageKey(GetImageKey(ParseSpecificationVersion( @@ -233,116 +304,134 @@ NotificationData::NotificationData( } if (capabilities.contains(qsl("actions"))) { - _actions << qsl("default") << QString(); + _actions.push_back(qsl("default")); + _actions.push_back(QString()); - _actions - << qsl("mail-mark-read") - << tr::lng_context_mark_read(tr::now); + _actions.push_back(qsl("mail-mark-read")); + _actions.push_back(tr::lng_context_mark_read(tr::now)); if (capabilities.contains(qsl("inline-reply")) && !hideReplyButton) { - _actions - << qsl("inline-reply") - << tr::lng_notification_reply(tr::now); - - _dbusConnection.connect( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("NotificationReplied"), - this, - SLOT(notificationReplied(uint,QString))); + _actions.push_back(qsl("inline-reply")); + _actions.push_back(tr::lng_notification_reply(tr::now)); } else { // icon name according to https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html - _actions - << qsl("mail-reply-sender") - << tr::lng_notification_reply(tr::now); + _actions.push_back(qsl("mail-reply-sender")); + _actions.push_back(tr::lng_notification_reply(tr::now)); } - - _dbusConnection.connect( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("ActionInvoked"), - this, - SLOT(actionInvoked(uint,QString))); } if (capabilities.contains(qsl("action-icons"))) { - _hints["action-icons"] = true; + _hints.emplace(qsl("action-icons"), g_variant_new_boolean(true)); } - // suppress system sound if telegram sound activated, otherwise use system sound + // suppress system sound if telegram sound activated, + // otherwise use system sound if (capabilities.contains(qsl("sound"))) { if (Core::App().settings().soundNotify()) { - _hints["suppress-sound"] = true; + _hints.emplace( + qsl("suppress-sound"), + g_variant_new_boolean(true)); } else { // sound name according to http://0pointer.de/public/sound-naming-spec.html - _hints["sound-name"] = qsl("message-new-instant"); + _hints.emplace( + qsl("sound-name"), + g_variant_new_string("message-new-instant")); } } if (capabilities.contains(qsl("x-canonical-append"))) { - _hints["x-canonical-append"] = qsl("true"); + _hints.emplace( + qsl("x-canonical-append"), + g_variant_new_string("true")); } - _hints["category"] = qsl("im.received"); - _hints["desktop-entry"] = GetLauncherBasename(); + _hints.emplace(qsl("category"), g_variant_new_string("im.received")); - _dbusConnection.connect( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("NotificationClosed"), - this, - SLOT(notificationClosed(uint))); + _hints.emplace( + qsl("desktop-entry"), + g_variant_new_string(GetLauncherBasename().toUtf8())); + + g_signal_connect(_dbusProxy, "g-signal", G_CALLBACK(signalEmitted), this); +} + +NotificationData::~NotificationData() { + g_object_unref(_dbusProxy); + + for (const auto &[key, value] : _hints) { + if (value) { + g_variant_unref(value); + } + } } bool NotificationData::show() { + GVariantBuilder actionsBuilder, hintsBuilder; + GError *error = nullptr; + + g_variant_builder_init(&actionsBuilder, G_VARIANT_TYPE("as")); + for (const auto &value : _actions) { + g_variant_builder_add( + &actionsBuilder, + "s", + value.toUtf8().constData()); + } + + g_variant_builder_init(&hintsBuilder, G_VARIANT_TYPE("a{sv}")); + for (auto &[key, value] : _hints) { + g_variant_builder_add( + &hintsBuilder, + "{sv}", + key.toUtf8().constData(), + value); + + value = nullptr; + } + const auto iconName = _imageKey.isEmpty() || !_hints.contains(_imageKey) ? GetIconName() : QString(); - auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("Notify")); + auto reply = g_dbus_proxy_call_sync( + _dbusProxy, + "Notify", + g_variant_new( + kNotifyArgsType.utf8(), + AppName.utf8().constData(), + 0, + iconName.toUtf8().constData(), + _title.toUtf8().constData(), + _body.toUtf8().constData(), + &actionsBuilder, + &hintsBuilder, + -1), + G_DBUS_CALL_FLAGS_NONE, + kDBusTimeout, + nullptr, + &error); - message.setArguments({ - AppName.utf16(), - uint(0), - iconName, - _title, - _body, - _actions, - _hints, - -1 - }); + const auto replyValid = !error; - const QDBusReply reply = _dbusConnection.call( - message); - - if (reply.isValid()) { - _notificationId = reply.value(); + if (replyValid) { + g_variant_get(reply, "(u)", &_notificationId); + g_variant_unref(reply); } else { - LOG(("Native notification error: %1").arg(reply.error().message())); + LOG(("Native notification error: %1").arg(error->message)); + g_error_free(error); } - return reply.isValid(); + return replyValid; } void NotificationData::close() { - auto message = QDBusMessage::createMethodCall( - kService.utf16(), - kObjectPath.utf16(), - kInterface.utf16(), - qsl("CloseNotification")); - - message.setArguments({ - _notificationId - }); - - _dbusConnection.send(message); + g_dbus_proxy_call( + _dbusProxy, + "CloseNotification", + g_variant_new("(u)", _notificationId), + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + nullptr, + nullptr); } void NotificationData::setImage(const QString &imagePath) { @@ -350,31 +439,60 @@ void NotificationData::setImage(const QString &imagePath) { return; } - const auto image = QImage(imagePath) - .convertToFormat(QImage::Format_RGBA8888); + _image = QImage(imagePath).convertToFormat(QImage::Format_RGBA8888); - const QByteArray imageBytes( - (const char*)image.constBits(), -#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) - image.byteCount()); -#else // Qt < 5.10.0 - image.sizeInBytes()); -#endif // Qt >= 5.10.0 - - const auto imageData = ImageData{ - image.width(), - image.height(), - image.bytesPerLine(), + _hints.emplace(_imageKey, g_variant_new( + kImageDataType.utf8(), + _image.width(), + _image.height(), + _image.bytesPerLine(), true, 8, 4, - imageBytes - }; - - _hints[_imageKey] = QVariant::fromValue(imageData); + g_variant_new_from_data( + G_VARIANT_TYPE("ay"), + _image.constBits(), +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + _image.byteCount(), +#else // Qt < 5.10.0 + _image.sizeInBytes(), +#endif // Qt >= 5.10.0 + true, + nullptr, + nullptr))); } -void NotificationData::notificationClosed(uint id) { +void NotificationData::signalEmitted( + GDBusProxy *proxy, + gchar *sender_name, + gchar *signal_name, + GVariant *parameters, + NotificationData *notificationData) { + if(signal_name == qstr("ActionInvoked")) { + guint32 id; + gchar *actionName; + g_variant_get(parameters, "(us)", &id, &actionName); + notificationData->actionInvoked(id, actionName); + g_free(actionName); + } + + if(signal_name == qstr("NotificationReplied")) { + guint32 id; + gchar *text; + g_variant_get(parameters, "(us)", &id, &text); + notificationData->notificationReplied(id, text); + g_free(text); + } + + if(signal_name == qstr("NotificationClosed")) { + guint32 id; + guint32 reason; + g_variant_get(parameters, "(uu)", &id, &reason); + notificationData->notificationClosed(id, reason); + } +} + +void NotificationData::notificationClosed(uint id, uint reason) { if (id == _notificationId) { const auto manager = _manager; const auto my = _id; @@ -415,37 +533,7 @@ void NotificationData::notificationReplied(uint id, const QString &text) { } } -QDBusArgument &operator<<( - QDBusArgument &argument, - const NotificationData::ImageData &imageData) { - argument.beginStructure(); - argument - << imageData.width - << imageData.height - << imageData.rowStride - << imageData.hasAlpha - << imageData.bitsPerSample - << imageData.channels - << imageData.data; - argument.endStructure(); - return argument; -} - -const QDBusArgument &operator>>( - const QDBusArgument &argument, - NotificationData::ImageData &imageData) { - argument.beginStructure(); - argument - >> imageData.width - >> imageData.height - >> imageData.rowStride - >> imageData.hasAlpha - >> imageData.bitsPerSample - >> imageData.channels - >> imageData.data; - argument.endStructure(); - return argument; -} +} // namespace #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION bool SkipAudio() { @@ -544,8 +632,6 @@ private: Manager::Private::Private(not_null manager, Type type) : _cachedUserpics(type) , _manager(manager) { - qDBusRegisterMetaType(); - if (!Supported()) { return; } diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h index a3a1c0ab5..2e4d1f4a0 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h @@ -10,76 +10,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/platform_notifications_manager.h" #include "base/weak_ptr.h" -#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION -#include -#include -#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION - namespace Platform { namespace Notifications { -#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION -class NotificationData : public QObject { - Q_OBJECT - -public: - using NotificationId = Window::Notifications::Manager::NotificationId; - - NotificationData( - const base::weak_ptr &manager, - const QString &title, - const QString &subtitle, - const QString &msg, - NotificationId id, - bool hideReplyButton); - - NotificationData(const NotificationData &other) = delete; - NotificationData &operator=(const NotificationData &other) = delete; - NotificationData(NotificationData &&other) = delete; - NotificationData &operator=(NotificationData &&other) = delete; - - bool show(); - void close(); - void setImage(const QString &imagePath); - - struct ImageData { - int width, height, rowStride; - bool hasAlpha; - int bitsPerSample, channels; - QByteArray data; - }; - -private: - QDBusConnection _dbusConnection; - base::weak_ptr _manager; - - QString _title; - QString _body; - QStringList _actions; - QVariantMap _hints; - QString _imageKey; - - uint _notificationId = 0; - NotificationId _id; - -private slots: - void notificationClosed(uint id); - void actionInvoked(uint id, const QString &actionName); - void notificationReplied(uint id, const QString &text); - -}; - -using Notification = std::shared_ptr; - -QDBusArgument &operator<<( - QDBusArgument &argument, - const NotificationData::ImageData &imageData); - -const QDBusArgument &operator>>( - const QDBusArgument &argument, - NotificationData::ImageData &imageData); -#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION - class Manager : public Window::Notifications::NativeManager , public base::has_weak_ptr { @@ -110,7 +43,3 @@ private: } // namespace Notifications } // namespace Platform - -#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION -Q_DECLARE_METATYPE(Platform::Notifications::NotificationData::ImageData) -#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION