diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 8171d321d..d4db1e3e1 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -2122,17 +2122,12 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne } } + // If empty then Automatic mode, otherwise Manual mode + QString savePath = params.savePath.isEmpty() ? categorySavePath(params.category) : params.savePath; libt::add_torrent_params p; InfoHash hash; - std::vector filePriorities; + const bool fromMagnetUri = magnetUri.isValid(); - QString savePath; - if (params.savePath.isEmpty()) // using Automatic mode - savePath = categorySavePath(params.category); - else // using Manual mode - savePath = params.savePath; - - bool fromMagnetUri = magnetUri.isValid(); if (fromMagnetUri) { hash = magnetUri.hash(); @@ -2195,9 +2190,7 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne p.flags |= libt::add_torrent_params::flag_use_resume_save_path; } else { - foreach (int prio, params.filePriorities) - filePriorities.push_back(prio); - p.file_priorities = filePriorities; + p.file_priorities = {params.filePriorities.begin(), params.filePriorities.end()}; } // We should not add torrent if it already diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 7a729a1ad..7fa4015e9 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1300,13 +1300,21 @@ void TorrentHandle::toggleSequentialDownload() void TorrentHandle::setFirstLastPiecePriority(const bool enabled) { + setFirstLastPiecePriorityImpl(enabled); +} + +void TorrentHandle::setFirstLastPiecePriorityImpl(const bool enabled, const QVector &updatedFilePrio) +{ + // Download first and last pieces first for every file in the torrent + if (!hasMetadata()) { m_needsToSetFirstLastPiecePriority = enabled; return; } - // Download first and last pieces first for every file in the torrent - const std::vector filePriorities = nativeHandle().file_priorities(); + // Updating file priorities is an async operation in libtorrent, when we just updated it and immediately query it + // we might get the old/wrong values, so we rely on `updatedFilePrio` in this case. + const std::vector filePriorities = !updatedFilePrio.isEmpty() ? updatedFilePrio.toStdVector() : nativeHandle().file_priorities(); std::vector piecePriorities = nativeHandle().piece_priorities(); for (int index = 0; index < static_cast(filePriorities.size()); ++index) { const int filePrio = filePriorities[index]; @@ -1991,7 +1999,7 @@ void TorrentHandle::prioritizeFiles(const QVector &priorities) if (priorities.size() != filesCount()) return; // Save first/last piece first option state - bool firstLastPieceFirst = hasFirstLastPiecePriority(); + const bool firstLastPieceFirst = hasFirstLastPiecePriority(); // Reset 'm_hasSeedStatus' if needed in order to react again to // 'torrent_finished_alert' and eg show tray notifications @@ -2064,7 +2072,7 @@ void TorrentHandle::prioritizeFiles(const QVector &priorities) // Restore first/last piece first option if necessary if (firstLastPieceFirst) - setFirstLastPiecePriority(true); + setFirstLastPiecePriorityImpl(true, priorities); updateStatus(); } diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index d5ad32bf7..88fe62946 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -420,6 +420,7 @@ namespace BitTorrent bool addTracker(const TrackerEntry &tracker); bool addUrlSeed(const QUrl &urlSeed); bool removeUrlSeed(const QUrl &urlSeed); + void setFirstLastPiecePriorityImpl(bool enabled, const QVector &updatedFilePrio = {}); Session *const m_session; libtorrent::torrent_handle m_nativeHandle; diff --git a/src/base/torrentfilter.cpp b/src/base/torrentfilter.cpp index b0617ebdc..4c37069d9 100644 --- a/src/base/torrentfilter.cpp +++ b/src/base/torrentfilter.cpp @@ -138,14 +138,14 @@ bool TorrentFilter::setTag(const QString &tag) return false; } -bool TorrentFilter::match(TorrentHandle *const torrent) const +bool TorrentFilter::match(const TorrentHandle *const torrent) const { if (!torrent) return false; return (matchState(torrent) && matchHash(torrent) && matchCategory(torrent) && matchTag(torrent)); } -bool TorrentFilter::matchState(BitTorrent::TorrentHandle *const torrent) const +bool TorrentFilter::matchState(const BitTorrent::TorrentHandle *const torrent) const { switch (m_type) { case All: @@ -171,19 +171,19 @@ bool TorrentFilter::matchState(BitTorrent::TorrentHandle *const torrent) const } } -bool TorrentFilter::matchHash(BitTorrent::TorrentHandle *const torrent) const +bool TorrentFilter::matchHash(const BitTorrent::TorrentHandle *const torrent) const { if (m_hashSet == AnyHash) return true; else return m_hashSet.contains(torrent->hash()); } -bool TorrentFilter::matchCategory(BitTorrent::TorrentHandle *const torrent) const +bool TorrentFilter::matchCategory(const BitTorrent::TorrentHandle *const torrent) const { if (m_category.isNull()) return true; else return (torrent->belongsToCategory(m_category)); } -bool TorrentFilter::matchTag(BitTorrent::TorrentHandle *const torrent) const +bool TorrentFilter::matchTag(const BitTorrent::TorrentHandle *const torrent) const { // Empty tag is a special value to indicate we're filtering for untagged torrents. if (m_tag.isNull()) return true; diff --git a/src/base/torrentfilter.h b/src/base/torrentfilter.h index 378bb7a6f..c4012f75d 100644 --- a/src/base/torrentfilter.h +++ b/src/base/torrentfilter.h @@ -58,7 +58,7 @@ public: // These mean any permutation, including no category / tag. static const QString AnyCategory; static const QStringSet AnyHash; - static const QString AnyTag; + static const QString AnyTag; static const TorrentFilter DownloadingTorrent; static const TorrentFilter SeedingTorrent; @@ -81,13 +81,13 @@ public: bool setCategory(const QString &category); bool setTag(const QString &tag); - bool match(BitTorrent::TorrentHandle *const torrent) const; + bool match(const BitTorrent::TorrentHandle *torrent) const; private: - bool matchState(BitTorrent::TorrentHandle *const torrent) const; - bool matchHash(BitTorrent::TorrentHandle *const torrent) const; - bool matchCategory(BitTorrent::TorrentHandle *const torrent) const; - bool matchTag(BitTorrent::TorrentHandle *const torrent) const; + bool matchState(const BitTorrent::TorrentHandle *torrent) const; + bool matchHash(const BitTorrent::TorrentHandle *torrent) const; + bool matchCategory(const BitTorrent::TorrentHandle *torrent) const; + bool matchTag(const BitTorrent::TorrentHandle *torrent) const; Type m_type; QString m_category;