Changeset 8328


Ignore:
Timestamp:
May 4, 2009, 2:58:05 AM (12 years ago)
Author:
charles
Message:

(trunk qt) fix bug in inspector's peers tab when the same peer is connected to us for more than one torrent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/details.cc

    r8318 r8328  
    8787
    8888    public:
    89         PeerItem( ) { }
    9089        virtual ~PeerItem( ) { }
    91 
    92     public:
    93         void setStatus( const QString& s ) {
    94             status = s;
    95         }
    96         void setPeer( const Peer& p ) {
     90        PeerItem( const Peer& p ) {
    9791            peer = p;
    9892            int q[4];
     
    10296                collatedAddress = p.address;
    10397        }
     98    public:
     99        void refresh( const Peer& p ) { peer = p; }
     100        void setStatus( const QString& s ) { status = s; }
    104101        virtual bool operator< ( const QTreeWidgetItem & other ) const {
    105102            const PeerItem * i = dynamic_cast<const PeerItem*>(&other);
     
    681678    myTimesCompletedLabel->setText( locale.toString( i ) );
    682679
    683     PeerList peers;
    684     foreach( const Torrent * t, torrents ) peers << t->peers( );
    685680    QMap<QString,QTreeWidgetItem*> peers2;
    686681    QList<QTreeWidgetItem*> newItems;
    687     static const QIcon myEncryptionIcon( ":/icons/encrypted.png" );
    688     static const QIcon myEmptyIcon;
    689     foreach( const Peer& peer, peers )
     682    foreach( const Torrent * t, torrents )
    690683    {
    691         PeerItem * item = (PeerItem*) myPeers.value( peer.address, 0 );
    692         if( item == 0 ) { // new peer has connected
    693             item = new PeerItem;
    694             item->setTextAlignment( COL_UP, Qt::AlignRight );
    695             item->setTextAlignment( COL_DOWN, Qt::AlignRight );
    696             item->setTextAlignment( COL_PERCENT, Qt::AlignRight );
    697             newItems << item;
    698         }
    699 
    700         QString code;
    701         if( peer.isDownloadingFrom )                           { code += 'D'; }
    702         else if( peer.clientIsInterested )                     { code += 'd'; }
    703         if( peer.isUploadingTo )                               { code += 'U'; }
    704         else if( peer.peerIsInterested )                       { code += 'u'; }
    705         if( !peer.clientIsChoked && !peer.clientIsInterested ) { code += 'K'; }
    706         if( !peer.peerIsChoked && !peer.peerIsInterested )     { code += '?'; }
    707         if( peer.isEncrypted )                                 { code += 'E'; }
    708         if( peer.isIncoming )                                  { code += 'I'; }
    709 
    710         item->setPeer( peer );
    711         item->setStatus( code );
    712 
    713         QString codeTip;
    714         foreach( QChar ch, code ) {
    715             QString txt;
    716             switch( ch.toAscii() ) {
    717                 case 'O': txt = tr( "Optimistic unchoke" ); break;
    718                 case 'D': txt = tr( "Downloading from this peer" ); break;
    719                 case 'd': txt = tr( "We would download from this peer if they would let us" ); break;
    720                 case 'U': txt = tr( "Uploading to peer" ); break;
    721                 case 'u': txt = tr( "We would upload to this peer if they asked" ); break;
    722                 case 'K': txt = tr( "Peer has unchoked us, but we're not interested" ); break;
    723                 case '?': txt = tr( "We unchoked this peer, but they're not interested" ); break;
    724                 case 'E': txt = tr( "Encrypted connection" ); break;
    725                 case 'X': txt = tr( "Peer was discovered through Peer Exchange (PEX)" ); break;
    726                 case 'I': txt = tr( "Peer is an incoming connection" ); break;
    727             }
    728             if( !txt.isEmpty( ) )
    729                 codeTip += QString("%1: %2\n").arg(ch).arg(txt);
    730         }
    731         if( !codeTip.isEmpty() )
    732             codeTip.resize( codeTip.size()-1 ); // eat the trailing linefeed
    733 
    734         item->setIcon( COL_LOCK, peer.isEncrypted ? myEncryptionIcon : myEmptyIcon );
    735         item->setToolTip( COL_LOCK, peer.isEncrypted ? tr( "Encrypted connection" ) : "" );
    736         item->setText( COL_UP, peer.rateToPeer.isZero() ? "" : Utils::speedToString( peer.rateToPeer ) );
    737         item->setText( COL_DOWN, peer.rateToClient.isZero() ? "" : Utils::speedToString( peer.rateToClient ) );
    738         item->setText( COL_PERCENT, peer.progress > 0 ? QString( "%1%" ).arg( locale.toString((int)(peer.progress*100.0))) : "" );
    739         item->setText( COL_STATUS, code );
    740         item->setToolTip( COL_STATUS, codeTip );
    741         item->setText( COL_ADDRESS, peer.address );
    742         item->setText( COL_CLIENT, peer.clientName );
    743         peers2.insert( peer.address, item );
     684        const QString idStr( QString::number( t->id( ) ) );
     685        PeerList peers = t->peers( );
     686
     687        foreach( const Peer& peer, peers )
     688        {
     689            const QString key = idStr + ":" + peer.address;
     690            PeerItem * item = (PeerItem*) myPeers.value( key, 0 );
     691
     692            if( item == 0 ) // new peer has connected
     693            {
     694                static const QIcon myEncryptionIcon( ":/icons/encrypted.png" );
     695                static const QIcon myEmptyIcon;
     696                item = new PeerItem( peer );
     697                item->setTextAlignment( COL_UP, Qt::AlignRight );
     698                item->setTextAlignment( COL_DOWN, Qt::AlignRight );
     699                item->setTextAlignment( COL_PERCENT, Qt::AlignRight );
     700                item->setIcon( COL_LOCK, peer.isEncrypted ? myEncryptionIcon : myEmptyIcon );
     701                item->setToolTip( COL_LOCK, peer.isEncrypted ? tr( "Encrypted connection" ) : "" );
     702                item->setText( COL_ADDRESS, peer.address );
     703                item->setText( COL_CLIENT, peer.clientName );
     704                newItems << item;
     705            }
     706
     707            QString code;
     708            if( peer.isDownloadingFrom )                           { code += 'D'; }
     709            else if( peer.clientIsInterested )                     { code += 'd'; }
     710            if( peer.isUploadingTo )                               { code += 'U'; }
     711            else if( peer.peerIsInterested )                       { code += 'u'; }
     712            if( !peer.clientIsChoked && !peer.clientIsInterested ) { code += 'K'; }
     713            if( !peer.peerIsChoked && !peer.peerIsInterested )     { code += '?'; }
     714            if( peer.isEncrypted )                                 { code += 'E'; }
     715            if( peer.isIncoming )                                  { code += 'I'; }
     716            item->setStatus( code );
     717            item->refresh( peer );
     718
     719            QString codeTip;
     720            foreach( QChar ch, code ) {
     721                QString txt;
     722                switch( ch.toAscii() ) {
     723                    case 'O': txt = tr( "Optimistic unchoke" ); break;
     724                    case 'D': txt = tr( "Downloading from this peer" ); break;
     725                    case 'd': txt = tr( "We would download from this peer if they would let us" ); break;
     726                    case 'U': txt = tr( "Uploading to peer" ); break;
     727                    case 'u': txt = tr( "We would upload to this peer if they asked" ); break;
     728                    case 'K': txt = tr( "Peer has unchoked us, but we're not interested" ); break;
     729                    case '?': txt = tr( "We unchoked this peer, but they're not interested" ); break;
     730                    case 'E': txt = tr( "Encrypted connection" ); break;
     731                    case 'X': txt = tr( "Peer was discovered through Peer Exchange (PEX)" ); break;
     732                    case 'I': txt = tr( "Peer is an incoming connection" ); break;
     733                }
     734                if( !txt.isEmpty( ) )
     735                    codeTip += QString("%1: %2\n").arg(ch).arg(txt);
     736            }
     737
     738            if( !codeTip.isEmpty() )
     739                codeTip.resize( codeTip.size()-1 ); // eat the trailing linefeed
     740
     741            item->setText( COL_UP, peer.rateToPeer.isZero() ? "" : Utils::speedToString( peer.rateToPeer ) );
     742            item->setText( COL_DOWN, peer.rateToClient.isZero() ? "" : Utils::speedToString( peer.rateToClient ) );
     743            item->setText( COL_PERCENT, peer.progress > 0 ? QString( "%1%" ).arg( locale.toString((int)(peer.progress*100.0))) : "" );
     744            item->setText( COL_STATUS, code );
     745            item->setToolTip( COL_STATUS, codeTip );
     746
     747            peers2.insert( key, item );
     748        }
    744749    }
    745750    myPeerTree->addTopLevelItems( newItems );
Note: See TracChangeset for help on using the changeset viewer.