1 | /* |
---|
2 | * This file Copyright (C) 2009-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU Public License v2 or v3 licenses, |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: torrent-model.cc 14379 2014-12-12 23:52:17Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <cassert> |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | #include <libtransmission/transmission.h> |
---|
14 | #include <libtransmission/variant.h> |
---|
15 | |
---|
16 | #include "torrent-delegate.h" |
---|
17 | #include "torrent-model.h" |
---|
18 | |
---|
19 | void |
---|
20 | TorrentModel::clear () |
---|
21 | { |
---|
22 | beginResetModel (); |
---|
23 | |
---|
24 | myIdToRow.clear (); |
---|
25 | myIdToTorrent.clear (); |
---|
26 | foreach (Torrent * tor, myTorrents) delete tor; |
---|
27 | myTorrents.clear (); |
---|
28 | |
---|
29 | endResetModel (); |
---|
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 | |
---|
45 | const Torrent * t = myTorrents.value (index.row(), 0); |
---|
46 | if (t != 0) |
---|
47 | { |
---|
48 | switch (role) |
---|
49 | { |
---|
50 | case Qt::DisplayRole: |
---|
51 | var.setValue (t->name()); |
---|
52 | break; |
---|
53 | |
---|
54 | case Qt::DecorationRole: |
---|
55 | var.setValue (t->getMimeTypeIcon()); |
---|
56 | break; |
---|
57 | |
---|
58 | case TorrentRole: |
---|
59 | var = qVariantFromValue(t); |
---|
60 | break; |
---|
61 | |
---|
62 | default: |
---|
63 | //std::cerr << "Unhandled role: " << role << std::endl; |
---|
64 | break; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | return var; |
---|
69 | } |
---|
70 | |
---|
71 | /*** |
---|
72 | **** |
---|
73 | ***/ |
---|
74 | |
---|
75 | void |
---|
76 | TorrentModel::addTorrent (Torrent * t) |
---|
77 | { |
---|
78 | myIdToTorrent.insert (t->id (), t); |
---|
79 | myIdToRow.insert (t->id (), myTorrents.size ()); |
---|
80 | myTorrents.append (t); |
---|
81 | } |
---|
82 | |
---|
83 | TorrentModel::TorrentModel (Prefs& prefs): |
---|
84 | myPrefs (prefs) |
---|
85 | { |
---|
86 | } |
---|
87 | |
---|
88 | TorrentModel::~TorrentModel () |
---|
89 | { |
---|
90 | clear (); |
---|
91 | } |
---|
92 | |
---|
93 | /*** |
---|
94 | **** |
---|
95 | ***/ |
---|
96 | |
---|
97 | Torrent* |
---|
98 | TorrentModel::getTorrentFromId (int id) |
---|
99 | { |
---|
100 | id_to_torrent_t::iterator it (myIdToTorrent.find (id)); |
---|
101 | return it == myIdToTorrent.end() ? 0 : it.value (); |
---|
102 | } |
---|
103 | |
---|
104 | const Torrent* |
---|
105 | TorrentModel::getTorrentFromId (int id) const |
---|
106 | { |
---|
107 | id_to_torrent_t::const_iterator it (myIdToTorrent.find (id)); |
---|
108 | return it == myIdToTorrent.end() ? 0 : it.value (); |
---|
109 | } |
---|
110 | |
---|
111 | /*** |
---|
112 | **** |
---|
113 | ***/ |
---|
114 | |
---|
115 | void |
---|
116 | TorrentModel::onTorrentChanged (int torrentId) |
---|
117 | { |
---|
118 | const int row (myIdToRow.value (torrentId, -1)); |
---|
119 | if (row >= 0) |
---|
120 | { |
---|
121 | QModelIndex qmi (index (row, 0)); |
---|
122 | emit dataChanged (qmi, qmi); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void |
---|
127 | TorrentModel::removeTorrents (tr_variant * torrents) |
---|
128 | { |
---|
129 | int i = 0; |
---|
130 | tr_variant * child; |
---|
131 | while( (child = tr_variantListChild (torrents, i++))) |
---|
132 | { |
---|
133 | int64_t intVal; |
---|
134 | if (tr_variantGetInt (child, &intVal)) |
---|
135 | removeTorrent (intVal); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void |
---|
140 | TorrentModel::updateTorrents (tr_variant * torrents, bool isCompleteList) |
---|
141 | { |
---|
142 | QList<Torrent*> newTorrents; |
---|
143 | QSet<int> oldIds; |
---|
144 | QSet<int> addIds; |
---|
145 | QSet<int> newIds; |
---|
146 | |
---|
147 | if (isCompleteList) |
---|
148 | oldIds = getIds (); |
---|
149 | |
---|
150 | if (tr_variantIsList (torrents)) |
---|
151 | { |
---|
152 | size_t i (0); |
---|
153 | tr_variant * child; |
---|
154 | while( (child = tr_variantListChild (torrents, i++))) |
---|
155 | { |
---|
156 | int64_t id; |
---|
157 | if (tr_variantDictFindInt (child, TR_KEY_id, &id)) |
---|
158 | { |
---|
159 | newIds.insert (id); |
---|
160 | |
---|
161 | Torrent * tor = getTorrentFromId (id); |
---|
162 | if (tor == 0) |
---|
163 | { |
---|
164 | tor = new Torrent (myPrefs, id); |
---|
165 | tor->update (child); |
---|
166 | if (!tor->hasMetadata()) |
---|
167 | tor->setMagnet (true); |
---|
168 | newTorrents.append (tor); |
---|
169 | connect (tor, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int))); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | tor->update (child); |
---|
174 | if (tor->isMagnet() && tor->hasMetadata()) |
---|
175 | { |
---|
176 | addIds.insert (tor->id()); |
---|
177 | tor->setMagnet (false); |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | if (!newTorrents.isEmpty ()) |
---|
185 | { |
---|
186 | const int oldCount (rowCount ()); |
---|
187 | const int newCount (oldCount + newTorrents.size ()); |
---|
188 | QSet<int> ids; |
---|
189 | |
---|
190 | beginInsertRows (QModelIndex(), oldCount, newCount - 1); |
---|
191 | |
---|
192 | foreach (Torrent * tor, newTorrents) |
---|
193 | { |
---|
194 | addTorrent (tor); |
---|
195 | addIds.insert (tor->id ()); |
---|
196 | } |
---|
197 | |
---|
198 | endInsertRows (); |
---|
199 | } |
---|
200 | |
---|
201 | if (!addIds.isEmpty()) |
---|
202 | emit torrentsAdded (addIds); |
---|
203 | |
---|
204 | if (isCompleteList) |
---|
205 | { |
---|
206 | QSet<int> removedIds (oldIds); |
---|
207 | removedIds -= newIds; |
---|
208 | foreach (int id, removedIds) |
---|
209 | removeTorrent (id); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | TorrentModel::removeTorrent (int id) |
---|
215 | { |
---|
216 | const int row = myIdToRow.value (id, -1); |
---|
217 | if (row >= 0) |
---|
218 | { |
---|
219 | Torrent * tor = myIdToTorrent.value (id, 0); |
---|
220 | |
---|
221 | beginRemoveRows (QModelIndex(), row, row); |
---|
222 | // make the myIdToRow map consistent with list view/model |
---|
223 | for (QMap<int,int>::iterator i = myIdToRow.begin(); i != myIdToRow.end(); ++i) |
---|
224 | if (i.value() > row) |
---|
225 | --i.value(); |
---|
226 | myIdToRow.remove (id); |
---|
227 | myIdToTorrent.remove (id); |
---|
228 | myTorrents.remove (myTorrents.indexOf (tor)); |
---|
229 | endRemoveRows (); |
---|
230 | |
---|
231 | delete tor; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | void |
---|
236 | TorrentModel::getTransferSpeed (Speed & uploadSpeed, |
---|
237 | size_t & uploadPeerCount, |
---|
238 | Speed & downloadSpeed, |
---|
239 | size_t & downloadPeerCount) |
---|
240 | { |
---|
241 | Speed upSpeed, downSpeed; |
---|
242 | size_t upCount=0, downCount=0; |
---|
243 | |
---|
244 | foreach (const Torrent * const tor, myTorrents) |
---|
245 | { |
---|
246 | upSpeed += tor->uploadSpeed (); |
---|
247 | upCount += tor->peersWeAreUploadingTo (); |
---|
248 | downSpeed += tor->downloadSpeed (); |
---|
249 | downCount += tor->webseedsWeAreDownloadingFrom(); |
---|
250 | downCount += tor->peersWeAreDownloadingFrom(); |
---|
251 | } |
---|
252 | |
---|
253 | uploadSpeed = upSpeed; |
---|
254 | uploadPeerCount = upCount; |
---|
255 | downloadSpeed = downSpeed; |
---|
256 | downloadPeerCount = downCount; |
---|
257 | } |
---|
258 | |
---|
259 | QSet<int> |
---|
260 | TorrentModel::getIds () const |
---|
261 | { |
---|
262 | QSet<int> ids; |
---|
263 | |
---|
264 | ids.reserve (myTorrents.size()); |
---|
265 | foreach (const Torrent * tor, myTorrents) |
---|
266 | ids.insert (tor->id()); |
---|
267 | |
---|
268 | return ids; |
---|
269 | } |
---|
270 | |
---|
271 | bool |
---|
272 | TorrentModel::hasTorrent (const QString& hashString) const |
---|
273 | { |
---|
274 | foreach (const Torrent * tor, myTorrents) |
---|
275 | if (tor->hashString () == hashString) |
---|
276 | return true; |
---|
277 | |
---|
278 | return false; |
---|
279 | } |
---|