Commit 4439e7c6 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

[plugins/notifications] Merge notificationsdbusinterface and notificationsplugin

We can just expose the plugin itself to DBus like we do for all other plugins.
This way we don't need a separate adaptor class any more.
parent 1060c1a8
Pipeline #38054 passed with stage
in 13 minutes and 38 seconds
......@@ -42,7 +42,7 @@ geninterface(${PROJECT_SOURCE_DIR}/core/daemon.h daemoninterface)
geninterface(${PROJECT_SOURCE_DIR}/core/device.h deviceinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/battery/batteryplugin.h batteryinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/sftp/sftpplugin.h devicesftpinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/notifications/notificationsdbusinterface.h devicenotificationsinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/notifications/notificationsplugin.h devicenotificationsinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/findmyphone/findmyphoneplugin.h devicefindmyphoneinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/notifications/notification.h notificationinterface)
geninterface(${PROJECT_SOURCE_DIR}/plugins/mprisremote/mprisremoteplugin.h mprisremoteinterface)
......
......@@ -8,7 +8,6 @@ ecm_qt_declare_logging_category(
set(kdeconnect_notifications_SRCS
notification.cpp
notificationsplugin.cpp
notificationsdbusinterface.cpp
sendreplydialog.cpp
${debug_file_SRCS}
)
......
/**
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "notificationsdbusinterface.h"
#include "plugin_notification_debug.h"
#include "notification.h"
#include <core/device.h>
#include <core/kdeconnectplugin.h>
#include <dbushelper.h>
#include "notificationsplugin.h"
#include "sendreplydialog.h"
//In older Qt released, qAsConst isnt available
#include "qtcompat_p.h"
NotificationsDbusInterface::NotificationsDbusInterface(KdeConnectPlugin* plugin)
: QDBusAbstractAdaptor(const_cast<Device*>(plugin->device()))
, m_device(plugin->device())
, m_plugin(plugin)
, m_lastId(0)
{
}
NotificationsDbusInterface::~NotificationsDbusInterface()
{
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Destroying NotificationsDbusInterface";
}
void NotificationsDbusInterface::clearNotifications()
{
qDeleteAll(m_notifications);
m_notifications.clear();
Q_EMIT allNotificationsRemoved();
}
QStringList NotificationsDbusInterface::activeNotifications()
{
return m_notifications.keys();
}
void NotificationsDbusInterface::notificationReady()
{
Notification* noti = static_cast<Notification*>(sender());
disconnect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady);
addNotification(noti);
}
void NotificationsDbusInterface::processPacket(const NetworkPacket& np)
{
if (np.get<bool>(QStringLiteral("isCancel"))) {
QString id = np.get<QString>(QStringLiteral("id"));
// cut off kdeconnect-android's prefix if there:
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
removeNotification(id);
return;
}
QString id = np.get<QString>(QStringLiteral("id"));
Notification* noti = nullptr;
if (!m_internalIdToPublicId.contains(id)) {
noti = new Notification(np, m_plugin->device(), this);
if (noti->isReady()) {
addNotification(noti);
} else {
connect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady);
}
} else {
QString pubId = m_internalIdToPublicId.value(id);
noti = m_notifications.value(pubId);
noti->update(np);
}
}
void NotificationsDbusInterface::addNotification(Notification* noti)
{
const QString& internalId = noti->internalId();
if (m_internalIdToPublicId.contains(internalId)) {
removeNotification(internalId);
}
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "addNotification" << internalId;
connect(noti, &Notification::dismissRequested,
this, &NotificationsDbusInterface::dismissRequested);
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;
DBusHelper::sessionBus().registerObject(m_device->dbusPath() + QStringLiteral("/notifications/") + publicId, noti, QDBusConnection::ExportScriptableContents);
Q_EMIT notificationPosted(publicId);
}
void NotificationsDbusInterface::removeNotification(const QString& internalId)
{
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "removeNotification" << internalId;
if (!m_internalIdToPublicId.contains(internalId)) {
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by internal Id: " << internalId;
return;
}
QString publicId = m_internalIdToPublicId.take(internalId);
Notification* noti = m_notifications.take(publicId);
if (!noti) {
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by public Id: " << publicId;
return;
}
//Deleting the notification will unregister it automatically
//DBusHelper::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId);
noti->deleteLater();
Q_EMIT notificationRemoved(publicId);
}
void NotificationsDbusInterface::dismissRequested(const QString& internalId)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REQUEST);
np.set<QString>(QStringLiteral("cancel"), internalId);
m_plugin->sendPacket(np);
//Workaround: we erase notifications without waiting a response from the
//phone because we won't receive a response if we are out of sync and this
//notification no longer exists. Ideally, each time we reach the phone
//after some time disconnected we should re-sync all the notifications.
removeNotification(internalId);
}
void NotificationsDbusInterface::replyRequested(Notification* noti)
{
QString replyId = noti->replyId();
QString appName = noti->appName();
QString originalMessage = noti->ticker();
SendReplyDialog* dialog = new SendReplyDialog(originalMessage, replyId, appName);
connect(dialog, &SendReplyDialog::sendReply, this, &NotificationsDbusInterface::sendReply);
dialog->show();
dialog->raise();
}
void NotificationsDbusInterface::sendReply(const QString& replyId, const QString& message)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REPLY);
np.set<QString>(QStringLiteral("requestReplyId"), replyId);
np.set<QString>(QStringLiteral("message"), message);
m_plugin->sendPacket(np);
}
void NotificationsDbusInterface::sendAction(const QString& key, const QString& action)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_ACTION);
np.set<QString>(QStringLiteral("key"), key);
np.set<QString>(QStringLiteral("action"), action);
m_plugin->sendPacket(np);
}
QString NotificationsDbusInterface::newId()
{
return QString::number(++m_lastId);
}
/**
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef NOTIFICATIONSDBUSINTERFACE_H
#define NOTIFICATIONSDBUSINTERFACE_H
#include <QDBusAbstractAdaptor>
#include <QHash>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QPointer>
#include "notification.h"
class KdeConnectPlugin;
class Device;
class NotificationsDbusInterface
: public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications")
public:
explicit NotificationsDbusInterface(KdeConnectPlugin* plugin);
~NotificationsDbusInterface() override;
void processPacket(const NetworkPacket& np);
void clearNotifications();
void dismissRequested(const QString& notification);
void replyRequested(Notification* noti);
void addNotification(Notification* noti);
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);
Q_SCRIPTABLE void notificationRemoved(const QString& publicId);
Q_SCRIPTABLE void notificationUpdated(const QString& publicId);
Q_SCRIPTABLE void allNotificationsRemoved();
private /*methods*/:
void removeNotification(const QString& internalId);
QString newId(); //Generates successive identifiers to use as public ids
void notificationReady();
private /*attributes*/:
const Device* m_device;
KdeConnectPlugin* m_plugin;
QHash<QString, QPointer<Notification>> m_notifications;
QHash<QString, QString> m_internalIdToPublicId;
int m_lastId;
};
#endif
......@@ -6,8 +6,9 @@
#include "notificationsplugin.h"
#include "notificationsdbusinterface.h"
#include "plugin_notification_debug.h"
#include "sendreplydialog.h"
#include <dbushelper.h>
#include <KPluginFactory>
......@@ -16,7 +17,6 @@ K_PLUGIN_CLASS_WITH_JSON(NotificationsPlugin, "kdeconnect_notifications.json")
NotificationsPlugin::NotificationsPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
{
notificationsDbusInterface = new NotificationsDbusInterface(this);
}
void NotificationsPlugin::connected()
......@@ -25,24 +25,157 @@ void NotificationsPlugin::connected()
sendPacket(np);
}
NotificationsPlugin::~NotificationsPlugin()
{
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Destroying NotificationsPlugin";
//FIXME: Qt dbus does not allow to remove an adaptor! (it causes a crash in
// the next dbus access to its parent). The implication of not deleting this
// is that disabling the plugin leaks the interface. As a mitigation we are
// cleaning up the notifications inside the adaptor here.
//notificationsDbusInterface->deleteLater();
notificationsDbusInterface->clearNotifications();
}
bool NotificationsPlugin::receivePacket(const NetworkPacket& np)
{
if (np.get<bool>(QStringLiteral("request"))) return false;
notificationsDbusInterface->processPacket(np);
if (np.get<bool>(QStringLiteral("isCancel"))) {
QString id = np.get<QString>(QStringLiteral("id"));
// cut off kdeconnect-android's prefix if there:
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
removeNotification(id);
return true;
}
QString id = np.get<QString>(QStringLiteral("id"));
Notification* noti = nullptr;
if (!m_internalIdToPublicId.contains(id)) {
noti = new Notification(np, device(), this);
if (noti->isReady()) {
addNotification(noti);
} else {
connect(noti, &Notification::ready, this, &NotificationsPlugin::notificationReady);
}
} else {
QString pubId = m_internalIdToPublicId.value(id);
noti = m_notifications.value(pubId);
noti->update(np);
}
return true;
}
void NotificationsPlugin::clearNotifications()
{
qDeleteAll(m_notifications);
m_notifications.clear();
Q_EMIT allNotificationsRemoved();
}
QStringList NotificationsPlugin::activeNotifications()
{
return m_notifications.keys();
}
void NotificationsPlugin::notificationReady()
{
Notification* noti = static_cast<Notification*>(sender());
disconnect(noti, &Notification::ready, this, &NotificationsPlugin::notificationReady);
addNotification(noti);
}
void NotificationsPlugin::addNotification(Notification* noti)
{
const QString& internalId = noti->internalId();
if (m_internalIdToPublicId.contains(internalId)) {
removeNotification(internalId);
}
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "addNotification" << internalId;
connect(noti, &Notification::dismissRequested,
this, &NotificationsPlugin::dismissRequested);
connect(noti, &Notification::replyRequested, this, [this,noti]{
replyRequested(noti);
});
connect(noti, &Notification::actionTriggered, this, &NotificationsPlugin::sendAction);
const QString& publicId = newId();
m_notifications[publicId] = noti;
m_internalIdToPublicId[internalId] = publicId;
DBusHelper::sessionBus().registerObject(device()->dbusPath() + QStringLiteral("/notifications/") + publicId, noti, QDBusConnection::ExportScriptableContents);
Q_EMIT notificationPosted(publicId);
}
void NotificationsPlugin::removeNotification(const QString& internalId)
{
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "removeNotification" << internalId;
if (!m_internalIdToPublicId.contains(internalId)) {
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by internal Id: " << internalId;
return;
}
QString publicId = m_internalIdToPublicId.take(internalId);
Notification* noti = m_notifications.take(publicId);
if (!noti) {
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by public Id: " << publicId;
return;
}
//Deleting the notification will unregister it automatically
noti->deleteLater();
Q_EMIT notificationRemoved(publicId);
}
void NotificationsPlugin::dismissRequested(const QString& internalId)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REQUEST);
np.set<QString>(QStringLiteral("cancel"), internalId);
sendPacket(np);
//Workaround: we erase notifications without waiting a response from the
//phone because we won't receive a response if we are out of sync and this
//notification no longer exists. Ideally, each time we reach the phone
//after some time disconnected we should re-sync all the notifications.
removeNotification(internalId);
}
void NotificationsPlugin::replyRequested(Notification* noti)
{
QString replyId = noti->replyId();
QString appName = noti->appName();
QString originalMessage = noti->ticker();
SendReplyDialog* dialog = new SendReplyDialog(originalMessage, replyId, appName);
connect(dialog, &SendReplyDialog::sendReply, this, &NotificationsPlugin::sendReply);
dialog->show();
dialog->raise();
}
void NotificationsPlugin::sendReply(const QString& replyId, const QString& message)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REPLY);
np.set<QString>(QStringLiteral("requestReplyId"), replyId);
np.set<QString>(QStringLiteral("message"), message);
sendPacket(np);
}
void NotificationsPlugin::sendAction(const QString& key, const QString& action)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_ACTION);
np.set<QString>(QStringLiteral("key"), key);
np.set<QString>(QStringLiteral("action"), action);
sendPacket(np);
}
QString NotificationsPlugin::newId()
{
return QString::number(++m_lastId);
}
QString NotificationsPlugin::dbusPath() const
{
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/notifications");
}
#include "notificationsplugin.moc"
......@@ -9,32 +9,49 @@
#include <core/kdeconnectplugin.h>
#include "notification.h"
#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
* because it can not inherit from QDBusAbstractAdaptor and
* KdeConnectPlugin at the same time (both are QObject)
*/
class NotificationsDbusInterface;
class NotificationsPlugin
: public KdeConnectPlugin
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications")
public:
explicit NotificationsPlugin(QObject* parent, const QVariantList& args);
~NotificationsPlugin() override;
bool receivePacket(const NetworkPacket& np) override;
void connected() override;
protected:
NotificationsDbusInterface* notificationsDbusInterface;
QString dbusPath() const override;
void clearNotifications();
void dismissRequested(const QString& notification);
void replyRequested(Notification* noti);
void addNotification(Notification* noti);
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);
Q_SCRIPTABLE void notificationRemoved(const QString& publicId);
Q_SCRIPTABLE void notificationUpdated(const QString& publicId);
Q_SCRIPTABLE void allNotificationsRemoved();
private:
void removeNotification(const QString& internalId);
QString newId(); //Generates successive identifiers to use as public ids
void notificationReady();
QHash<QString, QPointer<Notification>> m_notifications;
QHash<QString, QString> m_internalIdToPublicId;
int m_lastId = 0;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment