2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-30 01:28:28 +00:00
|
|
|
#include <algorithm>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Filters.h"
|
|
|
|
#include "Prefs.h"
|
|
|
|
#include "Torrent.h"
|
|
|
|
#include "TorrentFilter.h"
|
|
|
|
#include "TorrentModel.h"
|
|
|
|
#include "Utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-01-29 22:44:43 +00:00
|
|
|
TorrentFilter::TorrentFilter (const Prefs& prefs):
|
2013-09-14 22:45:04 +00:00
|
|
|
myPrefs (prefs)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// listen for changes to the preferences to know when to refilter / resort
|
|
|
|
connect (&myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int)));
|
|
|
|
|
|
|
|
setDynamicSortFilter (true);
|
|
|
|
|
|
|
|
// initialize our state from the current prefs
|
|
|
|
QList<int> initKeys;
|
2014-12-12 23:05:10 +00:00
|
|
|
initKeys << Prefs::SORT_MODE
|
|
|
|
<< Prefs::FILTER_MODE
|
|
|
|
<< Prefs::FILTER_TRACKERS
|
|
|
|
<< Prefs::FILTER_TEXT;
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const int key: initKeys)
|
2013-09-14 22:45:04 +00:00
|
|
|
refreshPref (key);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::~TorrentFilter ()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::refreshPref (int key)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (key)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-12 23:05:10 +00:00
|
|
|
case Prefs::FILTER_TEXT:
|
|
|
|
case Prefs::FILTER_MODE:
|
|
|
|
case Prefs::FILTER_TRACKERS:
|
2013-09-14 22:45:04 +00:00
|
|
|
invalidateFilter ();
|
|
|
|
/* force a re-sort */
|
|
|
|
sort (0, !myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder);
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case Prefs::SORT_MODE:
|
|
|
|
case Prefs::SORT_REVERSED:
|
2013-09-14 22:45:04 +00:00
|
|
|
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
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
template <typename T> int compare (const T a, const T b)
|
2010-04-07 13:37:08 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
if (a < b)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (b < a)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2010-04-07 13:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::lessThan (const QModelIndex& left, const QModelIndex& right) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
int val = 0;
|
|
|
|
const Torrent * a = sourceModel()->data (left, TorrentModel::TorrentRole).value<const Torrent*>();
|
|
|
|
const Torrent * b = sourceModel()->data (right, TorrentModel::TorrentRole).value<const Torrent*>();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (myPrefs.get<SortMode>(Prefs::SORT_MODE).mode())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_QUEUE:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = -compare (a->queuePosition(), b->queuePosition());
|
|
|
|
break;
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_SIZE:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = compare (a->sizeWhenDone(), b->sizeWhenDone());
|
|
|
|
break;
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_AGE:
|
2013-09-14 22:45:04 +00:00
|
|
|
val = compare (a->dateAdded().toTime_t(), b->dateAdded().toTime_t());
|
|
|
|
break;
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_ID:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = compare (a->id(), b->id());
|
|
|
|
break;
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_ACTIVITY:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = compare (a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed());
|
|
|
|
if (!val)
|
|
|
|
val = compare (a->peersWeAreUploadingTo() + a->webseedsWeAreDownloadingFrom(),
|
|
|
|
b->peersWeAreUploadingTo() + b->webseedsWeAreDownloadingFrom());
|
|
|
|
// fall through
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_STATE:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = -compare (a->isPaused(), b->isPaused());
|
|
|
|
if (!val)
|
|
|
|
val = compare (a->getActivity(), b->getActivity());
|
|
|
|
if (!val)
|
|
|
|
val = -compare (a->queuePosition(), b->queuePosition());
|
|
|
|
if (!val)
|
|
|
|
val = compare (a->hasError(), b->hasError());
|
|
|
|
// fall through
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_PROGRESS:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = compare (a->percentComplete(), b->percentComplete());
|
|
|
|
if (!val)
|
|
|
|
val = a->compareSeedRatio (*b);
|
|
|
|
if (!val)
|
|
|
|
val = -compare (a->queuePosition(), b->queuePosition());
|
2015-05-09 08:37:55 +00:00
|
|
|
// fall through
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_RATIO:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = a->compareRatio (*b);
|
|
|
|
break;
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
case SortMode::SORT_BY_ETA:
|
2013-09-14 22:45:04 +00:00
|
|
|
if (!val)
|
|
|
|
val = a->compareETA (*b);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
val = -a->name().compare (b->name(), Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
val = compare (a->hashString(), b->hashString());
|
|
|
|
|
|
|
|
return val < 0;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-11 18:25:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::trackerFilterAcceptsTorrent (const Torrent * tor, const QString& tracker) const
|
2010-07-30 22:24:48 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
return tracker.isEmpty() || tor->hasTrackerSubstring (tracker);
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::activityFilterAcceptsTorrent (const Torrent * tor, const FilterMode& m) const
|
2009-04-11 18:25:12 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
bool accepts;
|
2009-04-11 18:25:12 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (m.mode ())
|
2009-04-11 18:25:12 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
case FilterMode::SHOW_ACTIVE:
|
|
|
|
accepts = tor->peersWeAreUploadingTo () > 0 || tor->peersWeAreDownloadingFrom () > 0 || tor->isVerifying ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_DOWNLOADING:
|
|
|
|
accepts = tor->isDownloading () || tor->isWaitingToDownload ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_SEEDING:
|
|
|
|
accepts = tor->isSeeding () || tor->isWaitingToSeed ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_PAUSED:
|
|
|
|
accepts = tor->isPaused ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_FINISHED:
|
|
|
|
accepts = tor->isFinished ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_VERIFYING:
|
|
|
|
accepts = tor->isVerifying () || tor->isWaitingToVerify ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FilterMode::SHOW_ERROR:
|
|
|
|
accepts = tor->hasError ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // FilterMode::SHOW_ALL
|
|
|
|
accepts = true;
|
|
|
|
break;
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return accepts;
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::filterAcceptsRow (int sourceRow, const QModelIndex& sourceParent) const
|
2010-07-30 22:24:48 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QModelIndex childIndex = sourceModel()->index (sourceRow, 0, sourceParent);
|
|
|
|
const Torrent * tor = childIndex.model()->data (childIndex, TorrentModel::TorrentRole).value<const Torrent*>();
|
|
|
|
bool accepts = true;
|
2010-07-30 22:24:48 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (accepts)
|
|
|
|
{
|
|
|
|
const FilterMode m = myPrefs.get<FilterMode>(Prefs::FILTER_MODE);
|
|
|
|
accepts = activityFilterAcceptsTorrent (tor, m);
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (accepts)
|
|
|
|
{
|
|
|
|
const QString trackers = myPrefs.getString(Prefs::FILTER_TRACKERS);
|
|
|
|
accepts = trackerFilterAcceptsTorrent (tor, trackers);
|
2009-04-11 18:25:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (accepts)
|
|
|
|
{
|
|
|
|
const QString text = myPrefs.getString (Prefs::FILTER_TEXT);
|
|
|
|
if (!text.isEmpty ())
|
|
|
|
accepts = tor->name().contains (text, Qt::CaseInsensitive);
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return accepts;
|
2009-04-11 18:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::hiddenRowCount () const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
return sourceModel()->rowCount () - rowCount ();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2010-07-30 22:24:48 +00:00
|
|
|
|
2012-12-30 01:28:28 +00:00
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TorrentFilter::countTorrentsPerMode (int * setmeCounts) const
|
2010-07-30 22:24:48 +00:00
|
|
|
{
|
2013-09-08 18:39:37 +00:00
|
|
|
std::fill_n (setmeCounts, static_cast<std::size_t>(FilterMode::NUM_MODES), 0);
|
2012-12-30 01:28:28 +00:00
|
|
|
|
|
|
|
for (int row(0); ; ++row)
|
2015-01-02 11:15:31 +00:00
|
|
|
{
|
2012-12-30 01:28:28 +00:00
|
|
|
QModelIndex index (sourceModel()->index(row, 0));
|
|
|
|
if (!index.isValid())
|
|
|
|
break;
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
const Torrent * tor (index.data (TorrentModel::TorrentRole).value<const Torrent*>());
|
2012-12-30 01:28:28 +00:00
|
|
|
for (int mode(0); mode<FilterMode::NUM_MODES; ++mode)
|
|
|
|
if (activityFilterAcceptsTorrent (tor, mode))
|
|
|
|
++setmeCounts[mode];
|
2010-07-30 22:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-30 01:28:28 +00:00
|
|
|
|