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>
30 namespace Syndication {
32 struct Loader::LoaderPrivate
34 LoaderPrivate() : retriever(0), lastError(
Success),
44 DataRetriever* retriever;
65 Loader::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;
121 void 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;
162 void 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();