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>
17#include <qt6keychain/keychain.h>
19LibreTranslateEngineClient::LibreTranslateEngineClient(
QObject *parent)
20 : TextTranslator::TranslatorEngineClient{parent}
24LibreTranslateEngineClient::~LibreTranslateEngineClient() =
default;
26QString LibreTranslateEngineClient::name()
const
28 return QStringLiteral(
"libretranslate");
31QString LibreTranslateEngineClient::translatedName()
const
33 return i18n(
"Libre Translate");
36TextTranslator::TranslatorEnginePlugin *LibreTranslateEngineClient::createTranslator()
38 auto enginePlugin =
new LibreTranslateEnginePlugin();
39 connect(
this, &LibreTranslateEngineClient::configureChanged, enginePlugin, &LibreTranslateEnginePlugin::slotConfigureChanged);
46 mFromLanguages = fillLanguages();
48 return mFromLanguages;
53 return supportedFromLanguages();
56bool LibreTranslateEngineClient::hasConfigurationDialog()
const
61bool LibreTranslateEngineClient::showConfigureDialog(
QWidget *parentWidget)
63 bool settingsChanged =
false;
66 QString serverUrlFromConfig = myGroup.readEntry(LibreTranslateEngineUtil::serverUrlKey(), LibreTranslateEngineUtil::defaultServerUrl());
67 if (serverUrlFromConfig.
isEmpty()) {
68 serverUrlFromConfig = LibreTranslateEngineUtil::defaultServerUrl();
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) {
78 qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) <<
"We have an error during reading password " << job->errorString();
80 dlg->setApiKey(job->textData());
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());
95 settingsChanged =
true;
98 return settingsChanged;
101TextTranslator::TranslatorEngineClient::EngineType LibreTranslateEngineClient::engineType()
const
103 return TextTranslator::TranslatorEngineClient::Network;
106void LibreTranslateEngineClient::slotPasswordWritten(QKeychain::Job *baseJob)
108 if (baseJob->error()) {
109 qCWarning(TRANSLATOR_LIBRETRANSLATE_LOG) <<
"Error writing password using QKeychain:" << baseJob->errorString();
113bool LibreTranslateEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang)
const
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:
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
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
bool isEmpty() const const