2007-04-05 23:26:30 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez , Arnaud Demaiziere
2007-04-05 23:26:30 +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-04-05 23:26:30 +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-04-05 23:26:30 +04:00
*/
# include "rss_imp.h"
# include <QDesktopServices>
# include <QInputDialog>
# include <QMenu>
# include <QStandardItemModel>
// display a right-click menu
2007-04-15 22:53:53 +04:00
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 ) ;
2007-04-15 22:53:53 +04:00
} else {
myFinishedListMenu . addAction ( actionCreate ) ;
myFinishedListMenu . addAction ( actionRefreshAll ) ;
2007-04-05 23:26:30 +04:00
}
2007-04-11 16:33:41 +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 {
2007-06-16 03:40:33 +04:00
int ok = QMessageBox : : question ( this , tr ( " Are you sure? -- qBittorrent " ) , tr ( " Are you sure you want to delete this stream from the list? " ) ,
2007-04-16 01:43:58 +04:00
tr ( " &Yes " ) , tr ( " &No " ) ,
QString ( ) , 0 , 1 ) ;
if ( ok = = 0 ) {
textBrowser - > clear ( ) ;
listNews - > clear ( ) ;
rssmanager . removeStream ( rssmanager . getStream ( getNumStreamSelected ( ) ) ) ;
refreshStreamList ( ) ;
}
2007-04-05 23:26:30 +04:00
}
}
// refresh all streams by a button
void RSSImp : : on_refreshAll_button_clicked ( ) {
2007-04-11 16:59:55 +04:00
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-16 01:43:58 +04:00
int ok = QMessageBox : : question ( this , tr ( " Are you sure? -- qBittorrent " ) , tr ( " Are you sure you want to delete this stream from the list ? " ) , tr ( " &Yes " ) , tr ( " &No " ) , QString ( ) , 0 , 1 ) ;
if ( ok = = 0 ) {
moveCurrentItem ( ) ;
textBrowser - > clear ( ) ;
listNews - > clear ( ) ;
rssmanager . removeStream ( rssmanager . getStream ( getNumStreamSelected ( ) ) ) ;
refreshStreamList ( ) ;
}
2007-04-05 23:26:30 +04:00
}
}
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 ( ) ;
2007-04-11 16:33:41 +04:00
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 ( ) {
2007-04-12 00:24:41 +04:00
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
2007-04-11 16:59:55 +04:00
void RSSImp : : refreshAllStreams ( ) {
2007-04-12 00:24:41 +04:00
textBrowser - > clear ( ) ;
listNews - > clear ( ) ;
2007-04-14 16:41:42 +04:00
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 " ) ) ) ;
2007-04-12 00:24:41 +04:00
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 ) {
2007-04-11 16:59:55 +04:00
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 ( ) {
2007-04-13 16:07:14 +04:00
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 ;
2007-04-13 16:07:14 +04:00
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-06-16 03:40:33 +04:00
description - > setText ( 0 , tr ( " no description available " ) ) ;
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 " ) ) ) ;
2007-04-15 16:05:14 +04:00
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 ( ) ;
2007-04-13 16:07:14 +04:00
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 ( ) ) ;
2007-04-14 16:41:42 +04:00
textBrowser - > setHtml ( currentitem - > getTitle ( ) + " : \n " + currentitem - > getDescription ( ) ) ;
2007-04-05 23:26:30 +04:00
currentitem - > setRead ( ) ;
}
}
2007-04-14 16:41:42 +04:00
// 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 ) {
2007-04-15 16:05:14 +04:00
// 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 ( ) ) ) ) ;
2007-04-15 16:05:14 +04:00
}
// on click, show the age of the stream
if ( type = = LATENCY ) {
2007-07-21 23:37:10 +04:00
unsigned short nbitem = rssmanager . getStream ( i ) - > getNbNonRead ( ) ;
2007-04-15 23:06:19 +04:00
listStreams - > topLevelItem ( i ) - > setText ( 0 , rssmanager . getStream ( i ) - > getAlias ( ) . toUtf8 ( ) + " ( " + QString : : number ( nbitem , 10 ) . toUtf8 ( ) + " ) " ) ;
2007-04-15 16:05:14 +04:00
if ( nbitem = = 0 )
2007-04-15 23:06:19 +04:00
listStreams - > topLevelItem ( i ) - > setData ( 0 , Qt : : ForegroundRole , QVariant ( QColor ( " red " ) ) ) ;
2007-04-15 16:05:14 +04:00
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 " ) ) ) ;
2007-04-15 16:05:14 +04:00
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 " ) ) ) ;
2007-04-13 16:07:14 +04:00
}
2007-04-15 16:05:14 +04:00
// when news are refreshed, update all informations
if ( type = = NEWS ) {
2007-04-13 16:07:14 +04:00
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 ( ) + " ) " ) ;
2007-04-13 16:07:14 +04:00
if ( nbitem = = 0 )
2007-04-15 23:06:19 +04:00
listStreams - > topLevelItem ( i ) - > setData ( 0 , Qt : : ForegroundRole , QVariant ( QColor ( " red " ) ) ) ;
2007-04-13 16:07:14 +04:00
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 " ) ) ) ;
2007-04-13 16:07:14 +04:00
else
2007-04-15 23:06:19 +04:00
listStreams - > topLevelItem ( i ) - > setData ( 0 , Qt : : ForegroundRole , QVariant ( QColor ( " green " ) ) ) ;
2007-04-15 18:53:38 +04:00
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-13 16:07:14 +04:00
}
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 " ) ) ) ;
2007-04-16 01:43:58 +04:00
refreshAll_button - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/refresh.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 " ) ) ) ;
2007-04-15 22:53:53 +04:00
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 ( ) ) ) ;
2007-04-11 16:59:55 +04:00
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