KTextAddons

translatorengineloader.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "translatorengineloader.h"
8
9#include "texttranslator_debug.h"
10#include "translatorengineclient.h"
11#include <QCoreApplication>
12#include <QDir>
13#include <QPluginLoader>
14using namespace Qt::Literals::StringLiterals;
15using namespace TextTranslator;
16class TextTranslator::TranslatorEngineLoaderPrivate
17{
18public:
19 QSet<QString> loadedPlugins;
21};
22
23TranslatorEngineLoader *TranslatorEngineLoader::self()
24{
25 static TranslatorEngineLoader s_self;
26 return &s_self;
27}
28
29TranslatorEngineLoader::TranslatorEngineLoader(QObject *parent)
30 : QObject{parent}
31 , d(new TextTranslator::TranslatorEngineLoaderPrivate)
32{
33 loadPlugins();
34}
35
36TranslatorEngineLoader::~TranslatorEngineLoader() = default;
37
38void TranslatorEngineLoader::loadPlugins()
39{
41 const QString pathSuffix(QStringLiteral("/kf6/translator/"));
42 for (const QString &libPath : libPaths) {
43 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(TEXTTRANSLATOR_LOG) << "No translator plugins available!";
53 }
54}
55
56void TranslatorEngineLoader::loadPlugin(const QString &pluginPath)
57{
58 QPluginLoader plugin(pluginPath);
59 const QString pluginIID = plugin.metaData()["IID"_L1].toString();
60 if (!pluginIID.isEmpty()) {
61 if (d->loadedPlugins.contains(pluginIID)) {
62 qCDebug(TEXTTRANSLATOR_LOG) << "Skipping already loaded" << pluginPath;
63 return;
64 }
65 d->loadedPlugins.insert(pluginIID);
66 }
67
68 if (!plugin.load()) { // We do this separately for better error handling
69 qCDebug(TEXTTRANSLATOR_LOG) << "Unable to load plugin" << pluginPath << "Error:" << plugin.errorString();
70 d->loadedPlugins.remove(pluginIID);
71 return;
72 }
73 TranslatorEngineClient *client = qobject_cast<TranslatorEngineClient *>(plugin.instance());
74 if (!client) {
75 qCWarning(TEXTTRANSLATOR_LOG) << "Invalid plugin loaded" << pluginPath;
76 plugin.unload(); // don't leave it in memory
77 return;
78 }
79 d->translatorClients.insert(client->name(), client);
80}
81
82TranslatorEngineClient *TranslatorEngineLoader::createTranslatorClient(const QString &clientName)
83{
84 auto clientsItr = d->translatorClients.constFind(clientName);
85 if (clientsItr == d->translatorClients.constEnd()) {
86 qCWarning(TEXTTRANSLATOR_LOG) << "Client name not found: " << clientName;
87 Q_EMIT loadingTranslatorFailed();
88 return nullptr;
89 }
90 return (*clientsItr);
91}
92
93QString TranslatorEngineLoader::currentPluginName(const QString &key) const
94{
95 auto clientsItr = d->translatorClients.constFind(key);
96 if (clientsItr != d->translatorClients.constEnd()) {
97 return (*clientsItr)->translatedName();
98 }
99 return {};
100}
101
102QMap<QString, QString> TranslatorEngineLoader::translatorEngineInfos() const
103{
106 while (i.hasNext()) {
107 i.next();
108 map.insert(i.key(), i.value()->translatedName() + QStringLiteral(" (%1)").arg(TranslatorEngineClient::convertEngineType(i.value()->engineType())));
109 }
110 return map;
111}
112
113QMap<TextTranslator::TranslatorUtil::Language, QString> TranslatorEngineLoader::supportedFromLanguages(const QString &clientName) const
114{
116 auto clientsItr = d->translatorClients.constFind(clientName);
117 if (clientsItr == d->translatorClients.constEnd()) {
118 qCWarning(TEXTTRANSLATOR_LOG) << "Client name not found: " << clientName;
119 return supportedLanguages;
120 }
121 (*clientsItr)->updateListLanguages();
122 supportedLanguages = (*clientsItr)->supportedFromLanguages();
123 return supportedLanguages;
124}
125
126QMap<TextTranslator::TranslatorUtil::Language, QString> TranslatorEngineLoader::supportedToLanguages(const QString &clientName) const
127{
129 auto clientsItr = d->translatorClients.constFind(clientName);
130 if (clientsItr == d->translatorClients.constEnd()) {
131 qCWarning(TEXTTRANSLATOR_LOG) << "Client name not found: " << clientName;
132 return supportedLanguages;
133 }
134 (*clientsItr)->updateListLanguages();
135 supportedLanguages = (*clientsItr)->supportedToLanguages();
136 return supportedLanguages;
137}
138
139bool TranslatorEngineLoader::hasConfigurationDialog(const QString &clientName) const
140{
141 auto clientsItr = d->translatorClients.constFind(clientName);
142 if (clientsItr == d->translatorClients.constEnd()) {
143 qCWarning(TEXTTRANSLATOR_LOG) << "Client name not found: " << clientName;
144 return false;
145 }
146 return (*clientsItr)->hasConfigurationDialog();
147}
148
149bool TranslatorEngineLoader::showConfigureDialog(const QString &clientName, QWidget *parentWidget)
150{
151 auto clientsItr = d->translatorClients.constFind(clientName);
152 if (clientsItr == d->translatorClients.constEnd()) {
153 qCWarning(TEXTTRANSLATOR_LOG) << "Client name not found: " << clientName;
154 return false;
155 }
156 return (*clientsItr)->showConfigureDialog(parentWidget);
157}
158
159QString TranslatorEngineLoader::fallbackFirstEngine() const
160{
161 if (!d->translatorClients.isEmpty()) {
162 return *d->translatorClients.keyBegin();
163 }
164 qCWarning(TEXTTRANSLATOR_LOG) << "No plugin found ! ";
165 return QString();
166}
167
168#include "moc_translatorengineloader.cpp"
KIOCORE_EXPORT QString dir(const QString &fileClass)
QStringList libraryPaths()
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
bool isEmpty() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.