KTextAddons

managermodeltranslator.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "managermodeltranslator.h"
8#include "bergamotengineutils.h"
9#include "downloadlanguagejob.h"
10#include "libbergamot_debug.h"
11#include <KLocalizedString>
12#include <QFile>
13#include <QJsonArray>
14#include <QJsonDocument>
15#include <QJsonObject>
16#include <QNetworkReply>
17#include <QNetworkRequest>
18#include <TextTranslator/TranslatorEngineAccessManager>
19
20ManagerModelTranslator::ManagerModelTranslator(QObject *parent)
21 : QObject{parent}
22{
23}
24
25ManagerModelTranslator::~ManagerModelTranslator() = default;
26
27ManagerModelTranslator *ManagerModelTranslator::self()
28{
29 static ManagerModelTranslator s_self;
30 return &s_self;
31}
32
33void ManagerModelTranslator::downloadListModels()
34{
35 const QUrl url = QUrl(BergamotEngineUtils::defaultBergamotRepository());
36 // qDebug() << " url " << url;
37 QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(QNetworkRequest(url));
38
39 connect(reply, &QNetworkReply::sslErrors, this, [](const QList<QSslError> &errors) {
40 qDebug() << "Ssl Error: " << errors;
41 });
42 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
43 const auto readAll = reply->readAll();
44 // qDebug() << " readAll " << readAll;
45 parseListModel(QJsonDocument::fromJson(readAll).object());
46 reply->deleteLater();
47 });
50 Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
51 } else {
52 Q_EMIT errorText(i18n("Impossible to access to url: %1", BergamotEngineUtils::defaultBergamotRepository()));
53 }
54 });
55}
56
57void ManagerModelTranslator::loadModelList(const QString &fileName)
58{
59 QFile f(fileName);
60 if (f.open(QIODevice::ReadOnly)) {
61 const QByteArray content = f.readAll();
62 f.close();
63 const QJsonDocument doc = QJsonDocument::fromJson(content);
64 const QJsonObject fields = doc.object();
65 parseListModel(fields);
66 } else {
67 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open " << fileName;
68 }
69}
70
71void ManagerModelTranslator::parseListModel(const QJsonObject &obj)
72{
73 mTranslators.clear();
74 const QJsonArray arrays = obj[QLatin1String("models")].toArray();
75 for (const QJsonValue &current : arrays) {
76 if (current.type() == QJsonValue::Object) {
77 Translator translator;
78 const QJsonObject translatorObject = current.toObject();
79 translator.parse(translatorObject);
80 if (translator.isValid()) {
81 mTranslators.append(std::move(translator));
82 }
83 } else {
84 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << " Problem during parsing" << current;
85 }
86 }
87 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " mTranslators " << mTranslators.count();
88 Q_EMIT downLoadModelListDone();
89}
90
91QVector<Translator> ManagerModelTranslator::translators() const
92{
93 return mTranslators;
94}
95
96void ManagerModelTranslator::setTranslators(const QVector<Translator> &newTranslators)
97{
98 mTranslators = newTranslators;
99}
100
101void ManagerModelTranslator::downloadLanguage(const QString &url, const QString &checkSum)
102{
103 auto downloadJob = new DownloadLanguageJob(this);
104 downloadJob->setUrl(QUrl(url));
105 downloadJob->setCheckSum(checkSum);
106 const QString name = url;
107 connect(downloadJob, &DownloadLanguageJob::errorText, this, &ManagerModelTranslator::errorText);
108 connect(downloadJob, &DownloadLanguageJob::extractDone, this, &ManagerModelTranslator::extractDone);
109 connect(downloadJob, &DownloadLanguageJob::downloadProgress, this, [this, url](qint64 bytesReceived, qint64 bytesTotal) {
110 ManagerModelTranslator::ProgressInfo info;
111 info.bytesReceived = bytesReceived;
112 info.bytesTotal = bytesTotal;
113 info.languageName = url;
114 Q_EMIT progress(std::move(info));
115 });
116 downloadJob->start();
117}
118
119bool ManagerModelTranslator::needDownloadModelList() const
120{
121 return mTranslators.isEmpty();
122}
123
124#include "moc_managermodeltranslator.cpp"
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QNetworkReply * get(const QNetworkRequest &request)
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.