source: trunk/wx/filter.cc @ 5457

Last change on this file since 5457 was 5457, checked in by charles, 14 years ago

remove `TR_STATUS_DONE' from libtransmission's public API. It's useful as an internal state but not for code calling libtransmission.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.3 KB
Line 
1/*
2 * Xmission - a cross-platform bittorrent client
3 * Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 * $Id: filter.cc 5457 2008-03-31 17:59:16Z charles $
19 */
20
21#include "foreach.h"
22#include "filter.h"
23
24int
25TorrentFilter :: GetFlags( const tr_torrent * tor )
26{
27    int flags = 0;
28    const tr_stat * s = tr_torrentStat( (tr_torrent*)tor );
29
30    switch( s->status )
31    {
32        case TR_STATUS_DOWNLOAD:
33            flags |= FLAG_LEECHING;
34            break;
35
36        case TR_STATUS_SEED:
37            flags |= FLAG_SEEDING;
38            break;
39
40        case TR_STATUS_STOPPED:
41        case TR_STATUS_CHECK:
42        case TR_STATUS_CHECK_WAIT:
43            break;
44    }
45
46    flags |= ( ( s->rateUpload + s->rateDownload ) > 0.01 )
47        ? FLAG_ACTIVE
48        : FLAG_IDLE;
49
50    flags |= s->leftUntilDone
51        ? FLAG_INCOMPLETE
52        : FLAG_COMPLETE;
53
54    flags |= FLAG_ALL;
55
56    return flags;
57}
58
59void
60TorrentFilter :: CountHits( const torrents_v & torrents,
61                            int              * counts )
62{
63    memset( counts, '\0', sizeof(int) * N_FILTERS );
64    foreach_const( torrents_v, torrents, it ) {
65        const int flags = GetFlags( *it );
66        if( flags & FLAG_ALL )        ++counts[ALL];
67        if( flags & FLAG_LEECHING )   ++counts[LEECHING];
68        if( flags & FLAG_SEEDING )    ++counts[SEEDING];
69        if( flags & FLAG_ACTIVE )     ++counts[ACTIVE];
70        if( flags & FLAG_IDLE )       ++counts[IDLE];
71        if( flags & FLAG_COMPLETE )   ++counts[COMPLETE];
72        if( flags & FLAG_INCOMPLETE ) ++counts[INCOMPLETE];
73    }
74}
75
76wxString
77TorrentFilter :: GetName( int show, int count )
78{
79    static const wxString names[N_FILTERS] = {
80        _("&All"),
81        _("&Complete"),
82        _("&Incomplete"),
83        _("&Seeding"),
84        _("&Leeching"),
85        _("Acti&ve"),
86        _("I&dle")
87    };
88
89    assert( 0<=show && show<N_FILTERS );
90
91    wxString xstr = names[show];
92    if( count )
93        xstr += wxString::Format(_T(" (%d)"), count );
94    return xstr;
95}
96
97void
98TorrentFilter :: RemoveFailures( int            show,
99                                 torrents_v  &  torrents )
100{
101    torrents_v tmp;
102
103    foreach_const( torrents_v, torrents, it ) {
104        const int flags = GetFlags( *it );
105        if(   ( ( show == ALL )        && ( flags & FLAG_ALL ) )
106           || ( ( show == LEECHING )   && ( flags & FLAG_LEECHING ) )
107           || ( ( show == SEEDING )    && ( flags & FLAG_SEEDING ) )
108           || ( ( show == ACTIVE )     && ( flags & FLAG_ACTIVE ) )
109           || ( ( show == IDLE )       && ( flags & FLAG_IDLE ) )
110           || ( ( show == COMPLETE )   && ( flags & FLAG_COMPLETE ) )
111           || ( ( show == INCOMPLETE ) && ( flags & FLAG_INCOMPLETE ) ) )
112            tmp.push_back( *it );
113    }
114
115    torrents.swap( tmp );
116}
Note: See TracBrowser for help on using the repository browser.