File indexing completed on 2024-05-12 16:21:29

0001 /**
0002  * SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include <QNetworkReply>
0008 #include <QTimer>
0009 
0010 #include <KLocalizedString>
0011 
0012 #include "enclosuredownloadjob.h"
0013 #include "fetcher.h"
0014 
0015 EnclosureDownloadJob::EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, QObject *parent)
0016     : KJob(parent)
0017     , m_url(url)
0018     , m_filename(filename)
0019     , m_title(title)
0020 {
0021     setCapabilities(Killable);
0022 }
0023 
0024 void EnclosureDownloadJob::start()
0025 {
0026     QTimer::singleShot(0, this, &EnclosureDownloadJob::startDownload);
0027 }
0028 
0029 void EnclosureDownloadJob::startDownload()
0030 {
0031     m_reply = Fetcher::instance().download(m_url, m_filename);
0032 
0033     Q_EMIT description(this, i18n("Downloading %1", m_title));
0034 
0035     connect(m_reply, &QNetworkReply::downloadProgress, this, [this](qint64 received, qint64 total) {
0036         setProcessedAmount(Bytes, received);
0037         setTotalAmount(Bytes, total);
0038     });
0039 
0040     connect(m_reply, &QNetworkReply::finished, this, [this]() {
0041         emitResult();
0042     });
0043 
0044     connect(m_reply, &QNetworkReply::errorOccurred, this, [this](QNetworkReply::NetworkError code) {
0045         setError(code);
0046         setErrorText(m_reply->errorString());
0047     });
0048 }
0049 
0050 bool EnclosureDownloadJob::doKill()
0051 {
0052     m_reply->abort();
0053     return true;
0054 }