Syndication

loader.cpp
1/*
2 loader.cpp
3 SPDX-FileCopyrightText: 2001, 2002, 2003 Frerich Raabe <raabe@kde.org>
4
5 SPDX-License-Identifier: BSD-2-Clause
6*/
7
8#include "loader.h"
9#include "dataretriever.h"
10#include "documentsource.h"
11#include "feed.h"
12#include "global.h"
13#include "loaderutil_p.h"
14#include "parsercollection.h"
15
16#include <QUrl>
17
18
19#include <syndication_debug.h>
20
21namespace Syndication
22{
23struct SYNDICATION_NO_EXPORT Loader::LoaderPrivate {
24 LoaderPrivate()
25 {
26 }
27
28 ~LoaderPrivate()
29 {
30 delete retriever;
31 }
32
33 DataRetriever *retriever = nullptr;
35 int retrieverError = 0;
36 QUrl discoveredFeedURL;
37 QUrl url;
38};
39
41{
42 return new Loader;
43}
44
45Loader *Loader::create(QObject *object, const char *slot)
46{
47 Loader *loader = create();
48 connect(loader, SIGNAL(loadingComplete(Syndication::Loader *, Syndication::FeedPtr, Syndication::ErrorCode)), object, slot);
49 return loader;
50}
51
52Loader::Loader()
53 : d(new LoaderPrivate)
54{
55}
56
57Loader::~Loader() = default;
58
59void Loader::loadFrom(const QUrl &url, DataRetriever *retriever)
60{
61 if (d->retriever != nullptr) {
62 return;
63 }
64
65 d->url = url;
66 d->retriever = retriever;
67
68 connect(d->retriever, &DataRetriever::dataRetrieved, this, &Loader::slotRetrieverDone);
69
70 d->retriever->retrieveData(url);
71}
72
74{
75 return d->retrieverError;
76}
77
79{
80 return d->lastError;
81}
82
84{
85 if (d && d->retriever) {
86 d->retriever->abort();
87 delete d->retriever;
88 d->retriever = nullptr;
89 }
90
91 Q_EMIT loadingComplete(this, FeedPtr(), Aborted);
92 delete this;
93}
94
96{
97 return d->discoveredFeedURL;
98}
99
100void Loader::slotRetrieverDone(const QByteArray &data, bool success)
101{
102 d->retrieverError = d->retriever->errorCode();
104 FeedPtr feed;
105 delete d->retriever;
106 d->retriever = nullptr;
107
108 if (success) {
109 DocumentSource src(data, d->url.url());
110 feed = parserCollection()->parse(src);
111
112 if (parserCollection()->lastError() != Syndication::Success) {
113 status = parserCollection()->lastError();
114 discoverFeeds(data);
115 }
116 } else {
117 qCDebug(SYNDICATION_LOG) << "Retriever error:" << d->retrieverError;
118 // retriever is a custom impl, so we set OtherRetrieverError
120 }
121
122 Q_EMIT loadingComplete(this, feed, status);
123
124 delete this;
125}
126
127void Loader::discoverFeeds(const QByteArray &data)
128{
129 const QUrl url = LoaderUtil::parseFeed(data, d->url);
130 if (!url.isEmpty()) {
131 d->discoveredFeedURL = url;
132 }
133}
134
135} // namespace Syndication
136
137#include "moc_loader.cpp"
Abstract baseclass for all data retriever classes.
void dataRetrieved(const QByteArray &data, bool success)
Emit this signal to tell the Loader class that the retrieval process was finished.
Represents the source of a syndication document, as read from the downloaded file.
This class is the preferred way of loading feed sources.
Definition loader.h:74
QUrl discoveredFeedURL() const
returns the URL of a feed discovered in the feed source
Definition loader.cpp:95
void loadFrom(const QUrl &url, DataRetriever *retriever)
Loads the feed source referenced by the given URL using the specified retrieval algorithm.
Definition loader.cpp:59
void loadingComplete(Syndication::Loader *loader, Syndication::FeedPtr feed, Syndication::ErrorCode error)
This signal gets emitted when the loading process triggered by calling loadFrom() finished.
void abort()
aborts the loading process
Definition loader.cpp:83
static Loader * create()
Constructs a Loader instance.
Definition loader.cpp:40
int retrieverError() const
the error code returned from the retriever.
Definition loader.cpp:73
ErrorCode errorCode() const
Retrieves the error code of the last loading process (if any).
Definition loader.cpp:78
Q_SCRIPTABLE CaptureState status()
ParserCollection< Feed > * parserCollection()
The default ParserCollection instance parsing a DocumentSource into a Feed object.
Definition global.cpp:41
ErrorCode
error code indicating fetching or parsing errors
Definition global.h:70
@ OtherRetrieverError
retriever error not covered by the error codes above.
Definition global.h:82
@ Aborted
file downloading/parsing was aborted by the user
Definition global.h:74
@ Success
No error occurred, feed was fetched and parsed successfully.
Definition global.h:71
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.