[gedit/wip/gtkapp: 175/197] Handle command line options into the app directly



commit 15ddd544aa5eb428d700d35264801b10b98496ea
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Mon Jul 30 12:33:48 2012 +0200

    Handle command line options into the app directly

 gedit/Makefile.am          |    2 -
 gedit/gedit-app.c          |  256 ++++++++++++++++++++++++--
 gedit/gedit-command-line.c |  447 --------------------------------------------
 gedit/gedit-command-line.h |   86 ---------
 4 files changed, 244 insertions(+), 547 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index c4124ff..052fc5c 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -102,7 +102,6 @@ NOINST_H_FILES =			\
 	gedit-cell-renderer-button.h	\
 	gedit-close-button.h		\
 	gedit-close-confirmation-dialog.h \
-	gedit-command-line.h		\
 	gedit-dirs.h			\
 	gedit-document-input-stream.h	\
 	gedit-document-loader.h		\
@@ -175,7 +174,6 @@ libgedit_c_files =			\
 	gedit-cell-renderer-button.c	\
 	gedit-close-button.c		\
 	gedit-close-confirmation-dialog.c \
-	gedit-command-line.c		\
 	gedit-commands-documents.c	\
 	gedit-commands-edit.c		\
 	gedit-commands-file.c		\
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index ffe0a29..a4f65cf 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -34,11 +34,16 @@
 
 #include <string.h>
 #include <unistd.h>
+#include <stdlib.h>
 
 #include <glib/gi18n.h>
 #include <libpeas/peas-extension-set.h>
 #include <gtksourceview/gtksourcestyleschememanager.h>
 
+#ifdef ENABLE_INTROSPECTION
+#include <girepository.h>
+#endif
+
 #include "gedit-app.h"
 #include "gedit-commands.h"
 #include "gedit-notebook.h"
@@ -49,7 +54,6 @@
 #include "gedit-settings.h"
 #include "gedit-app-activatable.h"
 #include "gedit-plugins-engine.h"
-#include "gedit-command-line.h"
 
 #ifdef OS_OSX
 #include "gedit-app-osx.h"
@@ -275,27 +279,254 @@ gedit_app_activate (GApplication *application)
 	g_message ("activate");
 }
 
