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 14150 2013-07-27 21:58:14Z jordan $
16 #include <libtransmission/transmission.h>
17 #include <libtransmission/variant.h>
19 #include "torrent-delegate.h"
20 #include "torrent-model.h"
23 TorrentModel :: clear( )
28 myIdToTorrent
.clear( );
29 foreach( Torrent
* tor
, myTorrents
) delete tor
;
36 TorrentModel :: rowCount( const QModelIndex
& parent
) const
40 return myTorrents
.size( );
44 TorrentModel :: data (const QModelIndex
& index
, int role
) const
48 const Torrent
* t
= myTorrents
.value (index
.row(), 0);
54 var
.setValue (t
->name());
57 case Qt::DecorationRole
:
58 var
.setValue (t
->getMimeTypeIcon());
62 var
= qVariantFromValue(t
);
66 //std::cerr << "Unhandled role: " << role << std::endl;
79 TorrentModel :: addTorrent( Torrent
* t
)
81 myIdToTorrent
.insert( t
->id( ), t
);
82 myIdToRow
.insert( t
->id( ), myTorrents
.size( ) );
83 myTorrents
.append( t
);
86 TorrentModel :: TorrentModel( Prefs
& prefs
):
91 TorrentModel :: ~TorrentModel( )
101 TorrentModel :: getTorrentFromId( int id
)
103 id_to_torrent_t::iterator
it( myIdToTorrent
.find( id
) );
104 return it
== myIdToTorrent
.end() ? 0 : it
.value( );
108 TorrentModel :: getTorrentFromId( int id
) const
110 id_to_torrent_t::const_iterator
it( myIdToTorrent
.find( id
) );
111 return it
== myIdToTorrent
.end() ? 0 : it
.value( );
119 TorrentModel :: onTorrentChanged( int torrentId
)
121 const int row( myIdToRow
.value( torrentId
, -1 ) );
123 QModelIndex
qmi( index( row
, 0 ) );
124 emit
dataChanged( qmi
, qmi
);
129 TorrentModel :: removeTorrents( tr_variant
* torrents
)
133 while(( child
= tr_variantListChild( torrents
, i
++ ))) {
135 if( tr_variantGetInt( child
, &intVal
) )
136 removeTorrent( intVal
);
141 TorrentModel :: updateTorrents( tr_variant
* torrents
, bool isCompleteList
)
143 QList
<Torrent
*> newTorrents
;
147 int updatedCount
= 0;
149 if ( isCompleteList
)
152 if( tr_variantIsList( torrents
) )
156 while(( child
= tr_variantListChild( torrents
, i
++ )))
159 if( tr_variantDictFindInt( child
, TR_KEY_id
, &id
) )
163 Torrent
* tor
= getTorrentFromId( id
);
166 tor
= new Torrent( myPrefs
, id
);
167 tor
->update( child
);
168 if( !tor
->hasMetadata() )
169 tor
->setMagnet( true );
170 newTorrents
.append( tor
);
171 connect( tor
, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int)));
175 tor
->update( child
);
177 if( tor
->isMagnet() && tor
->hasMetadata() )
179 addIds
.insert( tor
->id() );
180 tor
->setMagnet( false );
187 if( !newTorrents
.isEmpty( ) )
189 const int oldCount( rowCount( ) );
190 const int newCount( oldCount
+ newTorrents
.size( ) );
193 beginInsertRows( QModelIndex(), oldCount
, newCount
- 1 );
195 foreach( Torrent
* tor
, newTorrents
) {
197 addIds
.insert( tor
->id( ) );
202 if( !addIds
.isEmpty() )
203 emit
torrentsAdded( addIds
);
207 QSet
<int> removedIds( oldIds
);
208 removedIds
-= newIds
;
209 foreach( int id
, removedIds
)
215 TorrentModel :: removeTorrent( int id
)
217 const int row
= myIdToRow
.value( id
, -1 );
220 Torrent
* tor
= myIdToTorrent
.value( id
, 0 );
222 beginRemoveRows( QModelIndex(), row
, row
);
223 // make the myIdToRow map consistent with list view/model
224 for( QMap
<int,int>::iterator i
= myIdToRow
.begin(); i
!= myIdToRow
.end(); ++i
)
225 if( i
.value() > row
)
227 myIdToRow
.remove( id
);
228 myIdToTorrent
.remove( id
);
229 myTorrents
.remove( myTorrents
.indexOf( tor
) );
237 TorrentModel :: getTransferSpeed (Speed
& uploadSpeed
,
238 size_t & uploadPeerCount
,
239 Speed
& downloadSpeed
,
240 size_t & downloadPeerCount
)
242 Speed upSpeed
, downSpeed
;
243 size_t upCount
=0, downCount
=0;
245 foreach (const Torrent
* const tor
, myTorrents
)
247 upSpeed
+= tor
->uploadSpeed ();
248 upCount
+= tor
->peersWeAreUploadingTo ();
249 downSpeed
+= tor
->downloadSpeed ();
250 downCount
+= tor
->webseedsWeAreDownloadingFrom();
251 downCount
+= tor
->peersWeAreDownloadingFrom();
254 uploadSpeed
= upSpeed
;
255 uploadPeerCount
= upCount
;
256 downloadSpeed
= downSpeed
;
257 downloadPeerCount
= downCount
;
261 TorrentModel :: getIds () const
265 ids
.reserve (myTorrents
.size());
266 foreach (const Torrent
* tor
, myTorrents
)
267 ids
.insert (tor
->id());
273 TorrentModel :: hasTorrent( const QString
& hashString
) const
275 foreach( const Torrent
* tor
, myTorrents
)
276 if( tor
->hashString( ) == hashString
)