2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
2015-06-12 22:41:36 +00:00
|
|
|
#include <cstdlib>
|
2020-09-09 14:24:39 +00:00
|
|
|
#include <string_view>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-12 22:12:12 +00:00
|
|
|
#include <QDateTime>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2020-11-08 19:54:40 +00:00
|
|
|
#include <QTextCodec>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include "CustomVariantType.h"
|
|
|
|
#include "Prefs.h"
|
2020-07-27 04:30:58 +00:00
|
|
|
#include "VariantHelpers.h"
|
|
|
|
|
|
|
|
using ::trqt::variant_helpers::dictAdd;
|
|
|
|
using ::trqt::variant_helpers::getValue;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
std::array<Prefs::PrefItem, Prefs::PREFS_COUNT> const Prefs::Items
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* gui settings */
|
2020-06-05 19:02:11 +00:00
|
|
|
PrefItem{ OPTIONS_PROMPT, TR_KEY_show_options_window, QVariant::Bool },
|
2017-04-19 12:04:45 +00:00
|
|
|
{ OPEN_DIALOG_FOLDER, TR_KEY_open_dialog_dir, QVariant::String },
|
|
|
|
{ INHIBIT_HIBERNATION, TR_KEY_inhibit_desktop_hibernation, QVariant::Bool },
|
|
|
|
{ DIR_WATCH, TR_KEY_watch_dir, QVariant::String },
|
|
|
|
{ DIR_WATCH_ENABLED, TR_KEY_watch_dir_enabled, QVariant::Bool },
|
|
|
|
{ SHOW_TRAY_ICON, TR_KEY_show_notification_area_icon, QVariant::Bool },
|
|
|
|
{ START_MINIMIZED, TR_KEY_start_minimized, QVariant::Bool },
|
|
|
|
{ SHOW_NOTIFICATION_ON_ADD, TR_KEY_torrent_added_notification_enabled, QVariant::Bool },
|
|
|
|
{ SHOW_NOTIFICATION_ON_COMPLETE, TR_KEY_torrent_complete_notification_enabled, QVariant::Bool },
|
|
|
|
{ ASKQUIT, TR_KEY_prompt_before_exit, QVariant::Bool },
|
|
|
|
{ SORT_MODE, TR_KEY_sort_mode, CustomVariantType::SortModeType },
|
|
|
|
{ SORT_REVERSED, TR_KEY_sort_reversed, QVariant::Bool },
|
|
|
|
{ COMPACT_VIEW, TR_KEY_compact_view, QVariant::Bool },
|
|
|
|
{ FILTERBAR, TR_KEY_show_filterbar, QVariant::Bool },
|
|
|
|
{ STATUSBAR, TR_KEY_show_statusbar, QVariant::Bool },
|
|
|
|
{ STATUSBAR_STATS, TR_KEY_statusbar_stats, QVariant::String },
|
|
|
|
{ SHOW_TRACKER_SCRAPES, TR_KEY_show_extra_peer_details, QVariant::Bool },
|
|
|
|
{ SHOW_BACKUP_TRACKERS, TR_KEY_show_backup_trackers, QVariant::Bool },
|
|
|
|
{ TOOLBAR, TR_KEY_show_toolbar, QVariant::Bool },
|
|
|
|
{ BLOCKLIST_DATE, TR_KEY_blocklist_date, QVariant::DateTime },
|
|
|
|
{ BLOCKLIST_UPDATES_ENABLED, TR_KEY_blocklist_updates_enabled, QVariant::Bool },
|
|
|
|
{ MAIN_WINDOW_LAYOUT_ORDER, TR_KEY_main_window_layout_order, QVariant::String },
|
|
|
|
{ MAIN_WINDOW_HEIGHT, TR_KEY_main_window_height, QVariant::Int },
|
|
|
|
{ MAIN_WINDOW_WIDTH, TR_KEY_main_window_width, QVariant::Int },
|
|
|
|
{ MAIN_WINDOW_X, TR_KEY_main_window_x, QVariant::Int },
|
|
|
|
{ MAIN_WINDOW_Y, TR_KEY_main_window_y, QVariant::Int },
|
|
|
|
{ FILTER_MODE, TR_KEY_filter_mode, CustomVariantType::FilterModeType },
|
|
|
|
{ FILTER_TRACKERS, TR_KEY_filter_trackers, QVariant::String },
|
|
|
|
{ FILTER_TEXT, TR_KEY_filter_text, QVariant::String },
|
|
|
|
{ SESSION_IS_REMOTE, TR_KEY_remote_session_enabled, QVariant::Bool },
|
|
|
|
{ SESSION_REMOTE_HOST, TR_KEY_remote_session_host, QVariant::String },
|
|
|
|
{ SESSION_REMOTE_PORT, TR_KEY_remote_session_port, QVariant::Int },
|
|
|
|
{ SESSION_REMOTE_AUTH, TR_KEY_remote_session_requres_authentication, QVariant::Bool },
|
|
|
|
{ SESSION_REMOTE_USERNAME, TR_KEY_remote_session_username, QVariant::String },
|
|
|
|
{ SESSION_REMOTE_PASSWORD, TR_KEY_remote_session_password, QVariant::String },
|
|
|
|
{ COMPLETE_SOUND_COMMAND, TR_KEY_torrent_complete_sound_command, QVariant::String },
|
|
|
|
{ COMPLETE_SOUND_ENABLED, TR_KEY_torrent_complete_sound_enabled, QVariant::Bool },
|
|
|
|
{ USER_HAS_GIVEN_INFORMED_CONSENT, TR_KEY_user_has_given_informed_consent, QVariant::Bool },
|
|
|
|
|
|
|
|
/* libtransmission settings */
|
|
|
|
{ ALT_SPEED_LIMIT_UP, TR_KEY_alt_speed_up, QVariant::Int },
|
|
|
|
{ ALT_SPEED_LIMIT_DOWN, TR_KEY_alt_speed_down, QVariant::Int },
|
|
|
|
{ ALT_SPEED_LIMIT_ENABLED, TR_KEY_alt_speed_enabled, QVariant::Bool },
|
|
|
|
{ ALT_SPEED_LIMIT_TIME_BEGIN, TR_KEY_alt_speed_time_begin, QVariant::Int },
|
|
|
|
{ ALT_SPEED_LIMIT_TIME_END, TR_KEY_alt_speed_time_end, QVariant::Int },
|
|
|
|
{ ALT_SPEED_LIMIT_TIME_ENABLED, TR_KEY_alt_speed_time_enabled, QVariant::Bool },
|
|
|
|
{ ALT_SPEED_LIMIT_TIME_DAY, TR_KEY_alt_speed_time_day, QVariant::Int },
|
|
|
|
{ BLOCKLIST_ENABLED, TR_KEY_blocklist_enabled, QVariant::Bool },
|
|
|
|
{ BLOCKLIST_URL, TR_KEY_blocklist_url, QVariant::String },
|
|
|
|
{ DSPEED, TR_KEY_speed_limit_down, QVariant::Int },
|
|
|
|
{ DSPEED_ENABLED, TR_KEY_speed_limit_down_enabled, QVariant::Bool },
|
|
|
|
{ DOWNLOAD_DIR, TR_KEY_download_dir, QVariant::String },
|
|
|
|
{ DOWNLOAD_QUEUE_ENABLED, TR_KEY_download_queue_enabled, QVariant::Bool },
|
|
|
|
{ DOWNLOAD_QUEUE_SIZE, TR_KEY_download_queue_size, QVariant::Int },
|
|
|
|
{ ENCRYPTION, TR_KEY_encryption, QVariant::Int },
|
|
|
|
{ IDLE_LIMIT, TR_KEY_idle_seeding_limit, QVariant::Int },
|
|
|
|
{ IDLE_LIMIT_ENABLED, TR_KEY_idle_seeding_limit_enabled, QVariant::Bool },
|
|
|
|
{ INCOMPLETE_DIR, TR_KEY_incomplete_dir, QVariant::String },
|
|
|
|
{ INCOMPLETE_DIR_ENABLED, TR_KEY_incomplete_dir_enabled, QVariant::Bool },
|
|
|
|
{ MSGLEVEL, TR_KEY_message_level, QVariant::Int },
|
|
|
|
{ PEER_LIMIT_GLOBAL, TR_KEY_peer_limit_global, QVariant::Int },
|
|
|
|
{ PEER_LIMIT_TORRENT, TR_KEY_peer_limit_per_torrent, QVariant::Int },
|
|
|
|
{ PEER_PORT, TR_KEY_peer_port, QVariant::Int },
|
|
|
|
{ PEER_PORT_RANDOM_ON_START, TR_KEY_peer_port_random_on_start, QVariant::Bool },
|
|
|
|
{ PEER_PORT_RANDOM_LOW, TR_KEY_peer_port_random_low, QVariant::Int },
|
|
|
|
{ PEER_PORT_RANDOM_HIGH, TR_KEY_peer_port_random_high, QVariant::Int },
|
|
|
|
{ QUEUE_STALLED_MINUTES, TR_KEY_queue_stalled_minutes, QVariant::Int },
|
|
|
|
{ SCRIPT_TORRENT_DONE_ENABLED, TR_KEY_script_torrent_done_enabled, QVariant::Bool },
|
|
|
|
{ SCRIPT_TORRENT_DONE_FILENAME, TR_KEY_script_torrent_done_filename, QVariant::String },
|
|
|
|
{ SOCKET_TOS, TR_KEY_peer_socket_tos, QVariant::Int },
|
|
|
|
{ START, TR_KEY_start_added_torrents, QVariant::Bool },
|
|
|
|
{ TRASH_ORIGINAL, TR_KEY_trash_original_torrent_files, QVariant::Bool },
|
|
|
|
{ PEX_ENABLED, TR_KEY_pex_enabled, QVariant::Bool },
|
|
|
|
{ DHT_ENABLED, TR_KEY_dht_enabled, QVariant::Bool },
|
|
|
|
{ UTP_ENABLED, TR_KEY_utp_enabled, QVariant::Bool },
|
|
|
|
{ LPD_ENABLED, TR_KEY_lpd_enabled, QVariant::Bool },
|
|
|
|
{ PORT_FORWARDING, TR_KEY_port_forwarding_enabled, QVariant::Bool },
|
|
|
|
{ PREALLOCATION, TR_KEY_preallocation, QVariant::Int },
|
|
|
|
{ RATIO, TR_KEY_ratio_limit, QVariant::Double },
|
|
|
|
{ RATIO_ENABLED, TR_KEY_ratio_limit_enabled, QVariant::Bool },
|
|
|
|
{ RENAME_PARTIAL_FILES, TR_KEY_rename_partial_files, QVariant::Bool },
|
|
|
|
{ RPC_AUTH_REQUIRED, TR_KEY_rpc_authentication_required, QVariant::Bool },
|
|
|
|
{ RPC_ENABLED, TR_KEY_rpc_enabled, QVariant::Bool },
|
|
|
|
{ RPC_PASSWORD, TR_KEY_rpc_password, QVariant::String },
|
|
|
|
{ RPC_PORT, TR_KEY_rpc_port, QVariant::Int },
|
|
|
|
{ RPC_USERNAME, TR_KEY_rpc_username, QVariant::String },
|
|
|
|
{ RPC_WHITELIST_ENABLED, TR_KEY_rpc_whitelist_enabled, QVariant::Bool },
|
|
|
|
{ RPC_WHITELIST, TR_KEY_rpc_whitelist, QVariant::String },
|
|
|
|
{ USPEED_ENABLED, TR_KEY_speed_limit_up_enabled, QVariant::Bool },
|
|
|
|
{ USPEED, TR_KEY_speed_limit_up, QVariant::Int },
|
|
|
|
{ UPLOAD_SLOTS_PER_TORRENT, TR_KEY_upload_slots_per_torrent, QVariant::Int }
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
2020-09-09 14:24:39 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
auto const FilterModes = std::array<std::pair<int, std::string_view>, FilterMode::NUM_MODES>
|
|
|
|
{{
|
|
|
|
{ FilterMode::SHOW_ALL, "show-all" },
|
|
|
|
{ FilterMode::SHOW_ACTIVE, "show-active" },
|
|
|
|
{ FilterMode::SHOW_DOWNLOADING, "show-downloading" },
|
|
|
|
{ FilterMode::SHOW_SEEDING, "show-seeding" },
|
|
|
|
{ FilterMode::SHOW_PAUSED, "show-paused" },
|
|
|
|
{ FilterMode::SHOW_FINISHED, "show-finished" },
|
|
|
|
{ FilterMode::SHOW_VERIFYING, "show-verifying" },
|
|
|
|
{ FilterMode::SHOW_ERROR, "show-error" }
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto const SortModes = std::array<std::pair<int, std::string_view>, SortMode::NUM_MODES>
|
|
|
|
{{
|
|
|
|
{ SortMode::SORT_BY_NAME, "sort-by-name" },
|
|
|
|
{ SortMode::SORT_BY_ACTIVITY, "sort-by-activity" },
|
|
|
|
{ SortMode::SORT_BY_AGE, "sort-by-age" },
|
|
|
|
{ SortMode::SORT_BY_ETA, "sort-by-eta" },
|
|
|
|
{ SortMode::SORT_BY_PROGRESS, "sort-by-progress" },
|
|
|
|
{ SortMode::SORT_BY_QUEUE, "sort-by-queue" },
|
|
|
|
{ SortMode::SORT_BY_RATIO, "sort-by-ratio" },
|
|
|
|
{ SortMode::SORT_BY_SIZE, "sort-by-size" },
|
|
|
|
{ SortMode::SORT_BY_STATE, "sort-by-state" },
|
|
|
|
{ SortMode::SORT_BY_ID, "sort-by-id" }
|
|
|
|
}};
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
bool isValidUtf8(QByteArray const& byteArray)
|
|
|
|
{
|
|
|
|
auto const* const codec = QTextCodec::codecForName("UTF-8");
|
|
|
|
auto state = QTextCodec::ConverterState {};
|
|
|
|
auto const text = codec->toUnicode(byteArray.constData(), byteArray.size(), &state);
|
|
|
|
return state.invalidChars == 0;
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:24:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
Prefs::Prefs(QString config_dir) :
|
2020-09-09 14:24:39 +00:00
|
|
|
config_dir_(std::move(config_dir))
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
static_assert(sizeof(Items) / sizeof(Items[0]) == PREFS_COUNT);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0; i < PREFS_COUNT; ++i)
|
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[i].id == i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// these are the prefs that don't get saved to settings.json
|
|
|
|
// when the application exits.
|
2020-05-27 21:53:12 +00:00
|
|
|
temporary_prefs_ << FILTER_TEXT;
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
tr_variantInitDict(&top, 0);
|
|
|
|
initDefaults(&top);
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionLoadSettings(&top, config_dir_.toUtf8().constData(), nullptr);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0; i < PREFS_COUNT; ++i)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
tr_variant const* b = tr_variantDictFind(&top, Items[i].key);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
switch (Items[i].type)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Int:
|
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = getValue<int64_t>(b);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
values_[i].setValue(*value);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case CustomVariantType::SortModeType:
|
|
|
|
{
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const value = getValue<std::string_view>(b);
|
2020-07-27 04:30:58 +00:00
|
|
|
if (value)
|
|
|
|
{
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const test = [&value](auto const& item) { return item.second == *value; };
|
|
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
|
|
auto const it = std::find_if(std::cbegin(SortModes), std::cend(SortModes), test);
|
|
|
|
auto const& pair = it == std::end(SortModes) ? SortModes.front() : *it;
|
2020-08-15 15:42:51 +00:00
|
|
|
values_[i] = QVariant::fromValue(SortMode(pair.first));
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case CustomVariantType::FilterModeType:
|
|
|
|
{
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const value = getValue<std::string_view>(b);
|
2020-07-27 04:30:58 +00:00
|
|
|
if (value)
|
|
|
|
{
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const test = [&value](auto const& item) { return item.second == *value; };
|
|
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
|
|
auto const it = std::find_if(std::cbegin(FilterModes), std::cend(FilterModes), test);
|
|
|
|
auto const& pair = it == std::end(FilterModes) ? FilterModes.front() : *it;
|
2020-08-15 15:42:51 +00:00
|
|
|
values_[i] = QVariant::fromValue(FilterMode(pair.first));
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::String:
|
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = getValue<QString>(b);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
values_[i].setValue(*value);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Bool:
|
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = getValue<bool>(b);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
values_[i].setValue(*value);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Double:
|
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = getValue<double>(b);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
values_[i].setValue(*value);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::DateTime:
|
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = getValue<time_t>(b);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
values_[i].setValue(QDateTime::fromTime_t(*value));
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2017-04-30 16:25:26 +00:00
|
|
|
assert(false && "unhandled type");
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Prefs::~Prefs()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// make a dict from settings.json
|
|
|
|
tr_variant current_settings;
|
|
|
|
tr_variantInitDict(¤t_settings, PREFS_COUNT);
|
|
|
|
|
|
|
|
for (int i = 0; i < PREFS_COUNT; ++i)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (temporary_prefs_.contains(i))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
tr_quark const key = Items[i].key;
|
2020-05-27 21:53:12 +00:00
|
|
|
QVariant const& val = values_[i];
|
2010-07-30 22:31:31 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
switch (Items[i].type)
|
2010-07-30 22:31:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Int:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(¤t_settings, key, val.toInt());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case CustomVariantType::SortModeType:
|
2020-08-15 15:42:51 +00:00
|
|
|
{
|
|
|
|
auto const mode = val.value<SortMode>().mode();
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const test = [&mode](auto const& item) { return item.first == mode; };
|
|
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
|
|
auto const it = std::find_if(std::cbegin(SortModes), std::cend(SortModes), test);
|
|
|
|
auto const& pair = it == std::end(SortModes) ? SortModes.front() : *it;
|
2020-08-15 15:42:51 +00:00
|
|
|
dictAdd(¤t_settings, key, pair.second);
|
|
|
|
break;
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case CustomVariantType::FilterModeType:
|
2020-08-15 15:42:51 +00:00
|
|
|
{
|
|
|
|
auto const mode = val.value<FilterMode>().mode();
|
2020-09-09 14:24:39 +00:00
|
|
|
auto const test = [&mode](auto const& item) { return item.first == mode; };
|
|
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
|
|
auto const it = std::find_if(std::cbegin(FilterModes), std::cend(FilterModes), test);
|
|
|
|
auto const& pair = it == std::end(FilterModes) ? FilterModes.front() : *it;
|
2020-08-15 15:42:51 +00:00
|
|
|
dictAdd(¤t_settings, key, pair.second);
|
|
|
|
break;
|
|
|
|
}
|
2012-12-24 19:53:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::String:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(¤t_settings, key, val.toString());
|
|
|
|
break;
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Bool:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(¤t_settings, key, val.toBool());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::Double:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(¤t_settings, key, val.toDouble());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case QVariant::DateTime:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(¤t_settings, key, val.toDateTime().toTime_t());
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2017-04-30 16:25:26 +00:00
|
|
|
assert(false && "unhandled type");
|
2012-12-22 20:35:19 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// update settings.json with our settings
|
|
|
|
tr_variant file_settings;
|
2020-05-29 17:40:07 +00:00
|
|
|
QFile const file(QDir(config_dir_).absoluteFilePath(QStringLiteral("settings.json")));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
if (!tr_variantFromFile(&file_settings, TR_VARIANT_FMT_JSON, file.fileName().toUtf8().constData(), nullptr))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantInitDict(&file_settings, PREFS_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantMergeDicts(&file_settings, ¤t_settings);
|
|
|
|
tr_variantToFile(&file_settings, TR_VARIANT_FMT_JSON, file.fileName().toUtf8().constData());
|
|
|
|
tr_variantFree(&file_settings);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_variantFree(¤t_settings);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is where we initialize the preferences file with the default values.
|
|
|
|
* If you add a new preferences key, you /must/ add a default value here.
|
|
|
|
*/
|
2020-11-09 03:31:02 +00:00
|
|
|
void Prefs::initDefaults(tr_variant* d) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-07-27 23:34:40 +00:00
|
|
|
auto constexpr FilterMode = std::string_view { "all" };
|
|
|
|
auto constexpr SessionHost = std::string_view { "localhost" };
|
|
|
|
auto constexpr SessionPassword = std::string_view { "" };
|
|
|
|
auto constexpr SessionUsername = std::string_view { "" };
|
|
|
|
auto constexpr SortMode = std::string_view { "sort-by-name" };
|
|
|
|
auto constexpr SoundCommand =
|
|
|
|
std::string_view { "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'" };
|
|
|
|
auto constexpr StatsMode = std::string_view { "total-ratio" };
|
|
|
|
auto constexpr WindowLayout = std::string_view { "menu,toolbar,filter,list,statusbar" };
|
|
|
|
|
|
|
|
auto const download_dir = std::string_view { tr_getDefaultDownloadDir() };
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictReserve(d, 38);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(d, TR_KEY_blocklist_updates_enabled, true);
|
|
|
|
dictAdd(d, TR_KEY_compact_view, false);
|
|
|
|
dictAdd(d, TR_KEY_inhibit_desktop_hibernation, false);
|
|
|
|
dictAdd(d, TR_KEY_prompt_before_exit, true);
|
|
|
|
dictAdd(d, TR_KEY_remote_session_enabled, false);
|
|
|
|
dictAdd(d, TR_KEY_remote_session_requres_authentication, false);
|
|
|
|
dictAdd(d, TR_KEY_show_backup_trackers, false);
|
|
|
|
dictAdd(d, TR_KEY_show_extra_peer_details, false);
|
|
|
|
dictAdd(d, TR_KEY_show_filterbar, true);
|
|
|
|
dictAdd(d, TR_KEY_show_notification_area_icon, false);
|
|
|
|
dictAdd(d, TR_KEY_start_minimized, false);
|
|
|
|
dictAdd(d, TR_KEY_show_options_window, true);
|
|
|
|
dictAdd(d, TR_KEY_show_statusbar, true);
|
|
|
|
dictAdd(d, TR_KEY_show_toolbar, true);
|
|
|
|
dictAdd(d, TR_KEY_show_tracker_scrapes, false);
|
|
|
|
dictAdd(d, TR_KEY_sort_reversed, false);
|
|
|
|
dictAdd(d, TR_KEY_torrent_added_notification_enabled, true);
|
|
|
|
dictAdd(d, TR_KEY_torrent_complete_notification_enabled, true);
|
2020-07-27 23:34:40 +00:00
|
|
|
dictAdd(d, TR_KEY_torrent_complete_sound_command, SoundCommand);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(d, TR_KEY_torrent_complete_sound_enabled, true);
|
|
|
|
dictAdd(d, TR_KEY_user_has_given_informed_consent, false);
|
|
|
|
dictAdd(d, TR_KEY_watch_dir_enabled, false);
|
|
|
|
dictAdd(d, TR_KEY_blocklist_date, 0);
|
|
|
|
dictAdd(d, TR_KEY_main_window_height, 500);
|
|
|
|
dictAdd(d, TR_KEY_main_window_width, 300);
|
|
|
|
dictAdd(d, TR_KEY_main_window_x, 50);
|
|
|
|
dictAdd(d, TR_KEY_main_window_y, 50);
|
|
|
|
dictAdd(d, TR_KEY_remote_session_port, TR_DEFAULT_RPC_PORT);
|
2020-07-27 23:34:40 +00:00
|
|
|
dictAdd(d, TR_KEY_download_dir, download_dir);
|
|
|
|
dictAdd(d, TR_KEY_filter_mode, FilterMode);
|
|
|
|
dictAdd(d, TR_KEY_main_window_layout_order, WindowLayout);
|
|
|
|
dictAdd(d, TR_KEY_open_dialog_dir, QDir::home().absolutePath());
|
|
|
|
dictAdd(d, TR_KEY_remote_session_host, SessionHost);
|
|
|
|
dictAdd(d, TR_KEY_remote_session_password, SessionPassword);
|
|
|
|
dictAdd(d, TR_KEY_remote_session_username, SessionUsername);
|
|
|
|
dictAdd(d, TR_KEY_sort_mode, SortMode);
|
|
|
|
dictAdd(d, TR_KEY_statusbar_stats, StatsMode);
|
|
|
|
dictAdd(d, TR_KEY_watch_dir, download_dir);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool Prefs::getBool(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[key].type == QVariant::Bool);
|
2020-05-27 21:53:12 +00:00
|
|
|
return values_[key].toBool();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString Prefs::getString(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[key].type == QVariant::String);
|
2020-05-27 21:53:12 +00:00
|
|
|
QByteArray const b = values_[key].toByteArray();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
if (isValidUtf8(b.constData()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
values_[key].setValue(QString::fromUtf8(b.constData()));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return values_[key].toString();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int Prefs::getInt(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[key].type == QVariant::Int);
|
2020-05-27 21:53:12 +00:00
|
|
|
return values_[key].toInt();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
double Prefs::getDouble(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[key].type == QVariant::Double);
|
2020-05-27 21:53:12 +00:00
|
|
|
return values_[key].toDouble();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QDateTime Prefs::getDateTime(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
assert(Items[key].type == QVariant::DateTime);
|
2020-05-27 21:53:12 +00:00
|
|
|
return values_[key].toDateTime();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Prefs::toggleBool(int key)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set(key, !getBool(key));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|