[gedit/wip/gtkapp-actions: 8/10] GeditApp: read-stdin action



commit 5a17fa2c1f14f4f3a305ad335ccb9f76f1147d63
Author: SÃbastien Wilmet <swilmet gnome org>
Date:   Fri Aug 3 07:46:49 2012 +0200

    GeditApp: read-stdin action
    
    With the "-" command line option.
    
    It works on the local (primary) instance, but not on a remote instance.
    
    To add the support in GApplication, no new API is needed. It should
    handle correctly GVariants containing a handle.
    
    It's also possible to re-add the command line functions in GeditApp,
    just for reading stdin.

 gedit/gedit-app.c |   36 ++++++++++++++++++++++++++++++++++++
 gedit/gedit.c     |   17 +++++++++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 86e85d4..b6e3264 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -46,6 +46,10 @@
 #include <girepository.h>
 #endif
 
+#ifdef G_OS_UNIX
+#include <gio/gunixinputstream.h>
+#endif
+
 #include "gedit-app.h"
 #include "gedit-commands.h"
 #include "gedit-notebook.h"
@@ -863,6 +867,29 @@ activate_open_files_action (GSimpleAction *action,
 }
 
 static void
+activate_read_stdin_action (GSimpleAction *action,
+			    GVariant      *parameter,
+			    GeditApp      *app)
+{
+#ifdef G_OS_UNIX
+	/* It would nice, but it doesn't work for a remote instance.
+	 * Most probably not handled in GApplication.
+	 */
+	gint32 handle = g_variant_get_handle (parameter);
+	gedit_debug_message (DEBUG_APP, "read stdin from handle=%d", handle);
+
+	GInputStream *stdin_stream = g_unix_input_stream_new (handle, TRUE);
+
+	gedit_window_create_tab_from_stream (app->priv->active_window,
+					     stdin_stream,
+					     app->priv->encoding,
+					     app->priv->line_position,
+					     app->priv->column_position,
+					     TRUE);
+#endif
+}
+
+static void
 add_actions (GeditApp *app)
 {
 	GSimpleAction *action;
@@ -925,6 +952,15 @@ add_actions (GeditApp *app)
 			  "activate",
 			  G_CALLBACK (activate_open_files_action),
 			  app);
+
+	/* Read stdin */
+	action = g_simple_action_new ("read-stdin", G_VARIANT_TYPE_HANDLE);
+	g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
+
+	g_signal_connect (action,
+			  "activate",
+			  G_CALLBACK (activate_read_stdin_action),
+			  app);
 }
 
 static void
diff --git a/gedit/gedit.c b/gedit/gedit.c
index b7d4556..7ae3990 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -173,7 +173,8 @@ get_line_column_position (const gchar *arg,
 
 static void
 parse_remaining_args (GVariant **files_to_open,
-		      GVariant **document_positions)
+		      GVariant **document_positions,
+		      gboolean  *read_stdin)
 {
 	GVariantBuilder builder;
 	gchar **cur_arg;
@@ -211,6 +212,10 @@ parse_remaining_args (GVariant **files_to_open,
 							     line_position,
 							     column_position);
 		}
+		else if (g_strcmp0 (arg, "-") == 0)
+		{
+			*read_stdin = TRUE;
+		}
 		else
 		{
 			/* Transform the argument into an URI.
@@ -290,6 +295,7 @@ main (int argc, char *argv[])
 	GeditApp *app = NULL;
 	GVariant *files_to_open = NULL;
 	GVariant *document_positions = NULL;
+	gboolean read_stdin = FALSE;
 	GError *error = NULL;
 
 	g_type_init ();
@@ -298,7 +304,7 @@ main (int argc, char *argv[])
 	setup_i18n ();
 
 	parse_command_line (argc, argv);
-	parse_remaining_args (&files_to_open, &document_positions);
+	parse_remaining_args (&files_to_open, &document_positions, &read_stdin);
 
 	app = gedit_app_get_default ();
 
@@ -352,6 +358,13 @@ main (int argc, char *argv[])
 						files_to_open);
 	}
 
+	if (read_stdin)
+	{
+		g_action_group_activate_action (G_ACTION_GROUP (app),
+						"read-stdin",
+						g_variant_new_handle (0));
+	}
+
 	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]