Line data Source code
1 : #ifndef PARSER_H
2 : #define PARSER_H
3 :
4 : #include <QObject>
5 :
6 : #include <QString>
7 : #include <QNetworkReply>
8 : #include <QThread>
9 :
10 : #include "../network/FangNetworkAccessManager.h"
11 : #include "ParserInterface.h"
12 :
13 : // Max number of HTTP redirects to prevent looping.
14 : #define MAX_PARSER_REDIRECTS 10
15 :
16 : /*!
17 : PaRSSes RSS/Atom feeds into RawFeed/RawNews objects.
18 : */
19 : class NewsParser : public ParserInterface
20 : {
21 : Q_OBJECT
22 :
23 : public:
24 : explicit NewsParser(QObject *parent = nullptr);
25 : explicit NewsParser(QNetworkAccessManager* networkManager, QObject *parent = nullptr);
26 : virtual ~NewsParser();
27 :
28 : public slots:
29 : virtual void parse(const QUrl& url, bool noParseIfCached = false,
30 : const QString& ifNoneMatch = QString(),
31 : const QString& ifModifiedSince = QString()); // Override.
32 :
33 : // For testing purposes.
34 : void parseFile(const QString& filename);
35 :
36 : virtual ParseResult getResult(); // Override.
37 0 : virtual QNetworkReply::NetworkError getNetworkError() { return networkError; }
38 : virtual RawFeed* getFeed(); // Override.
39 5 : virtual inline QUrl getURL() { return finalFeedURL; } // Override.
40 :
41 0 : virtual bool isFromCache() { return fromCache; } // Override
42 0 : virtual QString responseEtag() { return respEtag; }
43 0 : virtual QString responseLastModified() { return respLastModified; }
44 5 : bool wasPermanentRedirect() const { return permanentRedirect; }
45 :
46 : // These are used internally.
47 : signals:
48 :
49 : // Call this prior to adding XML.
50 : void triggerDocStart();
51 :
52 : // Call this when you're done adding XML.
53 : void triggerDocEnd();
54 :
55 : // Add a blurb of XML to parse.
56 : void triggerAddXML(QByteArray data);
57 :
58 : protected slots:
59 : void readyRead();
60 : void metaDataChanged();
61 : void error(QNetworkReply::NetworkError);
62 : void netFinished(QNetworkReply *reply);
63 :
64 : // When the worker thread thinks the document is complete, or an error occured.
65 : void workerDone(RawFeed* rawFeed);
66 :
67 : private:
68 : void initParse(const QUrl& url = QUrl("")); // called prior to parse.
69 : void parseInternal(const QUrl& url, bool noParseIfCached,
70 : const QString& ifNoneMatch = QString(),
71 : const QString& ifModifiedSince = QString()); // used for redirects.
72 :
73 : RawFeed* feed;
74 : ParseResult result;
75 : QNetworkReply::NetworkError networkError;
76 :
77 : FangNetworkAccessManager manager;
78 : QNetworkAccessManager* activeManager;
79 : QNetworkReply *currentReply;
80 : QUrl finalFeedURL;
81 : QNetworkReply *redirectReply;
82 :
83 : bool fromCache;
84 : bool noParseIfCached;
85 :
86 : QString condIfNoneMatch;
87 : QString condIfModifiedSince;
88 : QString respEtag;
89 : QString respLastModified;
90 :
91 : int redirectAttempts;
92 : bool permanentRedirect;
93 :
94 : QThread workerThread;
95 : };
96 :
97 : #endif // PARSER_H
|