2014-11-09 01:43:24 +03:00
|
|
|
/*
|
2018-06-06 16:48:17 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2013 Nick Tiskov <daymansmail@gmail.com>
|
2014-11-09 01:43:24 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2018-06-06 16:48:17 +03:00
|
|
|
#include "transferlistsortmodel.h"
|
|
|
|
|
2019-03-02 13:22:13 +08:00
|
|
|
#include <QDateTime>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QStringList>
|
2014-11-09 01:43:24 +03:00
|
|
|
|
2020-04-12 18:08:19 +03:00
|
|
|
#include "base/bittorrent/infohash.h"
|
2018-06-06 16:48:17 +03:00
|
|
|
#include "base/bittorrent/torrenthandle.h"
|
2019-12-23 09:58:28 +08:00
|
|
|
#include "base/global.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/types.h"
|
|
|
|
#include "base/utils/string.h"
|
2018-06-14 12:54:23 +03:00
|
|
|
#include "transferlistmodel.h"
|
2018-06-06 16:48:17 +03:00
|
|
|
|
2014-11-09 01:43:24 +03:00
|
|
|
TransferListSortModel::TransferListSortModel(QObject *parent)
|
2019-12-17 21:57:36 +03:00
|
|
|
: QSortFilterProxyModel {parent}
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2020-11-26 00:25:42 +08:00
|
|
|
setSortRole(TransferListModel::UnderlyingDataRole);
|
2019-12-17 21:57:36 +03:00
|
|
|
QMetaType::registerComparators<BitTorrent::TorrentState>();
|
2014-11-09 01:58:01 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
void TransferListSortModel::setStatusFilter(TorrentFilter::Type filter)
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2015-04-19 18:17:47 +03:00
|
|
|
if (m_filter.setType(filter))
|
2015-04-05 20:00:00 +01:00
|
|
|
invalidateFilter();
|
2014-11-09 02:49:21 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
void TransferListSortModel::setCategoryFilter(const QString &category)
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
if (m_filter.setCategory(category))
|
2015-04-05 20:00:00 +01:00
|
|
|
invalidateFilter();
|
2014-11-09 02:49:21 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
void TransferListSortModel::disableCategoryFilter()
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
if (m_filter.setCategory(TorrentFilter::AnyCategory))
|
2015-04-05 20:00:00 +01:00
|
|
|
invalidateFilter();
|
2015-03-22 01:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-04 19:22:17 -05:00
|
|
|
void TransferListSortModel::setTagFilter(const QString &tag)
|
|
|
|
{
|
|
|
|
if (m_filter.setTag(tag))
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransferListSortModel::disableTagFilter()
|
|
|
|
{
|
|
|
|
if (m_filter.setTag(TorrentFilter::AnyTag))
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2015-04-05 20:00:00 +01:00
|
|
|
void TransferListSortModel::setTrackerFilter(const QStringList &hashes)
|
|
|
|
{
|
2019-12-23 09:58:28 +08:00
|
|
|
if (m_filter.setHashSet(List::toSet(hashes)))
|
2015-04-05 20:00:00 +01:00
|
|
|
invalidateFilter();
|
2015-03-22 01:18:21 +02:00
|
|
|
}
|
|
|
|
|
2015-04-05 20:00:00 +01:00
|
|
|
void TransferListSortModel::disableTrackerFilter()
|
|
|
|
{
|
2015-04-19 18:17:47 +03:00
|
|
|
if (m_filter.setHashSet(TorrentFilter::AnyHash))
|
2015-04-05 20:00:00 +01:00
|
|
|
invalidateFilter();
|
|
|
|
}
|
2014-11-09 01:43:24 +03:00
|
|
|
|
2015-04-05 20:00:00 +01:00
|
|
|
bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
return lessThan_impl(left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferListSortModel::lessThan_impl(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(left.column() == right.column());
|
|
|
|
|
|
|
|
const auto invokeLessThanForColumn = [this, &left, &right](const int column) -> bool
|
|
|
|
{
|
2020-04-03 19:06:44 +03:00
|
|
|
return lessThan_impl(left.sibling(left.row(), column), right.sibling(right.row(), column));
|
2019-12-17 21:57:36 +03:00
|
|
|
};
|
|
|
|
|
2020-05-16 21:40:46 +08:00
|
|
|
const auto hashLessThan = [this, &left, &right]() -> bool
|
|
|
|
{
|
|
|
|
const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
|
|
|
|
const QString hashL = model->torrentHandle(left)->hash();
|
|
|
|
const QString hashR = model->torrentHandle(right)->hash();
|
|
|
|
return hashL < hashR;
|
|
|
|
};
|
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
const int sortColumn = left.column();
|
|
|
|
const QVariant leftValue = left.data(TransferListModel::UnderlyingDataRole);
|
|
|
|
const QVariant rightValue = right.data(TransferListModel::UnderlyingDataRole);
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
switch (sortColumn)
|
|
|
|
{
|
2018-06-14 12:54:23 +03:00
|
|
|
case TransferListModel::TR_CATEGORY:
|
|
|
|
case TransferListModel::TR_TAGS:
|
2019-12-17 21:57:36 +03:00
|
|
|
case TransferListModel::TR_NAME:
|
|
|
|
if (!leftValue.isValid() || !rightValue.isValid() || (leftValue == rightValue))
|
|
|
|
return invokeLessThanForColumn(TransferListModel::TR_QUEUE_POSITION);
|
|
|
|
return (Utils::String::naturalCompare(leftValue.toString(), rightValue.toString(), Qt::CaseInsensitive) < 0);
|
|
|
|
|
|
|
|
case TransferListModel::TR_STATUS:
|
|
|
|
// QSortFilterProxyModel::lessThan() uses the < operator only for specific QVariant types
|
|
|
|
// so our custom type is outside that list.
|
|
|
|
// In this case QSortFilterProxyModel::lessThan() converts other types to QString and
|
|
|
|
// sorts them.
|
|
|
|
// Thus we can't use the code in the default label.
|
|
|
|
if (leftValue != rightValue)
|
|
|
|
return leftValue < rightValue;
|
|
|
|
return invokeLessThanForColumn(TransferListModel::TR_QUEUE_POSITION);
|
2015-10-04 20:48:43 +08:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
case TransferListModel::TR_ADD_DATE:
|
|
|
|
case TransferListModel::TR_SEED_DATE:
|
2020-11-16 10:02:11 +03:00
|
|
|
case TransferListModel::TR_SEEN_COMPLETE_DATE:
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
const QDateTime dateL = leftValue.toDateTime();
|
|
|
|
const QDateTime dateR = rightValue.toDateTime();
|
2018-01-01 20:04:54 +02:00
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if (dateL.isValid() && dateR.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
if (dateL != dateR)
|
|
|
|
return dateL < dateR;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else if (dateL.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else if (dateR.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-16 21:40:46 +08:00
|
|
|
|
|
|
|
return hashLessThan();
|
2018-06-06 16:48:17 +03:00
|
|
|
}
|
2018-01-01 20:04:54 +02:00
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
case TransferListModel::TR_QUEUE_POSITION:
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
// QVariant has comparators for all basic types
|
2020-11-16 10:02:11 +03:00
|
|
|
if ((leftValue > 0) || (rightValue > 0))
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
if ((leftValue > 0) && (rightValue > 0))
|
|
|
|
return leftValue < rightValue;
|
2015-10-04 20:48:43 +08:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
return leftValue != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort according to TR_SEED_DATE
|
|
|
|
const QDateTime dateL = left.sibling(left.row(), TransferListModel::TR_SEED_DATE)
|
|
|
|
.data(TransferListModel::UnderlyingDataRole).toDateTime();
|
|
|
|
const QDateTime dateR = right.sibling(right.row(), TransferListModel::TR_SEED_DATE)
|
|
|
|
.data(TransferListModel::UnderlyingDataRole).toDateTime();
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if (dateL.isValid() && dateR.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
if (dateL != dateR)
|
|
|
|
return dateL < dateR;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else if (dateL.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else if (dateR.isValid())
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
return true;
|
|
|
|
}
|
2020-05-16 21:40:46 +08:00
|
|
|
|
|
|
|
return hashLessThan();
|
2019-12-17 21:57:36 +03:00
|
|
|
}
|
2015-10-04 20:48:43 +08:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
case TransferListModel::TR_SEEDS:
|
2020-11-16 10:02:11 +03:00
|
|
|
case TransferListModel::TR_PEERS:
|
|
|
|
{
|
2019-12-17 21:57:36 +03:00
|
|
|
// QVariant has comparators for all basic types
|
2018-06-06 16:48:17 +03:00
|
|
|
// Active peers/seeds take precedence over total peers/seeds.
|
2019-12-17 21:57:36 +03:00
|
|
|
if (leftValue != rightValue)
|
|
|
|
return (leftValue < rightValue);
|
2017-11-30 17:10:30 +08:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
const QVariant leftValueTotal = left.data(TransferListModel::AdditionalUnderlyingDataRole);
|
|
|
|
const QVariant rightValueTotal = right.data(TransferListModel::AdditionalUnderlyingDataRole);
|
|
|
|
if (leftValueTotal != rightValueTotal)
|
|
|
|
return (leftValueTotal < rightValueTotal);
|
2015-10-04 20:48:43 +08:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
return invokeLessThanForColumn(TransferListModel::TR_QUEUE_POSITION);
|
2015-04-05 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
case TransferListModel::TR_ETA:
|
|
|
|
{
|
2018-06-06 16:48:17 +03:00
|
|
|
// Sorting rules prioritized.
|
|
|
|
// 1. Active torrents at the top
|
|
|
|
// 2. Seeding torrents at the bottom
|
|
|
|
// 3. Torrents with invalid ETAs at the bottom
|
|
|
|
|
2019-08-07 14:26:36 +08:00
|
|
|
const TransferListModel *model = qobject_cast<TransferListModel *>(sourceModel());
|
|
|
|
|
|
|
|
// From QSortFilterProxyModel::lessThan() documentation:
|
|
|
|
// "Note: The indices passed in correspond to the source model"
|
|
|
|
const bool isActiveL = TorrentFilter::ActiveTorrent.match(model->torrentHandle(left));
|
|
|
|
const bool isActiveR = TorrentFilter::ActiveTorrent.match(model->torrentHandle(right));
|
2018-06-06 16:48:17 +03:00
|
|
|
if (isActiveL != isActiveR)
|
|
|
|
return isActiveL;
|
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
const int queuePosL = left.sibling(left.row(), TransferListModel::TR_QUEUE_POSITION)
|
|
|
|
.data(TransferListModel::UnderlyingDataRole).toInt();
|
|
|
|
const int queuePosR = right.sibling(right.row(), TransferListModel::TR_QUEUE_POSITION)
|
|
|
|
.data(TransferListModel::UnderlyingDataRole).toInt();
|
2018-12-08 01:01:09 +02:00
|
|
|
const bool isSeedingL = (queuePosL < 0);
|
|
|
|
const bool isSeedingR = (queuePosR < 0);
|
2020-11-16 10:02:11 +03:00
|
|
|
if (isSeedingL != isSeedingR)
|
|
|
|
{
|
2018-06-06 16:48:17 +03:00
|
|
|
const bool isAscendingOrder = (sortOrder() == Qt::AscendingOrder);
|
|
|
|
if (isSeedingL)
|
|
|
|
return !isAscendingOrder;
|
2019-02-12 20:05:01 +02:00
|
|
|
|
|
|
|
return isAscendingOrder;
|
2018-06-06 16:48:17 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
const qlonglong etaL = leftValue.toLongLong();
|
|
|
|
const qlonglong etaR = rightValue.toLongLong();
|
2018-06-06 16:48:17 +03:00
|
|
|
const bool isInvalidL = ((etaL < 0) || (etaL >= MAX_ETA));
|
|
|
|
const bool isInvalidR = ((etaR < 0) || (etaR >= MAX_ETA));
|
2020-11-16 10:02:11 +03:00
|
|
|
if (isInvalidL && isInvalidR)
|
|
|
|
{
|
2018-06-06 16:48:17 +03:00
|
|
|
if (isSeedingL) // Both seeding
|
2019-12-17 21:57:36 +03:00
|
|
|
return invokeLessThanForColumn(TransferListModel::TR_SEED_DATE);
|
2019-02-12 20:05:01 +02:00
|
|
|
|
2018-12-08 01:01:09 +02:00
|
|
|
return (queuePosL < queuePosR);
|
2018-06-06 16:48:17 +03:00
|
|
|
}
|
2019-12-17 21:57:36 +03:00
|
|
|
|
|
|
|
if (!isInvalidL && !isInvalidR)
|
2018-06-06 16:48:17 +03:00
|
|
|
return (etaL < etaR);
|
2019-02-12 20:05:01 +02:00
|
|
|
|
|
|
|
return !isInvalidL;
|
2015-04-05 20:00:00 +01:00
|
|
|
}
|
2015-10-04 20:48:43 +08:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
case TransferListModel::TR_LAST_ACTIVITY:
|
|
|
|
case TransferListModel::TR_RATIO_LIMIT:
|
|
|
|
// QVariant has comparators for all basic types
|
|
|
|
if (leftValue < 0) return false;
|
|
|
|
if (rightValue < 0) return true;
|
2015-04-05 20:00:00 +01:00
|
|
|
|
2019-12-17 21:57:36 +03:00
|
|
|
return (leftValue < rightValue);
|
2017-02-06 18:38:51 +02:00
|
|
|
}
|
2015-02-10 17:25:17 +01:00
|
|
|
|
2020-05-16 21:40:46 +08:00
|
|
|
return (leftValue != rightValue)
|
|
|
|
? QSortFilterProxyModel::lessThan(left, right)
|
|
|
|
: invokeLessThanForColumn(TransferListModel::TR_QUEUE_POSITION);
|
2015-02-10 17:25:17 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:26:36 +08:00
|
|
|
bool TransferListSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2015-04-19 18:17:47 +03:00
|
|
|
return matchFilter(sourceRow, sourceParent)
|
2017-02-06 18:38:51 +02:00
|
|
|
&& QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
2014-11-09 01:58:01 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:26:36 +08:00
|
|
|
bool TransferListSortModel::matchFilter(const int sourceRow, const QModelIndex &sourceParent) const
|
2015-04-05 20:00:00 +01:00
|
|
|
{
|
2019-08-07 14:26:36 +08:00
|
|
|
const auto *model = qobject_cast<TransferListModel *>(sourceModel());
|
2015-04-05 20:00:00 +01:00
|
|
|
if (!model) return false;
|
|
|
|
|
2019-08-07 14:26:36 +08:00
|
|
|
const BitTorrent::TorrentHandle *torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent));
|
2015-04-19 18:17:47 +03:00
|
|
|
if (!torrent) return false;
|
2015-03-22 01:18:21 +02:00
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
return m_filter.match(torrent);
|
2015-03-22 01:18:21 +02:00
|
|
|
}
|