2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
4
* This file is licensed by the GPL version 2. Works owned by the
5
* Transmission project are granted a special exemption to clause 2(b)
6
* so that the bulk of its code can remain under the MIT license.
7
* This exemption does not extend to derived works not owned by
8
* the Transmission project.
16
#include <libtransmission/transmission.h>
17
#include <libtransmission/bencode.h>
19
#include "torrent-delegate.h"
20
#include "torrent-model.h"
23
TorrentModel :: clear( )
26
myIdToTorrent.clear( );
27
foreach( Torrent * tor, myTorrents ) delete tor;
33
TorrentModel :: rowCount( const QModelIndex& parent ) const
37
return myTorrents.size( );
41
TorrentModel :: data( const QModelIndex& index, int role ) const
44
const int row = index.row( );
45
if( row<0 || row>=myTorrents.size() )
48
const Torrent* t = myTorrents.at( row );
53
var = QString( t->name() );
56
case Qt::DecorationRole:
57
var = t->getMimeTypeIcon( );
61
var = qVariantFromValue( t );
65
//std::cerr << "Unhandled role: " << role << std::endl;
77
TorrentModel :: addTorrent( Torrent * t )
79
myIdToTorrent.insert( t->id( ), t );
80
myIdToRow.insert( t->id( ), myTorrents.size( ) );
81
myTorrents.append( t );
84
TorrentModel :: TorrentModel( Prefs& prefs ):
89
TorrentModel :: ~TorrentModel( )
99
TorrentModel :: getTorrentFromId( int id )
101
id_to_torrent_t::iterator it( myIdToTorrent.find( id ) );
102
return it == myIdToTorrent.end() ? 0 : it.value( );
106
TorrentModel :: getTorrentFromId( int id ) const
108
id_to_torrent_t::const_iterator it( myIdToTorrent.find( id ) );
109
return it == myIdToTorrent.end() ? 0 : it.value( );
117
TorrentModel :: onTorrentChanged( int torrentId )
119
const int row( myIdToRow.value( torrentId, -1 ) );
121
QModelIndex qmi( index( row, 0 ) );
122
dataChanged( qmi, qmi );
127
TorrentModel :: removeTorrents( tr_benc * torrents )
131
while(( child = tr_bencListChild( torrents, i++ ))) {
133
if( tr_bencGetInt( child, &intVal ) )
134
removeTorrent( intVal );
139
TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList )
141
QList<Torrent*> newTorrents;
142
QSet<int> oldIds( getIds( ) );
144
int updatedCount = 0;
146
if( tr_bencIsList( torrents ) )
150
while(( child = tr_bencListChild( torrents, i++ )))
153
if( tr_bencDictFindInt( child, "id", &id ) )
157
Torrent * tor = getTorrentFromId( id );
160
tor = new Torrent( myPrefs, id );
161
tor->update( child );
162
newTorrents.append( tor );
163
connect( tor, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int)));
167
tor->update( child );
174
if( !newTorrents.isEmpty( ) )
176
const int oldCount( rowCount( ) );
177
const int newCount( oldCount + newTorrents.size( ) );
180
beginInsertRows( QModelIndex(), oldCount, newCount - 1 );
182
foreach( Torrent * tor, newTorrents ) {
184
ids.insert( tor->id( ) );
188
emit torrentsAdded( ids );
193
QSet<int> removedIds( oldIds );
194
removedIds -= newIds;
195
foreach( int id, removedIds )
201
TorrentModel :: removeTorrent( int id )
203
const int row = myIdToRow.value( id, -1 );
206
Torrent * tor = myIdToTorrent.value( id, 0 );
208
beginRemoveRows( QModelIndex(), row, row );
209
myIdToRow.remove( id );
210
myIdToTorrent.remove( id );
211
myTorrents.remove( myTorrents.indexOf( tor ) );
219
TorrentModel :: getUploadSpeed( ) const
222
foreach( const Torrent * tor, myTorrents )
223
up += tor->uploadSpeed( );
228
TorrentModel :: getDownloadSpeed( ) const
231
foreach( const Torrent * tor, myTorrents )
232
down += tor->downloadSpeed( );
237
TorrentModel :: getIds( ) const
240
foreach( const Torrent * tor, myTorrents )
241
ids.insert( tor->id( ) );
246
TorrentModel :: hasTorrent( const QString& hashString ) const
248
foreach( const Torrent * tor, myTorrents )
249
if( tor->hashString( ) == hashString )