/* vim: colorcolumn=80 ts=4 sw=4
 */
/*
 * tree.c
 *
 * Copyright © 2002 Sun Microsystems, Inc.
 * Copyright © 2021-2023 Logan Rathbone
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Original Authors: Glynn Foster <glynn.foster@sun.com>
 *          Jonathan Blanford <jrb@redhat.com>
 *          Kristian Rietveld <kris@gtk.org>
 */


#include "util.h"
#include "zenity.h"

#include <stdlib.h>
#include <string.h>

#include <config.h>

#define PRINT_HIDE_COLUMN_SEPARATOR ","

static GtkTreeView *tree_view;
static GSList *selected;
static char *separator;
static gboolean print_all_columns = FALSE;
static int *print_columns = NULL;
static int *hide_columns = NULL;
static GIOChannel *channel;

static int *zenity_tree_extract_column_indexes (char *indexes, int n_columns);
static gboolean zenity_tree_column_is_hidden (int column_index);
static void zenity_tree_dialog_response (GtkWidget *widget, char *rstr, gpointer data);
static void zenity_tree_row_activated (GtkTreeView *tree_view,
	GtkTreePath *tree_path, GtkTreeViewColumn *tree_col, gpointer data);

static gboolean
zenity_tree_dialog_untoggle (GtkTreeModel *model, GtkTreePath *path,
		GtkTreeIter *iter, gpointer data)
{
	GValue toggle_value = G_VALUE_INIT;

	gtk_tree_model_get_value (model, iter, 0, &toggle_value);

	if (g_value_get_boolean (&toggle_value))
		gtk_list_store_set (GTK_LIST_STORE (model), iter, 0, FALSE, -1);
	
	return FALSE;
}

static void
check_or_radio_label_activated_cb (GtkTreeView *tree_view,
		GtkTreePath *path,
		GtkTreeViewColumn *column,
		gpointer user_data)
{
	GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
	GtkTreeIter iter;
	gboolean value;

	/* Because this is a radio list, we should untoggle the previous toggle so
	 * that we only have one selection at any given time
	 */
	if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (model), "radio")) == 1)
	{
		gtk_tree_model_foreach (model, zenity_tree_dialog_untoggle, NULL);
	}

	gtk_tree_model_get_iter (model, &iter, path);
	gtk_tree_model_get (model, &iter, 0, &value, -1);

	value = !value;
	gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, value, -1);
}

static void
zenity_load_pixbuf (GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
	GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
	static GHashTable *pixbuf_cache = NULL;
	GError *error = NULL;
	GdkPixbuf *pixbuf;
	g_autofree char *str = NULL;

	gtk_tree_model_get (tree_model, iter, 0, &str, -1);

	if (! str)
		return;

	if (! pixbuf_cache)
	{
		pixbuf_cache = g_hash_table_new (g_str_hash, g_str_equal);
		g_assert (pixbuf_cache);
	}

	pixbuf = g_hash_table_lookup (pixbuf_cache, str);

	if (! pixbuf)
	{
		pixbuf = gdk_pixbuf_new_from_file (str, &error);

		if (! pixbuf)
			g_warning ("Failed to load '%s'", str);

		g_hash_table_insert (pixbuf_cache, g_strdup (str), pixbuf);
	}

	if (pixbuf)
		g_object_set (cell, "pixbuf", pixbuf, NULL);
}

