qBittorrent/src/rss.h

452 lines
11 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
*
* 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 : chris@qbittorrent.org, arnaud@qbittorrent.org
*/
#ifndef RSS_H
#define RSS_H
#define STREAM_MAX_ITEM 15
// FIXME: not used yet
#define GLOBAL_MAX_ITEM 150
2007-03-29 19:27:40 +04:00
#include <QFile>
#include <QList>
#include <curl/curl.h>
#include <QTemporaryFile>
#include <QSettings>
#include <QDomDocument>
#include "misc.h"
#include "downloadThread.h"
class RssStream;
// Item of a rss stream, single information
class RssItem{
private:
QString title;
QString link;
QString description;
QString image;
bool read;
RssStream* parent;
QString downloadLink;
public:
// public constructor
RssItem(const QDomElement& properties, RssStream* _parent){
read = false;
parent = _parent;
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();
}
//displayItem();
}
~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
}
RssStream* getParent() const{
return parent;
}
void displayItem(){
2007-04-06 00:29:30 +04:00
qDebug(" - "+title.toUtf8()+" - "+link.toUtf8());
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;
QList<RssItem*> listItem;
downloadThread* downloader;
public slots :
// read and store the downloaded rss' informations
2007-04-06 00:29:30 +04:00
void processDownloadedFile(const QString&, const QString& file_path, int return_code, const QString&) {
// delete the former file
if(QFile::exists(filePath)) {
QFile::remove(filePath);
}
2007-03-29 19:27:40 +04:00
filePath = file_path;
if(return_code){
// Download failed
qDebug("(download failure) "+file_path.toUtf8());
if(QFile::exists(filePath)) {
QFile::remove(file_path);
}
2007-03-29 19:27:40 +04:00
return;
}
2007-04-06 00:29:30 +04:00
openRss();
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;
downloader = new downloadThread(this);
connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&, int, const QString&)));
downloader->downloadUrl(url);
}
~RssStream(){
removeAllItem();
2007-03-29 19:27:40 +04:00
delete downloader;
if(QFile::exists(filePath))
QFile::remove(filePath);
}
// 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);
}
}
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
}
RssItem* getItem(unsigned short 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
}
QList<RssItem*> getListItem() const{
2007-04-06 00:29:30 +04:00
return listItem;
2007-03-29 19:27:40 +04:00
}
void displayStream(){
qDebug(" # "+getTitle().toUtf8()+" - "+getUrl().toUtf8()+" - "+getAlias().toUtf8());
for(int i=0; i<listItem.size(); i++){
getItem(i)->displayItem();
}
}
private:
short read(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 wrong or the server is too busy");
return -1;
}
else if(root.tagName() != "rss"){
qDebug("the file is not a rss stream, <rss> omitted"+root.tagName().toUtf8());
return -1;
}
QDomNode rss = root.firstChild();
QDomElement channel = root.firstChild().toElement();
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()) {
2007-03-29 19:27:40 +04:00
if (property.tagName() == "title")
2007-04-06 00:29:30 +04:00
title = property.text();
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") {
if(getListSize() < STREAM_MAX_ITEM) {
2007-04-06 02:02:04 +04:00
//TODO: find a way to break here
2007-04-06 00:29:30 +04:00
//add it to a list
listItem.append(new RssItem(property, this));
}
2007-03-29 19:27:40 +04:00
}
property = property.nextSibling().toElement();
}
}
channel = channel.nextSibling().toElement();
}
return 0;
}
// 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 = read(doc);
fileRss.close();
if(QFile::exists(filePath)) {
fileRss.remove();
}
2007-03-29 19:27:40 +04:00
return return_lecture;
}
};
// global class, manage the whole rss stream
class RssManager{
private :
QList<RssStream*> streamList;
QStringList streamListUrl;
QStringList streamListAlias;
public :
RssManager(){
loadStreamList();
}
~RssManager(){
saveStreamList();
2007-04-06 00:29:30 +04:00
for(unsigned short i=0; i<streamList.size(); ++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();
streamListAlias = settings.value("streamAlias").toStringList();
2007-03-29 19:27:40 +04:00
settings.endGroup();
2007-04-06 00:29:30 +04:00
for(unsigned short i=0; i<streamListUrl.size(); ++i){
RssStream *stream = new RssStream(streamListUrl.at(i));
stream->setAlias(streamListAlias.at(i));
streamList.append(stream);
2007-03-29 19:27:40 +04:00
}
}
// save the list of the rss stream for the next session
void saveStreamList(){
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());
streamListAlias.append(stream->getUrl());
2007-03-29 19:27:40 +04:00
}
}
// add a stream to the manager
void addStream(QString url){
// completion of the address
if(!url.endsWith(".xml")){
2007-04-06 00:29:30 +04:00
if(url.endsWith("/")) {
2007-03-29 19:27:40 +04:00
url.append("rss.xml");
2007-04-06 00:29:30 +04:00
} else {
url.append("/rss.xml");
2007-03-29 19:27:40 +04:00
}
}
2007-04-06 00:29:30 +04:00
if(hasStream(url) < 0) {
streamList.append(new RssStream(url));
streamListUrl.append(url);
streamListAlias.append(url);
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){
unsigned int streamListSize = streamList.size();
for(unsigned short i=0; i<streamListSize; ++i){
if(getStream(i)->getUrl() == stream->getUrl()){
delete streamList.takeAt(i);
2007-03-29 19:27:40 +04:00
}
}
2007-04-06 00:29:30 +04:00
streamListUrl.removeAt(index);
streamListAlias.removeAt(index);
2007-03-29 19:27:40 +04:00
}
}
// remove all the streams in the manager
void removeAll(){
QList<RssStream*> newStreamList;
2007-03-29 19:27:40 +04:00
QStringList newUrlList, newAliasList;
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;
streamListAlias = newAliasList;
2007-03-29 19:27:40 +04:00
}
// reload all the xml files from the web
void refreshAll(){
QList<RssStream*> newStreamList;
2007-04-06 00:29:30 +04:00
unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){
delete getStream(i);
}
2007-04-06 00:29:30 +04:00
streamList = newStreamList;
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
RssStream *stream = new RssStream(streamListUrl.at(i));
stream->setAlias(streamListAlias.at(i));
streamList.append(stream);
}
}
void refresh(int index) {
if(index>=0 && index<getNbStream()) {
delete getStream(index);
2007-04-06 00:29:30 +04:00
RssStream *stream = new RssStream(streamListUrl.at(index));
stream->setAlias(streamListAlias.at(index));
streamList.replace(index, stream);
}
}
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
}
2007-04-06 00:29:30 +04:00
bool hasStream(const QString& url) const{
2007-03-29 19:27:40 +04:00
return streamListUrl.indexOf(url);
}
RssStream* getStream(const int& index) const{
return streamList.at(index);
}
void displayManager(){
for(unsigned short i=0; i<streamList.size(); i++){
getStream(i)->displayStream();
}
}
int getNbStream() {
return streamList.size();
}
//set an alias to an stream and save it for later
void setAlias(int index, QString newAlias) {
if(newAlias.length()>=2 && !streamListAlias.contains(newAlias, Qt::CaseInsensitive)) {
getStream(index)->setAlias(newAlias);
streamListAlias.replace(index, newAlias);
}
}
2007-03-29 19:27:40 +04:00
};
#endif