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"
|
|
|
|
#include "util.h"
|
|
|
|
|
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
|
2007-10-13 03:15:02 +00:00
|
|
|
cf_init(const char *dir, char **errstr)
|
|
|
|
{
|
|
|
|
if( errstr != NULL )
|
|
|
|
*errstr = NULL;
|
|
|
|
|
|
|
|
gl_confdir = g_build_filename( dir, "gtk", NULL );
|
|
|
|
|
|
|
|
if( mkdir_p(gl_confdir, 0755 ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if( errstr != NULL )
|
2008-03-04 02:02:25 +00:00
|
|
|
*errstr = g_strdup_printf( _( "Couldn't create \"%s\": %s" ),
|
2007-10-13 03:15:02 +00:00
|
|
|
gl_confdir, g_strerror(errno) );
|
|
|
|
|
|
|
|
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
|
|
|
|
lockfile(const char * filename, char **errstr)
|
2007-10-13 03:15:02 +00:00
|
|
|
{
|
2008-02-28 19:06:23 +00:00
|
|
|
const int state = tr_lockfile( filename );
|
|
|
|
const gboolean success = state == TR_LOCKFILE_SUCCESS;
|
|
|
|
|
|
|
|
if( errstr ) switch( state ) {
|
|
|
|
case TR_LOCKFILE_EOPEN:
|
2008-03-04 02:02:25 +00:00
|
|
|
*errstr = g_strdup_printf( _( "Couldn't open \"%s\": %s" ),
|
|
|
|
filename, g_strerror( errno ) );
|
2008-02-28 19:06:23 +00:00
|
|
|
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-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 )
|
|
|
|
{
|
|
|
|
g_unlink( gl_lockpath );
|
|
|
|
g_free( gl_lockpath );
|
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
|
|
|
|
cf_lock( char ** errstr )
|
|
|
|
{
|
2007-10-22 14:15:16 +00:00
|
|
|
char * path = getLockFilename( );
|
2008-02-28 19:06:23 +00:00
|
|
|
const gboolean didLock = lockfile( path, errstr );
|
|
|
|
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
|
|
|
char*
|
|
|
|
cf_sockname( void )
|
|
|
|
{
|
2007-10-13 03:15:02 +00:00
|
|
|
assert( gl_confdir != NULL );
|
|
|
|
return g_build_filename( gl_confdir, "socket", NULL );
|
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
|
|
|
|
2007-10-03 21:02:30 +00:00
|
|
|
#define GROUP "general"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
static char*
|
|
|
|
getPrefsFilename( void )
|
|
|
|
{
|
2007-10-13 03:15:02 +00:00
|
|
|
assert( gl_confdir != NULL );
|
|
|
|
return g_build_filename( gl_confdir, "prefs.ini", NULL );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
static GKeyFile*
|
|
|
|
getPrefsKeyFile( void )
|
|
|
|
{
|
|
|
|
static GKeyFile * myKeyFile = NULL;
|
|
|
|
|
|
|
|
if( myKeyFile == NULL )
|
|
|
|
{
|
|
|
|
char * filename = getPrefsFilename( );
|
|
|
|
myKeyFile = g_key_file_new( );
|
|
|
|
g_key_file_load_from_file( myKeyFile, filename, 0, NULL );
|
|
|
|
g_free( filename );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
return myKeyFile;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
int
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_int_get( const char * key ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
return g_key_file_get_integer( getPrefsKeyFile( ), GROUP, key, NULL );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_int_set( const char * key, int value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
g_key_file_set_integer( getPrefsKeyFile( ), GROUP, key, value );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_int_set_default( const char * key, int value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
if( !g_key_file_has_key( getPrefsKeyFile( ), GROUP, key, NULL ) )
|
2007-09-27 20:57:58 +00:00
|
|
|
pref_int_set( key, value );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
gboolean
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_flag_get ( const char * key ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
return g_key_file_get_boolean( getPrefsKeyFile( ), GROUP, key, NULL );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2008-03-19 14:54:32 +00:00
|
|
|
gboolean
|
|
|
|
pref_flag_eval( pref_flag_t val, const char * key ) {
|
|
|
|
switch( val ) {
|
|
|
|
case PREF_FLAG_TRUE: return TRUE;
|
|
|
|
case PREF_FLAG_FALSE: return FALSE;
|
|
|
|
default: return pref_flag_get( key );
|
|
|
|
}
|
|
|
|
}
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_flag_set( const char * key, gboolean value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
g_key_file_set_boolean( getPrefsKeyFile( ), GROUP, key, value );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_flag_set_default( const char * key, gboolean value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
if( !g_key_file_has_key( getPrefsKeyFile( ), GROUP, key, NULL ) )
|
2007-09-27 20:57:58 +00:00
|
|
|
pref_flag_set( key, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_string_get( const char * key ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
return g_key_file_get_string( getPrefsKeyFile( ), GROUP, key, NULL );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_string_set( const char * key, const char * value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
g_key_file_set_string( getPrefsKeyFile( ), GROUP, key, value );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
|
|
|
void
|
2007-10-13 03:15:02 +00:00
|
|
|
pref_string_set_default( const char * key, const char * value ) {
|
2007-10-03 21:02:30 +00:00
|
|
|
if( !g_key_file_has_key( getPrefsKeyFile( ), GROUP, key, NULL ) )
|
2007-09-27 20:57:58 +00:00
|
|
|
pref_string_set( key, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pref_save(char **errstr)
|
|
|
|
{
|
|
|
|
gsize datalen;
|
2007-09-28 00:46:22 +00:00
|
|
|
GError * err = NULL;
|
2007-09-27 20:57:58 +00:00
|
|
|
char * data;
|
2007-09-28 00:46:22 +00:00
|
|
|
char * filename;
|
|
|
|
char * path;
|
|
|
|
|
|
|
|
filename = getPrefsFilename( );
|
|
|
|
path = g_path_get_dirname( filename );
|
|
|
|
mkdir_p( path, 0755 );
|
2007-09-27 20:57:58 +00:00
|
|
|
|
|
|
|
data = g_key_file_to_data( getPrefsKeyFile(), &datalen, &err );
|
|
|
|
if( !err ) {
|
|
|
|
GIOChannel * out = g_io_channel_new_file( filename, "w+", &err );
|
|
|
|
g_io_channel_write_chars( out, data, datalen, NULL, &err );
|
|
|
|
g_io_channel_unref( out );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( errstr != NULL )
|
|
|
|
*errstr = err ? g_strdup( err->message ) : NULL;
|
|
|
|
|
|
|
|
g_clear_error( &err );
|
2007-09-28 00:46:22 +00:00
|
|
|
g_free( data );
|
|
|
|
g_free( path );
|
|
|
|
g_free( filename );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-12-01 01:41:10 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-01 02:52:13 +00:00
|
|
|
#if !GLIB_CHECK_VERSION(2,8,0)
|
|
|
|
static void
|
|
|
|
tr_file_set_contents( const char * filename, const void * out, size_t len, GError* unused UNUSED )
|
|
|
|
{
|
|
|
|
FILE * fp = fopen( filename, "wb+" );
|
|
|
|
if( fp != NULL ) {
|
|
|
|
fwrite( out, 1, len, fp );
|
|
|
|
fclose( fp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define g_file_set_contents tr_file_set_contents
|
|
|
|
#endif
|
|
|
|
|
2007-12-01 01:41:10 +00:00
|
|
|
static char*
|
|
|
|
getCompat08PrefsFilename( void )
|
|
|
|
{
|
|
|
|
assert( gl_confdir != NULL );
|
|
|
|
return g_build_filename( gl_confdir, "prefs", NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
translate_08_to_09( const char* oldfile, const char* newfile )
|
|
|
|
{
|
|
|
|
static struct pref_entry {
|
|
|
|
const char* oldkey;
|
|
|
|
const char* newkey;
|
|
|
|
} pref_table[] = {
|
|
|
|
{ "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"}
|
|
|
|
};
|
|
|
|
|
|
|
|
GString * out = g_string_new( NULL );
|
|
|
|
gchar * contents = NULL;
|
|
|
|
gsize contents_len = 0;
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc top;
|
2007-12-01 01:41:10 +00:00
|
|
|
|
2008-02-26 21:58:58 +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 )
|
|
|
|
&& !tr_bencLoad( contents, contents_len, &top, NULL )
|
|
|
|
&& top.type==TYPE_DICT )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
g_string_append( out, "\n[general]\n" );
|
|
|
|
for ( i=0; i<G_N_ELEMENTS(pref_table); ++i ) {
|
2008-02-26 21:58:58 +00:00
|
|
|
const tr_benc * val = tr_bencDictFind( &top, pref_table[i].oldkey );
|
2007-12-01 01:41:10 +00:00
|
|
|
if( val != NULL ) {
|
|
|
|
const char * valstr = val->val.s.s;
|
|
|
|
if( !strcmp( valstr, "yes" ) ) valstr = "true";
|
|
|
|
if( !strcmp( valstr, "no" ) ) valstr = "false";
|
|
|
|
g_string_append_printf( out, "%s=%s\n", pref_table[i].newkey, valstr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_file_set_contents( newfile, out->str, out->len, NULL );
|
|
|
|
g_string_free( out, TRUE );
|
|
|
|
g_free( contents );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cf_check_older_configs( void )
|
|
|
|
{
|
|
|
|
char * cfn = getPrefsFilename( );
|
|
|
|
char * cfn08 = getCompat08PrefsFilename( );
|
|
|
|
|
|
|
|
if( !g_file_test( cfn, G_FILE_TEST_IS_REGULAR )
|
|
|
|
&& g_file_test( cfn08, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
translate_08_to_09( cfn08, cfn );
|
|
|
|
|
|
|
|
g_free( cfn08 );
|
|
|
|
g_free( cfn );
|
|
|
|
}
|