File indexing completed on 2024-07-14 14:40:04

0001 /*
0002     loader.cpp
0003     SPDX-FileCopyrightText: 2001, 2002, 2003 Frerich Raabe <raabe@kde.org>
0004 
0005     SPDX-License-Identifier: BSD-2-Clause
0006 */
0007 
0008 #include "loader.h"
0009 #include "dataretriever.h"
0010 #include "documentsource.h"
0011 #include "feed.h"
0012 #include "global.h"
0013 #include "loaderutil_p.h"
0014 #include "parsercollection.h"
0015 
0016 #include <QUrl>
0017 
0018 
0019 #include <syndication_debug.h>
0020 
0021 namespace Syndication
0022 {
0023 struct SYNDICATION_NO_EXPORT Loader::LoaderPrivate {
0024     LoaderPrivate()
0025     {
0026     }
0027 
0028     ~LoaderPrivate()
0029     {
0030         delete retriever;
0031     }
0032 
0033     DataRetriever *retriever = nullptr;
0034     Syndication::ErrorCode lastError = Success;
0035     int retrieverError = 0;
0036     QUrl discoveredFeedURL;
0037     QUrl url;
0038 };
0039 
0040 Loader *Loader::create()
0041 {
0042     return new Loader;
0043 }
0044 
0045 Loader *Loader::create(QObject *object, const char *slot)
0046 {
0047     Loader *loader = create();
0048     connect(loader, SIGNAL(loadingComplete(Syndication::Loader *, Syndication::FeedPtr, Syndication::ErrorCode)), object, slot);
0049     return loader;
0050 }
0051 
0052 Loader::Loader()
0053     : d(new LoaderPrivate)
0054 {
0055 }
0056 
0057 Loader::~Loader()
0058 {
0059     delete d;
0060 }
0061 
0062 void Loader::loadFrom(const QUrl &url, DataRetriever *retriever)
0063 {
0064     if (d->retriever != nullptr) {
0065         return;
0066     }
0067 
0068     d->url = url;
0069     d->retriever = retriever;
0070 
0071     connect(d->retriever, &DataRetriever::dataRetrieved, this, &Loader::slotRetrieverDone);
0072 
0073     d->retriever->retrieveData(url);
0074 }
0075 
0076 int Loader::retrieverError() const
0077 {
0078     return d->retrieverError;
0079 }
0080 
0081 Syndication::ErrorCode Loader::errorCode() const
0082 {
0083     return d->lastError;
0084 }
0085 
0086 void Loader::abort()
0087 {
0088     if (d && d->retriever) {
0089         d->retriever->abort();
0090         delete d->retriever;
0091         d->retriever = nullptr;
0092     }
0093 
0094     Q_EMIT loadingComplete(this, FeedPtr(), Aborted);
0095     delete this;
0096 }
0097 
0098 QUrl Loader::discoveredFeedURL() const
0099 {
0100     return d->discoveredFeedURL;
0101 }
0102 
0103 void Loader::slotRetrieverDone(const QByteArray &data, bool success)
0104 {
0105     d->retrieverError = d->retriever->errorCode();
0106     ErrorCode status = Success;
0107     FeedPtr feed;
0108     delete d->retriever;
0109     d->retriever = nullptr;
0110 
0111     if (success) {
0112         DocumentSource src(data, d->url.url());
0113         feed = parserCollection()->parse(src);
0114 
0115         if (parserCollection()->lastError() != Syndication::Success) {
0116             status = parserCollection()->lastError();
0117             discoverFeeds(data);
0118         }
0119     } else {
0120         qCDebug(SYNDICATION_LOG) << "Retriever error:" << d->retrieverError;
0121         // retriever is a custom impl, so we set OtherRetrieverError
0122         status = OtherRetrieverError;
0123     }
0124 
0125     Q_EMIT loadingComplete(this, feed, status);
0126 
0127     delete this;
0128 }
0129 
0130 void Loader::discoverFeeds(const QByteArray &data)
0131 {
0132     const QUrl url = LoaderUtil::parseFeed(data, d->url);
0133     if (!url.isEmpty()) {
0134         d->discoveredFeedURL = url;
0135     }
0136 }
0137 
0138 } // namespace Syndication
0139 
0140 #include "moc_loader.cpp"