KTextAddons

lingvaengineplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5
6 based on digikam onlinetranslator code
7
8*/
9
10#include "lingvaengineplugin.h"
11using namespace Qt::Literals::StringLiterals;
12
13#include "lingvaengineutil.h"
14#include "lingvatranslator_debug.h"
15#include <TextTranslator/TranslatorEngineAccessManager>
16
17#include <KConfigGroup>
18#include <KSharedConfig>
19
20#include <QJsonDocument>
21#include <QJsonObject>
22
23LingvaEnginePlugin::LingvaEnginePlugin(QObject *parent)
24 : TextTranslator::TranslatorEnginePlugin(parent)
25{
26 loadSettings();
27}
28
29LingvaEnginePlugin::~LingvaEnginePlugin() = default;
30
31void LingvaEnginePlugin::translate()
32{
33 if (verifyFromAndToLanguage()) {
34 return;
35 }
36 translateText();
37}
38
39void LingvaEnginePlugin::translateText()
40{
41 clear();
42
43 const QUrl url(QStringLiteral("%1/api/v1/%2/%3/%4")
44 .arg(mServerUrl, languageCode(from()), languageCode(to()), QString::fromUtf8(QUrl::toPercentEncoding(inputText()))));
45
46 qCDebug(TRANSLATOR_LINGVA_LOG) << " url " << url;
47 const QNetworkRequest request(url);
48
49 QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(request);
50 connect(reply, &QNetworkReply::errorOccurred, this, [this, reply](QNetworkReply::NetworkError error) {
51 slotError(error);
52 reply->deleteLater();
53 });
54 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
55 reply->deleteLater();
56 parseTranslation(reply);
57 });
58}
59
60void LingvaEnginePlugin::parseTranslation(QNetworkReply *reply)
61{
62 if (reply->error() != QNetworkReply::NoError) {
63 Q_EMIT translateFailed(reply->errorString());
64 reply->deleteLater();
65 return;
66 }
67
68 const QJsonDocument jsonResponse = QJsonDocument::fromJson(reply->readAll());
69 if (hasDebug()) {
70 setJsonDebug(QString::fromUtf8(jsonResponse.toJson(QJsonDocument::Indented)));
71 }
72
73 const QJsonObject responseObject = jsonResponse.object();
74 setResult(responseObject.value(QStringLiteral("translation")).toString());
75 reply->deleteLater();
76 qCDebug(TRANSLATOR_LINGVA_LOG) << " result " << result();
77 Q_EMIT translateDone();
78}
79
80void LingvaEnginePlugin::loadSettings()
81{
82 KConfigGroup myGroup(KSharedConfig::openConfig(), LingvaEngineUtil::groupName());
83 mServerUrl = myGroup.readEntry(LingvaEngineUtil::serverUrlKey(), LingvaEngineUtil::defaultServerUrl());
84}
85
86void LingvaEnginePlugin::slotConfigureChanged()
87{
88 loadSettings();
89}
90
91QString LingvaEnginePlugin::languageCode(const QString &langStr)
92{
93 if (langStr == "zh"_L1) {
94 return QStringLiteral("zh");
95 } else if (langStr == "zt"_L1) {
96 return QStringLiteral("zh_HANT");
97 }
98 return langStr;
99}
100
101#include "moc_lingvaengineplugin.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
char * toString(const EngineQuery &query)
QString errorString() const const
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QByteArray toJson(JsonFormat format) const const
QJsonValue value(QLatin1StringView key) const const
QNetworkReply * get(const QNetworkRequest &request)
NetworkError error() const const
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString fromUtf8(QByteArrayView str)
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.