From 4db50dae1050b2ccb6ec1ec4c54886a1beb92d73 Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Mon, 14 Oct 2024 00:36:38 +0800 Subject: [PATCH] feat: save queue order between sessions (#6753) Co-authored-by: Charles Kerr --- libtransmission/quark.cc | 1 + libtransmission/quark.h | 1 + libtransmission/resume.cc | 44 ++++++++++++++++++++++++++++++++++++++ libtransmission/resume.h | 1 + libtransmission/torrent.cc | 10 +++++++++ libtransmission/torrent.h | 1 + 6 files changed, 58 insertions(+) diff --git a/libtransmission/quark.cc b/libtransmission/quark.cc index e6c53fdb3..52921710d 100644 --- a/libtransmission/quark.cc +++ b/libtransmission/quark.cc @@ -170,6 +170,7 @@ auto constexpr MyStatic = std::array{ "isStalled"sv, "isUTP"sv, "isUploadingTo"sv, + "is_queued"sv, "labels"sv, "lastAnnouncePeerCount"sv, "lastAnnounceResult"sv, diff --git a/libtransmission/quark.h b/libtransmission/quark.h index 2ed31649c..ba191144e 100644 --- a/libtransmission/quark.h +++ b/libtransmission/quark.h @@ -172,6 +172,7 @@ enum TR_KEY_isStalled, TR_KEY_isUTP, TR_KEY_isUploadingTo, + TR_KEY_is_queued, TR_KEY_labels, TR_KEY_lastAnnouncePeerCount, TR_KEY_lastAnnounceResult, diff --git a/libtransmission/resume.cc b/libtransmission/resume.cc index c91e95786..f0a70d557 100644 --- a/libtransmission/resume.cc +++ b/libtransmission/resume.cc @@ -404,6 +404,44 @@ tr_resume::fields_t loadFilenames(tr_variant* dict, tr_torrent* tor) // --- +void saveQueueState(tr_variant* dict, tr_torrent const* tor) +{ + auto* const map = dict->get_if(); + if (map == nullptr) + { + return; + } + + map->try_emplace(TR_KEY_queuePosition, tor->queue_position()); + map->try_emplace(TR_KEY_is_queued, tor->is_queued(tor->queue_direction())); +} + +auto loadQueueState(tr_variant* dict, tr_torrent* tor, tr_torrent::ResumeHelper& helper) +{ + auto ret = tr_resume::fields_t{}; + auto const* const map = dict->get_if(); + if (map == nullptr) + { + return ret; + } + + if (auto val = map->value_if(TR_KEY_queuePosition)) + { + helper.load_queue_position(*val); + ret = tr_resume::QueueState; + } + + if (auto val = map->value_if(TR_KEY_is_queued)) + { + tor->set_is_queued(*val); + ret = tr_resume::QueueState; + } + + return ret; +} + +// --- + void bitfieldToRaw(tr_bitfield const& b, tr_variant* benc) { if (b.has_none() || std::empty(b)) @@ -784,6 +822,11 @@ tr_resume::fields_t load_from_file(tr_torrent* tor, tr_torrent::ResumeHelper& he fields_loaded |= loadGroup(&top, tor); } + if ((fields_to_load & tr_resume::QueueState) != 0) + { + fields_loaded |= loadQueueState(&top, tor, helper); + } + return fields_loaded; } @@ -905,6 +948,7 @@ void save(tr_torrent* const tor, tr_torrent::ResumeHelper const& helper) saveName(&top, tor); saveLabels(&top, tor); saveGroup(&top, tor); + saveQueueState(&top, tor); auto serde = tr_variant_serde::benc(); if (!serde.to_file(top, tor->resume_file())) diff --git a/libtransmission/resume.h b/libtransmission/resume.h index c9449bdb6..800ae2aff 100644 --- a/libtransmission/resume.h +++ b/libtransmission/resume.h @@ -45,6 +45,7 @@ auto inline constexpr Name = fields_t{ 1 << 21 }; auto inline constexpr Labels = fields_t{ 1 << 22 }; auto inline constexpr Group = fields_t{ 1 << 23 }; auto inline constexpr SequentialDownload = fields_t{ 1 << 24 }; +auto inline constexpr QueueState = fields_t{ 1 << 25 }; auto inline constexpr All = ~fields_t{ 0 }; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index d83c2908d..268d6181b 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -513,12 +513,14 @@ void tr_torrent::set_unique_queue_position(size_t const new_pos) { --walk->queue_position_; walk->mark_changed(); + walk->set_dirty(); } if ((old_pos > new_pos) && (new_pos <= walk->queue_position_) && (walk->queue_position_ < old_pos)) { ++walk->queue_position_; walk->mark_changed(); + walk->set_dirty(); } max_pos = std::max(max_pos, walk->queue_position_); @@ -526,6 +528,7 @@ void tr_torrent::set_unique_queue_position(size_t const new_pos) queue_position_ = std::min(new_pos, max_pos + 1); mark_changed(); + set_dirty(); TR_ASSERT(torrents_are_sorted_by_queue_position(torrents.get_all())); } @@ -2727,6 +2730,13 @@ void tr_torrent::ResumeHelper::load_incomplete_dir(std::string_view const dir) n // --- +void tr_torrent::ResumeHelper::load_queue_position(size_t pos) noexcept +{ + tor_.queue_position_ = pos; +} + +// --- + void tr_torrent::ResumeHelper::load_start_when_stable(bool const val) noexcept { tor_.start_when_stable_ = val; diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 6efe19a4b..cb77927e1 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -76,6 +76,7 @@ struct tr_torrent void load_date_done(time_t when) noexcept; void load_download_dir(std::string_view dir) noexcept; void load_incomplete_dir(std::string_view dir) noexcept; + void load_queue_position(size_t pos) noexcept; void load_seconds_downloading_before_current_start(time_t when) noexcept; void load_seconds_seeding_before_current_start(time_t when) noexcept; void load_start_when_stable(bool val) noexcept;