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

View file

@ -25,6 +25,15 @@ public:
static PluginLoader *instance();
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;
KdeConnectPlugin *instantiatePluginForDevice(const QString &name, Device *device) const;

View file

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