File indexing completed on 2024-11-24 04:16:55

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "managermodeltranslator.h"
0008 #include "bergamotengineutils.h"
0009 #include "downloadlanguagejob.h"
0010 #include "libbergamot_debug.h"
0011 #include <KLocalizedString>
0012 #include <QFile>
0013 #include <QJsonArray>
0014 #include <QJsonDocument>
0015 #include <QJsonObject>
0016 #include <QNetworkReply>
0017 #include <QNetworkRequest>
0018 #include <TextTranslator/TranslatorEngineAccessManager>
0019 
0020 ManagerModelTranslator::ManagerModelTranslator(QObject *parent)
0021     : QObject{parent}
0022 {
0023 }
0024 
0025 ManagerModelTranslator::~ManagerModelTranslator() = default;
0026 
0027 ManagerModelTranslator *ManagerModelTranslator::self()
0028 {
0029     static ManagerModelTranslator s_self;
0030     return &s_self;
0031 }
0032 
0033 void ManagerModelTranslator::downloadListModels()
0034 {
0035     const QUrl url = QUrl(BergamotEngineUtils::defaultBergamotRepository());
0036     // qDebug() << " url " << url;
0037     QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(QNetworkRequest(url));
0038 
0039     connect(reply, &QNetworkReply::sslErrors, this, [](const QList<QSslError> &errors) {
0040         qDebug() << "Ssl Error: " << errors;
0041     });
0042     connect(reply, &QNetworkReply::finished, this, [this, reply]() {
0043         const auto readAll = reply->readAll();
0044         // qDebug() << " readAll " << readAll;
0045         parseListModel(QJsonDocument::fromJson(readAll).object());
0046         reply->deleteLater();
0047     });
0048     connect(reply, &QNetworkReply::errorOccurred, this, [this](QNetworkReply::NetworkError error) {
0049         if (error == QNetworkReply::ServiceUnavailableError) {
0050             Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
0051         } else {
0052             Q_EMIT errorText(i18n("Impossible to access to url: %1", BergamotEngineUtils::defaultBergamotRepository()));
0053         }
0054     });
0055 }
0056 
0057 void ManagerModelTranslator::loadModelList(const QString &fileName)
0058 {
0059     QFile f(fileName);
0060     if (f.open(QIODevice::ReadOnly)) {
0061         const QByteArray content = f.readAll();
0062         f.close();
0063         const QJsonDocument doc = QJsonDocument::fromJson(content);
0064         const QJsonObject fields = doc.object();
0065         parseListModel(fields);
0066     } else {
0067         qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open " << fileName;
0068     }
0069 }
0070 
0071 void ManagerModelTranslator::parseListModel(const QJsonObject &obj)
0072 {
0073     mTranslators.clear();
0074     const QJsonArray arrays = obj[QLatin1String("models")].toArray();
0075     for (const QJsonValue &current : arrays) {
0076         if (current.type() == QJsonValue::Object) {
0077             Translator translator;
0078             const QJsonObject translatorObject = current.toObject();
0079             translator.parse(translatorObject);
0080             if (translator.isValid()) {
0081                 mTranslators.append(std::move(translator));
0082             }
0083         } else {
0084             qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << " Problem during parsing" << current;
0085         }
0086     }
0087     qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " mTranslators " << mTranslators.count();
0088     Q_EMIT downLoadModelListDone();
0089 }
0090 
0091 QVector<Translator> ManagerModelTranslator::translators() const
0092 {
0093     return mTranslators;
0094 }
0095 
0096 void ManagerModelTranslator::setTranslators(const QVector<Translator> &newTranslators)
0097 {
0098     mTranslators = newTranslators;
0099 }
0100 
0101 void ManagerModelTranslator::downloadLanguage(const QString &url, const QString &checkSum)
0102 {
0103     auto downloadJob = new DownloadLanguageJob(this);
0104     downloadJob->setUrl(QUrl(url));
0105     downloadJob->setCheckSum(checkSum);
0106     const QString name = url;
0107     connect(downloadJob, &DownloadLanguageJob::errorText, this, &ManagerModelTranslator::errorText);
0108     connect(downloadJob, &DownloadLanguageJob::extractDone, this, &ManagerModelTranslator::extractDone);
0109     connect(downloadJob, &DownloadLanguageJob::downloadProgress, this, [this, url](qint64 bytesReceived, qint64 bytesTotal) {
0110         ManagerModelTranslator::ProgressInfo info;
0111         info.bytesReceived = bytesReceived;
0112         info.bytesTotal = bytesTotal;
0113         info.languageName = url;
0114         Q_EMIT progress(std::move(info));
0115     });
0116     downloadJob->start();
0117 }
0118 
0119 bool ManagerModelTranslator::needDownloadModelList() const
0120 {
0121     return mTranslators.isEmpty();
0122 }
0123 
0124 #include "moc_managermodeltranslator.cpp"