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 <array>
|
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <cstdint> // int64_t, uint32_t
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <utility>
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-11-02 00:32:26 +00:00
|
|
|
|
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" // for tr_log_level
|
|
|
|
#include "libtransmission/net.h" // for tr_port
|
2023-06-30 14:49:58 +00:00
|
|
|
#include "libtransmission/utils.h" // for tr_strv_strip(), tr_strlower()
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/variant.h"
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-18 08:09:29 +00:00
|
|
|
namespace
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
2023-01-18 08:09:29 +00:00
|
|
|
auto constexpr EncryptionKeys = std::array<std::pair<std::string_view, tr_encryption_mode>, 3>{ {
|
|
|
|
{ "required", TR_ENCRYPTION_REQUIRED },
|
|
|
|
{ "preferred", TR_ENCRYPTION_PREFERRED },
|
|
|
|
{ "allowed", TR_CLEAR_PREFERRED },
|
|
|
|
} };
|
|
|
|
|
|
|
|
auto constexpr LogKeys = std::array<std::pair<std::string_view, tr_log_level>, 7>{ {
|
|
|
|
{ "critical", TR_LOG_CRITICAL },
|
|
|
|
{ "debug", TR_LOG_DEBUG },
|
|
|
|
{ "error", TR_LOG_ERROR },
|
|
|
|
{ "info", TR_LOG_INFO },
|
|
|
|
{ "off", TR_LOG_OFF },
|
|
|
|
{ "trace", TR_LOG_TRACE },
|
|
|
|
{ "warn", TR_LOG_WARN },
|
|
|
|
} };
|
2022-11-02 00:32:26 +00:00
|
|
|
|
2023-01-18 08:09:29 +00:00
|
|
|
auto constexpr PreallocationKeys = std::array<std::pair<std::string_view, tr_preallocation_mode>, 5>{ {
|
|
|
|
{ "off", TR_PREALLOCATE_NONE },
|
|
|
|
{ "none", TR_PREALLOCATE_NONE },
|
|
|
|
{ "fast", TR_PREALLOCATE_SPARSE },
|
|
|
|
{ "sparse", TR_PREALLOCATE_SPARSE },
|
|
|
|
{ "full", TR_PREALLOCATE_FULL },
|
|
|
|
} };
|
|
|
|
|
|
|
|
auto constexpr VerifyModeKeys = std::array<std::pair<std::string_view, tr_verify_added_mode>, 2>{ {
|
|
|
|
{ "fast", TR_VERIFY_ADDED_FAST },
|
|
|
|
{ "full", TR_VERIFY_ADDED_FULL },
|
|
|
|
} };
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
template<>
|
|
|
|
std::optional<bool> VariantConverter::load<bool>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = bool{}; tr_variantGetBool(src, &val))
|
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<bool>(tr_variant* tgt, bool const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitBool(tgt, val);
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<double> VariantConverter::load<double>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = double{}; tr_variantGetReal(src, &val))
|
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<double>(tr_variant* tgt, double const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitReal(tgt, val);
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_encryption_mode> VariantConverter::load<tr_encryption_mode>(tr_variant* src)
|
|
|
|
{
|
2023-01-18 08:09:29 +00:00
|
|
|
static constexpr auto Keys = EncryptionKeys;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const needle = tr_strlower(tr_strv_strip(val));
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
for (auto const& [key, encryption] : Keys)
|
|
|
|
{
|
|
|
|
if (key == needle)
|
|
|
|
{
|
|
|
|
return encryption;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
for (auto const& [key, encryption] : Keys)
|
|
|
|
{
|
|
|
|
if (encryption == val)
|
|
|
|
{
|
|
|
|
return encryption;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_encryption_mode>(tr_variant* tgt, tr_encryption_mode const& val)
|
|
|
|
{
|
2022-11-26 13:32:51 +00:00
|
|
|
tr_variantInitInt(tgt, val);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_log_level> VariantConverter::load<tr_log_level>(tr_variant* src)
|
|
|
|
{
|
2023-01-18 08:09:29 +00:00
|
|
|
static constexpr auto Keys = LogKeys;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const needle = tr_strlower(tr_strv_strip(val));
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
for (auto const& [name, log_level] : Keys)
|
|
|
|
{
|
|
|
|
if (needle == name)
|
|
|
|
{
|
|
|
|
return log_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
for (auto const& [name, log_level] : Keys)
|
|
|
|
{
|
|
|
|
if (log_level == val)
|
|
|
|
{
|
|
|
|
return log_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_log_level>(tr_variant* tgt, tr_log_level const& val)
|
|
|
|
{
|
2022-11-26 13:32:51 +00:00
|
|
|
tr_variantInitInt(tgt, val);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_mode_t> VariantConverter::load<tr_mode_t>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
if (auto const mode = tr_num_parse<uint32_t>(val, nullptr, 8); mode)
|
2022-11-02 00:32:26 +00:00
|
|
|
{
|
|
|
|
return static_cast<tr_mode_t>(*mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
return static_cast<tr_mode_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_mode_t>(tr_variant* tgt, tr_mode_t const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitStr(tgt, fmt::format("{:#03o}", val));
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_port> VariantConverter::load<tr_port>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return tr_port::from_host(val);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_port>(tr_variant* tgt, tr_port const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitInt(tgt, val.host());
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_preallocation_mode> VariantConverter::load<tr_preallocation_mode>(tr_variant* src)
|
|
|
|
{
|
2023-01-18 08:09:29 +00:00
|
|
|
static constexpr auto Keys = PreallocationKeys;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const needle = tr_strlower(tr_strv_strip(val));
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (name == needle)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (value == val)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_preallocation_mode>(tr_variant* tgt, tr_preallocation_mode const& val)
|
|
|
|
{
|
2022-11-26 13:32:51 +00:00
|
|
|
tr_variantInitInt(tgt, val);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<size_t> VariantConverter::load<size_t>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
return static_cast<size_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<size_t>(tr_variant* tgt, size_t const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitInt(tgt, val);
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<std::string> VariantConverter::load<std::string>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
|
|
|
return std::string{ val };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<std::string>(tr_variant* tgt, std::string const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitStr(tgt, val);
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_tos_t> VariantConverter::load<tr_tos_t>(tr_variant* src)
|
|
|
|
{
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
return tr_tos_t::from_string(val);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
return tr_tos_t{ static_cast<int>(val) };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_tos_t>(tr_variant* tgt, tr_tos_t const& val)
|
|
|
|
{
|
|
|
|
tr_variantInitStr(tgt, val.toString());
|
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:29 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::optional<tr_verify_added_mode> VariantConverter::load<tr_verify_added_mode>(tr_variant* src)
|
|
|
|
{
|
|
|
|
static constexpr auto& Keys = VerifyModeKeys;
|
|
|
|
|
|
|
|
if (auto val = std::string_view{}; tr_variantGetStrView(src, &val))
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const needle = tr_strlower(tr_strv_strip(val));
|
2023-01-18 08:09:29 +00:00
|
|
|
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (name == needle)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantGetInt(src, &val))
|
|
|
|
{
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (value == val)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void VariantConverter::save<tr_verify_added_mode>(tr_variant* tgt, tr_verify_added_mode const& val)
|
|
|
|
{
|
|
|
|
for (auto const& [key, value] : VerifyModeKeys)
|
|
|
|
{
|
|
|
|
if (value == val)
|
|
|
|
{
|
|
|
|
tr_variantInitStrView(tgt, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantInitInt(tgt, val);
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
} // namespace libtransmission
|