Commit 10af9d7a authored by Nicolas Fella's avatar Nicolas Fella Committed by Alexander Semke
Browse files

Port plugin loading away from KPluginLoader

It is deprecated

This also simplifies the code a lot
parent f2ea4433
Pipeline #243394 passed with stage
in 8 minutes and 33 seconds
......@@ -323,9 +323,10 @@ void CantorShell::addWorksheet(const QString& backendName)
// this routine will find and load our Part. it finds the Part by
// name which is a bad idea usually.. but it's alright in this
// case since our Part is made for this Shell
KPluginLoader loader(QLatin1String("kf5/parts/cantorpart"));
KPluginFactory* factory = loader.factory();
if (factory)
const auto partResult = KPluginFactory::instantiatePlugin<KParts::ReadWritePart>(KPluginMetaData(QStringLiteral("kf5/parts/cantorpart")), m_tabWidget, {backendName});
if (partResult)
{
Cantor::Backend* backend = nullptr;
if (!backendName.isEmpty())
......@@ -347,49 +348,42 @@ void CantorShell::addWorksheet(const QString& backendName)
}
// now that the Part is loaded, we cast it to a Part to get our hands on it
auto* part = factory->create<KParts::ReadWritePart>(m_tabWidget, QVariantList()<<backendName);
if (part)
{
connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
connect(part, SIGNAL(showHelp(QString)), this, SIGNAL(showHelp(QString)));
connect(part, SIGNAL(hierarchyChanged(QStringList, QStringList, QList<int>)), this, SIGNAL(hierarchyChanged(QStringList, QStringList, QList<int>)));
connect(part, SIGNAL(hierarhyEntryNameChange(QString, QString, int)), this, SIGNAL(hierarhyEntryNameChange(QString, QString, int)));
connect(this, SIGNAL(requestScrollToHierarchyEntry(QString)), part, SIGNAL(requestScrollToHierarchyEntry(QString)));
connect(this, SIGNAL(settingsChanges()), part, SIGNAL(settingsChanges()));
connect(part, SIGNAL(requestDocumentation(QString)), this, SIGNAL(requestDocumentation(QString)));
m_parts.append(part);
if (backend) {// If backend empty (loading worksheet from file), then we connect to signal and wait
m_parts2Backends[part] = backend->id();
//show the default help string in the help panel
emit showHelp(backend->defaultHelp());
}
else
{
m_parts2Backends[part] = QString();
connect(part, SIGNAL(setBackendName(QString)), this, SLOT(updateBackendForPart(QString)));
}
int tab = m_tabWidget->addTab(part->widget(), i18n("Session %1", sessionCount++));
m_tabWidget->setCurrentIndex(tab);
// Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
// This fix https://bugs.kde.org/show_bug.cgi?id=395976
part->widget()->findChild<QGraphicsView*>()->setFocus();
// Force run updateCaption for getting proper backend icon
QMetaObject::invokeMethod(part, "updateCaption");
}
else
auto part = partResult.plugin;
connect(part, SIGNAL(setCaption(QString,QIcon)), this, SLOT(setTabCaption(QString,QIcon)));
connect(part, SIGNAL(worksheetSave(QUrl)), this, SLOT(onWorksheetSave(QUrl)));
connect(part, SIGNAL(showHelp(QString)), this, SIGNAL(showHelp(QString)));
connect(part, SIGNAL(hierarchyChanged(QStringList, QStringList, QList<int>)), this, SIGNAL(hierarchyChanged(QStringList, QStringList, QList<int>)));
connect(part, SIGNAL(hierarhyEntryNameChange(QString, QString, int)), this, SIGNAL(hierarhyEntryNameChange(QString, QString, int)));
connect(this, SIGNAL(requestScrollToHierarchyEntry(QString)), part, SIGNAL(requestScrollToHierarchyEntry(QString)));
connect(this, SIGNAL(settingsChanges()), part, SIGNAL(settingsChanges()));
connect(part, SIGNAL(requestDocumentation(QString)), this, SIGNAL(requestDocumentation(QString)));
m_parts.append(part);
if (backend) {// If backend empty (loading worksheet from file), then we connect to signal and wait
m_parts2Backends[part] = backend->id();
//show the default help string in the help panel
emit showHelp(backend->defaultHelp());
} else
{
qDebug()<<"error creating part ";
m_parts2Backends[part] = QString();
connect(part, SIGNAL(setBackendName(QString)), this, SLOT(updateBackendForPart(QString)));
}
int tab = m_tabWidget->addTab(part->widget(), i18n("Session %1", sessionCount++));
m_tabWidget->setCurrentIndex(tab);
// Setting focus on worksheet view, because Qt clear focus of added widget inside addTab
// This fix https://bugs.kde.org/show_bug.cgi?id=395976
part->widget()->findChild<QGraphicsView*>()->setFocus();
// Force run updateCaption for getting proper backend icon
QMetaObject::invokeMethod(part, "updateCaption");
}
else
{
// if we couldn't find our Part, we exit since the Shell by
// itself can't do anything useful
KMessageBox::error(this, i18n("Failed to find the Cantor Part with error %1", loader.errorString()));
KMessageBox::error(this, i18n("Failed to find the Cantor Part with error %1", partResult.errorString));
qApp->quit();
// we return here, cause qApp->quit() only means "exit the
// next time we enter the event loop...
......
......@@ -752,53 +752,36 @@ void CantorPart::loadAssistants()
{
qDebug()<<"loading assistants...";
QStringList assistantDirs;
for (const QString& dir : QCoreApplication::libraryPaths())
assistantDirs << dir + QDir::separator() + QLatin1String("cantor/assistants");
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("cantor/assistants"));
QPluginLoader loader;
for (const QString& dir : assistantDirs) {
for (const KPluginMetaData &plugin : plugins) {
qDebug() << "dir: " << dir;
QStringList assistants;
QDir assistantDir = QDir(dir);
const auto result = KPluginFactory::instantiatePlugin<Cantor::Assistant>(plugin, this);
assistants = assistantDir.entryList();
for (const QString& assistant : assistants) {
if (assistant == QLatin1String(".") || assistant == QLatin1String(".."))
continue;
loader.setFileName(dir + QDir::separator() + assistant);
if (!loader.load()){
qDebug() << "Error while loading assistant: " << assistant;
continue;
}
KPluginFactory* factory = KPluginLoader(loader.fileName()).factory();
auto* plugin = factory->create<Cantor::Assistant>(this);
auto* backend=worksheet()->session()->backend();
if (!result) {
qDebug() << "Error while loading assistant plugin: " << result.errorText;
continue;
}
KPluginMetaData info(loader);
plugin->setPluginInfo(info);
plugin->setBackend(backend);
Cantor::Assistant *assistant = result.plugin;
auto* backend=worksheet()->session()->backend();
assistant->setPluginInfo(plugin);
assistant->setBackend(backend);
bool supported=true;
for (const QString& req : plugin->requiredExtensions())
supported = supported && backend->extensions().contains(req);
bool supported=true;
for (const QString& req : assistant->requiredExtensions())
supported = supported && backend->extensions().contains(req);
if(supported)
{
qDebug() << "plugin " << info.name() << " is supported by " << backend->name() << ", requires extensions " << plugin->requiredExtensions();
plugin->initActions();
connect(plugin, &Cantor::Assistant::requested, this, &CantorPart::runAssistant);
}else
{
qDebug() << "plugin " << info.name() << " is not supported by "<<backend->name();
removeChildClient(plugin);
plugin->deleteLater();
}
if(supported)
{
qDebug() << "plugin " << plugin.name() << " is supported by " << backend->name() << ", requires extensions " << assistant->requiredExtensions();
assistant->initActions();
connect(assistant, &Cantor::Assistant::requested, this, &CantorPart::runAssistant);
}else
{
qDebug() << "plugin " << plugin.name() << " is not supported by "<<backend->name();
removeChildClient(assistant);
assistant->deleteLater();
}
}
}
......
......@@ -15,6 +15,7 @@
#include <QStandardPaths>
#include <QPluginLoader>
#include <KPluginFactory>
#include <KPluginMetaData>
#include <KPluginFactory>
#include <KLocalizedString>
......@@ -105,46 +106,24 @@ QList<Backend*> Backend::availableBackends()
return backendCache;
}
QStringList pluginDirs;
for (const QString& dir : QCoreApplication::libraryPaths()){
pluginDirs << dir + QDir::separator() + QLatin1String("cantor/backends");
}
QPluginLoader loader;
for (const QString &dir : pluginDirs){
qDebug() << "dir: " << dir;
QStringList plugins;
QDir pluginDir = QDir(dir);
plugins = pluginDir.entryList();
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("cantor/backends"));
for (const QString &plugin : plugins){
if (plugin==QLatin1String(".") || plugin==QLatin1String(".."))
continue;
for (const KPluginMetaData &plugin : plugins) {
loader.setFileName(dir + QDir::separator() + plugin);
const auto result = KPluginFactory::instantiatePlugin<Backend>(plugin, QCoreApplication::instance());
if (!loader.load()){
qDebug() << "Error while loading plugin: " << plugin;
continue;
}
KPluginFactory* factory = KPluginLoader(loader.fileName()).factory();
Backend* backend = factory->create<Backend>(QCoreApplication::instance());
if (!result) {
qDebug() << "Error while loading backend: " << result.errorText;
continue;
}
if (!backend){
qDebug() << "Error using plugin " << loader.fileName();
qDebug() << "Error message: " << loader.errorString();
continue;
}
Backend *backend = result.plugin;
KPluginMetaData info(loader);
backend->d->name=info.name();
backend->d->comment=info.description();
backend->d->icon=info.iconName();
backend->d->url=info.website();
backendCache<<backend;
}
backend->d->name = plugin.name();
backend->d->comment = plugin.description();
backend->d->icon = plugin.iconName();
backend->d->url = plugin.website();
backendCache << backend;
}
return backendCache;
......
......@@ -7,7 +7,6 @@
#define _CANTOR_MACROS_H
#include <KPluginFactory>
#include <KPluginLoader>
/**
Exports Backend plugin.
......
......@@ -8,6 +8,8 @@ using namespace Cantor;
#include <QDebug>
#include <QDir>
#include <KPluginFactory>
#include <KPluginMetaData>
#include <KPluginFactory>
......@@ -33,34 +35,22 @@ PanelPluginHandler::~PanelPluginHandler()
void PanelPluginHandler::loadPlugins()
{
QStringList panelDirs;
for (const QString& path : QCoreApplication::libraryPaths()) {
const QString& dir = path + QDir::separator() + QLatin1String("cantor/panels");
qDebug() << "dir: " << dir;
QDir panelDir = QDir(dir);
const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("cantor/panels"));
QPluginLoader loader;
const QStringList& panels = panelDir.entryList();
for (const KPluginMetaData &plugin : plugins) {
for (const QString& panel : panels)
{
if (panel==QLatin1String(".") || panel==QLatin1String(".."))
continue;
const auto result = KPluginFactory::instantiatePlugin<PanelPlugin>(plugin, QCoreApplication::instance());
loader.setFileName(dir + QDir::separator() + panel);
if (!result) {
qDebug() << "Error while loading panel: " << result.errorText;
continue;
}
if (!loader.load()){
qDebug() << "Error while loading panel" << panel << ": \"" << loader.errorString() << "\"";
continue;
}
PanelPlugin *panel = result.plugin;
KPluginFactory* factory = KPluginLoader(loader.fileName()).factory();
PanelPlugin* plugin = factory->create<PanelPlugin>(this);
panel->setPluginInfo(plugin);
d->plugins.append(panel);
KPluginMetaData info(loader);
plugin->setPluginInfo(info);
d->plugins.append(plugin);
}
}
}
......
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