1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 01:03:01 +00:00
transmission/libtransmission/session-alt-speeds.cc

118 lines
3.2 KiB
C++

// This file Copyright © 2022-2023 Mnemosyne LLC.
// 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.
#include <fmt/chrono.h>
#include "libtransmission/transmission.h"
#include "libtransmission/log.h"
#include "libtransmission/session-alt-speeds.h"
#include "libtransmission/variant.h"
#include "libtransmission/utils.h" // for _()
using namespace std::literals;
void tr_session_alt_speeds::load(tr_variant* src)
{
#define V(key, field, type, default_value, comment) \
if (auto* const child = tr_variantDictFind(src, key); child != nullptr) \
{ \
if (auto val = libtransmission::VariantConverter::load<decltype(field)>(child); val) \
{ \
this->field = *val; \
} \
}
ALT_SPEEDS_FIELDS(V)
#undef V
update_scheduler();
}
void tr_session_alt_speeds::save(tr_variant* tgt) const
{
#define V(key, field, type, default_value, comment) \
tr_variantDictRemove(tgt, key); \
libtransmission::VariantConverter::save<decltype(field)>(tr_variantDictAdd(tgt, key), field);
ALT_SPEEDS_FIELDS(V)
#undef V
}
void tr_session_alt_speeds::default_settings(tr_variant* tgt)
{
#define V(key, field, type, default_value, comment) \
{ \
type const val = default_value; \
tr_variantDictRemove(tgt, key); \
libtransmission::VariantConverter::save<decltype(field)>(tr_variantDictAdd(tgt, key), val); \
}
ALT_SPEEDS_FIELDS(V)
#undef V
}
// --- minutes
void tr_session_alt_speeds::update_minutes()
{
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);
}
}
}
}
void tr_session_alt_speeds::update_scheduler()
{
update_minutes();
scheduler_set_is_active_to_.reset();
check_scheduler();
}
void tr_session_alt_speeds::check_scheduler()
{
if (!is_scheduler_enabled())
{
return;
}
if (auto const active = is_active_minute(mediator_.time());
!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;
set_active(active, ChangeReason::Scheduler);
}
}
void tr_session_alt_speeds::set_active(bool active, ChangeReason reason)
{
if (is_active_ != active)
{
is_active_ = active;
mediator_.is_active_changed(is_active_, reason);
}
}
[[nodiscard]] bool tr_session_alt_speeds::is_active_minute(time_t time) const noexcept
{
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);
}