Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / qt / torrent-filter.cc
blobbb3e98948285638cacd24c226012c8ad13b5878c
1 /*
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 $
13 #include <iostream>
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"
23 TorrentFilter :: TorrentFilter( Prefs& prefs ):
24 myPrefs( 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
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 );
41 TorrentFilter :: ~TorrentFilter( )
45 void
46 TorrentFilter :: refreshPref( int key )
48 switch( key )
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 );
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;
65 /***
66 ****
67 ***/
69 namespace
71 template <typename T> int compare( const T a, const T b )
73 if( a < b ) return -1;
74 if( b < a ) return 1;
75 return 0;
79 bool
80 TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
82 int val = 0;
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() );
90 break;
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() );
94 break;
95 case SortMode :: SORT_BY_AGE:
96 val = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() );
97 break;
98 case SortMode :: SORT_BY_ID:
99 if( !val ) val = compare( a->id(), b->id() );
100 break;
101 case SortMode :: SORT_BY_STATE:
102 if( !val ) val = compare( a->hasError(), b->hasError() );
103 if( !val ) val = compare( a->getActivity(), b->getActivity() );
104 // fall through
105 case SortMode :: SORT_BY_PROGRESS:
106 if( !val ) val = compare( a->percentComplete(), b->percentComplete() );
107 if( !val ) val = a->compareSeedRatio( *b );
108 // fall through
109 case SortMode :: SORT_BY_RATIO:
110 if( !val ) val = a->compareRatio( *b );
111 break;
112 case SortMode :: SORT_BY_ETA:
113 if( !val ) val = a->compareETA( *b );
114 break;
115 default:
116 break;
118 if( val == 0 )
119 val = -a->name().compare( b->name(), Qt::CaseInsensitive );
120 if( val == 0 )
121 val = compare( a->hashString(), b->hashString() );
122 return val < 0;
126 /***
127 ****
128 ***/
130 bool
131 TorrentFilter :: trackerFilterAcceptsTorrent( const Torrent * tor, const QString& tracker ) const
133 return tracker.isEmpty() || tor->hasTrackerSubstring( tracker );
136 bool
137 TorrentFilter :: activityFilterAcceptsTorrent( const Torrent * tor, const FilterMode& m ) const
139 bool accepts;
141 switch( m.mode( ) )
143 case FilterMode::SHOW_ACTIVE:
144 accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( );
145 break;
146 case FilterMode::SHOW_DOWNLOADING:
147 accepts = tor->isDownloading( );
148 break;
149 case FilterMode::SHOW_SEEDING:
150 accepts = tor->isSeeding( );
151 break;
152 case FilterMode::SHOW_PAUSED:
153 accepts = tor->isPaused( );
154 break;
155 case FilterMode::SHOW_FINISHED:
156 accepts = tor->isFinished( );
157 break;
158 case FilterMode::SHOW_QUEUED:
159 accepts = tor->isWaitingToVerify( );
160 break;
161 case FilterMode::SHOW_VERIFYING:
162 accepts = tor->isVerifying( );
163 break;
164 case FilterMode::SHOW_ERROR:
165 accepts = tor->hasError( );
166 break;
167 default: // FilterMode::SHOW_ALL
168 accepts = true;
169 break;
172 return accepts;
175 bool
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*>();
180 bool accepts = true;
182 if( accepts ) {
183 const FilterMode m = myPrefs.get<FilterMode>(Prefs::FILTER_MODE);
184 accepts = activityFilterAcceptsTorrent( tor, m );
187 if( accepts ) {
188 const QString trackers = myPrefs.getString(Prefs::FILTER_TRACKERS);
189 accepts = trackerFilterAcceptsTorrent( tor, trackers );
192 if( accepts ) {
193 const QString text = myPrefs.getString( Prefs::FILTER_TEXT );
194 if( !text.isEmpty( ) )
195 accepts = tor->name().contains( text, Qt::CaseInsensitive );
198 #if 0
199 if( accepts && !myText.isEmpty( ) ) switch( myTextMode )
201 case FILTER_BY_NAME:
202 accepts = tor->name().contains( myText, Qt::CaseInsensitive );
203 break;
204 case FILTER_BY_FILES:
205 accepts = tor->hasFileSubstring( myText );
206 break;
207 case FILTER_BY_TRACKER:
208 accepts = tor->hasTrackerSubstring( myText );
209 break;
211 #endif
213 return accepts;
217 TorrentFilter :: hiddenRowCount( ) const
219 return sourceModel()->rowCount( ) - rowCount( );
223 TorrentFilter :: count( const FilterMode& mode ) const
225 int count = 0;
227 for( int row=0; ; ++row ) {
228 QModelIndex index = sourceModel()->index( row, 0 );
229 if( !index.isValid( ) )
230 break;
231 const Torrent * tor = index.data( TorrentModel::TorrentRole ).value<const Torrent*>();
232 if( activityFilterAcceptsTorrent( tor, mode ) )
233 ++count;
236 return count;