From 957975898bfc22b17ebfff1321f9c89f753a34c2 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Fri, 8 Feb 2019 23:47:41 +0100 Subject: [PATCH] Add actions to notifications Summary: When Android sends a list of possible actions add them to the notification. When the action is triggered send a package containing the id and the action back CCBUG: 366475 Test Plan: Send dummy notification, see the actions, trigger it, look for received package on Android Reviewers: #kde_connect, apol Reviewed By: #kde_connect, apol Subscribers: apol, kdeconnect, broulik, mtijink, #kde_connect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D12293 --- .../kdeconnect_notifications.json | 3 ++- plugins/notifications/notification.cpp | 22 ++++++++++++++++++- plugins/notifications/notification.h | 2 ++ .../notificationsdbusinterface.cpp | 10 +++++++++ .../notificationsdbusinterface.h | 1 + plugins/notifications/notificationsplugin.h | 2 ++ 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/plugins/notifications/kdeconnect_notifications.json b/plugins/notifications/kdeconnect_notifications.json index 8f746c6cf..048eabd8e 100644 --- a/plugins/notifications/kdeconnect_notifications.json +++ b/plugins/notifications/kdeconnect_notifications.json @@ -122,7 +122,8 @@ }, "X-KdeConnect-OutgoingPacketType": [ "kdeconnect.notification.request", - "kdeconnect.notification.reply" + "kdeconnect.notification.reply", + "kdeconnect.notification.action" ], "X-KdeConnect-SupportedPacketType": [ "kdeconnect.notification" diff --git a/plugins/notifications/notification.cpp b/plugins/notifications/notification.cpp index 4f498c9cf..797d737b8 100644 --- a/plugins/notifications/notification.cpp +++ b/plugins/notifications/notification.cpp @@ -22,6 +22,7 @@ #include "notification_debug.h" #include +#include #include #include #include @@ -29,6 +30,8 @@ #include #include +#include + #include QMap Notification::s_downloadsInProgress; @@ -104,6 +107,15 @@ KNotification* Notification::createKNotification(const NetworkPacket& np) m_notification->setText(escapedTitle+": "+escapedText); } + connect(m_notification, QOverload::of(&KNotification::activated), this, [this] (unsigned int actionIndex) { + // Do nothing for our own reply action + if(!m_requestReplyId.isEmpty() && actionIndex == 1) { + return; + } + // Notification action idices start at 1 + Q_EMIT actionTriggered(m_internalId, m_actions[actionIndex - 1]); + }); + m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty(); if (!m_hasIcon) { @@ -115,9 +127,10 @@ KNotification* Notification::createKNotification(const NetworkPacket& np) } if (!m_requestReplyId.isEmpty()) { - m_notification->setActions(QStringList(i18n("Reply"))); + m_actions.prepend(i18n("Reply")); connect(m_notification, &KNotification::action1Activated, this, &Notification::reply); } + m_notification->setActions(m_actions); return m_notification; } @@ -179,4 +192,11 @@ void Notification::parseNetworkPacket(const NetworkPacket& np) m_silent = np.get(QStringLiteral("silent")); m_payloadHash = np.get(QStringLiteral("payloadHash")); m_requestReplyId = np.get(QStringLiteral("requestReplyId"), QString()); + + m_actions.clear(); + + for (QJsonValue value : np.get(QStringLiteral("actions"))) { + m_actions.append(value.toString()); + } + } diff --git a/plugins/notifications/notification.h b/plugins/notifications/notification.h index 44b212dd8..e243db348 100644 --- a/plugins/notifications/notification.h +++ b/plugins/notifications/notification.h @@ -72,6 +72,7 @@ public Q_SLOTS: void dismissRequested(const QString& m_internalId); void replyRequested(); Q_SCRIPTABLE void ready(); + void actionTriggered(const QString& key, const QString& action); private: QString m_internalId; @@ -88,6 +89,7 @@ private: bool m_silent; QString m_payloadHash; bool m_ready; + QStringList m_actions; void parseNetworkPacket(const NetworkPacket& np); void loadIcon(const NetworkPacket& np); diff --git a/plugins/notifications/notificationsdbusinterface.cpp b/plugins/notifications/notificationsdbusinterface.cpp index 417149795..843b7881b 100644 --- a/plugins/notifications/notificationsdbusinterface.cpp +++ b/plugins/notifications/notificationsdbusinterface.cpp @@ -113,6 +113,8 @@ void NotificationsDbusInterface::addNotification(Notification* noti) replyRequested(noti); }); + connect(noti, &Notification::actionTriggered, this, &NotificationsDbusInterface::sendAction); + const QString& publicId = newId(); m_notifications[publicId] = noti; m_internalIdToPublicId[internalId] = publicId; @@ -177,6 +179,14 @@ void NotificationsDbusInterface::sendReply(const QString& replyId, const QString m_plugin->sendPacket(np); } +void NotificationsDbusInterface::sendAction(const QString& key, const QString& action) +{ + NetworkPacket np(PACKET_TYPE_NOTIFICATION_ACTION); + np.set("key", key); + np.set("action", action); + m_plugin->sendPacket(np); +} + QString NotificationsDbusInterface::newId() { return QString::number(++m_lastId); diff --git a/plugins/notifications/notificationsdbusinterface.h b/plugins/notifications/notificationsdbusinterface.h index 16943285b..682617222 100644 --- a/plugins/notifications/notificationsdbusinterface.h +++ b/plugins/notifications/notificationsdbusinterface.h @@ -52,6 +52,7 @@ public: public Q_SLOTS: Q_SCRIPTABLE QStringList activeNotifications(); Q_SCRIPTABLE void sendReply(const QString& replyId, const QString& message); + Q_SCRIPTABLE void sendAction(const QString& key, const QString& action); Q_SIGNALS: Q_SCRIPTABLE void notificationPosted(const QString& publicId); diff --git a/plugins/notifications/notificationsplugin.h b/plugins/notifications/notificationsplugin.h index e5890e60a..cfb34f215 100644 --- a/plugins/notifications/notificationsplugin.h +++ b/plugins/notifications/notificationsplugin.h @@ -25,6 +25,8 @@ #define PACKET_TYPE_NOTIFICATION_REQUEST QStringLiteral("kdeconnect.notification.request") #define PACKET_TYPE_NOTIFICATION_REPLY QStringLiteral("kdeconnect.notification.reply") +#define PACKET_TYPE_NOTIFICATION_ACTION QStringLiteral("kdeconnect.notification.action") + /* * This class is just a proxy for NotificationsDbusInterface