1 | /* |
---|
2 | * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com> |
---|
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 | |
---|
11 | #include <iostream> |
---|
12 | #include <wx/intl.h> |
---|
13 | #include <torrent-list.h> |
---|
14 | |
---|
15 | namespace |
---|
16 | { |
---|
17 | typedef std::vector<tr_torrent_t*> torrents_t; |
---|
18 | |
---|
19 | enum |
---|
20 | { |
---|
21 | COL_POSITION, |
---|
22 | COL_DONE, |
---|
23 | COL_DOWNLOAD_SPEED, |
---|
24 | COL_ETA, |
---|
25 | COL_HASH, |
---|
26 | COL_NAME, |
---|
27 | COL_PEERS, |
---|
28 | COL_RATIO, |
---|
29 | COL_RECEIVED, |
---|
30 | COL_REMAINING, |
---|
31 | COL_SEEDS, |
---|
32 | COL_SENT, |
---|
33 | COL_SIZE, |
---|
34 | COL_STATE, |
---|
35 | COL_STATUS, |
---|
36 | COL_TOTAL, |
---|
37 | COL_UPLOAD_SPEED, |
---|
38 | N_COLS |
---|
39 | }; |
---|
40 | |
---|
41 | const wxString columnKeys[N_COLS] = |
---|
42 | { |
---|
43 | _T("position"), |
---|
44 | _T("done"), |
---|
45 | _T("download-speed"), |
---|
46 | _T("eta"), |
---|
47 | _T("hash"), |
---|
48 | _T("name"), |
---|
49 | _T("peers"), |
---|
50 | _T("ratio"), |
---|
51 | _T("received"), |
---|
52 | _T("remaining"), |
---|
53 | _T("seeds"), |
---|
54 | _T("sent"), |
---|
55 | _T("size"), |
---|
56 | _T("state"), |
---|
57 | _T("status"), |
---|
58 | _T("total"), |
---|
59 | _T("upload-speed") |
---|
60 | }; |
---|
61 | |
---|
62 | int getTorrentColumn( const wxString& key ) |
---|
63 | { |
---|
64 | typedef std::map<wxString,int> string2key_t; |
---|
65 | static string2key_t columns; |
---|
66 | |
---|
67 | if( columns.empty() ) |
---|
68 | { |
---|
69 | columns[_T("position")] = COL_POSITION; |
---|
70 | columns[_T("done")] = COL_DONE; |
---|
71 | columns[_T("download-speed")] = COL_DOWNLOAD_SPEED; |
---|
72 | columns[_T("eta")] = COL_ETA; |
---|
73 | columns[_T("hash")] = COL_HASH; |
---|
74 | columns[_T("name")] = COL_NAME; |
---|
75 | columns[_T("peers")] = COL_PEERS; |
---|
76 | columns[_T("ratio")] = COL_RATIO; |
---|
77 | columns[_T("received")] = COL_RECEIVED; |
---|
78 | columns[_T("remaining")] = COL_REMAINING; |
---|
79 | columns[_T("seeds")] = COL_SEEDS; |
---|
80 | columns[_T("sent")] = COL_SENT; |
---|
81 | columns[_T("size")] = COL_SIZE; |
---|
82 | columns[_T("state")] = COL_STATE; |
---|
83 | columns[_T("status")] = COL_STATUS; |
---|
84 | columns[_T("total")] = COL_TOTAL; |
---|
85 | columns[_T("upload-speed")] = COL_UPLOAD_SPEED; |
---|
86 | } |
---|
87 | |
---|
88 | int i = -1; |
---|
89 | string2key_t::const_iterator it = columns.find( key ); |
---|
90 | if( it != columns.end() ) |
---|
91 | i = it->second; |
---|
92 | |
---|
93 | return i; |
---|
94 | } |
---|
95 | |
---|
96 | typedef std::vector<int> int_v; |
---|
97 | |
---|
98 | int_v getTorrentColumns( wxConfig * config ) |
---|
99 | { |
---|
100 | const wxString key = _T("torrent-list-columns"); |
---|
101 | wxString columnStr; |
---|
102 | if( !config->Read( key, &columnStr, _T("name|download-speed|upload-speed|eta|peers|size|done|status|seeds") ) ) |
---|
103 | config->Write( key, columnStr ); |
---|
104 | |
---|
105 | int_v cols; |
---|
106 | while( !columnStr.IsEmpty() ) |
---|
107 | { |
---|
108 | const wxString key = columnStr.BeforeFirst(_T('|')); |
---|
109 | columnStr.Remove( 0, key.Len() + 1 ); |
---|
110 | cols.push_back( getTorrentColumn( key ) ); |
---|
111 | } |
---|
112 | return cols; |
---|
113 | } |
---|
114 | |
---|
115 | int bestDecimal( double num ) { |
---|
116 | if ( num < 10 ) return 2; |
---|
117 | if ( num < 100 ) return 1; |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | wxString toWxStr( const std::string& s ) |
---|
122 | { |
---|
123 | return wxString( s.c_str(), wxConvUTF8 ); |
---|
124 | } |
---|
125 | |
---|
126 | wxString toWxStr( const char * s ) |
---|
127 | { |
---|
128 | return wxString( s, wxConvUTF8 ); |
---|
129 | } |
---|
130 | |
---|
131 | wxString getReadableSize( uint64_t size ) |
---|
132 | { |
---|
133 | int i; |
---|
134 | static const char *sizestrs[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" }; |
---|
135 | for ( i=0; size>>10; ++i ) size = size>>10; |
---|
136 | char buf[512]; |
---|
137 | snprintf( buf, sizeof(buf), "%.*f %s", bestDecimal(size), (double)size, sizestrs[i] ); |
---|
138 | return toWxStr( buf ); |
---|
139 | } |
---|
140 | |
---|
141 | wxString getReadableSize( float f ) |
---|
142 | { |
---|
143 | return getReadableSize( (uint64_t)f ); |
---|
144 | } |
---|
145 | |
---|
146 | wxString getReadableSpeed( float f ) |
---|
147 | { |
---|
148 | wxString xstr = getReadableSize(f); |
---|
149 | xstr += _T("/s"); |
---|
150 | return xstr; |
---|
151 | } |
---|
152 | |
---|
153 | wxString getReadableTime( int i /*seconds*/ ) /*FIXME*/ |
---|
154 | { |
---|
155 | const int s = i % 60; i /= 60; |
---|
156 | const int m = i % 60; i /= 60; |
---|
157 | const int h = i; |
---|
158 | return wxString::Format( _T("%d:%02d:%02d"), h, m, s ); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | enum |
---|
163 | { |
---|
164 | TORRENT_LIST_CTRL = 1000 |
---|
165 | }; |
---|
166 | |
---|
167 | BEGIN_EVENT_TABLE(TorrentListCtrl, wxListCtrl) |
---|
168 | EVT_LIST_COL_CLICK(TORRENT_LIST_CTRL, TorrentListCtrl::OnSort) |
---|
169 | END_EVENT_TABLE() |
---|
170 | |
---|
171 | TorrentListCtrl :: TorrentListCtrl( tr_handle_t * handle, |
---|
172 | wxConfig * config, |
---|
173 | wxWindow * parent, |
---|
174 | const wxPoint & pos, |
---|
175 | const wxSize & size): |
---|
176 | wxListCtrl( parent, TORRENT_LIST_CTRL, pos, size, wxLC_REPORT|wxLC_SINGLE_SEL ), |
---|
177 | myHandle( handle ), |
---|
178 | myConfig( config ) |
---|
179 | { |
---|
180 | wxString sortColStr; |
---|
181 | myConfig->Read( _T("torrent-sort-column"), &sortColStr, columnKeys[COL_NAME] ); |
---|
182 | prevSortCol = getTorrentColumn( sortColStr ); |
---|
183 | bool descending; |
---|
184 | myConfig->Read( _T("torrent-sort-is-descending"), &descending, FALSE ); |
---|
185 | if( descending ) |
---|
186 | prevSortCol = -prevSortCol; |
---|
187 | Rebuild (); |
---|
188 | } |
---|
189 | |
---|
190 | TorrentListCtrl :: ~TorrentListCtrl() |
---|
191 | { |
---|
192 | } |
---|
193 | |
---|
194 | void |
---|
195 | TorrentListCtrl :: RefreshTorrent( tr_torrent_t * tor, |
---|
196 | int myTorrents_index, |
---|
197 | const int_v & cols ) |
---|
198 | { |
---|
199 | int row = -1; |
---|
200 | int col = 0; |
---|
201 | char buf[512]; |
---|
202 | std::string str; |
---|
203 | const tr_stat_t * s = tr_torrentStat( tor ); |
---|
204 | const tr_info_t* info = tr_torrentInfo( tor ); |
---|
205 | |
---|
206 | for( int_v::const_iterator it(cols.begin()), end(cols.end()); it!=end; ++it ) |
---|
207 | { |
---|
208 | wxString xstr; |
---|
209 | |
---|
210 | switch( *it ) |
---|
211 | { |
---|
212 | case COL_POSITION: |
---|
213 | snprintf( buf, sizeof(buf), "%d", 666 ); |
---|
214 | xstr = toWxStr( buf ); |
---|
215 | break; |
---|
216 | |
---|
217 | case COL_DONE: |
---|
218 | snprintf( buf, sizeof(buf), "%d%%", (int)(s->percentDone*100.0) ); |
---|
219 | xstr = toWxStr( buf ); |
---|
220 | break; |
---|
221 | |
---|
222 | case COL_DOWNLOAD_SPEED: break; |
---|
223 | xstr = getReadableSpeed( s->rateDownload ); |
---|
224 | break; |
---|
225 | |
---|
226 | case COL_ETA: |
---|
227 | if( (int)(s->percentDone*100) >= 100 ) |
---|
228 | xstr = wxString (); |
---|
229 | else if( s->eta < 0 ) |
---|
230 | xstr = toWxStr( "\xE2\x88\x9E" ); /* infinity, in utf-8 */ |
---|
231 | else |
---|
232 | xstr = getReadableTime( s->eta ); |
---|
233 | break; |
---|
234 | |
---|
235 | case COL_HASH: |
---|
236 | xstr = toWxStr( info->hashString ); |
---|
237 | break; |
---|
238 | |
---|
239 | case COL_NAME: |
---|
240 | xstr = toWxStr( info->name ); |
---|
241 | break; |
---|
242 | |
---|
243 | case COL_PEERS: |
---|
244 | /* FIXME: this is all peers, not just leechers */ |
---|
245 | snprintf( buf, sizeof(buf), "%d (%d)", s->peersTotal, s->peersConnected ); |
---|
246 | xstr = toWxStr( buf ); |
---|
247 | break; |
---|
248 | |
---|
249 | case COL_RATIO: |
---|
250 | snprintf( buf, sizeof(buf), "%%%d", (int)(s->uploaded / (double)s->downloadedValid) ); |
---|
251 | xstr = toWxStr( buf ); |
---|
252 | break; |
---|
253 | |
---|
254 | case COL_RECEIVED: |
---|
255 | xstr = getReadableSize( s->downloaded ); |
---|
256 | break; |
---|
257 | |
---|
258 | case COL_REMAINING: |
---|
259 | xstr = getReadableSize( s->left ); |
---|
260 | break; |
---|
261 | |
---|
262 | case COL_SEEDS: |
---|
263 | snprintf( buf, sizeof(buf), "%d", s->seeders ); /* FIXME: %d (%d) */ |
---|
264 | xstr = toWxStr( buf ); |
---|
265 | break; |
---|
266 | |
---|
267 | case COL_SENT: |
---|
268 | xstr = getReadableSize( s->uploaded ); |
---|
269 | break; |
---|
270 | |
---|
271 | case COL_SIZE: |
---|
272 | xstr = getReadableSize( info->totalSize ); |
---|
273 | break; |
---|
274 | |
---|
275 | case COL_STATE: |
---|
276 | xstr = _T("Fixme"); |
---|
277 | break; |
---|
278 | |
---|
279 | case COL_STATUS: |
---|
280 | xstr = _T("Fixme"); |
---|
281 | break; |
---|
282 | |
---|
283 | case COL_TOTAL: |
---|
284 | xstr = _T("Fixme"); |
---|
285 | break; |
---|
286 | |
---|
287 | case COL_UPLOAD_SPEED: |
---|
288 | xstr = getReadableSpeed( s->rateUpload ); |
---|
289 | break; |
---|
290 | |
---|
291 | default: |
---|
292 | xstr = _T("Fixme"); |
---|
293 | } |
---|
294 | |
---|
295 | if( col ) |
---|
296 | SetItem( row, col++, xstr ); |
---|
297 | else { |
---|
298 | // first column... find the right row to put the info in. |
---|
299 | // if the torrent's in the list already, update that row. |
---|
300 | // otherwise, add a new row. |
---|
301 | if( row < 0 ) { |
---|
302 | str2int_t::const_iterator it = myHashToRow.find( info->hashString ); |
---|
303 | if( it != myHashToRow.end() ) { |
---|
304 | row = it->second; |
---|
305 | } |
---|
306 | } |
---|
307 | if( row >= 0 ) { |
---|
308 | SetItem( row, col++, xstr ); |
---|
309 | } |
---|
310 | else { |
---|
311 | row = InsertItem( GetItemCount(), xstr ); |
---|
312 | col = 1; |
---|
313 | myHashToRow[info->hashString] = row; |
---|
314 | SetItemData( row, myTorrents_index ); |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | static torrents_t * uglyHack = NULL; |
---|
321 | |
---|
322 | int |
---|
323 | TorrentListCtrl :: Compare( long item1, long item2, long sortData ) |
---|
324 | { |
---|
325 | const tr_torrent_t * a = (*uglyHack)[item1]; |
---|
326 | const tr_torrent_t * b = (*uglyHack)[item2]; |
---|
327 | const tr_info_t* ia = tr_torrentInfo( a ); |
---|
328 | const tr_info_t* ib = tr_torrentInfo( b ); |
---|
329 | int ret = 0; |
---|
330 | |
---|
331 | switch( abs(sortData) ) |
---|
332 | { |
---|
333 | case COL_POSITION: |
---|
334 | ret = item1 - item2; |
---|
335 | |
---|
336 | case COL_DONE: |
---|
337 | /* ccc |
---|
338 | snprintf( buf, sizeof(buf), "%d%%", (int)(s->percentDone*100.0) ); |
---|
339 | xstr = toWxStr( buf );*/ |
---|
340 | break; |
---|
341 | |
---|
342 | case COL_DOWNLOAD_SPEED: break; |
---|
343 | /*xstr = getReadableSpeed( s->rateDownload );*/ |
---|
344 | break; |
---|
345 | |
---|
346 | case COL_ETA: |
---|
347 | /* if( (int)(s->percentDone*100) >= 100 ) */ |
---|
348 | break; |
---|
349 | |
---|
350 | case COL_HASH: |
---|
351 | /*xstr = toWxStr( info->hashString );*/ |
---|
352 | break; |
---|
353 | |
---|
354 | case COL_NAME: |
---|
355 | ret = strcmp( ia->name, ib->name ); |
---|
356 | break; |
---|
357 | |
---|
358 | case COL_PEERS: |
---|
359 | /* FIXME: this is all peers, not just leechers |
---|
360 | snprintf( buf, sizeof(buf), "%d (%d)", s->peersTotal, s->peersConnected ); |
---|
361 | xstr = toWxStr( buf );*/ |
---|
362 | break; |
---|
363 | |
---|
364 | case COL_RATIO: |
---|
365 | /*snprintf( buf, sizeof(buf), "%%%d", (int)(s->uploaded / (double)s->downloadedValid) ); |
---|
366 | xstr = toWxStr( buf );*/ |
---|
367 | break; |
---|
368 | |
---|
369 | case COL_RECEIVED: |
---|
370 | /*xstr = getReadableSize( s->downloaded );*/ |
---|
371 | break; |
---|
372 | |
---|
373 | case COL_REMAINING: |
---|
374 | /*xstr = getReadableSize( s->left );*/ |
---|
375 | break; |
---|
376 | |
---|
377 | case COL_SEEDS: |
---|
378 | /*snprintf( buf, sizeof(buf), "%d", s->seeders ); |
---|
379 | xstr = toWxStr( buf );*/ |
---|
380 | break; |
---|
381 | |
---|
382 | case COL_SENT: |
---|
383 | /*xstr = getReadableSize( s->uploaded );*/ |
---|
384 | break; |
---|
385 | |
---|
386 | case COL_SIZE: |
---|
387 | if( ia->totalSize < ib->totalSize ) ret = -1; |
---|
388 | else if( ia->totalSize > ib->totalSize ) ret = 1; |
---|
389 | else ret = 0; |
---|
390 | break; |
---|
391 | |
---|
392 | case COL_STATE: |
---|
393 | /*xstr = _T("Fixme");*/ |
---|
394 | break; |
---|
395 | |
---|
396 | case COL_STATUS: |
---|
397 | /*xstr = _T("Fixme");*/ |
---|
398 | break; |
---|
399 | |
---|
400 | case COL_TOTAL: |
---|
401 | /*xstr = _T("Fixme");*/ |
---|
402 | break; |
---|
403 | |
---|
404 | case COL_UPLOAD_SPEED: |
---|
405 | /*xstr = getReadableSpeed( s->rateUpload );*/ |
---|
406 | break; |
---|
407 | |
---|
408 | default: |
---|
409 | abort (); |
---|
410 | } |
---|
411 | |
---|
412 | if( sortData < 0 ) |
---|
413 | ret = -ret; |
---|
414 | |
---|
415 | return ret; |
---|
416 | } |
---|
417 | |
---|
418 | void |
---|
419 | TorrentListCtrl :: OnSort( wxListEvent& event ) |
---|
420 | { |
---|
421 | const int_v cols = getTorrentColumns( myConfig ); |
---|
422 | const int key = cols[ event.GetColumn() ]; |
---|
423 | Sort( key ); |
---|
424 | } |
---|
425 | |
---|
426 | void |
---|
427 | TorrentListCtrl :: Sort( int column ) |
---|
428 | { |
---|
429 | if( column == prevSortCol ) |
---|
430 | column = -column; |
---|
431 | prevSortCol = column; |
---|
432 | Resort (); |
---|
433 | } |
---|
434 | |
---|
435 | void |
---|
436 | TorrentListCtrl :: Resort( ) |
---|
437 | { |
---|
438 | uglyHack = &myTorrents; |
---|
439 | |
---|
440 | myConfig->Write( _T("torrent-sort-column"), columnKeys[abs(prevSortCol)] ); |
---|
441 | myConfig->Write( _T("torrent-sort-is-descending"), prevSortCol < 0 ); |
---|
442 | |
---|
443 | SortItems( Compare, prevSortCol ); |
---|
444 | |
---|
445 | const int n = GetItemCount (); |
---|
446 | str2int_t tmp; |
---|
447 | for( int i=0; i<n; ++i ) { |
---|
448 | int idx = GetItemData( i ); |
---|
449 | const tr_info_t* info = tr_torrentInfo( myTorrents[idx] ); |
---|
450 | tmp[info->hashString] = i; |
---|
451 | } |
---|
452 | myHashToRow.swap( tmp ); |
---|
453 | uglyHack = NULL; |
---|
454 | } |
---|
455 | |
---|
456 | void |
---|
457 | TorrentListCtrl :: Refresh () |
---|
458 | { |
---|
459 | const int_v cols = getTorrentColumns( myConfig ); |
---|
460 | const int rowCount = GetItemCount(); |
---|
461 | for( int row=0; row<rowCount; ++row ) |
---|
462 | { |
---|
463 | int array_index = GetItemData( row ); |
---|
464 | tr_torrent_t * tor = myTorrents[array_index]; |
---|
465 | RefreshTorrent( tor, array_index, cols ); |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | void |
---|
470 | TorrentListCtrl :: Repopulate () |
---|
471 | { |
---|
472 | DeleteAllItems(); |
---|
473 | myHashToRow.clear (); |
---|
474 | |
---|
475 | const int_v cols = getTorrentColumns( myConfig ); |
---|
476 | int i = 0; |
---|
477 | for( torrents_t::const_iterator it(myTorrents.begin()), |
---|
478 | end(myTorrents.end()); it!=end; ++it ) |
---|
479 | RefreshTorrent( *it, i++, cols ); |
---|
480 | |
---|
481 | Resort( ); |
---|
482 | } |
---|
483 | |
---|
484 | void |
---|
485 | TorrentListCtrl :: Rebuild() |
---|
486 | { |
---|
487 | ClearAll( ); |
---|
488 | myHashToRow.clear (); |
---|
489 | |
---|
490 | int i = 0; |
---|
491 | const int_v cols = getTorrentColumns( myConfig ); |
---|
492 | for( int_v ::const_iterator it(cols.begin()), end(cols.end()); it!=end; ++it ) |
---|
493 | { |
---|
494 | int format = wxLIST_FORMAT_LEFT; |
---|
495 | int width = -1; |
---|
496 | wxString h; |
---|
497 | |
---|
498 | switch( *it ) |
---|
499 | { |
---|
500 | case COL_POSITION: h = _("#"); format = wxLIST_FORMAT_CENTRE; break; |
---|
501 | case COL_DONE: h = _("Done"); format = wxLIST_FORMAT_RIGHT; break; |
---|
502 | case COL_DOWNLOAD_SPEED: h = _("Download"); break; |
---|
503 | case COL_ETA: h = _("ETA"); format = wxLIST_FORMAT_RIGHT; break; |
---|
504 | case COL_HASH: h = _("SHA1 Hash"); break; |
---|
505 | case COL_NAME: h = _("Name"); width = 500; break; |
---|
506 | case COL_PEERS: h = _("Peers"); format = wxLIST_FORMAT_RIGHT; break; |
---|
507 | case COL_RATIO: h = _("Ratio"); format = wxLIST_FORMAT_RIGHT; break; |
---|
508 | case COL_RECEIVED: h = _("Received"); format = wxLIST_FORMAT_RIGHT; break; |
---|
509 | case COL_REMAINING: h = _("Remaining"); format = wxLIST_FORMAT_RIGHT; break; |
---|
510 | case COL_SEEDS: h = _("Seeds"); format = wxLIST_FORMAT_RIGHT; break; |
---|
511 | case COL_SENT: h = _("Sent"); format = wxLIST_FORMAT_RIGHT; break; |
---|
512 | case COL_SIZE: h = _("Size"); format = wxLIST_FORMAT_RIGHT; break; |
---|
513 | case COL_STATE: h = _("State"); break; |
---|
514 | case COL_STATUS: h = _("Status"); break; |
---|
515 | case COL_TOTAL: h = _("Total"); break; |
---|
516 | case COL_UPLOAD_SPEED: h = _("Upload"); format = wxLIST_FORMAT_RIGHT;break; |
---|
517 | default: h = _("Error"); break; |
---|
518 | } |
---|
519 | |
---|
520 | InsertColumn( i++, h, format, width ); |
---|
521 | } |
---|
522 | |
---|
523 | Repopulate (); |
---|
524 | } |
---|
525 | |
---|
526 | void |
---|
527 | TorrentListCtrl :: Add( const torrents_t& add ) |
---|
528 | { |
---|
529 | myTorrents.insert( myTorrents.end(), add.begin(), add.end() ); |
---|
530 | Repopulate (); |
---|
531 | } |
---|
532 | |
---|
533 | |
---|