~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plugins/infowidget/peerviewmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <interfaces/torrentinterface.h>
26
26
#include <util/functions.h>
27
27
#include "flagdb.h"
 
28
#include <kdebug.h>
28
29
 
29
30
#ifdef USE_SYSTEM_GEOIP
30
31
#include <GeoIP.h>
85
86
                if (geo_ip)
86
87
                {
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 
 
91
#ifdef Q_WS_WIN
 
92
                        if (country_id >= sizeof(GeoIP_country_name)/sizeof(char *))
 
93
                        {
 
94
                                kWarning() << "ip" << stats.ip_address << "not found in GeoIP database";
 
95
                                country_id = sizeof(GeoIP_country_name)/sizeof(char *)-1;       
 
96
                        }
 
97
#endif
88
98
                        country = GeoIP_country_name[country_id];
89
99
                        flag = KIcon(flagDB.getFlag(GeoIP_country_code[country_id]));
90
100
                }
91
101
        }
92
 
                        
 
102
                /*      
93
103
        bool PeerViewModel::Item::changed() const
94
104
        {
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)
109
121
                {
110
122
                        stats = s;
111
123
                        return true;
112
124
                }
113
125
                return false;
114
126
        }
 
127
        */
 
128
        bool PeerViewModel::Item::changed(int col,bool & modified) const
 
129
        {
 
130
                const PeerInterface::Stats & s = peer->getStats();
 
131
                bool ret = false;
 
132
                
 
133
                switch (col)
 
134
                {
 
135
                        case 3: 
 
136
                                ret = s.download_rate != stats.download_rate;
 
137
                                break;
 
138
                        case 4: 
 
139
                                ret = s.upload_rate != stats.upload_rate;
 
140
                                break;
 
141
                        case 5: 
 
142
                                ret = s.choked != stats.choked;
 
143
                                break;
 
144
                        case 6: 
 
145
                                ret = s.snubbed != stats.snubbed;
 
146
                                break;
 
147
                        case 7: 
 
148
                                ret = s.perc_of_file != stats.perc_of_file;
 
149
                                break;
 
150
                        case 9: 
 
151
                                ret = s.aca_score != stats.aca_score;
 
152
                                break;
 
153
                        case 10: 
 
154
                                ret = s.has_upload_slot != stats.has_upload_slot;
 
155
                                break;
 
156
                        case 11:
 
157
                                ret = (s.num_down_requests != stats.num_down_requests || s.num_up_requests != stats.num_up_requests);
 
158
                                break;
 
159
                        case 12: 
 
160
                                ret = s.bytes_downloaded != stats.bytes_downloaded;
 
161
                                break;
 
162
                        case 13: 
 
163
                                ret = s.bytes_uploaded != stats.bytes_uploaded;
 
164
                                break;
 
165
                        case 14: 
 
166
                                ret = s.interested != stats.interested;
 
167
                                break;
 
168
                        case 15:
 
169
                                ret = s.am_interested != stats.am_interested;
 
170
                                break;
 
171
                        default: 
 
172
                                ret = false;
 
173
                                break;
 
174
                }
 
175
 
 
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;
 
189
                stats = s;
 
190
                return ret;
 
191
        }
115
192
        
116
193
        QVariant PeerViewModel::Item::data(int col) const
117
194
        {
122
199
                        case 2: return stats.client;
123
200
                        case 3: 
124
201
                                if (stats.download_rate >= 103)
125
 
                                        return KBytesPerSecToString(stats.download_rate / 1024.0);
 
202
                                        return BytesPerSecToString(stats.download_rate);
126
203
                                else
127
204
                                        return QVariant();
128
205
                        case 4: 
129
206
                                if (stats.upload_rate >= 103)
130
 
                                        return KBytesPerSecToString(stats.upload_rate / 1024.0);
 
207
                                        return BytesPerSecToString(stats.upload_rate);
131
208
                                else
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();
143
222
                }
144
223
                return QVariant();
145
224
        }
146
225
        
147
 
        QVariant PeerViewModel::Item::dataForSorting(int col) const
 
226
        bool PeerViewModel::Item::lessThan(int col,const Item* other) const
148
227
        {
149
228
                switch (col)
150
229
                {
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;
166
248
                }
167
 
                return QVariant();
 
249
                return false;
168
250
        }
169
251
        
170
252
        QVariant PeerViewModel::Item::decoration(int col) const
188
270
        PeerViewModel::PeerViewModel ( QObject* parent )
189
271
        : QAbstractTableModel(parent)
190
272
        {
 
273
                sort_column = 0;
 
274
                sort_order = Qt::AscendingOrder;
191
275
        }
192
276
 
193
277
 
194
278
        PeerViewModel::~PeerViewModel()
195
279
        {
 
280
                qDeleteAll(items);
196
281
        }
