87
88
int country_id = GeoIP_id_by_name(geo_ip, stats.ip_address.toAscii());
89
// fixed case where the ip wasn't found in the database
90
// may be usefull for unix too
92
if (country_id >= sizeof(GeoIP_country_name)/sizeof(char *))
94
kWarning() << "ip" << stats.ip_address << "not found in GeoIP database";
95
country_id = sizeof(GeoIP_country_name)/sizeof(char *)-1;
88
98
country = GeoIP_country_name[country_id];
89
99
flag = KIcon(flagDB.getFlag(GeoIP_country_code[country_id]));
93
103
bool PeerViewModel::Item::changed() const
95
105
const PeerInterface::Stats & s = peer->getStats();
105
115
s.num_down_requests != stats.num_down_requests ||
106
116
s.num_up_requests != stats.num_up_requests ||
107
117
s.bytes_downloaded != stats.bytes_downloaded ||
108
s.bytes_uploaded != stats.bytes_uploaded)
118
s.bytes_uploaded != stats.bytes_uploaded ||
119
s.interested != stats.interested ||
120
s.am_interested != stats.am_interested)
128
bool PeerViewModel::Item::changed(int col,bool & modified) const
130
const PeerInterface::Stats & s = peer->getStats();
136
ret = s.download_rate != stats.download_rate;
139
ret = s.upload_rate != stats.upload_rate;
142
ret = s.choked != stats.choked;
145
ret = s.snubbed != stats.snubbed;
148
ret = s.perc_of_file != stats.perc_of_file;
151
ret = s.aca_score != stats.aca_score;
154
ret = s.has_upload_slot != stats.has_upload_slot;
157
ret = (s.num_down_requests != stats.num_down_requests || s.num_up_requests != stats.num_up_requests);
160
ret = s.bytes_downloaded != stats.bytes_downloaded;
163
ret = s.bytes_uploaded != stats.bytes_uploaded;
166
ret = s.interested != stats.interested;
169
ret = s.am_interested != stats.am_interested;
176
modified = s.download_rate != stats.download_rate ||
177
s.upload_rate != stats.upload_rate ||
178
s.choked != stats.choked ||
179
s.snubbed != stats.snubbed ||
180
s.perc_of_file != stats.perc_of_file ||
181
s.aca_score != stats.aca_score ||
182
s.has_upload_slot != stats.has_upload_slot ||
183
s.num_down_requests != stats.num_down_requests ||
184
s.num_up_requests != stats.num_up_requests ||
185
s.bytes_downloaded != stats.bytes_downloaded ||
186
s.bytes_uploaded != stats.bytes_uploaded ||
187
s.interested != stats.interested ||
188
s.am_interested != stats.am_interested;
116
193
QVariant PeerViewModel::Item::data(int col) const
122
199
case 2: return stats.client;
124
201
if (stats.download_rate >= 103)
125
return KBytesPerSecToString(stats.download_rate / 1024.0);
202
return BytesPerSecToString(stats.download_rate);
127
204
return QVariant();
129
206
if (stats.upload_rate >= 103)
130
return KBytesPerSecToString(stats.upload_rate / 1024.0);
207
return BytesPerSecToString(stats.upload_rate);
132
209
return QVariant();
133
210
case 5: return stats.choked ? i18nc("Choked", "Yes") : i18nc("Not choked", "No");
139
216
case 11: return QString("%1 / %2").arg(stats.num_down_requests).arg(stats.num_up_requests);
140
217
case 12: return BytesToString(stats.bytes_downloaded);
141
218
case 13: return BytesToString(stats.bytes_uploaded);
219
case 14: return stats.interested ? i18nc("Interested", "Yes") : i18nc("Not Interested", "No");
220
case 15: return stats.am_interested ? i18nc("Interesting", "Yes") : i18nc("Not Interesting", "No");
142
221
default: return QVariant();
144
223
return QVariant();
147
QVariant PeerViewModel::Item::dataForSorting(int col) const
226
bool PeerViewModel::Item::lessThan(int col,const Item* other) const
151
case 0: return stats.ip_address;
152
case 1: return country;
153
case 2: return stats.client;
154
case 3: return stats.download_rate;
155
case 4: return stats.upload_rate;
156
case 5: return stats.choked;
157
case 6: return stats.snubbed;
158
case 7: return stats.perc_of_file;
159
case 8: return stats.dht_support;
160
case 9: return stats.aca_score;
161
case 10: return stats.has_upload_slot;
162
case 11: return stats.num_down_requests + stats.num_up_requests;
163
case 12: return stats.bytes_downloaded;
164
case 13: return stats.bytes_uploaded;
165
default: return QVariant();
230
case 0: return stats.ip_address < other->stats.ip_address;
231
case 1: return QString::localeAwareCompare(country,other->country) < 0;
232
case 2: return QString::localeAwareCompare(stats.client,other->stats.client) < 0;
233
case 3: return stats.download_rate < other->stats.download_rate;
234
case 4: return stats.upload_rate < other->stats.upload_rate;
235
case 5: return stats.choked < other->stats.choked;
236
case 6: return stats.snubbed < other->stats.snubbed;
237
case 7: return stats.perc_of_file < other->stats.perc_of_file;
238
case 8: return stats.dht_support < other->stats.dht_support;
239
case 9: return stats.aca_score < other->stats.aca_score;
240
case 10: return stats.has_upload_slot < other->stats.has_upload_slot;
241
case 11: return stats.num_down_requests + stats.num_up_requests <
242
other->stats.num_down_requests + other->stats.num_up_requests;
243
case 12: return stats.bytes_downloaded < other->stats.bytes_downloaded;
244
case 13: return stats.bytes_uploaded < other->stats.bytes_uploaded;
245
case 14: return stats.interested < other->stats.interested;
246
case 15: return stats.am_interested < other->stats.am_interested;
247
default: return false;
170
252
QVariant PeerViewModel::Item::decoration(int col) const
188
270
PeerViewModel::PeerViewModel ( QObject* parent )
189
271
: QAbstractTableModel(parent)
274
sort_order = Qt::AscendingOrder;
194
278
PeerViewModel::~PeerViewModel()
198
283
void PeerViewModel::peerAdded(bt::PeerInterface* peer)
200
items.append(Item(peer));
285
items.append(new Item(peer));
201
286
insertRow(items.count() - 1);
287
sort(sort_column,sort_order);
204
290
void PeerViewModel::peerRemoved(bt::PeerInterface* peer)
207
for (QList<Item>::iterator i = items.begin();i != items.end();i++)
293
for (QList<Item*>::iterator i = items.begin();i != items.end();i++)
209
const Item & item = *i;
210
if (item.peer == peer)
296
if (item->peer == peer)
220
307
void PeerViewModel::clear()
226
bool PeerViewModel::update()
314
void PeerViewModel::update()
230
foreach (const Item & i,items)
318
foreach (Item* i,items)
320
bool modified = false;
321
if (i->changed(sort_column,modified))
324
if (modified && !resort)
325
emit dataChanged(index(idx,3),index(idx,15));
330
sort(sort_column,sort_order);
333
QModelIndex PeerViewModel::index(int row,int column,const QModelIndex & parent) const
335
if (!hasIndex(row,column,parent) || parent.isValid())
336
return QModelIndex();
338
return createIndex(row,column,items[row]);
239
341
int PeerViewModel::rowCount( const QModelIndex & parent) const
249
351
if (parent.isValid())
255
357
QVariant PeerViewModel::headerData(int section, Qt::Orientation orientation,int role) const
257
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
359
if (orientation != Qt::Horizontal)
258
360
return QVariant();
262
case 0: return i18n("IP Address");
263
case 1: return i18n("Country");
264
case 2: return i18n("Client");
265
case 3: return i18n("Down Speed");
266
case 4: return i18n("Up Speed");
267
case 5: return i18n("Choked");
268
case 6: return i18n("Snubbed");
269
case 7: return i18n("Availability");
270
case 8: return i18n("DHT");
271
case 9: return i18n("Score");
272
case 10: return i18n("Upload Slot");
273
case 11: return i18n("Requests");
274
case 12: return i18n("Downloaded");
275
case 13: return i18n("Uploaded");
276
default: return QVariant();
362
if (role == Qt::DisplayRole)
366
case 0: return i18n("IP Address");
367
case 1: return i18n("Country");
368
case 2: return i18n("Client");
369
case 3: return i18n("Down Speed");
370
case 4: return i18n("Up Speed");
371
case 5: return i18n("Choked");
372
case 6: return i18n("Snubbed");
373
case 7: return i18n("Availability");
374
case 8: return i18n("DHT");
375
case 9: return i18n("Score");
376
case 10: return i18n("Upload Slot");
377
case 11: return i18n("Requests");
378
case 12: return i18n("Downloaded");
379
case 13: return i18n("Uploaded");
380
case 14: return i18n("Interested");
381
case 15: return i18n("Interesting");
382
default: return QVariant();
385
else if (role == Qt::ToolTipRole)
389
case 0: return i18n("IP address of the peer");
390
case 1: return i18n("Country the peer is in");
391
case 2: return i18n("Which client the peer is using");
392
case 3: return i18n("Download speed");
393
case 4: return i18n("Upload speed");
394
case 5: return i18n("Whether or not the peer has choked us, when we are choked the peer will not send any data to us");
395
case 6: return i18n("Snubbed means the peer hasn't sent us any data in the last 2 minutes");
396
case 7: return i18n("How much data the peer has of the torrent");
397
case 8: return i18n("Whether or not the peer has DHT enabled");
398
case 9: return i18n("The score of the peer, KTorrent uses this to determine who to upload to");
399
case 10: return i18n("Only peers which have an upload slot will get data from us");
400
case 11: return i18n("The number of download and upload requests");
401
case 12: return i18n("How much data we downloaded from this peer");
402
case 13: return i18n("How much data we uploaded to this peer");
403
case 14: return i18n("Whether the peer is interested in downloading data from us");
404
case 15: return i18n("Whether we are interested in downloading from this peer");
405
default: return QVariant();
280
412
QVariant PeerViewModel::data(const QModelIndex & index,int role) const
282
414
if (!index.isValid() || index.row() >= items.count() || index.row() < 0)
283
415
return QVariant();
417
Item* item = (Item*)index.internalPointer();
285
418
if (role == Qt::DisplayRole)
286
return items[index.row()].data(index.column());
287
else if (role == Qt::UserRole) // UserRole is for sorting
288
return items[index.row()].dataForSorting(index.column());
419
return item->data(index.column());
289
420
else if (role == Qt::DecorationRole)
290
return items[index.row()].decoration(index.column());
421
return item->decoration(index.column());
292
423
return QVariant();
311
442
if (!index.isValid() || index.row() >= items.count() || index.row() < 0)
314
return items[index.row()].peer;
445
return ((Item*)index.internalPointer())->peer;
448
class PeerViewModelItemCmp
451
PeerViewModelItemCmp(int col,Qt::SortOrder order) : col(col),order(order)
454
bool operator()(PeerViewModel::Item* a,PeerViewModel::Item* b)
456
if (order == Qt::AscendingOrder)
457
return a->lessThan(col,b);
459
return !a->lessThan(col,b);
466
void PeerViewModel::sort(int col, Qt::SortOrder order)
470
emit layoutAboutToBeChanged();
471
qStableSort(items.begin(),items.end(),PeerViewModelItemCmp(col,order));
472
emit layoutChanged();