[gedit/wip/gtkapp-actions: 7/10] GeditApp: add open-files action and active it in main()



commit ed79931dddbfdb2caed3cec65188794e34d656ed
Author: SÃbastien Wilmet <swilmet gnome org>
Date:   Fri Aug 3 07:45:59 2012 +0200

    GeditApp: add open-files action and active it in main()

 gedit/gedit-app.c |   51 +++++++++++++++++++++++++++++++
 gedit/gedit.c     |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 0 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 2c25dd2..86e85d4 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -821,6 +821,48 @@ activate_set_document_positions_action (GSimpleAction *action,
 }
 
 static void
+activate_open_files_action (GSimpleAction *action,
+			    GVariant      *parameter,
+			    GeditApp      *app)
+{
+	const gchar **uri_list = NULL;
+	const gchar **cur_uri = NULL;
+	GSList *file_list = NULL;
+
+	uri_list = g_variant_get_strv (parameter, NULL);
+
+	if (uri_list == NULL)
+	{
+		return;
+	}
+
+	for (cur_uri = uri_list; *cur_uri != NULL; cur_uri++)
+	{
+		GFile *file = g_file_new_for_uri (*cur_uri);
+		file_list = g_slist_prepend (file_list, file);
+	}
+
+	file_list = g_slist_reverse (file_list);
+
+	if (file_list != NULL)
+	{
+		GSList *loaded;
+
+		gedit_debug_message (DEBUG_APP, "Load files");
+		loaded = _gedit_cmd_load_files_from_prompt (app->priv->active_window,
+		                                            file_list,
+		                                            app->priv->encoding,
+		                                            app->priv->line_position,
+		                                            app->priv->column_position);
+
+		g_slist_free (loaded);
+	}
+
+	g_free (uri_list);
+	g_slist_free_full (file_list, g_object_unref);
+}
+
+static void
 add_actions (GeditApp *app)
 {
 	GSimpleAction *action;
@@ -874,6 +916,15 @@ add_actions (GeditApp *app)
 			  "activate",
 			  G_CALLBACK (activate_set_document_positions_action),
 			  app);
+
+	/* Open files */
+	action = g_simple_action_new ("open-files", G_VARIANT_TYPE_STRING_ARRAY);
+	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
+
+	g_signal_connect (action,
+			  "activate",
+			  G_CALLBACK (activate_open_files_action),
+			  app);
 }
 
 static void
diff --git a/gedit/gedit.c b/gedit/gedit.c
index feb697b..b7d4556 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -172,6 +172,75 @@ get_line_column_position (const gchar *arg,
 }
 
 static void
+parse_remaining_args (GVariant **files_to_open,
+		      GVariant **document_positions)
+{
+	GVariantBuilder builder;
+	gchar **cur_arg;
+
+	if (remaining_args == NULL)
+	{
+		return;
+	}
+
+	/* The builder will contain the list of URIs */
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
+
+	for (cur_arg = remaining_args; *cur_arg != NULL; cur_arg++)
+	{
+		gchar *arg = *cur_arg;
+
+		if (arg[0] == '+')
+		{
+			gint line_position = 0;
+			gint column_position = 0;
+
+			if (arg[1] == '\0')
+			{
+				/* goto the last line of the document */
+				line_position = G_MAXINT;
+			}
+			else
+			{
+				get_line_column_position (arg + 1,
+							  &line_position,
+							  &column_position);
+			}
+
+			*document_positions = g_variant_new ("(ii)",
+							     line_position,
+							     column_position);
+		}
+		else
+		{
+			/* Transform the argument into an URI.
+			 * If we call g_file_new_for_commandline_arg() in
+			 * GeditApp, the relative path resolution can be wrong
+			 * because the current working directory can be
+			 * different for the primary instance.
+			 */
+			GFile *file = g_file_new_for_commandline_arg (arg);
+			gchar *uri = g_file_get_uri (file);
+
+			g_variant_builder_add (&builder, "s", uri);
+
+			g_object_unref (file);
+			g_free (uri);
+		}
+	}
+
+	*files_to_open = g_variant_builder_end (&builder);
+
+	if (g_variant_n_children (*files_to_open) == 0)
+	{
+		g_variant_unref (*files_to_open);
+		*files_to_open = NULL;
+	}
+
+	g_strfreev (remaining_args);
+}
+
+static void
 parse_command_line (int argc, char **argv)
 {
 	GOptionContext *context;
@@ -219,6 +288,8 @@ int
 main (int argc, char *argv[])
 {
 	GeditApp *app = NULL;
+	GVariant *files_to_open = NULL;
+	GVariant *document_positions = NULL;
 	GError *error = NULL;
 
 	g_type_init ();
@@ -227,6 +298,7 @@ main (int argc, char *argv[])
 	setup_i18n ();
 
 	parse_command_line (argc, argv);
+	parse_remaining_args (&files_to_open, &document_positions);
 
 	app = gedit_app_get_default ();
 
@@ -266,6 +338,20 @@ main (int argc, char *argv[])
 						g_variant_new_string (geometry));
 	}
 
+	if (document_positions != NULL)
+	{
+		g_action_group_activate_action (G_ACTION_GROUP (app),
+						"set-document-positions",
+						document_positions);
+	}
+
+	if (files_to_open != NULL)
+	{
+		g_action_group_activate_action (G_ACTION_GROUP (app),
+						"open-files",
+						files_to_open);
+	}
+
 	if (new_document)
 	{
 		g_action_group_activate_action (G_ACTION_GROUP (app),



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