KTextAddons

textautogenerateengineloader.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "textautogenerateengineloader.h"
8
9#include "textautogeneratetextclient.h"
10#include "textautogeneratetextcore_debug.h"
11#include <QCoreApplication>
12#include <QDir>
13#include <QPluginLoader>
14using namespace Qt::Literals::StringLiterals;
15using namespace TextAutogenerateText;
16class TextAutogenerateText::TextAutogenerateEngineLoaderPrivate
17{
18public:
19 QSet<QString> loadedPlugins;
20 QHash<QString, TextAutogenerateTextClient *> autogenerateTextClients;
21};
22
23TextAutogenerateEngineLoader *TextAutogenerateEngineLoader::self()
24{
25 static TextAutogenerateEngineLoader s_self;
26 return &s_self;
27}
28
29TextAutogenerateEngineLoader::TextAutogenerateEngineLoader(QObject *parent)
30 : QObject{parent}
31 , d(new TextAutogenerateText::TextAutogenerateEngineLoaderPrivate)
32{
33 loadPlugins();
34}
35
36TextAutogenerateEngineLoader::~TextAutogenerateEngineLoader() = default;
37
38void TextAutogenerateEngineLoader::loadPlugins()
39{
40 const QStringList libPaths = QCoreApplication::libraryPaths();
41 const QString pathSuffix(QStringLiteral("/kf6/textautogeneratetext/"));
42 for (const QString &libPath : libPaths) {
43 const QDir dir(libPath + pathSuffix);
44 if (!dir.exists()) {
45 continue;
46 }
47 for (const QString &fileName : dir.entryList(QDir::Files)) {
48 loadPlugin(dir.absoluteFilePath(fileName));
49 }
50 }
51 if (d->loadedPlugins.isEmpty()) {
52 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) << "No autogenerate plugins available!";
53 Q_EMIT noPluginsFound();
54 }
55}
56
57void TextAutogenerateEngineLoader::loadPlugin(const QString &pluginPath)
58{
59 QPluginLoader plugin(pluginPath);
60 const QString pluginIID = plugin.metaData()["IID"_L1].toString();
61 if (!pluginIID.isEmpty()) {
62 if (d->loadedPlugins.contains(pluginIID)) {
63 qCDebug(TEXTAUTOGENERATETEXT_CORE_LOG) << "Skipping already loaded" << pluginPath;
64 return;
65 }
66 d->loadedPlugins.insert(pluginIID);
67 }
68
69 if (!plugin.load()) { // We do this separately for better error handling
70 qCDebug(TEXTAUTOGENERATETEXT_CORE_LOG) << "Unable to load plugin" << pluginPath << "Error:" << plugin.errorString();
71 d->loadedPlugins.remove(pluginIID);
72 return;
73 }
74 TextAutogenerateTextClient *client = qobject_cast<TextAutogenerateTextClient *>(plugin.instance());
75 if (!client) {
76 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) << "Invalid plugin loaded" << pluginPath;
77 plugin.unload(); // don't leave it in memory
78 return;
79 }
80 d->autogenerateTextClients.insert(client->name(), client);
81}
82
83TextAutogenerateTextClient *TextAutogenerateEngineLoader::createTextAutoGenerateTextClient(const QString &clientName)
84{
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();
89 return nullptr;
90 }
91 return (*clientsItr);
92}
93
94bool TextAutogenerateEngineLoader::hasConfigurationDialog(const QString &clientName) const
95{
96 auto clientsItr = d->autogenerateTextClients.constFind(clientName);
97 if (clientsItr == d->autogenerateTextClients.constEnd()) {
98 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) << "Client name not found: " << clientName;
99 return false;
100 }
101 return (*clientsItr)->hasConfigurationDialog();
102}
103
104bool TextAutogenerateEngineLoader::showConfigureDialog(const QString &clientName, QWidget *parentWidget)
105{
106 auto clientsItr = d->autogenerateTextClients.constFind(clientName);
107 if (clientsItr == d->autogenerateTextClients.constEnd()) {
108 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) << "Client name not found: " << clientName;
109 return false;
110 }
111 return (*clientsItr)->showConfigureDialog(parentWidget);
112}
113
114QMap<QString, QString> TextAutogenerateEngineLoader::textAutoGenerateTextEngineInfos() const
115{
116 QMap<QString, QString> map;
117 QHashIterator<QString, TextAutogenerateTextClient *> i(d->autogenerateTextClients);
118 while (i.hasNext()) {
119 i.next();
120 map.insert(i.key(), generateDisplayName(i.value()));
121 }
122 return map;
123}
124
125QString TextAutogenerateEngineLoader::generateDisplayName(TextAutogenerateTextClient *client) const
126{
127 return client->translatedName() + QStringLiteral(" (%1)").arg(client->convertEngineType(client->engineType()));
128}
129
130QString TextAutogenerateEngineLoader::fallbackFirstEngine() const
131{
132 if (!d->autogenerateTextClients.isEmpty()) {
133 return *d->autogenerateTextClients.keyBegin();
134 }
135 qCWarning(TEXTAUTOGENERATETEXT_CORE_LOG) << "No plugin found ! ";
136 return QString();
137}
138
139bool TextAutogenerateEngineLoader::hasEngine() const
140{
141 return !d->autogenerateTextClients.isEmpty();
142}
143
144#include "moc_textautogenerateengineloader.cpp"
KIOCORE_EXPORT QString dir(const QString &fileClass)
QStringList libraryPaths()
Q_EMITQ_EMIT
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)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 4 2025 11:55:34 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.