From 0bc8314809b5464775224ff655d5d8c4322c0b27 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Wed, 16 Jun 2010 03:02:17 +0000 Subject: [PATCH] (qt) #3277: Crash after getting magnet torrent metadata --- qt/details.cc | 10 ++++++++++ qt/torrent-delegate-min.cc | 8 +++++--- qt/torrent-delegate.cc | 12 ++++++++++-- qt/torrent-model.cc | 15 ++++++++++++--- qt/torrent.h | 4 ++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/qt/details.cc b/qt/details.cc index b003b2f0b..f64056ea8 100644 --- a/qt/details.cc +++ b/qt/details.cc @@ -207,7 +207,17 @@ void Details :: onTimer( ) { if( !myIds.empty( ) ) + { + QSet infos; + foreach( int id, myIds ) { + const Torrent * tor = myModel.getTorrentFromId( id ); + if( tor->isMagnet() ) + infos.insert( tor->id() ); + } + if( !infos.isEmpty() ) + mySession.initTorrents( infos ); mySession.refreshExtraStats( myIds ); + } } void diff --git a/qt/torrent-delegate-min.cc b/qt/torrent-delegate-min.cc index b9efcd11d..b3f262d13 100644 --- a/qt/torrent-delegate-min.cc +++ b/qt/torrent-delegate-min.cc @@ -52,7 +52,8 @@ TorrentDelegateMin :: sizeHint( const QStyleOptionViewItem& option, const Torren QFont nameFont( option.font ); const QFontMetrics nameFM( nameFont ); - const QString nameStr( tor.name( ) ); + const bool isMagnet( !tor.hasMetadata( ) ); + const QString nameStr = (isMagnet ? progressString( tor ) : tor.name( ) ); const QSize nameSize( nameFM.size( 0, nameStr ) ); QFont statusFont( option.font ); @@ -76,7 +77,8 @@ TorrentDelegateMin :: drawTorrent( QPainter * painter, const QStyleOptionViewIte QFont nameFont( option.font ); const QFontMetrics nameFM( nameFont ); - const QString nameStr( tor.name( ) ); + const bool isMagnet( !tor.hasMetadata( ) ); + const QString nameStr = (isMagnet ? progressString( tor ) : tor.name( ) ); const QSize nameSize( nameFM.size( 0, nameStr ) ); QFont statusFont( option.font ); @@ -153,7 +155,7 @@ TorrentDelegateMin :: drawTorrent( QPainter * painter, const QStyleOptionViewIte myProgressBarStyle->palette = option.palette; myProgressBarStyle->palette.setCurrentColorGroup( cg ); myProgressBarStyle->state = progressBarState; - myProgressBarStyle->progress = int(myProgressBarStyle->minimum + ((tor.percentDone() * (myProgressBarStyle->maximum - myProgressBarStyle->minimum)))); + myProgressBarStyle->progress = int(myProgressBarStyle->minimum + (((isMagnet ? tor.metadataPercentDone() : tor.percentDone()) * (myProgressBarStyle->maximum - myProgressBarStyle->minimum)))); style->drawControl( QStyle::CE_ProgressBar, myProgressBarStyle, painter ); painter->restore( ); diff --git a/qt/torrent-delegate.cc b/qt/torrent-delegate.cc index 9a2b3425a..b0dcc63f2 100644 --- a/qt/torrent-delegate.cc +++ b/qt/torrent-delegate.cc @@ -62,6 +62,7 @@ TorrentDelegate :: margin( const QStyle& style ) const QString TorrentDelegate :: progressString( const Torrent& tor ) const { + const bool isMagnet( !tor.hasMetadata( ) ); const bool isDone( tor.isDone( ) ); const bool isSeed( tor.isSeed( ) ); const uint64_t haveTotal( tor.haveTotal( ) ); @@ -69,7 +70,13 @@ TorrentDelegate :: progressString( const Torrent& tor ) const double seedRatio; const bool hasSeedRatio( tor.getSeedRatio( seedRatio ) ); - if( !isDone ) // downloading + if( isMagnet ) // magnet link with no metadata + { + /* %1 is the percentage of torrent metadata downloaded */ + str = tr( "Magnetized transfer - retrieving metadata ( %1% )" ) + .arg( tor.metadataPercentDone() ); + } + else if( !isDone ) // downloading { /* %1 is how much we've got, %2 is how much we'll have when done, @@ -386,12 +393,13 @@ TorrentDelegate :: drawTorrent( QPainter * painter, const QStyleOptionViewItem& painter->drawText( statusArea, 0, statusFM.elidedText( statusStr, Qt::ElideRight, statusArea.width( ) ) ); painter->setFont( progressFont ); painter->drawText( progArea, 0, progressFM.elidedText( progressStr, Qt::ElideRight, progArea.width( ) ) ); + const bool isMagnet( !tor.hasMetadata( ) ); myProgressBarStyle->rect = barArea; myProgressBarStyle->direction = option.direction; myProgressBarStyle->palette = option.palette; myProgressBarStyle->palette.setCurrentColorGroup( cg ); myProgressBarStyle->state = progressBarState; - myProgressBarStyle->progress = int(myProgressBarStyle->minimum + ((tor.percentDone() * (myProgressBarStyle->maximum - myProgressBarStyle->minimum)))); + myProgressBarStyle->progress = int(myProgressBarStyle->minimum + (((isMagnet ? tor.metadataPercentDone() : tor.percentDone()) * (myProgressBarStyle->maximum - myProgressBarStyle->minimum)))); style->drawControl( QStyle::CE_ProgressBar, myProgressBarStyle, painter ); painter->restore( ); diff --git a/qt/torrent-model.cc b/qt/torrent-model.cc index 95d00d606..31b893ed7 100644 --- a/qt/torrent-model.cc +++ b/qt/torrent-model.cc @@ -140,6 +140,7 @@ TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList ) { QList newTorrents; QSet oldIds( getIds( ) ); + QSet addIds; QSet newIds; int updatedCount = 0; @@ -159,6 +160,8 @@ TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList ) { tor = new Torrent( myPrefs, id ); tor->update( child ); + if( !tor->hasMetadata() ) + tor->setMagnet( true ); newTorrents.append( tor ); connect( tor, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int))); } @@ -166,6 +169,11 @@ TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList ) { tor->update( child ); ++updatedCount; + if( tor->isMagnet() && tor->hasMetadata() ) + { + addIds.insert( tor->id() ); + tor->setMagnet( false ); + } } } } @@ -181,13 +189,14 @@ TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList ) foreach( Torrent * tor, newTorrents ) { addTorrent( tor ); - ids.insert( tor->id( ) ); + addIds.insert( tor->id( ) ); } endInsertRows( ); - - emit torrentsAdded( ids ); } + if( !addIds.isEmpty() ) + emit torrentsAdded( addIds ); + if( isCompleteList ) { QSet removedIds( oldIds ); diff --git a/qt/torrent.h b/qt/torrent.h index e51f754d9..efda07e1b 100644 --- a/qt/torrent.h +++ b/qt/torrent.h @@ -198,6 +198,8 @@ class Torrent: public QObject static Property myProperties[]; + bool magnetTorrent; + public: typedef QList KeyList; static const KeyList& getInfoKeys( ); @@ -251,6 +253,7 @@ class Torrent: public QObject uint64_t leftUntilDone( ) const { return getSize( LEFT_UNTIL_DONE ); } uint64_t pieceSize( ) const { return getSize( PIECE_SIZE ); } bool hasMetadata( ) const { return getDouble( METADATA_PERCENT_DONE ) >= 1.0; } + bool isMagnet( ) const { return magnetTorrent; } int pieceCount( ) const { return getInt( PIECE_COUNT ); } double ratio( ) const { return getDouble( RATIO ); } double percentComplete( ) const { return haveTotal() / (double)totalSize(); } @@ -305,6 +308,7 @@ class Torrent: public QObject public: void update( tr_benc * dict ); + void setMagnet( bool magnet ) { magnetTorrent = magnet; } private: const char * getMimeTypeString( ) const;