2013-08-12 15:09:52 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-08-12 15:09:52 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-08-12 15:09:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pluginloader.h"
|
|
|
|
|
2015-04-10 16:30:39 +01:00
|
|
|
#include <KPluginFactory>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KPluginMetaData>
|
|
|
|
#include <QPluginLoader>
|
2020-03-01 20:00:40 +00:00
|
|
|
#include <QStaticPlugin>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QVector>
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2014-06-14 15:34:00 +01:00
|
|
|
#include "device.h"
|
2023-07-16 15:20:34 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "kdeconnectplugin.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
PluginLoader *PluginLoader::instance()
|
2013-08-12 15:09:52 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
static PluginLoader *instance = new PluginLoader();
|
2013-08-13 04:07:32 +01:00
|
|
|
return instance;
|
2013-08-12 15:09:52 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
PluginLoader::PluginLoader()
|
2013-08-12 15:09:52 +01:00
|
|
|
{
|
2023-07-26 09:52:38 +01:00
|
|
|
const QVector<KPluginMetaData> data = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect"));
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const KPluginMetaData &metadata : data) {
|
2015-03-19 15:36:53 +00:00
|
|
|
plugins[metadata.pluginId()] = metadata;
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2013-10-12 00:54:08 +01:00
|
|
|
QStringList PluginLoader::getPluginList() const
|
2013-08-13 04:07:32 +01:00
|
|
|
{
|
|
|
|
return plugins.keys();
|
|
|
|
}
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
bool PluginLoader::doesPluginExist(const QString &name) const
|
|
|
|
{
|
|
|
|
return plugins.contains(name);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KPluginMetaData PluginLoader::getPluginInfo(const QString &name) const
|
2013-10-12 00:54:08 +01:00
|
|
|
{
|
2015-03-19 15:36:53 +00:00
|
|
|
return plugins.value(name);
|
2013-08-13 05:03:12 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectPlugin *PluginLoader::instantiatePluginForDevice(const QString &pluginName, Device *device) const
|
2013-10-12 00:54:08 +01:00
|
|
|
{
|
2023-07-25 20:18:14 +01:00
|
|
|
const KPluginMetaData data = plugins.value(pluginName);
|
|
|
|
if (!data.isValid()) {
|
2015-03-14 04:19:39 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Plugin unknown" << pluginName;
|
2023-07-25 20:18:14 +01:00
|
|
|
return nullptr;
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2023-07-25 20:18:14 +01:00
|
|
|
const QStringList outgoingInterfaces = data.value(QStringLiteral("X-KdeConnect-OutgoingPacketType"), QStringList());
|
|
|
|
const QVariantList args{QVariant::fromValue<Device *>(device), pluginName, outgoingInterfaces, data.iconName()};
|
2013-10-29 16:29:31 +00:00
|
|
|
|
2023-07-25 20:18:14 +01:00
|
|
|
if (auto result = KPluginFactory::instantiatePlugin<KdeConnectPlugin>(data, device, args)) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Loaded plugin:" << data.pluginId();
|
|
|
|
return result.plugin;
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Error loading plugin" << result.errorText;
|
|
|
|
return nullptr;
|
2013-08-12 15:09:52 +01:00
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:37:22 +01:00
|
|
|
QStringList PluginLoader::incomingCapabilities() const
|
2014-07-11 00:54:19 +01:00
|
|
|
{
|
|
|
|
QSet<QString> ret;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const KPluginMetaData &service : qAsConst(plugins)) {
|
2022-10-17 22:17:49 +01:00
|
|
|
QStringList rawValues = service.value(QStringLiteral("X-KdeConnect-SupportedPacketType"), QStringList());
|
|
|
|
ret += QSet<QString>(rawValues.begin(), rawValues.end());
|
2014-07-11 00:54:19 +01:00
|
|
|
}
|
2021-03-03 14:20:24 +00:00
|
|
|
return ret.values();
|
2014-07-11 00:54:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:37:22 +01:00
|
|
|
QStringList PluginLoader::outgoingCapabilities() const
|
2014-07-11 00:54:19 +01:00
|
|
|
{
|
|
|
|
QSet<QString> ret;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const KPluginMetaData &service : qAsConst(plugins)) {
|
2022-10-17 22:17:49 +01:00
|
|
|
QStringList rawValues = service.value(QStringLiteral("X-KdeConnect-OutgoingPacketType"), QStringList());
|
|
|
|
ret += QSet<QString>(rawValues.begin(), rawValues.end());
|
2014-07-11 00:54:19 +01:00
|
|
|
}
|
2021-03-03 14:20:24 +00:00
|
|
|
return ret.values();
|
2014-07-11 00:54:19 +01:00
|
|
|
}
|
2016-07-06 16:37:22 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSet<QString> PluginLoader::pluginsForCapabilities(const QSet<QString> &incoming, const QSet<QString> &outgoing)
|
2016-07-06 16:37:22 +01:00
|
|
|
{
|
|
|
|
QSet<QString> ret;
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QString myDeviceType = KdeConnectConfig::instance().deviceType().toString();
|
2023-04-07 22:52:58 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const KPluginMetaData &service : qAsConst(plugins)) {
|
2023-04-07 22:52:58 +01:00
|
|
|
// Check if the plugin support this device type
|
2023-04-16 08:21:13 +01:00
|
|
|
const QStringList supportedDeviceTypes = service.rawData().value(QStringLiteral("X-KdeConnect-SupportedDeviceTypes")).toVariant().toStringList();
|
2023-04-07 22:52:58 +01:00
|
|
|
if (!supportedDeviceTypes.isEmpty()) {
|
|
|
|
if (!supportedDeviceTypes.contains(myDeviceType)) {
|
2023-07-16 15:20:34 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because this device of type" << myDeviceType
|
2023-04-17 18:10:08 +01:00
|
|
|
<< "is not supported. Supports:" << supportedDeviceTypes.join(QStringLiteral(", "));
|
2023-04-07 22:52:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if capbilites intersect with the remote device
|
2023-04-16 08:21:13 +01:00
|
|
|
const QStringList pluginIncomingCapabilities = service.rawData().value(QStringLiteral("X-KdeConnect-SupportedPacketType")).toVariant().toStringList();
|
|
|
|
const QStringList pluginOutgoingCapabilities = service.rawData().value(QStringLiteral("X-KdeConnect-OutgoingPacketType")).toVariant().toStringList();
|
2016-07-06 16:37:22 +01:00
|
|
|
|
2016-08-28 10:19:00 +01:00
|
|
|
bool capabilitiesEmpty = (pluginIncomingCapabilities.isEmpty() && pluginOutgoingCapabilities.isEmpty());
|
2023-04-07 22:52:58 +01:00
|
|
|
if (!capabilitiesEmpty) {
|
2023-04-16 08:21:13 +01:00
|
|
|
bool capabilitiesIntersect = (outgoing.intersects(QSet(pluginIncomingCapabilities.begin(), pluginIncomingCapabilities.end()))
|
2023-07-22 14:21:26 +01:00
|
|
|
|| incoming.intersects(QSet(pluginOutgoingCapabilities.begin(), pluginOutgoingCapabilities.end())));
|
2023-04-07 22:52:58 +01:00
|
|
|
|
|
|
|
if (!capabilitiesIntersect) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because device doesn't support it";
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-06 16:37:22 +01:00
|
|
|
}
|
2023-04-07 22:52:58 +01:00
|
|
|
|
|
|
|
// If we get here, the plugin can be loaded
|
|
|
|
ret += service.pluginId();
|
2016-07-06 16:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|