[eog] Use new DBus API to show the current image in the file browser



commit fa744735911a7445bd83f2119385b82e674b020c
Author: Felix Riemann <friemann gnome org>
Date:   Thu Dec 15 19:36:06 2011 +0100

    Use new DBus API to show the current image in the file browser
    
    This improves the functionality implemented with commit 9df5fd43.
    The new API that will be included in Nautilus 3.3.4 not only opens a
    view for the containing folder but also marks the image in the view.
    The old behaviour is still available as fallback if the new API is
    not offered on the system.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=650402

 src/eog-util.c   |   86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/eog-util.h   |    4 ++
 src/eog-window.c |   40 ++++---------------------
 3 files changed, 96 insertions(+), 34 deletions(-)
---
diff --git a/src/eog-util.c b/src/eog-util.c
index 3c807cd..3dabd64 100644
--- a/src/eog-util.c
+++ b/src/eog-util.c
@@ -418,3 +418,89 @@ eog_util_file_is_persistent (GFile *file)
 
 	return TRUE;
 }
+
+static void
+_eog_util_show_file_in_filemanager_fallback (GFile *file, GdkScreen *screen)
+{
+	gchar *uri = NULL;
+	GError *error = NULL;
+	guint32 timestamp = gtk_get_current_event_time ();
+
+	if (g_file_query_file_type (file, 0, NULL) == G_FILE_TYPE_DIRECTORY) {
+		uri = g_file_get_uri (file);
+	} else {
+		/* If input file is not a directory we must open it's
+		   folder/parent to avoid opening the file itself     */
+		GFile *parent_file;
+
+		parent_file = g_file_get_parent (file);
+		if (G_LIKELY (parent_file))
+			uri = g_file_get_uri (parent_file);
+		g_object_unref (parent_file);
+	}
+
+	if (uri && !gtk_show_uri (screen, uri, timestamp, &error)) {
+		g_warning ("Couldn't show containing folder \"%s\": %s", uri,
+			   error->message);
+		g_error_free (error);
+	}
+
+	g_free (uri);
+}
+
+void
+eog_util_show_file_in_filemanager (GFile *file, GdkScreen *screen)
+{
+	GDBusProxy *proxy;
+	gboolean done = FALSE;
+
+	g_return_if_fail (file != NULL);
+
+	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+				G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
+				G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+				NULL, "org.freedesktop.FileManager1",
+				"/org/freedesktop/FileManager1",
+				"org.freedesktop.FileManager1",
+				NULL, NULL);
+
+	if (proxy) {
+		gchar *uri = g_file_get_uri (file);
+		gchar *startup_id;
+		GVariant *params, *result;
+		GVariantBuilder builder;
+
+		g_variant_builder_init (&builder,
+					G_VARIANT_TYPE ("as"));
+		g_variant_builder_add (&builder, "s", uri);
+
+		/* This seems to be the expected format, as other values
+		   cause the filemanager window not to get focus. */
+		startup_id = g_strdup_printf("_TIME%u",
+					     gtk_get_current_event_time());
+
+		/* params is floating! */
+		params = g_variant_new ("(ass)", &builder, startup_id);
+
+		g_free (startup_id);
+		g_variant_builder_clear (&builder);
+
+		/* Floating params-GVariant is consumed here */
+		result = g_dbus_proxy_call_sync (proxy, "ShowItems",
+						 params, G_DBUS_CALL_FLAGS_NONE,
+						 -1, NULL, NULL);
+
+		/* Receiving a non-NULL result counts as a successful call. */
+		if (G_LIKELY (result != NULL)) {
+			done = TRUE;
+			g_variant_unref (result);
+		}
+
+		g_free (uri);
+		g_object_unref (proxy);
+	}
+
+	/* Fallback to gtk_show_uri() if launch over DBus is not possible */
+	if (!done)
+		_eog_util_show_file_in_filemanager_fallback (file, screen);
+}
diff --git a/src/eog-util.h b/src/eog-util.h
index 37d7525..fe8d58e 100644
--- a/src/eog-util.h
+++ b/src/eog-util.h
@@ -65,6 +65,10 @@ char *  eog_util_filename_get_extension      (const char * filename);
 G_GNUC_INTERNAL
 gboolean eog_util_file_is_persistent (GFile *file);
 
+G_GNUC_INTERNAL
+void     eog_util_show_file_in_filemanager   (GFile *file,
+					      GdkScreen *screen);
+
 G_END_DECLS
 
 #endif /* __EOG_UTIL_H__ */
diff --git a/src/eog-window.c b/src/eog-window.c
index 8479c02..311f263 100644
--- a/src/eog-window.c
+++ b/src/eog-window.c
@@ -3103,49 +3103,21 @@ eog_window_cmd_save_as (GtkAction *action, gpointer user_data)
 static void
 eog_window_cmd_open_containing_folder (GtkAction *action, gpointer user_data)
 {
-	EogWindow *window = EOG_WINDOW (user_data);
 	EogWindowPrivate *priv;
-
-	GtkWidget *eog_window_widget;
-
 	GFile *file;
-	GFile *parent = NULL;
 
-	eog_window_widget = GTK_WIDGET (window);
-	priv = window->priv;
+	g_return_if_fail (EOG_IS_WINDOW (user_data));
+
+	priv = EOG_WINDOW (user_data)->priv;
 
 	g_return_if_fail (priv->image != NULL);
 
 	file = eog_image_get_file (priv->image);
 
-	if (file) {
-		parent = g_file_get_parent (file);
-		g_object_unref(file);
-	}
-
-	if (parent) {
-		char *parent_uri;
+	g_return_if_fail (file != NULL);
 
-		parent_uri = g_file_get_uri (parent);
-		if (parent_uri) {
-			GdkScreen *screen;
-			guint32 timestamp;
-			GError *error;
-
-			screen = gtk_widget_get_screen (eog_window_widget);
-			timestamp = gtk_get_current_event_time ();
-
-			error = NULL;
-			if (!gtk_show_uri (screen, parent_uri, timestamp, &error)) {
-				eog_debug_message (DEBUG_WINDOW, "Could not open the containing folder");
-				g_error_free (error);
-			}
-
-			g_free (parent_uri);
-		}
-
-		g_object_unref(parent);
-	}
+	eog_util_show_file_in_filemanager (file,
+				gtk_widget_get_screen (GTK_WIDGET (user_data)));
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]