File indexing completed on 2024-05-19 15:58:56

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "translatorenginemanager.h"
0008 #include <TextTranslator/TranslatorEngineClient>
0009 #include <TextTranslator/TranslatorEngineLoader>
0010 #include <TextTranslator/TranslatorEnginePlugin>
0011 #include <TextTranslator/TranslatorUtil>
0012 TranslatorEngineManager::TranslatorEngineManager(QObject *parent)
0013     : QObject{parent}
0014 {
0015     initializeTranslateEngine();
0016 }
0017 
0018 TranslatorEngineManager::~TranslatorEngineManager() = default;
0019 
0020 TranslatorEngineManager *TranslatorEngineManager::self()
0021 {
0022     static TranslatorEngineManager s_self;
0023     return &s_self;
0024 }
0025 
0026 void TranslatorEngineManager::translatorConfigChanged()
0027 {
0028     initializeTranslateEngine();
0029     Q_EMIT updateTranslateEngine();
0030 }
0031 
0032 void TranslatorEngineManager::initializeTranslateEngine()
0033 {
0034     delete mTranslatorEnginePlugin;
0035     const QString engineName = TextTranslator::TranslatorUtil::loadEngine();
0036     TextTranslator::TranslatorEngineClient *translatorClient = TextTranslator::TranslatorEngineLoader::self()->createTranslatorClient(engineName);
0037     if (translatorClient) {
0038         mTranslatorEnginePlugin = translatorClient->createTranslator();
0039         connect(mTranslatorEnginePlugin, &TextTranslator::TranslatorEnginePlugin::translateDone, this, &TranslatorEngineManager::slotTranslateDone);
0040         connect(mTranslatorEnginePlugin, &TextTranslator::TranslatorEnginePlugin::translateFailed, this, &TranslatorEngineManager::translateFailed);
0041     } else {
0042         mTranslatorEnginePlugin = nullptr;
0043     }
0044 }
0045 
0046 TextTranslator::TranslatorEnginePlugin *TranslatorEngineManager::translatorEngineBase() const
0047 {
0048     return mTranslatorEnginePlugin;
0049 }
0050 
0051 void TranslatorEngineManager::slotTranslateDone()
0052 {
0053     const QString result = mTranslatorEnginePlugin->resultTranslate();
0054     Q_EMIT translateDone(result);
0055 }
0056 
0057 #include "moc_translatorenginemanager.cpp"