From 2a297420c2ebd72089f74a59f3bc93b0313510ec Mon Sep 17 00:00:00 2001 From: Glynn Foster Date: Mon, 26 Jan 2004 03:50:50 +0000 Subject: [PATCH] Handle stdin. Fixes #132517. 2004-01-26 Glynn Foster * src/text.c: Handle stdin. Fixes #132517. --- ChangeLog | 4 +++ src/text.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/ChangeLog b/ChangeLog index 4712120..d6a6a60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-01-26 Glynn Foster + + * src/text.c: Handle stdin. Fixes #132517. + 2004-01-20 Åsmund Skjæveland * configure.in: Added 'nn' to ALL_LINGUAS. diff --git a/src/text.c b/src/text.c index 0cf992c..c4a49f8 100644 --- a/src/text.c +++ b/src/text.c @@ -29,6 +29,79 @@ static ZenityTextData *zen_text_data; static void zenity_text_dialog_response (GtkWidget *widget, int response, gpointer data); +static gboolean +zenity_text_handle_stdin (GIOChannel *channel, + GIOCondition condition, + gpointer data) +{ + static GtkTextBuffer *buffer; + gchar buf[1024]; + + static GtkTextIter iter, end; + static gboolean first_time = FALSE; + gchar *str; + gsize len; + + buffer = GTK_TEXT_BUFFER (data); + + if ((condition == G_IO_IN) || (condition == G_IO_IN + G_IO_HUP)) { + GError *error = NULL; + + while (channel->is_readable != TRUE) + ; + do { + gint status; + + do { + status = g_io_channel_read_chars (channel, buf, 1024, &len, &error); + + while (gtk_events_pending ()) + gtk_main_iteration (); + + } while (status == G_IO_STATUS_AGAIN); + + if (status != G_IO_STATUS_NORMAL) { + if (error) { + g_warning ("zenity_text_handle_stdin () : %s", error->message); + g_error_free (error); + error = NULL; + } + continue; + } + + if (len > 0) { + GtkTextIter end; + gchar *utftext; + gsize localelen; + gsize utflen; + + gtk_text_buffer_get_end_iter (buffer, &end); + utftext = g_convert_with_fallback (buf, len, "UTF-8", "ISO-8859-1", NULL, &localelen, &utflen, NULL); + gtk_text_buffer_insert (buffer, &end, utftext, utflen); + g_free (utftext); + } + + } while (g_io_channel_get_buffer_condition (channel) == G_IO_IN); + } + + if (condition != G_IO_IN) { + g_io_channel_shutdown (channel, TRUE, NULL); + return FALSE; + } + return TRUE; +} + +static void +zenity_text_fill_entries_from_stdin (GtkTextBuffer *text_buffer) +{ + GIOChannel *channel; + + channel = g_io_channel_unix_new (0); + g_io_channel_set_encoding (channel, NULL, NULL); + g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); + g_io_add_watch (channel, G_IO_IN | G_IO_HUP, zenity_text_handle_stdin, text_buffer); +} + void zenity_text (ZenityData *data, ZenityTextData *text_data) { @@ -70,6 +143,8 @@ zenity_text (ZenityData *data, ZenityTextData *text_data) if (text_data->uri) zenity_util_fill_file_buffer (text_buffer, text_data->uri); + else + zenity_text_fill_entries_from_stdin (GTK_TEXT_BUFFER (text_buffer)); if (text_data->editable) zen_text_data->buffer = text_buffer;