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
40Loader *Loader::create()
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
73int Loader::retrieverError() const
74{
75 return d->retrieverError;
76}
77
78Syndication::ErrorCode Loader::errorCode() const
79{
80 return d->lastError;
81}
82
83void Loader::abort()
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
95QUrl Loader::discoveredFeedURL() const
96{
97 return d->discoveredFeedURL;
98}
99
100void Loader::slotRetrieverDone(const QByteArray &data, bool success)
101{
102 d->retrieverError = d->retriever->errorCode();
103 ErrorCode status = Success;
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"
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
@ Success
No error occurred, feed was fetched and parsed successfully.
Definition global.h:71
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:50:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.