qBittorrent/src/rss_imp.cpp

256 lines
9.8 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-05 23:26:30 +04:00
QMenu myFinishedListMenu(this);
QListWidgetItem* item = listStreams->itemAt(pos);
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() {
if(listStreams->currentRow()<0 || rssmanager.getNbStream()==0) {
qDebug("no stream selected");
return;
}else {
textBrowser->clear();
listNews->clear();
rssmanager.removeStream(rssmanager.getStream(listStreams->currentRow()));
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() {
rssmanager.getStream(listStreams->currentRow())->setRead();
listStreams->item(listStreams->currentRow())->setData(Qt::BackgroundRole, QVariant(QColor("white")));
// update the color of the stream, is it old ?
updateStreamName(listStreams->currentRow(), LATENCY);
2007-04-05 23:26:30 +04:00
refreshNewsList();
}
// 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-05 23:26:30 +04:00
refreshTextBrowser();
}
// open the url of the news in a browser
void RSSImp::on_listNews_doubleClicked() {
if(listStreams->currentRow()>=0 && listNews->currentRow()>=0 && rssmanager.getStream(listStreams->currentRow())->getListSize()>0) {
RssItem* currentItem = rssmanager.getStream(listStreams->currentRow())->getItem(listNews->currentRow());
if(currentItem->getLink()!=NULL && currentItem->getLink().length()>5)
QDesktopServices::openUrl(QUrl(currentItem->getLink()));
}
}
//right-clik on stream : delete it
void RSSImp::deleteStream() {
if(rssmanager.getNbStream()==0) {
qDebug("no stream selected");
return;
}else {
textBrowser->clear();
listNews->clear();
rssmanager.removeStream(rssmanager.getStream(listStreams->currentRow()));
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 {
bool ok;
short index = listStreams->currentRow();
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() {
short index = listStreams->currentRow();
if(rssmanager.getNbStream()>0) {
2007-04-05 23:26:30 +04:00
textBrowser->clear();
listNews->clear();
2007-04-14 22:04:47 +04:00
listStreams->item(index)->setData(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-14 22:04:47 +04:00
listStreams->item(i)->setData(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() {
//short currentStream = listStreams->currentRow();
unsigned short nbstream = rssmanager.getNbStream();
2007-04-05 23:26:30 +04:00
listStreams->clear();
for(unsigned short i=0; i<nbstream; i++) {
2007-04-05 23:26:30 +04:00
new QListWidgetItem(rssmanager.getStream(i)->getAlias()+" ("+QString::number(rssmanager.getStream(i)->getListSize(),10).toUtf8()+")", listStreams);
updateStreamName(i, NEWS);
2007-04-05 23:26:30 +04:00
}
/*if(currentStream>=0 && currentStream<nbstream) {
listStreams->setCurrentRow(currentStream);
2007-04-12 23:24:05 +04:00
listNews->clear();
refreshNewsList();
}*/
2007-04-05 23:26:30 +04:00
}
// fills the newsList
void RSSImp::refreshNewsList() {
if(rssmanager.getNbStream()>0) {
RssStream* currentstream = rssmanager.getStream(listStreams->currentRow());
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-13 03:53:15 +04:00
if(currentstream->getItem(i)->isRead())
listNews->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("grey")));
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() {
if(listStreams->currentRow()>=0 && listNews->currentRow()>=0) {
RssItem* currentitem = rssmanager.getStream(listStreams->currentRow())->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) {
//qDebug("###################"+rssmanager.getStream(i)->getIconPath().toUtf8());
listStreams->item(i)->setData(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();
listStreams->item(i)->setText(rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("orange")));
else
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
}
// when news are refreshed, update all informations
if(type == NEWS) {
//qDebug("###################"+rssmanager.getStream(i)->getIconPath().toUtf8());
unsigned short nbitem = rssmanager.getStream(i)->getListSize();
listStreams->item(i)->setText(rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("orange")));
else
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
2007-04-14 22:04:47 +04:00
if(!rssmanager.getStream(i)->isRead())
listStreams->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
2007-04-14 22:04:47 +04:00
if(listStreams->currentRow()==i) {
listNews->clear();
refreshNewsList();
}
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath())));
}
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);
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")));
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-06 02:38:46 +04:00
2007-04-05 23:26:30 +04:00