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 *****************************************************************************/
27 #include <stdlib.h> /* strtol() */
33 #include <glib/gi18n.h>
34 #include <glib/gstdio.h>
36 #include <libtransmission/transmission.h>
37 #include <libtransmission/bencode.h>
43 #define MY_CONFIG_NAME "transmission"
44 #define MY_READABLE_NAME "transmission-gtk"
46 static char * gl_confdir
= NULL
;
49 gtr_pref_init( const char * config_dir
)
51 gl_confdir
= g_strdup( config_dir
);
60 static void cf_check_older_configs( void );
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.
67 tr_prefs_init_defaults( tr_benc
* d
)
70 const char * special_dl_dir
= g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD
);
72 cf_check_older_configs( );
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
);
118 getPrefsFilename( void )
120 g_assert( gl_confdir
!= NULL
);
121 return g_build_filename( gl_confdir
, "settings.json", NULL
);
127 static tr_benc settings
;
128 static gboolean loaded
= FALSE
;
132 tr_bencInitDict( &settings
, 0 );
133 tr_prefs_init_defaults( &settings
);
134 tr_sessionLoadSettings( &settings
, gl_confdir
, MY_CONFIG_NAME
);
146 gtr_pref_get_all( void )
152 gtr_pref_int_get( const char * key
)
156 tr_bencDictFindInt( getPrefs( ), key
, &i
);
161 gtr_pref_int_set( const char * key
, int64_t value
)
163 tr_bencDictAddInt( getPrefs( ), key
, value
);
167 gtr_pref_double_get( const char * key
)
170 tr_bencDictFindReal( getPrefs( ), key
, &d
);
175 gtr_pref_double_set( const char * key
, double value
)
177 tr_bencDictAddReal( getPrefs( ), key
, value
);
185 gtr_pref_flag_get( const char * key
)
188 tr_bencDictFindBool( getPrefs( ), key
, &boolVal
);
193 gtr_pref_flag_set( const char * key
, gboolean value
)
195 tr_bencDictAddBool( getPrefs( ), key
, value
);
203 gtr_pref_string_get( const char * key
)
205 const char * str
= NULL
;
206 tr_bencDictFindStr( getPrefs( ), key
, &str
);
211 gtr_pref_string_set( const char * key
, const char * value
)
213 tr_bencDictAddStr( getPrefs( ), key
, value
);
221 gtr_pref_save( tr_session
* session
)
223 tr_sessionSaveSettings( session
, gl_confdir
, getPrefs( ) );
231 getCompat090PrefsFilename( void )
233 g_assert( gl_confdir
!= NULL
);
235 return g_build_filename( g_get_home_dir( ), ".transmission", "gtk", "prefs.ini", NULL
);
239 getCompat121PrefsFilename( void )
241 return g_build_filename( g_get_user_config_dir( ), "transmission", "gtk", "prefs.ini", NULL
);
245 translate_keyfile_to_json( const char * old_file
, const char * new_file
)
253 static struct pref_entry
{
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
);
269 keys
= g_key_file_get_keys( keyfile
, "general", &length
, NULL
);
271 tr_bencInitDict( &dict
, length
);
272 for( i
= 0; i
< length
; ++i
)
275 const char * key
= keys
[i
];
276 gchar
* val
= g_key_file_get_value( keyfile
, "general", key
,
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" ) );
290 l
= strtol( val
, &end
, 10 );
291 if( !errno
&& end
&& !*end
)
292 tr_bencDictAddInt( &dict
, key
, l
);
294 tr_bencDictAddStr( &dict
, key
, val
);
300 g_key_file_free( keyfile
);
301 tr_bencToFile( &dict
, TR_FMT_JSON
, new_file
);
302 tr_bencFree( &dict
);
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
);