Add a method for creating simple notifications

Summary:
Add a helper function to the daemon interface, which must be subclassed
by the implementations, which creates a simple notification.

For more complex needs, involving interacting with the notification, it
will still be nescessary to use KNotification directly, but this allows
for other future implementations to create a simple notification without
ifdef'ing the code.

Reviewers: kdeconnect, apol

Reviewed By: apol

Subscribers: kdeconnect

Differential Revision: https://phabricator.kde.org/D11104
This commit is contained in:
Adam Pigg 2018-03-18 12:42:15 +01:00 committed by Matthijs Tijink
parent f7656cef2c
commit ebc316f703
10 changed files with 29 additions and 25 deletions

View file

@ -73,6 +73,8 @@ public Q_SLOTS:
Q_SCRIPTABLE QString deviceIdByName(const QString& name) const;
Q_SCRIPTABLE virtual void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) = 0;
Q_SIGNALS:
Q_SCRIPTABLE void deviceAdded(const QString& id);
Q_SCRIPTABLE void deviceRemoved(const QString& id); //Note that paired devices will never be removed

View file

@ -68,6 +68,17 @@ public:
return m_nam;
}
Q_SCRIPTABLE void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override
{
KNotification* notification = new KNotification(eventId); //KNotification::Persistent
notification->setIconName(iconName);
notification->setComponentName(QStringLiteral("kdeconnect"));
notification->setTitle(title);
notification->setText(text);
notification->sendEvent();
}
private:
QNetworkAccessManager* m_nam;
};

View file

@ -8,6 +8,6 @@ kdeconnect_add_plugin(kdeconnect_battery JSON kdeconnect_battery.json SOURCES ${
target_link_libraries(kdeconnect_battery
kdeconnectcore
Qt5::DBus
Qt5::Gui
KF5::I18n
KF5::Notifications
)

View file

@ -20,11 +20,12 @@
#include "batteryplugin.h"
#include <KNotification>
#include <QIcon>
#include <KLocalizedString>
#include <KPluginFactory>
#include <core/daemon.h>
#include "batterydbusinterface.h"
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_battery.json", registerPlugin< BatteryPlugin >(); )
@ -71,12 +72,8 @@ bool BatteryPlugin::receivePacket(const NetworkPacket& np)
}
if ( thresholdEvent == ThresholdBatteryLow && !isCharging ) {
KNotification* notification = new KNotification(QStringLiteral("batteryLow"));
notification->setIconName(QStringLiteral("battery-040"));
notification->setComponentName(QStringLiteral("kdeconnect"));
notification->setTitle(i18nc("device name: low battery", "%1: Low Battery", device()->name()));
notification->setText(i18n("Battery at %1%", currentCharge));
notification->sendEvent();
Daemon::instance()->sendSimpleNotification(QStringLiteral("batteryLow"), i18nc("device name: low battery", "%1: Low Battery", device()->name()), i18n("Battery at %1%", currentCharge), QStringLiteral("battery-040"));
}
return true;

View file

@ -21,8 +21,6 @@
#ifndef NOTIFICATIONSPLUGIN_H
#define NOTIFICATIONSPLUGIN_H
#include <knotification.h>
#include <core/kdeconnectplugin.h>
#define PACKET_TYPE_NOTIFICATION_REQUEST QStringLiteral("kdeconnect.notification.request")

View file

@ -8,5 +8,4 @@ target_link_libraries(kdeconnect_ping
kdeconnectcore
Qt5::DBus
KF5::I18n
KF5::Notifications
)

View file

@ -20,7 +20,6 @@
#include "pingplugin.h"
#include <KNotification>
#include <KLocalizedString>
#include <KPluginFactory>
@ -29,6 +28,7 @@
#include <QLoggingCategory>
#include <core/device.h>
#include <core/daemon.h>
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_ping.json", registerPlugin< PingPlugin >(); )
@ -47,12 +47,7 @@ PingPlugin::~PingPlugin()
bool PingPlugin::receivePacket(const NetworkPacket& np)
{
KNotification* notification = new KNotification(QStringLiteral("pingReceived")); //KNotification::Persistent
notification->setIconName(QStringLiteral("dialog-ok"));
notification->setComponentName(QStringLiteral("kdeconnect"));
notification->setTitle(device()->name());
notification->setText(np.get<QString>(QStringLiteral("message"),i18n("Ping!"))); //This can be a source of spam
notification->sendEvent();
Daemon::instance()->sendSimpleNotification(QStringLiteral("pingReceived"), device()->name(), np.get<QString>(QStringLiteral("message"),i18n("Ping!")), QStringLiteral("dialog-ok"));
return true;
}

View file

@ -26,8 +26,6 @@
#define PACKET_TYPE_SFTP_REQUEST QStringLiteral("kdeconnect.sftp.request")
class KNotification;
class SftpPlugin
: public KdeConnectPlugin
{

View file

@ -21,7 +21,6 @@
#ifndef SHAREPLUGIN_H
#define SHAREPLUGIN_H
#include <KNotification>
#include <KIO/Job>
#include <core/kdeconnectplugin.h>

View file

@ -50,6 +50,11 @@ public:
return m_nam;
}
Q_SCRIPTABLE virtual void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override
{
qDebug() << eventId << title << text << iconName;
}
private:
QNetworkAccessManager* m_nam;
};