1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #ifndef PARSEOBJECT_H
19 #define PARSEOBJECT_H
20 
21 #include <QtSql>
22 #include <QDateTime>
23 #include <QDomDocument>
24 #include <QQueue>
25 #include <QObject>
26 #include <QUrl>
27 #include <QMutex>
28 
29 struct FeedItemStruct {
30   QString title;
31   QString updated;
32   QString link;
33   QString linkBase;
34   QString language;
35   QString author;
36   QString authorUri;
37   QString authorEmail;
38   QString description;
39 };
40 
41 struct NewsItemStruct {
42   QString id;
43   QString title;
44   QString updated;
45   QString link;
46   QString linkAlternate;
47   QString language;
48   QString author;
49   QString authorUri;
50   QString authorEmail;
51   QString description;
52   QString content;
53   QString category;
54   QString eUrl;
55   QString eType;
56   QString eLength;
57   QString comments;
58 };
59 
60 struct FeedCountStruct{
61   int feedId;
62   int unreadCount;
63   int newCount;
64   int undeleteCount;
65   QString updated;
66   QString lastBuildDate;
67   QString htmlUrl;
68   QString xmlUrl;
69   QString title;
70 };
71 
Q_DECLARE_METATYPE(FeedCountStruct)72 Q_DECLARE_METATYPE(FeedCountStruct)
73 
74 class ParseObject : public QObject
75 {
76   Q_OBJECT
77 public:
78   explicit ParseObject(QObject *parent = 0);
79   ~ParseObject();
80 
81   void disconnectObjects();
82 
83 public slots:
84   void parseXml(QByteArray data, int feedId,
85                 QDateTime dtReply, QString codecName);
86   void runUserFilter(int feedId, int filterId = -1);
87 
88 signals:
89   void signalReadyParse(const QByteArray &xml, const int &feedId,
90                         const QDateTime &dtReply, const QString &codecName);
91   void signalFinishUpdate(int feedId, bool changed, int newCount, QString status);
92   void feedCountsUpdate(FeedCountStruct counts);
93   void signalPlaySound(const QString &soundPath);
94   void signalAddColorList(int id, const QString &color);
95 
96 private slots:
97   void getQueuedXml();
98   void slotParse(const QByteArray &xmlData, const int &feedId,
99                  const QDateTime &dtReply, const QString &codecName);
100   void addAtomNewsIntoBase(NewsItemStruct *newsItem);
101   void addRssNewsIntoBase(NewsItemStruct *newsItem);
102 
103 private:
104   void parseAtom(const QString &feedUrl, const QDomDocument &doc);
105   void parseRss(const QString &feedUrl, const QDomDocument &doc);
106   QString toPlainText(const QString &text);
107   QString fromPlainText(QString text);
108   QString getCommunity(const QDomNode &nodeContent);
109   QString parseDate(const QString &dateString, const QString &urlString);
110   int recountFeedCounts(int feedId, const QString &feedUrl,
111                         const QString &updated, const QString &lastBuildDate);
112 
113   QSqlDatabase db_;
114   QTimer *parseTimer_;
115   QMutex mutex_;
116   QQueue<int> idsQueue_;
117   QQueue<QByteArray> xmlsQueue_;
118   QQueue<QDateTime> dtReadyQueue_;
119   QQueue<QString> codecNameQueue_;
120 
121   int parseFeedId_;
122   bool duplicateNewsMode_;
123   bool feedChanged_;
124   bool addSingleNewsAnyDate_;
125   bool avoidedOldSingleNews_;
126   QDate avoidedOldSingleNewsDate_;
127 
128   QStringList guidList_;
129   QStringList linkList_;
130   QStringList titleList_;
131   QStringList publishedList_;
132 
133   QDateTime lastBuildDate_;
134 
135 };
136 
137 #endif // PARSEOBJECT_H
138