7#include "textautogenerateengineloader.h"
9#include "textautogeneratetextclient.h"
10#include "textautogeneratetextcore_debug.h"
11#include <QCoreApplication>
13#include <QPluginLoader>
15using namespace TextAutogenerateText;
16class TextAutogenerateText::TextAutogenerateEngineLoaderPrivate
19 QSet<QString> loadedPlugins;
20 QHash<QString, TextAutogenerateTextClient *> autogenerateTextClients;
25 static TextAutogenerateEngineLoader s_self;
29TextAutogenerateEngineLoader::TextAutogenerateEngineLoader(QObject *parent)
31 , d(new TextAutogenerateText::TextAutogenerateEngineLoaderPrivate)
36TextAutogenerateEngineLoader::~TextAutogenerateEngineLoader() =
default;
38void TextAutogenerateEngineLoader::loadPlugins()
41 const QString pathSuffix(QStringLiteral(
"/kf6/textautogeneratetext/"));
42 for (
const QString &libPath : libPaths) {
43 const QDir
dir(libPath + pathSuffix);
48 loadPlugin(
dir.absoluteFilePath(fileName));
51 if (d->loadedPlugins.isEmpty()) {
52 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"No autogenerate plugins available!";
57void TextAutogenerateEngineLoader::loadPlugin(
const QString &pluginPath)
59 QPluginLoader plugin(pluginPath);
60 const QString pluginIID = plugin.metaData()[
"IID"_L1].toString();
62 if (d->loadedPlugins.contains(pluginIID)) {
63 qCDebug(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Skipping already loaded" << pluginPath;
66 d->loadedPlugins.insert(pluginIID);
70 qCDebug(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Unable to load plugin" << pluginPath <<
"Error:" << plugin.errorString();
71 d->loadedPlugins.
remove(pluginIID);
76 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Invalid plugin loaded" << pluginPath;
80 d->autogenerateTextClients.insert(client->
name(), client);
85 auto clientsItr = d->autogenerateTextClients.constFind(clientName);
86 if (clientsItr == d->autogenerateTextClients.constEnd()) {
87 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Client name not found: " << clientName;
88 Q_EMIT loadingPluginFailed();
94bool TextAutogenerateEngineLoader::hasConfigurationDialog(
const QString &clientName)
const
96 auto clientsItr = d->autogenerateTextClients.constFind(clientName);
97 if (clientsItr == d->autogenerateTextClients.constEnd()) {
98 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Client name not found: " << clientName;
101 return (*clientsItr)->hasConfigurationDialog();
104bool TextAutogenerateEngineLoader::showConfigureDialog(
const QString &clientName, QWidget *parentWidget)
106 auto clientsItr = d->autogenerateTextClients.constFind(clientName);
107 if (clientsItr == d->autogenerateTextClients.constEnd()) {
108 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"Client name not found: " << clientName;
111 return (*clientsItr)->showConfigureDialog(parentWidget);
114QMap<QString, QString> TextAutogenerateEngineLoader::textAutoGenerateTextEngineInfos()
const
116 QMap<QString, QString>
map;
117 QHashIterator<QString, TextAutogenerateTextClient *> i(d->autogenerateTextClients);
118 while (i.hasNext()) {
120 map.insert(i.key(), generateDisplayName(i.value()));
127 return client->translatedName() + QStringLiteral(
" (%1)").
arg(client->convertEngineType(client->engineType()));
130QString TextAutogenerateEngineLoader::fallbackFirstEngine()
const
132 if (!d->autogenerateTextClients.isEmpty()) {
133 return *d->autogenerateTextClients.keyBegin();
135 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) <<
"No plugin found ! ";
139bool TextAutogenerateEngineLoader::hasEngine()
const
141 return !d->autogenerateTextClients.isEmpty();
144#include "moc_textautogenerateengineloader.cpp"
The TextAutogenerateEngineLoader class.
The TextAutogenerateTextClient class.
virtual QString name() const =0
KIOCORE_EXPORT QString dir(const QString &fileClass)
QStringList libraryPaths()
T qobject_cast(QObject *object)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)