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 "downloadlanguagejob.h" 0008 #include "extractlanguagejob.h" 0009 #include "libbergamot_debug.h" 0010 #include <KLocalizedString> 0011 #include <QCryptographicHash> 0012 #include <QFileInfo> 0013 #include <QNetworkReply> 0014 #include <QNetworkRequest> 0015 #include <QTemporaryFile> 0016 #include <TextTranslator/TranslatorEngineAccessManager> 0017 0018 DownloadLanguageJob::DownloadLanguageJob(QObject *parent) 0019 : QObject{parent} 0020 { 0021 } 0022 0023 DownloadLanguageJob::~DownloadLanguageJob() 0024 { 0025 delete mHash; 0026 } 0027 0028 void DownloadLanguageJob::start() 0029 { 0030 if (!canStart()) { 0031 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to start DownloadLanguageJob"; 0032 deleteLater(); 0033 return; 0034 } 0035 mDestination = new QTemporaryFile(this); 0036 if (!mDestination->open()) { 0037 Q_EMIT errorText(i18n("Cannot open file for downloading.")); 0038 delete mDestination; 0039 deleteLater(); 0040 return; 0041 } 0042 0043 mHash = new QCryptographicHash(QCryptographicHash::Sha256); 0044 0045 QNetworkRequest request(mUrl); 0046 // qDebug() << " mUrl " << mUrl; 0047 QNetworkReply *reply = TextTranslator::TranslatorEngineAccessManager::self()->networkManager()->get(request); 0048 connect(reply, &QNetworkReply::errorOccurred, this, [this, reply](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", mUrl.toString())); 0053 } 0054 }); 0055 0056 connect(reply, &QNetworkReply::downloadProgress, this, &DownloadLanguageJob::downloadProgress); 0057 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0058 mDestination->flush(); 0059 mDestination->seek(0); 0060 reply->deleteLater(); 0061 if (!mCheckSum.isEmpty() && mHash->result().toHex() != mCheckSum.toLatin1()) { 0062 // qDebug() << " mHash->result() " << mHash->result().toHex() << " mCheckSum " << mCheckSum; 0063 Q_EMIT errorText(i18n("CheckSum is not correct.")); 0064 deleteLater(); 0065 return; 0066 } else { 0067 extractLanguage(); 0068 } 0069 }); 0070 connect(reply, &QIODevice::readyRead, this, [this, reply] { 0071 const QByteArray buffer = reply->readAll(); 0072 if (mDestination->write(buffer) == -1) { 0073 Q_EMIT errorText(i18n("Error during writing on disk: %1", mDestination->errorString())); 0074 reply->abort(); 0075 } 0076 mHash->addData(buffer); 0077 }); 0078 } 0079 0080 bool DownloadLanguageJob::canStart() const 0081 { 0082 return !mUrl.isEmpty(); 0083 } 0084 0085 QUrl DownloadLanguageJob::url() const 0086 { 0087 return mUrl; 0088 } 0089 0090 void DownloadLanguageJob::setUrl(const QUrl &newUrl) 0091 { 0092 mUrl = newUrl; 0093 } 0094 0095 void DownloadLanguageJob::extractLanguage() 0096 { 0097 auto extraJob = new ExtractLanguageJob(this); 0098 extraJob->setSource(mDestination->fileName()); 0099 connect(extraJob, &ExtractLanguageJob::errorText, this, &DownloadLanguageJob::errorText); 0100 connect(extraJob, &ExtractLanguageJob::finished, this, &DownloadLanguageJob::extractDone); 0101 0102 extraJob->start(); 0103 } 0104 0105 QString DownloadLanguageJob::checkSum() const 0106 { 0107 return mCheckSum; 0108 } 0109 0110 void DownloadLanguageJob::setCheckSum(const QString &newCheckSum) 0111 { 0112 mCheckSum = newCheckSum; 0113 } 0114 0115 void DownloadLanguageJob::slotExtractDone() 0116 { 0117 Q_EMIT extractDone(); 0118 deleteLater(); 0119 } 0120 0121 #include "moc_downloadlanguagejob.cpp"