<commit_before>/*
 * forms.c
 *
 * Copyright (C) 2010 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: <NAME> <<EMAIL>>
 */

#include "config.h"
#include "util.h"
#include "zenity.h"
#include <string.h>

static ZenityData *zen_data;
static GSList *selected;
static void zenity_forms_dialog_response (
	GtkWidget *widget, int response, gpointer data);

static void
zenity_forms_dialog_get_selected (GtkTreeModel *model, GtkTreePath *path_buf,
	GtkTreeIter *iter, GtkTreeView *tree_view) {
	gint n_columns = 0;
	gint i = 0;
	GValue value = {
		0,
	};

	n_columns = gtk_tree_model_get_n_columns (model);
	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);
	}
}

static GtkWidget *
zenity_forms_create_and_fill_combo (
	ZenityFormsData *forms_data, int combo_number) {
	GtkListStore *list_store;
	GtkWidget *combo_box;
	GtkCellRenderer *renderer;
	gchar *combo_values;

	list_store = gtk_list_store_new (1, G_TYPE_STRING);

	if (forms_data->combo_values) {
		combo_values =
			g_slist_nth_data (forms_data->combo_values, combo_number);
		if (combo_values) {
			gchar **row_values = g_strsplit_set (combo_values, "|", -1);
			if (row_values) {
				gint i = 0;
				GtkTreeIter iter;
				gchar *row = row_values[i];

				while (row != NULL) {
					gtk_list_store_append (list_store, &iter);
					gtk_list_store_set (list_store, &iter, 0, row, -1);
					row = row_values[++i];
				}
				g_strfreev (row_values);
			}
			g_free (combo_values);
		}
	}

	combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store));
	g_object_unref (G_OBJECT (list_store));

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
	gtk_cell_layout_set_attributes (
		GTK_CELL_LAYOUT (combo_box), renderer, "text", 0, NULL);

	return combo_box;
}

