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>
|
|
|
|
#include <stdio.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdlib.h> /* strtol() */
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <string.h>
|
2007-10-13 03:15:02 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gi18n.h>
|
2007-09-28 00:46:22 +00:00
|
|
|
#include <glib/gstdio.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
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "conf.h"
|
2009-03-10 15:24:40 +00:00
|
|
|
#include "tr-prefs.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2010-08-05 13:24:46 +00:00
|
|
|
#define MY_CONFIG_NAME "transmission"
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
static char* gl_confdir = nullptr;
|
2007-10-12 20:50:03 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void gtr_pref_init(char const* config_dir)
|
2007-10-13 03:15:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
gl_confdir = g_strdup(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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* dir;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
dir = g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD);
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (dir == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
dir = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (dir == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
dir = tr_getDefaultDownloadDir();
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:34:02 +00:00
|
|
|
tr_variantDictReserve(d, 31);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_watch_dir, dir);
|
|
|
|
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);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_open_dialog_dir, g_get_home_dir());
|
|
|
|
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);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_statusbar_stats, "total-ratio");
|
|
|
|
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);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_show_options_window, TRUE);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_main_window_is_maximized, FALSE);
|
|
|
|
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);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, dir);
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_sort_mode, "sort-by-name");
|
|
|
|
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);
|
|
|
|
tr_variantListAddStr(list, "canberra-gtk-play");
|
|
|
|
tr_variantListAddStr(list, "-i");
|
|
|
|
tr_variantListAddStr(list, "complete-download");
|
|
|
|
tr_variantListAddStr(list, "-d");
|
|
|
|
tr_variantListAddStr(list, "transmission torrent downloaded");
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant* getPrefs(void)
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant settings;
|
|
|
|
static gboolean 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);
|
|
|
|
tr_sessionLoadSettings(&settings, gl_confdir, MY_CONFIG_NAME);
|
2021-05-25 16:21:41 +00:00
|
|
|
ensure_sound_cmd_is_a_list(&settings);
|
2017-04-19 12:04:45 +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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* gtr_pref_get_all(void)
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
gboolean 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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void gtr_pref_flag_set(tr_quark const key, gboolean 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-05-25 16:21:41 +00:00
|
|
|
char** gtr_pref_strv_get(tr_quark const key)
|
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char** ret = nullptr;
|
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 out = 0;
|
|
|
|
size_t const n = tr_variantListSize(list);
|
|
|
|
ret = g_new0(char*, n + 1);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char const* str = nullptr;
|
2021-05-25 16:21:41 +00:00
|
|
|
size_t len = 0;
|
|
|
|
if (tr_variantGetStr(tr_variantListChild(list, i), &str, &len))
|
|
|
|
{
|
|
|
|
ret[out++] = g_strndup(str, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* gtr_pref_string_get(tr_quark const key)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
return tr_variantDictFindStr(getPrefs(), key, &str, nullptr) ? str : nullptr;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-06-12 16:25:36 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void gtr_pref_string_set(tr_quark const key, char const* value)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionSaveSettings(session, gl_confdir, getPrefs());
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|