Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / qt / torrent-model.cc
blob4a2d671a15c3f3bdacb1b78bbded5d90f67edb9c
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 11092 2010-08-01 20:36:13Z charles $
13 #include <cassert>
14 #include <iostream>
16 #include <libtransmission/transmission.h>
17 #include <libtransmission/bencode.h>
19 #include "torrent-delegate.h"
20 #include "torrent-model.h"
22 void
23 TorrentModel :: clear( )
25 myIdToRow.clear( );
26 myIdToTorrent.clear( );
27 foreach( Torrent * tor, myTorrents ) delete tor;
28 myTorrents.clear( );
29 reset( );
32 int
33 TorrentModel :: rowCount( const QModelIndex& parent ) const
35 Q_UNUSED( parent );
37 return myTorrents.size( );
40 QVariant
41 TorrentModel :: data( const QModelIndex& index, int role ) const
43 QVariant var;
44 const int row = index.row( );
45 if( row<0 || row>=myTorrents.size() )
46 return QVariant( );
48 const Torrent* t = myTorrents.at( row );
50 switch( role )
52 case Qt::DisplayRole:
53 var = QString( t->name() );
54 break;
56 case Qt::DecorationRole:
57 var = t->getMimeTypeIcon( );
58 break;
60 case TorrentRole:
61 var = qVariantFromValue( t );
62 break;
64 default:
65 //std::cerr << "Unhandled role: " << role << std::endl;
66 break;
69 return var;
72 /***
73 ****
74 ***/
76 void
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 ):
85 myPrefs( prefs )
89 TorrentModel :: ~TorrentModel( )
91 clear( );
94 /***
95 ****
96 ***/
98 Torrent*
99 TorrentModel :: getTorrentFromId( int id )
101 id_to_torrent_t::iterator it( myIdToTorrent.find( id ) );
102 return it == myIdToTorrent.end() ? 0 : it.value( );
105 const Torrent*
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( );
112 /***
113 ****
114 ***/
116 void
117 TorrentModel :: onTorrentChanged( int torrentId )
119 const int row( myIdToRow.value( torrentId, -1 ) );
120 if( row >= 0 ) {
121 QModelIndex qmi( index( row, 0 ) );
122 emit dataChanged( qmi, qmi );
126 void
127 TorrentModel :: removeTorrents( tr_benc * torrents )
129 int i = 0;
130 tr_benc * child;
131 while(( child = tr_bencListChild( torrents, i++ ))) {
132 int64_t intVal;
133 if( tr_bencGetInt( child, &intVal ) )
134 removeTorrent( intVal );
138 void
139 TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList )
141 QList<Torrent*> newTorrents;
142 QSet<int> oldIds( getIds( ) );
143 QSet<int> addIds;
144 QSet<int> newIds;
145 int updatedCount = 0;
147 if( tr_bencIsList( torrents ) )
149 size_t i( 0 );
150 tr_benc * child;
151 while(( child = tr_bencListChild( torrents, i++ )))
153 int64_t id;
154 if( tr_bencDictFindInt( child, "id", &id ) )
156 newIds.insert( id );
158 Torrent * tor = getTorrentFromId( id );
159 if( tor == 0 )
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)));
168 else
170 tor->update( child );
171 ++updatedCount;
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( ) );
186 QSet<int> ids;
188 beginInsertRows( QModelIndex(), oldCount, newCount - 1 );
190 foreach( Torrent * tor, newTorrents ) {
191 addTorrent( tor );
192 addIds.insert( tor->id( ) );
194 endInsertRows( );
197 if( !addIds.isEmpty() )
198 emit torrentsAdded( addIds );
200 if( isCompleteList )
202 QSet<int> removedIds( oldIds );
203 removedIds -= newIds;
204 foreach( int id, removedIds )
205 removeTorrent( id );
209 void
210 TorrentModel :: removeTorrent( int id )
212 const int row = myIdToRow.value( id, -1 );
213 if( row >= 0 )
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 )
221 --i.value();
222 myIdToRow.remove( id );
223 myIdToTorrent.remove( id );
224 myTorrents.remove( myTorrents.indexOf( tor ) );
225 endRemoveRows( );
227 delete tor;
231 Speed
232 TorrentModel :: getUploadSpeed( ) const
234 Speed up;
235 foreach( const Torrent * tor, myTorrents )
236 up += tor->uploadSpeed( );
237 return up;
240 Speed
241 TorrentModel :: getDownloadSpeed( ) const
243 Speed down;
244 foreach( const Torrent * tor, myTorrents )
245 down += tor->downloadSpeed( );
246 return down;
249 QSet<int>
250 TorrentModel :: getIds( ) const
252 QSet<int> ids;
253 foreach( const Torrent * tor, myTorrents )
254 ids.insert( tor->id( ) );
255 return ids;
258 bool
259 TorrentModel :: hasTorrent( const QString& hashString ) const
261 foreach( const Torrent * tor, myTorrents )
262 if( tor->hashString( ) == hashString )
263 return true;
264 return false;