transmission 2.51 update
[tomato.git] / release / src / router / transmission / gtk / conf.c
blob82cdcc5fb241ac762b949b59727645f267cabc35
1 /******************************************************************************
2 * $Id: conf.c 13273 2012-04-08 16:02:48Z jordan $
4 * Copyright (c) Transmission authors and contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 *****************************************************************************/
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h> /* strtol() */
28 #include <string.h>
30 #include <unistd.h>
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <glib/gstdio.h>
36 #include <libtransmission/transmission.h>
37 #include <libtransmission/bencode.h>
39 #include "conf.h"
40 #include "tr-prefs.h"
41 #include "util.h"
43 #define MY_CONFIG_NAME "transmission"
44 #define MY_READABLE_NAME "transmission-gtk"
46 static char * gl_confdir = NULL;
48 void
49 gtr_pref_init( const char * config_dir )
51 gl_confdir = g_strdup( config_dir );
54 /***
55 ****
56 **** Preferences
57 ****
58 ***/
60 static void cf_check_older_configs( void );
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.
66 static void
67 tr_prefs_init_defaults( tr_benc * d )
69 const char * str;
70 const char * special_dl_dir = g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD );
72 cf_check_older_configs( );
74 str = NULL;
75 if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD );
76 if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DESKTOP );
77 if( !str ) str = tr_getDefaultDownloadDir( );
78 tr_bencDictAddStr ( d, PREF_KEY_DIR_WATCH, str );
79 tr_bencDictAddBool( d, PREF_KEY_DIR_WATCH_ENABLED, FALSE );
81 tr_bencDictAddBool( d, PREF_KEY_USER_HAS_GIVEN_INFORMED_CONSENT, FALSE );
82 tr_bencDictAddBool( d, PREF_KEY_INHIBIT_HIBERNATION, FALSE );
83 tr_bencDictAddBool( d, PREF_KEY_BLOCKLIST_UPDATES_ENABLED, TRUE );
85 tr_bencDictAddStr ( d, PREF_KEY_OPEN_DIALOG_FOLDER, g_get_home_dir( ) );
87 tr_bencDictAddBool( d, PREF_KEY_TOOLBAR, TRUE );
88 tr_bencDictAddBool( d, PREF_KEY_FILTERBAR, TRUE );
89 tr_bencDictAddBool( d, PREF_KEY_STATUSBAR, TRUE );
90 tr_bencDictAddBool( d, PREF_KEY_TRASH_CAN_ENABLED, TRUE );
91 tr_bencDictAddBool( d, PREF_KEY_SHOW_TRAY_ICON, FALSE );
92 tr_bencDictAddBool( d, PREF_KEY_SHOW_MORE_TRACKER_INFO, FALSE );
93 tr_bencDictAddBool( d, PREF_KEY_SHOW_MORE_PEER_INFO, FALSE );
94 tr_bencDictAddBool( d, PREF_KEY_SHOW_BACKUP_TRACKERS, FALSE );
95 tr_bencDictAddStr ( d, PREF_KEY_STATUSBAR_STATS, "total-ratio" );
97 tr_bencDictAddBool( d, PREF_KEY_TORRENT_ADDED_NOTIFICATION_ENABLED, true );
98 tr_bencDictAddBool( d, PREF_KEY_TORRENT_COMPLETE_NOTIFICATION_ENABLED, true );
99 tr_bencDictAddStr ( d, PREF_KEY_TORRENT_COMPLETE_SOUND_COMMAND, "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'" );
100 tr_bencDictAddBool( d, PREF_KEY_TORRENT_COMPLETE_SOUND_ENABLED, true );
102 tr_bencDictAddBool( d, PREF_KEY_OPTIONS_PROMPT, TRUE );
104 tr_bencDictAddBool( d, PREF_KEY_MAIN_WINDOW_IS_MAXIMIZED, FALSE );
105 tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_HEIGHT, 500 );
106 tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_WIDTH, 300 );
107 tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_X, 50 );
108 tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_Y, 50 );
110 tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, special_dl_dir ? special_dl_dir : str );
112 tr_bencDictAddStr( d, PREF_KEY_SORT_MODE, "sort-by-name" );
113 tr_bencDictAddBool( d, PREF_KEY_SORT_REVERSED, FALSE );
114 tr_bencDictAddBool( d, PREF_KEY_COMPACT_VIEW, FALSE );
117 static char*
118 getPrefsFilename( void )
120 g_assert( gl_confdir != NULL );
121 return g_build_filename( gl_confdir, "settings.json", NULL );
124 static tr_benc*
125 getPrefs( void )
127 static tr_benc settings;
128 static gboolean loaded = FALSE;
130 if( !loaded )
132 tr_bencInitDict( &settings, 0 );
133 tr_prefs_init_defaults( &settings );
134 tr_sessionLoadSettings( &settings, gl_confdir, MY_CONFIG_NAME );
135 loaded = TRUE;
138 return &settings;
141 /***
142 ****
143 ***/
145 tr_benc*
146 gtr_pref_get_all( void )
148 return getPrefs( );
151 int64_t
152 gtr_pref_int_get( const char * key )
154 int64_t i = 0;
156 tr_bencDictFindInt( getPrefs( ), key, &i );
157 return i;
160 void
161 gtr_pref_int_set( const char * key, int64_t value )
163 tr_bencDictAddInt( getPrefs( ), key, value );
166 double
167 gtr_pref_double_get( const char * key )
169 double d = 0.0;
170 tr_bencDictFindReal( getPrefs( ), key, &d );
171 return d;
174 void
175 gtr_pref_double_set( const char * key, double value )
177 tr_bencDictAddReal( getPrefs( ), key, value );
180 /***
181 ****
182 ***/
184 gboolean
185 gtr_pref_flag_get( const char * key )
187 bool boolVal;
188 tr_bencDictFindBool( getPrefs( ), key, &boolVal );
189 return boolVal != 0;
192 void
193 gtr_pref_flag_set( const char * key, gboolean value )
195 tr_bencDictAddBool( getPrefs( ), key, value );
198 /***
199 ****
200 ***/
202 const char*
203 gtr_pref_string_get( const char * key )
205 const char * str = NULL;
206 tr_bencDictFindStr( getPrefs( ), key, &str );
207 return str;
210 void
211 gtr_pref_string_set( const char * key, const char * value )
213 tr_bencDictAddStr( getPrefs( ), key, value );
216 /***
217 ****
218 ***/
220 void
221 gtr_pref_save( tr_session * session )
223 tr_sessionSaveSettings( session, gl_confdir, getPrefs( ) );
226 /***
227 ****
228 ***/
230 static char*
231 getCompat090PrefsFilename( void )
233 g_assert( gl_confdir != NULL );
235 return g_build_filename( g_get_home_dir( ), ".transmission", "gtk", "prefs.ini", NULL );
238 static char*
239 getCompat121PrefsFilename( void )
241 return g_build_filename( g_get_user_config_dir( ), "transmission", "gtk", "prefs.ini", NULL );
244 static void
245 translate_keyfile_to_json( const char * old_file, const char * new_file )
247 tr_benc dict;
248 GKeyFile * keyfile;
249 gchar ** keys;
250 gsize i;
251 gsize length;
253 static struct pref_entry {
254 const char* oldkey;
255 const char* newkey;
256 } renamed[] = {
257 { "default-download-directory", "download-dir" },
258 { "encrypted-connections-only", "encryption" },
259 { "listening-port", "peer-port" },
260 { "nat-traversal-enabled", "port-forwarding-enabled" },
261 { "open-dialog-folder", "open-dialog-dir" },
262 { "watch-folder", "watch-dir" },
263 { "watch-folder-enabled", "watch-dir-enabled" }
266 keyfile = g_key_file_new( );
267 g_key_file_load_from_file( keyfile, old_file, 0, NULL );
268 length = 0;
269 keys = g_key_file_get_keys( keyfile, "general", &length, NULL );
271 tr_bencInitDict( &dict, length );
272 for( i = 0; i < length; ++i )
274 guint j;
275 const char * key = keys[i];
276 gchar * val = g_key_file_get_value( keyfile, "general", key,
277 NULL );
279 for( j = 0; j < G_N_ELEMENTS( renamed ); ++j )
280 if( !strcmp( renamed[j].oldkey, key ) )
281 key = renamed[j].newkey;
283 if( !strcmp( val, "true" ) || !strcmp( val, "false" ) )
284 tr_bencDictAddInt( &dict, key, !strcmp( val, "true" ) );
285 else
287 char * end;
288 long l;
289 errno = 0;
290 l = strtol( val, &end, 10 );
291 if( !errno && end && !*end )
292 tr_bencDictAddInt( &dict, key, l );
293 else
294 tr_bencDictAddStr( &dict, key, val );
297 g_free( val );
300 g_key_file_free( keyfile );
301 tr_bencToFile( &dict, TR_FMT_JSON, new_file );
302 tr_bencFree( &dict );
305 static void
306 cf_check_older_configs( void )
308 char * filename = getPrefsFilename( );
310 if( !g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
312 char * key1 = getCompat121PrefsFilename( );
313 char * key2 = getCompat090PrefsFilename( );
315 if( g_file_test( key1, G_FILE_TEST_IS_REGULAR ) )
317 g_message( _( "Importing \"%s\"" ), key1 );
318 translate_keyfile_to_json( key1, filename );
320 else if( g_file_test( key2, G_FILE_TEST_IS_REGULAR ) )
322 g_message( _( "Importing \"%s\"" ), key2 );
323 translate_keyfile_to_json( key2, filename );
326 g_free( key2 );
327 g_free( key1 );
330 g_free( filename );