2 * This file Copyright (C) Mnemosyne LLC
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.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: torrent-filter.cc 11446 2010-12-01 05:41:58Z charles $
19 #include "torrent-filter.h"
20 #include "torrent-model.h"
23 TorrentFilter :: TorrentFilter( Prefs
& prefs
):
26 // listen for changes to the preferences to know when to refilter / resort
27 connect( &myPrefs
, SIGNAL(changed(int)), this, SLOT(refreshPref(int)));
29 setDynamicSortFilter( true );
31 // initialize our state from the current prefs
33 initKeys
<< Prefs :: SORT_MODE
34 << Prefs :: FILTER_MODE
35 << Prefs :: FILTER_TRACKERS
36 << Prefs :: FILTER_TEXT
;
37 foreach( int key
, initKeys
)
41 TorrentFilter :: ~TorrentFilter( )
46 TorrentFilter :: refreshPref( int key
)
50 case Prefs :: FILTER_TEXT
:
51 case Prefs :: FILTER_MODE
:
52 case Prefs :: FILTER_TRACKERS
:
55 sort( 0, !myPrefs
.getBool(Prefs::SORT_REVERSED
) ? Qt::AscendingOrder
: Qt::DescendingOrder
);
57 case Prefs :: SORT_MODE
:
58 case Prefs :: SORT_REVERSED
:
59 sort( 0, myPrefs
.getBool(Prefs::SORT_REVERSED
) ? Qt::AscendingOrder
: Qt::DescendingOrder
);
71 template <typename T
> int compare( const T a
, const T b
)
73 if( a
< b
) return -1;
80 TorrentFilter :: lessThan( const QModelIndex
& left
, const QModelIndex
& right
) const
83 const Torrent
* a
= sourceModel()->data( left
, TorrentModel::TorrentRole
).value
<const Torrent
*>();
84 const Torrent
* b
= sourceModel()->data( right
, TorrentModel::TorrentRole
).value
<const Torrent
*>();
86 switch( myPrefs
.get
<SortMode
>(Prefs::SORT_MODE
).mode() )
88 case SortMode :: SORT_BY_SIZE
:
89 if( !val
) val
= compare( a
->sizeWhenDone(), b
->sizeWhenDone() );
91 case SortMode :: SORT_BY_ACTIVITY
:
92 if( !val
) val
= compare( a
->downloadSpeed() + a
->uploadSpeed(), b
->downloadSpeed() + b
->uploadSpeed() );
93 if( !val
) val
= compare( a
->uploadedEver(), b
->uploadedEver() );
95 case SortMode :: SORT_BY_AGE
:
96 val
= compare( a
->dateAdded().toTime_t(), b
->dateAdded().toTime_t() );
98 case SortMode :: SORT_BY_ID
:
99 if( !val
) val
= compare( a
->id(), b
->id() );
101 case SortMode :: SORT_BY_STATE
:
102 if( !val
) val
= compare( a
->hasError(), b
->hasError() );
103 if( !val
) val
= compare( a
->getActivity(), b
->getActivity() );
105 case SortMode :: SORT_BY_PROGRESS
:
106 if( !val
) val
= compare( a
->percentComplete(), b
->percentComplete() );
107 if( !val
) val
= a
->compareSeedRatio( *b
);
109 case SortMode :: SORT_BY_RATIO
:
110 if( !val
) val
= a
->compareRatio( *b
);
112 case SortMode :: SORT_BY_ETA
:
113 if( !val
) val
= a
->compareETA( *b
);
119 val
= -a
->name().compare( b
->name(), Qt::CaseInsensitive
);
121 val
= compare( a
->hashString(), b
->hashString() );
131 TorrentFilter :: trackerFilterAcceptsTorrent( const Torrent
* tor
, const QString
& tracker
) const
133 return tracker
.isEmpty() || tor
->hasTrackerSubstring( tracker
);
137 TorrentFilter :: activityFilterAcceptsTorrent( const Torrent
* tor
, const FilterMode
& m
) const
143 case FilterMode::SHOW_ACTIVE
:
144 accepts
= tor
->peersWeAreUploadingTo( ) > 0 || tor
->peersWeAreDownloadingFrom( ) > 0 || tor
->isVerifying( );
146 case FilterMode::SHOW_DOWNLOADING
:
147 accepts
= tor
->isDownloading( );
149 case FilterMode::SHOW_SEEDING
:
150 accepts
= tor
->isSeeding( );
152 case FilterMode::SHOW_PAUSED
:
153 accepts
= tor
->isPaused( );
155 case FilterMode::SHOW_FINISHED
:
156 accepts
= tor
->isFinished( );
158 case FilterMode::SHOW_QUEUED
:
159 accepts
= tor
->isWaitingToVerify( );
161 case FilterMode::SHOW_VERIFYING
:
162 accepts
= tor
->isVerifying( );
164 case FilterMode::SHOW_ERROR
:
165 accepts
= tor
->hasError( );
167 default: // FilterMode::SHOW_ALL
176 TorrentFilter :: filterAcceptsRow( int sourceRow
, const QModelIndex
& sourceParent
) const
178 QModelIndex childIndex
= sourceModel()->index( sourceRow
, 0, sourceParent
);
179 const Torrent
* tor
= childIndex
.model()->data( childIndex
, TorrentModel::TorrentRole
).value
<const Torrent
*>();
183 const FilterMode m
= myPrefs
.get
<FilterMode
>(Prefs::FILTER_MODE
);
184 accepts
= activityFilterAcceptsTorrent( tor
, m
);
188 const QString trackers
= myPrefs
.getString(Prefs::FILTER_TRACKERS
);
189 accepts
= trackerFilterAcceptsTorrent( tor
, trackers
);
193 const QString text
= myPrefs
.getString( Prefs::FILTER_TEXT
);
194 if( !text
.isEmpty( ) )
195 accepts
= tor
->name().contains( text
, Qt::CaseInsensitive
);
199 if( accepts
&& !myText
.isEmpty( ) ) switch( myTextMode
)
202 accepts
= tor
->name().contains( myText
, Qt::CaseInsensitive
);
204 case FILTER_BY_FILES
:
205 accepts
= tor
->hasFileSubstring( myText
);
207 case FILTER_BY_TRACKER
:
208 accepts
= tor
->hasTrackerSubstring( myText
);
217 TorrentFilter :: hiddenRowCount( ) const
219 return sourceModel()->rowCount( ) - rowCount( );
223 TorrentFilter :: count( const FilterMode
& mode
) const
227 for( int row
=0; ; ++row
) {
228 QModelIndex index
= sourceModel()->index( row
, 0 );
229 if( !index
.isValid( ) )
231 const Torrent
* tor
= index
.data( TorrentModel::TorrentRole
).value
<const Torrent
*>();
232 if( activityFilterAcceptsTorrent( tor
, mode
) )