Commit 90a2314a authored by Arjen Hiemstra's avatar Arjen Hiemstra
Browse files

Use plugin metadata for providing providerName

This allows us to filter out duplicate plugins before trying to load
them, avoiding nasty crashes because the system-installed plugin is
abi-incompatible with a newer version of the plugin api. Eventually we
can extend this with additional checks to make sure plugins are
compatible.
parent f50b717e
......@@ -70,22 +70,31 @@ void KSysGuardDaemon::init()
void KSysGuardDaemon::loadProviders()
{
//instantiate all plugins
const auto plugins = KPluginLoader::instantiatePlugins(QStringLiteral("ksysguard"), [this](const KPluginMetaData &metaData) {
auto providerName = metaData.rawData().value("providerName").toString();
auto itr = std::find_if(m_providers.cbegin(), m_providers.cend(), [providerName](SensorPlugin *plugin) {
return plugin->providerName() == providerName;
});
if (itr != m_providers.cend()) {
return false;
}
QPluginLoader loader;
KPluginLoader::forEachPlugin(QStringLiteral("ksysguard"), [&loader, this](const QString &pluginPath) {
loader.setFileName(pluginPath);
QObject* obj = loader.instance();
auto factory = qobject_cast<KPluginFactory*>(obj);
return true;
}, this);
for (auto object : plugins) {
auto factory = qobject_cast<KPluginFactory*>(object);
if (!factory) {
qWarning() << "Failed to load ksysguard factory";
return;
qWarning() << "Plugin object" << object << "did not provide a proper KPluginFactory";
continue;
}
SensorPlugin *provider = factory->create<SensorPlugin>(this);
auto provider = factory->create<SensorPlugin>(this);
if (!provider) {
return;
qWarning() << "Plugin object" << object << "did not provide a proper SensorPlugin";
continue;
}
registerProvider(provider);
});
}
if (m_providers.isEmpty()) {
qWarning() << "No plugins found";
......@@ -93,14 +102,6 @@ void KSysGuardDaemon::loadProviders()
}
void KSysGuardDaemon::registerProvider(SensorPlugin *provider) {
auto itr = std::find_if(m_providers.cbegin(), m_providers.cend(), [provider](SensorPlugin *plugin) {
return plugin->providerName() == provider->providerName();
});
if (itr != m_providers.cend()) {
qWarning() << "Skipping" << provider->providerName() << "as it is already registered";
return;
}
m_providers.append(provider);
const auto containers = provider->containers();
for (auto container : containers) {
......
......@@ -404,6 +404,6 @@ QString KSGRDIface::shortNameFor(const QString &key)
return shortNames.value(key, QString {});
}
K_PLUGIN_FACTORY(KSGRDPluginFactory, registerPlugin<KSGRDIface>();)
K_PLUGIN_CLASS_WITH_JSON(KSGRDIface, "metadata.json")
#include "ksgrdiface.moc"
{
"providerName": "ksgrd"
}
{
"providerName": "nvidia"
}
......@@ -177,6 +177,6 @@ void NvidiaPlugin::gpuSubscriptionChanged(bool subscribed)
}
}
K_PLUGIN_FACTORY(PluginFactory, registerPlugin<NvidiaPlugin>();)
K_PLUGIN_CLASS_WITH_JSON(NvidiaPlugin, "metadata.json")
#include "nvidia.moc"
{
"providerName": "osinfo"
}
......@@ -213,6 +213,6 @@ OSInfoPlugin::OSInfoPlugin(QObject *parent, const QVariantList &args)
d->update();
}
K_PLUGIN_FACTORY(PluginFactory, registerPlugin<OSInfoPlugin>();)
K_PLUGIN_CLASS_WITH_JSON(OSInfoPlugin, "metadata.json")
#include "osinfo.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment