2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-2023 Mnemosyne LLC.
|
2022-11-02 00:32:26 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef> // size_t
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
#include <fmt/chrono.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-11-02 00:32:26 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/session-alt-speeds.h"
|
|
|
|
#include "libtransmission/variant.h"
|
|
|
|
#include "libtransmission/utils.h" // for _()
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-09-08 00:05:16 +00:00
|
|
|
void tr_session_alt_speeds::load(tr_variant const& src)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-09-08 00:05:16 +00:00
|
|
|
auto const* const src_map = src.get_if<tr_variant::Map>();
|
|
|
|
if (src_map != nullptr)
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
#define V(key, field, type, default_value, comment) \
|
2023-09-08 00:05:16 +00:00
|
|
|
if (auto const iter = src_map->find(key); iter != std::end(*src_map)) \
|
2022-11-02 00:32:26 +00:00
|
|
|
{ \
|
2023-09-08 00:05:16 +00:00
|
|
|
if (auto val = libtransmission::VariantConverter::load<decltype(field)>(iter->second); val) \
|
2022-11-02 00:32:26 +00:00
|
|
|
{ \
|
|
|
|
this->field = *val; \
|
|
|
|
} \
|
|
|
|
}
|
2023-09-08 00:05:16 +00:00
|
|
|
ALT_SPEEDS_FIELDS(V)
|
2022-11-02 00:32:26 +00:00
|
|
|
#undef V
|
2023-09-08 00:05:16 +00:00
|
|
|
}
|
2022-11-02 00:32:26 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
update_scheduler();
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:05:16 +00:00
|
|
|
tr_variant tr_session_alt_speeds::settings() const
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-09-08 00:05:16 +00:00
|
|
|
auto settings = tr_variant::Map{};
|
2022-11-02 00:32:26 +00:00
|
|
|
#define V(key, field, type, default_value, comment) \
|
2023-09-08 00:05:16 +00:00
|
|
|
settings.try_emplace(key, libtransmission::VariantConverter::save<decltype(field)>(field));
|
2022-11-02 00:32:26 +00:00
|
|
|
ALT_SPEEDS_FIELDS(V)
|
|
|
|
#undef V
|
2023-09-08 00:05:16 +00:00
|
|
|
return tr_variant{ std::move(settings) };
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:05:16 +00:00
|
|
|
tr_variant tr_session_alt_speeds::default_settings()
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-09-08 00:05:16 +00:00
|
|
|
auto settings = tr_variant::Map{};
|
2022-11-02 00:32:26 +00:00
|
|
|
#define V(key, field, type, default_value, comment) \
|
2023-09-08 00:05:16 +00:00
|
|
|
settings.try_emplace(key, libtransmission::VariantConverter::save<decltype(field)>(static_cast<type>(default_value)));
|
2022-11-02 00:32:26 +00:00
|
|
|
ALT_SPEEDS_FIELDS(V)
|
|
|
|
#undef V
|
2023-09-08 00:05:16 +00:00
|
|
|
return tr_variant{ std::move(settings) };
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- minutes
|
2022-11-02 00:32:26 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session_alt_speeds::update_minutes()
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
|
|
|
minutes_.reset();
|
|
|
|
|
|
|
|
for (int day = 0; day < 7; ++day)
|
|
|
|
{
|
|
|
|
if ((static_cast<tr_sched_day>(use_on_these_weekdays_) & (1 << day)) != 0)
|
|
|
|
{
|
|
|
|
auto const begin = minute_begin_;
|
|
|
|
auto const end = minute_end_ > minute_begin_ ? minute_end_ : minute_end_ + MinutesPerDay;
|
|
|
|
for (auto i = begin; i < end; ++i)
|
|
|
|
{
|
|
|
|
minutes_.set((i + day * MinutesPerDay) % MinutesPerWeek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session_alt_speeds::update_scheduler()
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
update_minutes();
|
2022-11-02 00:32:26 +00:00
|
|
|
scheduler_set_is_active_to_.reset();
|
2023-05-06 04:11:05 +00:00
|
|
|
check_scheduler();
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session_alt_speeds::check_scheduler()
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
if (!is_scheduler_enabled())
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
if (auto const active = is_active_minute(mediator_.time());
|
2022-11-02 00:32:26 +00:00
|
|
|
!scheduler_set_is_active_to_ || scheduler_set_is_active_to_ != active)
|
|
|
|
{
|
|
|
|
tr_logAddInfo(active ? _("Time to turn on turtle mode") : _("Time to turn off turtle mode"));
|
|
|
|
scheduler_set_is_active_to_ = active;
|
2023-05-06 04:11:05 +00:00
|
|
|
set_active(active, ChangeReason::Scheduler);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session_alt_speeds::set_active(bool active, ChangeReason reason)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
|
|
|
if (is_active_ != active)
|
|
|
|
{
|
|
|
|
is_active_ = active;
|
2023-05-06 04:11:05 +00:00
|
|
|
mediator_.is_active_changed(is_active_, reason);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] bool tr_session_alt_speeds::is_active_minute(time_t time) const noexcept
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
|
|
|
auto const tm = fmt::localtime(time);
|
|
|
|
|
|
|
|
size_t minute_of_the_week = tm.tm_wday * MinutesPerDay + tm.tm_hour * MinutesPerHour + tm.tm_min;
|
|
|
|
|
|
|
|
if (minute_of_the_week >= MinutesPerWeek) /* leap minutes? */
|
|
|
|
{
|
|
|
|
minute_of_the_week = MinutesPerWeek - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return minutes_.test(minute_of_the_week);
|
|
|
|
}
|