/* choose_pkgs_dialog.c generated by valac 0.56.14, the Vala compiler
 * generated from choose_pkgs_dialog.vala, do not modify */

/*
 *  pamac-vala
 *
 *  Copyright (C) 2015-2021 Guillaume Benoit <guillaume@manjaro.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a get of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pamac-gtk.h"
#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>

enum  {
	PAMAC_CHOOSE_PKGS_DIALOG_0_PROPERTY,
	PAMAC_CHOOSE_PKGS_DIALOG_NUM_PROPERTIES
};
static GParamSpec* pamac_choose_pkgs_dialog_properties[PAMAC_CHOOSE_PKGS_DIALOG_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block5Data Block5Data;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _Block5Data {
	int _ref_count_;
	PamacChoosePkgsDialog* self;
	gchar* search_string;
};

static gpointer pamac_choose_pkgs_dialog_parent_class = NULL;

static void _g_free0_ (gpointer var);
static void pamac_choose_pkgs_dialog_on_search_text_changed (PamacChoosePkgsDialog* self);
static Block5Data* block5_data_ref (Block5Data* _data5_);
static void block5_data_unref (void * _userdata_);
static gboolean ___lambda5_ (Block5Data* _data5_,
                      GtkListBoxRow* row);
static gboolean ____lambda5__gtk_list_box_filter_func (GtkListBoxRow* row,
                                                gpointer self);
static void _pamac_choose_pkgs_dialog_on_search_text_changed_gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                                                                       gpointer self);
static void pamac_choose_pkgs_dialog_finalize (GObject * obj);
static GType pamac_choose_pkgs_dialog_get_type_once (void);

PamacChoosePkgsDialog*
pamac_choose_pkgs_dialog_construct (GType object_type,
                                    GtkWindow* window)
{
	PamacChoosePkgsDialog * self = NULL;
	g_return_val_if_fail (window != NULL, NULL);
	self = (PamacChoosePkgsDialog*) g_object_new (object_type, "transient-for", window, NULL);
	return self;
}

PamacChoosePkgsDialog*
pamac_choose_pkgs_dialog_new (GtkWindow* window)
{
	return pamac_choose_pkgs_dialog_construct (PAMAC_TYPE_CHOOSE_PKGS_DIALOG, window);
}

void
pamac_choose_pkgs_dialog_enable_search (PamacChoosePkgsDialog* self)
{
	GtkSearchEntry* _tmp0_;
	GtkSearchEntry* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->search_entry;
	gtk_widget_set_visible ((GtkWidget*) _tmp0_, TRUE);
	_tmp1_ = self->search_entry;
	gtk_search_entry_set_key_capture_widget (_tmp1_, (GtkWidget*) self);
}

void
pamac_choose_pkgs_dialog_add_pkg (PamacChoosePkgsDialog* self,
                                  const gchar* pkgname)
{
	GtkCheckButton* radiobutton = NULL;
	GtkCheckButton* _tmp0_;
	GtkLabel* label = NULL;
	GtkLabel* _tmp1_;
	GtkListBox* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (pkgname != NULL);
	_tmp0_ = (GtkCheckButton*) gtk_check_button_new ();
	g_object_ref_sink (_tmp0_);
	radiobutton = _tmp0_;
	_tmp1_ = (GtkLabel*) gtk_label_new (pkgname);
	g_object_ref_sink (_tmp1_);
	label = _tmp1_;
	gtk_label_set_wrap (label, TRUE);
	gtk_check_button_set_child (radiobutton, (GtkWidget*) label);
	gtk_widget_add_css_class ((GtkWidget*) radiobutton, "selection-mode");
	_tmp2_ = self->listbox;
	gtk_list_box_append (_tmp2_, (GtkWidget*) radiobutton);
	_g_object_unref0 (label);
	_g_object_unref0 (radiobutton);
}

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

GPtrArray*
pamac_choose_pkgs_dialog_get_selected_pkgs (PamacChoosePkgsDialog* self)
{
	GPtrArray* selected = NULL;
	GPtrArray* _tmp0_;
	GtkWidget* widget = NULL;
	GtkListBox* _tmp1_;
	GtkWidget* _tmp2_;
	GPtrArray* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_ptr_array_new_full ((guint) 0, _g_free0_);
	selected = _tmp0_;
	_tmp1_ = self->listbox;
	_tmp2_ = gtk_widget_get_first_child ((GtkWidget*) _tmp1_);
	widget = _tmp2_;
	while (TRUE) {
		GtkWidget* _tmp3_;
		GtkListBoxRow* row = NULL;
		GtkWidget* _tmp4_;
		GtkWidget* child = NULL;
		GtkListBoxRow* _tmp5_;
		GtkWidget* _tmp6_;
		GtkCheckButton* radiobutton = NULL;
		GtkWidget* _tmp7_;
		GtkCheckButton* _tmp8_;
		GtkWidget* _tmp9_;
		GtkLabel* label = NULL;
		GtkWidget* _tmp10_;
		GtkCheckButton* _tmp11_;
		gboolean _tmp12_;
		gboolean _tmp13_;
		GtkWidget* _tmp19_;
		GtkWidget* _tmp20_;
		_tmp3_ = widget;
		if (!(_tmp3_ != NULL)) {
			break;
		}
		_tmp4_ = widget;
		row = G_TYPE_CHECK_INSTANCE_TYPE (_tmp4_, gtk_list_box_row_get_type ()) ? ((GtkListBoxRow*) _tmp4_) : NULL;
		_tmp5_ = row;
		_tmp6_ = gtk_list_box_row_get_child (_tmp5_);
		child = _tmp6_;
		_tmp7_ = child;
		radiobutton = G_TYPE_CHECK_INSTANCE_TYPE (_tmp7_, gtk_check_button_get_type ()) ? ((GtkCheckButton*) _tmp7_) : NULL;
		_tmp8_ = radiobutton;
		_tmp9_ = gtk_check_button_get_child (_tmp8_);
		child = _tmp9_;
		_tmp10_ = child;
		label = G_TYPE_CHECK_INSTANCE_TYPE (_tmp10_, gtk_label_get_type ()) ? ((GtkLabel*) _tmp10_) : NULL;
		_tmp11_ = radiobutton;
		_tmp12_ = gtk_check_button_get_active (_tmp11_);
		_tmp13_ = _tmp12_;
		if (_tmp13_) {
			GPtrArray* _tmp14_;
			GtkLabel* _tmp15_;
			const gchar* _tmp16_;
			const gchar* _tmp17_;
			gchar* _tmp18_;
			_tmp14_ = selected;
			_tmp15_ = label;
			_tmp16_ = gtk_label_get_label (_tmp15_);
			_tmp17_ = _tmp16_;
			_tmp18_ = g_strdup (_tmp17_);
			g_ptr_array_add (_tmp14_, _tmp18_);
		}
		_tmp19_ = widget;
		_tmp20_ = gtk_widget_get_next_sibling (_tmp19_);
		widget = _tmp20_;
	}
	result = selected;
	return result;
}

static Block5Data*
block5_data_ref (Block5Data* _data5_)
{
	g_atomic_int_inc (&_data5_->_ref_count_);
	return _data5_;
}

static void
block5_data_unref (void * _userdata_)
{
	Block5Data* _data5_;
	_data5_ = (Block5Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data5_->_ref_count_)) {
		PamacChoosePkgsDialog* self;
		self = _data5_->self;
		_g_free0 (_data5_->search_string);
		_g_object_unref0 (self);
		g_slice_free (Block5Data, _data5_);
	}
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static gboolean
___lambda5_ (Block5Data* _data5_,
             GtkListBoxRow* row)
{
	PamacChoosePkgsDialog* self;
	GtkWidget* child = NULL;
	GtkWidget* _tmp0_;
	GtkCheckButton* radiobutton = NULL;
	GtkWidget* _tmp1_;
	GtkCheckButton* _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	gboolean result;
	self = _data5_->self;
	g_return_val_if_fail (row != NULL, FALSE);
	_tmp0_ = gtk_list_box_row_get_child (row);
	child = _tmp0_;
	_tmp1_ = child;
	radiobutton = G_TYPE_CHECK_INSTANCE_TYPE (_tmp1_, gtk_check_button_get_type ()) ? ((GtkCheckButton*) _tmp1_) : NULL;
	_tmp2_ = radiobutton;
	_tmp3_ = gtk_check_button_get_label (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = _data5_->search_string;
	if (g_str_has_prefix (_tmp4_, _tmp5_)) {
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

static gboolean
____lambda5__gtk_list_box_filter_func (GtkListBoxRow* row,
                                       gpointer self)
{
	gboolean result;
	result = ___lambda5_ (self, row);
	return result;
}

static void
pamac_choose_pkgs_dialog_on_search_text_changed (PamacChoosePkgsDialog* self)
{
	Block5Data* _data5_;
	GtkSearchEntry* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	g_return_if_fail (self != NULL);
	_data5_ = g_slice_new0 (Block5Data);
	_data5_->_ref_count_ = 1;
	_data5_->self = g_object_ref (self);
	_tmp0_ = self->search_entry;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) _tmp0_);
	_tmp2_ = g_utf8_strdown (_tmp1_, (gssize) -1);
	_tmp3_ = _tmp2_;
	_tmp4_ = string_strip (_tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_data5_->search_string = _tmp5_;
	_tmp6_ = _data5_->search_string;
	if (g_strcmp0 (_tmp6_, "") != 0) {
		GtkListBox* _tmp7_;
		_tmp7_ = self->listbox;
		gtk_list_box_set_filter_func (_tmp7_, ____lambda5__gtk_list_box_filter_func, block5_data_ref (_data5_), block5_data_unref);
	} else {
		GtkListBox* _tmp8_;
		_tmp8_ = self->listbox;
		gtk_list_box_set_filter_func (_tmp8_, NULL, NULL, NULL);
	}
	block5_data_unref (_data5_);
	_data5_ = NULL;
}

static void
_pamac_choose_pkgs_dialog_on_search_text_changed_gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                                                                  gpointer self)
{
	pamac_choose_pkgs_dialog_on_search_text_changed ((PamacChoosePkgsDialog*) self);
}

static void
pamac_choose_pkgs_dialog_class_init (PamacChoosePkgsDialogClass * klass,
                                     gpointer klass_data)
{
	pamac_choose_pkgs_dialog_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = pamac_choose_pkgs_dialog_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/manjaro/pamac/transaction/choose_pkgs_dialog.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "search_entry", FALSE, G_STRUCT_OFFSET (PamacChoosePkgsDialog, search_entry));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "listbox", FALSE, G_STRUCT_OFFSET (PamacChoosePkgsDialog, listbox));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_search_text_changed", G_CALLBACK(_pamac_choose_pkgs_dialog_on_search_text_changed_gtk_search_entry_search_changed));
}

static void
pamac_choose_pkgs_dialog_instance_init (PamacChoosePkgsDialog * self,
                                        gpointer klass)
{
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
pamac_choose_pkgs_dialog_finalize (GObject * obj)
{
	PamacChoosePkgsDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PAMAC_TYPE_CHOOSE_PKGS_DIALOG, PamacChoosePkgsDialog);
	G_OBJECT_CLASS (pamac_choose_pkgs_dialog_parent_class)->finalize (obj);
}

static GType
pamac_choose_pkgs_dialog_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PamacChoosePkgsDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pamac_choose_pkgs_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PamacChoosePkgsDialog), 0, (GInstanceInitFunc) pamac_choose_pkgs_dialog_instance_init, NULL };
	GType pamac_choose_pkgs_dialog_type_id;
	pamac_choose_pkgs_dialog_type_id = g_type_register_static (adw_message_dialog_get_type (), "PamacChoosePkgsDialog", &g_define_type_info, 0);
	return pamac_choose_pkgs_dialog_type_id;
}

GType
pamac_choose_pkgs_dialog_get_type (void)
{
	static volatile gsize pamac_choose_pkgs_dialog_type_id__once = 0;
	if (g_once_init_enter (&pamac_choose_pkgs_dialog_type_id__once)) {
		GType pamac_choose_pkgs_dialog_type_id;
		pamac_choose_pkgs_dialog_type_id = pamac_choose_pkgs_dialog_get_type_once ();
		g_once_init_leave (&pamac_choose_pkgs_dialog_type_id__once, pamac_choose_pkgs_dialog_type_id);
	}
	return pamac_choose_pkgs_dialog_type_id__once;
}

