Expand tilde with file: and view: remote parameters.
authorLaurent Monin <geeqie@norz.org>
Tue, 13 May 2008 16:09:43 +0000 (16:09 +0000)
committerLaurent Monin <geeqie@norz.org>
Tue, 13 May 2008 16:09:43 +0000 (16:09 +0000)
Now these are working:
geeqie -r file:~/dir
geeqie -r view:~user/file

src/main.c
src/main.h
src/remote.c

index f549f10..8bcbeb8 100644 (file)
@@ -45,6 +45,9 @@
 
 
 #include <math.h>
+#ifdef G_OS_UNIX
+#include <pwd.h>
+#endif
 
 
 static RemoteConnection *remote_connection = NULL;
@@ -81,6 +84,54 @@ gchar *utf8_validate_or_convert(gchar *text)
        return text;
 }
 
+/* Borrowed from gtkfilesystemunix.c */
+gchar *expand_tilde(const gchar *filename)
+{
+#ifndef G_OS_UNIX
+       return g_strdup(filename);
+#else
+       const char *notilde;
+       const char *slash;
+       const char *home;
+
+       if (filename[0] != '~')
+               return g_strdup(filename);
+
+       notilde = filename + 1;
+       slash = strchr(notilde, G_DIR_SEPARATOR);
+       if (slash == notilde || !*notilde)
+               {
+               home = g_get_home_dir();
+               if (!home)
+                       return g_strdup(filename);
+               }
+       else
+               {
+               gchar *username;
+               struct passwd *passwd;
+
+               if (slash)
+                       username = g_strndup(notilde, slash - notilde);
+               else
+                       username = g_strdup(notilde);
+
+               passwd = getpwnam(username);
+               g_free(username);
+
+               if (!passwd)
+                       return g_strdup(filename);
+
+               home = passwd->pw_dir;
+               }
+
+       if (slash)
+               return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
+       else
+               return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
+#endif
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  * keyboard functions
index 732f09c..a424a8a 100644 (file)
 
 gdouble get_zoom_increment(void);
 gchar *utf8_validate_or_convert(gchar *text);
+gchar *expand_tilde(const gchar *filename);
 
 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event);
 gint key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
index dcb0954..ebfa8b9 100644 (file)
@@ -475,30 +475,37 @@ static void gr_quit(const gchar *text, gpointer data)
 
 static void gr_file_load(const gchar *text, gpointer data)
 {
-       if (isfile(text))
+       gchar *filename = expand_tilde(text);
+
+       if (isfile(filename))
                {
-               if (file_extension_match(text, ".gqv"))
+               if (file_extension_match(filename, ".gqv"))
                        {
-                       collection_window_new(text);
+                       collection_window_new(filename);
                        }
                else
                        {
-                       layout_set_path(NULL, text);
+                       layout_set_path(NULL, filename);
                        }
                }
-       else if (isdir(text))
+       else if (isdir(filename))
                {
-               layout_set_path(NULL, text);
+               layout_set_path(NULL, filename);
                }
        else
                {
-               printf("remote sent filename that does not exist:\"%s\"\n", text);
+               printf("remote sent filename that does not exist:\"%s\"\n", filename);
                }
+
+       g_free(filename);
 }
 
 static void gr_file_view(const gchar *text, gpointer data)
 {
-       view_window_new(file_data_new_simple(text));
+       gchar *filename = expand_tilde(text);
+
+       view_window_new(file_data_new_simple(filename));
+       g_free(filename);
 }
 
 static void gr_list_clear(const gchar *text, gpointer data)