static gboolean
zenity_tree_handle_stdin (GIOChannel *channel, GIOCondition condition,
		gpointer data)
{
	GtkTreeModel *model;
	static GtkTreeIter iter;
	static int column_count = 0;
	static int row_count = 0;
	static int n_columns;
	static gboolean editable;
	static gboolean toggles;
	static gboolean first_time = TRUE;
	GIOStatus status = G_IO_STATUS_NORMAL;

	/* Make sure global is properly defined */
	g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), FALSE);

	n_columns =
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_view), "n_columns"));
	editable =
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_view), "editable"));
	toggles =
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_view), "toggles"));

	model = gtk_tree_view_get_model (tree_view);

	if (first_time)
	{
		first_time = FALSE;
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
	}

	if ((condition & G_IO_IN) == G_IO_IN)
	{
		g_autoptr(GString) string = NULL;
		g_autoptr(GError) error = NULL;

		string = g_string_new (NULL);

		while ((g_io_channel_get_flags (channel) & G_IO_FLAG_IS_READABLE) !=
			G_IO_FLAG_IS_READABLE)
			;
		do {
			do {
				if (g_io_channel_get_flags (channel) & G_IO_FLAG_IS_READABLE) {
					status = g_io_channel_read_line_string (channel,
							string, NULL, &error);
				} else {
					return FALSE;
				}

				while (g_main_context_pending (NULL)) {
					g_main_context_iteration (NULL, FALSE);
				}
				/* FIXME: Find a better way to avoid 100% cpu utilization */
				g_usleep (10000);

			} while (status == G_IO_STATUS_AGAIN);

			if (status != G_IO_STATUS_NORMAL)
			{
				if (error) {
					g_warning ("%s: %s",
						__func__, error->message);
					error = NULL;
				}
				continue;
			}

			if (column_count == n_columns)
			{
				/* We're starting a new row */
				column_count = 0;
				row_count++;
				gtk_list_store_append (GTK_LIST_STORE (model), &iter);
			}

			if (toggles && column_count == 0)
			{
				if (strcmp (g_ascii_strdown (
								zenity_util_strip_newline (string->str), -1),
						"true") == 0)
				{
					gtk_list_store_set (GTK_LIST_STORE(model),
							&iter, column_count, TRUE, -1);
				}
				else {
					gtk_list_store_set (
						GTK_LIST_STORE (model), &iter, column_count, FALSE, -1);
				}
			}
			else
			{
				gtk_list_store_set (GTK_LIST_STORE (model),
					&iter,
					column_count,
					zenity_util_strip_newline (string->str),
					-1);
			}

			if (editable) {
				gtk_list_store_set (GTK_LIST_STORE(model),
						&iter, n_columns, TRUE, -1);
			}

			column_count++;

		} while ((g_io_channel_get_buffer_condition (channel) & G_IO_IN) ==
				G_IO_IN &&
			status != G_IO_STATUS_EOF);	/* !do while */
	}

	if ((condition & G_IO_IN) != G_IO_IN || status == G_IO_STATUS_EOF)
	{
		g_io_channel_shutdown (channel, TRUE, NULL);
		return FALSE;
	}
	return TRUE;
}

static void
zenity_tree_fill_entries_from_stdin (GtkTreeView *loc_tv, int n_columns,
	gboolean toggles, gboolean editable)
{
	/* No reason this should fail. Sanity check. */
	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
	g_assert (loc_tv == tree_view);

	g_object_set_data (
		G_OBJECT (tree_view), "n_columns", GINT_TO_POINTER (n_columns));
	g_object_set_data (
		G_OBJECT (tree_view), "toggles", GINT_TO_POINTER (toggles));
	g_object_set_data (
		G_OBJECT (tree_view), "editable", GINT_TO_POINTER (editable));

	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_tree_handle_stdin, NULL);
}

static void
zenity_tree_fill_entries (GtkTreeView *loc_tv, const char **args,
	int n_columns, gboolean toggles, gboolean editable)
{
	GtkTreeModel *model;
	GtkTreeIter iter;
	int i = 0;

	/* No reason this should fail. Sanity check. */
	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
	g_assert (loc_tv == tree_view);

	model = gtk_tree_view_get_model (tree_view);

	g_object_set_data (
		G_OBJECT (tree_view), "n_columns", GINT_TO_POINTER (n_columns));

	while (args[i] != NULL) {
		int j;

		gtk_list_store_append (GTK_LIST_STORE (model), &iter);

		for (j = 0; j < n_columns; j++) {

			if (toggles && j == 0) {
				if (strcmp (g_ascii_strdown ((gchar *) args[i + j], -1),
						"true") == 0)
					gtk_list_store_set (
						GTK_LIST_STORE (model), &iter, j, TRUE, -1);
				else
					gtk_list_store_set (
						GTK_LIST_STORE (model), &iter, j, FALSE, -1);
			} else
				gtk_list_store_set (
					GTK_LIST_STORE (model), &iter, j, args[i + j], -1);
		}

		if (editable) {
			gtk_list_store_set (GTK_LIST_STORE(model),
					&iter, n_columns, TRUE, -1);
		}
		i += n_columns;
	}
}