static GtkWidget *
zenity_forms_create_and_fill_list (
	ZenityFormsData *forms_data, int list_number, gchar *header) {
	GtkListStore *list_store;
	GtkWidget *tree_view;
	GtkWidget *scrolled_window;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GType *column_types = NULL;
	gchar *list_values;
	gchar *column_values;

	gint i = 0;
	/* If no column names available, default is one */
	gint n_columns = 1;
	gint column_index = 0;

	tree_view = gtk_tree_view_new ();

	if (forms_data->column_values) {
		column_values =
			g_slist_nth_data (forms_data->column_values, list_number);
		if (column_values) {
			gchar **values = g_strsplit_set (column_values, "|", -1);
			if (values) {
				n_columns = g_strv_length (values);
				column_types = g_new (GType, n_columns);
				for (i = 0; i < n_columns; i++)
					column_types[i] = G_TYPE_STRING;

				for (i = 0; i < n_columns; i++) {
					gchar *column_name = values[i];
					renderer = gtk_cell_renderer_text_new ();
					column = gtk_tree_view_column_new_with_attributes (
						column_name, renderer, "text", column_index, NULL);
					gtk_tree_view_append_column (
						GTK_TREE_VIEW (tree_view), column);
					column_index++;
				}
			}
		}
	}

	list_store = g_object_new (GTK_TYPE_LIST_STORE, NULL);

	gtk_list_store_set_column_types (list_store, n_columns, column_types);

	if (forms_data->list_values) {
		list_values = g_slist_nth_data (forms_data->list_values, list_number);
		if (list_values) {
			gchar **row_values = g_strsplit_set (list_values, "|", -1);
			if (row_values) {
				GtkTreeIter iter;
				gchar *row = row_values[0];
				gint position = -1;
				i = 0;

				while (row != NULL) {<commit_msg>src/forms.c: Fix segfault on multiple lists without values<commit_after>/*
 * forms.c
 *
 * Copyright (C) 2010 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: <NAME> <<EMAIL>>
 */

#include "config.h"
#include "util.h"
#include "zenity.h"
#include <string.h>

static ZenityData *zen_data;
static GSList *selected;
static void zenity_forms_dialog_response (
	GtkWidget *widget, int response, gpointer data);

static void
zenity_forms_dialog_get_selected (GtkTreeModel *model, GtkTreePath *path_buf,
	GtkTreeIter *iter, GtkTreeView *tree_view) {
	gint n_columns = 0;
	gint i = 0;
	GValue value = {
		0,
	};

	n_columns = gtk_tree_model_get_n_columns (model);
	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);
	}
}

static GtkWidget *
zenity_forms_create_and_fill_combo (
	ZenityFormsData *forms_data, int combo_number) {
	GtkListStore *list_store;
	GtkWidget *combo_box;
	GtkCellRenderer *renderer;
	gchar *combo_values;

	list_store = gtk_list_store_new (1, G_TYPE_STRING);

	if (forms_data->combo_values) {
		combo_values =
			g_slist_nth_data (forms_data->combo_values, combo_number);
		if (combo_values) {
			gchar **row_values = g_strsplit_set (combo_values, "|", -1);
			if (row_values) {
				gint i = 0;
				GtkTreeIter iter;
				gchar *row = row_values[i];

				while (row != NULL) {
					gtk_list_store_append (list_store, &iter);
					gtk_list_store_set (list_store, &iter, 0, row, -1);
					row = row_values[++i];
				}
				g_strfreev (row_values);
			}
			g_free (combo_values);
		}
	}

	combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store));
	g_object_unref (G_OBJECT (list_store));

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
	gtk_cell_layout_set_attributes (
		GTK_CELL_LAYOUT (combo_box), renderer, "text", 0, NULL);

	return combo_box;
}

static GtkWidget *
zenity_forms_create_and_fill_list (
	ZenityFormsData *forms_data, int list_number, gchar *header) {
	GtkListStore *list_store;
	GtkWidget *tree_view;
	GtkWidget *scrolled_window;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GType *column_types = NULL;
	gchar *list_values;
	gchar *column_values;

	gint i = 0;
	/* If no column names available, default is one */
	gint n_columns = 1;
	gint column_index = 0;

	tree_view = gtk_tree_view_new ();

	if (forms_data->column_values) {
		int columns_values_count =
			g_slist_length (forms_data->column_values);
		int column_number = 0;
		if (list_number < columns_values_count) {
			column_number = list_number;
		}

		column_values =
			g_slist_nth_data (forms_data->column_values, column_number);
		if (column_values) {
			gchar **values = g_strsplit_set (column_values, "|", -1);
			if (values) {
				n_columns = g_strv_length (values);
				column_types = g_new (GType, n_columns);
				for (i = 0; i < n_columns; i++)
					column_types[i] = G_TYPE_STRING;

				for (i = 0; i < n_columns; i++) {
					gchar *column_name = values[i];
					renderer = gtk_cell_renderer_text_new ();
					column = gtk_tree_view_column_new_with_attributes (
						column_name, renderer, "text", column_index, NULL);
					gtk_tree_view_append_column (
						GTK_TREE_VIEW (tree_view), column);
					column_index++;
				}
			}
		} else {
			/* If no values available, add one with string type*/
			column_types = g_new (GType, n_columns);
			column_types[0] = G_TYPE_STRING;
		}
	}

	list_store = g_object_new (GTK_TYPE_LIST_STORE, NULL);

	gtk_list_store_set_column_types (list_store, n_columns, column_types);

	if (forms_data->list_values) {
		list_values = g_slist_nth_data (forms_data->list_values, list_number);
		if (list_values) {
			gchar **row_values = g_strsplit_set (list_values, "|", -1);
			if (row_values) {
				GtkTreeIter iter;
				gchar *row = row_values[0];
				gint position = -1;
				i = 0;

				while (row != NULL) {
{'fraction_non_alphanumeric': 0.10478410478410478, 'fraction_numerical': 0.008436008436008436, 'mean_word_length': 4.356718192627824, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3974439', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3283, 'n_words': 847}