2007-04-04 16:01:32 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez
2007-04-04 16:01:32 +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-04 16:01:32 +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
2007-04-04 16:01:32 +04:00
*/
# include "FinishedTorrents.h"
# include "misc.h"
2007-04-04 16:52:50 +04:00
# include "properties_imp.h"
2007-07-23 16:12:55 +04:00
# include "bittorrent.h"
2007-04-10 14:50:34 +04:00
# include "allocationDlg.h"
2007-08-26 20:25:22 +04:00
# include "FinishedListDelegate.h"
# include "GUI.h"
2007-07-23 16:12:55 +04:00
2007-04-04 16:01:32 +04:00
# include <QFile>
2007-04-04 17:12:11 +04:00
# include <QSettings>
2007-07-23 16:12:55 +04:00
# include <QStandardItemModel>
2007-07-23 16:46:36 +04:00
# include <QHeaderView>
2007-08-26 20:25:22 +04:00
# include <QMenu>
2008-06-26 21:31:29 +04:00
# include <QMessageBox>
2007-04-04 16:01:32 +04:00
2007-08-28 20:17:32 +04:00
FinishedTorrents : : FinishedTorrents ( QObject * parent , bittorrent * BTSession ) : parent ( parent ) , BTSession ( BTSession ) , nbFinished ( 0 ) {
2007-04-04 16:01:32 +04:00
setupUi ( this ) ;
2007-08-28 18:18:09 +04:00
actionStart - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/play.png " ) ) ) ;
actionPause - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/pause.png " ) ) ) ;
2007-08-26 20:25:22 +04:00
connect ( BTSession , SIGNAL ( addedTorrent ( QString , QTorrentHandle & , bool ) ) , this , SLOT ( torrentAdded ( QString , QTorrentHandle & , bool ) ) ) ;
2008-07-15 02:01:05 +04:00
finishedListModel = new QStandardItemModel ( 0 , 7 ) ;
2007-07-23 11:44:58 +04:00
finishedListModel - > setHeaderData ( F_NAME , Qt : : Horizontal , tr ( " Name " , " i.e: file name " ) ) ;
finishedListModel - > setHeaderData ( F_SIZE , Qt : : Horizontal , tr ( " Size " , " i.e: file size " ) ) ;
finishedListModel - > setHeaderData ( F_UPSPEED , Qt : : Horizontal , tr ( " UP Speed " , " i.e: Upload speed " ) ) ;
2007-11-25 01:24:01 +03:00
finishedListModel - > setHeaderData ( F_LEECH , Qt : : Horizontal , tr ( " Leechers " , " i.e: full/partial sources " ) ) ;
2007-07-23 11:44:58 +04:00
finishedListModel - > setHeaderData ( F_RATIO , Qt : : Horizontal , tr ( " Ratio " ) ) ;
2008-07-15 02:01:05 +04:00
finishedListModel - > setHeaderData ( F_PRIORITY , Qt : : Horizontal , tr ( " Priority " ) ) ;
2007-04-04 16:01:32 +04:00
finishedList - > setModel ( finishedListModel ) ;
2007-11-19 03:58:14 +03:00
loadHiddenColumns ( ) ;
2008-07-15 02:01:05 +04:00
// Hide priority column
finishedList - > hideColumn ( F_PRIORITY ) ;
2007-11-25 01:24:01 +03:00
// Hide hash column
2007-07-23 11:44:58 +04:00
finishedList - > hideColumn ( F_HASH ) ;
2007-04-11 17:10:13 +04:00
// Load last columns width for download list
if ( ! loadColWidthFinishedList ( ) ) {
finishedList - > header ( ) - > resizeSection ( 0 , 200 ) ;
}
2007-04-11 17:19:58 +04:00
// Make download list header clickable for sorting
finishedList - > header ( ) - > setClickable ( true ) ;
finishedList - > header ( ) - > setSortIndicatorShown ( true ) ;
connect ( finishedList - > header ( ) , SIGNAL ( sectionPressed ( int ) ) , this , SLOT ( sortFinishedList ( int ) ) ) ;
2007-07-23 11:44:58 +04:00
finishedListDelegate = new FinishedListDelegate ( finishedList ) ;
2007-04-04 16:01:32 +04:00
finishedList - > setItemDelegate ( finishedListDelegate ) ;
2007-04-04 17:12:11 +04:00
connect ( finishedList , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayFinishedListMenu ( const QPoint & ) ) ) ;
2007-11-24 03:14:19 +03:00
finishedList - > header ( ) - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
connect ( finishedList - > header ( ) , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayFinishedHoSMenu ( const QPoint & ) ) ) ;
2007-08-26 20:25:22 +04:00
connect ( finishedList , SIGNAL ( doubleClicked ( const QModelIndex & ) ) , this , SLOT ( notifyTorrentDoubleClicked ( const QModelIndex & ) ) ) ;
2008-08-08 17:17:26 +04:00
connect ( BTSession , SIGNAL ( forceFinishedListUpdate ( ) ) , this , SLOT ( updateFinishedList ( ) ) ) ;
2007-04-04 17:12:11 +04:00
actionDelete - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/delete.png " ) ) ) ;
actionPreview_file - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/preview.png " ) ) ) ;
actionDelete_Permanently - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/delete_perm.png " ) ) ) ;
actionTorrent_Properties - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/properties.png " ) ) ) ;
2007-04-10 14:50:34 +04:00
actionSet_upload_limit - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/seeding.png " ) ) ) ;
2007-08-28 18:18:09 +04:00
connect ( actionPause , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionPause_triggered ( ) ) ) ;
connect ( actionStart , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionStart_triggered ( ) ) ) ;
2007-04-13 01:10:44 +04:00
connect ( actionDelete , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionDelete_triggered ( ) ) ) ;
2008-08-08 17:17:26 +04:00
connect ( actionIncreasePriority , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionIncreasePriority_triggered ( ) ) ) ;
connect ( actionDecreasePriority , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionDecreasePriority_triggered ( ) ) ) ;
2007-04-13 01:10:44 +04:00
connect ( actionPreview_file , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionPreview_file_triggered ( ) ) ) ;
connect ( actionDelete_Permanently , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( on_actionDelete_Permanently_triggered ( ) ) ) ;
2007-11-16 21:53:21 +03:00
connect ( actionOpen_destination_folder , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( openDestinationFolder ( ) ) ) ;
2007-12-08 01:33:19 +03:00
connect ( actionBuy_it , SIGNAL ( triggered ( ) ) , ( GUI * ) parent , SLOT ( goBuyPage ( ) ) ) ;
2007-04-04 18:29:59 +04:00
connect ( actionTorrent_Properties , SIGNAL ( triggered ( ) ) , this , SLOT ( propertiesSelection ( ) ) ) ;
2007-11-19 03:58:14 +03:00
connect ( actionHOSColName , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnName ( ) ) ) ;
connect ( actionHOSColSize , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnSize ( ) ) ) ;
connect ( actionHOSColUpSpeed , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnUpSpeed ( ) ) ) ;
connect ( actionHOSColLeechers , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnLeechers ( ) ) ) ;
connect ( actionHOSColRatio , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnRatio ( ) ) ) ;
2008-07-15 02:01:05 +04:00
connect ( actionHOSColPriority , SIGNAL ( triggered ( ) ) , this , SLOT ( hideOrShowColumnPriority ( ) ) ) ;
2007-04-04 16:01:32 +04:00
}
FinishedTorrents : : ~ FinishedTorrents ( ) {
2007-04-11 17:10:13 +04:00
saveColWidthFinishedList ( ) ;
2007-11-19 03:58:14 +03:00
saveHiddenColumns ( ) ;
2007-04-04 16:01:32 +04:00
delete finishedListDelegate ;
delete finishedListModel ;
}
2007-08-26 20:25:22 +04:00
void FinishedTorrents : : notifyTorrentDoubleClicked ( const QModelIndex & index ) {
unsigned int row = index . row ( ) ;
QString hash = getHashFromRow ( row ) ;
2007-11-07 00:11:09 +03:00
emit torrentDoubleClicked ( hash , true ) ;
2007-08-26 20:25:22 +04:00
}
2008-07-15 02:01:05 +04:00
void FinishedTorrents : : hidePriorityColumn ( bool hide ) {
finishedList - > setColumnHidden ( F_PRIORITY , hide ) ;
2008-08-19 04:10:56 +04:00
if ( hide )
getActionHoSCol ( F_PRIORITY ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_cancel.png " ) ) ) ;
else
getActionHoSCol ( F_PRIORITY ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_ok.png " ) ) ) ;
2008-07-15 02:01:05 +04:00
}
2007-08-26 20:25:22 +04:00
void FinishedTorrents : : addTorrent ( QString hash ) {
if ( ! BTSession - > isFinished ( hash ) ) {
BTSession - > setFinishedTorrent ( hash ) ;
}
2007-08-19 20:50:05 +04:00
int row = getRowFromHash ( hash ) ;
if ( row ! = - 1 ) return ;
row = finishedListModel - > rowCount ( ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = BTSession - > getTorrentHandle ( hash ) ;
2007-08-19 18:20:54 +04:00
// Adding torrent to download list
finishedListModel - > insertRow ( row ) ;
2007-08-20 10:29:18 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( h . name ( ) ) ) ;
2007-08-28 23:10:35 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_SIZE ) , QVariant ( ( qlonglong ) h . actual_size ( ) ) ) ;
2007-08-19 18:20:54 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_UPSPEED ) , QVariant ( ( double ) 0. ) ) ;
2007-11-25 01:24:01 +03:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_LEECH ) , QVariant ( " 0 " ) ) ;
2007-08-20 10:29:18 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_RATIO ) , QVariant ( QString : : fromUtf8 ( misc : : toString ( BTSession - > getRealRatio ( hash ) ) . c_str ( ) ) ) ) ;
2008-07-15 02:01:05 +04:00
if ( BTSession - > isQueueingEnabled ( ) )
finishedListModel - > setData ( finishedListModel - > index ( row , F_PRIORITY ) , QVariant ( ( int ) BTSession - > getUpTorrentPriority ( hash ) ) ) ;
2007-08-19 18:20:54 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_HASH ) , QVariant ( hash ) ) ;
if ( h . is_paused ( ) ) {
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QIcon ( " :/Icons/skin/paused.png " ) , Qt : : DecorationRole ) ;
setRowColor ( row , " red " ) ;
} else {
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( QIcon ( " :/Icons/skin/seeding.png " ) ) , Qt : : DecorationRole ) ;
setRowColor ( row , " orange " ) ;
2007-04-04 16:01:32 +04:00
}
2007-08-19 18:20:54 +04:00
// Update the number of finished torrents
+ + nbFinished ;
2007-08-26 20:25:22 +04:00
emit finishedTorrentsNumberChanged ( nbFinished ) ;
}
void FinishedTorrents : : torrentAdded ( QString , QTorrentHandle & h , bool ) {
QString hash = h . hash ( ) ;
if ( BTSession - > isFinished ( hash ) ) {
addTorrent ( hash ) ;
}
2007-04-04 16:01:32 +04:00
}
// Set the color of a row in data model
2007-07-22 13:47:27 +04:00
void FinishedTorrents : : setRowColor ( int row , QString color ) {
2007-09-03 21:45:16 +04:00
unsigned int nbColumns = finishedListModel - > columnCount ( ) - 1 ;
for ( unsigned int i = 0 ; i < nbColumns ; + + i ) {
2007-07-31 14:39:03 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , i ) , QVariant ( QColor ( color ) ) , Qt : : ForegroundRole ) ;
2007-04-04 16:01:32 +04:00
}
}
2007-08-26 20:25:22 +04:00
QStringList FinishedTorrents : : getSelectedTorrents ( bool only_one ) const {
QStringList res ;
QModelIndex index ;
QModelIndexList selectedIndexes = finishedList - > selectionModel ( ) - > selectedIndexes ( ) ;
foreach ( index , selectedIndexes ) {
if ( index . column ( ) = = F_NAME ) {
// Get the file hash
QString hash = finishedListModel - > data ( finishedListModel - > index ( index . row ( ) , F_HASH ) ) . toString ( ) ;
res < < hash ;
if ( only_one ) break ;
}
}
return res ;
}
unsigned int FinishedTorrents : : getNbTorrentsInList ( ) const {
return nbFinished ;
}
2007-04-11 17:10:13 +04:00
// Load columns width in a file that were saved previously
// (finished list)
bool FinishedTorrents : : loadColWidthFinishedList ( ) {
qDebug ( " Loading columns width for finished list " ) ;
QSettings settings ( " qBittorrent " , " qBittorrent " ) ;
QString line = settings . value ( " FinishedListColsWidth " , QString ( ) ) . toString ( ) ;
if ( line . isEmpty ( ) )
return false ;
QStringList width_list = line . split ( ' ' ) ;
2007-11-19 03:58:14 +03:00
if ( width_list . size ( ) < finishedListModel - > columnCount ( ) - 1 )
2007-04-11 17:10:13 +04:00
return false ;
unsigned int listSize = width_list . size ( ) ;
for ( unsigned int i = 0 ; i < listSize ; + + i ) {
finishedList - > header ( ) - > resizeSection ( i , width_list . at ( i ) . toInt ( ) ) ;
}
qDebug ( " Finished list columns width loaded " ) ;
return true ;
}
// Save columns width in a file to remember them
// (finished list)
void FinishedTorrents : : saveColWidthFinishedList ( ) const {
qDebug ( " Saving columns width in finished list " ) ;
QSettings settings ( " qBittorrent " , " qBittorrent " ) ;
QStringList width_list ;
2007-11-19 03:58:14 +03:00
QStringList new_width_list ;
short nbColumns = finishedListModel - > columnCount ( ) - 1 ;
QString line = settings . value ( " FinishedListColsWidth " , QString ( ) ) . toString ( ) ;
if ( ! line . isEmpty ( ) ) {
width_list = line . split ( ' ' ) ;
}
for ( short i = 0 ; i < nbColumns ; + + i ) {
if ( finishedList - > columnWidth ( i ) < 1 & & width_list . size ( ) = = finishedListModel - > columnCount ( ) - 1 & & width_list . at ( i ) . toInt ( ) > = 1 ) {
// load the former width
new_width_list < < width_list . at ( i ) ;
} else if ( finishedList - > columnWidth ( i ) > = 1 ) {
// usual case, save the current width
new_width_list < < QString : : fromUtf8 ( misc : : toString ( finishedList - > columnWidth ( i ) ) . c_str ( ) ) ;
} else {
// default width
finishedList - > resizeColumnToContents ( i ) ;
new_width_list < < QString : : fromUtf8 ( misc : : toString ( finishedList - > columnWidth ( i ) ) . c_str ( ) ) ;
}
2007-04-11 17:10:13 +04:00
}
2007-11-19 03:58:14 +03:00
settings . setValue ( " FinishedListColsWidth " , new_width_list . join ( " " ) ) ;
2007-04-11 17:10:13 +04:00
qDebug ( " Finished list columns width saved " ) ;
}
2007-04-10 14:11:36 +04:00
void FinishedTorrents : : on_actionSet_upload_limit_triggered ( ) {
2007-04-10 14:50:34 +04:00
QModelIndexList selectedIndexes = finishedList - > selectionModel ( ) - > selectedIndexes ( ) ;
2007-04-10 14:11:36 +04:00
QModelIndex index ;
QStringList hashes ;
foreach ( index , selectedIndexes ) {
2007-07-23 11:44:58 +04:00
if ( index . column ( ) = = F_NAME ) {
2007-04-10 14:11:36 +04:00
// Get the file hash
2007-07-23 11:44:58 +04:00
hashes < < finishedListModel - > data ( finishedListModel - > index ( index . row ( ) , F_HASH ) ) . toString ( ) ;
2007-04-10 14:11:36 +04:00
}
}
2007-04-10 14:50:34 +04:00
new BandwidthAllocationDialog ( this , true , BTSession , hashes ) ;
2007-04-10 14:11:36 +04:00
}
2007-04-04 16:01:32 +04:00
void FinishedTorrents : : updateFinishedList ( ) {
QString hash ;
2007-08-19 18:20:54 +04:00
QStringList finishedSHAs = BTSession - > getFinishedTorrents ( ) ;
2007-04-04 16:01:32 +04:00
foreach ( hash , finishedSHAs ) {
2007-08-20 10:29:18 +04:00
QTorrentHandle h = BTSession - > getTorrentHandle ( hash ) ;
2007-04-04 16:01:32 +04:00
if ( ! h . is_valid ( ) ) {
qDebug ( " Problem: This torrent is not valid in finished list " ) ;
continue ;
}
2007-08-01 12:43:00 +04:00
int row = getRowFromHash ( hash ) ;
if ( row = = - 1 ) {
2007-08-19 18:20:54 +04:00
qDebug ( " Cannot find torrent in finished list, adding it " ) ;
2007-08-26 20:25:22 +04:00
addTorrent ( hash ) ;
row = getRowFromHash ( hash ) ;
2007-08-01 12:43:00 +04:00
}
2007-08-26 20:25:22 +04:00
Q_ASSERT ( row ! = - 1 ) ;
2008-07-15 02:01:05 +04:00
// Update priority
if ( BTSession - > isQueueingEnabled ( ) ) {
2008-07-15 22:11:27 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_PRIORITY ) , QVariant ( ( int ) BTSession - > getUpTorrentPriority ( hash ) ) ) ;
2008-07-15 02:01:05 +04:00
if ( h . is_paused ( ) & & BTSession - > isUploadQueued ( hash ) ) {
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/queued.png " ) ) ) , Qt : : DecorationRole ) ;
setRowColor ( row , QString : : fromUtf8 ( " grey " ) ) ;
}
}
2007-08-01 12:43:00 +04:00
if ( h . is_paused ( ) ) continue ;
2007-09-02 02:57:50 +04:00
if ( BTSession - > getTorrentsToPauseAfterChecking ( ) . indexOf ( hash ) ! = - 1 ) {
continue ;
}
2008-09-03 22:19:17 +04:00
if ( h . state ( ) = = torrent_status : : downloading | | ( h . state ( ) ! = torrent_status : : checking_files & & h . state ( ) ! = torrent_status : : queued_for_checking & & h . is_seed ( ) ) ) {
2007-07-23 12:32:06 +04:00
// What are you doing here? go back to download tab!
2008-06-26 21:37:39 +04:00
int reponse = QMessageBox : : question ( this , tr ( " Incomplete torrent in seeding list " ) , tr ( " It appears that the state of '%1' torrent changed from 'seeding' to 'downloading'. Would you like to move it back to download list? (otherwise the torrent will simply be deleted) " ) . arg ( h . name ( ) ) , QMessageBox : : Yes | QMessageBox : : No ) ;
2008-06-26 21:31:29 +04:00
if ( reponse = = QMessageBox : : Yes ) {
qDebug ( " Info: a torrent was moved from finished to download tab " ) ;
deleteTorrent ( hash ) ;
2008-08-17 07:47:52 +04:00
BTSession - > setUnfinishedTorrent ( hash ) ;
2008-06-26 21:31:29 +04:00
emit torrentMovedFromFinishedList ( hash ) ;
}
else if ( reponse = = QMessageBox : : No ) {
qDebug ( " Deleted from the finished " ) ;
BTSession - > deleteTorrent ( hash , true ) ;
}
2007-04-04 16:01:32 +04:00
continue ;
}
2007-08-28 18:30:24 +04:00
if ( h . state ( ) = = torrent_status : : checking_files ) {
2007-09-02 02:57:50 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( QIcon ( QString : : fromUtf8 ( " :/Icons/time.png " ) ) ) , Qt : : DecorationRole ) ;
setRowColor ( row , QString : : fromUtf8 ( " grey " ) ) ;
2007-08-28 18:30:24 +04:00
continue ;
}
setRowColor ( row , QString : : fromUtf8 ( " orange " ) ) ;
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/seeding.png " ) ) ) , Qt : : DecorationRole ) ;
2007-11-25 01:16:00 +03:00
if ( ! finishedList - > isColumnHidden ( F_UPSPEED ) ) {
finishedListModel - > setData ( finishedListModel - > index ( row , F_UPSPEED ) , QVariant ( ( double ) h . upload_payload_rate ( ) ) ) ;
}
2007-11-25 01:24:01 +03:00
if ( ! finishedList - > isColumnHidden ( F_LEECH ) ) {
finishedListModel - > setData ( finishedListModel - > index ( row , F_LEECH ) , misc : : toQString ( h . num_peers ( ) - h . num_seeds ( ) , true ) ) ;
2007-11-25 01:16:00 +03:00
}
if ( ! finishedList - > isColumnHidden ( F_RATIO ) ) {
finishedListModel - > setData ( finishedListModel - > index ( row , F_RATIO ) , QVariant ( misc : : toQString ( BTSession - > getRealRatio ( hash ) ) ) ) ;
}
2007-04-04 16:01:32 +04:00
}
}
2007-07-22 13:47:27 +04:00
int FinishedTorrents : : getRowFromHash ( QString hash ) const {
2007-04-15 22:15:22 +04:00
unsigned int nbRows = finishedListModel - > rowCount ( ) ;
for ( unsigned int i = 0 ; i < nbRows ; + + i ) {
2007-07-23 11:44:58 +04:00
if ( finishedListModel - > data ( finishedListModel - > index ( i , F_HASH ) ) = = hash ) {
2007-04-15 22:15:22 +04:00
return i ;
}
}
return - 1 ;
}
2007-08-26 20:25:22 +04:00
// Note: does not actually pause the torrent in BT Session
void FinishedTorrents : : pauseTorrent ( QString hash ) {
2007-06-17 16:15:04 +04:00
int row = getRowFromHash ( hash ) ;
2008-05-16 11:10:50 +04:00
if ( row = = - 1 )
return ;
2007-08-26 20:25:22 +04:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_UPSPEED ) , QVariant ( ( double ) 0.0 ) ) ;
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QIcon ( QString : : fromUtf8 ( " :/Icons/skin/paused.png " ) ) , Qt : : DecorationRole ) ;
2007-11-25 01:24:01 +03:00
finishedListModel - > setData ( finishedListModel - > index ( row , F_LEECH ) , QVariant ( QString : : fromUtf8 ( " 0 " ) ) ) ;
2007-08-26 20:25:22 +04:00
setRowColor ( row , QString : : fromUtf8 ( " red " ) ) ;
}
void FinishedTorrents : : resumeTorrent ( QString hash ) {
int row = getRowFromHash ( hash ) ;
Q_ASSERT ( row ! = - 1 ) ;
finishedListModel - > setData ( finishedListModel - > index ( row , F_NAME ) , QVariant ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/seeding.png " ) ) ) , Qt : : DecorationRole ) ;
setRowColor ( row , QString : : fromUtf8 ( " orange " ) ) ;
2007-04-04 16:01:32 +04:00
}
2007-04-04 16:33:53 +04:00
2007-08-26 20:25:22 +04:00
QString FinishedTorrents : : getHashFromRow ( unsigned int row ) const {
Q_ASSERT ( row < ( unsigned int ) finishedListModel - > rowCount ( ) ) ;
return finishedListModel - > data ( finishedListModel - > index ( row , F_HASH ) ) . toString ( ) ;
2007-04-04 16:33:53 +04:00
}
2007-08-26 20:25:22 +04:00
// Will move it to download tab
void FinishedTorrents : : deleteTorrent ( QString hash ) {
int row = getRowFromHash ( hash ) ;
2007-08-26 21:25:37 +04:00
if ( row = = - 1 ) {
qDebug ( " Torrent is not in finished list, nothing to delete " ) ;
return ;
}
2007-08-26 20:25:22 +04:00
finishedListModel - > removeRow ( row ) ;
- - nbFinished ;
emit finishedTorrentsNumberChanged ( nbFinished ) ;
2007-04-04 16:33:53 +04:00
}
2007-04-04 16:52:50 +04:00
// Show torrent properties dialog
void FinishedTorrents : : showProperties ( const QModelIndex & index ) {
2007-11-20 00:10:57 +03:00
showPropertiesFromHash ( finishedListModel - > data ( finishedListModel - > index ( index . row ( ) , F_HASH ) ) . toString ( ) ) ;
2007-04-04 16:52:50 +04:00
}
2007-11-07 00:11:09 +03:00
void FinishedTorrents : : showPropertiesFromHash ( QString hash ) {
QTorrentHandle h = BTSession - > getTorrentHandle ( hash ) ;
properties * prop = new properties ( this , BTSession , h ) ;
connect ( prop , SIGNAL ( filteredFilesChanged ( QString ) ) , this , SLOT ( updateFileSize ( QString ) ) ) ;
2007-11-20 00:10:57 +03:00
connect ( prop , SIGNAL ( trackersChanged ( QString ) ) , BTSession , SLOT ( saveTrackerFile ( QString ) ) ) ;
2007-11-07 00:11:09 +03:00
prop - > show ( ) ;
}
2007-07-23 11:44:58 +04:00
void FinishedTorrents : : updateFileSize ( QString hash ) {
int row = getRowFromHash ( hash ) ;
2007-08-20 10:29:18 +04:00
QTorrentHandle h = BTSession - > getTorrentHandle ( hash ) ;
finishedListModel - > setData ( finishedListModel - > index ( row , F_SIZE ) , QVariant ( ( qlonglong ) h . actual_size ( ) ) ) ;
2007-07-23 11:44:58 +04:00
}
2007-04-04 16:52:50 +04:00
// display properties of selected items
void FinishedTorrents : : propertiesSelection ( ) {
QModelIndexList selectedIndexes = finishedList - > selectionModel ( ) - > selectedIndexes ( ) ;
QModelIndex index ;
foreach ( index , selectedIndexes ) {
2007-07-23 11:44:58 +04:00
if ( index . column ( ) = = F_NAME ) {
2007-04-04 16:52:50 +04:00
showProperties ( index ) ;
}
}
}
2007-04-04 17:12:11 +04:00
void FinishedTorrents : : displayFinishedListMenu ( const QPoint & pos ) {
QMenu myFinishedListMenu ( this ) ;
QModelIndex index ;
// Enable/disable pause/start action given the DL state
QModelIndexList selectedIndexes = finishedList - > selectionModel ( ) - > selectedIndexes ( ) ;
2007-08-28 18:18:09 +04:00
bool has_pause = false , has_start = false , has_preview = false ;
foreach ( index , selectedIndexes ) {
if ( index . column ( ) = = F_NAME ) {
2007-04-04 17:12:11 +04:00
// Get the file name
2007-08-20 10:29:18 +04:00
QString hash = finishedListModel - > data ( finishedListModel - > index ( index . row ( ) , F_HASH ) ) . toString ( ) ;
2007-08-28 18:18:09 +04:00
// Get handle and pause the torrent
2007-08-20 10:29:18 +04:00
QTorrentHandle h = BTSession - > getTorrentHandle ( hash ) ;
2007-08-28 18:18:09 +04:00
if ( ! h . is_valid ( ) ) continue ;
if ( h . is_paused ( ) ) {
if ( ! has_start ) {
myFinishedListMenu . addAction ( actionStart ) ;
has_start = true ;
}
} else {
if ( ! has_pause ) {
myFinishedListMenu . addAction ( actionPause ) ;
has_pause = true ;
}
}
2007-11-16 21:36:57 +03:00
if ( BTSession - > isFilePreviewPossible ( hash ) & & ! has_preview ) {
2007-04-04 17:12:11 +04:00
myFinishedListMenu . addAction ( actionPreview_file ) ;
2007-08-28 18:18:09 +04:00
has_preview = true ;
2007-04-04 17:12:11 +04:00
}
2007-08-28 18:18:09 +04:00
if ( has_pause & & has_start & & has_preview ) break ;
2007-04-04 17:12:11 +04:00
}
}
2007-08-28 18:18:09 +04:00
myFinishedListMenu . addSeparator ( ) ;
myFinishedListMenu . addAction ( actionDelete ) ;
myFinishedListMenu . addAction ( actionDelete_Permanently ) ;
myFinishedListMenu . addSeparator ( ) ;
myFinishedListMenu . addAction ( actionSet_upload_limit ) ;
myFinishedListMenu . addSeparator ( ) ;
2007-11-16 21:53:21 +03:00
myFinishedListMenu . addAction ( actionOpen_destination_folder ) ;
2007-08-28 18:18:09 +04:00
myFinishedListMenu . addAction ( actionTorrent_Properties ) ;
2008-08-08 17:17:26 +04:00
if ( BTSession - > isQueueingEnabled ( ) ) {
myFinishedListMenu . addSeparator ( ) ;
myFinishedListMenu . addAction ( actionIncreasePriority ) ;
myFinishedListMenu . addAction ( actionDecreasePriority ) ;
}
2007-12-08 01:33:19 +03:00
myFinishedListMenu . addSeparator ( ) ;
myFinishedListMenu . addAction ( actionBuy_it ) ;
2007-11-19 03:58:14 +03:00
2007-04-04 17:12:11 +04:00
// Call menu
// XXX: why mapToGlobal() is not enough?
2008-07-27 19:51:14 +04:00
myFinishedListMenu . exec ( mapToGlobal ( pos ) + QPoint ( 10 , 58 ) ) ;
2007-04-04 17:12:11 +04:00
}
2007-07-23 16:12:55 +04:00
2007-11-24 03:14:19 +03:00
2007-11-19 03:58:14 +03:00
/*
* Hiding Columns functions
*/
2007-11-24 03:14:19 +03:00
// hide/show columns menu
void FinishedTorrents : : displayFinishedHoSMenu ( const QPoint & pos ) {
QMenu hideshowColumn ( this ) ;
hideshowColumn . setTitle ( tr ( " Hide or Show Column " ) ) ;
2008-07-15 02:01:05 +04:00
int lastCol ;
if ( BTSession - > isQueueingEnabled ( ) ) {
lastCol = F_PRIORITY ;
} else {
lastCol = F_RATIO ;
}
2008-08-19 04:10:56 +04:00
for ( int i = 0 ; i < = lastCol ; i + + ) {
2007-11-24 03:14:19 +03:00
hideshowColumn . addAction ( getActionHoSCol ( i ) ) ;
}
// Call menu
2008-07-27 19:51:14 +04:00
hideshowColumn . exec ( mapToGlobal ( pos ) + QPoint ( 10 , 34 ) ) ;
2007-11-24 03:14:19 +03:00
}
2007-11-19 03:58:14 +03:00
// toggle hide/show a column
void FinishedTorrents : : hideOrShowColumn ( int index ) {
2007-11-24 22:04:28 +03:00
unsigned int nbVisibleColumns = 0 ;
unsigned int nbCols = finishedListModel - > columnCount ( ) ;
// Count visible columns
for ( unsigned int i = 0 ; i < nbCols ; + + i ) {
if ( ! finishedList - > isColumnHidden ( i ) )
+ + nbVisibleColumns ;
}
2007-11-19 03:58:14 +03:00
if ( ! finishedList - > isColumnHidden ( index ) ) {
2007-11-24 22:04:28 +03:00
// User wants to hide the column
// Is there at least one other visible column?
if ( nbVisibleColumns < = 1 ) return ;
// User can hide the column, do it.
finishedList - > setColumnHidden ( index , true ) ;
getActionHoSCol ( index ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_cancel.png " ) ) ) ;
- - nbVisibleColumns ;
2007-11-19 03:58:14 +03:00
} else {
2007-11-24 22:04:28 +03:00
// User want to display the column
2007-11-19 03:58:14 +03:00
finishedList - > setColumnHidden ( index , false ) ;
getActionHoSCol ( index ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_ok.png " ) ) ) ;
2007-11-24 22:04:28 +03:00
+ + nbVisibleColumns ;
}
//resize all others non-hidden columns
for ( unsigned int i = 0 ; i < nbCols ; + + i ) {
if ( ! finishedList - > isColumnHidden ( i ) ) {
finishedList - > setColumnWidth ( i , ( int ) ceil ( finishedList - > columnWidth ( i ) + ( finishedList - > columnWidth ( index ) / nbVisibleColumns ) ) ) ;
2007-11-19 03:58:14 +03:00
}
}
}
void FinishedTorrents : : hideOrShowColumnName ( ) {
hideOrShowColumn ( F_NAME ) ;
}
void FinishedTorrents : : hideOrShowColumnSize ( ) {
hideOrShowColumn ( F_SIZE ) ;
}
void FinishedTorrents : : hideOrShowColumnUpSpeed ( ) {
hideOrShowColumn ( F_UPSPEED ) ;
}
void FinishedTorrents : : hideOrShowColumnLeechers ( ) {
2007-11-25 01:24:01 +03:00
hideOrShowColumn ( F_LEECH ) ;
2007-11-19 03:58:14 +03:00
}
void FinishedTorrents : : hideOrShowColumnRatio ( ) {
hideOrShowColumn ( F_RATIO ) ;
}
2008-07-15 02:01:05 +04:00
void FinishedTorrents : : hideOrShowColumnPriority ( ) {
hideOrShowColumn ( F_PRIORITY ) ;
}
2007-11-19 03:58:14 +03:00
// load the previous settings, and hide the columns
bool FinishedTorrents : : loadHiddenColumns ( ) {
bool loaded = false ;
QSettings settings ( " qBittorrent " , " qBittorrent " ) ;
QString line = settings . value ( " FinishedListColsHoS " , QString ( ) ) . toString ( ) ;
QStringList ishidden_list ;
if ( ! line . isEmpty ( ) ) {
ishidden_list = line . split ( ' ' ) ;
if ( ishidden_list . size ( ) = = finishedListModel - > columnCount ( ) - 1 ) {
unsigned int listSize = ishidden_list . size ( ) ;
for ( unsigned int i = 0 ; i < listSize ; + + i ) {
finishedList - > header ( ) - > resizeSection ( i , ishidden_list . at ( i ) . toInt ( ) ) ;
}
loaded = true ;
}
}
for ( int i = 0 ; i < finishedListModel - > columnCount ( ) - 1 ; i + + ) {
if ( loaded & & ishidden_list . at ( i ) = = " 0 " ) {
finishedList - > setColumnHidden ( i , true ) ;
getActionHoSCol ( i ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_cancel.png " ) ) ) ;
} else {
getActionHoSCol ( i ) - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/button_ok.png " ) ) ) ;
}
}
return loaded ;
}
// save the hidden columns in settings
void FinishedTorrents : : saveHiddenColumns ( ) {
QSettings settings ( " qBittorrent " , " qBittorrent " ) ;
QStringList ishidden_list ;
short nbColumns = finishedListModel - > columnCount ( ) - 1 ;
for ( short i = 0 ; i < nbColumns ; + + i ) {
if ( finishedList - > isColumnHidden ( i ) ) {
ishidden_list < < QString : : fromUtf8 ( misc : : toString ( 0 ) . c_str ( ) ) ;
} else {
ishidden_list < < QString : : fromUtf8 ( misc : : toString ( 1 ) . c_str ( ) ) ;
}
}
settings . setValue ( " FinishedListColsHoS " , ishidden_list . join ( " " ) ) ;
}
// getter, return the action hide or show whose id is index
QAction * FinishedTorrents : : getActionHoSCol ( int index ) {
switch ( index ) {
case F_NAME :
return actionHOSColName ;
2007-11-25 01:24:01 +03:00
break ;
2007-11-19 03:58:14 +03:00
case F_SIZE :
return actionHOSColSize ;
2007-11-25 01:24:01 +03:00
break ;
2007-11-19 03:58:14 +03:00
case F_UPSPEED :
return actionHOSColUpSpeed ;
2007-11-25 01:24:01 +03:00
break ;
case F_LEECH :
2007-11-19 03:58:14 +03:00
return actionHOSColLeechers ;
2007-11-25 01:24:01 +03:00
break ;
2007-11-19 03:58:14 +03:00
case F_RATIO :
return actionHOSColRatio ;
2007-11-25 01:24:01 +03:00
break ;
2008-07-15 02:01:05 +04:00
case F_PRIORITY :
return actionHOSColPriority ;
break ;
2007-11-19 03:58:14 +03:00
default :
return NULL ;
}
}
2007-07-23 16:12:55 +04:00
/*
* Sorting functions
*/
void FinishedTorrents : : sortFinishedList ( int index ) {
static Qt : : SortOrder sortOrder = Qt : : AscendingOrder ;
if ( finishedList - > header ( ) - > sortIndicatorSection ( ) = = index ) {
if ( sortOrder = = Qt : : AscendingOrder ) {
sortOrder = Qt : : DescendingOrder ;
} else {
sortOrder = Qt : : AscendingOrder ;
}
}
finishedList - > header ( ) - > setSortIndicator ( index , sortOrder ) ;
switch ( index ) {
case F_SIZE :
case F_UPSPEED :
2008-08-19 16:17:05 +04:00
case F_PRIORITY :
2007-07-23 16:12:55 +04:00
sortFinishedListFloat ( index , sortOrder ) ;
break ;
default :
sortFinishedListString ( index , sortOrder ) ;
}
}
void FinishedTorrents : : sortFinishedListFloat ( int index , Qt : : SortOrder sortOrder ) {
QList < QPair < int , double > > lines ;
// insertion sorting
unsigned int nbRows = finishedListModel - > rowCount ( ) ;
for ( unsigned int i = 0 ; i < nbRows ; + + i ) {
misc : : insertSort ( lines , QPair < int , double > ( i , finishedListModel - > data ( finishedListModel - > index ( i , index ) ) . toDouble ( ) ) , sortOrder ) ;
}
// Insert items in new model, in correct order
unsigned int nbRows_old = lines . size ( ) ;
for ( unsigned int row = 0 ; row < nbRows_old ; + + row ) {
finishedListModel - > insertRow ( finishedListModel - > rowCount ( ) ) ;
unsigned int sourceRow = lines [ row ] . first ;
unsigned int nbColumns = finishedListModel - > columnCount ( ) ;
for ( unsigned int col = 0 ; col < nbColumns ; + + col ) {
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) ) ) ;
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) , Qt : : DecorationRole ) , Qt : : DecorationRole ) ;
2007-07-31 14:39:03 +04:00
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) , Qt : : ForegroundRole ) , Qt : : ForegroundRole ) ;
2007-07-23 16:12:55 +04:00
}
}
// Remove old rows
finishedListModel - > removeRows ( 0 , nbRows_old ) ;
}
void FinishedTorrents : : sortFinishedListString ( int index , Qt : : SortOrder sortOrder ) {
QList < QPair < int , QString > > lines ;
// Insertion sorting
unsigned int nbRows = finishedListModel - > rowCount ( ) ;
for ( unsigned int i = 0 ; i < nbRows ; + + i ) {
misc : : insertSortString ( lines , QPair < int , QString > ( i , finishedListModel - > data ( finishedListModel - > index ( i , index ) ) . toString ( ) ) , sortOrder ) ;
}
// Insert items in new model, in correct order
unsigned int nbRows_old = lines . size ( ) ;
for ( unsigned int row = 0 ; row < nbRows_old ; + + row ) {
finishedListModel - > insertRow ( finishedListModel - > rowCount ( ) ) ;
unsigned int sourceRow = lines [ row ] . first ;
unsigned int nbColumns = finishedListModel - > columnCount ( ) ;
for ( unsigned int col = 0 ; col < nbColumns ; + + col ) {
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) ) ) ;
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) , Qt : : DecorationRole ) , Qt : : DecorationRole ) ;
2007-07-31 14:39:03 +04:00
finishedListModel - > setData ( finishedListModel - > index ( nbRows_old + row , col ) , finishedListModel - > data ( finishedListModel - > index ( sourceRow , col ) , Qt : : ForegroundRole ) , Qt : : ForegroundRole ) ;
2007-07-23 16:12:55 +04:00
}
}
// Remove old rows
finishedListModel - > removeRows ( 0 , nbRows_old ) ;
}