Changeset 8136


Ignore:
Timestamp:
Apr 5, 2009, 1:33:32 PM (14 years ago)
Author:
charles
Message:

(trunk libT) #1957: tr_getDownloadDir() should honor XDG

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/platform.c

    r7978 r8136  
    4141
    4242#include "transmission.h"
     43#include "ggets.h"
    4344#include "session.h"
    4445#include "list.h"
     
    444445}
    445446
     447/* This was stolen from gthumb, though it probably originates from
     448 * xdg-user-dirs's xdg-user-dir-lookup.c. See:
     449 * http://www.redhat.com/archives/fedora-devel-list/2007-March/msg00677.html
     450 */
    446451const char*
    447452tr_getDefaultDownloadDir( void )
    448453{
    449     static char * s = NULL;
    450 
    451     if( s == NULL )
    452         s = tr_buildPath( getHomeDir( ), "Downloads", NULL );
    453 
    454     return s;
     454    static char * user_dir = NULL;
     455
     456#ifdef SYS_DARWIN
     457
     458    user_dir = tr_buildPath( getHomeDir( ), "Downloads", NULL );
     459
     460#else
     461
     462    if( user_dir == NULL )
     463    {
     464        const char * config_home;
     465        char * config_file;
     466        char * content;
     467        size_t content_len;
     468
     469        /* figure out where to look for user-dirs.dirs */
     470        config_home = getenv( "XDG_CONFIG_HOME" );
     471        if( config_home && *config_home )
     472            config_file = tr_buildPath( config_home, "user-dirs.dirs", NULL );
     473        else
     474            config_file = tr_buildPath( getHomeDir( ), ".config", "user-dirs.dirs", NULL );
     475
     476        /* read in user-dirs.dirs and look for the download dir entry */
     477        content = (char *) tr_loadFile( config_file, &content_len );
     478        if( content && content_len>0 )
     479        {
     480            const char * key = "XDG_DOWNLOAD_DIR=\"";
     481            char * line = strstr( content, key );
     482            if( line != NULL )
     483            {
     484                char * value = line + strlen( key );
     485                char * end = strchr( value, '"' );
     486
     487                if( end )
     488                {
     489                    *end = '\0';
     490
     491                    if( !memcmp( value, "$HOME/", 6 ) )
     492                        user_dir = tr_buildPath( getHomeDir( ), value+6, NULL );
     493                    else
     494                        user_dir = tr_strdup( value );
     495                }
     496            }
     497        }
     498
     499        if( user_dir == NULL )
     500            user_dir = tr_buildPath( getHomeDir( ), "Downloads", NULL );
     501
     502        tr_free( content );
     503        tr_free( config_file );
     504    }
     505
     506#endif
     507
     508    return user_dir;
    455509}
    456510
Note: See TracChangeset for help on using the changeset viewer.