qBittorrent/src/rss_imp.cpp

300 lines
12 KiB
C++
Raw Normal View History

2007-04-05 23:26:30 +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
*/
#include "rss_imp.h"
#include <QDesktopServices>
#include <QInputDialog>
#include <QMenu>
#include <QStandardItemModel>
// display a right-click menu
void RSSImp::displayRSSListMenu(const QPoint& pos){
2007-04-15 23:06:19 +04:00
moveCurrentItem();
2007-04-05 23:26:30 +04:00
QMenu myFinishedListMenu(this);
2007-04-15 23:06:19 +04:00
QTreeWidgetItem* item = listStreams->itemAt(pos);
2007-04-05 23:26:30 +04:00
if(item!=NULL) {
myFinishedListMenu.addAction(actionDelete);
myFinishedListMenu.addAction(actionRename);
myFinishedListMenu.addAction(actionRefresh);
}else{
myFinishedListMenu.addAction(actionCreate);
myFinishedListMenu.addAction(actionRefreshAll);
2007-04-05 23:26:30 +04:00
}
myFinishedListMenu.exec(mapToGlobal(pos)+QPoint(10,33));
2007-04-05 23:26:30 +04:00
}
// add a stream by a button
void RSSImp::on_addStream_button_clicked() {
createStream();
}
// delete a stream by a button
void RSSImp::on_delStream_button_clicked() {
2007-04-15 23:06:19 +04:00
if(getNumStreamSelected()<0 || rssmanager.getNbStream()==0) {
2007-04-05 23:26:30 +04:00
qDebug("no stream selected");
return;
}else {
textBrowser->clear();
listNews->clear();
2007-04-15 23:06:19 +04:00
rssmanager.removeStream(rssmanager.getStream(getNumStreamSelected()));
2007-04-05 23:26:30 +04:00
refreshStreamList();
}
}
// refresh all streams by a button
void RSSImp::on_refreshAll_button_clicked() {
refreshAllStreams();
2007-04-05 23:26:30 +04:00
}
// display the news of a stream when click on it
void RSSImp::on_listStreams_clicked() {
2007-04-15 23:06:19 +04:00
if(rssmanager.getNbStream()>0) {
moveCurrentItem();
rssmanager.getStream(getNumStreamSelected())->setRead();
// update the color of the stream, is it old ?
updateStreamName(getNumStreamSelected(), LATENCY);
2007-04-16 01:31:32 +04:00
refreshNewsList();
2007-04-15 23:06:19 +04:00
}
2007-04-05 23:26:30 +04:00
}
// display the content of a new when clicked on it
void RSSImp::on_listNews_clicked() {
2007-04-13 03:53:15 +04:00
listNews->item(listNews->currentRow())->setData(Qt::ForegroundRole, QVariant(QColor("grey")));
2007-04-16 01:31:32 +04:00
listNews->item(listNews->currentRow())->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png")));
2007-04-05 23:26:30 +04:00
refreshTextBrowser();
}
// open the url of the news in a browser
void RSSImp::on_listNews_doubleClicked() {
2007-04-15 23:06:19 +04:00
if(getNumStreamSelected()>=0 && listNews->currentRow()>=0 && rssmanager.getStream(getNumStreamSelected())->getListSize()>0) {
RssItem* currentItem = rssmanager.getStream(getNumStreamSelected())->getItem(listNews->currentRow());
2007-04-05 23:26:30 +04:00
if(currentItem->getLink()!=NULL && currentItem->getLink().length()>5)
QDesktopServices::openUrl(QUrl(currentItem->getLink()));
}
}
2007-04-15 23:06:19 +04:00
// move the current selection if it is not a toplevelitem (id : stream)
void RSSImp::moveCurrentItem() {
if(getNumStreamSelected()<0) {
int index = listStreams->indexOfTopLevelItem(listStreams->currentItem()->parent());
if(index>=0)
listStreams->setCurrentItem(listStreams->topLevelItem(index));
else
listStreams->setCurrentItem(listStreams->topLevelItem(0));
}
}
2007-04-05 23:26:30 +04:00
//right-clik on stream : delete it
void RSSImp::deleteStream() {
if(rssmanager.getNbStream()==0) {
qDebug("no stream selected");
return;
}else {
2007-04-15 23:06:19 +04:00
moveCurrentItem();
2007-04-05 23:26:30 +04:00
textBrowser->clear();
listNews->clear();
2007-04-15 23:06:19 +04:00
rssmanager.removeStream(rssmanager.getStream(getNumStreamSelected()));
2007-04-05 23:26:30 +04:00
refreshStreamList();
}
}
2007-04-06 02:38:46 +04:00
//right-clik on stream : give him an alias
2007-04-05 23:26:30 +04:00
void RSSImp::renameStream() {
if(rssmanager.getNbStream()==0) {
qDebug("no stream selected");
return;
}else {
2007-04-15 23:06:19 +04:00
moveCurrentItem();
2007-04-05 23:26:30 +04:00
bool ok;
2007-04-15 23:06:19 +04:00
short index = getNumStreamSelected();
QString newAlias = QInputDialog::getText(this, tr("Please choose a new name for this stream"), tr("New stream name:"), QLineEdit::Normal, rssmanager.getStream(index)->getAlias(), &ok);
2007-04-05 23:26:30 +04:00
if(ok) {
rssmanager.setAlias(index, newAlias);
2007-04-14 22:04:47 +04:00
updateStreamName(index, NEWS);
2007-04-05 23:26:30 +04:00
}
}
}
//right-clik on stream : refresh it
void RSSImp::refreshStream() {
if(rssmanager.getNbStream()>0) {
2007-04-15 23:06:19 +04:00
moveCurrentItem();
short index = getNumStreamSelected();
2007-04-05 23:26:30 +04:00
textBrowser->clear();
listNews->clear();
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(index)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
2007-04-05 23:26:30 +04:00
rssmanager.refresh(index);
}
}
//right-click somewhere, refresh all the streams
void RSSImp::refreshAllStreams() {
textBrowser->clear();
listNews->clear();
unsigned short nbstream = rssmanager.getNbStream();
for(unsigned short i=0; i<nbstream; i++)
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
rssmanager.refreshAll();
2007-04-05 23:26:30 +04:00
}
//right-click, register a new stream
void RSSImp::createStream() {
bool ok;
QString newUrl = QInputDialog::getText(this, tr("Please type a rss stream url"), tr("Stream URL:"), QLineEdit::Normal, "http://", &ok);
if(ok) {
newUrl = newUrl.trimmed();
if(!newUrl.isEmpty() && newUrl != "http://"){
rssmanager.addStream(newUrl);
refreshStreamList();
}
2007-04-05 23:26:30 +04:00
}
}
2007-04-06 02:38:46 +04:00
2007-04-05 23:26:30 +04:00
// fills the streamList
void RSSImp::refreshStreamList() {
unsigned short nbstream = rssmanager.getNbStream();
2007-04-05 23:26:30 +04:00
listStreams->clear();
2007-04-15 23:06:19 +04:00
QList<QTreeWidgetItem *> streams;
for(unsigned short i=0; i<nbstream; i++) {
2007-04-15 23:06:19 +04:00
QTreeWidgetItem* stream = new QTreeWidgetItem(listStreams);
QTreeWidgetItem* description = new QTreeWidgetItem(stream);
2007-04-16 01:31:32 +04:00
QTreeWidgetItem* url = new QTreeWidgetItem(stream);
QTreeWidgetItem* time = new QTreeWidgetItem(stream);
2007-04-15 23:06:19 +04:00
description->setText(0, tr("no description avalaible"));
2007-04-16 01:31:32 +04:00
url->setText(0, rssmanager.getStream(i)->getUrl());
time->setText(0, tr("no refresh"));
description->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/description.png")));
url->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/url.png")));
time->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/time.png")));
description->setData(0,Qt::ForegroundRole, QVariant(QColor("grey")));
url->setData(0,Qt::ForegroundRole, QVariant(QColor("grey")));
time->setData(0,Qt::ForegroundRole, QVariant(QColor("grey")));
updateStreamName(i, NEWS);
2007-04-05 23:26:30 +04:00
}
}
// fills the newsList
void RSSImp::refreshNewsList() {
if(rssmanager.getNbStream()>0) {
2007-04-15 23:06:19 +04:00
RssStream* currentstream = rssmanager.getStream(getNumStreamSelected());
2007-04-05 23:26:30 +04:00
listNews->clear();
unsigned short currentStreamSize = currentstream->getListSize();
for(unsigned short i=0; i<currentStreamSize; ++i) {
2007-04-05 23:26:30 +04:00
new QListWidgetItem(currentstream->getItem(i)->getTitle(), listNews);
2007-04-16 01:31:32 +04:00
if(currentstream->getItem(i)->isRead()){
2007-04-13 03:53:15 +04:00
listNews->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("grey")));
2007-04-16 01:31:32 +04:00
listNews->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png")));
}
else {
listNews->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere2.png")));
listNews->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("blue")));
}
2007-04-13 16:13:27 +04:00
if(i%2==0)
listNews->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 255, 20)));
2007-04-05 23:26:30 +04:00
}
}
}
// display a news
void RSSImp::refreshTextBrowser() {
2007-04-15 23:06:19 +04:00
if(getNumStreamSelected()>=0 && listNews->currentRow()>=0) {
RssItem* currentitem = rssmanager.getStream(getNumStreamSelected())->getItem(listNews->currentRow());
textBrowser->setHtml(currentitem->getTitle()+" : \n"+currentitem->getDescription());
2007-04-05 23:26:30 +04:00
currentitem->setRead();
}
}
// show the number of news for a stream, his status and an icon
2007-04-14 22:04:47 +04:00
void RSSImp::updateStreamName(const unsigned short& i, const unsigned short& type) {
// icon has just been download
2007-04-14 22:04:47 +04:00
if(type == ICON) {
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath())));
}
// on click, show the age of the stream
if(type == LATENCY) {
unsigned short nbitem = rssmanager.getStream(i)->getListSize();
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setText(0,rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange")));
else
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green")));
listStreams->topLevelItem(getNumStreamSelected())->setData(0,Qt::BackgroundRole, QVariant(QColor("white")));
}
// when news are refreshed, update all informations
if(type == NEWS) {
unsigned short nbitem = rssmanager.getStream(i)->getListSize();
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setText(0,rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange")));
else
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green")));
2007-04-14 22:04:47 +04:00
if(!rssmanager.getStream(i)->isRead())
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
if(getNumStreamSelected()==i) {
2007-04-14 22:04:47 +04:00
listNews->clear();
refreshNewsList();
}
2007-04-15 23:06:19 +04:00
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath())));
2007-04-16 01:31:32 +04:00
// update description and display last refresh
if(rssmanager.getStream(i)->getDescription()!="")
listStreams->topLevelItem(i)->child(DESCRIPTION_CHILD)->setText(0, rssmanager.getStream(i)->getDescription());
listStreams->topLevelItem(i)->child(TIME_CHILD)->setText(0, rssmanager.getStream(i)->getLastRefresh());
}
2007-04-14 22:04:47 +04:00
}
2007-04-06 02:38:46 +04:00
2007-04-05 23:26:30 +04:00
RSSImp::RSSImp() : QWidget(){
setupUi(this);
2007-04-16 01:31:32 +04:00
// icons of bottom buttons
2007-04-05 23:26:30 +04:00
addStream_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png")));
delStream_button->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png")));
refreshAll_button->setIcon(QIcon(QString::fromUtf8(":/Icons/exec.png")));
2007-04-16 01:31:32 +04:00
// icons of right-click menu
actionDelete->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png")));
actionRename->setIcon(QIcon(QString::fromUtf8(":/Icons/log.png")));
actionRefresh->setIcon(QIcon(QString::fromUtf8(":/Icons/refresh.png")));
actionCreate->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png")));
actionRefreshAll->setIcon(QIcon(QString::fromUtf8(":/Icons/refresh.png")));
connect(listStreams, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRSSListMenu(const QPoint&)));
2007-04-05 23:26:30 +04:00
connect(actionDelete, SIGNAL(triggered()), this, SLOT(deleteStream()));
connect(actionRename, SIGNAL(triggered()), this, SLOT(renameStream()));
connect(actionRefresh, SIGNAL(triggered()), this, SLOT(refreshStream()));
connect(actionCreate, SIGNAL(triggered()), this, SLOT(createStream()));
connect(actionRefreshAll, SIGNAL(triggered()), this, SLOT(refreshAllStreams()));
2007-04-14 22:04:47 +04:00
connect(&rssmanager, SIGNAL(streamNeedRefresh(const unsigned short&, const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&, const unsigned short&)));
2007-04-05 23:26:30 +04:00
refreshStreamList();
refreshTextBrowser();
}
RSSImp::~RSSImp(){
}
2007-04-15 23:06:19 +04:00
short RSSImp::getNumStreamSelected(){
return listStreams->indexOfTopLevelItem(listStreams->currentItem());
}
2007-04-05 23:26:30 +04:00