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.
This commit is contained in:
Aleix Pol 2014-06-14 20:35:00 +02:00
parent 9459001fbe
commit 6ce0b6b874
22 changed files with 65 additions and 29 deletions

View file

@ -105,7 +105,8 @@ QStringList Device::loadedPlugins() const
void Device::reloadPlugins()
{
QMap<QString, KdeConnectPlugin*> newPluginMap;
QMultiMap<QString, KdeConnectPlugin*> newPluginsByInterface;
QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingInterface;
QMultiMap<QString, KdeConnectPlugin*> 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<KdeConnectPlugin*> plugins = m_pluginsByinterface.values(np.type());
QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingInterface.values(np.type());
foreach(KdeConnectPlugin* plugin, plugins) {
plugin->receivePackage(np);
}

View file

@ -132,7 +132,8 @@ private:
QList<DeviceLink*> m_deviceLinks;
QMap<QString, KdeConnectPlugin*> m_plugins;
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByinterface;
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByIncomingInterface;
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByOutgoingInterface;
QTimer m_pairingTimeut;

View file

@ -23,6 +23,7 @@
struct KdeConnectPluginPrivate
{
Device* mDevice;
QSet<QString> 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);
}

View file

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

View file

@ -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*>(device);
//FIXME any reason to use QObject in template param instead KdeConnectPlugin?
ret.plugin = factory->create<KdeConnectPlugin>(device, QVariantList() << deviceVariant);
ret.plugin = factory->create<KdeConnectPlugin>(device, QVariantList() << deviceVariant << ret.outgoingInterfaces);
if (!ret.plugin) {
kDebug(kdeconnect_kded()) << "Error loading plugin";
return ret;

View file

@ -36,7 +36,8 @@ struct PluginData
{
PluginData() : plugin(0) {}
KdeConnectPlugin* plugin;
QStringList interfaces;
QStringList incomingInterfaces;
QStringList outgoingInterfaces;
};
class PluginLoader

View file

@ -44,7 +44,7 @@ void BatteryPlugin::connected()
{
NetworkPackage np(PACKAGE_TYPE_BATTERY);
np.set("request",true);
device()->sendPackage(np);
sendPackage(np);
}
BatteryPlugin::~BatteryPlugin()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -30,9 +30,10 @@
#include <core/filetransferjob.h>
#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<QString>("cancel", internalId);
mDevice->sendPackage(np);
mPlugin->sendPackage(np);
//This should be called automatically back from server
//removeNotification(internalId);

View file

@ -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<QString, Notification*> mNotifications;
QHash<QString, QString> mInternalIdToPublicId;
int mLastId;

View file

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

View file

@ -59,3 +59,4 @@ Comment[uk]=Надсилання і отримання сигналів підт
Comment[x-test]=xxSend and receive pingsxx
X-KdeConnect-SupportedPackageType=kdeconnect.ping
# X-KdeConnect-OutgoingPackageType=kdeconnect.ping

View file

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

View file

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

View file

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

View file

@ -154,7 +154,7 @@ void SharePlugin::shareUrl(const QUrl& url)
} else {
package.set<QString>("url", url.toString());
}
device()->sendPackage(package);
sendPackage(package);
}
void SharePlugin::connected()

View file

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