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

0001 /**
0002  * SPDX-FileCopyrightText: 2021-2022 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "fetchfeedsjob.h"
0008 
0009 #include <QTimer>
0010 
0011 #include <KLocalizedString>
0012 #include <ThreadWeaver/Queue>
0013 
0014 #include "error.h"
0015 #include "fetcher.h"
0016 #include "fetcherlogging.h"
0017 #include "models/errorlogmodel.h"
0018 #include "settingsmanager.h"
0019 #include "updatefeedjob.h"
0020 
0021 using namespace ThreadWeaver;
0022 
0023 FetchFeedsJob::FetchFeedsJob(const QStringList &urls, QObject *parent)
0024     : KJob(parent)
0025     , m_urls(urls)
0026 {
0027     for (int i = 0; i < m_urls.count(); i++) {
0028         m_feedjobs += nullptr;
0029     }
0030     connect(this, &FetchFeedsJob::processedAmountChanged, this, &FetchFeedsJob::monitorProgress);
0031     connect(this, &FetchFeedsJob::logError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
0032 }
0033 
0034 void FetchFeedsJob::start()
0035 {
0036     QTimer::singleShot(0, this, &FetchFeedsJob::fetch);
0037 }
0038 
0039 void FetchFeedsJob::fetch()
0040 {
0041     if (m_urls.count() == 0) {
0042         emitResult();
0043         return;
0044     }
0045 
0046     setTotalAmount(KJob::Unit::Items, m_urls.count());
0047     setProcessedAmount(KJob::Unit::Items, 0);
0048 
0049     qCDebug(kastsFetcher) << "Number of feed update threads:" << Queue::instance()->currentNumberOfThreads();
0050 
0051     for (int i = 0; i < m_urls.count(); i++) {
0052         QString url = m_urls[i];
0053 
0054         qCDebug(kastsFetcher) << "Starting to fetch" << url;
0055         Q_EMIT Fetcher::instance().feedUpdateStatusChanged(url, true);
0056 
0057         QNetworkRequest request((QUrl(url)));
0058         request.setTransferTimeout();
0059 
0060         QNetworkReply *reply = Fetcher::instance().get(request);
0061         connect(this, &FetchFeedsJob::aborting, reply, &QNetworkReply::abort);
0062         connect(reply, &QNetworkReply::finished, this, [this, reply, i, url]() {
0063             qCDebug(kastsFetcher) << "got networkreply for" << reply;
0064             if (reply->error()) {
0065                 if (!m_abort) {
0066                     qCDebug(kastsFetcher) << "Error fetching feed" << reply->errorString();
0067                     Q_EMIT logError(Error::Type::FeedUpdate, url, QString(), reply->error(), reply->errorString(), QString());
0068                 }
0069                 setProcessedAmount(KJob::Unit::Items, processedAmount(KJob::Unit::Items) + 1);
0070                 Q_EMIT Fetcher::instance().feedUpdateStatusChanged(url, false);
0071             } else {
0072                 QByteArray data = reply->readAll();
0073 
0074                 UpdateFeedJob *updateFeedJob = new UpdateFeedJob(url, data);
0075                 m_feedjobs[i] = updateFeedJob;
0076                 connect(this, &FetchFeedsJob::aborting, updateFeedJob, &UpdateFeedJob::abort);
0077                 connect(updateFeedJob, &UpdateFeedJob::finished, this, [this, url]() {
0078                     setProcessedAmount(KJob::Unit::Items, processedAmount(KJob::Unit::Items) + 1);
0079                     Q_EMIT Fetcher::instance().feedUpdateStatusChanged(url, false);
0080                 });
0081 
0082                 stream() << updateFeedJob;
0083                 qCDebug(kastsFetcher) << "Just started updateFeedJob" << i + 1;
0084             }
0085             reply->deleteLater();
0086         });
0087         qCDebug(kastsFetcher) << "End of retrieveFeed for" << url;
0088     }
0089     qCDebug(kastsFetcher) << "End of FetchFeedsJob::fetch";
0090 }
0091 
0092 void FetchFeedsJob::monitorProgress()
0093 {
0094     if (processedAmount(KJob::Unit::Items) == totalAmount(KJob::Unit::Items)) {
0095         emitResult();
0096     }
0097 }
0098 
0099 bool FetchFeedsJob::aborted()
0100 {
0101     return m_abort;
0102 }
0103 
0104 void FetchFeedsJob::abort()
0105 {
0106     qCDebug(kastsFetcher) << "Fetching aborted";
0107     m_abort = true;
0108     Q_EMIT aborting();
0109 }