diff --git a/plugins/notifications/kdeconnect_notifications.json b/plugins/notifications/kdeconnect_notifications.json --- 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.h b/plugins/notifications/notification.h --- a/plugins/notifications/notification.h +++ b/plugins/notifications/notification.h @@ -72,6 +72,7 @@ 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 @@ 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/notification.cpp b/plugins/notifications/notification.cpp --- a/plugins/notifications/notification.cpp +++ b/plugins/notifications/notification.cpp @@ -22,13 +22,16 @@ #include "notification_debug.h" #include +#include #include #include #include #include #include #include +#include + #include QMap Notification::s_downloadsInProgress; @@ -104,6 +107,15 @@ 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 @@ } 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 @@ 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/notificationsdbusinterface.h b/plugins/notifications/notificationsdbusinterface.h --- a/plugins/notifications/notificationsdbusinterface.h +++ b/plugins/notifications/notificationsdbusinterface.h @@ -52,6 +52,7 @@ 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/notificationsdbusinterface.cpp b/plugins/notifications/notificationsdbusinterface.cpp --- a/plugins/notifications/notificationsdbusinterface.cpp +++ b/plugins/notifications/notificationsdbusinterface.cpp @@ -113,6 +113,8 @@ 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 @@ 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/notificationsplugin.h b/plugins/notifications/notificationsplugin.h --- 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