KTextAddons

downloadlanguagejob.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "downloadlanguagejob.h"
8#include "extractlanguagejob.h"
9#include "libbergamot_debug.h"
10#include <KLocalizedString>
11#include <QNetworkReply>
12#include <QNetworkRequest>
13#include <QTemporaryFile>
14#include <TextTranslator/TranslatorEngineAccessManager>
15
16DownloadLanguageJob::DownloadLanguageJob(QObject *parent)
17 : QObject{parent}
18{
19}
20
21DownloadLanguageJob::~DownloadLanguageJob()
22{
23 delete mHash;
24}
25
26void DownloadLanguageJob::start()
27{
28 if (!canStart()) {
29 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to start DownloadLanguageJob";
31 return;
32 }
33 mDestination = new QTemporaryFile(this);
34 if (!mDestination->open()) {
35 Q_EMIT errorText(i18n("Cannot open file for downloading."));
36 delete mDestination;
38 return;
39 }
40
41 mHash = new QCryptographicHash(QCryptographicHash::Sha256);
42
43 QNetworkRequest request(mUrl);
44 // qDebug() << " mUrl " << mUrl;
45 QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(request);
46 connect(reply, &QNetworkReply::errorOccurred, this, [this, reply](QNetworkReply::NetworkError error) {
48 Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
49 } else {
50 Q_EMIT errorText(i18n("Impossible to access to url: %1", mUrl.toString()));
51 }
52 });
53
54 connect(reply, &QNetworkReply::downloadProgress, this, &DownloadLanguageJob::downloadProgress);
55 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
56 mDestination->flush();
57 mDestination->seek(0);
58 reply->deleteLater();
59 if (!mCheckSum.isEmpty() && mHash->result().toHex() != mCheckSum.toLatin1()) {
60 // qDebug() << " mHash->result() " << mHash->result().toHex() << " mCheckSum " << mCheckSum;
61 Q_EMIT errorText(i18n("CheckSum is not correct."));
63 return;
64 } else {
65 extractLanguage();
66 }
67 });
68 connect(reply, &QIODevice::readyRead, this, [this, reply] {
69 const QByteArray buffer = reply->readAll();
70 if (mDestination->write(buffer) == -1) {
71 Q_EMIT errorText(i18n("Error during writing on disk: %1", mDestination->errorString()));
72 reply->abort();
73 }
74 mHash->addData(buffer);
75 });
76}
77
78bool DownloadLanguageJob::canStart() const
79{
80 return !mUrl.isEmpty();
81}
82
83QUrl DownloadLanguageJob::url() const
84{
85 return mUrl;
86}
87
88void DownloadLanguageJob::setUrl(const QUrl &newUrl)
89{
90 mUrl = newUrl;
91}
92
93void DownloadLanguageJob::extractLanguage()
94{
95 auto extraJob = new ExtractLanguageJob(this);
96 extraJob->setSource(mDestination->fileName());
97 connect(extraJob, &ExtractLanguageJob::errorText, this, &DownloadLanguageJob::errorText);
98 connect(extraJob, &ExtractLanguageJob::finished, this, &DownloadLanguageJob::extractDone);
99
100 extraJob->start();
101}
102
103QString DownloadLanguageJob::checkSum() const
104{
105 return mCheckSum;
106}
107
108void DownloadLanguageJob::setCheckSum(const QString &newCheckSum)
109{
110 mCheckSum = newCheckSum;
111}
112
113void DownloadLanguageJob::slotExtractDone()
114{
115 Q_EMIT extractDone();
116 deleteLater();
117}
118
119#include "moc_downloadlanguagejob.cpp"
QString i18n(const char *text, const TYPE &arg...)
QByteArray readAll()
void readyRead()
QNetworkReply * get(const QNetworkRequest &request)
virtual void abort()=0
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:24 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.