2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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.
17
#include "torrent-filter.h"
18
#include "torrent-model.h"
20
TorrentFilter :: TorrentFilter( Prefs& prefs ):
22
myShowMode( SHOW_ALL ),
23
myTextMode( FILTER_BY_NAME ),
24
mySortMode( SORT_BY_ID ),
25
myIsAscending( FALSE )
29
TorrentFilter :: ~TorrentFilter( )
38
TorrentFilter :: setShowMode( int showMode )
40
if( myShowMode != showMode )
42
myShowMode = ShowMode( showMode );
48
TorrentFilter :: setTextMode( int textMode )
50
if( myTextMode != textMode )
52
myTextMode = TextMode( textMode );
58
TorrentFilter :: setText( QString text )
60
QString trimmed = text.trimmed( );
62
if( myText != trimmed )
70
TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
72
QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent );
73
const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value<const Torrent*>();
74
const tr_torrent_activity activity = tor->getActivity( );
83
accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0;
85
case SHOW_DOWNLOADING:
86
accepts = activity == TR_STATUS_DOWNLOAD;
89
accepts = activity == TR_STATUS_SEED;
92
accepts = activity == TR_STATUS_STOPPED;
96
if( accepts && !myText.isEmpty( ) ) switch( myTextMode )
99
accepts = tor->name().contains( myText, Qt::CaseInsensitive );
101
case FILTER_BY_FILES:
102
accepts = tor->hasFileSubstring( myText );
104
case FILTER_BY_TRACKER:
105
accepts = tor->hasTrackerSubstring( myText );
117
TorrentFilter :: getSortKey( int modeIn )
119
switch( modeIn < 0 ? getSortMode( ) : SortMode( modeIn ) )
121
case SORT_BY_ACTIVITY: return "sort-by-activity";
122
case SORT_BY_AGE: return "sort-by-age";
123
case SORT_BY_ETA: return "sort-by-eta";
124
case SORT_BY_PROGRESS: return "sort-by-progress";
125
case SORT_BY_RATIO: return "sort-by-ratio";
126
case SORT_BY_SIZE: return "sort-by-size";
127
case SORT_BY_STATE: return "sort-by-state";
128
case SORT_BY_TRACKER: return "sort-by-tracker";
129
default: return "sort-by-name";
134
TorrentFilter :: resort( )
137
sort( 0, myIsAscending ? Qt::AscendingOrder : Qt::DescendingOrder );
141
TorrentFilter :: setAscending( bool b )
143
if( myIsAscending != b )
151
TorrentFilter :: setSortMode( int sortMode )
153
if( mySortMode != sortMode )
155
myPrefs.set( Prefs :: SORT_MODE, getSortKey( sortMode ) );
156
mySortMode = SortMode( sortMode );
157
setDynamicSortFilter ( true );
162
void TorrentFilter :: sortByActivity ( ) { setSortMode( SORT_BY_ACTIVITY ); }
163
void TorrentFilter :: sortByAge ( ) { setSortMode( SORT_BY_AGE ); }
164
void TorrentFilter :: sortByETA ( ) { setSortMode( SORT_BY_ETA ); }
165
void TorrentFilter :: sortById ( ) { setSortMode( SORT_BY_ID ); }
166
void TorrentFilter :: sortByName ( ) { setSortMode( SORT_BY_NAME ); }
167
void TorrentFilter :: sortByProgress ( ) { setSortMode( SORT_BY_PROGRESS ); }
168
void TorrentFilter :: sortByRatio ( ) { setSortMode( SORT_BY_RATIO ); }
169
void TorrentFilter :: sortBySize ( ) { setSortMode( SORT_BY_SIZE ); }
170
void TorrentFilter :: sortByState ( ) { setSortMode( SORT_BY_STATE ); }
171
void TorrentFilter :: sortByTracker ( ) { setSortMode( SORT_BY_TRACKER ); }
174
TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
176
const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
177
const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
180
switch( getSortMode( ) )
183
less = a->sizeWhenDone() < b->sizeWhenDone();
185
case SORT_BY_ACTIVITY:
186
less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed();
189
less = a->dateAdded() < b->dateAdded();
192
less = a->id() < b->id();
195
less = a->compareRatio( *b ) < 0;
197
case SORT_BY_PROGRESS:
198
less = a->percentDone() < b->percentDone();
201
less = a->compareETA( *b ) < 0;
204
if( a->hasError() != b->hasError() )
205
less = a->hasError();
207
less = a->getActivity() < b->getActivity();
209
case SORT_BY_TRACKER:
210
less = a->compareTracker( *b ) < 0;
213
less = a->name().compare( b->name(), Qt::CaseInsensitive ) > 0;
221
TorrentFilter :: hiddenRowCount( ) const
223
return sourceModel()->rowCount( ) - rowCount( );