2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
#include "filters.h"
|
2010-07-30 22:24:48 +00:00
|
|
|
#include "hig.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrent-filter.h"
|
|
|
|
#include "torrent-model.h"
|
2010-07-30 22:24:48 +00:00
|
|
|
#include "utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
TorrentFilter :: TorrentFilter( Prefs& prefs ):
|
2010-07-30 22:24:48 +00:00
|
|
|
myPrefs( prefs )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-04-11 18:25:12 +00:00
|
|
|
// listen for changes to the preferences to know when to refilter / resort
|
|
|
|
connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int)));
|
|
|
|
|
2009-04-12 21:15:35 +00:00
|
|
|
setDynamicSortFilter( true );
|
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
// initialize our state from the current prefs
|
|
|
|
QList<int> initKeys;
|
|
|
|
initKeys << Prefs :: SORT_MODE
|
2010-07-30 22:24:48 +00:00
|
|
|
<< Prefs :: FILTER_MODE
|
|
|
|
<< Prefs :: FILTER_TRACKERS
|
|
|
|
<< Prefs :: FILTER_TEXT;
|
2009-04-11 18:25:12 +00:00
|
|
|
foreach( int key, initKeys )
|
|
|
|
refreshPref( key );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TorrentFilter :: ~TorrentFilter( )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-11 18:25:12 +00:00
|
|
|
TorrentFilter :: refreshPref( int key )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-04-11 18:25:12 +00:00
|
|
|
switch( key )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2010-07-30 22:24:48 +00:00
|
|
|
case Prefs :: FILTER_TEXT:
|
2010-05-26 05:03:00 +00:00
|
|
|
case Prefs :: FILTER_MODE:
|
2010-07-30 22:24:48 +00:00
|
|
|
case Prefs :: FILTER_TRACKERS:
|
2010-05-26 05:03:00 +00:00
|
|
|
invalidateFilter( );
|
|
|
|
/* force a re-sort */
|
|
|
|
sort( 0, !myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder );
|
2010-07-30 22:24:48 +00:00
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
case Prefs :: SORT_MODE:
|
|
|
|
case Prefs :: SORT_REVERSED:
|
|
|
|
sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder );
|
|
|
|
invalidate( );
|
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-04-07 13:37:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <typename T> int compare( const T a, const T b )
|
|
|
|
{
|
|
|
|
if( a < b ) return -1;
|
|
|
|
if( b < a ) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
bool
|
|
|
|
TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
|
|
|
|
{
|
|
|
|
const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
|
|
|
|
const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
|
2010-04-07 13:37:08 +00:00
|
|
|
int less = 0;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
switch( myPrefs.get<SortMode>(Prefs::SORT_MODE).mode() )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-04-11 18:25:12 +00:00
|
|
|
case SortMode :: SORT_BY_SIZE:
|
2010-04-07 13:37:08 +00:00
|
|
|
less = compare( a->sizeWhenDone(), b->sizeWhenDone() );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
2009-04-11 18:25:12 +00:00
|
|
|
case SortMode :: SORT_BY_ACTIVITY:
|
2010-04-07 13:37:08 +00:00
|
|
|
less = compare( a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed() );
|
|
|
|
if( !less )
|
|
|
|
less = compare( a->uploadedEver(), b->uploadedEver() );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
2009-04-11 18:25:12 +00:00
|
|
|
case SortMode :: SORT_BY_AGE:
|
2010-04-07 13:37:08 +00:00
|
|
|
less = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
2009-04-11 18:25:12 +00:00
|
|
|
case SortMode :: SORT_BY_ID:
|
2010-04-07 13:37:08 +00:00
|
|
|
less = compare( a->id(), b->id() );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
2009-04-11 18:25:12 +00:00
|
|
|
case SortMode :: SORT_BY_STATE:
|
2009-04-09 18:55:47 +00:00
|
|
|
if( a->hasError() != b->hasError() )
|
|
|
|
less = a->hasError();
|
|
|
|
else
|
2010-04-07 13:37:08 +00:00
|
|
|
less = compare( a->getActivity(), b->getActivity() );
|
|
|
|
if( less )
|
|
|
|
break;
|
|
|
|
case SortMode :: SORT_BY_PROGRESS:
|
|
|
|
less = compare( a->percentDone(), b->percentDone() );
|
|
|
|
if( less )
|
|
|
|
break;
|
|
|
|
case SortMode :: SORT_BY_RATIO:
|
|
|
|
less = a->compareRatio( *b );
|
|
|
|
break;
|
|
|
|
case SortMode :: SORT_BY_ETA:
|
|
|
|
less = a->compareETA( *b );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-04-07 13:37:08 +00:00
|
|
|
if( less == 0 )
|
|
|
|
less = -a->name().compare( b->name(), Qt::CaseInsensitive );
|
|
|
|
if( less == 0 )
|
|
|
|
less = compare( a->hashString(), b->hashString() );
|
|
|
|
return less < 0;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
bool
|
2010-07-30 22:24:48 +00:00
|
|
|
TorrentFilter :: trackerFilterAcceptsTorrent( const Torrent * tor, const QString& tracker ) const
|
|
|
|
{
|
|
|
|
return tracker.isEmpty() || tor->hasTrackerSubstring( tracker );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TorrentFilter :: activityFilterAcceptsTorrent( const Torrent * tor, const FilterMode& m ) const
|
2009-04-11 18:25:12 +00:00
|
|
|
{
|
|
|
|
bool accepts;
|
|
|
|
|
2010-07-30 22:24:48 +00:00
|
|
|
switch( m.mode( ) )
|
2009-04-11 18:25:12 +00:00
|
|
|
{
|
|
|
|
case FilterMode::SHOW_ACTIVE:
|
|
|
|
accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( );
|
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_DOWNLOADING:
|
2010-08-01 17:43:35 +00:00
|
|
|
accepts = tor->isDownloading( );
|
2009-04-11 18:25:12 +00:00
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_SEEDING:
|
2010-08-01 17:43:35 +00:00
|
|
|
accepts = tor->isSeeding( );
|
2009-04-11 18:25:12 +00:00
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_PAUSED:
|
2010-08-01 17:43:35 +00:00
|
|
|
accepts = tor->isPaused( );
|
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_FINISHED:
|
|
|
|
accepts = tor->isFinished( );
|
2009-04-11 18:25:12 +00:00
|
|
|
break;
|
2010-07-30 22:24:48 +00:00
|
|
|
case FilterMode::SHOW_QUEUED:
|
2010-08-01 17:43:35 +00:00
|
|
|
accepts = tor->isWaitingToVerify( );
|
2010-07-30 22:24:48 +00:00
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_VERIFYING:
|
2010-08-01 17:43:35 +00:00
|
|
|
accepts = tor->isVerifying( );
|
2010-07-30 22:24:48 +00:00
|
|
|
break;
|
|
|
|
case FilterMode::SHOW_ERROR:
|
|
|
|
accepts = tor->hasError( );
|
|
|
|
break;
|
2010-09-20 13:46:08 +00:00
|
|
|
default: // FilterMode::SHOW_ALL
|
|
|
|
accepts = true;
|
|
|
|
break;
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accepts;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Torrent*>();
|
|
|
|
bool accepts = true;
|
|
|
|
|
|
|
|
if( accepts ) {
|
|
|
|
const FilterMode m = myPrefs.get<FilterMode>(Prefs::FILTER_MODE);
|
|
|
|
accepts = activityFilterAcceptsTorrent( tor, m );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( accepts ) {
|
|
|
|
const QString trackers = myPrefs.getString(Prefs::FILTER_TRACKERS);
|
|
|
|
accepts = trackerFilterAcceptsTorrent( tor, trackers );
|
2009-04-11 18:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-07-30 22:24:48 +00:00
|
|
|
if( accepts ) {
|
|
|
|
const QString text = myPrefs.getString( Prefs::FILTER_TEXT );
|
|
|
|
if( !text.isEmpty( ) )
|
|
|
|
accepts = tor->name().contains( text, Qt::CaseInsensitive );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2009-04-11 18:25:12 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-07-30 22:24:48 +00:00
|
|
|
#endif
|
2009-04-11 18:25:12 +00:00
|
|
|
|
|
|
|
return accepts;
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
int
|
|
|
|
TorrentFilter :: hiddenRowCount( ) const
|
|
|
|
{
|
|
|
|
return sourceModel()->rowCount( ) - rowCount( );
|
|
|
|
}
|
2010-07-30 22:24:48 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
TorrentFilter :: count( const FilterMode& mode ) const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for( int row=0; ; ++row ) {
|
|
|
|
QModelIndex index = sourceModel()->index( row, 0 );
|
|
|
|
if( !index.isValid( ) )
|
|
|
|
break;
|
|
|
|
const Torrent * tor = index.data( TorrentModel::TorrentRole ).value<const Torrent*>();
|
|
|
|
if( activityFilterAcceptsTorrent( tor, mode ) )
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|