From 96cd55920ea5dfcfed899253dfea86b2701dd09a Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sun, 2 Jun 2019 14:03:11 +0000 Subject: [PATCH] Use notification hints from new Plasma notification system --- core/CMakeLists.txt | 1 + core/daemon.cpp | 3 + core/notificationserverinfo.cpp | 64 +++++++++++++++++++ core/notificationserverinfo.h | 49 ++++++++++++++ plugins/notifications/notification.cpp | 39 +++++++---- plugins/notifications/notification.h | 4 +- .../notificationsdbusinterface.cpp | 2 +- 7 files changed, 148 insertions(+), 14 deletions(-) create mode 100644 core/notificationserverinfo.cpp create mode 100644 core/notificationserverinfo.h diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 27584c695..836a140ac 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -37,6 +37,7 @@ set(kdeconnectcore_SRCS daemon.cpp device.cpp core_debug.cpp + notificationserverinfo.cpp ) add_library(kdeconnectcore ${kdeconnectcore_SRCS}) diff --git a/core/daemon.cpp b/core/daemon.cpp index e9755a55b..2ecf1526a 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -29,6 +29,7 @@ #include "core_debug.h" #include "kdeconnectconfig.h" #include "networkpacket.h" +#include "notificationserverinfo.h" #ifdef KDECONNECT_BLUETOOTH #include "backends/bluetooth/bluetoothlinkprovider.h" @@ -110,6 +111,8 @@ void Daemon::init() QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect")); QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents); + NotificationServerInfo::instance().init(); + qCDebug(KDECONNECT_CORE) << "Daemon started"; } diff --git a/core/notificationserverinfo.cpp b/core/notificationserverinfo.cpp new file mode 100644 index 000000000..d95ac3350 --- /dev/null +++ b/core/notificationserverinfo.cpp @@ -0,0 +1,64 @@ +/** + * Copyright 2019 Nicolas Fella + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "notificationserverinfo.h" + +#include +#include +#include +#include + +#include "core_debug.h" + +NotificationServerInfo& NotificationServerInfo::instance() +{ + static NotificationServerInfo instance; + return instance; +} + +void NotificationServerInfo::init() +{ + QDBusMessage query = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("GetCapabilities")); + + QDBusPendingReply reply = QDBusConnection::sessionBus().asyncCall(query); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); + connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, reply, watcher] { + watcher->deleteLater(); + + if (reply.isError()) { + qCWarning(KDECONNECT_CORE) << "Could not query capabilities from notifications server"; + return; + } + + if (reply.value().contains(QLatin1String("x-kde-display-appname"))) { + m_supportedHints |= X_KDE_DISPLAY_APPNAME; + } + + if (reply.value().contains(QLatin1String("x-kde-origin-name"))) { + m_supportedHints |= X_KDE_ORIGIN_NAME; + } + }); +} + +NotificationServerInfo::Hints NotificationServerInfo::supportedHints() +{ + return m_supportedHints; +} + diff --git a/core/notificationserverinfo.h b/core/notificationserverinfo.h new file mode 100644 index 000000000..a83d672f5 --- /dev/null +++ b/core/notificationserverinfo.h @@ -0,0 +1,49 @@ +/** + * Copyright 2019 Nicolas Fella + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "kdeconnectcore_export.h" +#include + +class KDECONNECTCORE_EXPORT NotificationServerInfo + : public QObject +{ + Q_OBJECT + +public: + enum Hint { + X_KDE_DISPLAY_APPNAME = 1, + X_KDE_ORIGIN_NAME = 2 + }; + + Q_DECLARE_FLAGS(Hints, Hint) + + static NotificationServerInfo& instance(); + + void init(); + + Hints supportedHints(); + +private: + Hints m_supportedHints; +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(NotificationServerInfo::Hints) diff --git a/plugins/notifications/notification.cpp b/plugins/notifications/notification.cpp index 52ef31c7e..52dfa8284 100644 --- a/plugins/notifications/notification.cpp +++ b/plugins/notifications/notification.cpp @@ -29,15 +29,17 @@ #include #include #include - +#include #include #include +#include QMap Notification::s_downloadsInProgress; -Notification::Notification(const NetworkPacket& np, QObject* parent) +Notification::Notification(const NetworkPacket& np, const Device* device, QObject* parent) : QObject(parent) + , m_device(device) { //Make a own directory for each user so noone can see each others icons QString username; @@ -102,20 +104,33 @@ void Notification::createKNotification(const NetworkPacket& np) QString escapedText = m_text.toHtmlEscaped(); QString escapedTicker = m_ticker.toHtmlEscaped(); - m_notification->setTitle(m_appName.toHtmlEscaped()); - - if (m_title.isEmpty() && m_text.isEmpty()) { - m_notification->setText(escapedTicker); - } else if (m_appName == m_title) { +#if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5, 57, 0) + if (NotificationServerInfo::instance().supportedHints().testFlag(NotificationServerInfo::X_KDE_DISPLAY_APPNAME)) { + m_notification->setTitle(escapedTitle); m_notification->setText(escapedText); - } else if (m_title.isEmpty()) { - m_notification->setText(escapedText); - } else if (m_text.isEmpty()) { - m_notification->setText(escapedTitle); + m_notification->setHint(QStringLiteral("x-kde-display-appname"), m_appName.toHtmlEscaped()); } else { - m_notification->setText(escapedTitle + ": " + escapedText); +#endif + m_notification->setTitle(m_appName.toHtmlEscaped()); + + if (m_title.isEmpty() && m_text.isEmpty()) { + m_notification->setText(escapedTicker); + } else if (m_appName == m_title) { + m_notification->setText(escapedText); + } else if (m_title.isEmpty()) { + m_notification->setText(escapedText); + } else if (m_text.isEmpty()) { + m_notification->setText(escapedTitle); + } else { + m_notification->setText(escapedTitle + ": " + escapedText); + } + +#if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5, 57, 0) } + m_notification->setHint(QStringLiteral("x-kde-origin-name"), m_device->name()); +#endif + m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty(); if (!m_hasIcon) { diff --git a/plugins/notifications/notification.h b/plugins/notifications/notification.h index 4971e146c..52dff4917 100644 --- a/plugins/notifications/notification.h +++ b/plugins/notifications/notification.h @@ -28,6 +28,7 @@ #include #include +#include class Notification : public QObject @@ -46,7 +47,7 @@ class Notification Q_PROPERTY(QString replyId READ replyId NOTIFY ready) public: - Notification(const NetworkPacket& np, QObject* parent); + Notification(const NetworkPacket& np, const Device* device, QObject* parent); ~Notification() override; QString internalId() const { return m_internalId; } @@ -90,6 +91,7 @@ private: QString m_payloadHash; bool m_ready; QStringList m_actions; + const Device* m_device; void parseNetworkPacket(const NetworkPacket& np); void loadIcon(const NetworkPacket& np); diff --git a/plugins/notifications/notificationsdbusinterface.cpp b/plugins/notifications/notificationsdbusinterface.cpp index 51af4e7e5..9b9e1c617 100644 --- a/plugins/notifications/notificationsdbusinterface.cpp +++ b/plugins/notifications/notificationsdbusinterface.cpp @@ -82,7 +82,7 @@ void NotificationsDbusInterface::processPacket(const NetworkPacket& np) Notification* noti = nullptr; if (!m_internalIdToPublicId.contains(id)) { - noti = new Notification(np, this); + noti = new Notification(np, m_plugin->device(), this); if (noti->isReady()) { addNotification(noti);