feat: save queue order between sessions (#6753)

Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
Yat Ho 2024-10-14 00:36:38 +08:00 committed by GitHub
parent 45f5a19950
commit 4db50dae10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 0 deletions

View File

@ -170,6 +170,7 @@ auto constexpr MyStatic = std::array<std::string_view, TR_N_KEYS>{
"isStalled"sv, "isStalled"sv,
"isUTP"sv, "isUTP"sv,
"isUploadingTo"sv, "isUploadingTo"sv,
"is_queued"sv,
"labels"sv, "labels"sv,
"lastAnnouncePeerCount"sv, "lastAnnouncePeerCount"sv,
"lastAnnounceResult"sv, "lastAnnounceResult"sv,

View File

@ -172,6 +172,7 @@ enum
TR_KEY_isStalled, TR_KEY_isStalled,
TR_KEY_isUTP, TR_KEY_isUTP,
TR_KEY_isUploadingTo, TR_KEY_isUploadingTo,
TR_KEY_is_queued,
TR_KEY_labels, TR_KEY_labels,
TR_KEY_lastAnnouncePeerCount, TR_KEY_lastAnnouncePeerCount,
TR_KEY_lastAnnounceResult, TR_KEY_lastAnnounceResult,

View File

@ -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<tr_variant::Map>();
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<tr_variant::Map>();
if (map == nullptr)
{
return ret;
}
if (auto val = map->value_if<int64_t>(TR_KEY_queuePosition))
{
helper.load_queue_position(*val);
ret = tr_resume::QueueState;
}
if (auto val = map->value_if<bool>(TR_KEY_is_queued))
{
tor->set_is_queued(*val);
ret = tr_resume::QueueState;
}
return ret;
}
// ---
void bitfieldToRaw(tr_bitfield const& b, tr_variant* benc) void bitfieldToRaw(tr_bitfield const& b, tr_variant* benc)
{ {
if (b.has_none() || std::empty(b)) 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); fields_loaded |= loadGroup(&top, tor);
} }
if ((fields_to_load & tr_resume::QueueState) != 0)
{
fields_loaded |= loadQueueState(&top, tor, helper);
}
return fields_loaded; return fields_loaded;
} }
@ -905,6 +948,7 @@ void save(tr_torrent* const tor, tr_torrent::ResumeHelper const& helper)
saveName(&top, tor); saveName(&top, tor);
saveLabels(&top, tor); saveLabels(&top, tor);
saveGroup(&top, tor); saveGroup(&top, tor);
saveQueueState(&top, tor);
auto serde = tr_variant_serde::benc(); auto serde = tr_variant_serde::benc();
if (!serde.to_file(top, tor->resume_file())) if (!serde.to_file(top, tor->resume_file()))

View File

@ -45,6 +45,7 @@ auto inline constexpr Name = fields_t{ 1 << 21 };
auto inline constexpr Labels = fields_t{ 1 << 22 }; auto inline constexpr Labels = fields_t{ 1 << 22 };
auto inline constexpr Group = fields_t{ 1 << 23 }; auto inline constexpr Group = fields_t{ 1 << 23 };
auto inline constexpr SequentialDownload = fields_t{ 1 << 24 }; auto inline constexpr SequentialDownload = fields_t{ 1 << 24 };
auto inline constexpr QueueState = fields_t{ 1 << 25 };
auto inline constexpr All = ~fields_t{ 0 }; auto inline constexpr All = ~fields_t{ 0 };

View File

@ -513,12 +513,14 @@ void tr_torrent::set_unique_queue_position(size_t const new_pos)
{ {
--walk->queue_position_; --walk->queue_position_;
walk->mark_changed(); walk->mark_changed();
walk->set_dirty();
} }
if ((old_pos > new_pos) && (new_pos <= walk->queue_position_) && (walk->queue_position_ < old_pos)) if ((old_pos > new_pos) && (new_pos <= walk->queue_position_) && (walk->queue_position_ < old_pos))
{ {
++walk->queue_position_; ++walk->queue_position_;
walk->mark_changed(); walk->mark_changed();
walk->set_dirty();
} }
max_pos = std::max(max_pos, walk->queue_position_); 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); queue_position_ = std::min(new_pos, max_pos + 1);
mark_changed(); mark_changed();
set_dirty();
TR_ASSERT(torrents_are_sorted_by_queue_position(torrents.get_all())); 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 void tr_torrent::ResumeHelper::load_start_when_stable(bool const val) noexcept
{ {
tor_.start_when_stable_ = val; tor_.start_when_stable_ = val;

View File

@ -76,6 +76,7 @@ struct tr_torrent
void load_date_done(time_t when) noexcept; void load_date_done(time_t when) noexcept;
void load_download_dir(std::string_view dir) noexcept; void load_download_dir(std::string_view dir) noexcept;
void load_incomplete_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_downloading_before_current_start(time_t when) noexcept;
void load_seconds_seeding_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; void load_start_when_stable(bool val) noexcept;