From 6ce0b6b874e4f02625c61c7ffafc8ce986f40c0c Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Sat, 14 Jun 2014 20:35:00 +0200 Subject: [PATCH] Document in the desktop files the outgoing types for the plugins This way we narrow the things a plugin can send, to its own scope. Still every plugin can set a list of types, so it should be ok. --- core/device.cpp | 23 ++++++++++++------- core/device.h | 3 ++- core/kdeconnectplugin.cpp | 14 ++++++++++- core/kdeconnectplugin.h | 4 +++- core/pluginloader.cpp | 5 ++-- core/pluginloader.h | 3 ++- plugins/battery/batteryplugin.cpp | 2 +- plugins/battery/kdeconnect_battery.desktop | 1 + plugins/clipboard/clipboardplugin.cpp | 2 +- plugins/kdeconnect_plugin.desktop | 3 +++ .../kdeconnect_mpriscontrol.desktop | 1 + plugins/mpriscontrol/mpriscontrolplugin.cpp | 6 ++--- .../kdeconnect_notifications.desktop | 1 + .../notificationsdbusinterface.cpp | 9 ++++---- .../notificationsdbusinterface.h | 5 ++-- plugins/notifications/notificationsplugin.cpp | 4 ++-- plugins/ping/kdeconnect_ping.desktop | 1 + plugins/sftp/kdeconnect_sftp.desktop | 1 + plugins/sftp/mounter.cpp | 2 +- plugins/share/kdeconnect_share.desktop | 1 + plugins/share/shareplugin.cpp | 2 +- .../telephony/kdeconnect_telephony.desktop | 1 + 22 files changed, 65 insertions(+), 29 deletions(-) diff --git a/core/device.cpp b/core/device.cpp index 65c5809ed..429ab75d6 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -105,7 +105,8 @@ QStringList Device::loadedPlugins() const void Device::reloadPlugins() { QMap newPluginMap; - QMultiMap newPluginsByInterface; + QMultiMap newPluginsByIncomingInterface; + QMultiMap newPluginsByOutgoingInterface; if (isPaired() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices @@ -123,16 +124,21 @@ void Device::reloadPlugins() if (isPluginEnabled) { KdeConnectPlugin* plugin = m_plugins.take(pluginName); - QStringList interfaces; + QStringList incomingInterfaces, outgoingInterfaces; if (plugin) { - interfaces = m_pluginsByinterface.keys(plugin); + incomingInterfaces = m_pluginsByIncomingInterface.keys(plugin); + outgoingInterfaces = m_pluginsByOutgoingInterface.keys(plugin); } else { PluginData data = loader->instantiatePluginForDevice(pluginName, this); plugin = data.plugin; - interfaces = data.interfaces; + incomingInterfaces = data.incomingInterfaces; + outgoingInterfaces = data.outgoingInterfaces; } - foreach(const QString& interface, interfaces) { - newPluginsByInterface.insert(interface, plugin); + foreach(const QString& interface, incomingInterfaces) { + newPluginsByIncomingInterface.insert(interface, plugin); + } + foreach(const QString& interface, outgoingInterfaces) { + newPluginsByOutgoingInterface.insert(interface, plugin); } newPluginMap[pluginName] = plugin; } @@ -143,7 +149,8 @@ void Device::reloadPlugins() //them anymore, otherwise they would have been moved to the newPluginMap) qDeleteAll(m_plugins); m_plugins = newPluginMap; - m_pluginsByinterface = newPluginsByInterface; + m_pluginsByIncomingInterface = newPluginsByIncomingInterface; + m_pluginsByOutgoingInterface = newPluginsByOutgoingInterface; Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) { plugin->connected(); @@ -377,7 +384,7 @@ void Device::privateReceivedPackage(const NetworkPackage& np) } } else if (isPaired()) { - QList plugins = m_pluginsByinterface.values(np.type()); + QList plugins = m_pluginsByIncomingInterface.values(np.type()); foreach(KdeConnectPlugin* plugin, plugins) { plugin->receivePackage(np); } diff --git a/core/device.h b/core/device.h index 5ed10489d..bfff08518 100644 --- a/core/device.h +++ b/core/device.h @@ -132,7 +132,8 @@ private: QList m_deviceLinks; QMap m_plugins; - QMultiMap m_pluginsByinterface; + QMultiMap m_pluginsByIncomingInterface; + QMultiMap m_pluginsByOutgoingInterface; QTimer m_pairingTimeut; diff --git a/core/kdeconnectplugin.cpp b/core/kdeconnectplugin.cpp index 0288bf72c..620550b21 100644 --- a/core/kdeconnectplugin.cpp +++ b/core/kdeconnectplugin.cpp @@ -23,6 +23,7 @@ struct KdeConnectPluginPrivate { Device* mDevice; + QSet mOutgoingTypes; // The Initializer object sets things up, and also does cleanup when it goes out of scope // Since the plugins use their own memory, they need their own initializer in order to send encrypted packages @@ -34,13 +35,14 @@ KdeConnectPlugin::KdeConnectPlugin(QObject* parent, const QVariantList& args) , d(new KdeConnectPluginPrivate) { d->mDevice = qvariant_cast< Device* >(args.first()); + d->mOutgoingTypes = args.last().toStringList().toSet(); } KdeConnectPlugin::~KdeConnectPlugin() { } -Device* KdeConnectPlugin::device() +const Device* KdeConnectPlugin::device() { return d->mDevice; } @@ -49,3 +51,13 @@ Device const* KdeConnectPlugin::device() const { return d->mDevice; } + +bool KdeConnectPlugin::sendPackage(NetworkPackage& np) const +{ + if(!d->mOutgoingTypes.contains(np.type())) { + qWarning() << metaObject()->className() << "tried to send an unsupported package type" << np.type() << ". Supported:" << d->mOutgoingTypes; + return false; + } +// qWarning() << metaObject()->className() << "sends" << np.type() << ". Supported:" << d->mOutgoingTypes; + return d->mDevice->sendPackage(np); +} diff --git a/core/kdeconnectplugin.h b/core/kdeconnectplugin.h index 3753c9cae..d73491f5e 100644 --- a/core/kdeconnectplugin.h +++ b/core/kdeconnectplugin.h @@ -42,9 +42,11 @@ public: KdeConnectPlugin(QObject* parent, const QVariantList& args); virtual ~KdeConnectPlugin(); - Device* device(); + const Device* device(); Device const* device() const; + bool sendPackage(NetworkPackage& np) const; + public Q_SLOTS: /** * Returns true if it has handled the package in some way diff --git a/core/pluginloader.cpp b/core/pluginloader.cpp index b430c1516..1c10c0705 100644 --- a/core/pluginloader.cpp +++ b/core/pluginloader.cpp @@ -73,12 +73,13 @@ PluginData PluginLoader::instantiatePluginForDevice(const QString& name, Device* return ret; } - ret.interfaces = service->property("X-KdeConnect-SupportedPackageType", QVariant::StringList).toStringList(); + ret.incomingInterfaces = service->property("X-KdeConnect-SupportedPackageType", QVariant::StringList).toStringList(); + ret.outgoingInterfaces = service->property("X-KdeConnect-OutgoingPackageType", QVariant::StringList).toStringList(); QVariant deviceVariant = QVariant::fromValue(device); //FIXME any reason to use QObject in template param instead KdeConnectPlugin? - ret.plugin = factory->create(device, QVariantList() << deviceVariant); + ret.plugin = factory->create(device, QVariantList() << deviceVariant << ret.outgoingInterfaces); if (!ret.plugin) { kDebug(kdeconnect_kded()) << "Error loading plugin"; return ret; diff --git a/core/pluginloader.h b/core/pluginloader.h index 1a03259b8..4983730c7 100644 --- a/core/pluginloader.h +++ b/core/pluginloader.h @@ -36,7 +36,8 @@ struct PluginData { PluginData() : plugin(0) {} KdeConnectPlugin* plugin; - QStringList interfaces; + QStringList incomingInterfaces; + QStringList outgoingInterfaces; }; class PluginLoader diff --git a/plugins/battery/batteryplugin.cpp b/plugins/battery/batteryplugin.cpp index e1884535c..af3e47aaf 100644 --- a/plugins/battery/batteryplugin.cpp +++ b/plugins/battery/batteryplugin.cpp @@ -44,7 +44,7 @@ void BatteryPlugin::connected() { NetworkPackage np(PACKAGE_TYPE_BATTERY); np.set("request",true); - device()->sendPackage(np); + sendPackage(np); } BatteryPlugin::~BatteryPlugin() diff --git a/plugins/battery/kdeconnect_battery.desktop b/plugins/battery/kdeconnect_battery.desktop index 806d4d55e..06f4aabbc 100644 --- a/plugins/battery/kdeconnect_battery.desktop +++ b/plugins/battery/kdeconnect_battery.desktop @@ -58,3 +58,4 @@ Comment[uk]=Показ даних щодо рівня заряду акумул Comment[x-test]=xxShow your phone battery next to your computer batteryxx X-KdeConnect-SupportedPackageType=kdeconnect.battery +X-KdeConnect-OutgoingPackageType=kdeconnect.battery diff --git a/plugins/clipboard/clipboardplugin.cpp b/plugins/clipboard/clipboardplugin.cpp index 9acfa768c..bb31bb546 100644 --- a/plugins/clipboard/clipboardplugin.cpp +++ b/plugins/clipboard/clipboardplugin.cpp @@ -45,7 +45,7 @@ void ClipboardPlugin::clipboardChanged(QClipboard::Mode mode) //kDebug(kdeconnect_kded()) << "ClipboardChanged"; NetworkPackage np(PACKAGE_TYPE_CLIPBOARD); np.set("content",clipboard->text()); - device()->sendPackage(np); + sendPackage(np); } bool ClipboardPlugin::receivePackage(const NetworkPackage& np) diff --git a/plugins/kdeconnect_plugin.desktop b/plugins/kdeconnect_plugin.desktop index 5fe070ca6..017917b87 100644 --- a/plugins/kdeconnect_plugin.desktop +++ b/plugins/kdeconnect_plugin.desktop @@ -29,3 +29,6 @@ Name[x-test]=xxKDEConnect Pluginxx # mandatory, list of all the package types supported [PropertyDef::X-KdeConnect-SupportedPackageType] Type=QStringList + +[PropertyDef::X-KdeConnect-OutgoingPackageType] +Type=QStringList diff --git a/plugins/mpriscontrol/kdeconnect_mpriscontrol.desktop b/plugins/mpriscontrol/kdeconnect_mpriscontrol.desktop index adee287af..f7d3c24a1 100644 --- a/plugins/mpriscontrol/kdeconnect_mpriscontrol.desktop +++ b/plugins/mpriscontrol/kdeconnect_mpriscontrol.desktop @@ -57,3 +57,4 @@ Comment[uk]=Віддалене керування відтворенням му Comment[x-test]=xxRemote control your music and videosxx X-KdeConnect-SupportedPackageType=kdeconnect.mpris +X-KdeConnect-OutgoingPackageType=kdeconnect.mpris diff --git a/plugins/mpriscontrol/mpriscontrolplugin.cpp b/plugins/mpriscontrol/mpriscontrolplugin.cpp index 8be55738f..30a78c65e 100644 --- a/plugins/mpriscontrol/mpriscontrolplugin.cpp +++ b/plugins/mpriscontrol/mpriscontrolplugin.cpp @@ -125,7 +125,7 @@ void MprisControlPlugin::propertiesChanged(const QString& propertyInterface, con const QString& service = interface->service(); const QString& player = playerList.key(service); np.set("player", player); - device()->sendPackage(np); + sendPackage(np); } } @@ -202,7 +202,7 @@ bool MprisControlPlugin::receivePackage (const NetworkPackage& np) } if (somethingToSend) { answer.set("player", player); - device()->sendPackage(answer); + sendPackage(answer); } return true; @@ -213,5 +213,5 @@ void MprisControlPlugin::sendPlayerList() { NetworkPackage np(PACKAGE_TYPE_MPRIS); np.set("playerList",playerList.keys()); - device()->sendPackage(np); + sendPackage(np); } diff --git a/plugins/notifications/kdeconnect_notifications.desktop b/plugins/notifications/kdeconnect_notifications.desktop index 279049aef..3c11a2866 100644 --- a/plugins/notifications/kdeconnect_notifications.desktop +++ b/plugins/notifications/kdeconnect_notifications.desktop @@ -58,3 +58,4 @@ Comment[uk]=Показ сповіщень з телефону у KDE та під Comment[x-test]=xxShow phone notifications in KDE and keep them in syncxx X-KdeConnect-SupportedPackageType=kdeconnect.notification +X-KdeConnect-OutgoingPackageType=kdeconnect.notification diff --git a/plugins/notifications/notificationsdbusinterface.cpp b/plugins/notifications/notificationsdbusinterface.cpp index 25d1bc329..c5bdaeb74 100644 --- a/plugins/notifications/notificationsdbusinterface.cpp +++ b/plugins/notifications/notificationsdbusinterface.cpp @@ -30,9 +30,10 @@ #include #include "notificationsplugin.h" -NotificationsDbusInterface::NotificationsDbusInterface(Device* device, QObject *parent) - : QDBusAbstractAdaptor(parent) - , mDevice(device) +NotificationsDbusInterface::NotificationsDbusInterface(KdeConnectPlugin* plugin) + : QDBusAbstractAdaptor(plugin) + , mDevice(plugin->device()) + , mPlugin(plugin) , mLastId(0) , imagesDir(QDir::temp().absoluteFilePath("kdeconnect")) { @@ -134,7 +135,7 @@ void NotificationsDbusInterface::dismissRequested(Notification* notification) NetworkPackage np(PACKAGE_TYPE_NOTIFICATION); np.set("cancel", internalId); - mDevice->sendPackage(np); + mPlugin->sendPackage(np); //This should be called automatically back from server //removeNotification(internalId); diff --git a/plugins/notifications/notificationsdbusinterface.h b/plugins/notifications/notificationsdbusinterface.h index 019c02ac3..acd67368c 100644 --- a/plugins/notifications/notificationsdbusinterface.h +++ b/plugins/notifications/notificationsdbusinterface.h @@ -37,7 +37,7 @@ class NotificationsDbusInterface Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications") public: - explicit NotificationsDbusInterface(Device* device, QObject *parent); + explicit NotificationsDbusInterface(KdeConnectPlugin* plugin); virtual ~NotificationsDbusInterface(); void processPackage(const NetworkPackage& np); @@ -56,7 +56,8 @@ private /*methods*/: QString newId(); //Generates successive identifitiers to use as public ids private /*attributes*/: - Device* mDevice; + const Device* mDevice; + KdeConnectPlugin* mPlugin; QHash mNotifications; QHash mInternalIdToPublicId; int mLastId; diff --git a/plugins/notifications/notificationsplugin.cpp b/plugins/notifications/notificationsplugin.cpp index 0440584a1..67fa82530 100644 --- a/plugins/notifications/notificationsplugin.cpp +++ b/plugins/notifications/notificationsplugin.cpp @@ -31,14 +31,14 @@ K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_notifications", "kdeconnect NotificationsPlugin::NotificationsPlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) { - notificationsDbusInterface = new NotificationsDbusInterface(device(), parent); + notificationsDbusInterface = new NotificationsDbusInterface(this); } void NotificationsPlugin::connected() { NetworkPackage np(PACKAGE_TYPE_NOTIFICATION); np.set("request", true); - device()->sendPackage(np); + sendPackage(np); } NotificationsPlugin::~NotificationsPlugin() diff --git a/plugins/ping/kdeconnect_ping.desktop b/plugins/ping/kdeconnect_ping.desktop index 556ef3625..23bfe8ebf 100644 --- a/plugins/ping/kdeconnect_ping.desktop +++ b/plugins/ping/kdeconnect_ping.desktop @@ -59,3 +59,4 @@ Comment[uk]=Надсилання і отримання сигналів підт Comment[x-test]=xxSend and receive pingsxx X-KdeConnect-SupportedPackageType=kdeconnect.ping +# X-KdeConnect-OutgoingPackageType=kdeconnect.ping diff --git a/plugins/sftp/kdeconnect_sftp.desktop b/plugins/sftp/kdeconnect_sftp.desktop index ae1626294..84b9fda39 100644 --- a/plugins/sftp/kdeconnect_sftp.desktop +++ b/plugins/sftp/kdeconnect_sftp.desktop @@ -49,3 +49,4 @@ Comment[uk]=Перегляд файлових систем на сторонні Comment[x-test]=xxBrowse the remote device filesystem using SFTPxx X-KdeConnect-SupportedPackageType=kdeconnect.sftp +X-KdeConnect-OutgoingPackageType=kdeconnect.sftp diff --git a/plugins/sftp/mounter.cpp b/plugins/sftp/mounter.cpp index 8233399eb..de28e1200 100644 --- a/plugins/sftp/mounter.cpp +++ b/plugins/sftp/mounter.cpp @@ -231,7 +231,7 @@ void Mounter::start() np.set("start", true); np.set("id", m_id); np.set("idleTimeout", m_idleTimer.interval()); - m_sftp->device()->sendPackage(np); + m_sftp->sendPackage(np); m_connectTimer.start(); } diff --git a/plugins/share/kdeconnect_share.desktop b/plugins/share/kdeconnect_share.desktop index 9a4c539e6..6c82d6a63 100644 --- a/plugins/share/kdeconnect_share.desktop +++ b/plugins/share/kdeconnect_share.desktop @@ -50,3 +50,4 @@ Comment[uk]=Спрощене отримання і надсилання файл Comment[x-test]=xxReceive and send files, URLs or plain text easilyxx X-KdeConnect-SupportedPackageType=kdeconnect.share +X-KdeConnect-OutgoingPackageType=kdeconnect.share diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index 674657353..8af0ae1f8 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -154,7 +154,7 @@ void SharePlugin::shareUrl(const QUrl& url) } else { package.set("url", url.toString()); } - device()->sendPackage(package); + sendPackage(package); } void SharePlugin::connected() diff --git a/plugins/telephony/kdeconnect_telephony.desktop b/plugins/telephony/kdeconnect_telephony.desktop index 7bb6f9f0b..5df0cd398 100644 --- a/plugins/telephony/kdeconnect_telephony.desktop +++ b/plugins/telephony/kdeconnect_telephony.desktop @@ -58,3 +58,4 @@ Comment[uk]=Показ сповіщень щодо дзвінків і SMS (ск Comment[x-test]=xxShow notifications for calls and SMS (answering coming soon)xx X-KdeConnect-SupportedPackageType=kdeconnect.telephony +X-KdeConnect-OutgoingPackageType=