static gboolean
zenity_mid_search_func (GtkTreeModel *model, int column, const char *key,
	GtkTreeIter *iter, gpointer search_data)
{
	char *iter_string = NULL;

	gtk_tree_model_get (model, iter, column, &iter_string, -1);

	return !(g_strrstr (g_utf8_strdown (iter_string, -1),
				 g_utf8_strdown (key, -1)) != NULL);
}

static void
zenity_cell_edited_callback (GtkCellRendererText *cell,
	const char *path_string, const char *new_text, gpointer data)
{
	GtkTreeModel *model;
	g_autoptr(GtkTreePath) path = NULL;
	GtkTreeIter iter;
	int column;

	model = GTK_TREE_MODEL (data);
	path = gtk_tree_path_new_from_string (path_string);

	column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));
	gtk_tree_model_get_iter (model, &iter, path);

	gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);
}

void
zenity_tree (ZenityData *data, ZenityTreeData *tree_data)
{
	g_autoptr(GtkBuilder) builder = NULL;
	GtkWidget *dialog;
	GObject *text;
	GtkTreeViewColumn *column;
	GtkListStore *model;
	GType *column_types;
	GSList *tmp;
	gboolean first_column = FALSE;
	int i, column_index, n_columns;

	builder = zenity_util_load_ui_file ("zenity_tree_dialog", "zenity_tree_box", NULL);

	if (builder == NULL) {
		data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR);
		return;
	}

	separator = g_strcompress (tree_data->separator);

	n_columns = g_slist_length (tree_data->columns);

	if (tree_data->print_column)
	{
		if (strcmp (g_ascii_strdown (tree_data->print_column, -1), "all") == 0)
			print_all_columns = TRUE;
		else
			print_columns = zenity_tree_extract_column_indexes (
				tree_data->print_column, n_columns);
	}
	else
	{
		print_columns = g_new (gint, 2);
		print_columns[0] = (tree_data->radiobox || tree_data->checkbox ? 2 : 1);
		print_columns[1] = 0;
	}

	if (tree_data->hide_column) {
		hide_columns =
			zenity_tree_extract_column_indexes (tree_data->hide_column,
					n_columns);
	}

	if (n_columns == 0) {
		g_printerr (_("No column titles specified for List dialog.\n"));
		data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR);
		return;
	}

	if (tree_data->checkbox + tree_data->radiobox + tree_data->imagebox > 1)
	{
		g_printerr (_ ("You should use only one List dialog type.\n"));
		data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR);
		return;
	}

	dialog = GTK_WIDGET(gtk_builder_get_object (builder,
				"zenity_tree_dialog"));

	g_signal_connect (dialog, "response", G_CALLBACK(zenity_tree_dialog_response), data);

	if (data->dialog_title)
		gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title);

	if (data->modal)
		gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

	if (data->extra_label)
	{
		ZENITY_UTIL_ADD_EXTRA_LABELS (dialog)
	}

	if (data->ok_label) {
		ZENITY_UTIL_SETUP_OK_BUTTON_LABEL (dialog)
	}

	if (data->cancel_label) {
		ZENITY_UTIL_SETUP_CANCEL_BUTTON_LABEL (dialog)
	}

	text = gtk_builder_get_object (builder, "zenity_tree_text");

	if (tree_data->dialog_text)
		gtk_label_set_markup (GTK_LABEL(text),
				g_strcompress (tree_data->dialog_text));

	gtk_window_set_icon_name (GTK_WINDOW(dialog),
			"view-sort-ascending");

	if (data->width > -1 || data->height > -1) {
		gtk_window_set_default_size (GTK_WINDOW(dialog),
				data->width, data->height);
	}

	/* Define global tree_view. */
	tree_view = GTK_TREE_VIEW(gtk_builder_get_object (builder,
				"zenity_tree_view"));

	if (tree_data->radiobox || tree_data->checkbox)
	{
		gtk_tree_view_set_activate_on_single_click (GTK_TREE_VIEW(tree_view),
				TRUE);

		g_signal_connect (tree_view,
			"row-activated",
			G_CALLBACK (check_or_radio_label_activated_cb),
			data);
	}
	else
	{
		g_signal_connect (tree_view,
			"row-activated",
			G_CALLBACK (zenity_tree_row_activated),
			data);
	}

	/* Create an empty list store */
	model = g_object_new (GTK_TYPE_LIST_STORE, NULL);

	if (tree_data->editable)
		column_types = g_new (GType, n_columns + 1);
	else
		column_types = g_new (GType, n_columns);

	for (i = 0; i < n_columns; i++)
	{
		/* Have the limitation that the radioboxes and checkboxes are in the
		 * first column */
		if (i == 0 && (tree_data->checkbox || tree_data->radiobox))
			column_types[i] = G_TYPE_BOOLEAN;
		else
			column_types[i] = G_TYPE_STRING;
	}

	if (tree_data->editable)
		column_types[n_columns] = G_TYPE_BOOLEAN;

	if (tree_data->editable)
		gtk_list_store_set_column_types (model, n_columns + 1, column_types);
	else
		gtk_list_store_set_column_types (model, n_columns, column_types);

	gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL(model));

	if (tree_data->radiobox || tree_data->checkbox)
	{
		gtk_tree_selection_set_mode (
			gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
			GTK_SELECTION_SINGLE);
	}
	else
	{
		if (tree_data->multi)
			gtk_tree_selection_set_mode (
				gtk_tree_view_get_selection (tree_view),
				GTK_SELECTION_MULTIPLE);
		else
			gtk_tree_selection_set_mode (
				gtk_tree_view_get_selection (tree_view),
				GTK_SELECTION_SINGLE);
	}

	column_index = 0;

	for (tmp = tree_data->columns; tmp; tmp = tmp->next)
	{
		if (! first_column)
		{
			if (tree_data->checkbox || tree_data->radiobox)
			{
				GtkCellRenderer *cell_renderer;

				cell_renderer = gtk_cell_renderer_toggle_new ();

				if (tree_data->radiobox) {
					g_object_set (
						G_OBJECT (cell_renderer), "radio", TRUE, NULL);
					g_object_set_data (
						G_OBJECT (model), "radio", GINT_TO_POINTER (1));
				}

				column = gtk_tree_view_column_new_with_attributes (
					tmp->data, cell_renderer, "active", column_index, NULL);
			}
			else if (tree_data->imagebox)
			{
				GtkCellRenderer *cell_renderer =
					gtk_cell_renderer_pixbuf_new ();

				column = gtk_tree_view_column_new_with_attributes (
					tmp->data, cell_renderer, NULL);
				gtk_tree_view_column_set_cell_data_func (
					column, cell_renderer, zenity_load_pixbuf, NULL, NULL);
			}
			else
			{
				if (tree_data->editable)
				{
					GtkCellRenderer *cell_renderer;

					cell_renderer = gtk_cell_renderer_text_new ();
					g_signal_connect (cell_renderer, "edited",
						G_CALLBACK (zenity_cell_edited_callback),
						gtk_tree_view_get_model (tree_view));

					g_object_set_data (G_OBJECT (cell_renderer),
						"column",
						GINT_TO_POINTER (column_index));

					column =
						gtk_tree_view_column_new_with_attributes (tmp->data,
							cell_renderer,
							"text",
							column_index,
							"editable",
							n_columns,
							NULL);
				}
				else
				{
					column =
						gtk_tree_view_column_new_with_attributes (tmp->data,
							gtk_cell_renderer_text_new (),
							"text",
							column_index,
							NULL);
				}

				gtk_tree_view_column_set_sort_column_id (column, column_index);
				gtk_tree_view_column_set_resizable (column, TRUE);
			}
			if (zenity_tree_column_is_hidden (1))
				gtk_tree_view_column_set_visible (column, FALSE);

			first_column = TRUE;
		}
		else
		{
			if (tree_data->editable)
			{
				GtkCellRenderer *cell_renderer;

				cell_renderer = gtk_cell_renderer_text_new ();
				g_signal_connect (cell_renderer, "edited",
					G_CALLBACK (zenity_cell_edited_callback),
					gtk_tree_view_get_model (tree_view));

				g_object_set_data (G_OBJECT (cell_renderer),
					"column",
					GINT_TO_POINTER (column_index));

				column = gtk_tree_view_column_new_with_attributes (tmp->data,
					cell_renderer,
					"text",
					column_index,
					"editable",
					n_columns,
					NULL);
			}
			else
			{
				column = gtk_tree_view_column_new_with_attributes (tmp->data,
					gtk_cell_renderer_text_new (),
					"text",
					column_index,
					NULL);
			}

			gtk_tree_view_column_set_sort_column_id (column, column_index);
			gtk_tree_view_column_set_resizable (column, TRUE);

			if (zenity_tree_column_is_hidden (column_index + 1))
				gtk_tree_view_column_set_visible (column, FALSE);
		}

		gtk_tree_view_append_column (tree_view, column);
		column_index++;
	}

	if (tree_data->hide_header)
		gtk_tree_view_set_headers_visible (tree_view, FALSE);

	if (tree_data->radiobox || tree_data->checkbox)
	{
		if (tree_data->data && *tree_data->data) {
			zenity_tree_fill_entries (tree_view,
				tree_data->data,
				n_columns,
				TRUE,
				tree_data->editable);
		}
		else {
			zenity_tree_fill_entries_from_stdin (tree_view,
				n_columns,
				TRUE,
				tree_data->editable);
		}
	}
	else
	{
		if (tree_data->data && *tree_data->data) {
			zenity_tree_fill_entries (tree_view,
				tree_data->data,
				n_columns,
				FALSE,
				tree_data->editable);
		} else {
			zenity_tree_fill_entries_from_stdin (tree_view,
				n_columns,
				FALSE,
				tree_data->editable);
		}
	}

	zenity_util_show_dialog (dialog);

	if (tree_data->mid_search) {
		gtk_tree_view_set_search_equal_func (tree_view,
			(GtkTreeViewSearchEqualFunc) zenity_mid_search_func,
			model,
			NULL);
	}

	if (data->timeout_delay > 0) {
		g_timeout_add_seconds (data->timeout_delay,
			(GSourceFunc) zenity_util_timeout_handle,
			dialog);
	}

	zenity_util_gapp_main (GTK_WINDOW(dialog));
}

