2007-03-29 19:27:40 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez, Arnaud Demaiziere
|
2007-03-29 19:27:40 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* 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.
|
2007-03-29 19:27:40 +04:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org, arnaud@qbittorrent.org
|
2007-03-29 19:27:40 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RSS_H
|
|
|
|
#define RSS_H
|
|
|
|
|
2007-04-12 00:24:41 +04:00
|
|
|
// MAX ITEM A STREAM
|
2007-08-03 19:16:42 +04:00
|
|
|
#define STREAM_MAX_ITEM 50
|
|
|
|
// 10min
|
|
|
|
#define STREAM_REFRESH_INTERVAL 600000
|
2007-04-05 23:42:39 +04:00
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
#include <QFile>
|
|
|
|
#include <QList>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QDomDocument>
|
2007-04-12 00:24:41 +04:00
|
|
|
#include <QTime>
|
2007-04-14 22:04:47 +04:00
|
|
|
#include <QUrl>
|
2007-08-03 19:16:42 +04:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QImage>
|
2007-08-03 18:37:56 +04:00
|
|
|
#include <QHash>
|
2007-03-29 19:27:40 +04:00
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "downloadThread.h"
|
|
|
|
|
2007-08-29 14:20:39 +04:00
|
|
|
#ifdef HAVE_MAGICK
|
2007-08-29 13:22:21 +04:00
|
|
|
#include <Magick++.h>
|
|
|
|
using namespace Magick;
|
|
|
|
#endif
|
|
|
|
|
2007-04-12 00:24:41 +04:00
|
|
|
class RssManager;
|
2007-03-29 19:27:40 +04:00
|
|
|
class RssStream;
|
2007-04-12 00:24:41 +04:00
|
|
|
class RssItem;
|
2007-03-29 19:27:40 +04:00
|
|
|
|
|
|
|
// Item of a rss stream, single information
|
2007-07-22 15:53:43 +04:00
|
|
|
class RssItem : public QObject {
|
|
|
|
Q_OBJECT
|
2007-03-29 19:27:40 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
QString title;
|
|
|
|
QString link;
|
|
|
|
QString description;
|
|
|
|
QString image;
|
|
|
|
bool read;
|
|
|
|
QString downloadLink;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// public constructor
|
2007-08-28 20:17:32 +04:00
|
|
|
RssItem(const QDomElement& properties) : read(false), downloadLink("none") {
|
2007-04-05 23:42:39 +04:00
|
|
|
QDomElement property = properties.firstChild().toElement();
|
|
|
|
while(!property.isNull()) {
|
2007-03-29 19:27:40 +04:00
|
|
|
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{
|
2007-07-22 15:53:43 +04:00
|
|
|
if(description.isEmpty())
|
|
|
|
return tr("No description available");
|
2007-03-29 19:27:40 +04:00
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getImage() const{
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getDownloadLink() const{
|
|
|
|
return downloadLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isRead() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return read;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRead(){
|
2007-04-06 00:29:30 +04:00
|
|
|
read = true;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Rss stream, loaded form an xml file
|
|
|
|
class RssStream : public QObject{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2007-04-06 00:29:30 +04:00
|
|
|
private:
|
2007-03-29 19:27:40 +04:00
|
|
|
QString title;
|
|
|
|
QString link;
|
|
|
|
QString description;
|
|
|
|
QString image;
|
|
|
|
QString url;
|
2007-08-28 20:17:32 +04:00
|
|
|
QString alias;
|
2007-03-29 19:27:40 +04:00
|
|
|
QString filePath;
|
2007-04-14 22:04:47 +04:00
|
|
|
QString iconPath;
|
2007-03-29 19:27:40 +04:00
|
|
|
QList<RssItem*> listItem;
|
2007-04-12 00:24:41 +04:00
|
|
|
QTime lastRefresh;
|
2007-04-13 16:07:14 +04:00
|
|
|
bool read;
|
2007-08-03 18:37:56 +04:00
|
|
|
bool refreshed;
|
2007-08-28 20:17:32 +04:00
|
|
|
bool downloadFailure;
|
2007-08-03 18:37:56 +04:00
|
|
|
bool currently_loading;
|
2007-04-14 22:04:47 +04:00
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
public slots :
|
|
|
|
// read and store the downloaded rss' informations
|
2007-08-03 18:37:56 +04:00
|
|
|
void processDownloadedFile(QString file_path) {
|
2007-08-03 19:16:42 +04:00
|
|
|
// delete the old file
|
2007-04-05 16:39:42 +04:00
|
|
|
if(QFile::exists(filePath)) {
|
|
|
|
QFile::remove(filePath);
|
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
filePath = file_path;
|
2007-07-22 13:12:50 +04:00
|
|
|
downloadFailure = false;
|
2007-04-06 00:29:30 +04:00
|
|
|
openRss();
|
2007-08-03 18:37:56 +04:00
|
|
|
lastRefresh.start();
|
|
|
|
refreshed = true;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
void setDownloadFailed(){
|
|
|
|
downloadFailure = true;
|
|
|
|
lastRefresh.start();
|
2007-04-15 18:53:38 +04:00
|
|
|
}
|
2007-04-14 22:04:47 +04:00
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
public:
|
2007-08-28 20:17:32 +04:00
|
|
|
RssStream(QString _url): url(_url), alias(""), iconPath(":/Icons/rss.png"), refreshed(false), downloadFailure(false), currently_loading(false) {
|
2007-07-22 19:02:19 +04:00
|
|
|
qDebug("RSSStream constructed");
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~RssStream(){
|
2007-08-03 18:37:56 +04:00
|
|
|
removeAllItems();
|
2007-03-29 19:27:40 +04:00
|
|
|
if(QFile::exists(filePath))
|
|
|
|
QFile::remove(filePath);
|
2007-07-22 08:14:44 +04:00
|
|
|
if(QFile::exists(iconPath) && !iconPath.startsWith(":/"))
|
|
|
|
QFile::remove(iconPath);
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-04-05 16:39:42 +04:00
|
|
|
// delete all the items saved
|
2007-08-03 18:37:56 +04:00
|
|
|
void removeAllItems() {
|
2007-08-19 17:00:51 +04:00
|
|
|
qDeleteAll(listItem);
|
|
|
|
listItem.clear();
|
2007-04-05 16:39:42 +04:00
|
|
|
}
|
2007-04-05 23:42:39 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
void setLoading(bool val) {
|
|
|
|
currently_loading = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLoading() {
|
|
|
|
return currently_loading;
|
2007-04-12 00:24:41 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
QString getTitle() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return title;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getAlias() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return alias;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-07-22 13:47:27 +04:00
|
|
|
void setAlias(QString _alias){
|
2007-04-06 00:29:30 +04:00
|
|
|
alias = _alias;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
// Return the alias if the stream has one, the url if it has no alias
|
|
|
|
QString getAliasOrUrl() const{
|
|
|
|
if(!alias.isEmpty())
|
|
|
|
return alias;
|
|
|
|
if(!title.isEmpty())
|
|
|
|
return title;
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
QString getLink() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return link;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getUrl() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return url;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getDescription() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return description;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getImage() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return image;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getFilePath() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return filePath;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-04-14 22:04:47 +04:00
|
|
|
QString getIconPath() const{
|
2007-08-03 18:37:56 +04:00
|
|
|
if(downloadFailure)
|
|
|
|
return ":/Icons/unavailable.png";
|
2007-04-15 16:05:14 +04:00
|
|
|
return iconPath;
|
2007-04-15 18:53:38 +04:00
|
|
|
}
|
2007-04-14 22:04:47 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
bool hasCustomIcon() const{
|
|
|
|
return !iconPath.startsWith(":/");
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIconPath(QString path) {
|
|
|
|
iconPath = path;
|
|
|
|
}
|
|
|
|
|
2007-04-06 02:10:55 +04:00
|
|
|
RssItem* getItem(unsigned int index) const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return listItem.at(index);
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
unsigned int getNbNews() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return listItem.size();
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
unsigned int getNbUnRead() const{
|
|
|
|
unsigned int nbUnread=0;
|
|
|
|
RssItem *item;
|
|
|
|
foreach(item, listItem){
|
|
|
|
if(!item->isRead())
|
|
|
|
++nbUnread;
|
2007-07-21 23:37:10 +04:00
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
return nbUnread;
|
2007-07-22 07:07:59 +04:00
|
|
|
}
|
2007-07-21 23:37:10 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
QList<RssItem*> getNewsList() const{
|
2007-04-06 00:29:30 +04:00
|
|
|
return listItem;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-07-22 15:53:43 +04:00
|
|
|
QString getLastRefreshElapsedString() const{
|
2007-08-03 18:37:56 +04:00
|
|
|
if(!refreshed)
|
|
|
|
return tr("Never");
|
2007-07-22 15:53:43 +04:00
|
|
|
return tr("%1 ago", "10min ago").arg(misc::userFriendlyDuration((long)(lastRefresh.elapsed()/1000.)).replace("<", "<"));
|
|
|
|
}
|
|
|
|
|
2007-04-13 16:07:14 +04:00
|
|
|
unsigned int getLastRefreshElapsed() const{
|
2007-08-03 19:16:42 +04:00
|
|
|
if(!refreshed)
|
|
|
|
return STREAM_REFRESH_INTERVAL+1;
|
2007-04-12 00:24:41 +04:00
|
|
|
return lastRefresh.elapsed();
|
2007-04-13 16:07:14 +04:00
|
|
|
}
|
|
|
|
|
2007-07-21 23:37:10 +04:00
|
|
|
// download the icon from the adress
|
2007-08-03 18:37:56 +04:00
|
|
|
QString getIconUrl() {
|
2007-04-14 22:04:47 +04:00
|
|
|
QUrl siteUrl(url);
|
2007-08-20 11:03:41 +04:00
|
|
|
return QString::fromUtf8("http://")+siteUrl.host()+QString::fromUtf8("/favicon.ico");
|
2007-04-14 22:04:47 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
private:
|
2007-04-12 23:24:05 +04:00
|
|
|
// read and create items from a rss document
|
2007-04-13 16:07:14 +04:00
|
|
|
short readDoc(const QDomDocument& doc) {
|
2007-03-29 19:27:40 +04:00
|
|
|
// is it a rss file ?
|
|
|
|
QDomElement root = doc.documentElement();
|
2007-08-20 11:03:41 +04:00
|
|
|
if(root.tagName() == QString::fromUtf8("html")){
|
2007-07-21 01:06:50 +04:00
|
|
|
qDebug("the file is empty, maybe the url is invalid or the server is too busy");
|
2007-03-29 19:27:40 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-20 11:03:41 +04:00
|
|
|
else if(root.tagName() != QString::fromUtf8("rss")){
|
|
|
|
qDebug("the file is not a rss stream, <rss> omitted: %s", root.tagName().toUtf8().data());
|
2007-03-29 19:27:40 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
QDomNode rss = root.firstChild();
|
|
|
|
QDomElement channel = root.firstChild().toElement();
|
2007-08-03 18:37:56 +04:00
|
|
|
unsigned short listsize = getNbNews();
|
2007-07-22 15:53:43 +04:00
|
|
|
for(unsigned short i=0; i<listsize; ++i) {
|
2007-04-12 23:24:05 +04:00
|
|
|
listItem.removeLast();
|
|
|
|
}
|
2007-04-15 18:53:38 +04:00
|
|
|
|
2007-04-06 00:29:30 +04:00
|
|
|
while(!channel.isNull()) {
|
2007-08-03 19:16:42 +04:00
|
|
|
// we are reading the rss'main info
|
2007-04-06 00:29:30 +04:00
|
|
|
if (channel.tagName() == "channel") {
|
|
|
|
QDomElement property = channel.firstChild().toElement();
|
|
|
|
while(!property.isNull()) {
|
2007-04-06 16:57:41 +04:00
|
|
|
if (property.tagName() == "title") {
|
2007-04-06 00:29:30 +04:00
|
|
|
title = property.text();
|
2007-04-06 16:57:41 +04:00
|
|
|
if(alias==getUrl())
|
|
|
|
setAlias(title);
|
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
else if (property.tagName() == "link")
|
2007-04-06 00:29:30 +04:00
|
|
|
link = property.text();
|
2007-03-29 19:27:40 +04:00
|
|
|
else if (property.tagName() == "description")
|
2007-04-06 00:29:30 +04:00
|
|
|
description = property.text();
|
2007-03-29 19:27:40 +04:00
|
|
|
else if (property.tagName() == "image")
|
2007-04-06 00:29:30 +04:00
|
|
|
image = property.text();
|
|
|
|
else if(property.tagName() == "item") {
|
2007-08-03 18:37:56 +04:00
|
|
|
if(getNbNews() < STREAM_MAX_ITEM) {
|
2007-04-12 00:24:41 +04:00
|
|
|
listItem.append(new RssItem(property));
|
2007-04-05 16:39:42 +04:00
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
property = property.nextSibling().toElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
channel = channel.nextSibling().toElement();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 23:24:05 +04:00
|
|
|
// not actually used, it is used to resize the list of item AFTER the update, instead of delete it BEFORE, some troubles
|
2007-04-12 00:24:41 +04:00
|
|
|
void resizeList() {
|
|
|
|
unsigned short lastindex = 0;
|
|
|
|
QString firstTitle = getItem(0)->getTitle();
|
2007-08-03 18:37:56 +04:00
|
|
|
unsigned short listsize = getNbNews();
|
2007-07-22 15:53:43 +04:00
|
|
|
for(unsigned short i=0; i<listsize; ++i) {
|
2007-04-12 00:24:41 +04:00
|
|
|
if(getItem(i)->getTitle() == firstTitle)
|
|
|
|
lastindex = i;
|
|
|
|
}
|
2007-07-22 15:53:43 +04:00
|
|
|
for(unsigned short i=0; i<lastindex; ++i) {
|
2007-04-12 00:24:41 +04:00
|
|
|
listItem.removeFirst();
|
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
while(getNbNews()>STREAM_MAX_ITEM) {
|
2007-04-12 23:24:05 +04:00
|
|
|
listItem.removeAt(STREAM_MAX_ITEM);
|
2007-04-12 00:24:41 +04:00
|
|
|
}
|
2007-04-15 18:53:38 +04:00
|
|
|
|
2007-04-12 00:24:41 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
// existing and opening test after download
|
|
|
|
short openRss(){
|
|
|
|
QDomDocument doc("Rss Seed");
|
|
|
|
QFile fileRss(filePath);
|
2007-04-06 00:29:30 +04:00
|
|
|
if(!fileRss.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2007-04-05 16:39:42 +04:00
|
|
|
qDebug("error : open failed, no file or locked, "+filePath.toUtf8());
|
|
|
|
if(QFile::exists(filePath)) {
|
|
|
|
fileRss.remove();
|
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2007-04-06 00:29:30 +04:00
|
|
|
if(!doc.setContent(&fileRss)) {
|
2007-03-29 19:27:40 +04:00
|
|
|
qDebug("can't read temp file, might be empty");
|
|
|
|
fileRss.close();
|
2007-04-05 16:39:42 +04:00
|
|
|
if(QFile::exists(filePath)) {
|
|
|
|
fileRss.remove();
|
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// start reading the xml
|
2007-04-13 16:07:14 +04:00
|
|
|
short return_lecture = readDoc(doc);
|
2007-03-29 19:27:40 +04:00
|
|
|
fileRss.close();
|
2007-04-05 16:39:42 +04:00
|
|
|
if(QFile::exists(filePath)) {
|
|
|
|
fileRss.remove();
|
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
return return_lecture;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// global class, manage the whole rss stream
|
2007-04-12 00:24:41 +04:00
|
|
|
class RssManager : public QObject{
|
|
|
|
Q_OBJECT
|
2007-04-15 18:53:38 +04:00
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
private :
|
2007-08-03 18:37:56 +04:00
|
|
|
QHash<QString, RssStream*> streams;
|
|
|
|
downloadThread *downloader;
|
2007-08-03 19:16:42 +04:00
|
|
|
QTimer newsRefresher;
|
2007-03-29 19:27:40 +04:00
|
|
|
|
2007-04-12 00:24:41 +04:00
|
|
|
signals:
|
2007-08-03 18:37:56 +04:00
|
|
|
void feedInfosChanged(QString url, QString aliasOrUrl, unsigned int nbUnread);
|
|
|
|
void feedIconChanged(QString url, QString icon_path);
|
2007-04-12 00:24:41 +04:00
|
|
|
|
|
|
|
public slots :
|
2007-08-03 18:37:56 +04:00
|
|
|
|
|
|
|
void processFinishedDownload(QString url, QString path) {
|
|
|
|
if(url.endsWith("favicon.ico")){
|
|
|
|
// Icon downloaded
|
|
|
|
QImage fileIcon;
|
2007-08-29 14:20:39 +04:00
|
|
|
#ifdef HAVE_MAGICK
|
2007-08-29 13:22:21 +04:00
|
|
|
try{
|
|
|
|
QFile::copy(path, path+".ico");
|
|
|
|
Image image(QDir::cleanPath(path+".ico").toUtf8().data());
|
|
|
|
// Convert to PNG since we can't read ICO format
|
|
|
|
image.magick("PNG");
|
2007-08-29 17:45:51 +04:00
|
|
|
// Resize to 16x16px
|
|
|
|
image.sample(Geometry(16, 16));
|
2007-08-29 13:22:21 +04:00
|
|
|
image.write(path.toUtf8().data());
|
|
|
|
QFile::remove(path+".ico");
|
|
|
|
}catch(Magick::Exception &error_){
|
|
|
|
qDebug("favicon conversion to PNG failure: %s", error_.what());
|
|
|
|
}
|
|
|
|
#endif
|
2007-08-03 18:37:56 +04:00
|
|
|
if(fileIcon.load(path)) {
|
|
|
|
QList<RssStream*> res = findFeedsWithIcon(url);
|
|
|
|
RssStream* stream;
|
|
|
|
foreach(stream, res){
|
|
|
|
stream->setIconPath(path);
|
|
|
|
if(!stream->isLoading())
|
|
|
|
emit feedIconChanged(stream->getUrl(), stream->getIconPath());
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
qDebug("Unsupported icon format at %s", (const char*)url.toUtf8());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RssStream *stream = streams.value(url, 0);
|
|
|
|
if(!stream){
|
|
|
|
qDebug("This rss stream was deleted in the meantime, nothing to update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stream->processDownloadedFile(path);
|
|
|
|
stream->setLoading(false);
|
|
|
|
// If the feed has no alias, then we use the title as Alias
|
|
|
|
// this is more user friendly
|
|
|
|
if(stream->getAlias().isEmpty()){
|
|
|
|
if(!stream->getTitle().isEmpty())
|
|
|
|
stream->setAlias(stream->getTitle());
|
|
|
|
}
|
|
|
|
emit feedInfosChanged(url, stream->getAliasOrUrl(), stream->getNbUnRead());
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDownloadFailure(QString url, QString reason) {
|
|
|
|
if(url.endsWith("favicon.ico")){
|
|
|
|
// Icon download failure
|
|
|
|
qDebug("Could not download icon at %s, reason: %s", (const char*)url.toUtf8(), (const char*)reason.toUtf8());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RssStream *stream = streams.value(url, 0);
|
|
|
|
if(!stream){
|
|
|
|
qDebug("This rss stream was deleted in the meantime, nothing to update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stream->setLoading(false);
|
|
|
|
qDebug("Could not download Rss at %s, reason: %s", (const char*)url.toUtf8(), (const char*)reason.toUtf8());
|
|
|
|
stream->setDownloadFailed();
|
2007-08-03 18:51:32 +04:00
|
|
|
emit feedInfosChanged(url, stream->getAliasOrUrl(), stream->getNbUnRead());
|
2007-04-12 00:24:41 +04:00
|
|
|
}
|
2007-04-15 18:53:38 +04:00
|
|
|
|
2007-08-03 19:16:42 +04:00
|
|
|
void refreshOldFeeds(){
|
|
|
|
RssStream *stream;
|
|
|
|
foreach(stream, streams){
|
|
|
|
QString url = stream->getUrl();
|
|
|
|
if(stream->isLoading()) return;
|
|
|
|
if(stream->getLastRefreshElapsed() < STREAM_REFRESH_INTERVAL) return;
|
2007-08-03 19:17:34 +04:00
|
|
|
qDebug("Refreshing old feed: %s...", (const char*)url.toUtf8());
|
2007-08-03 19:16:42 +04:00
|
|
|
stream->setLoading(true);
|
|
|
|
downloader->downloadUrl(url);
|
|
|
|
if(!stream->hasCustomIcon()){
|
|
|
|
downloader->downloadUrl(stream->getIconUrl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
public :
|
|
|
|
RssManager(){
|
2007-08-03 18:37:56 +04:00
|
|
|
downloader = new downloadThread(this);
|
|
|
|
connect(downloader, SIGNAL(downloadFinished(QString, QString)), this, SLOT(processFinishedDownload(QString, QString)));
|
|
|
|
connect(downloader, SIGNAL(downloadFailure(QString, QString)), this, SLOT(handleDownloadFailure(QString, QString)));
|
2007-03-29 19:27:40 +04:00
|
|
|
loadStreamList();
|
2007-08-03 19:16:42 +04:00
|
|
|
connect(&newsRefresher, SIGNAL(timeout()), this, SLOT(refreshOldFeeds()));
|
2007-09-01 14:23:39 +04:00
|
|
|
newsRefresher.start(60000); // 1min
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~RssManager(){
|
2007-08-19 17:00:51 +04:00
|
|
|
qDeleteAll(streams);
|
2007-08-03 18:37:56 +04:00
|
|
|
delete downloader;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// load the list of the rss stream
|
|
|
|
void loadStreamList(){
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
2007-08-03 18:37:56 +04:00
|
|
|
QStringList streamsUrl = settings.value("Rss/streamList").toStringList();
|
|
|
|
QStringList aliases = settings.value("Rss/streamAlias").toStringList();
|
|
|
|
if(streamsUrl.size() != aliases.size()){
|
|
|
|
std::cerr << "Corrupted Rss list, not loading it\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString url;
|
|
|
|
unsigned int i = 0;
|
|
|
|
foreach(url, streamsUrl){
|
|
|
|
RssStream *stream = new RssStream(url);
|
|
|
|
QString alias = aliases.at(i);
|
|
|
|
if(!alias.isEmpty()) {
|
|
|
|
stream->setAlias(alias);
|
|
|
|
}
|
|
|
|
streams[url] = stream;
|
|
|
|
++i;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
qDebug("NB RSS streams loaded: %d", streamsUrl.size());
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the list of the rss stream for the next session
|
|
|
|
void saveStreamList(){
|
2007-08-03 18:37:56 +04:00
|
|
|
QList<QPair<QString, QString> > streamsList;
|
|
|
|
QStringList streamsUrl;
|
|
|
|
QStringList aliases;
|
|
|
|
RssStream *stream;
|
|
|
|
foreach(stream, streams){
|
|
|
|
streamsUrl << stream->getUrl();
|
|
|
|
aliases << stream->getAlias();
|
2007-04-13 16:07:14 +04:00
|
|
|
}
|
2007-03-29 19:27:40 +04:00
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
settings.beginGroup("Rss");
|
2007-08-03 18:37:56 +04:00
|
|
|
settings.setValue("streamList", streamsUrl);
|
|
|
|
settings.setValue("streamAlias", aliases);
|
2007-03-29 19:27:40 +04:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a stream to the manager
|
|
|
|
void addStream(RssStream* stream){
|
2007-08-03 18:37:56 +04:00
|
|
|
QString url = stream->getUrl();
|
|
|
|
if(streams.contains(url)){
|
2007-04-06 02:38:46 +04:00
|
|
|
qDebug("Not adding the Rss stream because it is already in the list");
|
2007-08-03 18:37:56 +04:00
|
|
|
return;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
streams[url] = stream;
|
|
|
|
emit feedIconChanged(url, stream->getIconPath());
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a stream to the manager
|
2007-08-03 18:37:56 +04:00
|
|
|
RssStream* addStream(QString url){
|
|
|
|
if(streams.contains(url)){
|
2007-04-06 02:38:46 +04:00
|
|
|
qDebug("Not adding the Rss stream because it is already in the list");
|
2007-08-03 18:37:56 +04:00
|
|
|
return 0;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
RssStream* stream = new RssStream(url);
|
|
|
|
streams[url] = stream;
|
|
|
|
refresh(url);
|
|
|
|
return stream;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove a stream from the manager
|
|
|
|
void removeStream(RssStream* stream){
|
2007-08-03 18:37:56 +04:00
|
|
|
QString url = stream->getUrl();
|
|
|
|
Q_ASSERT(streams.contains(url));
|
|
|
|
delete streams.take(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<RssStream*> findFeedsWithIcon(QString icon_url){
|
|
|
|
QList<RssStream*> res;
|
|
|
|
RssStream* stream;
|
|
|
|
foreach(stream, streams){
|
|
|
|
if(stream->getIconUrl() == icon_url)
|
|
|
|
res << stream;
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
2007-08-03 18:37:56 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeStream(QString url){
|
|
|
|
Q_ASSERT(streams.contains(url));
|
|
|
|
delete streams.take(url);
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove all the streams in the manager
|
|
|
|
void removeAll(){
|
2007-08-19 17:00:51 +04:00
|
|
|
qDeleteAll(streams);
|
2007-08-03 18:37:56 +04:00
|
|
|
streams.clear();
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-04-05 16:39:42 +04:00
|
|
|
// reload all the xml files from the web
|
|
|
|
void refreshAll(){
|
2007-08-03 18:37:56 +04:00
|
|
|
qDebug("Refreshing all rss feeds");
|
|
|
|
RssStream *stream;
|
|
|
|
foreach(stream, streams){
|
|
|
|
QString url = stream->getUrl();
|
|
|
|
if(stream->isLoading()) return;
|
|
|
|
qDebug("Refreshing feed: %s...", (const char*)url.toUtf8());
|
|
|
|
stream->setLoading(true);
|
|
|
|
downloader->downloadUrl(url);
|
|
|
|
if(!stream->hasCustomIcon()){
|
|
|
|
downloader->downloadUrl(stream->getIconUrl());
|
|
|
|
}
|
2007-04-05 16:39:42 +04:00
|
|
|
}
|
|
|
|
}
|
2007-04-05 22:30:20 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
void refresh(QString url) {
|
|
|
|
Q_ASSERT(streams.contains(url));
|
|
|
|
RssStream *stream = streams[url];
|
|
|
|
if(stream->isLoading()) return;
|
|
|
|
stream->setLoading(true);
|
|
|
|
downloader->downloadUrl(url);
|
|
|
|
if(!stream->hasCustomIcon()){
|
|
|
|
downloader->downloadUrl(stream->getIconUrl());
|
|
|
|
}else{
|
|
|
|
qDebug("No need to download this feed's icon, it was already downloaded");
|
2007-04-05 22:30:20 +04:00
|
|
|
}
|
|
|
|
}
|
2007-04-05 23:42:39 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
// XXX: Used?
|
|
|
|
unsigned int getNbFeeds() {
|
|
|
|
return streams.size();
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
RssStream* getFeed(QString url){
|
|
|
|
Q_ASSERT(streams.contains(url));
|
|
|
|
return streams[url];
|
2007-03-29 19:27:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
// Set an alias for a stream and save it for later
|
|
|
|
void setAlias(QString url, QString newAlias) {
|
|
|
|
Q_ASSERT(!newAlias.isEmpty());
|
|
|
|
RssStream * stream = streams.value(url, 0);
|
|
|
|
Q_ASSERT(stream != 0);
|
|
|
|
stream->setAlias(newAlias);
|
|
|
|
emit feedInfosChanged(url, stream->getAliasOrUrl(), stream->getNbUnRead());
|
2007-04-05 16:39:42 +04:00
|
|
|
}
|
2007-04-05 22:30:20 +04:00
|
|
|
|
2007-08-03 18:37:56 +04:00
|
|
|
// Return all the rss feeds we have
|
|
|
|
QList<RssStream*> getRssFeeds() const {
|
|
|
|
return streams.values();
|
2007-04-13 16:07:14 +04:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:27:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|