1 /******************************************************************************
2 * Copyright (c) Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h> /* strtol() */
26 #include <string.h>
27
28 #include <unistd.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <glib/gstdio.h>
33
34 #include <libtransmission/transmission.h>
35 #include <libtransmission/variant.h>
36
37 #include "conf.h"
38 #include "tr-prefs.h"
39 #include "util.h"
40
41 #define MY_CONFIG_NAME "transmission"
42 #define MY_READABLE_NAME "transmission-gtk"
43
44 static char* gl_confdir = NULL;
45
gtr_pref_init(char const * config_dir)46 void gtr_pref_init(char const* config_dir)
47 {
48 gl_confdir = g_strdup(config_dir);
49 }
50
51 /***
52 ****
53 **** Preferences
54 ****
55 ***/
56
57 /**
58 * This is where we initialize the preferences file with the default values.
59 * If you add a new preferences key, you /must/ add a default value here.
60 */
tr_prefs_init_defaults(tr_variant * d)61 static void tr_prefs_init_defaults(tr_variant* d)
62 {
63 char const* dir;
64
65 dir = g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD);
66
67 if (dir == NULL)
68 {
69 dir = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
70 }
71
72 if (dir == NULL)
73 {
74 dir = tr_getDefaultDownloadDir();
75 }
76
77 tr_variantDictReserve(d, 31);
78 tr_variantDictAddStr(d, TR_KEY_watch_dir, dir);
79 tr_variantDictAddBool(d, TR_KEY_watch_dir_enabled, FALSE);
80 tr_variantDictAddBool(d, TR_KEY_user_has_given_informed_consent, FALSE);
81 tr_variantDictAddBool(d, TR_KEY_inhibit_desktop_hibernation, FALSE);
82 tr_variantDictAddBool(d, TR_KEY_blocklist_updates_enabled, TRUE);
83 tr_variantDictAddStr(d, TR_KEY_open_dialog_dir, g_get_home_dir());
84 tr_variantDictAddBool(d, TR_KEY_show_toolbar, TRUE);
85 tr_variantDictAddBool(d, TR_KEY_show_filterbar, TRUE);
86 tr_variantDictAddBool(d, TR_KEY_show_statusbar, TRUE);
87 tr_variantDictAddBool(d, TR_KEY_trash_can_enabled, TRUE);
88 tr_variantDictAddBool(d, TR_KEY_show_notification_area_icon, FALSE);
89 tr_variantDictAddBool(d, TR_KEY_show_tracker_scrapes, FALSE);
90 tr_variantDictAddBool(d, TR_KEY_show_extra_peer_details, FALSE);
91 tr_variantDictAddBool(d, TR_KEY_show_backup_trackers, FALSE);
92 tr_variantDictAddStr(d, TR_KEY_statusbar_stats, "total-ratio");
93 tr_variantDictAddBool(d, TR_KEY_torrent_added_notification_enabled, true);
94 tr_variantDictAddBool(d, TR_KEY_torrent_complete_notification_enabled, true);
95 tr_variantDictAddStr(d, TR_KEY_torrent_complete_sound_command,
96 "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'");
97 tr_variantDictAddBool(d, TR_KEY_torrent_complete_sound_enabled, true);
98 tr_variantDictAddBool(d, TR_KEY_show_options_window, TRUE);
99 tr_variantDictAddBool(d, TR_KEY_main_window_is_maximized, FALSE);
100 tr_variantDictAddInt(d, TR_KEY_main_window_height, 500);
101 tr_variantDictAddInt(d, TR_KEY_main_window_width, 300);
102 tr_variantDictAddInt(d, TR_KEY_main_window_x, 50);
103 tr_variantDictAddInt(d, TR_KEY_main_window_y, 50);
104 tr_variantDictAddInt(d, TR_KEY_details_window_height, 500);
105 tr_variantDictAddInt(d, TR_KEY_details_window_width, 700);
106 tr_variantDictAddStr(d, TR_KEY_download_dir, dir);
107 tr_variantDictAddStr(d, TR_KEY_sort_mode, "sort-by-name");
108 tr_variantDictAddBool(d, TR_KEY_sort_reversed, FALSE);
109 tr_variantDictAddBool(d, TR_KEY_compact_view, FALSE);
110 }
111
getPrefs(void)112 static tr_variant* getPrefs(void)
113 {
114 static tr_variant settings;
115 static gboolean loaded = FALSE;
116
117 if (!loaded)
118 {
119 tr_variantInitDict(&settings, 0);
120 tr_prefs_init_defaults(&settings);
121 tr_sessionLoadSettings(&settings, gl_confdir, MY_CONFIG_NAME);
122 loaded = TRUE;
123 }
124
125 return &settings;
126 }
127
128 /***
129 ****
130 ***/
131
gtr_pref_get_all(void)132 tr_variant* gtr_pref_get_all(void)
133 {
134 return getPrefs();
135 }
136
gtr_pref_int_get(tr_quark const key)137 int64_t gtr_pref_int_get(tr_quark const key)
138 {
139 int64_t i;
140
141 return tr_variantDictFindInt(getPrefs(), key, &i) ? i : 0;
142 }
143
gtr_pref_int_set(tr_quark const key,int64_t value)144 void gtr_pref_int_set(tr_quark const key, int64_t value)
145 {
146 tr_variantDictAddInt(getPrefs(), key, value);
147 }
148
gtr_pref_double_get(tr_quark const key)149 double gtr_pref_double_get(tr_quark const key)
150 {
151 double d;
152
153 return tr_variantDictFindReal(getPrefs(), key, &d) ? d : 0.0;
154 }
155
gtr_pref_double_set(tr_quark const key,double value)156 void gtr_pref_double_set(tr_quark const key, double value)
157 {
158 tr_variantDictAddReal(getPrefs(), key, value);
159 }
160
161 /***
162 ****
163 ***/
164
gtr_pref_flag_get(tr_quark const key)165 gboolean gtr_pref_flag_get(tr_quark const key)
166 {
167 bool boolVal;
168
169 return tr_variantDictFindBool(getPrefs(), key, &boolVal) ? boolVal : false;
170 }
171
gtr_pref_flag_set(tr_quark const key,gboolean value)172 void gtr_pref_flag_set(tr_quark const key, gboolean value)
173 {
174 tr_variantDictAddBool(getPrefs(), key, value);
175 }
176
177 /***
178 ****
179 ***/
180
gtr_pref_string_get(tr_quark const key)181 char const* gtr_pref_string_get(tr_quark const key)
182 {
183 char const* str;
184
185 return tr_variantDictFindStr(getPrefs(), key, &str, NULL) ? str : NULL;
186 }
187
gtr_pref_string_set(tr_quark const key,char const * value)188 void gtr_pref_string_set(tr_quark const key, char const* value)
189 {
190 tr_variantDictAddStr(getPrefs(), key, value);
191 }
192
193 /***
194 ****
195 ***/
196
gtr_pref_save(tr_session * session)197 void gtr_pref_save(tr_session* session)
198 {
199 tr_sessionSaveSettings(session, gl_confdir, getPrefs());
200 }
201