[nautilus] application: support an explicit --select command line option



commit dc7e851ae4a223522c3158dd4134a3b313283596
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Jan 2 18:30:59 2013 +0100

    application: support an explicit --select command line option
    
    When invoked this way, just route a request to
    org.freedesktop.FileManager1.ShowItems(), which does exactly what we
    want.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=675563

 src/nautilus-application.c |   73 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 69 insertions(+), 4 deletions(-)
---
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index 00db5c6..780b04d 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -1008,6 +1008,7 @@ do_cmdline_sanity_checks (NautilusApplication *self,
 			  gboolean perform_self_check,
 			  gboolean version,
 			  gboolean kill_shell,
+			  gboolean select_uris,
 			  gchar **remaining)
 {
 	gboolean retval = FALSE;
@@ -1031,6 +1032,12 @@ do_cmdline_sanity_checks (NautilusApplication *self,
 		goto out;
 	}
 
+	if (select_uris && remaining == NULL) {
+		g_printerr ("%s\n",
+			    _("--select must be used with at least an URI."));
+		goto out;
+	}
+
 	retval = TRUE;
 
  out:
@@ -1059,6 +1066,55 @@ do_perform_self_checks (gint *exit_status)
 	*exit_status = EXIT_SUCCESS;
 }
 
+static void
+select_items_ready_cb (GObject *source,
+		       GAsyncResult *res,
+		       gpointer user_data)
+{
+	GDBusConnection *connection = G_DBUS_CONNECTION (source);
+	NautilusApplication *self = user_data;
+	GError *error = NULL;
+
+	g_dbus_connection_call_finish (connection, res, &error);
+
+	if (error != NULL) {
+		g_warning ("Unable to select specified URIs %s\n", error->message);
+		g_error_free (error);
+
+		/* open default location instead */
+		g_application_open (G_APPLICATION (self), NULL, 0, "");
+	}
+}
+
+static void
+nautilus_application_select (NautilusApplication *self,
+			     GFile **files,
+			     gint len)
+{
+	GDBusConnection *connection = g_application_get_dbus_connection (G_APPLICATION (self));
+	GVariantBuilder builder;
+	gint idx;
+	gchar *uri;
+
+	g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
+	for (idx = 0; idx < len; idx++) {
+		uri = g_file_get_uri (files[idx]);
+		g_variant_builder_add (&builder, "s", uri);
+		g_free (uri);
+	}
+
+	g_dbus_connection_call (connection,
+				NAUTILUS_FDO_DBUS_NAME,
+				NAUTILUS_FDO_DBUS_PATH,
+				NAUTILUS_FDO_DBUS_IFACE,
+				"ShowItems",
+				g_variant_new ("(ass)", &builder, ""), NULL,
+				G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL,
+				select_items_ready_cb, self);
+
+	g_variant_builder_clear (&builder);
+}
+
 static gboolean
 nautilus_application_local_command_line (GApplication *application,
 					 gchar ***arguments,
@@ -1069,6 +1125,7 @@ nautilus_application_local_command_line (GApplication *application,
 	gboolean browser = FALSE;
 	gboolean kill_shell = FALSE;
 	gboolean no_default_window = FALSE;
+	gboolean select_uris = FALSE;
 	gchar **remaining = NULL;
 	NautilusApplication *self = NAUTILUS_APPLICATION (application);
 
@@ -1090,6 +1147,8 @@ nautilus_application_local_command_line (GApplication *application,
 		  N_("Do not manage the desktop (ignore the preference set in the preferences dialog)."), NULL },
 		{ "quit", 'q', 0, G_OPTION_ARG_NONE, &kill_shell, 
 		  N_("Quit Nautilus."), NULL },
+		{ "select", 's', 0, G_OPTION_ARG_NONE, &select_uris,
+		  N_("Select specified URI in parent folder."), NULL },
 		{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining, NULL,  N_("[URI...]") },
 
 		{ NULL }
@@ -1126,7 +1185,7 @@ nautilus_application_local_command_line (GApplication *application,
 	}
 
 	if (!do_cmdline_sanity_checks (self, perform_self_check,
-				       version, kill_shell, remaining)) {
+				       version, kill_shell, select_uris, remaining)) {
 		*exit_status = EXIT_FAILURE;
 		goto out;
 	}
@@ -1184,7 +1243,7 @@ nautilus_application_local_command_line (GApplication *application,
 		g_strfreev (remaining);
 	}
 
-	if (files == NULL && !no_default_window) {
+	if (files == NULL && !no_default_window && !select_uris) {
 		files = g_malloc0 (2 * sizeof (GFile *));
 		len = 1;
 
@@ -1192,8 +1251,14 @@ nautilus_application_local_command_line (GApplication *application,
 		files[1] = NULL;
 	}
 
-	/* Invoke "Open" to create new windows */
-	if (len > 0) {
+	if (len == 0) {
+		goto out;
+	}
+
+	if (select_uris) {
+		nautilus_application_select (self, files, len);
+	} else {
+		/* Invoke "Open" to create new windows */
 		g_application_open (application, files, len, "");
 	}
 



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