1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License version 2 |
---|
6 | * as published by the Free Software Foundation. |
---|
7 | * |
---|
8 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | * |
---|
10 | * $Id: torrent-filter.cc 11240 2010-09-20 13:46:08Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | #include "filters.h" |
---|
16 | #include "hig.h" |
---|
17 | #include "prefs.h" |
---|
18 | #include "torrent.h" |
---|
19 | #include "torrent-filter.h" |
---|
20 | #include "torrent-model.h" |
---|
21 | #include "utils.h" |
---|
22 | |
---|
23 | TorrentFilter :: TorrentFilter( Prefs& prefs ): |
---|
24 | myPrefs( prefs ) |
---|
25 | { |
---|
26 | // listen for changes to the preferences to know when to refilter / resort |
---|
27 | connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int))); |
---|
28 | |
---|
29 | setDynamicSortFilter( true ); |
---|
30 | |
---|
31 | // initialize our state from the current prefs |
---|
32 | QList<int> initKeys; |
---|
33 | initKeys << Prefs :: SORT_MODE |
---|
34 | << Prefs :: FILTER_MODE |
---|
35 | << Prefs :: FILTER_TRACKERS |
---|
36 | << Prefs :: FILTER_TEXT; |
---|
37 | foreach( int key, initKeys ) |
---|
38 | refreshPref( key ); |
---|
39 | } |
---|
40 | |
---|
41 | TorrentFilter :: ~TorrentFilter( ) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | void |
---|
46 | TorrentFilter :: refreshPref( int key ) |
---|
47 | { |
---|
48 | switch( key ) |
---|
49 | { |
---|
50 | case Prefs :: FILTER_TEXT: |
---|
51 | case Prefs :: FILTER_MODE: |
---|
52 | case Prefs :: FILTER_TRACKERS: |
---|
53 | invalidateFilter( ); |
---|
54 | /* force a re-sort */ |
---|
55 | sort( 0, !myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder ); |
---|
56 | |
---|
57 | case Prefs :: SORT_MODE: |
---|
58 | case Prefs :: SORT_REVERSED: |
---|
59 | sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder ); |
---|
60 | invalidate( ); |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /*** |
---|
66 | **** |
---|
67 | ***/ |
---|
68 | |
---|
69 | namespace |
---|
70 | { |
---|
71 | template <typename T> int compare( const T a, const T b ) |
---|
72 | { |
---|
73 | if( a < b ) return -1; |
---|
74 | if( b < a ) return 1; |
---|
75 | return 0; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | bool |
---|
80 | TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const |
---|
81 | { |
---|
82 | const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
83 | const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
84 | int less = 0; |
---|
85 | |
---|
86 | switch( myPrefs.get<SortMode>(Prefs::SORT_MODE).mode() ) |
---|
87 | { |
---|
88 | case SortMode :: SORT_BY_SIZE: |
---|
89 | less = compare( a->sizeWhenDone(), b->sizeWhenDone() ); |
---|
90 | break; |
---|
91 | case SortMode :: SORT_BY_ACTIVITY: |
---|
92 | less = compare( a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed() ); |
---|
93 | if( !less ) |
---|
94 | less = compare( a->uploadedEver(), b->uploadedEver() ); |
---|
95 | break; |
---|
96 | case SortMode :: SORT_BY_AGE: |
---|
97 | less = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() ); |
---|
98 | break; |
---|
99 | case SortMode :: SORT_BY_ID: |
---|
100 | less = compare( a->id(), b->id() ); |
---|
101 | break; |
---|
102 | case SortMode :: SORT_BY_STATE: |
---|
103 | if( a->hasError() != b->hasError() ) |
---|
104 | less = a->hasError(); |
---|
105 | else |
---|
106 | less = compare( a->getActivity(), b->getActivity() ); |
---|
107 | if( less ) |
---|
108 | break; |
---|
109 | case SortMode :: SORT_BY_PROGRESS: |
---|
110 | less = compare( a->percentDone(), b->percentDone() ); |
---|
111 | if( less ) |
---|
112 | break; |
---|
113 | case SortMode :: SORT_BY_RATIO: |
---|
114 | less = a->compareRatio( *b ); |
---|
115 | break; |
---|
116 | case SortMode :: SORT_BY_ETA: |
---|
117 | less = a->compareETA( *b ); |
---|
118 | break; |
---|
119 | default: |
---|
120 | break; |
---|
121 | } |
---|
122 | if( less == 0 ) |
---|
123 | less = -a->name().compare( b->name(), Qt::CaseInsensitive ); |
---|
124 | if( less == 0 ) |
---|
125 | less = compare( a->hashString(), b->hashString() ); |
---|
126 | return less < 0; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /*** |
---|
131 | **** |
---|
132 | ***/ |
---|
133 | |
---|
134 | bool |
---|
135 | TorrentFilter :: trackerFilterAcceptsTorrent( const Torrent * tor, const QString& tracker ) const |
---|
136 | { |
---|
137 | return tracker.isEmpty() || tor->hasTrackerSubstring( tracker ); |
---|
138 | } |
---|
139 | |
---|
140 | bool |
---|
141 | TorrentFilter :: activityFilterAcceptsTorrent( const Torrent * tor, const FilterMode& m ) const |
---|
142 | { |
---|
143 | bool accepts; |
---|
144 | |
---|
145 | switch( m.mode( ) ) |
---|
146 | { |
---|
147 | case FilterMode::SHOW_ACTIVE: |
---|
148 | accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( ); |
---|
149 | break; |
---|
150 | case FilterMode::SHOW_DOWNLOADING: |
---|
151 | accepts = tor->isDownloading( ); |
---|
152 | break; |
---|
153 | case FilterMode::SHOW_SEEDING: |
---|
154 | accepts = tor->isSeeding( ); |
---|
155 | break; |
---|
156 | case FilterMode::SHOW_PAUSED: |
---|
157 | accepts = tor->isPaused( ); |
---|
158 | break; |
---|
159 | case FilterMode::SHOW_FINISHED: |
---|
160 | accepts = tor->isFinished( ); |
---|
161 | break; |
---|
162 | case FilterMode::SHOW_QUEUED: |
---|
163 | accepts = tor->isWaitingToVerify( ); |
---|
164 | break; |
---|
165 | case FilterMode::SHOW_VERIFYING: |
---|
166 | accepts = tor->isVerifying( ); |
---|
167 | break; |
---|
168 | case FilterMode::SHOW_ERROR: |
---|
169 | accepts = tor->hasError( ); |
---|
170 | break; |
---|
171 | default: // FilterMode::SHOW_ALL |
---|
172 | accepts = true; |
---|
173 | break; |
---|
174 | } |
---|
175 | |
---|
176 | return accepts; |
---|
177 | } |
---|
178 | |
---|
179 | bool |
---|
180 | TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const |
---|
181 | { |
---|
182 | QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent ); |
---|
183 | const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
184 | bool accepts = true; |
---|
185 | |
---|
186 | if( accepts ) { |
---|
187 | const FilterMode m = myPrefs.get<FilterMode>(Prefs::FILTER_MODE); |
---|
188 | accepts = activityFilterAcceptsTorrent( tor, m ); |
---|
189 | } |
---|
190 | |
---|
191 | if( accepts ) { |
---|
192 | const QString trackers = myPrefs.getString(Prefs::FILTER_TRACKERS); |
---|
193 | accepts = trackerFilterAcceptsTorrent( tor, trackers ); |
---|
194 | } |
---|
195 | |
---|
196 | if( accepts ) { |
---|
197 | const QString text = myPrefs.getString( Prefs::FILTER_TEXT ); |
---|
198 | if( !text.isEmpty( ) ) |
---|
199 | accepts = tor->name().contains( text, Qt::CaseInsensitive ); |
---|
200 | } |
---|
201 | |
---|
202 | #if 0 |
---|
203 | if( accepts && !myText.isEmpty( ) ) switch( myTextMode ) |
---|
204 | { |
---|
205 | case FILTER_BY_NAME: |
---|
206 | accepts = tor->name().contains( myText, Qt::CaseInsensitive ); |
---|
207 | break; |
---|
208 | case FILTER_BY_FILES: |
---|
209 | accepts = tor->hasFileSubstring( myText ); |
---|
210 | break; |
---|
211 | case FILTER_BY_TRACKER: |
---|
212 | accepts = tor->hasTrackerSubstring( myText ); |
---|
213 | break; |
---|
214 | } |
---|
215 | #endif |
---|
216 | |
---|
217 | return accepts; |
---|
218 | } |
---|
219 | |
---|
220 | int |
---|
221 | TorrentFilter :: hiddenRowCount( ) const |
---|
222 | { |
---|
223 | return sourceModel()->rowCount( ) - rowCount( ); |
---|
224 | } |
---|
225 | |
---|
226 | int |
---|
227 | TorrentFilter :: count( const FilterMode& mode ) const |
---|
228 | { |
---|
229 | int count = 0; |
---|
230 | |
---|
231 | for( int row=0; ; ++row ) { |
---|
232 | QModelIndex index = sourceModel()->index( row, 0 ); |
---|
233 | if( !index.isValid( ) ) |
---|
234 | break; |
---|
235 | const Torrent * tor = index.data( TorrentModel::TorrentRole ).value<const Torrent*>(); |
---|
236 | if( activityFilterAcceptsTorrent( tor, mode ) ) |
---|
237 | ++count; |
---|
238 | } |
---|
239 | |
---|
240 | return count; |
---|
241 | } |
---|