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 @@ -89,7 +89,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 @@ -73,6 +73,7 @@ void dismissRequested(const QString& m_internalId); void replyRequested(); void ready(); + void actionTriggered(const QString& key, const QString& action); private: QString m_internalId; @@ -90,6 +91,7 @@ bool m_closed; 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; @@ -106,6 +109,11 @@ m_notification->setText(escapedTitle+": "+escapedText); } + m_notification->setActions(m_actions); + connect(m_notification, QOverload::of(&KNotification::activated), this, [this] (unsigned int actionIndex) { + Q_EMIT actionTriggered(m_internalId, m_actions[actionIndex]); + }); + m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty(); if (!m_hasIcon) { @@ -117,10 +125,12 @@ } if (!m_requestReplyId.isEmpty()) { - m_notification->setActions(QStringList(i18n("Reply"))); + m_actions.prepend(i18n("Reply")); connect(m_notification, &KNotification::action1Activated, this, &Notification::reply); } + + connect(m_notification, &KNotification::closed, this, &Notification::closed); return m_notification; @@ -188,4 +198,13 @@ m_silent = np.get(QStringLiteral("silent")); m_payloadHash = np.get(QStringLiteral("payloadHash")); m_requestReplyId = np.get(QStringLiteral("requestReplyId"), QString()); + + m_actions.clear(); + + qCDebug(KDECONNECT_PLUGIN_NOTIFICATION()) << np.get(QStringLiteral("actions")); + + 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 @@ -114,11 +114,13 @@ connect(noti, &Notification::dismissRequested, this, &NotificationsDbusInterface::dismissRequested); - - connect(noti, &Notification::replyRequested, this, [this,noti]{ - replyRequested(noti); + + connect(noti, &Notification::replyRequested, this, [this,noti]{ + replyRequested(noti); }); + connect(noti, &Notification::actionTriggered, this, &NotificationsDbusInterface::sendAction); + const QString& publicId = newId(); m_notifications[publicId] = noti; m_internalIdToPublicId[internalId] = publicId; @@ -183,6 +185,16 @@ 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); + qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Sent action" << action; + +} + 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