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