-static gint
-gedit_app_command_line (GApplication            *application,
-                        GApplicationCommandLine *cmdline)
+static void
+get_line_column_position (const gchar      *arg,
+                          gint             *line,
+                          gint             *column)
 {
-	GeditCommandLine *command_line;
+	gchar **split;
+
+	split = g_strsplit (arg, ":", 2);
+
+	if (split != NULL)
+	{
+		if (split[0] != NULL)
+		{
+			*line = atoi (split[0]);
+		}
+
+		if (split[1] != NULL)
+		{
+			*column = atoi (split[1]);
+		}
+	}
+
+	g_strfreev (split);
+}
+
+static gboolean
+command_line_handler (gpointer data)
+{
+	GApplicationCommandLine *command_line = data;
+	GOptionContext *context;
+	GError *error = NULL;
+	gchar **args;
 	gchar **argv;
 	gint argc;
+	gboolean version = FALSE;
+	gboolean list_encodings = FALSE;
+	gchar *encoding_charset = NULL;
+	gboolean new_window = FALSE;
+	gboolean new_document = FALSE;
+	gchar *geometry = NULL;
+	gboolean wait = FALSE;
+	gboolean background = FALSE;
+	gchar **remaining_args = NULL;
+	GSList *file_list = NULL;
+	gint line_position = 0;
+	gint column_position = 0;
+	const GeditEncoding *encoding;
+	gint i;
+
+	const GOptionEntry options[] =
+	{
+		/* Version */
+		{
+			"version", 'V', 0, G_OPTION_ARG_NONE, &version,
+			N_("Show the application's version"), NULL
+		},
 
-	g_message ("command line");
+		/* List available encodings */
+		{
+			"list-encodings", '\0', 0, G_OPTION_ARG_NONE, &list_encodings,
+			N_("Display list of possible values for the encoding option"),
+			NULL
+		},
+
+		/* Encoding */
+		{
+			"encoding", '\0', 0, G_OPTION_ARG_STRING,
+			&encoding_charset,
+			N_("Set the character encoding to be used to open the files listed on the command line"),
+			N_("ENCODING")
+		},
+
+		/* Open a new window */
+		{
+			"new-window", '\0', 0, G_OPTION_ARG_NONE,
+			&new_window,
+			N_("Create a new top-level window in an existing instance of gedit"),
+			NULL
+		},
+
+		/* Create a new empty document */
+		{
+			"new-document", '\0', 0, G_OPTION_ARG_NONE,
+			&new_document,
+			N_("Create a new document in an existing instance of gedit"),
+			NULL
+		},
+
+		/* Window geometry */
+		{
+			"geometry", 'g', 0, G_OPTION_ARG_STRING,
+			&geometry,
+			N_("Set the size and position of the window (WIDTHxHEIGHT+X+Y)"),
+			N_("GEOMETRY")
+		},
+
+		/* Wait for closing documents */
+		{
+			"wait", 'w', 0, G_OPTION_ARG_NONE,
+			&wait,
+			N_("Open files and block process until files are closed"),
+			NULL
+		},
+
+		/* Run in the background */
+		{
+			"background", 'b', 0, G_OPTION_ARG_NONE,
+			&background,
+			N_("Run gedit in the background"),
+			NULL
+		},
 
-	command_line = gedit_command_line_get_default ();
+		/* collects file arguments */
+		{
+			G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY,
+			&remaining_args,
+			NULL,
+			N_("[FILE...] [+LINE[:COLUMN]]")
+		},
+
+		{NULL}
+	};
 
-	argv = g_application_command_line_get_arguments (cmdline, &argc);
+	args = g_application_command_line_get_arguments (command_line, &argc);
 
-	if (!gedit_command_line_parse (command_line, &argc, &argv))
+	/* We have to make an extra copy of the array, since g_option_context_parse()
+	 * assumes that it can remove strings from the array without freeing them.
+	 */
+	argv = g_new (gchar*, argc + 1);
+	for (i = 0; i <= argc; i++)
 	{
-		g_strfreev (argv);
-		return 1;
+		argv[i] = args[i];
 	}
 
-	g_strfreev (argv);
+	/* Setup command line options */
+	context = g_option_context_new (_("- Edit text files"));
+	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
+	g_option_context_add_group (context, gtk_get_option_group (TRUE));
+
+#ifdef ENABLE_INTROSPECTION
+	g_option_context_add_group (context, g_irepository_get_option_group ());
+#endif
+
+	if (!g_option_context_parse (context, &argc, &argv, &error))
+	{
+		g_application_command_line_print (command_line,
+		                                  _("%s\nRun '%s --help' to see a full list of available command line options.\n"),
+		                                  error->message, argv[0]);
+
+		g_error_free (error);
+		g_application_command_line_set_exit_status (command_line, 1);
+	}
+	else if (version)
+	{
+		g_application_command_line_print (command_line,
+		                                  "%s - Version %s",
+		                                  g_get_application_name (), VERSION);
+	}
+	else if (list_encodings)
+	{
+		gint e = 0;
+		const GeditEncoding *enc;
+
+		while ((enc = gedit_encoding_get_from_index (e)) != NULL)
+		{
+			g_application_command_line_print (command_line,
+			                                  "%s",
+			                                  gedit_encoding_get_charset (enc));
+
+			++e;
+		}
+	}
+	else
+	{
+		if (remaining_args)
+		{
+			for (i = 0; remaining_args[i]; i++)
+			{
+				if (*remaining_args[i] == '+')
+				{
+					if (*(remaining_args[i] + 1) == '\0')
+					{
+						/* goto the last line of the document */
+						line_position = G_MAXINT;
+						column_position = 0;
+					}
+					else
+					{
+						get_line_column_position (remaining_args[i] + 1,
+							                  &line_position,
+							                  &column_position);
+					}
+				}
+				else
+				{
+					GFile *file;
+
+					file = g_file_new_for_commandline_arg (remaining_args[i]);
+					file_list = g_slist_prepend (file_list, file);
+				}
+			}
+
+			file_list = g_slist_reverse (file_list);
+		}
+
+		/* Parse encoding */
+		if (encoding_charset)
+		{
+			encoding = gedit_encoding_get_from_charset (encoding_charset);
+
+			if (encoding == NULL)
+			{
+				g_application_command_line_print (command_line,
+				                                  _("%s: invalid encoding."),
+				                                  encoding_charset);
+			}
+
+			g_free (encoding_charset);
+		}
+	}
+
+	g_option_context_free (context);
+	g_free (encoding_charset);
+	g_strfreev (remaining_args);
+	g_free (geometry);
+	g_slist_free_full (file_list, g_object_unref);
+
+	/* we are done handling this commandline */
+	g_object_unref (command_line);
+
+	return G_SOURCE_REMOVE;
+}
+
+static gint
+gedit_app_command_line (GApplication            *application,
+                        GApplicationCommandLine *command_line)
+{
+	g_message ("command line");
+
+	/* keep the application running until we are done with this commandline */
+	g_application_hold (application);
+
+	g_object_set_data_full (G_OBJECT (command_line),
+	                        "application", application,
+	                        (GDestroyNotify)g_application_release);
+
+	g_object_ref (command_line);
+	g_idle_add (command_line_handler, command_line);
 
 	return 0;
 }
@@ -326,6 +557,7 @@ gedit_app_local_command_line (GApplication   *application,
 				argv[j] = argv[j + 1];
 			}
 		}
+		i++;
 	}
 
 	*exit_status = 0;



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