changeset 653:e06947d07086

Expand tilde with file: and view: remote parameters. Now these are working: geeqie -r file:~/dir geeqie -r view:~user/file
author zas_
date Tue, 13 May 2008 16:09:43 +0000
parents 9bcfd6d7a902
children 6dcfac4b356f
files src/main.c src/main.h src/remote.c
diffstat 3 files changed, 67 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.c	Tue May 13 14:49:38 2008 +0000
+++ b/src/main.c	Tue May 13 16:09:43 2008 +0000
@@ -45,6 +45,9 @@
 
 
 #include <math.h>
+#ifdef G_OS_UNIX
+#include <pwd.h>
+#endif
 
 
 static RemoteConnection *remote_connection = NULL;
@@ -81,6 +84,54 @@
 	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
--- a/src/main.h	Tue May 13 14:49:38 2008 +0000
+++ b/src/main.h	Tue May 13 16:09:43 2008 +0000
@@ -126,6 +126,7 @@
 
 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);
--- a/src/remote.c	Tue May 13 14:49:38 2008 +0000
+++ b/src/remote.c	Tue May 13 16:09:43 2008 +0000
@@ -475,30 +475,37 @@
 
 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)