12#include "dataretriever.h"
13#include "documentsource.h"
16#include "parsercollection.h"
18#include <kio/global.h>
21#include <QtCore/QBuffer>
22#include <QtCore/QRegExp>
23#include <QtCore/QStringList>
25#include <boost/shared_ptr.hpp>
32struct Loader::LoaderPrivate
34 LoaderPrivate() : retriever(0), lastError(
Success),
44 DataRetriever* retriever;
47 KUrl discoveredFeedURL;
65Loader::Loader() : d(new LoaderPrivate)
81 if (d->retriever != 0L)
85 d->retriever = retriever;
87 connect(d->retriever, SIGNAL(dataRetrieved(QByteArray,
bool)),
88 this, SLOT(slotRetrieverDone(QByteArray,
bool)));
90 d->retriever->retrieveData(url);
95 return d->retrieverError;
105 if (d && d->retriever)
107 d->retriever->abort();
118 return d->discoveredFeedURL;
121void Loader::slotRetrieverDone(
const QByteArray& data,
bool success)
123 d->retrieverError = d->retriever->errorCode();
126 bool isFileRetriever =
dynamic_cast<FileRetriever*
>(d->retriever) != 0;
148 std::cout <<
"file retriever error: " << d->retrieverError << std::endl;
162void Loader::discoverFeeds(
const QByteArray &data)
164 QString str = QString::fromLatin1(data.constData()).simplified();
172 QRegExp rx( QLatin1String(
"(?:REL)[^=]*=[^sAa]*(?:service.feed|ALTERNATE)[\\s]*[^s][^s](?:[^>]*)(?:HREF)[^=]*=[^A-Z0-9-_~,./$]*([^'\">\\s]*)"), Qt::CaseInsensitive );
173 if (rx.indexIn(str)!=-1)
179 QString host=d->url.host();
180 rx.setPattern(QLatin1String(
"(?:<A )[^H]*(?:HREF)[^=]*=[^A-Z0-9-_~,./]*([^'\">\\s]*)"));
182 pos = rx.indexIn( str, pos );
184 if (s2.endsWith(QLatin1String(
".rdf")) ||
185 s2.endsWith(QLatin1String(
".rss")) ||
186 s2.endsWith(QLatin1String(
".xml")))
189 pos += rx.matchedLength();
195 QStringList::const_iterator end( feeds.constEnd() );
196 for ( QStringList::const_iterator it = feeds.constBegin(); it != end; ++it ) {
198 if (testURL.host()==host)
211 if (KUrl::isRelativeUrl(s2))
213 if (s2.startsWith(QLatin1String(
"//")))
215 s2=s2.prepend(d->url.protocol()+QLatin1Char(
':'));
216 d->discoveredFeedURL=s2;
218 else if (s2.startsWith(QLatin1Char(
'/')))
220 d->discoveredFeedURL=d->url;
221 d->discoveredFeedURL.setPath(s2);
225 d->discoveredFeedURL=d->url;
226 d->discoveredFeedURL.addPath(s2);
228 d->discoveredFeedURL.cleanPath();
231 d->discoveredFeedURL=s2;
233 d->discoveredFeedURL.cleanPath();
Abstract baseclass for all data retriever classes.
Represents the source of a syndication document, as read from the downloaded file.
Implements a file retriever, to be used with Loader::loadFrom().
This class is the preferred way of loading feed sources.
KUrl discoveredFeedURL() const
returns the URL of a feed discovered in the feed source
void loadFrom(const KUrl &url, DataRetriever *retriever)
Loads the feed source referenced by the given URL using the specified retrieval algorithm.
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
static Loader * create()
Constructs a Loader instance.
int retrieverError() const
the error code returned from the retriever.
ErrorCode errorCode() const
Retrieves the error code of the last loading process (if any).
ErrorCode
error code indicating fetching or parsing errors
@ OtherRetrieverError
retriever error not covered by the error codes above.
@ FileNotFound
the host was contacted successfully, but reported a 404 error
@ Aborted
file downloading/parsing was aborted by the user
@ Success
No error occurred, feed was fetched and parsed successfully.
ParserCollection< Feed > * parserCollection()
The default ParserCollection instance parsing a DocumentSource into a Feed object.