forked from qbittorrent/qBittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.h
585 lines (511 loc) · 16.2 KB
/
rss.h
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez, Arnaud Demaiziere
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contact : [email protected], [email protected]
*/
#ifndef RSS_H
#define RSS_H
// MAX ITEM A STREAM
#define STREAM_MAX_ITEM 18
// FIXME: not used yet
#define GLOBAL_MAX_ITEM 150
// avoid crash if too many refresh
#define REFRESH_FREQ_MAX 5000
// type of refresh
#define ICON 0
#define NEWS 1
#define LATENCY 2
#include <QFile>
#include <QImage>
#include <QList>
#include <QTemporaryFile>
#include <QSettings>
#include <QDomDocument>
#include <QTime>
#include <QUrl>
#include "misc.h"
#include "downloadThread.h"
class RssManager;
class RssStream;
class RssItem;
// Item of a rss stream, single information
class RssItem : public QObject {
Q_OBJECT
private:
QString title;
QString link;
QString description;
QString image;
bool read;
QString downloadLink;
public:
// public constructor
RssItem(const QDomElement& properties) {
read = false;
downloadLink = "none";
QDomElement property = properties.firstChild().toElement();
while(!property.isNull()) {
if (property.tagName() == "title")
title = property.text();
else if (property.tagName() == "link")
link = property.text();
else if (property.tagName() == "description")
description = property.text();
else if (property.tagName() == "image")
image = property.text();
property = property.nextSibling().toElement();
}
}
~RssItem(){
}
QString getTitle() const{
return title;
}
QString getLink() const{
return link;
}
QString getDescription() const{
if(description.isEmpty())
return tr("No description available");
return description;
}
QString getImage() const{
return image;
}
QString getDownloadLink() const{
return downloadLink;
}
bool isRead() const{
return read;
}
void setRead(){
read = true;
}
};
// Rss stream, loaded form an xml file
class RssStream : public QObject{
Q_OBJECT
private:
QString title;
QString alias;
QString link;
QString description;
QString image;
QString url;
QString filePath;
QString iconPath;
QList<RssItem*> listItem;
downloadThread* downloaderRss;
downloadThread* downloaderIcon;
QTime lastRefresh;
bool read;
bool downloadFailure;
signals:
void refreshFinished(QString msg, const unsigned short& type);
public slots :
// read and store the downloaded rss' informations
void processDownloadedFile(QString, QString file_path) {
// delete the former file
if(QFile::exists(filePath)) {
QFile::remove(filePath);
}
filePath = file_path;
downloadFailure = false;
openRss();
emit refreshFinished(url, NEWS);
}
// display the icon in the rss window
void displayIcon(QString, QString file_path) {
iconPath = file_path;
qDebug("Icon tmp path is %s", (const char*) file_path.toUtf8());
openIcon();
emit refreshFinished(url, ICON);
}
public:
RssStream(QString _url) {
qDebug("New rss stream created");
url = _url;
alias = url;
read = true;
downloadFailure = false;
qDebug("Creating rss & ico downloadThreads");
downloaderRss = new downloadThread(this);
downloaderIcon = new downloadThread(this);
qDebug("RSS & ico downloaders created");
connect(downloaderRss, SIGNAL(downloadFinished(QString, QString)), this, SLOT(processDownloadedFile(QString, QString)));
connect(downloaderRss, SIGNAL(downloadFailure(QString, QString)), this, SLOT(handleDownloadFailure(QString, QString)));
qDebug("RSS is calling downloadUrl");
downloaderRss->downloadUrl(url);
qDebug("downloadUrl called, ok");
// XXX: remove it when gif can be displayed
iconPath = ":/Icons/rss.png";
qDebug("Calling getIcon");
getIcon();
qDebug("getIcon called");
lastRefresh.start();
qDebug("RSSStream constructed");
}
~RssStream(){
removeAllItem();
delete downloaderRss;
delete downloaderIcon;
if(QFile::exists(filePath))
QFile::remove(filePath);
if(QFile::exists(iconPath) && !iconPath.startsWith(":/"))
QFile::remove(iconPath);
}
// delete all the items saved
void removeAllItem() {
unsigned int listSize = listItem.size();
for(unsigned int i=0; i<listSize; ++i){
delete getItem(i);
}
}
void refresh() {
downloaderRss->downloadUrl(url);
lastRefresh.start();
}
QString getTitle() const{
return title;
}
QString getAlias() const{
return alias;
}
//prefer the RssManager::setAlias, do not save the changed ones
void setAlias(QString _alias){
alias = _alias;
}
QString getLink() const{
return link;
}
QString getUrl() const{
return url;
}
QString getDescription() const{
return description;
}
QString getImage() const{
return image;
}
QString getFilePath() const{
return filePath;
}
QString getIconPath() const{
return iconPath;
}
RssItem* getItem(unsigned int index) const{
return listItem.at(index);
}
unsigned short getListSize() const{
return listItem.size();
}
unsigned short getNbNonRead() const{
int i=0, nbnonread=0;
for(i=0; i<listItem.size(); ++i) {
if(!listItem.at(i)->isRead())
nbnonread++;
}
return nbnonread;
}
QList<RssItem*> getListItem() const{
return listItem;
}
QString getLastRefreshElapsedString() const{
return tr("%1 ago", "10min ago").arg(misc::userFriendlyDuration((long)(lastRefresh.elapsed()/1000.)).replace("<", "<"));
}
unsigned int getLastRefreshElapsed() const{
return lastRefresh.elapsed();
}
QString getLastRefresh() const{
return QString::number(lastRefresh.hour())+"h"+QString::number(lastRefresh.minute())+"m";
}
bool isRead() const {
return read;
}
void setRead() {
read = true;
}
// download the icon from the adress
void getIcon() {
QUrl siteUrl(url);
QString iconUrl = "http:https://"+siteUrl.host()+"/favicon.ico";
connect(downloaderIcon, SIGNAL(downloadFinished(QString, QString)), this, SLOT(displayIcon(QString, QString)));
downloaderIcon->downloadUrl(iconUrl);
}
private:
// read and create items from a rss document
short readDoc(const QDomDocument& doc) {
// is it a rss file ?
QDomElement root = doc.documentElement();
if(root.tagName() == "html"){
qDebug("the file is empty, maybe the url is invalid or the server is too busy");
return -1;
}
else if(root.tagName() != "rss"){
qDebug("the file is not a rss stream, <rss> omitted: %s", (const char*)root.tagName().toUtf8());
return -1;
}
QDomNode rss = root.firstChild();
QDomElement channel = root.firstChild().toElement();
unsigned short listsize = getListSize();
for(unsigned short i=0; i<listsize; ++i) {
listItem.removeLast();
}
while(!channel.isNull()) {
// we are reading the rss'main info
if (channel.tagName() == "channel") {
QDomElement property = channel.firstChild().toElement();
while(!property.isNull()) {
if (property.tagName() == "title") {
title = property.text();
if(alias==getUrl())
setAlias(title);
}
else if (property.tagName() == "link")
link = property.text();
else if (property.tagName() == "description")
description = property.text();
else if (property.tagName() == "image")
image = property.text();
else if(property.tagName() == "item") {
if(getListSize() < STREAM_MAX_ITEM) {
listItem.append(new RssItem(property));
}
}
property = property.nextSibling().toElement();
}
read = false;
}
channel = channel.nextSibling().toElement();
}
return 0;
}
// not actually used, it is used to resize the list of item AFTER the update, instead of delete it BEFORE, some troubles
void resizeList() {
unsigned short lastindex = 0;
QString firstTitle = getItem(0)->getTitle();
unsigned short listsize = getListSize();
for(unsigned short i=0; i<listsize; ++i) {
if(getItem(i)->getTitle() == firstTitle)
lastindex = i;
}
for(unsigned short i=0; i<lastindex; ++i) {
listItem.removeFirst();
}
while(getListSize()>STREAM_MAX_ITEM) {
listItem.removeAt(STREAM_MAX_ITEM);
}
}
// existing and opening test after download
short openRss(){
QDomDocument doc("Rss Seed");
QFile fileRss(filePath);
if(!fileRss.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("error : open failed, no file or locked, "+filePath.toUtf8());
if(QFile::exists(filePath)) {
fileRss.remove();
}
return -1;
}
if(!doc.setContent(&fileRss)) {
qDebug("can't read temp file, might be empty");
fileRss.close();
if(QFile::exists(filePath)) {
fileRss.remove();
}
return -1;
}
// start reading the xml
short return_lecture = readDoc(doc);
fileRss.close();
if(QFile::exists(filePath)) {
fileRss.remove();
}
return return_lecture;
}
void openIcon() {
QImage fileIcon(iconPath,0);
if(!fileIcon.load(iconPath, 0)) {
qDebug("error: icon open failed, no file or empty file at "+iconPath.toUtf8());
if(QFile::exists(iconPath)) {
QFile::remove(iconPath);
if(downloadFailure){
iconPath = ":/Icons/unavailable.png";
qDebug("Could not open favicon nor rss stream, setting icon to unavailable (%s)",(const char*)getAlias().toUtf8());
} else {
iconPath = ":/Icons/rss.png";
qDebug("Could not open favicon, setting default icon (%s)",(const char*)getAlias().toUtf8());
}
}
}
}
protected slots:
void handleDownloadFailure(QString, QString){
// Change the stream icon to a red cross
iconPath = ":/Icons/unavailable.png";
downloadFailure = true;
emit refreshFinished(url, ICON);
}
};
// global class, manage the whole rss stream
class RssManager : public QObject{
Q_OBJECT
private :
QList<RssStream*> streamList;
QStringList streamListUrl;
signals:
void streamNeedRefresh(const unsigned short&, const unsigned short&);
public slots :
void streamNeedRefresh(QString _url, const unsigned short& type) {
emit(streamNeedRefresh(hasStream(_url), type));
}
public :
RssManager(){
loadStreamList();
}
~RssManager(){
saveStreamList();
unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){
delete getStream(i);
}
}
// load the list of the rss stream
void loadStreamList(){
QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss");
streamListUrl = settings.value("streamList").toStringList();
QStringList streamListAlias = settings.value("streamAlias").toStringList();
//check that both list have same size
while(streamListUrl.size()>streamListAlias.size())
streamListUrl.removeLast();
while(streamListAlias.size()>streamListUrl.size())
streamListAlias.removeLast();
qDebug("NB RSS streams loaded: %d", streamListUrl.size());
settings.endGroup();
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
RssStream *stream = new RssStream(streamListUrl.at(i));
qDebug("Setting rss alias");
stream->setAlias(streamListAlias.at(i));
qDebug("RSS Alias set");
streamList.append(stream);
connect(stream, SIGNAL(refreshFinished(QString, const unsigned short&)), this, SLOT(streamNeedRefresh(QString, const unsigned short&)));
}
qDebug("Streams loaded");
}
// save the list of the rss stream for the next session
void saveStreamList(){
streamListUrl.clear();
QStringList streamListAlias;
unsigned int nbStreams = getNbStreams();
for(unsigned int i=0; i<nbStreams; ++i) {
streamListUrl.append(getStream(i)->getUrl());
streamListAlias.append(getStream(i)->getAlias());
}
QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss");
settings.setValue("streamList", streamListUrl);
settings.setValue("streamAlias", streamListAlias);
settings.endGroup();
}
// add a stream to the manager
void addStream(RssStream* stream){
if(hasStream(stream) < 0){
streamList.append(stream);
streamListUrl.append(stream->getUrl());
connect(stream, SIGNAL(refreshFinished(QString, const unsigned short&)), this, SLOT(streamNeedRefresh(QString, const unsigned short&)));
}else{
qDebug("Not adding the Rss stream because it is already in the list");
}
}
// add a stream to the manager
void addStream(QString url){
if(hasStream(url) < 0) {
RssStream* stream = new RssStream(url);
streamList.append(stream);
streamListUrl.append(url);
connect(stream, SIGNAL(refreshFinished(QString, const unsigned short&)), this, SLOT(streamNeedRefresh(QString, const unsigned short&)));
}else {
qDebug("Not adding the Rss stream because it is already in the list");
}
}
// remove a stream from the manager
void removeStream(RssStream* stream){
short index = hasStream(stream);
if(index != -1){
delete streamList.takeAt(index);
streamListUrl.removeAt(index);
}
}
// remove all the streams in the manager
void removeAll(){
QList<RssStream*> newStreamList;
QStringList newUrlList;
unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){
delete getStream(i);
}
streamList = newStreamList;
streamListUrl = newUrlList;
}
// reload all the xml files from the web
void refreshAll(){
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
getStream(i)->refresh();
connect(getStream(i), SIGNAL(refreshFinished(QString, const unsigned short&)), this, SLOT(streamNeedRefresh(QString, const unsigned short&)));
}
}
void refresh(unsigned int index) {
if(index<getNbStreams()) {
if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) {
getStream(index)->refresh();
connect(getStream(index), SIGNAL(refreshFinished(QString, const unsigned short&)), this, SLOT(streamNeedRefresh(QString, const unsigned short&)));
}
}
}
// return the position index of a stream, if the manager owns it
short hasStream(RssStream* stream) const{
return hasStream(stream->getUrl());
}
short hasStream(QString url) const{
return streamListUrl.indexOf(url);
}
RssStream* getStream(const unsigned short& index) const{
return streamList.at(index);
}
unsigned int getNbStreams() {
return streamList.size();
}
//set an alias to an stream and save it for later
void setAlias(unsigned short index, QString newAlias) {
if(newAlias.length()>=2 && !getListAlias().contains(newAlias, Qt::CaseInsensitive)) {
getStream(index)->setAlias(newAlias);
}
}
QStringList getListAlias() {
QStringList listAlias;
unsigned int nbStreams = getNbStreams();
for(unsigned short i=0; i<nbStreams; ++i) {
listAlias.append(getStream(i)->getAlias());
}
return listAlias;
}
};
#endif