[gedit/wip/gtkapp: 6/17] GOption magic for both local and remote command line



commit e39786dcfa91dda5efd4b2dd4146847f51ec5a3c
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Jul 31 12:52:33 2012 +0200

    GOption magic for both local and remote command line

 gedit/gedit-app.c |  281 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 158 insertions(+), 123 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 205e28d..2c90170 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -94,6 +94,99 @@ struct _GeditAppPrivate
 
 static GeditApp *app_instance = NULL;
 
+static gboolean version = FALSE;
+static gboolean list_encodings = FALSE;
+static gchar *encoding_charset = NULL;
+static gboolean new_window = FALSE;
+static gboolean new_document = FALSE;
+static gchar *geometry = NULL;
+static gboolean wait = FALSE;
+static gboolean background = FALSE;
+static gboolean standalone = FALSE;
+static gchar **remaining_args = NULL;
+
+static const GOptionEntry options[] =
+{
+	/* Version */
+	{
+		"version", 'V', 0, G_OPTION_ARG_NONE, &version,
+		N_("Show the application's version"), NULL
+	},
+
+	/* 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
+	},
+
+	/* New instance */
+	{
+		"standalone", 's', 0, G_OPTION_ARG_NONE,
+		&standalone,
+		N_("Run gedit in standalone mode"),
+		NULL
+	},
+
+	/* collects file arguments */
+	{
+		G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY,
+		&remaining_args,
+		NULL,
+		N_("[FILE...] [+LINE[:COLUMN]]")
+	},
+
+	{NULL}
+};
+
 G_DEFINE_ABSTRACT_TYPE(GeditApp, gedit_app, GTK_TYPE_APPLICATION)
 
 static void
@@ -313,91 +406,8 @@ gedit_app_command_line (GApplication            *application,
 	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;
 	gint i;
 
-	const GOptionEntry options[] =
-	{
-		/* Version */
-		{
-			"version", 'V', 0, G_OPTION_ARG_NONE, &version,
-			N_("Show the application's version"), NULL
-		},
-
-		/* 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
-		},
-
-		/* collects file arguments */
-		{
-			G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY,
-			&remaining_args,
-			NULL,
-			N_("[FILE...] [+LINE[:COLUMN]]")
-		},
-
-		{NULL}
-	};
-
 	args = g_application_command_line_get_arguments (command_line, &argc);
 
 	/* We have to make an extra copy of the array, since g_option_context_parse()
@@ -427,26 +437,6 @@ gedit_app_command_line (GApplication            *application,
 		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
 	{
 		GSList *file_list = NULL;
@@ -471,8 +461,8 @@ gedit_app_command_line (GApplication            *application,
 					else
 					{
 						get_line_column_position (remaining_args[i] + 1,
-							                  &line_position,
-							                  &column_position);
+						                          &line_position,
+						                          &column_position);
 					}
 				}
 				else
@@ -520,10 +510,10 @@ gedit_app_command_line (GApplication            *application,
 
 			gedit_debug_message (DEBUG_APP, "Load files");
 			loaded = _gedit_cmd_load_files_from_prompt (window,
-				                                    file_list,
-				                                    encoding,
-				                                    line_position,
-				                                    column_position);
+			                                            file_list,
+			                                            encoding,
+			                                            line_position,
+			                                            column_position);
 
 			doc_created = loaded != NULL;
 			g_slist_free (loaded);
@@ -543,6 +533,18 @@ gedit_app_command_line (GApplication            *application,
 	g_strfreev (remaining_args);
 	g_free (geometry);
 
+	/* reset values for the next call */
+	version = FALSE;
+	list_encodings = FALSE;
+	encoding_charset = NULL;
+	new_window = FALSE;
+	new_document = FALSE;
+	geometry = NULL;
+	wait = FALSE;
+	background = FALSE;
+	standalone = FALSE;
+	remaining_args = NULL;
+
 	return 0;
 }
 
@@ -551,31 +553,64 @@ gedit_app_local_command_line (GApplication   *application,
                               gchar        ***arguments,
                               gint           *exit_status)
 {
-	gint i, j;
+	GOptionContext *context;
+	GError *error = NULL;
 	gchar **argv;
+	gint argc;
 
 	argv = *arguments;
 	g_message ("local command line");
+	*exit_status = 0;
+
+	/* 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));
 
-	i = 1;
-	while (argv[i])
+#ifdef ENABLE_INTROSPECTION
+	g_option_context_add_group (context, g_irepository_get_option_group ());
+#endif
+
+	if (!g_option_context_parse (context, &argc, &argv, &error))
 	{
-		if (g_str_equal (argv[i], "-s"))
+		g_error (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
+		         error->message, argv[0]);
+
+		g_error_free (error);
+		*exit_status = 1;
+	}
+	else if (version)
+	{
+		g_print ("%s - Version %s\n", g_get_application_name (), VERSION);
+
+		return TRUE;
+	}
+	else if (list_encodings)
+	{
+		gint i = 0;
+		const GeditEncoding *enc;
+
+		while ((enc = gedit_encoding_get_from_index (i)) != NULL)
 		{
-			GApplicationFlags old_flags;
+			g_print ("%s\n", gedit_encoding_get_charset (enc));
 
-			old_flags = g_application_get_flags (application);
-			g_application_set_flags (application, old_flags | G_APPLICATION_NON_UNIQUE);
-			g_free (argv[i]);
-			for (j = i; argv[j]; j++)
-			{
-				argv[j] = argv[j + 1];
-			}
+			++i;
 		}
-		i++;
+
+		return TRUE;
 	}
+	else if (standalone)
+	{
+		GApplicationFlags old_flags;
 
-	*exit_status = 0;
+		old_flags = g_application_get_flags (application);
+		g_application_set_flags (application, old_flags | G_APPLICATION_NON_UNIQUE);
+	}
+
+	g_option_context_free (context);
+	g_free (encoding_charset);
+	g_strfreev (remaining_args);
+	g_free (geometry);
 
 	return G_APPLICATION_CLASS (gedit_app_parent_class)->local_command_line (application, arguments, exit_status);
 }



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