1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
3 | * |
---|
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. |
---|
9 | * |
---|
10 | * $Id: torrent-model.cc 9868 2010-01-04 21:00:47Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <cassert> |
---|
14 | #include <iostream> |
---|
15 | |
---|
16 | #include <libtransmission/transmission.h> |
---|
17 | #include <libtransmission/bencode.h> |
---|
18 | |
---|
19 | #include "torrent-delegate.h" |
---|
20 | #include "torrent-model.h" |
---|
21 | |
---|
22 | void |
---|
23 | TorrentModel :: clear( ) |
---|
24 | { |
---|
25 | myIdToRow.clear( ); |
---|
26 | myIdToTorrent.clear( ); |
---|
27 | foreach( Torrent * tor, myTorrents ) delete tor; |
---|
28 | myTorrents.clear( ); |
---|
29 | reset( ); |
---|
30 | } |
---|
31 | |
---|
32 | int |
---|
33 | TorrentModel :: rowCount( const QModelIndex& parent ) const |
---|
34 | { |
---|
35 | Q_UNUSED( parent ); |
---|
36 | |
---|
37 | return myTorrents.size( ); |
---|
38 | } |
---|
39 | |
---|
40 | QVariant |
---|
41 | TorrentModel :: data( const QModelIndex& index, int role ) const |
---|
42 | { |
---|
43 | QVariant var; |
---|
44 | const int row = index.row( ); |
---|
45 | if( row<0 || row>=myTorrents.size() ) |
---|
46 | return QVariant( ); |
---|
47 | |
---|
48 | const Torrent* t = myTorrents.at( row ); |
---|
49 | |
---|
50 | switch( role ) |
---|
51 | { |
---|
52 | case Qt::DisplayRole: |
---|
53 | var = QString( t->name() ); |
---|
54 | break; |
---|
55 | |
---|
56 | case Qt::DecorationRole: |
---|
57 | var = t->getMimeTypeIcon( ); |
---|
58 | break; |
---|
59 | |
---|
60 | case TorrentRole: |
---|
61 | var = qVariantFromValue( t ); |
---|
62 | break; |
---|
63 | |
---|
64 | default: |
---|
65 | //std::cerr << "Unhandled role: " << role << std::endl; |
---|
66 | break; |
---|
67 | } |
---|
68 | |
---|
69 | return var; |
---|
70 | } |
---|
71 | |
---|
72 | /*** |
---|
73 | **** |
---|
74 | ***/ |
---|
75 | |
---|
76 | void |
---|
77 | TorrentModel :: addTorrent( Torrent * t ) |
---|
78 | { |
---|
79 | myIdToTorrent.insert( t->id( ), t ); |
---|
80 | myIdToRow.insert( t->id( ), myTorrents.size( ) ); |
---|
81 | myTorrents.append( t ); |
---|
82 | } |
---|
83 | |
---|
84 | TorrentModel :: TorrentModel( Prefs& prefs ): |
---|
85 | myPrefs( prefs ) |
---|
86 | { |
---|
87 | } |
---|
88 | |
---|
89 | TorrentModel :: ~TorrentModel( ) |
---|
90 | { |
---|
91 | clear( ); |
---|
92 | } |
---|
93 | |
---|
94 | /*** |
---|
95 | **** |
---|
96 | ***/ |
---|
97 | |
---|
98 | Torrent* |
---|
99 | TorrentModel :: getTorrentFromId( int id ) |
---|
100 | { |
---|
101 | id_to_torrent_t::iterator it( myIdToTorrent.find( id ) ); |
---|
102 | return it == myIdToTorrent.end() ? 0 : it.value( ); |
---|
103 | } |
---|
104 | |
---|
105 | const Torrent* |
---|
106 | TorrentModel :: getTorrentFromId( int id ) const |
---|
107 | { |
---|
108 | id_to_torrent_t::const_iterator it( myIdToTorrent.find( id ) ); |
---|
109 | return it == myIdToTorrent.end() ? 0 : it.value( ); |
---|
110 | } |
---|
111 | |
---|
112 | /*** |
---|
113 | **** |
---|
114 | ***/ |
---|
115 | |
---|
116 | void |
---|
117 | TorrentModel :: onTorrentChanged( int torrentId ) |
---|
118 | { |
---|
119 | const int row( myIdToRow.value( torrentId, -1 ) ); |
---|
120 | if( row >= 0 ) { |
---|
121 | QModelIndex qmi( index( row, 0 ) ); |
---|
122 | dataChanged( qmi, qmi ); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void |
---|
127 | TorrentModel :: removeTorrents( tr_benc * torrents ) |
---|
128 | { |
---|
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 ); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | void |
---|
139 | TorrentModel :: updateTorrents( tr_benc * torrents, bool isCompleteList ) |
---|
140 | { |
---|
141 | QList<Torrent*> newTorrents; |
---|
142 | QSet<int> oldIds( getIds( ) ); |
---|
143 | QSet<int> newIds; |
---|
144 | int updatedCount = 0; |
---|
145 | |
---|
146 | if( tr_bencIsList( torrents ) ) |
---|
147 | { |
---|
148 | size_t i( 0 ); |
---|
149 | tr_benc * child; |
---|
150 | while(( child = tr_bencListChild( torrents, i++ ))) |
---|
151 | { |
---|
152 | int64_t id; |
---|
153 | if( tr_bencDictFindInt( child, "id", &id ) ) |
---|
154 | { |
---|
155 | newIds.insert( id ); |
---|
156 | |
---|
157 | Torrent * tor = getTorrentFromId( id ); |
---|
158 | if( tor == 0 ) |
---|
159 | { |
---|
160 | tor = new Torrent( myPrefs, id ); |
---|
161 | tor->update( child ); |
---|
162 | newTorrents.append( tor ); |
---|
163 | connect( tor, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int))); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | tor->update( child ); |
---|
168 | ++updatedCount; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | if( !newTorrents.isEmpty( ) ) |
---|
175 | { |
---|
176 | const int oldCount( rowCount( ) ); |
---|
177 | const int newCount( oldCount + newTorrents.size( ) ); |
---|
178 | QSet<int> ids; |
---|
179 | |
---|
180 | beginInsertRows( QModelIndex(), oldCount, newCount - 1 ); |
---|
181 | |
---|
182 | foreach( Torrent * tor, newTorrents ) { |
---|
183 | addTorrent( tor ); |
---|
184 | ids.insert( tor->id( ) ); |
---|
185 | } |
---|
186 | endInsertRows( ); |
---|
187 | |
---|
188 | emit torrentsAdded( ids ); |
---|
189 | } |
---|
190 | |
---|
191 | if( isCompleteList ) |
---|
192 | { |
---|
193 | QSet<int> removedIds( oldIds ); |
---|
194 | removedIds -= newIds; |
---|
195 | foreach( int id, removedIds ) |
---|
196 | removeTorrent( id ); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | void |
---|
201 | TorrentModel :: removeTorrent( int id ) |
---|
202 | { |
---|
203 | const int row = myIdToRow.value( id, -1 ); |
---|
204 | if( row >= 0 ) |
---|
205 | { |
---|
206 | Torrent * tor = myIdToTorrent.value( id, 0 ); |
---|
207 | |
---|
208 | beginRemoveRows( QModelIndex(), row, row ); |
---|
209 | myIdToRow.remove( id ); |
---|
210 | myIdToTorrent.remove( id ); |
---|
211 | myTorrents.remove( myTorrents.indexOf( tor ) ); |
---|
212 | endRemoveRows( ); |
---|
213 | |
---|
214 | delete tor; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | Speed |
---|
219 | TorrentModel :: getUploadSpeed( ) const |
---|
220 | { |
---|
221 | Speed up; |
---|
222 | foreach( const Torrent * tor, myTorrents ) |
---|
223 | up += tor->uploadSpeed( ); |
---|
224 | return up; |
---|
225 | } |
---|
226 | |
---|
227 | Speed |
---|
228 | TorrentModel :: getDownloadSpeed( ) const |
---|
229 | { |
---|
230 | Speed down; |
---|
231 | foreach( const Torrent * tor, myTorrents ) |
---|
232 | down += tor->downloadSpeed( ); |
---|
233 | return down; |
---|
234 | } |
---|
235 | |
---|
236 | QSet<int> |
---|
237 | TorrentModel :: getIds( ) const |
---|
238 | { |
---|
239 | QSet<int> ids; |
---|
240 | foreach( const Torrent * tor, myTorrents ) |
---|
241 | ids.insert( tor->id( ) ); |
---|
242 | return ids; |
---|
243 | } |
---|
244 | |
---|
245 | bool |
---|
246 | TorrentModel :: hasTorrent( const QString& hashString ) const |
---|
247 | { |
---|
248 | foreach( const Torrent * tor, myTorrents ) |
---|
249 | if( tor->hashString( ) == hashString ) |
---|
250 | return true; |
---|
251 | return false; |
---|
252 | } |
---|