File indexing completed on 2024-11-24 04:16:54

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "bergamotengineclient.h"
0008 #include "begamotenginedialog.h"
0009 #include "bergamotengineplugin.h"
0010 #include "bergamotengineutils.h"
0011 #include "translator/misc/translatorutil.h"
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KSharedConfig>
0015 #include <QPointer>
0016 
0017 BergamotEngineClient::BergamotEngineClient(QObject *parent)
0018     : TextTranslator::TranslatorEngineClient{parent}
0019 {
0020     updateInstalledLanguage();
0021 }
0022 
0023 BergamotEngineClient::~BergamotEngineClient() = default;
0024 
0025 QString BergamotEngineClient::name() const
0026 {
0027     return QStringLiteral("bergamot");
0028 }
0029 
0030 QString BergamotEngineClient::translatedName() const
0031 {
0032     return i18n("Bergamot");
0033 }
0034 
0035 TextTranslator::TranslatorEnginePlugin *BergamotEngineClient::createTranslator()
0036 {
0037     auto enginePlugin = new BergamotEnginePlugin();
0038     connect(this, &BergamotEngineClient::configureChanged, enginePlugin, &BergamotEnginePlugin::slotConfigureChanged);
0039     return enginePlugin;
0040 }
0041 
0042 QMap<TextTranslator::TranslatorUtil::Language, QString> BergamotEngineClient::supportedFromLanguages()
0043 {
0044     return mFromLanguages;
0045 }
0046 
0047 QMap<TextTranslator::TranslatorUtil::Language, QString> BergamotEngineClient::supportedToLanguages()
0048 {
0049     return mToLanguages;
0050 }
0051 
0052 bool BergamotEngineClient::hasConfigurationDialog() const
0053 {
0054     return true;
0055 }
0056 
0057 bool BergamotEngineClient::showConfigureDialog(QWidget *parentWidget)
0058 {
0059     bool settingsChanged = false;
0060     KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
0061     QPointer<BegamotEngineDialog> dlg = new BegamotEngineDialog(parentWidget);
0062     BergamotEngineUtils::SettingsInfo info;
0063     info.loadSettingsInfo();
0064     dlg->setSettingsInfo(info);
0065     if (dlg->exec()) {
0066         info = dlg->settingsInfo();
0067         info.saveSettingsInfo();
0068         updateInstalledLanguage();
0069         Q_EMIT configureChanged();
0070         settingsChanged = true;
0071     }
0072     delete dlg;
0073     return settingsChanged;
0074 }
0075 
0076 void BergamotEngineClient::updateInstalledLanguage()
0077 {
0078     mLanguageInstalled = BergamotEngineUtils::languageLocallyStored();
0079     updateFromLanguageList();
0080     updateToLanguageList();
0081 }
0082 
0083 void BergamotEngineClient::updateFromLanguageList()
0084 {
0085     mFromLanguages.clear();
0086     for (const auto &lang : std::as_const(mLanguageInstalled)) {
0087         const TextTranslator::TranslatorUtil::Language langUtil = TextTranslator::TranslatorUtil::stringToLanguage(lang.from);
0088         mFromLanguages.insert(langUtil, TextTranslator::TranslatorUtil::translatedLanguage(langUtil));
0089         // qDebug() << " SSSSSSSSSSSSSSSSSS " << langUtil << " mFromLanguages " << mFromLanguages;
0090     }
0091 }
0092 
0093 void BergamotEngineClient::updateToLanguageList()
0094 {
0095     mToLanguages.clear();
0096     for (const auto &lang : std::as_const(mLanguageInstalled)) {
0097         // qDebug() << " lang.to " << lang.to << " mLanguageCode " << mLanguageCode;
0098         if (lang.to == mLanguageCode || mLanguageCode.isEmpty()) {
0099             const TextTranslator::TranslatorUtil::Language langUtil = TextTranslator::TranslatorUtil::stringToLanguage(lang.to);
0100             // qDebug() << " lang.to " << lang.to;
0101             mToLanguages.insert(langUtil, TextTranslator::TranslatorUtil::translatedLanguage(langUtil));
0102         }
0103     }
0104 }
0105 
0106 bool BergamotEngineClient::hasInvertSupport() const
0107 {
0108     return false;
0109 }
0110 
0111 void BergamotEngineClient::generateToListFromCurrentToLanguage(const QString &languageCode)
0112 {
0113     mLanguageCode = languageCode;
0114     updateToLanguageList();
0115 }
0116 
0117 TextTranslator::TranslatorEngineClient::EngineType BergamotEngineClient::engineType() const
0118 {
0119     return TextTranslator::TranslatorEngineClient::Locale;
0120 }
0121 
0122 bool BergamotEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const
0123 {
0124     Q_UNUSED(lang);
0125     return true;
0126 }
0127 
0128 void BergamotEngineClient::updateListLanguages()
0129 {
0130     updateInstalledLanguage();
0131 }
0132 
0133 #include "moc_bergamotengineclient.cpp"