static void
zenity_tree_dialog_get_selected (GtkTreeModel *model, GtkTreePath *path_buf,
	GtkTreeIter *iter, gpointer user_data)
{
	GValue value = G_VALUE_INIT;
	int n_columns, i;

	n_columns =
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_view), "n_columns"));

	if (print_all_columns)
	{
		for (i = 0; i < n_columns; i++)
		{
			gtk_tree_model_get_value (model, iter, i, &value);

			selected = g_slist_append (selected, g_value_dup_string (&value));
			g_value_unset (&value);
		}
		return;
	}

	for (i = 0; print_columns[i] != 0; i++)
	{
		gtk_tree_model_get_value (model, iter, print_columns[i] - 1, &value);
		selected = g_slist_append (selected, g_value_dup_string (&value));

		g_value_unset (&value);
	}
}

static gboolean
zenity_tree_dialog_toggle_get_selected (GtkTreeModel *model, GtkTreePath *path,
	GtkTreeIter *iter, gpointer user_data)
{
	GValue toggle_value = G_VALUE_INIT;
	int n_columns, i;

	n_columns =
		GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_view), "n_columns"));

	gtk_tree_model_get_value (model, iter, 0, &toggle_value);

	if (g_value_get_boolean (&toggle_value))
	{
		GValue value = G_VALUE_INIT;

		if (print_all_columns)
		{
			for (i = 1; i < n_columns; i++)
			{
				gtk_tree_model_get_value (model, iter, i, &value);

				selected =
					g_slist_append (selected, g_value_dup_string (&value));
				g_value_unset (&value);
			}
			g_value_unset (&toggle_value);
			return FALSE;
		}

		for (i = 0; print_columns[i] != 0; i++)
		{
			gtk_tree_model_get_value (
				model, iter, print_columns[i] - 1, &value);

			selected = g_slist_append (selected, g_value_dup_string (&value));
			g_value_unset (&value);
		}
	}
	g_value_unset (&toggle_value);

	return FALSE;
}

