From bc18bf1ab4f4fe11325b7255152261bf782778ed Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Tue, 22 Mar 2016 18:10:12 +0100 Subject: [PATCH] Add TorrentHandle::availableFileFractions() function The function returns list of availability values for all torrent files. The availability here is different to that one in the progress bar. Here it shows what part of the file's pieces is available (i.e. we do not count availability > 1 for a piece). --- src/base/bittorrent/torrenthandle.cpp | 21 +++++++++++++++++++++ src/base/bittorrent/torrenthandle.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index a6436ec34..f9ad2c697 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1953,3 +1953,24 @@ void TorrentHandle::prioritizeFiles(const QVector &priorities) updateStatus(); } + +QVector TorrentHandle::availableFileFractions() const +{ + QVector piecesAvailability = pieceAvailability(); + const auto filesCount = this->filesCount(); + // libtorrent returns empty array for seeding only torrents + if (piecesAvailability.empty()) return QVector(filesCount, -1.); + + QVector res; + res.reserve(filesCount); + TorrentInfo info = this->info(); + for (int file = 0; file < filesCount; ++file) { + TorrentInfo::PieceRange filePieces = info.filePieces(file); + int availablePieces = 0; + for (int piece = filePieces.first(); piece <= filePieces.last(); ++piece) { + availablePieces += piecesAvailability[piece] > 0 ? 1 : 0; + } + res.push_back(static_cast(availablePieces) / filePieces.size()); + } + return res; +} diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index 8cdcfe02e..cb0c183cc 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -361,6 +361,14 @@ namespace BitTorrent void handleAppendExtensionToggled(); void saveResumeData(bool updateStatus = false); + /** + * @brief fraction of file pieces that are available at least from one peer + * + * This is not the same as torrrent availability, it is just a fraction of pieces + * that can be downloaded right now. It varies between 0 to 1. + */ + QVector availableFileFractions() const; + private: typedef boost::function EventTrigger;