From b2b82a952c6eec43f68ba6ca136b5d20e99e9979 Mon Sep 17 00:00:00 2001 From: OscarCunningham <35840763+OscarCunningham@users.noreply.github.com> Date: Fri, 16 Sep 2022 18:38:32 +0100 Subject: [PATCH] Qt: Correct 'Sort by Progress' for seeding torrents (#3814) --- qt/Torrent.cc | 44 +++++++++++++++++++++++++++++++++++--------- qt/Torrent.h | 2 +- qt/TorrentFilter.cc | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/qt/Torrent.cc b/qt/Torrent.cc index 0ae4a1900..0c6923eb4 100644 --- a/qt/Torrent.cc +++ b/qt/Torrent.cc @@ -54,27 +54,53 @@ bool Torrent::includesTracker(QString const& sitename) const return std::binary_search(std::begin(sitenames_), std::end(sitenames_), sitename); } -int Torrent::compareSeedRatio(Torrent const& that) const +int Torrent::compareSeedProgress(Torrent const& that) const { - auto const a = getSeedRatioLimit(); - auto const b = that.getSeedRatioLimit(); + auto const a_ratio_limit = getSeedRatioLimit(); + auto const b_ratio_limit = that.getSeedRatioLimit(); - if (!a && !b) + if (!a_ratio_limit && !b_ratio_limit) { - return 0; + return compareRatio(that); } - if (!a || !b) + auto const a_ratio = ratio(); + auto const b_ratio = that.ratio(); + + if (!a_ratio_limit) { - return a ? -1 : 1; + return b_ratio < *b_ratio_limit ? 1 : -1; } - if (*a < *b) + if (!b_ratio_limit) + { + return a_ratio < *a_ratio_limit ? -1 : 1; + } + + if (!(*a_ratio_limit > 0) && !(*b_ratio_limit > 0)) + { + return compareRatio(that); + } + + if (!(*a_ratio_limit > 0)) + { + return 1; + } + + if (!(*b_ratio_limit > 0)) { return -1; } - if (*a > *b) + double const a_progress = a_ratio / *a_ratio_limit; + double const b_progress = b_ratio / *b_ratio_limit; + + if (a_progress < b_progress) + { + return -1; + } + + if (a_progress > b_progress) { return 1; } diff --git a/qt/Torrent.h b/qt/Torrent.h index b7070af5e..fa7b73bdb 100644 --- a/qt/Torrent.h +++ b/qt/Torrent.h @@ -325,7 +325,7 @@ public: return failed_ever_; } - int compareSeedRatio(Torrent const&) const; + int compareSeedProgress(Torrent const&) const; int compareRatio(Torrent const&) const; int compareETA(Torrent const&) const; diff --git a/qt/TorrentFilter.cc b/qt/TorrentFilter.cc index 18870664d..3fa56e46c 100644 --- a/qt/TorrentFilter.cc +++ b/qt/TorrentFilter.cc @@ -182,7 +182,7 @@ bool TorrentFilter::lessThan(QModelIndex const& left, QModelIndex const& right) if (val == 0) { - val = a->compareSeedRatio(*b); + val = a->compareSeedProgress(*b); } if (val == 0)