2006-08-13 00:26:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-08-13 00:26:52 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-13 03:15:02 +00:00
|
|
|
#include <assert.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-10-13 03:15:02 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gi18n.h>
|
2007-09-28 00:46:22 +00:00
|
|
|
#include <glib/gstdio.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2007-12-01 01:41:10 +00:00
|
|
|
#include <libtransmission/bencode.h>
|
2008-02-28 19:06:23 +00:00
|
|
|
#include <libtransmission/platform.h>
|
2007-09-27 20:57:58 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "conf.h"
|
2009-03-10 15:24:40 +00:00
|
|
|
#include "tr-prefs.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
#define MY_NAME "transmission"
|
|
|
|
|
2007-10-13 03:15:02 +00:00
|
|
|
static char * gl_confdir = NULL;
|
|
|
|
static char * gl_lockpath = NULL;
|
2007-10-12 20:50:03 +00:00
|
|
|
|
|
|
|
/* errstr may be NULL, this might be called before GTK is initialized */
|
|
|
|
gboolean
|
2008-12-13 23:17:36 +00:00
|
|
|
cf_init( const char * configDir,
|
|
|
|
char ** errstr )
|
2007-10-13 03:15:02 +00:00
|
|
|
{
|
|
|
|
if( errstr != NULL )
|
|
|
|
*errstr = NULL;
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
gl_confdir = g_strdup( configDir );
|
2007-10-13 03:15:02 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( mkdir_p( gl_confdir, 0755 ) )
|
2007-10-13 03:15:02 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if( errstr != NULL )
|
2008-03-30 13:52:55 +00:00
|
|
|
*errstr = g_strdup_printf( _( "Couldn't create \"%1$s\": %2$s" ),
|
2008-09-23 19:11:04 +00:00
|
|
|
gl_confdir, g_strerror( errno ) );
|
2007-10-13 03:15:02 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2007-10-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Lockfile
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* errstr may be NULL, this might be called before GTK is initialized */
|
2008-02-28 19:06:23 +00:00
|
|
|
static gboolean
|
2008-09-23 19:11:04 +00:00
|
|
|
lockfile( const char * filename,
|
|
|
|
tr_lockfile_state_t *tr_state,
|
|
|
|
char ** errstr )
|
2007-10-13 03:15:02 +00:00
|
|
|
{
|
2008-08-17 12:39:26 +00:00
|
|
|
const tr_lockfile_state_t state = tr_lockfile( filename );
|
2008-09-23 19:11:04 +00:00
|
|
|
const gboolean success = state == TR_LOCKFILE_SUCCESS;
|
|
|
|
|
|
|
|
if( errstr ) switch( state )
|
|
|
|
{
|
|
|
|
case TR_LOCKFILE_EOPEN:
|
|
|
|
*errstr =
|
|
|
|
g_strdup_printf( _( "Couldn't open \"%1$s\": %2$s" ),
|
|
|
|
filename, g_strerror( errno ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_LOCKFILE_ELOCK:
|
|
|
|
*errstr = g_strdup_printf( _( "%s is already running." ),
|
|
|
|
g_get_application_name( ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_LOCKFILE_SUCCESS:
|
|
|
|
*errstr = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_state != NULL )
|
2008-08-17 12:39:26 +00:00
|
|
|
*tr_state = state;
|
|
|
|
|
2008-02-28 19:06:23 +00:00
|
|
|
return success;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 20:50:03 +00:00
|
|
|
static char*
|
|
|
|
getLockFilename( void )
|
|
|
|
{
|
2007-10-13 03:15:02 +00:00
|
|
|
assert( gl_confdir != NULL );
|
|
|
|
return g_build_filename( gl_confdir, "lock", NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-28 00:46:22 +00:00
|
|
|
cf_removelocks( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( gl_lockpath )
|
|
|
|
{
|
|
|
|
g_unlink( gl_lockpath );
|
|
|
|
g_free( gl_lockpath );
|
2008-05-06 11:04:16 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 20:50:03 +00:00
|
|
|
/* errstr may be NULL, this might be called before GTK is initialized */
|
|
|
|
gboolean
|
2008-09-23 19:11:04 +00:00
|
|
|
cf_lock( tr_lockfile_state_t *tr_state,
|
|
|
|
char ** errstr )
|
2007-10-12 20:50:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * path = getLockFilename( );
|
2008-08-17 12:39:26 +00:00
|
|
|
const gboolean didLock = lockfile( path, tr_state, errstr );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-28 19:06:23 +00:00
|
|
|
if( didLock )
|
2007-10-12 20:50:03 +00:00
|
|
|
gl_lockpath = g_strdup( path );
|
|
|
|
g_atexit( cf_removelocks );
|
2007-10-22 14:15:16 +00:00
|
|
|
g_free( path );
|
2008-02-28 19:06:23 +00:00
|
|
|
return didLock;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 20:50:03 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Preferences
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-03-10 15:24:40 +00:00
|
|
|
static void cf_check_older_configs( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is where we initialize the preferences file with the default values.
|
|
|
|
* If you add a new preferences key, you /must/ add a default value here.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tr_prefs_init_defaults( tr_benc * d )
|
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
|
|
|
|
cf_check_older_configs( );
|
|
|
|
|
|
|
|
#ifdef HAVE_GIO
|
|
|
|
str = NULL;
|
|
|
|
if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD );
|
|
|
|
if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DESKTOP );
|
|
|
|
if( !str ) str = tr_getDefaultDownloadDir( );
|
|
|
|
tr_bencDictAddStr( d, PREF_KEY_DIR_WATCH, str );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_DIR_WATCH_ENABLED, FALSE );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_INHIBIT_HIBERNATION, FALSE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_BLOCKLIST_UPDATES_ENABLED, TRUE );
|
|
|
|
|
|
|
|
tr_bencDictAddStr( d, PREF_KEY_OPEN_DIALOG_FOLDER, g_get_home_dir( ) );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_TOOLBAR, TRUE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_FILTERBAR, TRUE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_STATUSBAR, TRUE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SHOW_TRAY_ICON, FALSE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SHOW_DESKTOP_NOTIFICATION, TRUE );
|
|
|
|
tr_bencDictAddStr( d, PREF_KEY_STATUSBAR_STATS, "total-ratio" );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SCHED_LIMIT_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SCHED_BEGIN, 60 * 23 ); /* 11pm */
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SCHED_END, 60 * 7 ); /* 7am */
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SCHED_DL_LIMIT, 200 ); /* 2x the other limit */
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SCHED_UL_LIMIT, 100 ); /* 2x the other limit */
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_OPTIONS_PROMPT, TRUE );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_HEIGHT, 500 );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_WIDTH, 300 );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_X, 50 );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_MAIN_WINDOW_Y, 50 );
|
|
|
|
tr_bencDictAddStr( d, PREF_KEY_MAIN_WINDOW_LAYOUT_ORDER, "menu,toolbar,filter,list,statusbar" );
|
|
|
|
|
|
|
|
str = NULL;
|
|
|
|
#if GLIB_CHECK_VERSION( 2, 14, 0 )
|
|
|
|
if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DOWNLOAD );
|
|
|
|
#endif
|
|
|
|
if( !str ) str = tr_getDefaultDownloadDir( );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, str );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_ASKQUIT, TRUE );
|
|
|
|
|
|
|
|
tr_bencDictAddStr( d, PREF_KEY_SORT_MODE, "sort-by-name" );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_SORT_REVERSED, FALSE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_MINIMAL_VIEW, FALSE );
|
|
|
|
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_START, TRUE );
|
|
|
|
tr_bencDictAddInt( d, PREF_KEY_TRASH_ORIGINAL, FALSE );
|
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
static char*
|
|
|
|
getPrefsFilename( void )
|
|
|
|
{
|
2007-10-13 03:15:02 +00:00
|
|
|
assert( gl_confdir != NULL );
|
2008-06-12 16:25:36 +00:00
|
|
|
return g_build_filename( gl_confdir, "settings.json", NULL );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static tr_benc*
|
|
|
|
getPrefs( void )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
static tr_benc settings;
|
2008-06-12 16:25:36 +00:00
|
|
|
static gboolean loaded = FALSE;
|
2007-09-27 20:57:58 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( !loaded )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencInitDict( &settings, 0 );
|
2009-03-10 15:24:40 +00:00
|
|
|
tr_prefs_init_defaults( &settings );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionLoadSettings( &settings, gl_confdir, MY_NAME );
|
2008-06-12 16:25:36 +00:00
|
|
|
loaded = TRUE;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
return &settings;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_benc*
|
|
|
|
pref_get_all( void )
|
|
|
|
{
|
|
|
|
return getPrefs( );
|
|
|
|
}
|
|
|
|
|
2008-09-02 20:59:00 +00:00
|
|
|
int64_t
|
2008-06-12 16:25:36 +00:00
|
|
|
pref_int_get( const char * key )
|
|
|
|
{
|
2008-09-02 20:59:00 +00:00
|
|
|
int64_t i = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
tr_bencDictFindInt( getPrefs( ), key, &i );
|
|
|
|
return i;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
pref_int_set( const char * key,
|
|
|
|
int64_t value )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-12-14 22:14:00 +00:00
|
|
|
tr_bencDictAddInt( getPrefs( ), key, value );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
double
|
|
|
|
pref_double_get( const char * key )
|
|
|
|
{
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
|
|
tr_bencDictFindDouble( getPrefs( ), key, &d );
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pref_double_set( const char * key,
|
|
|
|
double value )
|
|
|
|
{
|
|
|
|
tr_bencDictAddDouble( getPrefs( ), key, value );
|
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
gboolean
|
2008-09-23 19:11:04 +00:00
|
|
|
pref_flag_get( const char * key )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
tr_bencDictFindInt( getPrefs( ), key, &i );
|
|
|
|
return i != 0;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-19 14:54:32 +00:00
|
|
|
gboolean
|
2008-09-23 19:11:04 +00:00
|
|
|
pref_flag_eval( pref_flag_t val,
|
|
|
|
const char * key )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( val )
|
|
|
|
{
|
|
|
|
case PREF_FLAG_TRUE:
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case PREF_FLAG_FALSE:
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return pref_flag_get( key );
|
2008-03-19 14:54:32 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
pref_flag_set( const char * key,
|
|
|
|
gboolean value )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
pref_int_set( key, value != 0 );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
const char*
|
|
|
|
pref_string_get( const char * key )
|
|
|
|
{
|
|
|
|
const char * str = NULL;
|
|
|
|
tr_bencDictFindStr( getPrefs( ), key, &str );
|
|
|
|
return str;
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-06-12 16:25:36 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2009-03-10 15:24:40 +00:00
|
|
|
pref_string_set( const char * key, const char * value )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-12-14 22:14:00 +00:00
|
|
|
tr_bencDictAddStr( getPrefs( ), key, value );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-06-12 16:25:36 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2008-12-13 23:17:36 +00:00
|
|
|
pref_save( tr_session * session )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionSaveSettings( session, gl_confdir, getPrefs( ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-12-01 01:41:10 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
#if !GLIB_CHECK_VERSION( 2, 8, 0 )
|
2007-12-01 02:52:13 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_set_contents( const char * filename,
|
|
|
|
const void * out,
|
|
|
|
size_t len,
|
|
|
|
GError* unused UNUSED )
|
2007-12-01 02:52:13 +00:00
|
|
|
{
|
|
|
|
FILE * fp = fopen( filename, "wb+" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( fp != NULL )
|
|
|
|
{
|
2007-12-01 02:52:13 +00:00
|
|
|
fwrite( out, 1, len, fp );
|
|
|
|
fclose( fp );
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
#define g_file_set_contents tr_file_set_contents
|
2007-12-01 02:52:13 +00:00
|
|
|
#endif
|
|
|
|
|
2007-12-01 01:41:10 +00:00
|
|
|
static char*
|
2008-06-12 16:25:36 +00:00
|
|
|
getCompat080PrefsFilename( void )
|
2007-12-01 01:41:10 +00:00
|
|
|
{
|
|
|
|
assert( gl_confdir != NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
return g_build_filename( g_get_home_dir( ), ".transmission", "gtk", "prefs", NULL );
|
2008-04-05 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2008-06-12 16:25:36 +00:00
|
|
|
getCompat090PrefsFilename( void )
|
2008-04-05 20:12:11 +00:00
|
|
|
{
|
|
|
|
assert( gl_confdir != NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
return g_build_filename( g_get_home_dir( ), ".transmission", "gtk", "prefs.ini", NULL );
|
2007-12-01 01:41:10 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static char*
|
|
|
|
getCompat121PrefsFilename( void )
|
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
return g_build_filename( g_get_user_config_dir( ), "transmission", "gtk", "prefs.ini", NULL );
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-01 01:41:10 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
translate_08_to_09( const char* oldfile,
|
|
|
|
const char* newfile )
|
2007-12-01 01:41:10 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
static struct pref_entry {
|
2008-09-23 19:11:04 +00:00
|
|
|
const char* oldkey;
|
|
|
|
const char* newkey;
|
2007-12-01 01:41:10 +00:00
|
|
|
} pref_table[] = {
|
2008-10-07 15:17:52 +00:00
|
|
|
{ "add-behavior-ipc", "add-behavior-ipc" },
|
|
|
|
{ "add-behavior-standard", "add-behavior-standard" },
|
|
|
|
{ "download-directory", "default-download-directory" },
|
|
|
|
{ "download-limit", "download-limit" },
|
|
|
|
{ "use-download-limit", "download-limit-enabled" },
|
|
|
|
{ "listening-port", "listening-port" },
|
|
|
|
{ "use-nat-traversal", "nat-traversal-enabled" },
|
|
|
|
{ "use-peer-exchange", "pex-enabled" },
|
|
|
|
{ "ask-quit", "prompt-before-exit" },
|
|
|
|
{ "ask-download-directory", "prompt-for-download-directory" },
|
|
|
|
{ "use-tray-icon", "system-tray-icon-enabled" },
|
|
|
|
{ "upload-limit", "upload-limit" },
|
|
|
|
{ "use-upload-limit", "upload-limit-enabled" }
|
2007-12-01 01:41:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GString * out = g_string_new( NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
gchar * contents = NULL;
|
|
|
|
gsize contents_len = 0;
|
|
|
|
tr_benc top;
|
2007-12-01 01:41:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
memset( &top, 0, sizeof( tr_benc ) );
|
2007-12-01 01:41:10 +00:00
|
|
|
|
|
|
|
if( g_file_get_contents( oldfile, &contents, &contents_len, NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& !tr_bencLoad( contents, contents_len, &top, NULL )
|
|
|
|
&& top.type == TYPE_DICT )
|
2007-12-01 01:41:10 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
g_string_append( out, "\n[general]\n" );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < G_N_ELEMENTS( pref_table ); ++i )
|
|
|
|
{
|
|
|
|
const tr_benc * val = tr_bencDictFind( &top,
|
|
|
|
pref_table[i].oldkey );
|
|
|
|
if( val != NULL )
|
|
|
|
{
|
2007-12-01 01:41:10 +00:00
|
|
|
const char * valstr = val->val.s.s;
|
|
|
|
if( !strcmp( valstr, "yes" ) ) valstr = "true";
|
|
|
|
if( !strcmp( valstr, "no" ) ) valstr = "false";
|
2008-09-23 19:11:04 +00:00
|
|
|
g_string_append_printf( out, "%s=%s\n",
|
|
|
|
pref_table[i].newkey,
|
|
|
|
valstr );
|
2007-12-01 01:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_file_set_contents( newfile, out->str, out->len, NULL );
|
|
|
|
g_string_free( out, TRUE );
|
|
|
|
g_free( contents );
|
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
translate_keyfile_to_json( const char * old_file,
|
|
|
|
const char * new_file )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc dict;
|
2008-06-12 16:25:36 +00:00
|
|
|
GKeyFile * keyfile;
|
2008-09-23 19:11:04 +00:00
|
|
|
gchar ** keys;
|
|
|
|
gsize i;
|
|
|
|
gsize length;
|
2008-06-12 16:25:36 +00:00
|
|
|
|
2008-10-07 15:17:52 +00:00
|
|
|
static struct pref_entry {
|
2008-09-23 19:11:04 +00:00
|
|
|
const char* oldkey;
|
|
|
|
const char* newkey;
|
2008-06-12 16:25:36 +00:00
|
|
|
} renamed[] = {
|
2008-10-07 15:17:52 +00:00
|
|
|
{ "default-download-directory", "download-dir" },
|
|
|
|
{ "encrypted-connections-only", "encryption" },
|
|
|
|
{ "listening-port", "peer-port" },
|
|
|
|
{ "nat-traversal-enabled", "port-forwarding-enabled" },
|
|
|
|
{ "open-dialog-folder", "open-dialog-dir" },
|
|
|
|
{ "watch-folder", "watch-dir" },
|
|
|
|
{ "watch-folder-enabled", "watch-dir-enabled" }
|
2008-06-12 16:25:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
keyfile = g_key_file_new( );
|
|
|
|
g_key_file_load_from_file( keyfile, old_file, 0, NULL );
|
|
|
|
length = 0;
|
|
|
|
keys = g_key_file_get_keys( keyfile, "general", &length, NULL );
|
|
|
|
|
|
|
|
tr_bencInitDict( &dict, length );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < length; ++i )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
guint j;
|
2008-06-12 16:25:36 +00:00
|
|
|
const char * key = keys[i];
|
2008-09-23 19:11:04 +00:00
|
|
|
gchar * val = g_key_file_get_value( keyfile, "general", key,
|
|
|
|
NULL );
|
2008-06-12 16:25:36 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( j = 0; j < G_N_ELEMENTS( renamed ); ++j )
|
2008-06-12 16:25:36 +00:00
|
|
|
if( !strcmp( renamed[j].oldkey, key ) )
|
|
|
|
key = renamed[j].newkey;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !strcmp( val, "true" ) || !strcmp( val, "false" ) )
|
2008-06-12 17:39:35 +00:00
|
|
|
tr_bencDictAddInt( &dict, key, !strcmp( val, "true" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
char * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
long l;
|
2008-06-12 16:25:36 +00:00
|
|
|
errno = 0;
|
|
|
|
l = strtol( val, &end, 10 );
|
|
|
|
if( !errno && end && !*end )
|
|
|
|
tr_bencDictAddInt( &dict, key, l );
|
|
|
|
else
|
|
|
|
tr_bencDictAddStr( &dict, key, val );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free( val );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_key_file_free( keyfile );
|
|
|
|
tr_bencSaveJSONFile( new_file, &dict );
|
|
|
|
tr_bencFree( &dict );
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:24:40 +00:00
|
|
|
static void
|
2007-12-01 01:41:10 +00:00
|
|
|
cf_check_older_configs( void )
|
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
char * filename = getPrefsFilename( );
|
|
|
|
|
|
|
|
if( !g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
{
|
|
|
|
char * key1 = getCompat121PrefsFilename( );
|
|
|
|
char * key2 = getCompat090PrefsFilename( );
|
|
|
|
char * benc = getCompat080PrefsFilename( );
|
2008-04-05 20:12:11 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( g_file_test( key1, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
{
|
2009-03-10 15:24:40 +00:00
|
|
|
g_message( _( "Importing \"%s\"" ), key1 );
|
2008-06-12 16:25:36 +00:00
|
|
|
translate_keyfile_to_json( key1, filename );
|
|
|
|
}
|
|
|
|
else if( g_file_test( key2, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
{
|
2009-03-10 15:24:40 +00:00
|
|
|
g_message( _( "Importing \"%s\"" ), key2 );
|
2008-06-12 16:25:36 +00:00
|
|
|
translate_keyfile_to_json( key2, filename );
|
|
|
|
}
|
|
|
|
else if( g_file_test( benc, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
{
|
|
|
|
char * tmpfile;
|
2009-03-10 15:24:40 +00:00
|
|
|
int fd = g_file_open_tmp( "transmission-prefs-XXXXXX", &tmpfile, NULL );
|
|
|
|
g_message( _( "Importing \"%s\"" ), benc );
|
2008-06-12 16:25:36 +00:00
|
|
|
if( fd != -1 ) close( fd );
|
|
|
|
translate_08_to_09( benc, tmpfile );
|
|
|
|
translate_keyfile_to_json( tmpfile, filename );
|
|
|
|
unlink( tmpfile );
|
|
|
|
}
|
2007-12-01 01:41:10 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
g_free( benc );
|
|
|
|
g_free( key2 );
|
|
|
|
g_free( key1 );
|
|
|
|
}
|
2007-12-01 01:41:10 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
g_free( filename );
|
2007-12-01 01:41:10 +00:00
|
|
|
}
|