Modernize plugin loading code

Use the new utility-methods and improve the logging.
This commit is contained in:
Alexander Lohnau 2023-07-25 21:18:14 +02:00
parent 20d8c41a4f
commit b54f0e2467
2 changed files with 14 additions and 25 deletions

View file

@ -12,7 +12,7 @@ struct KdeConnectPluginPrivate {
Device *m_device;
QString m_pluginName;
QSet<QString> 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<Device *>(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();
}

View file

@ -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 *>(device), pluginName, outgoingInterfaces, data.iconName()};
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;
}
const QStringList outgoingInterfaces = service.value(QStringLiteral("X-KdeConnect-OutgoingPacketType"), QStringList());
QVariant deviceVariant = QVariant::fromValue<Device *>(device);
ret = factoryResult.plugin->create<KdeConnectPlugin>(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