From 06105072f9006c416439f7f596562fe00802b759 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 1 Jan 2021 16:08:01 +0300 Subject: [PATCH 1/2] Extract torrent reloading logic into separate method --- src/base/bittorrent/torrenthandleimpl.cpp | 53 ++++++++++++++--------- src/base/bittorrent/torrenthandleimpl.h | 1 + 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/base/bittorrent/torrenthandleimpl.cpp b/src/base/bittorrent/torrenthandleimpl.cpp index b9dff5119..22bea48ec 100644 --- a/src/base/bittorrent/torrenthandleimpl.cpp +++ b/src/base/bittorrent/torrenthandleimpl.cpp @@ -1308,44 +1308,57 @@ void TorrentHandleImpl::fileSearchFinished(const QString &savePath, const QStrin void TorrentHandleImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames) { - const auto queuePos = m_nativeHandle.queue_position(); + lt::add_torrent_params &p = m_ltAddTorrentParams; - lt::add_torrent_params p = m_ltAddTorrentParams; p.ti = std::const_pointer_cast(m_nativeHandle.torrent_file()); - - m_nativeSession->remove_torrent(m_nativeHandle, lt::session::delete_partfile); - for (int i = 0; i < fileNames.size(); ++i) p.renamed_files[lt::file_index_t {i}] = fileNames[i].toStdString(); - p.save_path = Utils::Fs::toNativePath(savePath).toStdString(); - p.flags |= lt::torrent_flags::update_subscribe - | lt::torrent_flags::override_trackers - | lt::torrent_flags::override_web_seeds; - m_nativeHandle = m_nativeSession->add_torrent(p); - m_nativeHandle.queue_position_set(queuePos); + reload(); - m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; // If first/last piece priority was specified when adding this torrent, // we should apply it now that we have metadata: if (m_hasFirstLastPiecePriority) applyFirstLastPiecePriority(true); - if (!m_isStopped) - { - setAutoManaged(m_operatingMode == TorrentOperatingMode::AutoManaged); - if (m_operatingMode == TorrentOperatingMode::Forced) - m_nativeHandle.resume(); - } - m_maintenanceJob = MaintenanceJob::None; - updateStatus(); m_session->handleTorrentMetadataReceived(this); } +void TorrentHandleImpl::reload() +{ + const auto queuePos = m_nativeHandle.queue_position(); + + m_nativeSession->remove_torrent(m_nativeHandle, lt::session::delete_partfile); + + lt::add_torrent_params p = m_ltAddTorrentParams; + p.flags |= lt::torrent_flags::update_subscribe + | lt::torrent_flags::override_trackers + | lt::torrent_flags::override_web_seeds; + + if (m_isStopped) + { + p.flags |= lt::torrent_flags::paused; + p.flags &= ~lt::torrent_flags::auto_managed; + } + else if (m_operatingMode == TorrentOperatingMode::AutoManaged) + { + p.flags |= (lt::torrent_flags::auto_managed | lt::torrent_flags::paused); + } + else + { + p.flags &= ~(lt::torrent_flags::auto_managed | lt::torrent_flags::paused); + } + + m_nativeHandle = m_nativeSession->add_torrent(p); + m_nativeHandle.queue_position_set(queuePos); + + m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; +} + void TorrentHandleImpl::pause() { if (!m_isStopped) diff --git a/src/base/bittorrent/torrenthandleimpl.h b/src/base/bittorrent/torrenthandleimpl.h index cbe4eaf2c..d959c0910 100644 --- a/src/base/bittorrent/torrenthandleimpl.h +++ b/src/base/bittorrent/torrenthandleimpl.h @@ -291,6 +291,7 @@ namespace BitTorrent void applyFirstLastPiecePriority(bool enabled, const QVector &updatedFilePrio = {}); void endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames); + void reload(); Session *const m_session; lt::session *m_nativeSession; From b2199202ab829a3818b8d1d6ae6bc4a5b8afc75a Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 1 Jan 2021 16:16:52 +0300 Subject: [PATCH 2/2] Reload "missing files" torrent instead of re-checking --- src/base/bittorrent/torrenthandleimpl.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/torrenthandleimpl.cpp b/src/base/bittorrent/torrenthandleimpl.cpp index 22bea48ec..447360ba8 100644 --- a/src/base/bittorrent/torrenthandleimpl.cpp +++ b/src/base/bittorrent/torrenthandleimpl.cpp @@ -1381,14 +1381,17 @@ void TorrentHandleImpl::resume(const TorrentOperatingMode mode) if (hasError()) m_nativeHandle.clear_error(); + m_operatingMode = mode; + if (m_hasMissingFiles) { m_hasMissingFiles = false; - m_nativeHandle.force_recheck(); + m_isStopped = false; + reload(); + updateStatus(); + return; } - m_operatingMode = mode; - if (m_isStopped) { // Torrent may have been temporarily resumed to perform checking files