Browse Source

Merge pull request #14363 from Chocobo1/queue_pos

Revise getter function for torrrent queue position
adaptive-webui-19844
Mike Tzou 4 years ago committed by GitHub
parent
commit
c8e8a44747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      src/base/bittorrent/session.cpp
  2. 4
      src/base/bittorrent/session.h
  3. 4
      src/base/bittorrent/torrentimpl.cpp
  4. 2
      src/gui/transferlistmodel.cpp
  5. 38
      src/webui/api/serialize/serialize_torrent.cpp

40
src/base/bittorrent/session.cpp

@ -1887,7 +1887,7 @@ bool Session::cancelDownloadMetadata(const InfoHash &hash)
void Session::increaseTorrentsQueuePos(const QVector<InfoHash> &hashes) void Session::increaseTorrentsQueuePos(const QVector<InfoHash> &hashes)
{ {
using ElementType = std::pair<int, TorrentImpl *>; using ElementType = std::pair<int, const TorrentImpl *>;
std::priority_queue<ElementType std::priority_queue<ElementType
, std::vector<ElementType> , std::vector<ElementType>
, std::greater<ElementType>> torrentQueue; , std::greater<ElementType>> torrentQueue;
@ -1895,9 +1895,10 @@ void Session::increaseTorrentsQueuePos(const QVector<InfoHash> &hashes)
// Sort torrents by queue position // Sort torrents by queue position
for (const InfoHash &infoHash : hashes) for (const InfoHash &infoHash : hashes)
{ {
TorrentImpl *const torrent = m_torrents.value(infoHash); const TorrentImpl *torrent = m_torrents.value(infoHash);
if (torrent && !torrent->isSeed()) if (!torrent) continue;
torrentQueue.emplace(torrent->queuePosition(), torrent); 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) // Increase torrents queue position (starting with the one in the highest queue position)
@ -1913,15 +1914,16 @@ void Session::increaseTorrentsQueuePos(const QVector<InfoHash> &hashes)
void Session::decreaseTorrentsQueuePos(const QVector<InfoHash> &hashes) void Session::decreaseTorrentsQueuePos(const QVector<InfoHash> &hashes)
{ {
using ElementType = std::pair<int, TorrentImpl *>; using ElementType = std::pair<int, const TorrentImpl *>;
std::priority_queue<ElementType> torrentQueue; std::priority_queue<ElementType> torrentQueue;
// Sort torrents by queue position // Sort torrents by queue position
for (const InfoHash &infoHash : hashes) for (const InfoHash &infoHash : hashes)
{ {
TorrentImpl *const torrent = m_torrents.value(infoHash); const TorrentImpl *torrent = m_torrents.value(infoHash);
if (torrent && !torrent->isSeed()) if (!torrent) continue;
torrentQueue.emplace(torrent->queuePosition(), torrent); 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) // Decrease torrents queue position (starting with the one in the lowest queue position)
@ -1940,15 +1942,16 @@ void Session::decreaseTorrentsQueuePos(const QVector<InfoHash> &hashes)
void Session::topTorrentsQueuePos(const QVector<InfoHash> &hashes) void Session::topTorrentsQueuePos(const QVector<InfoHash> &hashes)
{ {
using ElementType = std::pair<int, TorrentImpl *>; using ElementType = std::pair<int, const TorrentImpl *>;
std::priority_queue<ElementType> torrentQueue; std::priority_queue<ElementType> torrentQueue;
// Sort torrents by queue position // Sort torrents by queue position
for (const InfoHash &infoHash : hashes) for (const InfoHash &infoHash : hashes)
{ {
TorrentImpl *const torrent = m_torrents.value(infoHash); const TorrentImpl *torrent = m_torrents.value(infoHash);
if (torrent && !torrent->isSeed()) if (!torrent) continue;
torrentQueue.emplace(torrent->queuePosition(), torrent); 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) // Top torrents queue position (starting with the one in the lowest queue position)
@ -1964,7 +1967,7 @@ void Session::topTorrentsQueuePos(const QVector<InfoHash> &hashes)
void Session::bottomTorrentsQueuePos(const QVector<InfoHash> &hashes) void Session::bottomTorrentsQueuePos(const QVector<InfoHash> &hashes)
{ {
using ElementType = std::pair<int, TorrentImpl *>; using ElementType = std::pair<int, const TorrentImpl *>;
std::priority_queue<ElementType std::priority_queue<ElementType
, std::vector<ElementType> , std::vector<ElementType>
, std::greater<ElementType>> torrentQueue; , std::greater<ElementType>> torrentQueue;
@ -1972,9 +1975,10 @@ void Session::bottomTorrentsQueuePos(const QVector<InfoHash> &hashes)
// Sort torrents by queue position // Sort torrents by queue position
for (const InfoHash &infoHash : hashes) for (const InfoHash &infoHash : hashes)
{ {
TorrentImpl *const torrent = m_torrents.value(infoHash); const TorrentImpl *torrent = m_torrents.value(infoHash);
if (torrent && !torrent->isSeed()) if (!torrent) continue;
torrentQueue.emplace(torrent->queuePosition(), torrent); 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) // 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 // store hash in textual representation
QMap<int, QString> queue; // Use QMap since it should be ordered by key QMap<int, QString> queue; // Use QMap since it should be ordered by key
@ -2390,7 +2394,7 @@ void Session::saveTorrentsQueue()
#endif #endif
} }
void Session::removeTorrentsQueue() void Session::removeTorrentsQueue() const
{ {
const QString filename = QLatin1String {"queue"}; const QString filename = QLatin1String {"queue"};
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))

4
src/base/bittorrent/session.h

@ -632,8 +632,8 @@ namespace BitTorrent
void createTorrent(const lt::torrent_handle &nativeHandle); void createTorrent(const lt::torrent_handle &nativeHandle);
void saveResumeData(); void saveResumeData();
void saveTorrentsQueue(); void saveTorrentsQueue() const;
void removeTorrentsQueue(); void removeTorrentsQueue() const;
std::vector<lt::alert *> getPendingAlerts(lt::time_duration time = lt::time_duration::zero()) const; std::vector<lt::alert *> getPendingAlerts(lt::time_duration time = lt::time_duration::zero()) const;

4
src/base/bittorrent/torrentimpl.cpp

@ -818,9 +818,7 @@ bool TorrentImpl::hasFilteredPieces() const
int TorrentImpl::queuePosition() const int TorrentImpl::queuePosition() const
{ {
if (m_nativeStatus.queue_position < lt::queue_position_t {0}) return 0; return static_cast<int>(m_nativeStatus.queue_position);
return static_cast<int>(m_nativeStatus.queue_position) + 1;
} }
QString TorrentImpl::error() const QString TorrentImpl::error() const

2
src/gui/transferlistmodel.cpp

@ -292,7 +292,7 @@ QString TransferListModel::displayValue(const BitTorrent::Torrent *torrent, cons
const auto queuePositionString = [](const qint64 value) -> QString 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 const auto lastActivityString = [hideValues](qint64 value) -> QString

38
src/webui/api/serialize/serialize_torrent.cpp

@ -85,8 +85,24 @@ namespace
QVariantMap serialize(const BitTorrent::Torrent &torrent) 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_HASH, QString(torrent.hash())},
{KEY_TORRENT_NAME, torrent.name()}, {KEY_TORRENT_NAME, torrent.name()},
{KEY_TORRENT_MAGNET_URI, torrent.createMagnetURI()}, {KEY_TORRENT_MAGNET_URI, torrent.createMagnetURI()},
@ -94,7 +110,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_PROGRESS, torrent.progress()}, {KEY_TORRENT_PROGRESS, torrent.progress()},
{KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()}, {KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()},
{KEY_TORRENT_UPSPEED, torrent.uploadPayloadRate()}, {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_SEEDS, torrent.seedsCount()},
{KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()}, {KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()},
{KEY_TORRENT_LEECHS, torrent.leechsCount()}, {KEY_TORRENT_LEECHS, torrent.leechsCount()},
@ -125,29 +141,15 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()}, {KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()},
{KEY_TORRENT_MAX_RATIO, torrent.maxRatio()}, {KEY_TORRENT_MAX_RATIO, torrent.maxRatio()},
{KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()}, {KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()},
{KEY_TORRENT_RATIO, adjustRatio(torrent.realRatio())},
{KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()},
{KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()},
{KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toSecsSinceEpoch()}, {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toSecsSinceEpoch()},
{KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()}, {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()},
{KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()},
{KEY_TORRENT_LAST_ACTIVITY_TIME, adjustLastActivity(torrent.timeSinceActivity())},
{KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()}, {KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()},
{KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()} {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;
} }

Loading…
Cancel
Save