source: trunk/gtk/conf.c @ 13107

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

(trunk libT) #4372 "make notification and system sounds configurable" -- instead of using notify-send, use GDBus on the org.freedesktop.Notifications API. Patch by fmuellner.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.0 KB
Line 
1/******************************************************************************
2 * $Id: conf.c 13107 2011-12-10 19:00:50Z 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_TRASH_CAN_ENABLED, TRUE );
90    tr_bencDictAddBool( d, PREF_KEY_SHOW_TRAY_ICON, FALSE );
91    tr_bencDictAddBool( d, PREF_KEY_SHOW_MORE_TRACKER_INFO, FALSE );
92    tr_bencDictAddBool( d, PREF_KEY_SHOW_MORE_PEER_INFO, FALSE );
93    tr_bencDictAddBool( d, PREF_KEY_SHOW_BACKUP_TRACKERS, FALSE );
94    tr_bencDictAddStr ( d, PREF_KEY_STATUSBAR_STATS, "total-ratio" );
95
96    tr_bencDictAddBool( d, PREF_KEY_TORRENT_ADDED_NOTIFICATION_ENABLED, true );
97    tr_bencDictAddBool( d, PREF_KEY_TORRENT_COMPLETE_NOTIFICATION_ENABLED, true );
98    tr_bencDictAddStr ( d, PREF_KEY_TORRENT_COMPLETE_SOUND_COMMAND, "canberra-gtk-play -i complete-download -d 'transmission torrent downloaded'" );
99    tr_bencDictAddBool( d, PREF_KEY_TORRENT_COMPLETE_SOUND_ENABLED, true );
100
101    tr_bencDictAddBool( d, PREF_KEY_OPTIONS_PROMPT, TRUE );
102
103    tr_bencDictAddBool( d, PREF_KEY_MAIN_WINDOW_IS_MAXIMIZED, FALSE );
104    tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_HEIGHT, 500 );
105    tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_WIDTH, 300 );
106    tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_X, 50 );
107    tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_Y, 50 );
108
109    str = g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD );
110    tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, str );
111
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 );
115}
116
117static char*
118getPrefsFilename( void )
119{
120    g_assert( gl_confdir != NULL );
121    return g_build_filename( gl_confdir, "settings.json", NULL );
122}
123
124static tr_benc*
125getPrefs( void )
126{
127    static tr_benc settings;
128    static gboolean loaded = FALSE;
129
130    if( !loaded )
131    {
132        tr_bencInitDict( &settings, 0 );
133        tr_prefs_init_defaults( &settings );
134        tr_sessionLoadSettings( &settings, gl_confdir, MY_CONFIG_NAME );
135        loaded = TRUE;
136    }
137
138    return &settings;
139}
140
141/***
142****
143***/
144
145tr_benc*
146gtr_pref_get_all( void )
147{
148    return getPrefs( );
149}
150
151int64_t
152gtr_pref_int_get( const char * key )
153{
154    int64_t i = 0;
155
156    tr_bencDictFindInt( getPrefs( ), key, &i );
157    return i;
158}
159
160void
161gtr_pref_int_set( const char * key, int64_t value )
162{
163    tr_bencDictAddInt( getPrefs( ), key, value );
164}
165
166double
167gtr_pref_double_get( const char * key )
168{
169    double d = 0.0;
170    tr_bencDictFindReal( getPrefs( ), key, &d );
171    return d;
172}
173
174void
175gtr_pref_double_set( const char * key, double value )
176{
177    tr_bencDictAddReal( getPrefs( ), key, value );
178}
179
180/***
181****
182***/
183
184gboolean
185gtr_pref_flag_get( const char * key )
186{
187    bool boolVal;
188    tr_bencDictFindBool( getPrefs( ), key, &boolVal );
189    return boolVal != 0;
190}
191
192void
193gtr_pref_flag_set( const char * key, gboolean value )
194{
195    tr_bencDictAddBool( getPrefs( ), key, value );
196}
197
198/***
199****
200***/
201
202const char*
203gtr_pref_string_get( const char * key )
204{
205    const char * str = NULL;
206    tr_bencDictFindStr( getPrefs( ), key, &str );
207    return str;
208}
209
210void
211gtr_pref_string_set( const char * key, const char * value )
212{
213    tr_bencDictAddStr( getPrefs( ), key, value );
214}
215
216/***
217****
218***/
219
220void
221gtr_pref_save( tr_session * session )
222{
223    tr_sessionSaveSettings( session, gl_confdir, getPrefs( ) );
224}
225
226/***
227****
228***/
229
230static char*
231getCompat090PrefsFilename( void )
232{
233    g_assert( gl_confdir != NULL );
234
235    return g_build_filename( g_get_home_dir( ), ".transmission", "gtk", "prefs.ini", NULL );
236}
237
238static char*
239getCompat121PrefsFilename( void )
240{
241    return g_build_filename( g_get_user_config_dir( ), "transmission", "gtk", "prefs.ini", NULL );
242}
243
244static void
245translate_keyfile_to_json( const char * old_file, const char * new_file )
246{
247    tr_benc    dict;
248    GKeyFile * keyfile;
249    gchar **   keys;
250    gsize      i;
251    gsize      length;
252
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"        }
264    };
265
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 );
270
271    tr_bencInitDict( &dict, length );
272    for( i = 0; i < length; ++i )
273    {
274        guint        j;
275        const char * key = keys[i];
276        gchar *      val = g_key_file_get_value( keyfile, "general", key,
277                                                 NULL );
278
279        for( j = 0; j < G_N_ELEMENTS( renamed ); ++j )
280            if( !strcmp( renamed[j].oldkey, key ) )
281                key = renamed[j].newkey;
282
283        if( !strcmp( val, "true" ) || !strcmp( val, "false" ) )
284            tr_bencDictAddInt( &dict, key, !strcmp( val, "true" ) );
285        else
286        {
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 );
295        }
296
297        g_free( val );
298    }
299
300    g_key_file_free( keyfile );
301    tr_bencToFile( &dict, TR_FMT_JSON, new_file );
302    tr_bencFree( &dict );
303}
304
305static void
306cf_check_older_configs( void )
307{
308    char * filename = getPrefsFilename( );
309
310    if( !g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
311    {
312        char * key1 = getCompat121PrefsFilename( );
313        char * key2 = getCompat090PrefsFilename( );
314
315        if( g_file_test( key1, G_FILE_TEST_IS_REGULAR ) )
316        {
317            g_message( _( "Importing \"%s\"" ), key1 );
318            translate_keyfile_to_json( key1, filename );
319        }
320        else if( g_file_test( key2, G_FILE_TEST_IS_REGULAR ) )
321        {
322            g_message( _( "Importing \"%s\"" ), key2 );
323            translate_keyfile_to_json( key2, filename );
324        }
325
326        g_free( key2 );
327        g_free( key1 );
328    }
329
330    g_free( filename );
331}
Note: See TracBrowser for help on using the repository browser.