source: trunk/gtk/conf.c @ 13625

Last change on this file since 13625 was 13625, checked in by jordan, 9 years ago

Follow more common whitespace style conventions in the C code (libtransmission, daemon, utils, cli, gtk).

  • Property svn:keywords set to Date Rev Author Id
File size: 8.7 KB
Line 
1/******************************************************************************
2 * $Id: conf.c 13625 2012-12-05 17:29:46Z jordan $
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/bencode.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
46static char * gl_confdir = NULL;
47
48void
49gtr_pref_init (const char * config_dir)
50{
51  gl_confdir = g_strdup (config_dir);
52}
53
54/***
55****
56****  Preferences
57****
58***/
59
60static void cf_check_older_configs (void);
61
62/**
63 * This is where we initialize the preferences file with the default values.
64 * If you add a new preferences key, you /must/ add a default value here.
65 */
66static void
67tr_prefs_init_defaults (tr_benc * d)
68{
69  const char * str;
70  const char * special_dl_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
71
72  cf_check_older_configs ();
73
74  str = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
75  if (!str)
76    str = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
77  if (!str)
78    str = tr_getDefaultDownloadDir ();
79
80  tr_bencDictAddStr (d, PREF_KEY_DIR_WATCH, str);
81  tr_bencDictAddBool (d, PREF_KEY_DIR_WATCH_ENABLED, FALSE);
82
83  tr_bencDictAddBool (d, PREF_KEY_USER_HAS_GIVEN_INFORMED_CONSENT, FALSE);
84  tr_bencDictAddBool (d, PREF_KEY_INHIBIT_HIBERNATION, FALSE);
85  tr_bencDictAddBool (d, PREF_KEY_BLOCKLIST_UPDATES_ENABLED, TRUE);
86
87  tr_bencDictAddStr (d, PREF_KEY_OPEN_DIALOG_FOLDER, g_get_home_dir ());
88
89  tr_bencDictAddBool (d, PREF_KEY_TOOLBAR, TRUE);
90  tr_bencDictAddBool (d, PREF_KEY_FILTERBAR, TRUE);
91  tr_bencDictAddBool (d, PREF_KEY_STATUSBAR, TRUE);
92  tr_bencDictAddBool (d, PREF_KEY_TRASH_CAN_ENABLED, TRUE);
93  tr_bencDictAddBool (d, PREF_KEY_SHOW_TRAY_ICON, FALSE);
94  tr_bencDictAddBool (d, PREF_KEY_SHOW_MORE_TRACKER_INFO, FALSE);
95  tr_bencDictAddBool (d, PREF_KEY_SHOW_MORE_PEER_INFO, FALSE);
96  tr_bencDictAddBool (d, PREF_KEY_SHOW_BACKUP_TRACKERS, FALSE);
97  tr_bencDictAddStr (d, PREF_KEY_STATUSBAR_STATS, "total-ratio");
98
99  tr_bencDictAddBool (d, PREF_KEY_TORRENT_ADDED_NOTIFICATION_ENABLED, true);
100  tr_bencDictAddBool (d, PREF_KEY_TORRENT_COMPLETE_NOTIFICATION_ENABLED, true);
101  tr_bencDictAddStr (d, PREF_KEY_TORRENT_COMPLETE_SOUND_COMMAND, "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'");
102  tr_bencDictAddBool (d, PREF_KEY_TORRENT_COMPLETE_SOUND_ENABLED, true);
103
104  tr_bencDictAddBool (d, PREF_KEY_OPTIONS_PROMPT, TRUE);
105
106  tr_bencDictAddBool (d, PREF_KEY_MAIN_WINDOW_IS_MAXIMIZED, FALSE);
107  tr_bencDictAddInt (d, PREF_KEY_MAIN_WINDOW_HEIGHT, 500);
108  tr_bencDictAddInt (d, PREF_KEY_MAIN_WINDOW_WIDTH, 300);
109  tr_bencDictAddInt (d, PREF_KEY_MAIN_WINDOW_X, 50);
110  tr_bencDictAddInt (d, PREF_KEY_MAIN_WINDOW_Y, 50);
111
112  tr_bencDictAddStr (d, TR_PREFS_KEY_DOWNLOAD_DIR, special_dl_dir ? special_dl_dir : str);
113
114  tr_bencDictAddStr (d, PREF_KEY_SORT_MODE, "sort-by-name");
115  tr_bencDictAddBool (d, PREF_KEY_SORT_REVERSED, FALSE);
116  tr_bencDictAddBool (d, PREF_KEY_COMPACT_VIEW, FALSE);
117}
118
119static char*
120getPrefsFilename (void)
121{
122  g_assert (gl_confdir != NULL);
123  return g_build_filename (gl_confdir, "settings.json", NULL);
124}
125
126static tr_benc*
127getPrefs (void)
128{
129  static tr_benc settings;
130  static gboolean loaded = FALSE;
131
132  if (!loaded)
133    {
134      tr_bencInitDict (&settings, 0);
135      tr_prefs_init_defaults (&settings);
136      tr_sessionLoadSettings (&settings, gl_confdir, MY_CONFIG_NAME);
137      loaded = TRUE;
138    }
139
140  return &settings;
141}
142
143/***
144****
145***/
146
147tr_benc*
148gtr_pref_get_all (void)
149{
150  return getPrefs ();
151}
152
153int64_t
154gtr_pref_int_get (const char * key)
155{
156  int64_t i = 0;
157
158  tr_bencDictFindInt (getPrefs (), key, &i);
159
160  return i;
161}
162
163void
164gtr_pref_int_set (const char * key, int64_t value)
165{
166  tr_bencDictAddInt (getPrefs (), key, value);
167}
168
169double
170gtr_pref_double_get (const char * key)
171{
172  double d = 0.0;
173
174  tr_bencDictFindReal (getPrefs (), key, &d);
175
176  return d;
177}
178
179void
180gtr_pref_double_set (const char * key, double value)
181{
182  tr_bencDictAddReal (getPrefs (), key, value);
183}
184
185/***
186****
187***/
188
189gboolean
190gtr_pref_flag_get (const char * key)
191{
192  bool boolVal;
193
194  tr_bencDictFindBool (getPrefs (), key, &boolVal);
195
196  return boolVal != 0;
197}
198
199void
200gtr_pref_flag_set (const char * key, gboolean value)
201{
202  tr_bencDictAddBool (getPrefs (), key, value);
203}
204
205/***
206****
207***/
208
209const char*
210gtr_pref_string_get (const char * key)
211{
212  const char * str = NULL;
213
214  tr_bencDictFindStr (getPrefs (), key, &str);
215
216  return str;
217}
218
219void
220gtr_pref_string_set (const char * key, const char * value)
221{
222  tr_bencDictAddStr (getPrefs (), key, value);
223}
224
225/***
226****
227***/
228
229void
230gtr_pref_save (tr_session * session)
231{
232  tr_sessionSaveSettings (session, gl_confdir, getPrefs ());
233}
234
235/***
236****
237***/
238
239static char*
240getCompat090PrefsFilename (void)
241{
242  g_assert (gl_confdir != NULL);
243
244  return g_build_filename (g_get_home_dir (), ".transmission", "gtk", "prefs.ini", NULL);
245}
246
247static char*
248getCompat121PrefsFilename (void)
249{
250  return g_build_filename (g_get_user_config_dir (), "transmission", "gtk", "prefs.ini", NULL);
251}
252
253static void
254translate_keyfile_to_json (const char * old_file, const char * new_file)
255{
256  tr_benc    dict;
257  GKeyFile * keyfile;
258  gchar **   keys;
259  gsize      i;
260  gsize      length;
261
262  static struct pref_entry {
263    const char*   oldkey;
264    const char*   newkey;
265  } renamed[] = {
266    { "default-download-directory", "download-dir"             },
267    { "encrypted-connections-only", "encryption"               },
268    { "listening-port",             "peer-port"                },
269    { "nat-traversal-enabled",      "port-forwarding-enabled"  },
270    { "open-dialog-folder",         "open-dialog-dir"          },
271    { "watch-folder",               "watch-dir"                },
272    { "watch-folder-enabled",       "watch-dir-enabled"        }
273  };
274
275  keyfile = g_key_file_new ();
276  g_key_file_load_from_file (keyfile, old_file, 0, NULL);
277  length = 0;
278  keys = g_key_file_get_keys (keyfile, "general", &length, NULL);
279
280  tr_bencInitDict (&dict, length);
281  for (i=0; i<length; i++)
282    {
283      guint j;
284      const char * key = keys[i];
285      gchar * val = g_key_file_get_value (keyfile, "general", key, NULL);
286
287      for (j=0; j<G_N_ELEMENTS (renamed); j++)
288        if (!strcmp (renamed[j].oldkey, key))
289          key = renamed[j].newkey;
290
291      if (!strcmp (val, "true") || !strcmp (val, "false"))
292        {
293          tr_bencDictAddInt (&dict, key, !strcmp (val, "true"));
294        }
295      else
296        {
297          char * end;
298          long l;
299
300          errno = 0;
301
302          l = strtol (val, &end, 10);
303          if (!errno && end && !*end)
304            tr_bencDictAddInt (&dict, key, l);
305          else
306            tr_bencDictAddStr (&dict, key, val);
307        }
308
309      g_free (val);
310    }
311
312  g_key_file_free (keyfile);
313  tr_bencToFile (&dict, TR_FMT_JSON, new_file);
314  tr_bencFree (&dict);
315}
316
317static void
318cf_check_older_configs (void)
319{
320  char * filename = getPrefsFilename ();
321
322  if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
323    {
324      char * key1 = getCompat121PrefsFilename ();
325      char * key2 = getCompat090PrefsFilename ();
326
327      if (g_file_test (key1, G_FILE_TEST_IS_REGULAR))
328        {
329          g_message (_("Importing \"%s\""), key1);
330          translate_keyfile_to_json (key1, filename);
331        }
332      else if (g_file_test (key2, G_FILE_TEST_IS_REGULAR))
333        {
334          g_message (_("Importing \"%s\""), key2);
335          translate_keyfile_to_json (key2, filename);
336        }
337
338      g_free (key2);
339      g_free (key1);
340    }
341
342  g_free (filename);
343}
Note: See TracBrowser for help on using the repository browser.