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
This commit is contained in:
Nicolas Fella 2019-02-08 23:47:41 +01:00
parent e14b3dde32
commit 957975898b
6 changed files with 38 additions and 2 deletions

View file

@ -122,7 +122,8 @@
},
"X-KdeConnect-OutgoingPacketType": [
"kdeconnect.notification.request",
"kdeconnect.notification.reply"
"kdeconnect.notification.reply",
"kdeconnect.notification.action"
],
"X-KdeConnect-SupportedPacketType": [
"kdeconnect.notification"

View file

@ -22,6 +22,7 @@
#include "notification_debug.h"
#include <KNotification>
#include <QtGlobal>
#include <QIcon>
#include <QString>
#include <QUrl>
@ -29,6 +30,8 @@
#include <KLocalizedString>
#include <QFile>
#include <QJsonArray>
#include <core/filetransferjob.h>
QMap<QString, FileTransferJob*> Notification::s_downloadsInProgress;
@ -104,6 +107,15 @@ KNotification* Notification::createKNotification(const NetworkPacket& np)
m_notification->setText(escapedTitle+": "+escapedText);
}
connect(m_notification, QOverload<unsigned int>::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<bool>(QStringLiteral("silent"));
m_payloadHash = np.get<QString>(QStringLiteral("payloadHash"));
m_requestReplyId = np.get<QString>(QStringLiteral("requestReplyId"), QString());
m_actions.clear();
for (QJsonValue value : np.get<QJsonArray>(QStringLiteral("actions"))) {
m_actions.append(value.toString());
}
}

View file

@ -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);

View file

@ -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<QString>("key", key);
np.set<QString>("action", action);
m_plugin->sendPacket(np);
}
QString NotificationsDbusInterface::newId()
{
return QString::number(++m_lastId);

View file

@ -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);

View file

@ -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