/* * This file Copyright (C) 2009 Charles Kerr * * This file is licensed by the GPL version 2. Works owned by the * Transmission project are granted a special exemption to clause 2(b) * so that the bulk of its code can remain under the MIT license. * This exemption does not extend to derived works not owned by * the Transmission project. * * $Id:$ */ #include #include "prefs.h" #include "torrent.h" #include "torrent-filter.h" #include "torrent-model.h" TorrentFilter :: TorrentFilter( Prefs& prefs ): myPrefs( prefs ), myShowMode( getShowModeFromName( prefs.getString( Prefs::FILTER_MODE ) ) ), myTextMode( FILTER_BY_NAME ), mySortMode( getSortModeFromName( prefs.getString( Prefs::SORT_MODE ) ) ), myIsAscending( prefs.getBool( Prefs::SORT_REVERSED ) ) { resort( ); } TorrentFilter :: ~TorrentFilter( ) { } /*** **** ***/ void TorrentFilter :: setShowMode( int showMode ) { if( myShowMode != showMode ) { myPrefs.set( Prefs :: FILTER_MODE, getShowName( showMode ) ); myShowMode = ShowMode( showMode ); invalidateFilter( ); } } void TorrentFilter :: setTextMode( int textMode ) { if( myTextMode != textMode ) { myTextMode = TextMode( textMode ); invalidateFilter( ); } } void TorrentFilter :: setText( QString text ) { QString trimmed = text.trimmed( ); if( myText != trimmed ) { myText = trimmed; invalidateFilter( ); } } bool TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const { QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent ); const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value(); const tr_torrent_activity activity = tor->getActivity( ); bool accepts; switch( myShowMode ) { case SHOW_ALL: accepts = true; break; case SHOW_ACTIVE: accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( ); break; case SHOW_DOWNLOADING: accepts = activity == TR_STATUS_DOWNLOAD; break; case SHOW_SEEDING: accepts = activity == TR_STATUS_SEED; break; case SHOW_PAUSED: accepts = activity == TR_STATUS_STOPPED; break; } if( accepts && !myText.isEmpty( ) ) switch( myTextMode ) { case FILTER_BY_NAME: accepts = tor->name().contains( myText, Qt::CaseInsensitive ); break; case FILTER_BY_FILES: accepts = tor->hasFileSubstring( myText ); break; case FILTER_BY_TRACKER: accepts = tor->hasTrackerSubstring( myText ); break; } return accepts; } /*** **** ***/ namespace { struct NameAndNum { const char * name; int num; }; const struct NameAndNum showModes[] = { { "show-all", TorrentFilter::SHOW_ALL }, { "show-active", TorrentFilter::SHOW_ACTIVE }, { "show-downloading", TorrentFilter::SHOW_DOWNLOADING }, { "show-seeding", TorrentFilter::SHOW_SEEDING }, { "show-paused", TorrentFilter::SHOW_PAUSED } }; const int showModeCount = sizeof(showModes) / sizeof(showModes[0]); const struct NameAndNum sortModes[] = { { "sort-by-name", TorrentFilter::SORT_BY_NAME }, { "sort-by-activity", TorrentFilter::SORT_BY_ACTIVITY }, { "sort-by-age", TorrentFilter::SORT_BY_AGE }, { "sort-by-eta", TorrentFilter::SORT_BY_ETA }, { "sort-by-progress", TorrentFilter::SORT_BY_PROGRESS }, { "sort-by-ratio", TorrentFilter::SORT_BY_RATIO }, { "sort-by-size", TorrentFilter::SORT_BY_SIZE }, { "sort-by-state", TorrentFilter::SORT_BY_STATE }, { "sort-by-tracker", TorrentFilter::SORT_BY_TRACKER } }; const int sortModeCount = sizeof(sortModes) / sizeof(sortModes[0]); int getNum( const struct NameAndNum * rows, int numRows, const QString& name ) { for( int i=0; idata( left, TorrentModel::TorrentRole ).value(); const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value(); bool less; switch( getSortMode( ) ) { case SORT_BY_SIZE: less = a->sizeWhenDone() < b->sizeWhenDone(); break; case SORT_BY_ACTIVITY: less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed(); break; case SORT_BY_AGE: less = a->dateAdded() < b->dateAdded(); break; case SORT_BY_ID: less = a->id() < b->id(); break; case SORT_BY_RATIO: less = a->compareRatio( *b ) < 0; break; case SORT_BY_PROGRESS: less = a->percentDone() < b->percentDone(); break; case SORT_BY_ETA: less = a->compareETA( *b ) < 0; break; case SORT_BY_STATE: if( a->hasError() != b->hasError() ) less = a->hasError(); else less = a->getActivity() < b->getActivity(); break; case SORT_BY_TRACKER: less = a->compareTracker( *b ) < 0; break; default: less = a->name().compare( b->name(), Qt::CaseInsensitive ) > 0; break; } return less; } int TorrentFilter :: hiddenRowCount( ) const { return sourceModel()->rowCount( ) - rowCount( ); }