From f6eb341b32b4ef5b8a93e1706758cf6b22272a4e Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 28 Apr 2020 02:38:20 +0800 Subject: [PATCH 1/2] Clean up code --- src/base/bittorrent/torrenthandleimpl.cpp | 2 +- src/gui/torrentcontentmodel.cpp | 2 +- src/gui/torrentcontentmodelfile.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/torrenthandleimpl.cpp b/src/base/bittorrent/torrenthandleimpl.cpp index 431825a60..85f6f13cd 100644 --- a/src/base/bittorrent/torrenthandleimpl.cpp +++ b/src/base/bittorrent/torrenthandleimpl.cpp @@ -2124,7 +2124,7 @@ QVector TorrentHandleImpl::availableFileFractions() const const QVector piecesAvailability = pieceAvailability(); // libtorrent returns empty array for seeding only torrents - if (piecesAvailability.empty()) return QVector(filesCount, -1.); + if (piecesAvailability.empty()) return QVector(filesCount, -1); QVector res; res.reserve(filesCount); diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index b8bdc46d4..4886f826d 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -233,7 +233,7 @@ void TorrentContentModel::updateFilesAvailability(const QVector &fa) if (m_filesIndex.size() != fa.size()) return; emit layoutAboutToBeChanged(); - for (int i = 0; i < fa.size(); ++i) + for (int i = 0; i < m_filesIndex.size(); ++i) m_filesIndex[i]->setAvailability(fa[i]); // Update folders progress in the tree m_rootItem->recalculateProgress(); diff --git a/src/gui/torrentcontentmodelfile.cpp b/src/gui/torrentcontentmodelfile.cpp index ceeccbfc9..d14d9cc6e 100644 --- a/src/gui/torrentcontentmodelfile.cpp +++ b/src/gui/torrentcontentmodelfile.cpp @@ -73,7 +73,7 @@ void TorrentContentModelFile::setProgress(qreal progress) Q_ASSERT(m_progress <= 1.); } -void TorrentContentModelFile::setAvailability(qreal availability) +void TorrentContentModelFile::setAvailability(const qreal availability) { m_availability = availability; Q_ASSERT(m_availability <= 1.); From 5ca26516ebdd3773d0046ba0dd12a1da036f0906 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 28 Apr 2020 11:47:08 +0800 Subject: [PATCH 2/2] Handle empty piece range properly Related: #12677. --- src/base/bittorrent/torrenthandleimpl.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/base/bittorrent/torrenthandleimpl.cpp b/src/base/bittorrent/torrenthandleimpl.cpp index 85f6f13cd..a0a75cfd1 100644 --- a/src/base/bittorrent/torrenthandleimpl.cpp +++ b/src/base/bittorrent/torrenthandleimpl.cpp @@ -2120,7 +2120,7 @@ void TorrentHandleImpl::prioritizeFiles(const QVector &priorit QVector TorrentHandleImpl::availableFileFractions() const { const int filesCount = this->filesCount(); - if (filesCount < 0) return {}; + if (filesCount <= 0) return {}; const QVector piecesAvailability = pieceAvailability(); // libtorrent returns empty array for seeding only torrents @@ -2136,7 +2136,10 @@ QVector TorrentHandleImpl::availableFileFractions() const for (const int piece : filePieces) availablePieces += (piecesAvailability[piece] > 0) ? 1 : 0; - res.push_back(static_cast(availablePieces) / filePieces.size()); + const qreal availability = filePieces.isEmpty() + ? 1 // the file has no pieces, so it is available by default + : static_cast(availablePieces) / filePieces.size(); + res.push_back(availability); } return res; }