You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
transmission/transmission-1.51-xdg-downl...

139 lines
3.7 KiB

Use the XDG download directory for downloads instead of
made-up unusally nonesixtent ~/Downloads (fall back to it
in case there's no XDG user dir configuration).
Lubomir Rintel <lkundrak@v3.sk>
diff -up transmission-1.51/libtransmission/platform.c.xdg-download-dir transmission-1.51/libtransmission/platform.c
--- transmission-1.51/libtransmission/platform.c.xdg-download-dir 2009-03-28 12:30:43.817212449 +0100
+++ transmission-1.51/libtransmission/platform.c 2009-03-28 12:31:39.752214378 +0100
@@ -443,15 +443,124 @@ tr_getDefaultConfigDir( const char * app
return s;
}
+/* This was stolen from gthumb, though it probably originates from
+ * xdg-user-dirs's xdg-user-dir-lookup.c. See:
+ * http://www.redhat.com/archives/fedora-devel-list/2007-March/msg00677.html
+ */
const char*
tr_getDefaultDownloadDir( void )
{
- static char * s = NULL;
+ static char * user_dir = NULL;
+ char type[] = "DOWNLOAD";
+ FILE * file;
+ char * home_dir, * config_home, * config_file;
+ char buffer[512];
+ char * p, * d;
+ int len;
+ int relative;
+
+ if( user_dir != NULL )
+ return user_dir;
+
+ home_dir = getenv( "HOME" );
+ if( home_dir == NULL )
+ return strdup("/tmp" );
- if( s == NULL )
- s = tr_buildPath( getHomeDir( ), "Downloads", NULL );
+ config_home = getenv( "XDG_CONFIG_HOME" );
+ if( config_home == NULL || config_home[0] == 0 )
+ {
+ config_file = malloc( strlen( home_dir ) + strlen( "/.config/user-dirs.dirs" ) + 1 );
+ strcpy( config_file, home_dir );
+ strcat( config_file, "/.config/user-dirs.dirs" );
+ }
+ else
+ {
+ config_file = malloc( strlen ( config_home ) + strlen( "/user-dirs.dirs" ) + 1 );
+ strcpy( config_file, config_home );
+ strcat( config_file, "/user-dirs.dirs" );
+ }
- return s;
+ file = fopen( config_file, "r" );
+ free( config_file );
+ if( file == NULL )
+ goto error;
+
+ while( fgets( buffer, sizeof( buffer ), file ) )
+ {
+ /* Remove newline at end */
+ len = strlen( buffer );
+ if( len > 0 && buffer[len-1] == '\n' )
+ buffer[len-1] = 0;
+
+ p = buffer;
+ while( *p == ' ' || *p == '\t' )
+ p++;
+
+ if( strncmp( p, "XDG_", 4 ) != 0 )
+ continue;
+ p += 4;
+
+ if( strncmp(p, type, strlen (type )) != 0)
+ continue;
+ p += strlen( type );
+
+ if( strncmp(p, "_DIR", 4 ) != 0)
+ continue;
+ p += 4;
+
+ while( *p == ' ' || *p == '\t' )
+ p++;
+
+ if( *p != '=' )
+ continue;
+ p++;
+
+ while( *p == ' ' || *p == '\t' )
+ p++;
+
+ if( *p != '"' )
+ continue;
+ p++;
+
+ relative = 0;
+ if( strncmp(p, "$HOME/", 6 ) == 0)
+ {
+ p += 6;
+ relative = 1;
+ }
+ else
+ if( *p != '/' )
+ continue;
+
+ if( relative )
+ {
+ user_dir = malloc( strlen ( home_dir ) + 1 + strlen ( p ) + 1 );
+ strcpy( user_dir, home_dir );
+ strcat( user_dir, "/" );
+ }
+ else
+ {
+ user_dir = malloc( strlen ( p ) + 1 );
+ *user_dir = 0;
+ }
+
+ d = user_dir + strlen( user_dir );
+ while( *p && *p != '"' )
+ {
+ if( ( *p == '\\' ) && ( *( p + 1 ) != 0 ))
+ p++;
+ *d++ = *p++;
+ }
+ *d = 0;
+ }
+
+ fclose( file );
+
+error:
+ if( !user_dir )
+ user_dir = tr_buildPath( getHomeDir( ), "Downloads", NULL );
+
+ return user_dir;
}
/***