Syndication

loader.cpp
1 /*
2  loader.cpp
3  SPDX-FileCopyrightText: 2001, 2002, 2003 Frerich Raabe <[email protected]>
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 
21 namespace Syndication
22 {
23 struct SYNDICATION_NO_EXPORT Loader::LoaderPrivate {
24  LoaderPrivate()
25  {
26  }
27 
28  ~LoaderPrivate()
29  {
30  delete retriever;
31  }
32 
33  DataRetriever *retriever = nullptr;
34  Syndication::ErrorCode lastError = Success;
35  int retrieverError = 0;
36  QUrl discoveredFeedURL;
37  QUrl url;
38 };
39 
40 Loader *Loader::create()
41 {
42  return new Loader;
43 }
44 
45 Loader *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 
52 Loader::Loader()
53  : d(new LoaderPrivate)
54 {
55 }
56 
57 Loader::~Loader() = default;
58 
59 void 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 
73 int Loader::retrieverError() const
74 {
75  return d->retrieverError;
76 }
77 
78 Syndication::ErrorCode Loader::errorCode() const
79 {
80  return d->lastError;
81 }
82 
83 void 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 
95 QUrl Loader::discoveredFeedURL() const
96 {
97  return d->discoveredFeedURL;
98 }
99 
100 void 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 
127 void 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
bool isEmpty() const const
Q_SCRIPTABLE CaptureState status()
QAction * create(StandardGameAction id, const QObject *recvr, const char *slot, QObject *parent)
@ 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
ErrorCode
error code indicating fetching or parsing errors
Definition: global.h:70
Q_SCRIPTABLE Q_NOREPLY void abort()
ParserCollection< Feed > * parserCollection()
The default ParserCollection instance parsing a DocumentSource into a Feed object.
Definition: global.cpp:41
void dataRetrieved(const QByteArray &data, bool success)
Emit this signal to tell the Loader class that the retrieval process was finished.
@ Success
No error occurred, feed was fetched and parsed successfully.
Definition: global.h:71
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Jun 6 2023 03:56:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.