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

0001 /*
0002   SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "libretranslateengineclient.h"
0008 #include "libretranslateengineconfiguredialog.h"
0009 #include "libretranslateengineplugin.h"
0010 #include "libretranslateengineutil.h"
0011 #include "libretranslatetranslator_debug.h"
0012 #include "translator/misc/translatorutil.h"
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 #include <KSharedConfig>
0016 #include <QPointer>
0017 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0018 #include <qt5keychain/keychain.h>
0019 #else
0020 #include <qt6keychain/keychain.h>
0021 #endif
0022 
0023 LibreTranslateEngineClient::LibreTranslateEngineClient(QObject *parent)
0024     : TextTranslator::TranslatorEngineClient{parent}
0025 {
0026 }
0027 
0028 LibreTranslateEngineClient::~LibreTranslateEngineClient() = default;
0029 
0030 QString LibreTranslateEngineClient::name() const
0031 {
0032     return QStringLiteral("libretranslate");
0033 }
0034 
0035 QString LibreTranslateEngineClient::translatedName() const
0036 {
0037     return i18n("Libre Translate");
0038 }
0039 
0040 TextTranslator::TranslatorEnginePlugin *LibreTranslateEngineClient::createTranslator()
0041 {
0042     auto enginePlugin = new LibreTranslateEnginePlugin();
0043     connect(this, &LibreTranslateEngineClient::configureChanged, enginePlugin, &LibreTranslateEnginePlugin::slotConfigureChanged);
0044     return enginePlugin;
0045 }
0046 
0047 QMap<TextTranslator::TranslatorUtil::Language, QString> LibreTranslateEngineClient::supportedFromLanguages()
0048 {
0049     if (mFromLanguages.isEmpty()) {
0050         mFromLanguages = fillLanguages();
0051     }
0052     return mFromLanguages;
0053 }
0054 
0055 QMap<TextTranslator::TranslatorUtil::Language, QString> LibreTranslateEngineClient::supportedToLanguages()
0056 {
0057     return supportedFromLanguages();
0058 }
0059 
0060 bool LibreTranslateEngineClient::hasConfigurationDialog() const
0061 {
0062     return true;
0063 }
0064 
0065 bool LibreTranslateEngineClient::showConfigureDialog(QWidget *parentWidget)
0066 {
0067     bool settingsChanged = false;
0068     QPointer<LibreTranslateEngineConfigureDialog> dlg = new LibreTranslateEngineConfigureDialog(parentWidget);
0069     KConfigGroup myGroup(KSharedConfig::openConfig(), LibreTranslateEngineUtil::groupName());
0070     QString serverUrlFromConfig = myGroup.readEntry(LibreTranslateEngineUtil::serverUrlKey(), LibreTranslateEngineUtil::defaultServerUrl());
0071     if (serverUrlFromConfig.isEmpty()) {
0072         serverUrlFromConfig = LibreTranslateEngineUtil::defaultServerUrl();
0073     }
0074     serverUrlFromConfig = LibreTranslateEngineUtil::adaptUrl(serverUrlFromConfig);
0075     dlg->setServerUrl(serverUrlFromConfig);
0076     dlg->setServerRequiredApiKey(myGroup.readEntry(LibreTranslateEngineUtil::serverRequiredApiKey(), false));
0077     auto readJob = new QKeychain::ReadPasswordJob(LibreTranslateEngineUtil::translatorGroupName(), this);
0078     connect(readJob, &QKeychain::Job::finished, this, [dlg](QKeychain::Job *baseJob) {
0079         auto job = qobject_cast<QKeychain::ReadPasswordJob *>(baseJob);
0080         Q_ASSERT(job);
0081         if (job->error()) {
0082             qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) << "We have an error during reading password " << job->errorString();
0083         } else {
0084             dlg->setApiKey(job->textData());
0085         }
0086     });
0087     if (dlg->exec()) {
0088         const QString serverUrl = dlg->serverUrl();
0089         const bool requiredApiKey = dlg->serverRequiredApiKey();
0090         myGroup.writeEntry(LibreTranslateEngineUtil::serverUrlKey(), serverUrl);
0091         myGroup.writeEntry(LibreTranslateEngineUtil::serverRequiredApiKey(), requiredApiKey);
0092         auto writeJob = new QKeychain::WritePasswordJob(LibreTranslateEngineUtil::translatorGroupName(), this);
0093         connect(writeJob, &QKeychain::Job::finished, this, &LibreTranslateEngineClient::slotPasswordWritten);
0094         writeJob->setKey(LibreTranslateEngineUtil::apiGroupName());
0095         writeJob->setTextData(dlg->apiKey());
0096         writeJob->start();
0097         myGroup.sync();
0098         Q_EMIT configureChanged();
0099         settingsChanged = true;
0100     }
0101     delete dlg;
0102     return settingsChanged;
0103 }
0104 
0105 TextTranslator::TranslatorEngineClient::EngineType LibreTranslateEngineClient::engineType() const
0106 {
0107     return TextTranslator::TranslatorEngineClient::Network;
0108 }
0109 
0110 void LibreTranslateEngineClient::slotPasswordWritten(QKeychain::Job *baseJob)
0111 {
0112     if (baseJob->error()) {
0113         qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
0114     }
0115 }
0116 
0117 bool LibreTranslateEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const
0118 {
0119     switch (lang) {
0120     case TextTranslator::TranslatorUtil::sq:
0121     case TextTranslator::TranslatorUtil::af:
0122     case TextTranslator::TranslatorUtil::hy:
0123     case TextTranslator::TranslatorUtil::az:
0124     case TextTranslator::TranslatorUtil::eu:
0125     case TextTranslator::TranslatorUtil::be:
0126     case TextTranslator::TranslatorUtil::bs:
0127     case TextTranslator::TranslatorUtil::bg:
0128     case TextTranslator::TranslatorUtil::ca:
0129     case TextTranslator::TranslatorUtil::hr:
0130     case TextTranslator::TranslatorUtil::cs:
0131     case TextTranslator::TranslatorUtil::da:
0132     case TextTranslator::TranslatorUtil::nl:
0133     case TextTranslator::TranslatorUtil::et:
0134     case TextTranslator::TranslatorUtil::tl:
0135     case TextTranslator::TranslatorUtil::fi:
0136     case TextTranslator::TranslatorUtil::gl:
0137     case TextTranslator::TranslatorUtil::ka:
0138     case TextTranslator::TranslatorUtil::el:
0139     case TextTranslator::TranslatorUtil::iw:
0140     case TextTranslator::TranslatorUtil::hmong:
0141     case TextTranslator::TranslatorUtil::hu:
0142     case TextTranslator::TranslatorUtil::is:
0143     case TextTranslator::TranslatorUtil::lt:
0144     case TextTranslator::TranslatorUtil::mk:
0145     case TextTranslator::TranslatorUtil::ms:
0146     case TextTranslator::TranslatorUtil::mt:
0147     case TextTranslator::TranslatorUtil::no:
0148     case TextTranslator::TranslatorUtil::fa:
0149     case TextTranslator::TranslatorUtil::ro:
0150     case TextTranslator::TranslatorUtil::sk:
0151     case TextTranslator::TranslatorUtil::sl:
0152     case TextTranslator::TranslatorUtil::sw:
0153     case TextTranslator::TranslatorUtil::sv:
0154     case TextTranslator::TranslatorUtil::th:
0155     case TextTranslator::TranslatorUtil::uk:
0156     case TextTranslator::TranslatorUtil::ur:
0157     case TextTranslator::TranslatorUtil::cy:
0158     case TextTranslator::TranslatorUtil::yi:
0159         return false;
0160     default:
0161         break;
0162     }
0163     return true;
0164 }
0165 
0166 #include "moc_libretranslateengineclient.cpp"