File indexing completed on 2024-11-24 04:16:55
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 "deeplengineclient.h" 0008 #include "deeplengineconfiguredialog.h" 0009 #include "deeplengineplugin.h" 0010 #include "deeplengineutil.h" 0011 #include "deepltranslator_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 DeeplEngineClient::DeeplEngineClient(QObject *parent) 0024 : TextTranslator::TranslatorEngineClient{parent} 0025 { 0026 } 0027 0028 DeeplEngineClient::~DeeplEngineClient() = default; 0029 0030 QString DeeplEngineClient::name() const 0031 { 0032 return QStringLiteral("deepl"); 0033 } 0034 0035 QString DeeplEngineClient::translatedName() const 0036 { 0037 return i18n("DeepL"); 0038 } 0039 0040 TextTranslator::TranslatorEnginePlugin *DeeplEngineClient::createTranslator() 0041 { 0042 auto enginePlugin = new DeeplEnginePlugin(); 0043 connect(this, &DeeplEngineClient::configureChanged, enginePlugin, &DeeplEnginePlugin::slotConfigureChanged); 0044 return enginePlugin; 0045 } 0046 0047 QMap<TextTranslator::TranslatorUtil::Language, QString> DeeplEngineClient::supportedFromLanguages() 0048 { 0049 if (mFromLanguages.isEmpty()) { 0050 mFromLanguages = fillLanguages(); 0051 } 0052 return mFromLanguages; 0053 } 0054 0055 QMap<TextTranslator::TranslatorUtil::Language, QString> DeeplEngineClient::supportedToLanguages() 0056 { 0057 return supportedFromLanguages(); 0058 } 0059 0060 bool DeeplEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const 0061 { 0062 switch (lang) { 0063 case TextTranslator::TranslatorUtil::automatic: 0064 case TextTranslator::TranslatorUtil::bg: 0065 case TextTranslator::TranslatorUtil::cs: 0066 case TextTranslator::TranslatorUtil::da: 0067 case TextTranslator::TranslatorUtil::de: 0068 case TextTranslator::TranslatorUtil::en: 0069 case TextTranslator::TranslatorUtil::el: 0070 case TextTranslator::TranslatorUtil::es: 0071 case TextTranslator::TranslatorUtil::et: 0072 case TextTranslator::TranslatorUtil::fi: 0073 case TextTranslator::TranslatorUtil::fr: 0074 case TextTranslator::TranslatorUtil::hu: 0075 case TextTranslator::TranslatorUtil::id: 0076 case TextTranslator::TranslatorUtil::it: 0077 case TextTranslator::TranslatorUtil::ja: 0078 case TextTranslator::TranslatorUtil::lt: 0079 case TextTranslator::TranslatorUtil::lv: 0080 case TextTranslator::TranslatorUtil::nl: 0081 case TextTranslator::TranslatorUtil::pl: 0082 case TextTranslator::TranslatorUtil::pt: 0083 case TextTranslator::TranslatorUtil::ro: 0084 case TextTranslator::TranslatorUtil::ru: 0085 case TextTranslator::TranslatorUtil::sk: 0086 case TextTranslator::TranslatorUtil::sl: 0087 case TextTranslator::TranslatorUtil::sv: 0088 case TextTranslator::TranslatorUtil::tr: 0089 case TextTranslator::TranslatorUtil::uk: 0090 case TextTranslator::TranslatorUtil::zh: 0091 return true; 0092 default: 0093 break; 0094 } 0095 return false; 0096 } 0097 0098 bool DeeplEngineClient::hasConfigurationDialog() const 0099 { 0100 return true; 0101 } 0102 0103 bool DeeplEngineClient::showConfigureDialog(QWidget *parentWidget) 0104 { 0105 bool settingsChanged = false; 0106 QPointer<DeeplEngineConfigureDialog> dlg = new DeeplEngineConfigureDialog(parentWidget); 0107 KConfigGroup myGroup(KSharedConfig::openConfig(), DeeplEngineUtil::groupName()); 0108 dlg->setUseFreeLicenceKey(myGroup.readEntry(DeeplEngineUtil::freeLicenseKey(), false)); 0109 0110 auto readJob = new QKeychain::ReadPasswordJob(DeeplEngineUtil::translatorGroupName(), this); 0111 connect(readJob, &QKeychain::Job::finished, this, [dlg](QKeychain::Job *baseJob) { 0112 auto job = qobject_cast<QKeychain::ReadPasswordJob *>(baseJob); 0113 Q_ASSERT(job); 0114 if (job->error()) { 0115 qCWarning(TRANSLATOR_DEEPL_LOG) << "We have an error during reading password " << job->errorString(); 0116 } else { 0117 dlg->setApiKey(job->textData()); 0118 } 0119 }); 0120 readJob->setKey(DeeplEngineUtil::apiGroupName()); 0121 readJob->start(); 0122 if (dlg->exec()) { 0123 myGroup.writeEntry(DeeplEngineUtil::freeLicenseKey(), dlg->useFreeLicenceKey()); 0124 myGroup.sync(); 0125 0126 auto writeJob = new QKeychain::WritePasswordJob(DeeplEngineUtil::translatorGroupName(), this); 0127 connect(writeJob, &QKeychain::Job::finished, this, &DeeplEngineClient::slotPasswordWritten); 0128 writeJob->setKey(DeeplEngineUtil::apiGroupName()); 0129 writeJob->setTextData(dlg->apiKey()); 0130 writeJob->start(); 0131 Q_EMIT configureChanged(); 0132 settingsChanged = true; 0133 } 0134 delete dlg; 0135 return settingsChanged; 0136 } 0137 0138 TextTranslator::TranslatorEngineClient::EngineType DeeplEngineClient::engineType() const 0139 { 0140 return TextTranslator::TranslatorEngineClient::Network; 0141 } 0142 0143 void DeeplEngineClient::slotPasswordWritten(QKeychain::Job *baseJob) 0144 { 0145 if (baseJob->error()) { 0146 qCWarning(TRANSLATOR_DEEPL_LOG) << "Error writing password using QKeychain:" << baseJob->errorString(); 0147 } 0148 } 0149 0150 #include "moc_deeplengineclient.cpp"