static void
zenity_tree_dialog_output (void)
{
	GtkTreeSelection *selection;
	GtkTreeModel *model;

	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));

	model = gtk_tree_view_get_model (tree_view);

	if (gtk_tree_model_get_column_type (model, 0) == G_TYPE_BOOLEAN)
	{
		gtk_tree_model_foreach (model,
			(GtkTreeModelForeachFunc) zenity_tree_dialog_toggle_get_selected,
			NULL);
	}
	else
	{
		selection = gtk_tree_view_get_selection (tree_view);
		gtk_tree_selection_selected_foreach (selection,
			(GtkTreeSelectionForeachFunc) zenity_tree_dialog_get_selected,
			NULL);
	}


	for (GSList *tmp = selected; tmp != NULL; tmp = tmp->next)
	{
		if (tmp->next != NULL) {
			g_print ("%s%s", (char *)tmp->data, separator);
		} else {
			g_print ("%s\n", (char *)tmp->data);
		}
	}

	g_free (print_columns);
	g_free (hide_columns);
	g_free (separator);
	g_slist_free_full (g_steal_pointer (&selected), g_free);
}

static void
zenity_tree_dialog_response (GtkWidget *widget, char *rstr, gpointer data)
{
	ZenityData *zen_data = data;
	ZenityExitCode response = zenity_util_parse_dialog_response (rstr);

	switch (response)
	{
		case ZENITY_OK:
			zenity_tree_dialog_output ();
			zenity_util_exit_code_with_data (ZENITY_OK, zen_data);
			break;

		case ZENITY_CANCEL:
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
			break;

		case ZENITY_TIMEOUT:
			zenity_tree_dialog_output ();
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_TIMEOUT);
			break;

		default:
			if (zen_data->extra_label &&
				response < (int)g_strv_length (zen_data->extra_label))
			{
				printf ("%s\n", zen_data->extra_label[response]);
			}
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
			break;
	}
	if (channel != NULL &&
		g_io_channel_get_flags (channel) & G_IO_FLAG_IS_READABLE)
	{
		g_io_channel_shutdown (channel, TRUE, NULL);
	}

	zenity_util_gapp_quit (GTK_WINDOW(widget), zen_data);
}