197
282
        
198
283
        void PeerViewModel::peerAdded(bt::PeerInterface* peer)
199
284
        {
200
 
                items.append(Item(peer));
 
285
                items.append(new Item(peer));
201
286
                insertRow(items.count() - 1);
 
287
                sort(sort_column,sort_order);
202
288
        }
203
289
        
204
290
        void PeerViewModel::peerRemoved(bt::PeerInterface* peer)
205
291
        {
206
292
                int idx = 0;
207
 
                for (QList<Item>::iterator i = items.begin();i != items.end();i++)
 
293
                for (QList<Item*>::iterator i = items.begin();i != items.end();i++)
208
294
                {
209
 
                        const Item & item = *i;
210
 
                        if (item.peer == peer)
 
295
                        Item* item = *i;
 
296
                        if (item->peer == peer)
211
297
                        {
212
298
                                items.erase(i);
 
299
                                delete item;
213
300
                                removeRow(idx);
214
301
                                break;
215
302
                        }
219
306
        
220
307
        void PeerViewModel::clear()
221
308
        {
 
309
                qDeleteAll(items);
222
310
                items.clear();
223
311
                reset();
224
312
        }
225
313
        
226
 
        bool PeerViewModel::update()
 
314
        void PeerViewModel::update()
227
315
        {
228
 
                bool ret = false;
 
316
                bool resort = false;
229
317
                Uint32 idx=0;
230
 
                foreach (const Item & i,items)
 
318
                foreach (Item* i,items)
231
319
                {
232
 
                        if (i.changed())
233
 
                                ret = true;
 
320
                        bool modified = false;
 
321
                        if (i->changed(sort_column,modified))
 
322
                                resort = true;
 
323
                        
 
324
                        if (modified && !resort)
 
325
                                emit dataChanged(index(idx,3),index(idx,15));
234
326
                        idx++;
235
327
                }
236
 
                return ret;
 
328
        
 
329
                if (resort)
 
330
                        sort(sort_column,sort_order);
 
331
        }
 
332
        
 
333
        QModelIndex PeerViewModel::index(int row,int column,const QModelIndex & parent) const
 
334
        {
 
335
                if (!hasIndex(row,column,parent) || parent.isValid())
 
336
                        return QModelIndex();
 
337
                else
 
338
                        return createIndex(row,column,items[row]);
237
339
        }
238
340
 
239
341
        int PeerViewModel::rowCount( const QModelIndex & parent) const
249
351
                if (parent.isValid())
250
352
                        return 0;
251
353
                else
252
 
                        return 14;
 
354
                        return 16;
253
355
        }
254
356
        
255
357
        QVariant PeerViewModel::headerData(int section, Qt::Orientation orientation,int role) const
256
358
        {
257
 
                if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
 
359
                if (orientation != Qt::Horizontal)
258
360
                        return QVariant();
259
361
                 
260
 
                switch (section)
261
 
                {
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();
277
 
                }
 
362
                if (role == Qt::DisplayRole)
 
363
                {
 
364
                        switch (section)
 
365
                        {
 
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();
 
383
                        }
 
384
                }
 
385
                else if (role == Qt::ToolTipRole)
 
386
                {
 
387
                        switch (section)
 
388
                        {
 
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();
 
406
                        }
 
407
                }
 
408
                
 
409
                return QVariant();
278
410
        }
279
411
        
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(); 
284
416
                
 
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());
291
422
                
292
423
                return QVariant();
293
424
        }
311
442
                if (!index.isValid() || index.row() >= items.count() || index.row() < 0)
312
443
                        return 0; 
313
444
                else
314
 
                        return items[index.row()].peer;
 
445
                        return ((Item*)index.internalPointer())->peer;
315
446
        }
 
447
        
 
448
        class PeerViewModelItemCmp
 
449
        {
 
450
        public:
 
451
                PeerViewModelItemCmp(int col,Qt::SortOrder order) : col(col),order(order)
 
452
                {}
 
453
                
 
454
                bool operator()(PeerViewModel::Item* a,PeerViewModel::Item* b)
 
455
                {
 
456
                        if (order == Qt::AscendingOrder)
 
457
                                return a->lessThan(col,b);
 
458
                        else
 
459
                                return !a->lessThan(col,b);
 
460
                }
 
461
                
 
462
                int col;
 
463
                Qt::SortOrder order;
 
464
        };
316
465
 
 
466
        void PeerViewModel::sort(int col, Qt::SortOrder order)
 
467
        {
 
468
                sort_column = col;
 
469
                sort_order = order;
 
470
                emit layoutAboutToBeChanged();
 
471
                qStableSort(items.begin(),items.end(),PeerViewModelItemCmp(col,order));
 
472
                emit layoutChanged();
 
473
        }
317
474
}