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 <QCryptographicHash>
12#include <QFileInfo>
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QTemporaryFile>
16#include <TextTranslator/TranslatorEngineAccessManager>
17
18DownloadLanguageJob::DownloadLanguageJob(QObject *parent)
19 : QObject{parent}
20{
21}
22
23DownloadLanguageJob::~DownloadLanguageJob()
24{
25 delete mHash;
26}
27
28void DownloadLanguageJob::start()
29{
30 if (!canStart()) {
31 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to start DownloadLanguageJob";
33 return;
34 }
35 mDestination = new QTemporaryFile(this);
36 if (!mDestination->open()) {
37 Q_EMIT errorText(i18n("Cannot open file for downloading."));
38 delete mDestination;
40 return;
41 }
42
44
45 QNetworkRequest request(mUrl);
46 // qDebug() << " mUrl " << mUrl;
47 QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(request);
48 connect(reply, &QNetworkReply::errorOccurred, this, [this, reply](QNetworkReply::NetworkError error) {
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", mUrl.toString()));
53 }
54 });
55
56 connect(reply, &QNetworkReply::downloadProgress, this, &DownloadLanguageJob::downloadProgress);
57 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
58 mDestination->flush();
59 mDestination->seek(0);
60 reply->deleteLater();
61 if (!mCheckSum.isEmpty() && mHash->result().toHex() != mCheckSum.toLatin1()) {
62 // qDebug() << " mHash->result() " << mHash->result().toHex() << " mCheckSum " << mCheckSum;
63 Q_EMIT errorText(i18n("CheckSum is not correct."));
64 deleteLater();
65 return;
66 } else {
67 extractLanguage();
68 }
69 });
70 connect(reply, &QIODevice::readyRead, this, [this, reply] {
71 const QByteArray buffer = reply->readAll();
72 if (mDestination->write(buffer) == -1) {
73 Q_EMIT errorText(i18n("Error during writing on disk: %1", mDestination->errorString()));
74 reply->abort();
75 }
76 mHash->addData(buffer);
77 });
78}
79
80bool DownloadLanguageJob::canStart() const
81{
82 return !mUrl.isEmpty();
83}
84
85QUrl DownloadLanguageJob::url() const
86{
87 return mUrl;
88}
89
90void DownloadLanguageJob::setUrl(const QUrl &newUrl)
91{
92 mUrl = newUrl;
93}
94
95void DownloadLanguageJob::extractLanguage()
96{
97 auto extraJob = new ExtractLanguageJob(this);
98 extraJob->setSource(mDestination->fileName());
99 connect(extraJob, &ExtractLanguageJob::errorText, this, &DownloadLanguageJob::errorText);
100 connect(extraJob, &ExtractLanguageJob::finished, this, &DownloadLanguageJob::extractDone);
101
102 extraJob->start();
103}
104
105QString DownloadLanguageJob::checkSum() const
106{
107 return mCheckSum;
108}
109
110void DownloadLanguageJob::setCheckSum(const QString &newCheckSum)
111{
112 mCheckSum = newCheckSum;
113}
114
115void DownloadLanguageJob::slotExtractDone()
116{
117 Q_EMIT extractDone();
118 deleteLater();
119}
120
121#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
QByteArray readAll()
void readyRead()
qint64 write(const QByteArray &data)
QNetworkReply * get(const QNetworkRequest &request)
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 Fri Jul 26 2024 11:51:28 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.