qBittorrent/src/rss.h

564 lines
15 KiB
C
Raw Normal View History

2007-03-29 19:27:40 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez, Arnaud Demaiziere
2007-03-29 19:27:40 +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
* 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
// 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
2007-04-14 22:04:47 +04:00
#define ICON 0
#define NEWS 1
#define LATENCY 2
2007-04-14 22:04:47 +04:00
2007-03-29 19:27:40 +04:00
#include <QFile>
#include <QImage>
2007-03-29 19:27:40 +04:00
#include <QList>
#include <QTemporaryFile>
#include <QSettings>
#include <QDomDocument>
#include <QTime>
2007-04-14 22:04:47 +04:00
#include <QUrl>
2007-03-29 19:27:40 +04:00
#include "misc.h"
#include "downloadThread.h"
class RssManager;
2007-03-29 19:27:40 +04:00
class RssStream;
class RssItem;
2007-03-29 19:27:40 +04:00
// Item of a rss stream, single information
class RssItem{
private:
QString title;
QString link;
QString description;
QString image;
bool read;
QString downloadLink;
public:
// public constructor
RssItem(const QDomElement& properties){
2007-03-29 19:27:40 +04:00
read = false;
downloadLink = "none";
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{
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 alias;
QString link;
QString description;
QString image;
QString url;
QString filePath;
2007-04-14 22:04:47 +04:00
QString iconPath;
2007-03-29 19:27:40 +04:00
QList<RssItem*> listItem;
downloadThread* downloaderRss;
downloadThread* downloaderIcon;
QTime lastRefresh;
bool read;
2007-03-29 19:27:40 +04:00
signals:
2007-04-14 22:04:47 +04:00
void refreshFinished(const QString& msg, const unsigned short& type);
2007-03-29 19:27:40 +04:00
public slots :
// read and store the downloaded rss' informations
void processDownloadedFile(const QString&, const QString& file_path) {
// delete the former file
if(QFile::exists(filePath)) {
QFile::remove(filePath);
}
2007-03-29 19:27:40 +04:00
filePath = file_path;
2007-04-06 00:29:30 +04:00
openRss();
2007-04-14 22:04:47 +04:00
emit refreshFinished(url, NEWS);
2007-03-29 19:27:40 +04:00
}
// display the icon in the rss window
2007-07-22 07:07:59 +04:00
void displayIcon(const QString&, const QString& file_path) {
2007-04-14 22:04:47 +04:00
iconPath = file_path;
openIcon();
emit refreshFinished(url, ICON);
}
2007-04-14 22:04:47 +04:00
2007-03-29 19:27:40 +04:00
public:
2007-04-06 00:29:30 +04:00
RssStream(const QString& _url) {
2007-03-29 19:27:40 +04:00
url = _url;
alias = url;
read = true;
downloaderRss = new downloadThread(this);
downloaderIcon = new downloadThread(this);
connect(downloaderRss, SIGNAL(downloadFinished(const QString&, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&)));
connect(downloaderRss, SIGNAL(downloadFailure(const QString&, const QString&)), this, SLOT(handleDownloadFailure(const QString&, const QString&)));
downloaderRss->downloadUrl(url);
// XXX: remove it when gif can be displayed
iconPath = ":/Icons/rss.png";
getIcon();
lastRefresh.start();
2007-03-29 19:27:40 +04:00
}
~RssStream(){
removeAllItem();
delete downloaderRss;
delete downloaderIcon;
2007-03-29 19:27:40 +04:00
if(QFile::exists(filePath))
QFile::remove(filePath);
if(QFile::exists(iconPath) && iconPath!=":/Icons/rss.png")
QFile::remove(iconPath);
2007-03-29 19:27:40 +04:00
}
// delete all the items saved
void removeAllItem() {
2007-04-06 00:29:30 +04:00
unsigned int listSize = listItem.size();
for(unsigned int i=0; i<listSize; ++i){
delete getItem(i);
}
}
void refresh() {
downloaderRss->downloadUrl(url);
lastRefresh.start();
}
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
}
//prefer the RssManager::setAlias, do not save the changed ones
2007-03-29 19:27:40 +04:00
void setAlias(const QString& _alias){
2007-04-06 00:29:30 +04:00
alias = _alias;
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{
return iconPath;
}
2007-04-14 22:04:47 +04:00
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
}
unsigned short getListSize() const{
2007-04-06 00:29:30 +04:00
return listItem.size();
2007-03-29 19:27:40 +04:00
}
unsigned short getNbNonRead() const{
int i=0, nbnonread=0;
for(i=0; i<listItem.size(); i++) {
if(!listItem.at(i)->isRead())
nbnonread++;
}
return nbnonread;
2007-07-22 07:07:59 +04:00
}
2007-03-29 19:27:40 +04:00
QList<RssItem*> getListItem() const{
2007-04-06 00:29:30 +04:00
return listItem;
2007-03-29 19:27:40 +04:00
}
unsigned int getLastRefreshElapsed() const{
return lastRefresh.elapsed();
}
2007-04-16 01:31:32 +04:00
QString getLastRefresh() const{
return QString::number(lastRefresh.hour())+"h"+QString::number(lastRefresh.minute())+"m";
}
2007-04-16 01:31:32 +04:00
bool isRead() const {
return read;
}
void setRead() {
read = true;
}
2007-03-29 19:27:40 +04:00
// download the icon from the adress
2007-04-14 22:04:47 +04:00
void getIcon() {
QUrl siteUrl(url);
QString iconUrl = "http://"+siteUrl.host()+"/favicon.ico";
2007-07-22 07:07:59 +04:00
connect(downloaderIcon, SIGNAL(downloadFinished(const QString&, const QString&), this, SLOT(displayIcon(const QString&, const QString&)));
downloaderIcon->downloadUrl(iconUrl);
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
short readDoc(const QDomDocument& doc) {
2007-03-29 19:27:40 +04:00
// 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");
2007-03-29 19:27:40 +04:00
return -1;
}
else if(root.tagName() != "rss"){
qDebug("the file is not a rss stream, <rss> omitted: %s", (const char*)root.tagName().toUtf8());
2007-03-29 19:27:40 +04:00
return -1;
}
QDomNode rss = root.firstChild();
QDomElement channel = root.firstChild().toElement();
2007-04-12 23:24:05 +04:00
unsigned short listsize = getListSize();
for(unsigned short i=0; i<listsize; i++) {
listItem.removeLast();
}
2007-04-06 00:29:30 +04:00
while(!channel.isNull()) {
2007-03-29 19:27:40 +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()) {
if (property.tagName() == "title") {
2007-04-06 00:29:30 +04:00
title = property.text();
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-04-12 23:24:05 +04:00
if(getListSize() < STREAM_MAX_ITEM) {
listItem.append(new RssItem(property));
}
2007-03-29 19:27:40 +04:00
}
property = property.nextSibling().toElement();
}
read = false;
2007-03-29 19:27:40 +04:00
}
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
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) {
2007-04-12 23:24:05 +04:00
listItem.removeAt(STREAM_MAX_ITEM);
}
}
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)) {
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();
if(QFile::exists(filePath)) {
fileRss.remove();
}
2007-03-29 19:27:40 +04:00
return -1;
}
// start reading the xml
short return_lecture = readDoc(doc);
2007-03-29 19:27:40 +04:00
fileRss.close();
if(QFile::exists(filePath)) {
fileRss.remove();
}
2007-03-29 19:27:40 +04:00
return return_lecture;
}
void openIcon() {
QImage fileIcon(iconPath,0);
// if(!fileIcon.open(QIODevice::ReadOnly)) {
// qDebug("error : icon open failed, no file or locked, "+iconPath.toUtf8());
// if(QFile::exists(iconPath)) {
// fileIcon.remove();
// iconPath = ":/Icons/rss.png";
// }
// return;
// }
if(fileIcon.isNull()) {
qDebug("error : icon open failed, file empty, "+iconPath.toUtf8());
if(QFile::exists(iconPath)) {
//QFile::remove(iconPath);
//iconPath = ":/Icons/rss.png";
}
return;
}
}
protected slots:
void handleDownloadFailure(const QString&, const QString&){
// Change the stream icon to a red cross
iconPath = ":/Icons/unavailable.png";
emit refreshFinished(url, ICON);
}
2007-03-29 19:27:40 +04:00
};
// global class, manage the whole rss stream
class RssManager : public QObject{
Q_OBJECT
2007-03-29 19:27:40 +04:00
private :
QList<RssStream*> streamList;
QStringList streamListUrl;
signals:
2007-04-14 22:04:47 +04:00
void streamNeedRefresh(const unsigned short&, const unsigned short&);
public slots :
2007-04-14 22:04:47 +04:00
void streamNeedRefresh(const QString& _url, const unsigned short& type) {
emit(streamNeedRefresh(hasStream(_url), type));
}
2007-03-29 19:27:40 +04:00
public :
RssManager(){
loadStreamList();
}
~RssManager(){
saveStreamList();
2007-04-06 02:10:55 +04:00
unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){
2007-03-29 19:27:40 +04:00
delete getStream(i);
}
}
// load the list of the rss stream
void loadStreamList(){
QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss");
2007-04-06 00:29:30 +04:00
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();
2007-04-06 02:38:46 +04:00
qDebug("NB RSS streams loaded: %d", streamListUrl.size());
2007-03-29 19:27:40 +04:00
settings.endGroup();
2007-04-06 02:10:55 +04:00
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
2007-04-06 00:29:30 +04:00
RssStream *stream = new RssStream(streamListUrl.at(i));
stream->setAlias(streamListAlias.at(i));
streamList.append(stream);
2007-04-14 22:04:47 +04:00
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
2007-03-29 19:27:40 +04:00
}
}
// save the list of the rss stream for the next session
void saveStreamList(){
streamListUrl.clear();
QStringList streamListAlias;
for(unsigned short i=0; i<getNbStream(); i++) {
streamListUrl.append(getStream(i)->getUrl());
streamListAlias.append(getStream(i)->getAlias());
}
2007-03-29 19:27:40 +04:00
QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss");
2007-04-06 00:29:30 +04:00
settings.setValue("streamList", streamListUrl);
settings.setValue("streamAlias", streamListAlias);
2007-03-29 19:27:40 +04:00
settings.endGroup();
}
// add a stream to the manager
void addStream(RssStream* stream){
2007-04-06 00:29:30 +04:00
if(hasStream(stream) < 0){
streamList.append(stream);
streamListUrl.append(stream->getUrl());
2007-04-14 22:04:47 +04:00
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
2007-04-06 02:38:46 +04:00
}else{
qDebug("Not adding the Rss stream because it is already in the list");
2007-03-29 19:27:40 +04:00
}
}
// add a stream to the manager
void addStream(QString url){
2007-04-06 00:29:30 +04:00
if(hasStream(url) < 0) {
2007-04-12 23:24:05 +04:00
RssStream* stream = new RssStream(url);
streamList.append(stream);
2007-04-06 00:29:30 +04:00
streamListUrl.append(url);
2007-04-14 22:04:47 +04:00
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
2007-04-06 02:38:46 +04:00
}else {
qDebug("Not adding the Rss stream because it is already in the list");
2007-03-29 19:27:40 +04:00
}
}
// remove a stream from the manager
void removeStream(RssStream* stream){
short index = hasStream(stream);
2007-04-06 00:29:30 +04:00
if(index != -1){
2007-04-11 13:45:00 +04:00
delete streamList.takeAt(index);
2007-04-06 00:29:30 +04:00
streamListUrl.removeAt(index);
2007-03-29 19:27:40 +04:00
}
}
// remove all the streams in the manager
void removeAll(){
QList<RssStream*> newStreamList;
QStringList newUrlList;
2007-04-06 00:29:30 +04:00
unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){
2007-03-29 19:27:40 +04:00
delete getStream(i);
}
2007-04-06 00:29:30 +04:00
streamList = newStreamList;
streamListUrl = newUrlList;
2007-03-29 19:27:40 +04:00
}
// reload all the xml files from the web
void refreshAll(){
2007-04-06 00:29:30 +04:00
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
2007-04-12 23:24:05 +04:00
getStream(i)->refresh();
2007-04-14 22:04:47 +04:00
connect(getStream(i), SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}
}
void refresh(int index) {
if(index>=0 && index<getNbStream()) {
if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) {
getStream(index)->refresh();
2007-04-14 22:04:47 +04:00
connect(getStream(index), SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}
}
}
2007-03-29 19:27:40 +04:00
// return the position index of a stream, if the manager owns it
short hasStream(RssStream* stream) const{
2007-04-06 00:29:30 +04:00
return hasStream(stream->getUrl());
2007-03-29 19:27:40 +04:00
}
short hasStream(const QString& url) const{
2007-03-29 19:27:40 +04:00
return streamListUrl.indexOf(url);
}
RssStream* getStream(const unsigned short& index) const{
2007-03-29 19:27:40 +04:00
return streamList.at(index);
}
unsigned short getNbStream() {
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;
for(unsigned short i=0; i<getNbStream(); i++) {
listAlias.append(getStream(i)->getAlias());
}
return listAlias;
}
2007-03-29 19:27:40 +04:00
};
#endif