KTextAddons

deeplengineclient.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "deeplengineclient.h"
8#include "deeplengineconfiguredialog.h"
9#include "deeplengineplugin.h"
10#include "deeplengineutil.h"
11#include "deepltranslator_debug.h"
12#include "translator/misc/translatorutil.h"
13#include <KConfigGroup>
14#include <KLocalizedString>
15#include <KSharedConfig>
16#include <QPointer>
17#include <qt6keychain/keychain.h>
18
19DeeplEngineClient::DeeplEngineClient(QObject *parent)
20 : TextTranslator::TranslatorEngineClient{parent}
21{
22}
23
24DeeplEngineClient::~DeeplEngineClient() = default;
25
26QString DeeplEngineClient::name() const
27{
28 return QStringLiteral("deepl");
29}
30
31QString DeeplEngineClient::translatedName() const
32{
33 return i18n("DeepL");
34}
35
36TextTranslator::TranslatorEnginePlugin *DeeplEngineClient::createTranslator()
37{
38 auto enginePlugin = new DeeplEnginePlugin();
39 connect(this, &DeeplEngineClient::configureChanged, enginePlugin, &DeeplEnginePlugin::slotConfigureChanged);
40 return enginePlugin;
41}
42
43QMap<TextTranslator::TranslatorUtil::Language, QString> DeeplEngineClient::supportedFromLanguages()
44{
45 if (mFromLanguages.isEmpty()) {
46 mFromLanguages = fillLanguages();
47 }
48 return mFromLanguages;
49}
50
51QMap<TextTranslator::TranslatorUtil::Language, QString> DeeplEngineClient::supportedToLanguages()
52{
53 return supportedFromLanguages();
54}
55
56bool DeeplEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const
57{
58 switch (lang) {
59 case TextTranslator::TranslatorUtil::automatic:
60 case TextTranslator::TranslatorUtil::bg:
61 case TextTranslator::TranslatorUtil::cs:
62 case TextTranslator::TranslatorUtil::da:
63 case TextTranslator::TranslatorUtil::de:
64 case TextTranslator::TranslatorUtil::en:
65 case TextTranslator::TranslatorUtil::el:
66 case TextTranslator::TranslatorUtil::es:
67 case TextTranslator::TranslatorUtil::et:
68 case TextTranslator::TranslatorUtil::fi:
69 case TextTranslator::TranslatorUtil::fr:
70 case TextTranslator::TranslatorUtil::hu:
71 case TextTranslator::TranslatorUtil::id:
72 case TextTranslator::TranslatorUtil::it:
73 case TextTranslator::TranslatorUtil::ja:
74 case TextTranslator::TranslatorUtil::lt:
75 case TextTranslator::TranslatorUtil::lv:
76 case TextTranslator::TranslatorUtil::nl:
77 case TextTranslator::TranslatorUtil::pl:
78 case TextTranslator::TranslatorUtil::pt:
79 case TextTranslator::TranslatorUtil::ro:
80 case TextTranslator::TranslatorUtil::ru:
81 case TextTranslator::TranslatorUtil::sk:
82 case TextTranslator::TranslatorUtil::sl:
83 case TextTranslator::TranslatorUtil::sv:
84 case TextTranslator::TranslatorUtil::tr:
85 case TextTranslator::TranslatorUtil::uk:
86 case TextTranslator::TranslatorUtil::zh:
87 return true;
88 default:
89 break;
90 }
91 return false;
92}
93
94bool DeeplEngineClient::hasConfigurationDialog() const
95{
96 return true;
97}
98
99bool DeeplEngineClient::showConfigureDialog(QWidget *parentWidget)
100{
101 bool settingsChanged = false;
102 QPointer<DeeplEngineConfigureDialog> dlg = new DeeplEngineConfigureDialog(parentWidget);
103 KConfigGroup myGroup(KSharedConfig::openConfig(), DeeplEngineUtil::groupName());
104 dlg->setUseFreeLicenceKey(myGroup.readEntry(DeeplEngineUtil::freeLicenseKey(), false));
105
106 auto readJob = new QKeychain::ReadPasswordJob(DeeplEngineUtil::translatorGroupName(), this);
107 connect(readJob, &QKeychain::Job::finished, this, [dlg](QKeychain::Job *baseJob) {
109 Q_ASSERT(job);
110 if (job->error()) {
111 qCWarning(TRANSLATOR_DEEPL_LOG) << "We have an error during reading password " << job->errorString();
112 } else {
113 dlg->setApiKey(job->textData());
114 }
115 });
116 readJob->setKey(DeeplEngineUtil::apiGroupName());
117 readJob->start();
118 if (dlg->exec()) {
119 myGroup.writeEntry(DeeplEngineUtil::freeLicenseKey(), dlg->useFreeLicenceKey());
120 myGroup.sync();
121
122 auto writeJob = new QKeychain::WritePasswordJob(DeeplEngineUtil::translatorGroupName(), this);
123 connect(writeJob, &QKeychain::Job::finished, this, &DeeplEngineClient::slotPasswordWritten);
124 writeJob->setKey(DeeplEngineUtil::apiGroupName());
125 writeJob->setTextData(dlg->apiKey());
126 writeJob->start();
127 Q_EMIT configureChanged();
128 settingsChanged = true;
129 }
130 delete dlg;
131 return settingsChanged;
132}
133
134TextTranslator::TranslatorEngineClient::EngineType DeeplEngineClient::engineType() const
135{
136 return TextTranslator::TranslatorEngineClient::Network;
137}
138
139void DeeplEngineClient::slotPasswordWritten(QKeychain::Job *baseJob)
140{
141 if (baseJob->error()) {
142 qCWarning(TRANSLATOR_DEEPL_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
143 }
144}
145
146#include "moc_deeplengineclient.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.