2009-11-14 13:37:45 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
* Copyright ( C ) 2006 Christophe Dumez
*
* 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 .
*
* In addition , as a special exception , the copyright holders give permission to
* link this program with the OpenSSL project ' s " OpenSSL " library ( or with
* modified versions of it that use the same license as the " OpenSSL " library ) ,
* and distribute the linked executables . You must obey the GNU General Public
* License in all respects for all of the code used other than " OpenSSL " . If you
* modify file ( s ) , you may extend this exception to your version of the file ( s ) ,
* but you are not obligated to do so . If you do not wish to do so , delete this
* exception statement from your version .
*
* Contact : chris @ qbittorrent . org
*/
# include "peerlistwidget.h"
# include "peerlistdelegate.h"
2014-12-06 20:24:07 +03:00
# include "peerlistsortmodel.h"
2015-04-13 19:02:48 +03:00
# include "core/net/reverseresolution.h"
2015-04-13 19:19:53 +03:00
# include "core/preferences.h"
2009-11-15 13:00:07 +03:00
# include "propertieswidget.h"
2010-11-20 18:59:17 +03:00
# include "geoipmanager.h"
2009-11-17 14:46:43 +03:00
# include "peeraddition.h"
2009-11-17 17:19:50 +03:00
# include "speedlimitdlg.h"
2015-04-19 18:17:47 +03:00
# include "guiiconprovider.h"
# include "core/bittorrent/torrenthandle.h"
# include "core/bittorrent/peerinfo.h"
2015-04-13 19:19:53 +03:00
# include "core/logger.h"
2015-04-19 18:17:47 +03:00
2009-11-14 13:37:45 +03:00
# include <QStandardItemModel>
# include <QSortFilterProxyModel>
# include <QSet>
2009-11-29 00:46:46 +03:00
# include <QHeaderView>
2009-11-17 14:46:43 +03:00
# include <QMenu>
2010-07-01 02:28:23 +04:00
# include <QClipboard>
2010-11-23 00:55:32 +03:00
2012-02-25 23:02:19 +04:00
PeerListWidget : : PeerListWidget ( PropertiesWidget * parent ) :
QTreeView ( parent ) , m_properties ( parent ) , m_displayFlags ( false )
{
2011-03-13 13:09:31 +03:00
// Load settings
loadSettings ( ) ;
2009-11-14 13:37:45 +03:00
// Visual settings
2013-06-29 19:10:45 +04:00
setUniformRowHeights ( true ) ;
2009-11-14 13:37:45 +03:00
setRootIsDecorated ( false ) ;
setItemsExpandable ( false ) ;
setAllColumnsShowFocus ( true ) ;
2009-11-17 19:02:35 +03:00
setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
2009-11-14 13:37:45 +03:00
// List Model
2012-02-25 23:02:19 +04:00
m_listModel = new QStandardItemModel ( 0 , PeerListDelegate : : COL_COUNT ) ;
2013-07-05 17:29:07 +04:00
m_listModel - > setHeaderData ( PeerListDelegate : : COUNTRY , Qt : : Horizontal , QVariant ( ) ) ; // Country flag column
2012-02-25 23:02:19 +04:00
m_listModel - > setHeaderData ( PeerListDelegate : : IP , Qt : : Horizontal , tr ( " IP " ) ) ;
2014-08-29 13:23:00 +04:00
m_listModel - > setHeaderData ( PeerListDelegate : : PORT , Qt : : Horizontal , tr ( " Port " ) ) ;
2013-04-22 01:12:14 +04:00
m_listModel - > setHeaderData ( PeerListDelegate : : FLAGS , Qt : : Horizontal , tr ( " Flags " ) ) ;
2012-02-25 23:02:19 +04:00
m_listModel - > setHeaderData ( PeerListDelegate : : CONNECTION , Qt : : Horizontal , tr ( " Connection " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : CLIENT , Qt : : Horizontal , tr ( " Client " , " i.e.: Client application " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : PROGRESS , Qt : : Horizontal , tr ( " Progress " , " i.e: % downloaded " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : DOWN_SPEED , Qt : : Horizontal , tr ( " Down Speed " , " i.e: Download speed " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : UP_SPEED , Qt : : Horizontal , tr ( " Up Speed " , " i.e: Upload speed " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : TOT_DOWN , Qt : : Horizontal , tr ( " Downloaded " , " i.e: total data downloaded " ) ) ;
m_listModel - > setHeaderData ( PeerListDelegate : : TOT_UP , Qt : : Horizontal , tr ( " Uploaded " , " i.e: total data uploaded " ) ) ;
2014-12-06 20:07:26 +03:00
m_listModel - > setHeaderData ( PeerListDelegate : : RELEVANCE , Qt : : Horizontal , tr ( " Relevance " , " i.e: How relevant this peer is to us. How many pieces it has that we don't. " ) ) ;
2009-11-14 13:37:45 +03:00
// Proxy model to support sorting without actually altering the underlying model
2013-07-05 17:29:07 +04:00
m_proxyModel = new PeerListSortModel ( ) ;
2012-02-25 23:02:19 +04:00
m_proxyModel - > setDynamicSortFilter ( true ) ;
m_proxyModel - > setSourceModel ( m_listModel ) ;
2013-11-09 22:01:12 +04:00
m_proxyModel - > setSortCaseSensitivity ( Qt : : CaseInsensitive ) ;
2012-02-25 23:02:19 +04:00
setModel ( m_proxyModel ) ;
2013-06-29 14:28:29 +04:00
//Explicitly set the column visibility. When columns are added/removed
//between versions this prevents some of them being hidden due to
//incorrect restoreState() being used.
2013-10-14 15:57:51 +04:00
for ( unsigned int i = 0 ; i < PeerListDelegate : : IP_HIDDEN ; i + + )
2013-06-29 14:28:29 +04:00
showColumn ( i ) ;
2013-10-14 15:57:51 +04:00
hideColumn ( PeerListDelegate : : IP_HIDDEN ) ;
2013-06-29 14:28:29 +04:00
hideColumn ( PeerListDelegate : : COL_COUNT ) ;
2014-07-05 16:44:13 +04:00
if ( ! Preferences : : instance ( ) - > resolvePeerCountries ( ) )
2013-07-05 17:29:07 +04:00
hideColumn ( PeerListDelegate : : COUNTRY ) ;
2013-06-29 14:28:29 +04:00
//To also migitate the above issue, we have to resize each column when
//its size is 0, because explicitely 'showing' the column isn't enough
//in the above scenario.
2013-10-14 15:57:51 +04:00
for ( unsigned int i = 0 ; i < PeerListDelegate : : IP_HIDDEN ; i + + )
2013-06-29 14:28:29 +04:00
if ( ! columnWidth ( i ) )
resizeColumnToContents ( i ) ;
2009-11-17 14:46:43 +03:00
// Context menu
setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
connect ( this , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( showPeerListMenu ( QPoint ) ) ) ;
2009-11-14 13:37:45 +03:00
// List delegate
2012-02-25 23:02:19 +04:00
m_listDelegate = new PeerListDelegate ( this ) ;
setItemDelegate ( m_listDelegate ) ;
2009-11-14 13:37:45 +03:00
// Enable sorting
setSortingEnabled ( true ) ;
2009-11-14 16:33:55 +03:00
// IP to Hostname resolver
2009-11-15 13:00:07 +03:00
updatePeerHostNameResolutionState ( ) ;
2010-10-24 13:32:28 +04:00
// SIGNAL/SLOT
connect ( header ( ) , SIGNAL ( sectionClicked ( int ) ) , SLOT ( handleSortColumnChanged ( int ) ) ) ;
handleSortColumnChanged ( header ( ) - > sortIndicatorSection ( ) ) ;
2009-11-14 13:37:45 +03:00
}
2012-02-25 23:02:19 +04:00
PeerListWidget : : ~ PeerListWidget ( )
{
2009-11-15 11:40:26 +03:00
saveSettings ( ) ;
2012-02-25 23:02:19 +04:00
delete m_proxyModel ;
delete m_listModel ;
delete m_listDelegate ;
if ( m_resolver )
delete m_resolver ;
2009-11-15 13:00:07 +03:00
}
2012-02-25 23:02:19 +04:00
void PeerListWidget : : updatePeerHostNameResolutionState ( )
{
2014-07-05 16:44:13 +04:00
if ( Preferences : : instance ( ) - > resolvePeerHostNames ( ) ) {
2012-02-25 23:02:19 +04:00
if ( ! m_resolver ) {
2015-04-13 19:02:48 +03:00
m_resolver = new Net : : ReverseResolution ( this ) ;
connect ( m_resolver , SIGNAL ( ipResolved ( QString , QString ) ) , SLOT ( handleResolved ( QString , QString ) ) ) ;
2012-02-25 23:02:19 +04:00
loadPeers ( m_properties - > getCurrentTorrent ( ) , true ) ;
2009-11-15 13:00:07 +03:00
}
} else {
2012-02-25 23:02:19 +04:00
if ( m_resolver )
delete m_resolver ;
2009-11-15 13:00:07 +03:00
}
}
2012-02-25 23:02:19 +04:00
void PeerListWidget : : updatePeerCountryResolutionState ( )
{
2014-07-05 16:44:13 +04:00
if ( Preferences : : instance ( ) - > resolvePeerCountries ( ) ! = m_displayFlags ) {
2012-02-25 23:02:19 +04:00
m_displayFlags = ! m_displayFlags ;
if ( m_displayFlags )
loadPeers ( m_properties - > getCurrentTorrent ( ) ) ;
2009-11-15 15:57:25 +03:00
}
}
2012-02-25 23:02:19 +04:00
void PeerListWidget : : showPeerListMenu ( const QPoint & )
{
2009-11-17 14:46:43 +03:00
QMenu menu ;
2009-11-17 19:02:35 +03:00
bool empty_menu = true ;
2015-04-19 18:17:47 +03:00
BitTorrent : : TorrentHandle * const torrent = m_properties - > getCurrentTorrent ( ) ;
if ( ! torrent ) return ;
2009-11-17 17:19:50 +03:00
QModelIndexList selectedIndexes = selectionModel ( ) - > selectedRows ( ) ;
QStringList selectedPeerIPs ;
2015-02-01 02:11:43 +03:00
QStringList selectedPeerIPPort ;
2012-02-20 21:30:53 +04:00
foreach ( const QModelIndex & index , selectedIndexes ) {
2012-02-25 23:02:19 +04:00
int row = m_proxyModel - > mapToSource ( index ) . row ( ) ;
2013-10-14 15:57:51 +04:00
QString myip = m_listModel - > data ( m_listModel - > index ( row , PeerListDelegate : : IP_HIDDEN ) ) . toString ( ) ;
2015-02-01 02:11:43 +03:00
QString myport = m_listModel - > data ( m_listModel - > index ( row , PeerListDelegate : : PORT ) ) . toString ( ) ;
2010-04-10 19:46:52 +04:00
selectedPeerIPs < < myip ;
2015-02-01 02:11:43 +03:00
selectedPeerIPPort < < myip + " : " + myport ;
2009-11-17 17:19:50 +03:00
}
// Add Peer Action
2009-11-17 14:46:43 +03:00
QAction * addPeerAct = 0 ;
2015-04-19 18:17:47 +03:00
if ( ! torrent - > isQueued ( ) & & ! torrent - > isChecking ( ) ) {
addPeerAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " user-group-new " ) , tr ( " Add a new peer... " ) ) ;
2009-11-17 19:02:35 +03:00
empty_menu = false ;
2009-11-17 17:19:50 +03:00
}
2009-11-17 19:02:35 +03:00
QAction * banAct = 0 ;
2015-02-01 02:11:43 +03:00
QAction * copyPeerAct = 0 ;
2012-02-20 21:30:53 +04:00
if ( ! selectedPeerIPs . isEmpty ( ) ) {
2015-04-19 18:17:47 +03:00
copyPeerAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " edit-copy " ) , tr ( " Copy selected " ) ) ;
2010-07-01 02:28:23 +04:00
menu . addSeparator ( ) ;
2015-04-19 18:17:47 +03:00
banAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " user-group-delete " ) , tr ( " Ban peer permanently " ) ) ;
2009-11-17 19:02:35 +03:00
empty_menu = false ;
2009-11-17 14:46:43 +03:00
}
2012-02-20 21:30:53 +04:00
if ( empty_menu ) return ;
2009-11-17 14:46:43 +03:00
QAction * act = menu . exec ( QCursor : : pos ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( act = = 0 ) return ;
if ( act = = addPeerAct ) {
2015-04-19 18:17:47 +03:00
BitTorrent : : PeerAddress addr = PeerAdditionDlg : : askForPeerAddress ( ) ;
if ( ! addr . ip . isNull ( ) ) {
if ( torrent - > connectPeer ( addr ) )
2009-11-17 14:47:48 +03:00
QMessageBox : : information ( 0 , tr ( " Peer addition " ) , tr ( " The peer was added to this torrent. " ) ) ;
2015-04-19 18:17:47 +03:00
else
2009-11-17 14:47:48 +03:00
QMessageBox : : critical ( 0 , tr ( " Peer addition " ) , tr ( " The peer could not be added to this torrent. " ) ) ;
2015-04-19 18:17:47 +03:00
}
else {
2009-11-17 14:46:43 +03:00
qDebug ( " No peer was added " ) ;
}
return ;
}
2012-02-20 21:30:53 +04:00
if ( act = = banAct ) {
2009-11-17 19:02:35 +03:00
banSelectedPeers ( selectedPeerIPs ) ;
return ;
}
2015-02-01 02:11:43 +03:00
if ( act = = copyPeerAct ) {
2013-09-21 11:59:58 +04:00
# if defined(Q_OS_WIN) || defined(Q_OS_OS2)
2015-02-01 02:11:43 +03:00
QApplication : : clipboard ( ) - > setText ( selectedPeerIPPort . join ( " \r \n " ) ) ;
2010-07-01 02:28:23 +04:00
# else
2015-02-01 02:11:43 +03:00
QApplication : : clipboard ( ) - > setText ( selectedPeerIPPort . join ( " \n " ) ) ;
2010-07-01 02:28:23 +04:00
# endif
}
2009-11-17 19:02:35 +03:00
}
2012-02-25 23:02:19 +04:00
void PeerListWidget : : banSelectedPeers ( const QStringList & peer_ips )
{
2009-11-17 19:02:35 +03:00
// Confirm first
int ret = QMessageBox : : question ( this , tr ( " Are you sure? -- qBittorrent " ) , tr ( " Are you sure you want to ban permanently the selected peers? " ) ,
2010-10-20 21:19:18 +04:00
tr ( " &Yes " ) , tr ( " &No " ) ,
QString ( ) , 0 , 1 ) ;
2012-02-25 23:02:19 +04:00
if ( ret )
return ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & ip , peer_ips ) {
2009-11-17 19:02:35 +03:00
qDebug ( " Banning peer %s... " , ip . toLocal8Bit ( ) . data ( ) ) ;
2015-01-06 04:34:10 +03:00
Logger : : instance ( ) - > addMessage ( tr ( " Manually banning peer %1... " ) . arg ( ip ) ) ;
2015-04-19 18:17:47 +03:00
BitTorrent : : Session : : instance ( ) - > banIP ( ip ) ;
2009-11-17 19:02:35 +03:00
}
// Refresh list
2012-02-25 23:02:19 +04:00
loadPeers ( m_properties - > getCurrentTorrent ( ) ) ;
2009-11-17 14:46:43 +03:00
}
2009-11-15 13:00:07 +03:00
void PeerListWidget : : clear ( ) {
qDebug ( " clearing peer list " ) ;
2012-02-25 23:02:19 +04:00
m_peerItems . clear ( ) ;
2015-04-19 18:17:47 +03:00
m_peerAddresses . clear ( ) ;
2012-02-25 23:02:19 +04:00
m_missingFlags . clear ( ) ;
int nbrows = m_listModel - > rowCount ( ) ;
2012-02-20 21:30:53 +04:00
if ( nbrows > 0 ) {
2009-11-15 13:00:07 +03:00
qDebug ( " Cleared %d peers " , nbrows ) ;
2012-02-25 23:02:19 +04:00
m_listModel - > removeRows ( 0 , nbrows ) ;
2009-11-15 13:00:07 +03:00
}
2009-11-14 13:37:45 +03:00
}
2009-11-15 11:40:26 +03:00
void PeerListWidget : : loadSettings ( ) {
2014-07-05 16:44:13 +04:00
header ( ) - > restoreState ( Preferences : : instance ( ) - > getPeerListState ( ) ) ;
2009-11-15 11:40:26 +03:00
}
void PeerListWidget : : saveSettings ( ) const {
2014-07-05 16:44:13 +04:00
Preferences : : instance ( ) - > setPeerListState ( header ( ) - > saveState ( ) ) ;
2009-11-15 11:40:26 +03:00
}
2015-04-19 18:17:47 +03:00
void PeerListWidget : : loadPeers ( BitTorrent : : TorrentHandle * const torrent , bool force_hostname_resolution ) {
if ( ! torrent ) return ;
QList < BitTorrent : : PeerInfo > peers = torrent - > peers ( ) ;
2012-02-25 23:02:19 +04:00
QSet < QString > old_peers_set = m_peerItems . keys ( ) . toSet ( ) ;
2012-07-14 02:28:23 +04:00
2015-04-19 18:17:47 +03:00
foreach ( const BitTorrent : : PeerInfo & peer , peers ) {
BitTorrent : : PeerAddress addr = peer . address ( ) ;
if ( addr . ip . isNull ( ) ) continue ;
QString peer_ip = addr . ip . toString ( ) ;
2012-02-25 23:02:19 +04:00
if ( m_peerItems . contains ( peer_ip ) ) {
2009-11-14 13:37:45 +03:00
// Update existing peer
2015-04-19 18:17:47 +03:00
updatePeer ( peer_ip , torrent , peer ) ;
2009-11-14 13:37:45 +03:00
old_peers_set . remove ( peer_ip ) ;
2012-02-25 23:02:19 +04:00
if ( force_hostname_resolution & & m_resolver ) {
2013-03-28 23:47:45 +04:00
m_resolver - > resolve ( peer_ip ) ;
2009-11-15 15:57:25 +03:00
}
2009-11-14 13:37:45 +03:00
} else {
// Add new peer
2015-04-19 18:17:47 +03:00
m_peerItems [ peer_ip ] = addPeer ( peer_ip , torrent , peer ) ;
m_peerAddresses [ peer_ip ] = addr ;
2013-03-28 23:47:45 +04:00
// Resolve peer host name is asked
if ( m_resolver )
m_resolver - > resolve ( peer_ip ) ;
2009-11-14 13:37:45 +03:00
}
}
// Delete peers that are gone
QSetIterator < QString > it ( old_peers_set ) ;
while ( it . hasNext ( ) ) {
2012-02-25 23:02:19 +04:00
const QString & ip = it . next ( ) ;
m_missingFlags . remove ( ip ) ;
2015-04-19 18:17:47 +03:00
m_peerAddresses . remove ( ip ) ;
2012-02-25 23:02:19 +04:00
QStandardItem * item = m_peerItems . take ( ip ) ;
m_listModel - > removeRow ( item - > row ( ) ) ;
2009-11-14 13:37:45 +03:00
}
}
2015-04-19 18:17:47 +03:00
QStandardItem * PeerListWidget : : addPeer ( const QString & ip , BitTorrent : : TorrentHandle * const torrent , const BitTorrent : : PeerInfo & peer ) {
2012-02-25 23:02:19 +04:00
int row = m_listModel - > rowCount ( ) ;
2009-11-14 13:37:45 +03:00
// Adding Peer to peer list
2012-02-25 23:02:19 +04:00
m_listModel - > insertRow ( row ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : IP ) , ip ) ;
2013-11-14 17:04:36 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : IP ) , ip , Qt : : ToolTipRole ) ;
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : PORT ) , peer . address ( ) . port ) ;
2013-10-14 15:57:51 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : IP_HIDDEN ) , ip ) ;
2012-02-25 23:02:19 +04:00
if ( m_displayFlags ) {
2015-04-19 18:17:47 +03:00
const QIcon ico = GeoIPManager : : CountryISOCodeToIcon ( peer . country ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! ico . isNull ( ) ) {
2013-07-05 17:29:07 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : COUNTRY ) , ico , Qt : : DecorationRole ) ;
2015-04-19 18:17:47 +03:00
const QString country_name = GeoIPManager : : CountryISOCodeToName ( peer . country ( ) ) ;
2013-07-05 17:29:07 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : COUNTRY ) , country_name , Qt : : ToolTipRole ) ;
2009-11-15 15:57:25 +03:00
} else {
2012-02-25 23:02:19 +04:00
m_missingFlags . insert ( ip ) ;
2009-11-15 15:57:25 +03:00
}
}
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : CONNECTION ) , peer . connectionType ( ) ) ;
2013-11-21 19:37:16 +04:00
QString flags , tooltip ;
getFlags ( peer , flags , tooltip ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : FLAGS ) , flags ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : FLAGS ) , tooltip , Qt : : ToolTipRole ) ;
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : CLIENT ) , peer . client ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : PROGRESS ) , peer . progress ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : DOWN_SPEED ) , peer . payloadDownSpeed ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : UP_SPEED ) , peer . payloadUpSpeed ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : TOT_DOWN ) , peer . totalDownload ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : TOT_UP ) , peer . totalUpload ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : RELEVANCE ) , getPeerRelevance ( torrent - > pieces ( ) , peer . pieces ( ) ) ) ;
2012-02-25 23:02:19 +04:00
return m_listModel - > item ( row , PeerListDelegate : : IP ) ;
2009-11-14 13:37:45 +03:00
}
2015-04-19 18:17:47 +03:00
void PeerListWidget : : updatePeer ( const QString & ip , BitTorrent : : TorrentHandle * const torrent , const BitTorrent : : PeerInfo & peer ) {
2012-02-25 23:02:19 +04:00
QStandardItem * item = m_peerItems . value ( ip ) ;
2009-11-14 13:37:45 +03:00
int row = item - > row ( ) ;
2012-02-25 23:02:19 +04:00
if ( m_displayFlags ) {
2015-04-19 18:17:47 +03:00
const QIcon ico = GeoIPManager : : CountryISOCodeToIcon ( peer . country ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! ico . isNull ( ) ) {
2013-07-05 17:29:07 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : COUNTRY ) , ico , Qt : : DecorationRole ) ;
2015-04-19 18:17:47 +03:00
const QString country_name = GeoIPManager : : CountryISOCodeToName ( peer . country ( ) ) ;
2013-07-05 17:29:07 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : COUNTRY ) , country_name , Qt : : ToolTipRole ) ;
2012-02-25 23:02:19 +04:00
m_missingFlags . remove ( ip ) ;
2009-11-15 15:57:25 +03:00
}
}
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : CONNECTION ) , peer . connectionType ( ) ) ;
2013-11-21 19:37:16 +04:00
QString flags , tooltip ;
getFlags ( peer , flags , tooltip ) ;
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : PORT ) , peer . address ( ) . port ) ;
2013-11-21 19:37:16 +04:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : FLAGS ) , flags ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : FLAGS ) , tooltip , Qt : : ToolTipRole ) ;
2015-04-19 18:17:47 +03:00
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : CLIENT ) , peer . client ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : PROGRESS ) , peer . progress ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : DOWN_SPEED ) , peer . payloadDownSpeed ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : UP_SPEED ) , peer . payloadUpSpeed ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : TOT_DOWN ) , peer . totalDownload ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : TOT_UP ) , peer . totalUpload ( ) ) ;
m_listModel - > setData ( m_listModel - > index ( row , PeerListDelegate : : RELEVANCE ) , getPeerRelevance ( torrent - > pieces ( ) , peer . pieces ( ) ) ) ;
2009-11-14 13:37:45 +03:00
}
2009-11-14 16:33:55 +03:00
2011-01-22 21:55:54 +03:00
void PeerListWidget : : handleResolved ( const QString & ip , const QString & hostname ) {
2012-02-25 23:02:19 +04:00
QStandardItem * item = m_peerItems . value ( ip , 0 ) ;
2012-02-20 21:30:53 +04:00
if ( item ) {
2010-10-20 21:19:18 +04:00
qDebug ( " Resolved %s -> %s " , qPrintable ( ip ) , qPrintable ( hostname ) ) ;
2011-01-22 21:55:54 +03:00
item - > setData ( hostname , Qt : : DisplayRole ) ;
2009-11-14 16:33:55 +03:00
}
}
2010-10-24 13:32:28 +04:00
void PeerListWidget : : handleSortColumnChanged ( int col )
{
2013-07-05 17:29:07 +04:00
if ( col = = PeerListDelegate : : COUNTRY ) {
2010-10-24 13:32:28 +04:00
qDebug ( " Sorting by decoration " ) ;
2012-02-25 23:02:19 +04:00
m_proxyModel - > setSortRole ( Qt : : ToolTipRole ) ;
2010-10-24 13:32:28 +04:00
} else {
2012-02-25 23:02:19 +04:00
m_proxyModel - > setSortRole ( Qt : : DisplayRole ) ;
2010-10-24 13:32:28 +04:00
}
}
2011-04-17 18:42:38 +04:00
2015-04-19 18:17:47 +03:00
void PeerListWidget : : getFlags ( const BitTorrent : : PeerInfo & peer , QString & flags , QString & tooltip )
2013-11-21 19:37:16 +04:00
{
2015-04-19 18:17:47 +03:00
if ( peer . isInteresting ( ) ) {
2013-04-22 01:12:14 +04:00
//d = Your client wants to download, but peer doesn't want to send (interested and choked)
2015-04-19 18:17:47 +03:00
if ( peer . isRemoteChocked ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " d " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " interested(local) and choked(peer) " ) ;
tooltip + = " , " ;
}
else {
//D = Currently downloading (interested and not choked)
2013-04-22 01:12:14 +04:00
flags + = " D " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " interested(local) and unchoked(peer) " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
}
2015-04-19 18:17:47 +03:00
if ( peer . isRemoteInterested ( ) ) {
2013-04-22 01:12:14 +04:00
//u = Peer wants your client to upload, but your client doesn't want to (interested and choked)
2015-04-19 18:17:47 +03:00
if ( peer . isChocked ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " u " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " interested(peer) and choked(local) " ) ;
tooltip + = " , " ;
}
else {
//U = Currently uploading (interested and not choked)
2013-04-22 01:12:14 +04:00
flags + = " U " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " interested(peer) and unchoked(local) " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
}
//O = Optimistic unchoke
2015-04-19 18:17:47 +03:00
if ( peer . optimisticUnchoke ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " O " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " optimistic unchoke " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//S = Peer is snubbed
2015-04-19 18:17:47 +03:00
if ( peer . isSnubbed ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " S " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " peer snubbed " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//I = Peer is an incoming connection
2015-04-19 18:17:47 +03:00
if ( ! peer . isLocalConnection ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " I " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " incoming connection " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//K = Peer is unchoking your client, but your client is not interested
2015-04-19 18:17:47 +03:00
if ( ! peer . isRemoteChocked ( ) & & ! peer . isInteresting ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " K " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " not interested(local) and unchoked(peer) " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//? = Your client unchoked the peer but the peer is not interested
2015-04-19 18:17:47 +03:00
if ( ! peer . isChocked ( ) & & ! peer . isRemoteInterested ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " ? " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " not interested(peer) and unchoked(local) " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//X = Peer was included in peerlists obtained through Peer Exchange (PEX)
2015-04-19 18:17:47 +03:00
if ( peer . fromPeX ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " X " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " peer from PEX " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//H = Peer was obtained through DHT
2015-04-19 18:17:47 +03:00
if ( peer . fromDHT ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " H " ;
2013-11-24 18:21:31 +04:00
tooltip + = tr ( " peer from DHT " ) ;
2013-11-21 19:37:16 +04:00
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
2013-07-28 19:19:06 +04:00
//E = Peer is using Protocol Encryption (all traffic)
2015-04-19 18:17:47 +03:00
if ( peer . isRC4Encrypted ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " E " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " encrypted traffic " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
2013-07-28 19:19:06 +04:00
//e = Peer is using Protocol Encryption (handshake)
2015-04-19 18:17:47 +03:00
if ( peer . isPlaintextEncrypted ( ) ) {
2013-07-28 19:19:06 +04:00
flags + = " e " ;
2013-11-21 19:37:16 +04:00
tooltip + = tr ( " encrypted handshake " ) ;
tooltip + = " , " ;
}
2013-07-28 19:19:06 +04:00
2013-04-22 01:12:14 +04:00
//P = Peer is using uTorrent uTP
2015-04-19 18:17:47 +03:00
if ( peer . useUTPSocket ( ) ) {
2013-04-22 01:12:14 +04:00
flags + = " P " ;
2013-11-21 19:37:16 +04:00
tooltip + = QString : : fromUtf8 ( " μTP " ) ;
tooltip + = " , " ;
}
2013-04-22 01:12:14 +04:00
//L = Peer is local
2015-04-19 18:17:47 +03:00
if ( peer . fromLSD ( ) ) {
2013-11-21 19:37:16 +04:00
flags + = " L " ;
tooltip + = tr ( " peer from LSD " ) ;
}
2013-04-22 01:12:14 +04:00
2013-11-21 19:37:16 +04:00
flags = flags . trimmed ( ) ;
tooltip = tooltip . trimmed ( ) ;
if ( tooltip . endsWith ( ' , ' , Qt : : CaseInsensitive ) )
tooltip . chop ( 1 ) ;
2013-04-22 01:12:14 +04:00
}
2014-12-06 20:07:26 +03:00
2015-04-19 18:17:47 +03:00
qreal PeerListWidget : : getPeerRelevance ( const QBitArray & allPieces , const QBitArray & peerPieces )
2014-12-06 20:07:26 +03:00
{
int localMissing = 0 ;
int remoteHaves = 0 ;
2015-04-19 18:17:47 +03:00
for ( int i = 0 ; i < allPieces . size ( ) ; + + i ) {
if ( ! allPieces [ i ] ) {
2014-12-06 20:07:26 +03:00
+ + localMissing ;
2015-04-19 18:17:47 +03:00
if ( peerPieces [ i ] )
2014-12-06 20:07:26 +03:00
+ + remoteHaves ;
}
}
if ( localMissing = = 0 )
2014-12-07 02:39:49 +03:00
return 0.0 ;
2014-12-06 20:07:26 +03:00
2015-04-19 18:17:47 +03:00
return static_cast < qreal > ( remoteHaves ) / localMissing ;
2014-12-06 20:07:26 +03:00
}