KTextAddons

libretranslateengineclient.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 "libretranslateengineclient.h"
8#include "libretranslateengineconfiguredialog.h"
9#include "libretranslateengineplugin.h"
10#include "libretranslateengineutil.h"
11#include "libretranslatetranslator_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
19LibreTranslateEngineClient::LibreTranslateEngineClient(QObject *parent)
20 : TextTranslator::TranslatorEngineClient{parent}
21{
22}
23
24LibreTranslateEngineClient::~LibreTranslateEngineClient() = default;
25
26QString LibreTranslateEngineClient::name() const
27{
28 return QStringLiteral("libretranslate");
29}
30
31QString LibreTranslateEngineClient::translatedName() const
32{
33 return i18n("Libre Translate");
34}
35
36TextTranslator::TranslatorEnginePlugin *LibreTranslateEngineClient::createTranslator()
37{
38 auto enginePlugin = new LibreTranslateEnginePlugin();
39 connect(this, &LibreTranslateEngineClient::configureChanged, enginePlugin, &LibreTranslateEnginePlugin::slotConfigureChanged);
40 return enginePlugin;
41}
42
43QMap<TextTranslator::TranslatorUtil::Language, QString> LibreTranslateEngineClient::supportedFromLanguages()
44{
45 if (mFromLanguages.isEmpty()) {
46 mFromLanguages = fillLanguages();
47 }
48 return mFromLanguages;
49}
50
51QMap<TextTranslator::TranslatorUtil::Language, QString> LibreTranslateEngineClient::supportedToLanguages()
52{
53 return supportedFromLanguages();
54}
55
56bool LibreTranslateEngineClient::hasConfigurationDialog() const
57{
58 return true;
59}
60
61bool LibreTranslateEngineClient::showConfigureDialog(QWidget *parentWidget)
62{
63 bool settingsChanged = false;
64 QPointer<LibreTranslateEngineConfigureDialog> dlg = new LibreTranslateEngineConfigureDialog(parentWidget);
65 KConfigGroup myGroup(KSharedConfig::openConfig(), LibreTranslateEngineUtil::groupName());
66 QString serverUrlFromConfig = myGroup.readEntry(LibreTranslateEngineUtil::serverUrlKey(), LibreTranslateEngineUtil::defaultServerUrl());
67 if (serverUrlFromConfig.isEmpty()) {
68 serverUrlFromConfig = LibreTranslateEngineUtil::defaultServerUrl();
69 }
70 serverUrlFromConfig = LibreTranslateEngineUtil::adaptUrl(serverUrlFromConfig);
71 dlg->setServerUrl(serverUrlFromConfig);
72 dlg->setServerRequiredApiKey(myGroup.readEntry(LibreTranslateEngineUtil::serverRequiredApiKey(), false));
73 auto readJob = new QKeychain::ReadPasswordJob(LibreTranslateEngineUtil::translatorGroupName(), this);
74 connect(readJob, &QKeychain::Job::finished, this, [dlg](QKeychain::Job *baseJob) {
76 Q_ASSERT(job);
77 if (job->error()) {
78 qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) << "We have an error during reading password " << job->errorString();
79 } else {
80 dlg->setApiKey(job->textData());
81 }
82 });
83 if (dlg->exec()) {
84 const QString serverUrl = dlg->serverUrl();
85 const bool requiredApiKey = dlg->serverRequiredApiKey();
86 myGroup.writeEntry(LibreTranslateEngineUtil::serverUrlKey(), serverUrl);
87 myGroup.writeEntry(LibreTranslateEngineUtil::serverRequiredApiKey(), requiredApiKey);
88 auto writeJob = new QKeychain::WritePasswordJob(LibreTranslateEngineUtil::translatorGroupName(), this);
89 connect(writeJob, &QKeychain::Job::finished, this, &LibreTranslateEngineClient::slotPasswordWritten);
90 writeJob->setKey(LibreTranslateEngineUtil::apiGroupName());
91 writeJob->setTextData(dlg->apiKey());
92 writeJob->start();
93 myGroup.sync();
94 Q_EMIT configureChanged();
95 settingsChanged = true;
96 }
97 delete dlg;
98 return settingsChanged;
99}
100
101TextTranslator::TranslatorEngineClient::EngineType LibreTranslateEngineClient::engineType() const
102{
103 return TextTranslator::TranslatorEngineClient::Network;
104}
105
106void LibreTranslateEngineClient::slotPasswordWritten(QKeychain::Job *baseJob)
107{
108 if (baseJob->error()) {
109 qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
110 }
111}
112
113bool LibreTranslateEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const
114{
115 switch (lang) {
116 case TextTranslator::TranslatorUtil::sq:
117 case TextTranslator::TranslatorUtil::af:
118 case TextTranslator::TranslatorUtil::hy:
119 case TextTranslator::TranslatorUtil::az:
120 case TextTranslator::TranslatorUtil::eu:
121 case TextTranslator::TranslatorUtil::be:
122 case TextTranslator::TranslatorUtil::bs:
123 case TextTranslator::TranslatorUtil::bg:
124 case TextTranslator::TranslatorUtil::ca:
125 case TextTranslator::TranslatorUtil::hr:
126 case TextTranslator::TranslatorUtil::cs:
127 case TextTranslator::TranslatorUtil::da:
128 case TextTranslator::TranslatorUtil::nl:
129 case TextTranslator::TranslatorUtil::et:
130 case TextTranslator::TranslatorUtil::tl:
131 case TextTranslator::TranslatorUtil::fi:
132 case TextTranslator::TranslatorUtil::gl:
133 case TextTranslator::TranslatorUtil::ka:
134 case TextTranslator::TranslatorUtil::el:
135 case TextTranslator::TranslatorUtil::iw:
136 case TextTranslator::TranslatorUtil::hmong:
137 case TextTranslator::TranslatorUtil::hu:
138 case TextTranslator::TranslatorUtil::is:
139 case TextTranslator::TranslatorUtil::lt:
140 case TextTranslator::TranslatorUtil::mk:
141 case TextTranslator::TranslatorUtil::ms:
142 case TextTranslator::TranslatorUtil::mt:
143 case TextTranslator::TranslatorUtil::no:
144 case TextTranslator::TranslatorUtil::fa:
145 case TextTranslator::TranslatorUtil::ro:
146 case TextTranslator::TranslatorUtil::sk:
147 case TextTranslator::TranslatorUtil::sl:
148 case TextTranslator::TranslatorUtil::sw:
149 case TextTranslator::TranslatorUtil::sv:
150 case TextTranslator::TranslatorUtil::th:
151 case TextTranslator::TranslatorUtil::uk:
152 case TextTranslator::TranslatorUtil::ur:
153 case TextTranslator::TranslatorUtil::cy:
154 case TextTranslator::TranslatorUtil::yi:
155 return false;
156 default:
157 break;
158 }
159 return true;
160}
161
162#include "moc_libretranslateengineclient.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)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.