static void
zenity_tree_row_activated (GtkTreeView *loc_tv, GtkTreePath *tree_path,
	GtkTreeViewColumn *tree_col, gpointer data)
{
	GtkWindow *parent;
	ZenityData *zen_data = data;

	/* No reason this should fail. Sanity check. */
	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
	g_assert (loc_tv == tree_view);

	zenity_tree_dialog_output ();
	zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);

	parent = GTK_WINDOW(gtk_widget_get_native (GTK_WIDGET(tree_view)));

	zenity_util_gapp_quit (parent, zen_data);
}

static gboolean
zenity_tree_column_is_hidden (int column_index)
{
	if (hide_columns != NULL)
	{
		for (int i = 0; hide_columns[i] != 0; i++)
		{
			if (hide_columns[i] == column_index)
				return TRUE;
		}
	}
	return FALSE;
}

static int *
zenity_tree_extract_column_indexes (char *indexes, int n_columns)
{
	g_auto(GStrv) tmp;
	int *result;
	int i, j, index;

	tmp = g_strsplit (indexes, PRINT_HIDE_COLUMN_SEPARATOR, 0);

	result = g_new (int, 1);

	for (j = i = 0; tmp[i] != NULL; i++)
	{
		index = atoi (tmp[i]);

		if (index > 0 && index <= n_columns)
		{
			result[j] = index;
			j++;
			result = g_renew (int, result, j + 1);
		}
	}
	result[j] = 0;

	return result;
}
