2006-08-13 00:26:52 +00:00
|
|
|
/******************************************************************************
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) Transmission authors and contributors
|
2006-08-13 00:26:52 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdlib.h> /* strtol() */
|
2021-10-20 02:30:50 +00:00
|
|
|
#include <string_view>
|
2007-10-13 03:15:02 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2007-09-27 20:57:58 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "Prefs.h"
|
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::string gl_confdir;
|
2007-10-12 20:50:03 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_pref_init(std::string const& config_dir)
|
2007-10-13 03:15:02 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
gl_confdir = config_dir;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 20:50:03 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Preferences
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-03-10 15:24:40 +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.
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_prefs_init_defaults(tr_variant* d)
|
2009-03-10 15:24:40 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto dir = Glib::get_user_special_dir(Glib::USER_DIRECTORY_DOWNLOAD);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (dir.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
dir = Glib::get_user_special_dir(Glib::USER_DIRECTORY_DESKTOP);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (dir.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
dir = tr_getDefaultDownloadDir();
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:34:02 +00:00
|
|
|
tr_variantDictReserve(d, 31);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_watch_dir, dir);
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_watch_dir_enabled, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_user_has_given_informed_consent, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_inhibit_desktop_hibernation, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_updates_enabled, true);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_open_dialog_dir, Glib::get_home_dir());
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_toolbar, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_filterbar, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_statusbar, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_trash_can_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_notification_area_icon, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_tracker_scrapes, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_extra_peer_details, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_backup_trackers, false);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_statusbar_stats, "total-ratio"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_torrent_added_notification_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_torrent_complete_notification_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_torrent_complete_sound_enabled, true);
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_options_window, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_main_window_is_maximized, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_main_window_height, 500);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_main_window_width, 300);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_main_window_x, 50);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_main_window_y, 50);
|
2020-04-27 21:34:02 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_details_window_height, 500);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_details_window_width, 700);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, dir);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_sort_mode, "sort-by-name"sv);
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_sort_reversed, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_compact_view, false);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-05-25 16:21:41 +00:00
|
|
|
static void ensure_sound_cmd_is_a_list(tr_variant* dict)
|
|
|
|
{
|
|
|
|
tr_quark key = TR_KEY_torrent_complete_sound_command;
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* list = nullptr;
|
2021-05-25 16:21:41 +00:00
|
|
|
if (tr_variantDictFindList(dict, key, &list))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictRemove(dict, key);
|
|
|
|
list = tr_variantDictAddList(dict, key, 5);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantListAddStr(list, "canberra-gtk-play"sv);
|
|
|
|
tr_variantListAddStr(list, "-i"sv);
|
|
|
|
tr_variantListAddStr(list, "complete-download"sv);
|
|
|
|
tr_variantListAddStr(list, "-d"sv);
|
|
|
|
tr_variantListAddStr(list, "transmission torrent downloaded"sv);
|
2021-05-25 16:21:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
static tr_variant* getPrefs()
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant settings;
|
2021-10-18 20:22:31 +00:00
|
|
|
static bool loaded = false;
|
2007-09-27 20:57:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!loaded)
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
tr_prefs_init_defaults(&settings);
|
2021-12-14 08:43:27 +00:00
|
|
|
tr_sessionLoadSettings(&settings, gl_confdir.c_str(), nullptr);
|
2021-05-25 16:21:41 +00:00
|
|
|
ensure_sound_cmd_is_a_list(&settings);
|
2021-10-18 20:22:31 +00:00
|
|
|
loaded = true;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return &settings;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_variant* gtr_pref_get_all()
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return getPrefs();
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int64_t gtr_pref_int_get(tr_quark const key)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantDictFindInt(getPrefs(), key, &i) ? i : 0;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void gtr_pref_int_set(tr_quark const key, int64_t value)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(getPrefs(), key, value);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double gtr_pref_double_get(tr_quark const key)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
double d;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantDictFindReal(getPrefs(), key, &d) ? d : 0.0;
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void gtr_pref_double_set(tr_quark const key, double value)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddReal(getPrefs(), key, value);
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
bool gtr_pref_flag_get(tr_quark const key)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool boolVal;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantDictFindBool(getPrefs(), key, &boolVal) ? boolVal : false;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_pref_flag_set(tr_quark const key, bool value)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(getPrefs(), key, value);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::vector<std::string> gtr_pref_strv_get(tr_quark const key)
|
2021-05-25 16:21:41 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
std::vector<std::string> ret;
|
2021-05-25 16:21:41 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* list = nullptr;
|
2021-05-25 16:21:41 +00:00
|
|
|
if (tr_variantDictFindList(getPrefs(), key, &list))
|
|
|
|
{
|
|
|
|
size_t const n = tr_variantListSize(list);
|
2021-10-18 20:22:31 +00:00
|
|
|
ret.reserve(n);
|
2021-05-25 16:21:41 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
2021-11-14 23:28:37 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
if (tr_variantGetStrView(tr_variantListChild(list, i), &sv))
|
2021-05-25 16:21:41 +00:00
|
|
|
{
|
2021-11-14 23:28:37 +00:00
|
|
|
ret.emplace_back(sv);
|
2021-05-25 16:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::string gtr_pref_string_get(tr_quark const key)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2021-11-14 19:14:45 +00:00
|
|
|
auto sv = std::string_view{};
|
2021-11-17 21:56:50 +00:00
|
|
|
(void)tr_variantDictFindStrView(getPrefs(), key, &sv);
|
2021-11-14 19:14:45 +00:00
|
|
|
return std::string{ sv };
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-06-12 16:25:36 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void gtr_pref_string_set(tr_quark const key, std::string const& value)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantDictAddStr(getPrefs(), key, value);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-06-12 16:25:36 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void gtr_pref_save(tr_session* session)
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_sessionSaveSettings(session, gl_confdir.c_str(), getPrefs());
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|