235403f
--- gftp-2.0.18/src/gtk/gftp-gtk.c.user-dirs	2007-03-15 10:30:40.000000000 +0100
235403f
+++ gftp-2.0.18/src/gtk/gftp-gtk.c	2007-03-15 10:32:00.000000000 +0100
235403f
@@ -1264,11 +1264,130 @@
235403f
   return (0);
235403f
 }
235403f
 
235403f
+static char *
235403f
+xdg_user_dir_lookup (const char *type)
235403f
+{
235403f
+  FILE *file;
235403f
+  char *home_dir, *config_home, *config_file;
235403f
+  char buffer[512];
235403f
+  char *user_dir;
235403f
+  char *p, *d;
235403f
+  int len;
235403f
+  int relative;
235403f
+  
235403f
+  home_dir = getenv ("HOME");
235403f
+
235403f
+  if (home_dir == NULL)
235403f
+    return strdup ("/tmp");
235403f
+
235403f
+  config_home = getenv ("XDG_CONFIG_HOME");
235403f
+  if (config_home == NULL || config_home[0] == 0)
235403f
+    {
235403f
+      config_file = malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1);
235403f
+      strcpy (config_file, home_dir);
235403f
+      strcat (config_file, "/.config/user-dirs.dirs");
235403f
+    }
235403f
+  else
235403f
+    {
235403f
+      config_file = malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1);
235403f
+      strcpy (config_file, config_home);
235403f
+      strcat (config_file, "/user-dirs.dirs");
235403f
+    }
235403f
+
235403f
+  file = fopen (config_file, "r");
235403f
+  free (config_file);
235403f
+  if (file == NULL)
235403f
+    goto error;
235403f
+
235403f
+  user_dir = NULL;
235403f
+  while (fgets (buffer, sizeof (buffer), file))
235403f
+    {
235403f
+      /* Remove newline at end */
235403f
+      len = strlen (buffer);
235403f
+      if (len > 0 && buffer[len-1] == '\n')
235403f
+	buffer[len-1] = 0;
235403f
+      
235403f
+      p = buffer;
235403f
+      while (*p == ' ' || *p == '\t')
235403f
+	p++;
235403f
+      
235403f
+      if (strncmp (p, "XDG_", 4) != 0)
235403f
+	continue;
235403f
+      p += 4;
235403f
+      if (strncmp (p, type, strlen (type)) != 0)
235403f
+	continue;
235403f
+      p += strlen (type);
235403f
+      if (strncmp (p, "_DIR", 4) != 0)
235403f
+	continue;
235403f
+      p += 4;
235403f
+
235403f
+      while (*p == ' ' || *p == '\t')
235403f
+	p++;
235403f
+
235403f
+      if (*p != '=')
235403f
+	continue;
235403f
+      p++;
235403f
+      
235403f
+      while (*p == ' ' || *p == '\t')
235403f
+	p++;
235403f
+
235403f
+      if (*p != '"')
235403f
+	continue;
235403f
+      p++;
235403f
+      
235403f
+      relative = 0;
235403f
+      if (strncmp (p, "$HOME/", 6) == 0)
235403f
+	{
235403f
+	  p += 6;
235403f
+	  relative = 1;
235403f
+	}
235403f
+      else if (*p != '/')
235403f
+	continue;
235403f
+      
235403f
+      if (relative)
235403f
+	{
235403f
+	  user_dir = malloc (strlen (home_dir) + 1 + strlen (p) + 1);
235403f
+	  strcpy (user_dir, home_dir);
235403f
+	  strcat (user_dir, "/");
235403f
+	}
235403f
+      else
235403f
+	{
235403f
+	  user_dir = malloc (strlen (p) + 1);
235403f
+	  *user_dir = 0;
235403f
+	}
235403f
+      
235403f
+      d = user_dir + strlen (user_dir);
235403f
+      while (*p && *p != '"')
235403f
+	{
235403f
+	  if ((*p == '\\') && (*(p+1) != 0))
235403f
+	    p++;
235403f
+	  *d++ = *p++;
235403f
+	}
235403f
+      *d = 0;
235403f
+    }  
235403f
+  fclose (file);
235403f
+
235403f
+  if (user_dir)
235403f
+    return user_dir;
235403f
+
235403f
+ error:
235403f
+  /* Special case desktop for historical compatibility */
235403f
+  if (strcmp (type, "DESKTOP") == 0)
235403f
+    {
235403f
+      user_dir = malloc (strlen (home_dir) + strlen ("/Desktop") + 1);
235403f
+      strcpy (user_dir, home_dir);
235403f
+      strcat (user_dir, "/Desktop");
235403f
+      return user_dir;
235403f
+    }
235403f
+  else
235403f
+    return strdup (home_dir);
235403f
+}
235403f
 
235403f
 int
235403f
 main (int argc, char **argv)
235403f
 {
235403f
   GtkWidget *window, *ui;
235403f
+  char *cwd, *download;
235403f
 
235403f
   /* We override the read color functions because we are using a GdkColor 
235403f
      structures to store the color. If I put this in lib/config_file.c, then 
235403f
@@ -1287,6 +1406,19 @@
235403f
   gtk_set_locale ();
235403f
   gtk_init (&argc, &argv);
235403f
 
235403f
+  cwd = g_get_current_dir ();
235403f
+  if (strcmp (cwd, g_get_home_dir ()) == 0)
235403f
+    {
235403f
+      /* We're launched from the home dir, probably started from the UI, default
235403f
+	 to the download directory */
235403f
+
235403f
+      download = xdg_user_dir_lookup ("DOWNLOAD");
235403f
+      chdir (download); /* Ignore errors */
235403f
+      free (download);
235403f
+    }
235403f
+  g_free (cwd);
235403f
+
235403f
+  
235403f
   graphic_hash_table = g_hash_table_new (string_hash_function,
235403f
                                          string_hash_compare);
235403f