From f8a7c4b347c65057bc36f43e2b9ab7660bedbbe4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 15 Nov 2008 17:39:54 +0000 Subject: [PATCH] #1406: need tr_getDefaultDownloadDir() for consistency between apps --- cli/cli.c | 5 ++--- daemon/daemon.c | 7 ++----- daemon/remote.c | 19 +++++++++++++++++++ gtk/tr-prefs.c | 2 +- libtransmission/platform.c | 11 +++++++++++ libtransmission/session.c | 3 +-- libtransmission/transmission.h | 27 +++++++++++++++++---------- libtransmission/utils.c | 13 ------------- libtransmission/utils.h | 3 --- 9 files changed, 53 insertions(+), 37 deletions(-) diff --git a/cli/cli.c b/cli/cli.c index b240adee8..66e18d766 100644 --- a/cli/cli.c +++ b/cli/cli.c @@ -349,9 +349,8 @@ main( int argc, if( configdir == NULL ) configdir = tr_getDefaultConfigDir( ); - /* if no download directory specified, use cwd instead */ - if( !downloadDir ) - downloadDir = tr_strdup( tr_getcwd( ) ); + if( downloadDir == NULL ) + downloadDir = tr_getDefaultDownloadDir( ); /* Initialize libtransmission */ h = tr_sessionInitFull( diff --git a/daemon/daemon.c b/daemon/daemon.c index 3428bb6aa..fad29bece 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -167,7 +167,6 @@ session_init( const char * configDir, const char * password, int blocklistEnabled ) { - char * mycwd; tr_benc state, *dict = NULL; int peerPort = -1, peers = -1; int whitelistEnabled = -1; @@ -190,8 +189,8 @@ session_init( const char * configDir, **** If neither of those can be found, the TR_DEFAULT fields are used . ***/ - mycwd = tr_getcwd( ); - getConfigStr( dict, KEY_DOWNLOAD_DIR, &downloadDir, mycwd ); + getConfigStr( dict, KEY_DOWNLOAD_DIR, &downloadDir, + TR_DEFAULT_DOWNLOAD_DIR ); getConfigInt( dict, KEY_PEX_ENABLED, &pexEnabled, TR_DEFAULT_PEX_ENABLED ); getConfigInt( dict, KEY_PORT_FORWARDING, &fwdEnabled, @@ -258,8 +257,6 @@ session_init( const char * configDir, if( dict ) tr_bencFree( &state ); - - tr_free( mycwd ); } static const char * diff --git a/daemon/remote.c b/daemon/remote.c index 5522e4b82..387e5056c 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -15,6 +15,12 @@ #include #include /* strcmp */ +#ifdef WIN32 + #include /* getcwd */ +#else + #include /* getcwd */ +#endif + #include #include @@ -143,6 +149,19 @@ static int reqCount = 0; static int debug = 0; static char * auth = NULL; +static char* +tr_getcwd( void ) +{ + char buf[2048]; + *buf = '\0'; +#ifdef WIN32 + _getcwd( buf, sizeof( buf ) ); +#else + getcwd( buf, sizeof( buf ) ); +#endif + return tr_strdup( buf ); +} + static char* absolutify( const char * path ) { diff --git a/gtk/tr-prefs.c b/gtk/tr-prefs.c index b3f297358..fd8d9c9a1 100644 --- a/gtk/tr-prefs.c +++ b/gtk/tr-prefs.c @@ -48,7 +48,7 @@ tr_prefs_init_global( void ) #if HAVE_GIO str = NULL; if( !str ) str = g_get_user_special_dir( G_USER_DIRECTORY_DESKTOP ); - if( !str ) str = g_get_home_dir( ); + if( !str ) str = tr_getDefaultDownloadDir( ); pref_string_set_default ( PREF_KEY_DIR_WATCH, str ); pref_flag_set_default ( PREF_KEY_DIR_WATCH_ENABLED, FALSE ); #endif diff --git a/libtransmission/platform.c b/libtransmission/platform.c index 70c937221..e05865da1 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -483,6 +483,17 @@ tr_getDefaultConfigDir( void ) return s; } +const char* +tr_getDefaultDownloadDir( void ) +{ + static char * s = NULL; + + if( s == NULL ) + s = tr_buildPath( getHomeDir( ), "Downloads", NULL ); + + return s; +} + /*** **** ***/ diff --git a/libtransmission/session.c b/libtransmission/session.c index 1a1439ff1..f3450abe4 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -310,11 +310,10 @@ tr_sessionInitFull( const char * configDir, tr_handle * tr_sessionInit( const char * configDir, - const char * downloadDir, const char * tag ) { return tr_sessionInitFull( configDir, - downloadDir, + TR_DEFAULT_DOWNLOAD_DIR, tag, TR_DEFAULT_PEX_ENABLED, TR_DEFAULT_PORT_FORWARDING_ENABLED, diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index aacbf57c8..20d034dae 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -57,18 +57,24 @@ typedef uint64_t tr_block_index_t; * @brief returns Transmission's default configuration file directory. * * The default configuration directory is determined this way: - * - If the TRANSMISSION_HOME environmental variable is set, - * its value is returned. - * - otherwise, if we're running on Darwin, - * "$HOME/Library/Application Support/Transmission" is returned. - * - otherwise, if we're running on WIN32, - * "$EXE_FOLDER/Transmission" is returned. - * - otherwise, if XDG_CONFIG_HOME is set, - * "$XDG_CONFIG_HOME/transmission" is returned. - * - lastly, $HOME/.config/transmission" is used. + * 1. If the TRANSMISSION_HOME environmental variable is set, its value is used. + * 2. On Darwin, "${HOME}/Library/Application Support/Transmission" is used. + * 3. On Windows, "${CSIDL_APPDATA}/Transmission" is used. + * 4. If XDG_CONFIG_HOME is set, "${XDG_CONFIG_HOME}/transmission" is used. + * 5. ${HOME}/.config/transmission" is used as a last resort. */ const char* tr_getDefaultConfigDir( void ); +/** + * @brief returns Transmisson's default download directory. + * + * The default download directory is determined this way: + * 1. If the HOME environmental variable is set, "${HOME}/Downloads" is used. + * 2. On Windows, "${CSIDL_MYDOCUMENTS}/Downloads" is used. + * 3. Otherwise, getpwuid(getuid())->pw_dir + "/Downloads" is used. + */ +const char* tr_getDefaultDownloadDir( void ); + typedef struct tr_ctor tr_ctor; typedef struct tr_handle tr_handle; typedef struct tr_info tr_info; @@ -98,6 +104,8 @@ tr_proxy_type; /** @see tr_sessionInitFull */ #define TR_DEFAULT_CONFIG_DIR tr_getDefaultConfigDir( ) /** @see tr_sessionInitFull */ +#define TR_DEFAULT_DOWNLOAD_DIR tr_getDefaultDownloadDir( ) +/** @see tr_sessionInitFull */ #ifdef TR_EMBEDDED #define TR_DEFAULT_ENCRYPTION TR_CLEAR_PREFERRED #else @@ -285,7 +293,6 @@ tr_session * tr_sessionInitFull( const char * configDir, /** @brief Shorter form of tr_sessionInitFull() @deprecated Use tr_sessionInitFull() instead. */ tr_session * tr_sessionInit( const char * configDir, - const char * downloadDir, const char * tag ); /** @brief End a libtransmission session diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 27b82687f..bfda5d870 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -490,19 +490,6 @@ tr_loadFile( const char * path, return buf; } -char* -tr_getcwd( void ) -{ - char buf[2048]; - *buf = '\0'; -#ifdef WIN32 - _getcwd( buf, sizeof( buf ) ); -#else - getcwd( buf, sizeof( buf ) ); -#endif - return tr_strdup( buf ); -} - char* tr_basename( const char * path ) { diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 5d90d610a..84cb57467 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -144,9 +144,6 @@ void tr_deepLog( const char * file, char* tr_getLogTimeStr( char * buf, int buflen ); -/** a portability wrapper for getcwd(). */ -char* tr_getcwd( void ) TR_GNUC_MALLOC; - /** a portability wrapper for basename(). */ char* tr_basename( const char * path ) TR_GNUC_MALLOC;