1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
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 | * $Id: torrent-filter.cc 10459 2010-04-07 13:37:08Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | #include "filters.h" |
---|
16 | #include "prefs.h" |
---|
17 | #include "torrent.h" |
---|
18 | #include "torrent-filter.h" |
---|
19 | #include "torrent-model.h" |
---|
20 | |
---|
21 | TorrentFilter :: TorrentFilter( Prefs& prefs ): |
---|
22 | myPrefs( prefs ), |
---|
23 | myTextMode( FILTER_BY_NAME ) |
---|
24 | { |
---|
25 | // listen for changes to the preferences to know when to refilter / resort |
---|
26 | connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int))); |
---|
27 | |
---|
28 | setDynamicSortFilter( true ); |
---|
29 | |
---|
30 | // initialize our state from the current prefs |
---|
31 | QList<int> initKeys; |
---|
32 | initKeys << Prefs :: SORT_MODE |
---|
33 | << Prefs :: FILTER_MODE; |
---|
34 | foreach( int key, initKeys ) |
---|
35 | refreshPref( key ); |
---|
36 | } |
---|
37 | |
---|
38 | TorrentFilter :: ~TorrentFilter( ) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | void |
---|
43 | TorrentFilter :: refreshPref( int key ) |
---|
44 | { |
---|
45 | switch( key ) |
---|
46 | { |
---|
47 | case Prefs :: SORT_MODE: |
---|
48 | case Prefs :: SORT_REVERSED: |
---|
49 | sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder ); |
---|
50 | invalidate( ); |
---|
51 | break; |
---|
52 | case Prefs :: FILTER_MODE: |
---|
53 | invalidateFilter( ); |
---|
54 | break; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | /*** |
---|
59 | **** |
---|
60 | ***/ |
---|
61 | |
---|
62 | void |
---|
63 | TorrentFilter :: setTextMode( int i ) |
---|
64 | { |
---|
65 | if( myTextMode != i ) |
---|
66 | { |
---|
67 | myTextMode = TextMode( i ); |
---|
68 | invalidateFilter( ); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void |
---|
73 | TorrentFilter :: setText( QString text ) |
---|
74 | { |
---|
75 | QString trimmed = text.trimmed( ); |
---|
76 | |
---|
77 | if( myText != trimmed ) |
---|
78 | { |
---|
79 | myText = trimmed; |
---|
80 | invalidateFilter( ); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /*** |
---|
85 | **** |
---|
86 | ***/ |
---|
87 | |
---|
88 | namespace |
---|
89 | { |
---|
90 | template <typename T> int compare( const T a, const T b ) |
---|
91 | { |
---|
92 | if( a < b ) return -1; |
---|
93 | if( b < a ) return 1; |
---|
94 | return 0; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | bool |
---|
99 | TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const |
---|
100 | { |
---|
101 | const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
102 | const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
103 | int less = 0; |
---|
104 | |
---|
105 | switch( myPrefs.get<SortMode>(Prefs::SORT_MODE).mode() ) |
---|
106 | { |
---|
107 | case SortMode :: SORT_BY_SIZE: |
---|
108 | less = compare( a->sizeWhenDone(), b->sizeWhenDone() ); |
---|
109 | break; |
---|
110 | case SortMode :: SORT_BY_ACTIVITY: |
---|
111 | less = compare( a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed() ); |
---|
112 | if( !less ) |
---|
113 | less = compare( a->uploadedEver(), b->uploadedEver() ); |
---|
114 | break; |
---|
115 | case SortMode :: SORT_BY_AGE: |
---|
116 | less = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() ); |
---|
117 | break; |
---|
118 | case SortMode :: SORT_BY_ID: |
---|
119 | less = compare( a->id(), b->id() ); |
---|
120 | break; |
---|
121 | case SortMode :: SORT_BY_STATE: |
---|
122 | if( a->hasError() != b->hasError() ) |
---|
123 | less = a->hasError(); |
---|
124 | else |
---|
125 | less = compare( a->getActivity(), b->getActivity() ); |
---|
126 | if( less ) |
---|
127 | break; |
---|
128 | case SortMode :: SORT_BY_PROGRESS: |
---|
129 | less = compare( a->percentDone(), b->percentDone() ); |
---|
130 | if( less ) |
---|
131 | break; |
---|
132 | case SortMode :: SORT_BY_RATIO: |
---|
133 | less = a->compareRatio( *b ); |
---|
134 | break; |
---|
135 | case SortMode :: SORT_BY_ETA: |
---|
136 | less = a->compareETA( *b ); |
---|
137 | break; |
---|
138 | case SortMode :: SORT_BY_TRACKER: |
---|
139 | less = a->compareTracker( *b ); |
---|
140 | break; |
---|
141 | default: |
---|
142 | break; |
---|
143 | } |
---|
144 | if( less == 0 ) |
---|
145 | less = -a->name().compare( b->name(), Qt::CaseInsensitive ); |
---|
146 | if( less == 0 ) |
---|
147 | less = compare( a->hashString(), b->hashString() ); |
---|
148 | return less < 0; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /*** |
---|
153 | **** |
---|
154 | ***/ |
---|
155 | |
---|
156 | bool |
---|
157 | TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const |
---|
158 | { |
---|
159 | QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent ); |
---|
160 | const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
161 | const tr_torrent_activity activity = tor->getActivity( ); |
---|
162 | bool accepts; |
---|
163 | |
---|
164 | switch( myPrefs.get<FilterMode>(Prefs::FILTER_MODE).mode() ) |
---|
165 | { |
---|
166 | case FilterMode::SHOW_ALL: |
---|
167 | accepts = true; |
---|
168 | break; |
---|
169 | case FilterMode::SHOW_ACTIVE: |
---|
170 | accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( ); |
---|
171 | break; |
---|
172 | case FilterMode::SHOW_DOWNLOADING: |
---|
173 | accepts = activity == TR_STATUS_DOWNLOAD; |
---|
174 | break; |
---|
175 | case FilterMode::SHOW_SEEDING: |
---|
176 | accepts = activity == TR_STATUS_SEED; |
---|
177 | break; |
---|
178 | case FilterMode::SHOW_PAUSED: |
---|
179 | accepts = activity == TR_STATUS_STOPPED; |
---|
180 | break; |
---|
181 | } |
---|
182 | |
---|
183 | if( accepts && !myText.isEmpty( ) ) switch( myTextMode ) |
---|
184 | { |
---|
185 | case FILTER_BY_NAME: |
---|
186 | accepts = tor->name().contains( myText, Qt::CaseInsensitive ); |
---|
187 | break; |
---|
188 | case FILTER_BY_FILES: |
---|
189 | accepts = tor->hasFileSubstring( myText ); |
---|
190 | break; |
---|
191 | case FILTER_BY_TRACKER: |
---|
192 | accepts = tor->hasTrackerSubstring( myText ); |
---|
193 | break; |
---|
194 | } |
---|
195 | |
---|
196 | return accepts; |
---|
197 | } |
---|
198 | |
---|
199 | int |
---|
200 | TorrentFilter :: hiddenRowCount( ) const |
---|
201 | { |
---|
202 | return sourceModel()->rowCount( ) - rowCount( ); |
---|
203 | } |
---|