7#include "translatorengineloader.h"
9#include "texttranslator_debug.h"
10#include "translatorengineclient.h"
11#include <QCoreApplication>
13#include <QPluginLoader>
15using namespace TextTranslator;
16class TextTranslator::TranslatorEngineLoaderPrivate
23TranslatorEngineLoader *TranslatorEngineLoader::self()
25 static TranslatorEngineLoader s_self;
29TranslatorEngineLoader::TranslatorEngineLoader(
QObject *parent)
31 , d(new TextTranslator::TranslatorEngineLoaderPrivate)
36TranslatorEngineLoader::~TranslatorEngineLoader() =
default;
38void TranslatorEngineLoader::loadPlugins()
41 const QString pathSuffix(QStringLiteral(
"/kf6/translator/"));
42 for (
const QString &libPath : libPaths) {
48 loadPlugin(
dir.absoluteFilePath(fileName));
51 if (d->loadedPlugins.isEmpty()) {
52 qCWarning(TEXTTRANSLATOR_LOG) <<
"No translator plugins available!";
56void TranslatorEngineLoader::loadPlugin(
const QString &pluginPath)
59 const QString pluginIID = plugin.metaData()[
"IID"_L1].toString();
61 if (d->loadedPlugins.contains(pluginIID)) {
62 qCDebug(TEXTTRANSLATOR_LOG) <<
"Skipping already loaded" << pluginPath;
65 d->loadedPlugins.insert(pluginIID);
69 qCDebug(TEXTTRANSLATOR_LOG) <<
"Unable to load plugin" << pluginPath <<
"Error:" << plugin.errorString();
70 d->loadedPlugins.remove(pluginIID);
75 qCWarning(TEXTTRANSLATOR_LOG) <<
"Invalid plugin loaded" << pluginPath;
79 d->translatorClients.insert(client->name(), client);
82TranslatorEngineClient *TranslatorEngineLoader::createTranslatorClient(
const QString &clientName)
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();
93QString TranslatorEngineLoader::currentPluginName(
const QString &key)
const
95 auto clientsItr = d->translatorClients.constFind(key);
96 if (clientsItr != d->translatorClients.constEnd()) {
97 return (*clientsItr)->translatedName();
106 while (i.hasNext()) {
108 map.insert(i.key(), i.value()->translatedName() + QStringLiteral(
" (%1)").arg(TranslatorEngineClient::convertEngineType(i.value()->engineType())));
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;
121 (*clientsItr)->updateListLanguages();
122 supportedLanguages = (*clientsItr)->supportedFromLanguages();
123 return supportedLanguages;
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;
134 (*clientsItr)->updateListLanguages();
135 supportedLanguages = (*clientsItr)->supportedToLanguages();
136 return supportedLanguages;
139bool TranslatorEngineLoader::hasConfigurationDialog(
const QString &clientName)
const
141 auto clientsItr = d->translatorClients.constFind(clientName);
142 if (clientsItr == d->translatorClients.constEnd()) {
143 qCWarning(TEXTTRANSLATOR_LOG) <<
"Client name not found: " << clientName;
146 return (*clientsItr)->hasConfigurationDialog();
149bool TranslatorEngineLoader::showConfigureDialog(
const QString &clientName,
QWidget *parentWidget)
151 auto clientsItr = d->translatorClients.constFind(clientName);
152 if (clientsItr == d->translatorClients.constEnd()) {
153 qCWarning(TEXTTRANSLATOR_LOG) <<
"Client name not found: " << clientName;
156 return (*clientsItr)->showConfigureDialog(parentWidget);
159QString TranslatorEngineLoader::fallbackFirstEngine()
const
161 if (!d->translatorClients.isEmpty()) {
162 return *d->translatorClients.keyBegin();
164 qCWarning(TEXTTRANSLATOR_LOG) <<
"No plugin found ! ";
168#include "moc_translatorengineloader.cpp"
KIOCORE_EXPORT QString dir(const QString &fileClass)
QStringList libraryPaths()
T qobject_cast(QObject *object)
bool isEmpty() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)