Commit 7ac29836 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Port query logic for ui plugins to KPluginMetadata::findPlugins

With https://phabricator.kde.org/T15094 we can allow plugins without
embedded json metadata to be consiered valid and consequently
show up in the KPluginMetaData::findPlugins result.

This way we can drop the custom logic to iterate over the library paths
and check the containing files.

Also we can utilize the pluginId that is derived from the filename
instead of meddling with the filename manually and appending the file
extension when accessing our internal hash of loaded plugins.
parent d38b71ba
Pipeline #159231 passed with stage
in 1 minute
......@@ -8,6 +8,7 @@
#include "kaccountsuiplugin.h"
#include <KPluginMetaData>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
......@@ -46,57 +47,44 @@ UiPluginsManagerPrivate::~UiPluginsManagerPrivate()
void UiPluginsManagerPrivate::loadPlugins()
{
QString pluginPath;
const auto availablePlugins = KPluginMetaData::findPlugins(QStringLiteral("kaccounts/ui"), {}, KPluginMetaData::AllowEmptyMetaData);
for (const KPluginMetaData &plugin : availablePlugins) {
QPluginLoader loader(plugin.fileName());
const QStringList paths = QCoreApplication::libraryPaths();
for (const QString &libraryPath : paths) {
QString path(libraryPath + QStringLiteral("/kaccounts/ui"));
QDir dir(path);
if (!dir.exists()) {
if (!loader.load()) {
qWarning() << "Could not create KAccountsUiPlugin: " << plugin.fileName();
qWarning() << loader.errorString();
continue;
}
const QStringList entryList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
for (const QString &fileName : entryList) {
QPluginLoader loader(dir.absoluteFilePath(fileName));
if (!loader.load()) {
qWarning() << "Could not create KAccountsUiPlugin: " << pluginPath;
qWarning() << loader.errorString();
if (QObject *obj = loader.instance()) {
KAccountsUiPlugin *ui = qobject_cast<KAccountsUiPlugin *>(obj);
if (!ui) {
qDebug() << "Plugin could not be converted to an KAccountsUiPlugin";
qDebug() << plugin.fileName();
continue;
}
QObject *obj = loader.instance();
if (obj) {
KAccountsUiPlugin *ui = qobject_cast<KAccountsUiPlugin *>(obj);
if (!ui) {
qDebug() << "Plugin could not be converted to an KAccountsUiPlugin";
qDebug() << pluginPath;
continue;
}
qDebug() << "Adding plugin" << ui << fileName;
const QWindowList topLevelWindows = QGuiApplication::topLevelWindows();
if (topLevelWindows.size() == 1) {
QWindow *topLevelWindow = topLevelWindows.at(0);
obj->setProperty("transientParent", QVariant::fromValue(topLevelWindow));
} else {
qWarning() << "Unexpected topLevelWindows found:" << topLevelWindows.size() << "please report a bug";
}
// When the plugin has finished building the UI, show it right away
QObject::connect(ui, &KAccountsUiPlugin::uiReady, ui, &KAccountsUiPlugin::showNewAccountDialog, Qt::UniqueConnection);
pluginsForNames.insert(fileName, ui);
const auto services = ui->supportedServicesForConfig();
for (const QString &service : services) {
qDebug() << " Adding service" << service;
pluginsForServices.insert(service, ui);
}
qDebug() << "Adding plugin" << ui << plugin.fileName();
const QWindowList topLevelWindows = QGuiApplication::topLevelWindows();
if (topLevelWindows.size() == 1) {
QWindow *topLevelWindow = topLevelWindows.at(0);
obj->setProperty("transientParent", QVariant::fromValue(topLevelWindow));
} else {
qDebug() << "Plugin could not create instance" << pluginPath;
qWarning() << "Unexpected topLevelWindows found:" << topLevelWindows.size() << "please report a bug";
}
// When the plugin has finished building the UI, show it right away
QObject::connect(ui, &KAccountsUiPlugin::uiReady, ui, &KAccountsUiPlugin::showNewAccountDialog, Qt::UniqueConnection);
pluginsForNames.insert(plugin.pluginId(), ui);
const auto services = ui->supportedServicesForConfig();
for (const QString &service : services) {
qDebug() << " Adding service" << service;
pluginsForServices.insert(service, ui);
}
} else {
qDebug() << "Plugin could not create instance" << plugin.fileName();
}
}
......@@ -118,7 +106,7 @@ KAccountsUiPlugin *UiPluginsManager::pluginForName(const QString &name)
s_instance->loadPlugins();
}
return s_instance->pluginsForNames.value(name + QStringLiteral(".so"));
return s_instance->pluginsForNames.value(name);
}
KAccountsUiPlugin *UiPluginsManager::pluginForService(const QString &service)
......
Supports Markdown
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