2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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 <cstddef> // size_t
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <ctime>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <utility>
|
2023-07-08 15:24:03 +00:00
|
|
|
|
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;
|
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
void tr_session_alt_speeds::load(Settings&& settings)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2024-02-15 17:31:09 +00:00
|
|
|
settings_ = std::move(settings);
|
2023-05-06 04:11:05 +00:00
|
|
|
update_scheduler();
|
2024-07-10 23:55:34 +00:00
|
|
|
set_active(settings_.is_active, ChangeReason::LoadSettings, true);
|
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)
|
|
|
|
{
|
2024-02-15 17:31:09 +00:00
|
|
|
if ((static_cast<tr_sched_day>(settings_.use_on_these_weekdays) & (1 << day)) != 0)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2024-02-15 17:31:09 +00:00
|
|
|
auto const begin = settings_.minute_begin;
|
|
|
|
auto const end = settings_.minute_end > settings_.minute_begin ? settings_.minute_end :
|
|
|
|
settings_.minute_end + MinutesPerDay;
|
2022-11-02 00:32:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 23:55:34 +00:00
|
|
|
void tr_session_alt_speeds::set_active(bool active, ChangeReason reason, bool force)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2024-07-10 23:55:34 +00:00
|
|
|
if (auto& tgt = settings_.is_active; force || tgt != active)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2024-02-15 17:31:09 +00:00
|
|
|
tgt = active;
|
|
|
|
mediator_.is_active_changed(tgt, 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);
|
|
|
|
}
|