KTextAddons

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