Port deprecated KPluginLoader usage

This commit is contained in:
Alexander Lohnau 2023-04-16 09:21:13 +02:00
parent cd3c02417e
commit c1be9a28eb
3 changed files with 36 additions and 24 deletions

View file

@ -7,18 +7,17 @@
#include "pluginloader.h" #include "pluginloader.h"
#include <KPluginFactory> #include <KPluginFactory>
#include <KPluginLoader>
#include <KPluginMetaData> #include <KPluginMetaData>
#include <QPluginLoader> #include <QPluginLoader>
#include <QStaticPlugin> #include <QStaticPlugin>
#include <QVector> #include <QVector>
#include "kdeconnectconfig.h"
#include "core_debug.h" #include "core_debug.h"
#include "device.h" #include "device.h"
#include "kdeconnectconfig.h"
#include "kdeconnectplugin.h" #include "kdeconnectplugin.h"
// In older Qt released, qAsConst isnt available // In older Qt released, qAsConst isn't available
#include "qtcompat_p.h" #include "qtcompat_p.h"
PluginLoader *PluginLoader::instance() PluginLoader *PluginLoader::instance()
@ -40,7 +39,7 @@ PluginLoader::PluginLoader()
} }
} }
#else #else
const QVector<KPluginMetaData> data = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/")); const QVector<KPluginMetaData> data = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect/"));
for (const KPluginMetaData &metadata : data) { for (const KPluginMetaData &metadata : data) {
plugins[metadata.pluginId()] = metadata; plugins[metadata.pluginId()] = metadata;
} }
@ -70,15 +69,14 @@ KdeConnectPlugin *PluginLoader::instantiatePluginForDevice(const QString &plugin
#ifdef SAILFISHOS #ifdef SAILFISHOS
KPluginFactory *factory = pluginsFactories.value(pluginName); KPluginFactory *factory = pluginsFactories.value(pluginName);
#else #else
KPluginLoader loader(service.fileName()); auto factoryResult = KPluginFactory::loadFactory(service);
KPluginFactory *factory = loader.factory(); if (!factoryResult) {
if (!factory) { qCDebug(KDECONNECT_CORE) << "KPluginFactory could not load the plugin:" << service.pluginId() << factoryResult.errorString;
qCDebug(KDECONNECT_CORE) << "KPluginFactory could not load the plugin:" << service.pluginId() << loader.errorString();
return ret; return ret;
} }
KPluginFactory *factory = factoryResult.plugin;
#endif #endif
const QStringList outgoingInterfaces = service.rawData().value(QStringLiteral("X-KdeConnect-OutgoingPacketType")).toVariant().toStringList();
const QStringList outgoingInterfaces = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-OutgoingPacketType"));
QVariant deviceVariant = QVariant::fromValue<Device *>(device); QVariant deviceVariant = QVariant::fromValue<Device *>(device);
@ -119,33 +117,32 @@ QSet<QString> PluginLoader::pluginsForCapabilities(const QSet<QString> &incoming
QString myDeviceType = KdeConnectConfig::instance().deviceType(); QString myDeviceType = KdeConnectConfig::instance().deviceType();
for (const KPluginMetaData &service : qAsConst(plugins)) { for (const KPluginMetaData &service : qAsConst(plugins)) {
// Check if the plugin support this device type // Check if the plugin support this device type
const QSet<QString> supportedDeviceTypes = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedDeviceTypes")).toSet(); const QStringList supportedDeviceTypes = service.rawData().value(QStringLiteral("X-KdeConnect-SupportedDeviceTypes")).toVariant().toStringList();
if (!supportedDeviceTypes.isEmpty()) { if (!supportedDeviceTypes.isEmpty()) {
if (!supportedDeviceTypes.contains(myDeviceType)) { if (!supportedDeviceTypes.contains(myDeviceType)) {
qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because this device of type" << myDeviceType << "is not supported. Supports:" << supportedDeviceTypes.toList().join(QStringLiteral(", ")); qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because this device of type" << myDeviceType
<< "is not supported. Supports:" << supportedDeviceTypes.toList().join(QStringLiteral(", "));
continue; continue;
} }
} }
// Check if capbilites intersect with the remote device // Check if capbilites intersect with the remote device
const QSet<QString> pluginIncomingCapabilities = const QStringList pluginIncomingCapabilities = service.rawData().value(QStringLiteral("X-KdeConnect-SupportedPacketType")).toVariant().toStringList();
KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPacketType")).toSet(); const QStringList pluginOutgoingCapabilities = service.rawData().value(QStringLiteral("X-KdeConnect-OutgoingPacketType")).toVariant().toStringList();
const QSet<QString> pluginOutgoingCapabilities =
KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-OutgoingPacketType")).toSet();
bool capabilitiesEmpty = (pluginIncomingCapabilities.isEmpty() && pluginOutgoingCapabilities.isEmpty()); bool capabilitiesEmpty = (pluginIncomingCapabilities.isEmpty() && pluginOutgoingCapabilities.isEmpty());
if (!capabilitiesEmpty) { if (!capabilitiesEmpty) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
bool capabilitiesIntersect = (outgoing.intersects(pluginIncomingCapabilities) || incoming.intersects(pluginOutgoingCapabilities)); bool capabilitiesIntersect = (outgoing.intersects(QSet(pluginIncomingCapabilities.begin(), pluginIncomingCapabilities.end()))
#else || incoming.intersects(QSet(pluginOutgoingCapabilities.begin(), pluginOutgoingCapabilities.end())));
#else
QSet<QString> commonIncoming = incoming; QSet<QString> commonIncoming = incoming;
commonIncoming.intersect(pluginOutgoingCapabilities); commonIncoming.intersect(pluginOutgoingCapabilities.toSet());
QSet<QString> commonOutgoing = outgoing; QSet<QString> commonOutgoing = outgoing;
commonOutgoing.intersect(pluginIncomingCapabilities); commonOutgoing.intersect(pluginIncomingCapabilities.toSet());
bool capabilitiesIntersect = (!commonIncoming.isEmpty() || !commonOutgoing.isEmpty()); bool capabilitiesIntersect = (!commonIncoming.isEmpty() || !commonOutgoing.isEmpty());
#endif #endif
if (!capabilitiesIntersect) { if (!capabilitiesIntersect) {
qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because device doesn't support it"; qCDebug(KDECONNECT_CORE) << "Not loading plugin" << service.pluginId() << "because device doesn't support it";
@ -155,7 +152,6 @@ QSet<QString> PluginLoader::pluginsForCapabilities(const QSet<QString> &incoming
// If we get here, the plugin can be loaded // If we get here, the plugin can be loaded
ret += service.pluginId(); ret += service.pluginId();
} }
return ret; return ret;

View file

@ -25,6 +25,15 @@ public:
static PluginLoader *instance(); static PluginLoader *instance();
QStringList getPluginList() const; QStringList getPluginList() const;
QSet<QString> getPluginSet() const
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
return getPluginList().toSet();
#else
const QStringList pluginList = getPluginList();
return QSet(pluginList.begin(), pluginList.end());
#endif
}
KPluginMetaData getPluginInfo(const QString &name) const; KPluginMetaData getPluginInfo(const QString &name) const;
KdeConnectPlugin *instantiatePluginForDevice(const QString &name, Device *device) const; KdeConnectPlugin *instantiatePluginForDevice(const QString &name, Device *device) const;

View file

@ -8,8 +8,11 @@
#include <QDebug> #include <QDebug>
#include "kcoreaddons_version.h"
#include <KConfigGroup> #include <KConfigGroup>
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(5, 86, 0)
#include <KPluginLoader> #include <KPluginLoader>
#endif
PluginModel::PluginModel(QObject *parent) PluginModel::PluginModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
@ -18,7 +21,11 @@ PluginModel::PluginModel(QObject *parent)
connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged); connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged);
beginResetModel(); beginResetModel();
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(5, 84, 0)
m_plugins = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/")); m_plugins = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"));
#else
m_plugins = KPluginMetaData::findPlugins(QStringLiteral("kdeconnect/"));
#endif
endResetModel(); endResetModel();
} }