source: trunk/gtk/conf.c @ 12656

Last change on this file since 12656 was 12656, checked in by jordan, 10 years ago

(trunk gtk) first cut at using GApplication. This lets glib replace hundreds of lines of homegrown code. Whee!

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