From b54f0e2467cb125b73ff339b925656d48bbed7e5 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Tue, 25 Jul 2023 21:18:14 +0200 Subject: [PATCH] Modernize plugin loading code Use the new utility-methods and improve the logging. --- core/kdeconnectplugin.cpp | 5 ++--- core/pluginloader.cpp | 34 ++++++++++++---------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/core/kdeconnectplugin.cpp b/core/kdeconnectplugin.cpp index 4fc7c1fb4..f9fd472bf 100644 --- a/core/kdeconnectplugin.cpp +++ b/core/kdeconnectplugin.cpp @@ -12,7 +12,7 @@ struct KdeConnectPluginPrivate { Device *m_device; QString m_pluginName; QSet m_outgoingCapabilties; - KdeConnectPluginConfig *m_config; + KdeConnectPluginConfig *m_config = nullptr; QString iconName; }; @@ -20,13 +20,12 @@ KdeConnectPlugin::KdeConnectPlugin(QObject *parent, const QVariantList &args) : QObject(parent) , d(new KdeConnectPluginPrivate) { + Q_ASSERT(args.length() == 4); d->m_device = qvariant_cast(args.at(0)); d->m_pluginName = args.at(1).toString(); const QStringList cap = args.at(2).toStringList(); d->m_outgoingCapabilties = QSet(cap.begin(), cap.end()); - - d->m_config = nullptr; d->iconName = args.at(3).toString(); } diff --git a/core/pluginloader.cpp b/core/pluginloader.cpp index 5c6599d07..0a71de28b 100644 --- a/core/pluginloader.cpp +++ b/core/pluginloader.cpp @@ -48,32 +48,22 @@ KPluginMetaData PluginLoader::getPluginInfo(const QString &name) const KdeConnectPlugin *PluginLoader::instantiatePluginForDevice(const QString &pluginName, Device *device) const { - KdeConnectPlugin *ret = nullptr; - - KPluginMetaData service = plugins.value(pluginName); - if (!service.isValid()) { + const KPluginMetaData data = plugins.value(pluginName); + if (!data.isValid()) { qCDebug(KDECONNECT_CORE) << "Plugin unknown" << pluginName; - return ret; + return nullptr; } - auto factoryResult = KPluginFactory::loadFactory(service); - if (!factoryResult) { - qCDebug(KDECONNECT_CORE) << "KPluginFactory could not load the plugin:" << service.pluginId() << factoryResult.errorString; - return ret; + const QStringList outgoingInterfaces = data.value(QStringLiteral("X-KdeConnect-OutgoingPacketType"), QStringList()); + const QVariantList args{QVariant::fromValue(device), pluginName, outgoingInterfaces, data.iconName()}; + + if (auto result = KPluginFactory::instantiatePlugin(data, device, args)) { + qCDebug(KDECONNECT_CORE) << "Loaded plugin:" << data.pluginId(); + return result.plugin; + } else { + qCDebug(KDECONNECT_CORE) << "Error loading plugin" << result.errorText; + return nullptr; } - - const QStringList outgoingInterfaces = service.value(QStringLiteral("X-KdeConnect-OutgoingPacketType"), QStringList()); - - QVariant deviceVariant = QVariant::fromValue(device); - - ret = factoryResult.plugin->create(device, QVariantList() << deviceVariant << pluginName << outgoingInterfaces << service.iconName()); - if (!ret) { - qCDebug(KDECONNECT_CORE) << "Error loading plugin"; - return ret; - } - - // qCDebug(KDECONNECT_CORE) << "Loaded plugin:" << service.pluginId(); - return ret; } QStringList PluginLoader::incomingCapabilities() const