From e46c88580ac7752c280e37ffd2da6b64677b8a6d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 7 Feb 2021 21:59:47 +0800 Subject: [PATCH] Revise getter function for torrrent queue position This addresses https://github.com/qbittorrent/qBittorrent/pull/14335#issuecomment-774667836 The WebAPI is not affected as a workaround is added. --- src/base/bittorrent/session.cpp | 40 ++++++++++--------- src/base/bittorrent/session.h | 4 +- src/base/bittorrent/torrentimpl.cpp | 4 +- src/gui/transferlistmodel.cpp | 2 +- src/webui/api/serialize/serialize_torrent.cpp | 38 +++++++++--------- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index fb78d9df9..a6d0f6acc 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1887,7 +1887,7 @@ bool Session::cancelDownloadMetadata(const InfoHash &hash) void Session::increaseTorrentsQueuePos(const QVector &hashes) { - using ElementType = std::pair; + using ElementType = std::pair; std::priority_queue , std::greater> torrentQueue; @@ -1895,9 +1895,10 @@ void Session::increaseTorrentsQueuePos(const QVector &hashes) // Sort torrents by queue position for (const InfoHash &infoHash : hashes) { - TorrentImpl *const torrent = m_torrents.value(infoHash); - if (torrent && !torrent->isSeed()) - torrentQueue.emplace(torrent->queuePosition(), torrent); + const TorrentImpl *torrent = m_torrents.value(infoHash); + if (!torrent) continue; + if (const int position = torrent->queuePosition(); position >= 0) + torrentQueue.emplace(position, torrent); } // Increase torrents queue position (starting with the one in the highest queue position) @@ -1913,15 +1914,16 @@ void Session::increaseTorrentsQueuePos(const QVector &hashes) void Session::decreaseTorrentsQueuePos(const QVector &hashes) { - using ElementType = std::pair; + using ElementType = std::pair; std::priority_queue torrentQueue; // Sort torrents by queue position for (const InfoHash &infoHash : hashes) { - TorrentImpl *const torrent = m_torrents.value(infoHash); - if (torrent && !torrent->isSeed()) - torrentQueue.emplace(torrent->queuePosition(), torrent); + const TorrentImpl *torrent = m_torrents.value(infoHash); + if (!torrent) continue; + if (const int position = torrent->queuePosition(); position >= 0) + torrentQueue.emplace(position, torrent); } // Decrease torrents queue position (starting with the one in the lowest queue position) @@ -1940,15 +1942,16 @@ void Session::decreaseTorrentsQueuePos(const QVector &hashes) void Session::topTorrentsQueuePos(const QVector &hashes) { - using ElementType = std::pair; + using ElementType = std::pair; std::priority_queue torrentQueue; // Sort torrents by queue position for (const InfoHash &infoHash : hashes) { - TorrentImpl *const torrent = m_torrents.value(infoHash); - if (torrent && !torrent->isSeed()) - torrentQueue.emplace(torrent->queuePosition(), torrent); + const TorrentImpl *torrent = m_torrents.value(infoHash); + if (!torrent) continue; + if (const int position = torrent->queuePosition(); position >= 0) + torrentQueue.emplace(position, torrent); } // Top torrents queue position (starting with the one in the lowest queue position) @@ -1964,7 +1967,7 @@ void Session::topTorrentsQueuePos(const QVector &hashes) void Session::bottomTorrentsQueuePos(const QVector &hashes) { - using ElementType = std::pair; + using ElementType = std::pair; std::priority_queue , std::greater> torrentQueue; @@ -1972,9 +1975,10 @@ void Session::bottomTorrentsQueuePos(const QVector &hashes) // Sort torrents by queue position for (const InfoHash &infoHash : hashes) { - TorrentImpl *const torrent = m_torrents.value(infoHash); - if (torrent && !torrent->isSeed()) - torrentQueue.emplace(torrent->queuePosition(), torrent); + const TorrentImpl *torrent = m_torrents.value(infoHash); + if (!torrent) continue; + if (const int position = torrent->queuePosition(); position >= 0) + torrentQueue.emplace(position, torrent); } // Bottom torrents queue position (starting with the one in the highest queue position) @@ -2363,7 +2367,7 @@ void Session::saveResumeData() } } -void Session::saveTorrentsQueue() +void Session::saveTorrentsQueue() const { // store hash in textual representation QMap queue; // Use QMap since it should be ordered by key @@ -2390,7 +2394,7 @@ void Session::saveTorrentsQueue() #endif } -void Session::removeTorrentsQueue() +void Session::removeTorrentsQueue() const { const QString filename = QLatin1String {"queue"}; #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 9daced861..703137c45 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -632,8 +632,8 @@ namespace BitTorrent void createTorrent(const lt::torrent_handle &nativeHandle); void saveResumeData(); - void saveTorrentsQueue(); - void removeTorrentsQueue(); + void saveTorrentsQueue() const; + void removeTorrentsQueue() const; std::vector getPendingAlerts(lt::time_duration time = lt::time_duration::zero()) const; diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 483742336..dc86751b8 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -818,9 +818,7 @@ bool TorrentImpl::hasFilteredPieces() const int TorrentImpl::queuePosition() const { - if (m_nativeStatus.queue_position < lt::queue_position_t {0}) return 0; - - return static_cast(m_nativeStatus.queue_position) + 1; + return static_cast(m_nativeStatus.queue_position); } QString TorrentImpl::error() const diff --git a/src/gui/transferlistmodel.cpp b/src/gui/transferlistmodel.cpp index 716d0d34e..99d1429b9 100644 --- a/src/gui/transferlistmodel.cpp +++ b/src/gui/transferlistmodel.cpp @@ -292,7 +292,7 @@ QString TransferListModel::displayValue(const BitTorrent::Torrent *torrent, cons const auto queuePositionString = [](const qint64 value) -> QString { - return (value > 0) ? QString::number(value) : QLatin1String("*"); + return (value >= 0) ? QString::number(value + 1) : QLatin1String("*"); }; const auto lastActivityString = [hideValues](qint64 value) -> QString diff --git a/src/webui/api/serialize/serialize_torrent.cpp b/src/webui/api/serialize/serialize_torrent.cpp index 59c0039ed..9b987f7bd 100644 --- a/src/webui/api/serialize/serialize_torrent.cpp +++ b/src/webui/api/serialize/serialize_torrent.cpp @@ -85,8 +85,24 @@ namespace QVariantMap serialize(const BitTorrent::Torrent &torrent) { - QVariantMap ret = + const auto adjustQueuePosition = [](const int position) -> int { + return (position < 0) ? 0 : (position + 1); + }; + + const auto adjustRatio = [](const qreal ratio) -> qreal + { + return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio; + }; + + const auto adjustLastActivity = [&torrent](const qlonglong value) -> qlonglong + { + return (torrent.isPaused() || torrent.isChecking()) + ? 0 + : (QDateTime::currentDateTime().toSecsSinceEpoch() - value); + }; + + return { {KEY_TORRENT_HASH, QString(torrent.hash())}, {KEY_TORRENT_NAME, torrent.name()}, {KEY_TORRENT_MAGNET_URI, torrent.createMagnetURI()}, @@ -94,7 +110,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_PROGRESS, torrent.progress()}, {KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()}, {KEY_TORRENT_UPSPEED, torrent.uploadPayloadRate()}, - {KEY_TORRENT_QUEUE_POSITION, torrent.queuePosition()}, + {KEY_TORRENT_QUEUE_POSITION, adjustQueuePosition(torrent.queuePosition())}, {KEY_TORRENT_SEEDS, torrent.seedsCount()}, {KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()}, {KEY_TORRENT_LEECHS, torrent.leechsCount()}, @@ -125,29 +141,15 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()}, {KEY_TORRENT_MAX_RATIO, torrent.maxRatio()}, {KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()}, + {KEY_TORRENT_RATIO, adjustRatio(torrent.realRatio())}, {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toSecsSinceEpoch()}, {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()}, {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, + {KEY_TORRENT_LAST_ACTIVITY_TIME, adjustLastActivity(torrent.timeSinceActivity())}, {KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()}, {KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()} }; - - const qreal ratio = torrent.realRatio(); - ret[KEY_TORRENT_RATIO] = (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio; - - if (torrent.isPaused() || torrent.isChecking()) - { - ret[KEY_TORRENT_LAST_ACTIVITY_TIME] = 0; - } - else - { - const qint64 dt = (QDateTime::currentDateTime().toSecsSinceEpoch() - - torrent.timeSinceActivity()); - ret[KEY_TORRENT_LAST_ACTIVITY_TIME] = dt; - } - - return ret; }