1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
//===========================================
// Lumina-DE source code
// Copyright (c) 2016, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "RSSObjects.h"
#include <QNetworkRequest>
#include <QXmlStreamReader>
#include "LSession.h"
//============
// PUBLIC
//============
RSSReader::RSSReader(QObject *parent, QString settingsPrefix) : QObject(parent){
NMAN = new QNetworkAccessManager(this);
connect(NMAN, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)) );
connect(NMAN, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), this, SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&)) );
setprefix = settingsPrefix;
syncTimer = new QTimer(this);
syncTimer->setInterval(300000); //5 minutes
connect(syncTimer, SIGNAL(timeout()), this, SLOT(checkTimes()));
syncTimer->start();
}
RSSReader::~RSSReader(){
}
//Information retrieval
QStringList RSSReader::channels(){
QStringList urls = hash.keys();
QStringList ids;
//sort all the channels by title before output
for(int i=0; i<urls.length(); i++){
QString title = hash[urls[i]].title;
if(title.isEmpty()){ title = "ZZZ"; } //put currently-invalid ones at the end of the list
ids << title+" : "+urls[i];
}
ids.sort();
//Now strip off all the titles again to just get the IDs
for(int i=0; i<ids.length(); i++){
ids[i] = ids[i].section(" : ",-1);
}
return ids;
}
RSSchannel RSSReader::dataForID(QString ID){
if(hash.contains(ID)){ return hash[ID]; }
else{ return RSSchannel(); }
}
//Initial setup
void RSSReader::addUrls(QStringList urls){
//qDebug() << "Add URLS:" << urls;
QStringList known = hash.keys();
int orig = known.length();
for(int i=0; i<orig; i++){ known << hash[known[i]].originalURL; }
for(int i=0; i<urls.length(); i++){
//Note: Make sure we get the complete URL form for accurate comparison later
QString url = QUrl(urls[i]).toString();
if(known.contains(url)){ continue; } //already handled
RSSchannel blank;
blank.originalURL = url;
hash.insert(url, blank); //put the empty struct into the hash for now
requestRSS(url); //startup the initial request for this url
}
emit newChannelsAvailable();
}
void RSSReader::removeUrl(QString ID){
if(hash.contains(ID)){ hash.remove(ID); }
emit newChannelsAvailable();
}
//=================
// PUBLIC SLOTS
//=================
void RSSReader::syncNow(){
QStringList urls = hash.keys();
for(int i=0; i<urls.length(); i++){
requestRSS(urls[i]);
}
}
//=================
// PRIVATE
//=================
void RSSReader::requestRSS(QString url){
if(!outstandingURLS.contains(url)){
//qDebug() << "Request URL:" << url;
NMAN->get( QNetworkRequest( QUrl(url) ) );
outstandingURLS << url;
}
}
//RSS parsing functions
RSSchannel RSSReader::readRSS(QByteArray bytes){
//Note: We could expand this later to support multiple "channel"s per Feed
// but it seems like there is normally only one channel anyway
//qDebug() << "Read RSS:" << bytes.left(100);
QXmlStreamReader xml(bytes);
RSSchannel rssinfo;
//qDebug() << "Can Read XML Stream:" << !xml.hasError();
if(xml.readNextStartElement()){
//qDebug() << " - RSS Element:" << xml.name();
if(xml.name() == "rss" && (xml.attributes().value("version") =="2.0" || xml.attributes().value("version") =="0.91") ){
while(xml.readNextStartElement()){
//qDebug() << " - RSS Element:" << xml.name();
if(xml.name()=="channel"){ rssinfo = readRSSChannel(&xml); }
else{ xml.skipCurrentElement(); }
}
}
}
if(xml.hasError()){ qDebug() << " - XML Read Error:" << xml.errorString() << "\n" << bytes; }
return rssinfo;
}
RSSchannel RSSReader::readRSSChannel(QXmlStreamReader *rss){
RSSchannel info;
info.timetolive = -1;
while(rss->readNextStartElement()){
//qDebug() << " - RSS Element (channel):" <<rss->name();
if(rss->name()=="item"){ info.items << readRSSItem(rss); }
else if(rss->name()=="title"){ info.title = rss->readElementText(); }
else if(rss->name()=="link"){
QString txt = rss->readElementText();
if(!txt.isEmpty()){ info.link = txt; }
}
else if(rss->name()=="description"){ info.description = rss->readElementText(); }
else if(rss->name()=="lastBuildDate"){ info.lastBuildDate = RSSDateTime(rss->readElementText()); }
else if(rss->name()=="pubDate"){ info.lastPubDate = RSSDateTime(rss->readElementText()); }
else if(rss->name()=="image"){ readRSSImage(&info, rss); }
//else if(rss->name()=="skipHours"){ info.link = rss->readElementText(); }
//else if(rss->name()=="skipDays"){ info.link = rss->readElementText(); }
else if(rss->name()=="ttl"){ info.timetolive = rss->readElementText().toInt(); }
else{ rss->skipCurrentElement(); }
}
return info;
}
RSSitem RSSReader::readRSSItem(QXmlStreamReader *rss){
RSSitem item;
while(rss->readNextStartElement()){
//qDebug() << " - RSS Element (Item):" << rss->name();
if(rss->name()=="title"){ item.title = rss->readElementText(); }
else if(rss->name()=="link"){ item.link = rss->readElementText(); }
else if(rss->name()=="description"){ item.description = rss->readElementText(); }
else if(rss->name()=="comments"){ item.comments_url = rss->readElementText(); }
else if(rss->name()=="author"){
//Special handling - this field can contain both email and name
QString raw = rss->readElementText();
if(raw.contains("@")){
item.author_email = raw.split(" ").filter("@").first();
item.author = raw.remove(item.author_email).remove("(").remove(")").simplified(); //the name is often put within parentheses after the email
}else{ item.author = raw; }
}
else if(rss->name()=="guid"){ item.guid = rss->readElementText(); }
else if(rss->name()=="pubDate"){ item.pubdate = RSSDateTime(rss->readElementText()); }
else{ rss->skipCurrentElement(); }
}
return item;
}
void RSSReader::readRSSImage(RSSchannel *item, QXmlStreamReader *rss){
while(rss->readNextStartElement()){
//qDebug() << " - RSS Element (Image):" << rss->name();
if(rss->name()=="url"){ item->icon_url = rss->readElementText(); }
else if(rss->name()=="title"){ item->icon_title = rss->readElementText(); }
else if(rss->name()=="link"){ item->icon_link = rss->readElementText(); }
else if(rss->name()=="width"){ item->icon_size.setWidth(rss->readElementText().toInt()); }
else if(rss->name()=="height"){ item->icon_size.setHeight(rss->readElementText().toInt()); }
else if(rss->name()=="description"){ item->icon_description = rss->readElementText(); }
}
//Go ahead and kick off the request for the icon
if(!item->icon_url.isEmpty()){ requestRSS(item->icon_url); }
}
QDateTime RSSReader::RSSDateTime(QString datetime){
return QDateTime::fromString(datetime, Qt::RFC2822Date);
}
//=================
// PRIVATE SLOTS
//=================
void RSSReader::replyFinished(QNetworkReply *reply){
QString url = reply->request().url().toString();
//qDebug() << "Got Reply:" << url;
QByteArray data = reply->readAll();
outstandingURLS.removeAll(url);
if(data.isEmpty()){
//qDebug() << "No data returned:" << url;
//see if the URL can be adjusted for known issues
bool handled = false;
QUrl redirecturl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if(redirecturl.isValid() && (redirecturl.toString() != url )){
//New URL redirect - make the change and send a new request
QString newurl = redirecturl.toString();
//qDebug() << " - Redirect to:" << newurl;
if(hash.contains(url) && !hash.contains(newurl)){
hash.insert(newurl, hash.take(url) ); //just move the data over to the new url
requestRSS(newurl);
emit newChannelsAvailable();
handled = true;
}
}
if(!handled && hash.contains(url) ){
emit rssChanged(url);
}
return;
}
if(!hash.contains(url)){
//qDebug() << " - hash does not contain URL:" << url;
//URL removed from list while a request was outstanding?
//Could also be an icon fetch response
QStringList keys = hash.keys();
for(int i=0; i<keys.length(); i++){
//qDebug() << " - Check for icon URL:" << hash[keys[i]].icon_url;
if(hash[keys[i]].icon_url.toLower() == url.toLower()){ //needs to be case-insensitive
//Icon fetch response
RSSchannel info = hash[keys[i]];
QImage img = QImage::fromData(data);
info.icon = QIcon( QPixmap::fromImage(img) );
//qDebug() << "Got Icon response:" << url << info.icon;
hash.insert(keys[i], info); //insert back into the hash
emit rssChanged(keys[i]);
break;
}
}
reply->deleteLater();
}else{
//RSS reply
RSSchannel info = readRSS(data); //QNetworkReply can be used as QIODevice
reply->deleteLater(); //clean up
//Validate the info and announce any changes
if(info.title.isEmpty() || info.link.isEmpty() || info.description.isEmpty()){
qDebug() << "Missing XML Information:" << url << info.title << info.link << info.description;
return;
} //bad info/read
//Update the bookkeeping elements of the info
if(info.timetolive<=0){ info.timetolive = LSession::handle()->DesktopPluginSettings()->value(setprefix+"default_interval_minutes", 60).toInt(); }
if(info.timetolive <=0){ info.timetolive = 60; } //error in integer conversion from settings?
info.lastsync = QDateTime::currentDateTime(); info.nextsync = info.lastsync.addSecs(info.timetolive * 60);
//Now see if anything changed and save the info into the hash
bool changed = (hash[url].lastBuildDate.isNull() || (hash[url].lastBuildDate < info.lastBuildDate) );
bool newinfo = false;
if(changed){ newinfo = hash[url].title.isEmpty(); } //no previous info from this URL
info.originalURL = hash[url].originalURL; //make sure this info gets preserved across updates
hash.insert(url, info);
if(newinfo){ emit newChannelsAvailable(); } //new channel
else if(changed){ emit rssChanged(url); } //update to existing channel
}
}
void RSSReader::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors){
int ok = 0;
qDebug() << "SSL Errors Detected (RSS Reader):" << reply->url();
for(int i=0; i<errors.length(); i++){
if(errors[i].error()==QSslError::SelfSignedCertificate || errors[i].error()==QSslError::SelfSignedCertificateInChain){ ok++; }
else{ qDebug() << "Unhandled SSL Error:" << errors[i].errorString(); }
}
if(ok==errors.length()){ qDebug() << " - Permitted:" << reply->url(); reply->ignoreSslErrors(); }
else{ qDebug() << " - Denied:" << reply->url(); }
}
void RSSReader::checkTimes(){
if(LSession::handle()->DesktopPluginSettings()->value(setprefix+"manual_sync_only", false).toBool()){ return; }
QStringList urls = hash.keys();
QDateTime cdt = QDateTime::currentDateTime();
for(int i=0; i<urls.length(); i++){
if(hash[urls[i]].nextsync < cdt){ requestRSS(urls[i]); }
}
}
|