Transmission: update to 2.82
[tomato.git] / release / src / router / transmission / qt / torrent-model.cc
blob832aaf4d7b6400706f2d5597584a23e09364812b
1 /*
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 $
13 #include <cassert>
14 #include <iostream>
16 #include <libtransmission/transmission.h>
17 #include <libtransmission/variant.h>
19 #include "torrent-delegate.h"
20 #include "torrent-model.h"
22 void
23 TorrentModel :: clear( )
25 beginResetModel ();
27 myIdToRow.clear( );
28 myIdToTorrent.clear( );
29 foreach( Torrent * tor, myTorrents ) delete tor;
30 myTorrents.clear( );
32 endResetModel ();
35 int
36 TorrentModel :: rowCount( const QModelIndex& parent ) const
38 Q_UNUSED( parent );
40 return myTorrents.size( );
43 QVariant
44 TorrentModel :: data (const QModelIndex& index, int role) const
46 QVariant var;
48 const Torrent * t = myTorrents.value (index.row(), 0);
49 if (t != 0)
51 switch (role)
53 case Qt::DisplayRole:
54 var.setValue (t->name());
55 break;
57 case Qt::DecorationRole:
58 var.setValue (t->getMimeTypeIcon());
59 break;
61 case TorrentRole:
62 var = qVariantFromValue(t);
63 break;
65 default:
66 //std::cerr << "Unhandled role: " << role << std::endl;
67 break;
71 return var;
74 /***
75 ****
76 ***/
78 void
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 ):
87 myPrefs( prefs )
91 TorrentModel :: ~TorrentModel( )
93 clear( );
96 /***
97 ****
98 ***/
100 Torrent*
101 TorrentModel :: getTorrentFromId( int id )
103 id_to_torrent_t::iterator it( myIdToTorrent.find( id ) );
104 return it == myIdToTorrent.end() ? 0 : it.value( );
107 const Torrent*
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( );
114 /***
115 ****
116 ***/
118 void
119 TorrentModel :: onTorrentChanged( int torrentId )
121 const int row( myIdToRow.value( torrentId, -1 ) );
122 if( row >= 0 ) {
123 QModelIndex qmi( index( row, 0 ) );
124 emit dataChanged( qmi, qmi );
128 void
129 TorrentModel :: removeTorrents( tr_variant * torrents )
131 int i = 0;
132 tr_variant * child;
133 while(( child = tr_variantListChild( torrents, i++ ))) {
134 int64_t intVal;
135 if( tr_variantGetInt( child, &intVal ) )
136 removeTorrent( intVal );
140 void
141 TorrentModel :: updateTorrents( tr_variant * torrents, bool isCompleteList )
143 QList<Torrent*> newTorrents;
144 QSet<int> oldIds;
145 QSet<int> addIds;
146 QSet<int> newIds;
147 int updatedCount = 0;
149 if ( isCompleteList )
150 oldIds = getIds( );
152 if( tr_variantIsList( torrents ) )
154 size_t i( 0 );
155 tr_variant * child;
156 while(( child = tr_variantListChild( torrents, i++ )))
158 int64_t id;
159 if( tr_variantDictFindInt( child, TR_KEY_id, &id ) )
161 newIds.insert( id );
163 Torrent * tor = getTorrentFromId( id );
164 if( tor == 0 )
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)));
173 else
175 tor->update( child );
176 ++updatedCount;
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( ) );
191 QSet<int> ids;
193 beginInsertRows( QModelIndex(), oldCount, newCount - 1 );
195 foreach( Torrent * tor, newTorrents ) {
196 addTorrent( tor );
197 addIds.insert( tor->id( ) );
199 endInsertRows( );
202 if( !addIds.isEmpty() )
203 emit torrentsAdded( addIds );
205 if( isCompleteList )
207 QSet<int> removedIds( oldIds );
208 removedIds -= newIds;
209 foreach( int id, removedIds )
210 removeTorrent( id );
214 void
215 TorrentModel :: removeTorrent( int id )
217 const int row = myIdToRow.value( id, -1 );
218 if( row >= 0 )
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 )
226 --i.value();
227 myIdToRow.remove( id );
228 myIdToTorrent.remove( id );
229 myTorrents.remove( myTorrents.indexOf( tor ) );
230 endRemoveRows( );
232 delete tor;
236 void
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;
260 QSet<int>
261 TorrentModel :: getIds () const
263 QSet<int> ids;
265 ids.reserve (myTorrents.size());
266 foreach (const Torrent * tor, myTorrents)
267 ids.insert (tor->id());
269 return ids;
272 bool
273 TorrentModel :: hasTorrent( const QString& hashString ) const
275 foreach( const Torrent * tor, myTorrents )
276 if( tor->hashString( ) == hashString )
277 return true;
278 return false;