2 * This file Copyright (C) Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: torrent-model.cc 11092 2010-08-01 20:36:13Z charles $
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 emit
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( ) );
145 int updatedCount
= 0;
147 if( tr_bencIsList( torrents
) )
151 while(( child
= tr_bencListChild( torrents
, i
++ )))
154 if( tr_bencDictFindInt( child
, "id", &id
) )
158 Torrent
* tor
= getTorrentFromId( id
);
161 tor
= new Torrent( myPrefs
, id
);
162 tor
->update( child
);
163 if( !tor
->hasMetadata() )
164 tor
->setMagnet( true );
165 newTorrents
.append( tor
);
166 connect( tor
, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int)));
170 tor
->update( child
);
172 if( tor
->isMagnet() && tor
->hasMetadata() )
174 addIds
.insert( tor
->id() );
175 tor
->setMagnet( false );
182 if( !newTorrents
.isEmpty( ) )
184 const int oldCount( rowCount( ) );
185 const int newCount( oldCount
+ newTorrents
.size( ) );
188 beginInsertRows( QModelIndex(), oldCount
, newCount
- 1 );
190 foreach( Torrent
* tor
, newTorrents
) {
192 addIds
.insert( tor
->id( ) );
197 if( !addIds
.isEmpty() )
198 emit
torrentsAdded( addIds
);
202 QSet
<int> removedIds( oldIds
);
203 removedIds
-= newIds
;
204 foreach( int id
, removedIds
)
210 TorrentModel :: removeTorrent( int id
)
212 const int row
= myIdToRow
.value( id
, -1 );
215 Torrent
* tor
= myIdToTorrent
.value( id
, 0 );
217 beginRemoveRows( QModelIndex(), row
, row
);
218 // make the myIdToRow map consistent with list view/model
219 for( QMap
<int,int>::iterator i
= myIdToRow
.begin(); i
!= myIdToRow
.end(); ++i
)
220 if( i
.value() > row
)
222 myIdToRow
.remove( id
);
223 myIdToTorrent
.remove( id
);
224 myTorrents
.remove( myTorrents
.indexOf( tor
) );
232 TorrentModel :: getUploadSpeed( ) const
235 foreach( const Torrent
* tor
, myTorrents
)
236 up
+= tor
->uploadSpeed( );
241 TorrentModel :: getDownloadSpeed( ) const
244 foreach( const Torrent
* tor
, myTorrents
)
245 down
+= tor
->downloadSpeed( );
250 TorrentModel :: getIds( ) const
253 foreach( const Torrent
* tor
, myTorrents
)
254 ids
.insert( tor
->id( ) );
259 TorrentModel :: hasTorrent( const QString
& hashString
) const
261 foreach( const Torrent
* tor
, myTorrents
)
262 if( tor
->hashString( ) == hashString
)