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

/*
 * SPDX-License-Identifier: LGPL-3.0-or-later
 * SPDX-FileCopyrightText: 2018-2023 elementary, Inc. (https://elementary.io)
 *                         2015 Adam Bieńkowski
 *
 * Authored by: Adam Bieńkowski <donadigos159@gmail.com>
 *              Julien Spautz <spautz.julien@gmail.com>
 */

#include "screentime-limits.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <granite-7.h>
#include <glib/gi18n-lib.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	PC_WIDGETS_APP_CHOOSER_0_PROPERTY,
	PC_WIDGETS_APP_CHOOSER_NUM_PROPERTIES
};
static GParamSpec* pc_widgets_app_chooser_properties[PC_WIDGETS_APP_CHOOSER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
enum  {
	PC_WIDGETS_APP_CHOOSER_APP_CHOSEN_SIGNAL,
	PC_WIDGETS_APP_CHOOSER_NUM_SIGNALS
};
static guint pc_widgets_app_chooser_signals[PC_WIDGETS_APP_CHOOSER_NUM_SIGNALS] = {0};

struct _PCWidgetsAppChooserPrivate {
	GtkListBox* listbox;
	GtkSearchEntry* search_entry;
};

static gint PCWidgetsAppChooser_private_offset;
static gpointer pc_widgets_app_chooser_parent_class = NULL;

static gint pc_widgets_app_chooser_sort_function (PCWidgetsAppChooser* self,
                                           GtkListBoxRow* first_row,
                                           GtkListBoxRow* second_row);
static gboolean pc_widgets_app_chooser_filter_function (PCWidgetsAppChooser* self,
                                                 GtkListBoxRow* row);
static void pc_widgets_app_chooser_on_app_selected (PCWidgetsAppChooser* self,
                                             GtkListBoxRow* row);
static GObject * pc_widgets_app_chooser_constructor (GType type,
                                              guint n_construct_properties,
                                              GObjectConstructParam * construct_properties);
static gboolean _pc_widgets_app_chooser_filter_function_gtk_list_box_filter_func (GtkListBoxRow* row,
                                                                           gpointer self);
static gint _pc_widgets_app_chooser_sort_function_gtk_list_box_sort_func (GtkListBoxRow* row1,
                                                                   GtkListBoxRow* row2,
                                                                   gpointer self);
static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
static void _gtk_widget_hide_gtk_dialog_response (GtkDialog* _sender,
                                           gint response_id,
                                           gpointer self);
static void _pc_widgets_app_chooser_on_app_selected_gtk_list_box_row_activated (GtkListBox* _sender,
                                                                         GtkListBoxRow* row,
                                                                         gpointer self);
static void _pc_widgets_app_chooser___lambda12_ (PCWidgetsAppChooser* self);
static void __pc_widgets_app_chooser___lambda12__gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                                                           gpointer self);
static void pc_widgets_app_chooser_finalize (GObject * obj);
static GType pc_widgets_app_chooser_get_type_once (void);

static inline gpointer
pc_widgets_app_chooser_get_instance_private (PCWidgetsAppChooser* self)
{
	return G_STRUCT_MEMBER_P (self, PCWidgetsAppChooser_private_offset);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static gint
pc_widgets_app_chooser_sort_function (PCWidgetsAppChooser* self,
                                      GtkListBoxRow* first_row,
                                      GtkListBoxRow* second_row)
{
	PCWidgetsAppRow* row_1 = NULL;
	PCWidgetsAppRow* _tmp0_;
	PCWidgetsAppRow* row_2 = NULL;
	PCWidgetsAppRow* _tmp1_;
	gchar* name_1 = NULL;
	GAppInfo* _tmp2_;
	GAppInfo* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* name_2 = NULL;
	GAppInfo* _tmp6_;
	GAppInfo* _tmp7_;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (first_row != NULL, -1);
	g_return_val_if_fail (second_row != NULL, -1);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (first_row, PC_WIDGETS_TYPE_APP_ROW, PCWidgetsAppRow));
	row_1 = _tmp0_;
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (second_row, PC_WIDGETS_TYPE_APP_ROW, PCWidgetsAppRow));
	row_2 = _tmp1_;
	_tmp2_ = pc_widgets_app_row_get_app_info (row_1);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_app_info_get_display_name (_tmp3_);
	_tmp5_ = g_strdup (_tmp4_);
	name_1 = _tmp5_;
	_tmp6_ = pc_widgets_app_row_get_app_info (row_2);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_app_info_get_display_name (_tmp7_);
	_tmp9_ = g_strdup (_tmp8_);
	name_2 = _tmp9_;
	result = g_utf8_collate (name_1, name_2);
	_g_free0 (name_2);
	_g_free0 (name_1);
	_g_object_unref0 (row_2);
	_g_object_unref0 (row_1);
	return result;
}

static gboolean
string_contains (const gchar* self,
                 const gchar* needle)
{
	gchar* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (needle != NULL, FALSE);
	_tmp0_ = strstr ((gchar*) self, (gchar*) needle);
	result = _tmp0_ != NULL;
	return result;
}

static gboolean
pc_widgets_app_chooser_filter_function (PCWidgetsAppChooser* self,
                                        GtkListBoxRow* row)
{
	PCWidgetsAppRow* app_row = NULL;
	PCWidgetsAppRow* _tmp0_;
	gboolean _tmp1_ = FALSE;
	PCWidgetsAppRow* _tmp2_;
	GAppInfo* _tmp3_;
	GAppInfo* _tmp4_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GtkSearchEntry* _tmp8_;
	const gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gboolean _tmp13_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (row != NULL, FALSE);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (row, PC_WIDGETS_TYPE_APP_ROW, PCWidgetsAppRow));
	app_row = _tmp0_;
	_tmp2_ = app_row;
	_tmp3_ = pc_widgets_app_row_get_app_info (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_app_info_get_display_name (_tmp4_);
	_tmp6_ = g_utf8_strdown (_tmp5_, (gssize) -1);
	_tmp7_ = _tmp6_;
	_tmp8_ = self->priv->search_entry;
	_tmp9_ = gtk_editable_get_text ((GtkEditable*) _tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = g_utf8_strdown (_tmp10_, (gssize) -1);
	_tmp12_ = _tmp11_;
	_tmp13_ = string_contains (_tmp7_, _tmp12_);
	_g_free0 (_tmp12_);
	_g_free0 (_tmp7_);
	if (_tmp13_) {
		_tmp1_ = TRUE;
	} else {
		PCWidgetsAppRow* _tmp14_;
		GAppInfo* _tmp15_;
		GAppInfo* _tmp16_;
		const gchar* _tmp17_;
		gchar* _tmp18_;
		gchar* _tmp19_;
		GtkSearchEntry* _tmp20_;
		const gchar* _tmp21_;
		const gchar* _tmp22_;
		gchar* _tmp23_;
		gchar* _tmp24_;
		_tmp14_ = app_row;
		_tmp15_ = pc_widgets_app_row_get_app_info (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = g_app_info_get_description (_tmp16_);
		_tmp18_ = g_utf8_strdown (_tmp17_, (gssize) -1);
		_tmp19_ = _tmp18_;
		_tmp20_ = self->priv->search_entry;
		_tmp21_ = gtk_editable_get_text ((GtkEditable*) _tmp20_);
		_tmp22_ = _tmp21_;
		_tmp23_ = g_utf8_strdown (_tmp22_, (gssize) -1);
		_tmp24_ = _tmp23_;
		_tmp1_ = string_contains (_tmp19_, _tmp24_);
		_g_free0 (_tmp24_);
		_g_free0 (_tmp19_);
	}
	result = _tmp1_;
	_g_object_unref0 (app_row);
	return result;
}

static void
pc_widgets_app_chooser_on_app_selected (PCWidgetsAppChooser* self,
                                        GtkListBoxRow* row)
{
	PCWidgetsAppRow* app_row = NULL;
	PCWidgetsAppRow* _tmp0_;
	GAppInfo* _tmp1_;
	GAppInfo* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (row != NULL);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (row, PC_WIDGETS_TYPE_APP_ROW, PCWidgetsAppRow));
	app_row = _tmp0_;
	_tmp1_ = pc_widgets_app_row_get_app_info (app_row);
	_tmp2_ = _tmp1_;
	g_signal_emit (self, pc_widgets_app_chooser_signals[PC_WIDGETS_APP_CHOOSER_APP_CHOSEN_SIGNAL], 0, _tmp2_);
	gtk_widget_hide ((GtkWidget*) self);
	_g_object_unref0 (app_row);
}

PCWidgetsAppChooser*
pc_widgets_app_chooser_construct (GType object_type)
{
	PCWidgetsAppChooser * self = NULL;
	self = (PCWidgetsAppChooser*) granite_dialog_construct (object_type);
	return self;
}

PCWidgetsAppChooser*
pc_widgets_app_chooser_new (void)
{
	return pc_widgets_app_chooser_construct (PC_WIDGETS_TYPE_APP_CHOOSER);
}

static gboolean
_pc_widgets_app_chooser_filter_function_gtk_list_box_filter_func (GtkListBoxRow* row,
                                                                  gpointer self)
{
	gboolean result;
	result = pc_widgets_app_chooser_filter_function ((PCWidgetsAppChooser*) self, row);
	return result;
}

static gint
_pc_widgets_app_chooser_sort_function_gtk_list_box_sort_func (GtkListBoxRow* row1,
                                                              GtkListBoxRow* row2,
                                                              gpointer self)
{
	gint result;
	result = pc_widgets_app_chooser_sort_function ((PCWidgetsAppChooser*) self, row1, row2);
	return result;
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static inline void
_g_list_free__g_object_unref0_ (GList* self)
{
	g_list_free_full (self, (GDestroyNotify) _g_object_unref0_);
}

static void
_gtk_widget_hide_gtk_dialog_response (GtkDialog* _sender,
                                      gint response_id,
                                      gpointer self)
{
	gtk_widget_hide ((GtkWidget*) self);
}

static void
_pc_widgets_app_chooser_on_app_selected_gtk_list_box_row_activated (GtkListBox* _sender,
                                                                    GtkListBoxRow* row,
                                                                    gpointer self)
{
	pc_widgets_app_chooser_on_app_selected ((PCWidgetsAppChooser*) self, row);
}

static void
_pc_widgets_app_chooser___lambda12_ (PCWidgetsAppChooser* self)
{
	GtkListBox* _tmp0_;
	_tmp0_ = self->priv->listbox;
	gtk_list_box_invalidate_filter (_tmp0_);
}

static void
__pc_widgets_app_chooser___lambda12__gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                                                      gpointer self)
{
	_pc_widgets_app_chooser___lambda12_ ((PCWidgetsAppChooser*) self);
}

static GObject *
pc_widgets_app_chooser_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	PCWidgetsAppChooser * self;
	gchar* _tmp0_;
	GtkSearchEntry* _tmp1_ = NULL;
	GtkListBox* _tmp2_ = NULL;
	GtkListBox* _tmp3_;
	GtkListBox* _tmp4_;
	GtkListBox* _tmp5_;
	GtkScrolledWindow* scrolled = NULL;
	GtkListBox* _tmp6_;
	GtkWidget* _tmp7_;
	GtkScrolledWindow* _tmp8_ = NULL;
	GtkFrame* frame = NULL;
	GtkScrolledWindow* _tmp9_;
	GtkWidget* _tmp10_;
	GtkFrame* _tmp11_ = NULL;
	GtkBox* box = NULL;
	GtkBox* _tmp12_;
	GtkBox* _tmp13_;
	GtkSearchEntry* _tmp14_;
	GtkBox* _tmp15_;
	GtkFrame* _tmp16_;
	GtkSearchEntry* _tmp17_;
	GtkBox* _tmp18_;
	GtkBox* _tmp19_;
	GList* _tmp20_;
	GtkListBox* _tmp27_;
	GtkSearchEntry* _tmp28_;
	parent_class = G_OBJECT_CLASS (pc_widgets_app_chooser_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PC_WIDGETS_TYPE_APP_CHOOSER, PCWidgetsAppChooser);
	_tmp0_ = g_strdup (_ ("Search Applications"));
	_tmp1_ = (GtkSearchEntry*) gtk_search_entry_new ();
	g_object_set (_tmp1_, "placeholder-text", _tmp0_, NULL);
	_g_free0 (_tmp0_);
	g_object_ref_sink (_tmp1_);
	_g_object_unref0 (self->priv->search_entry);
	self->priv->search_entry = _tmp1_;
	_tmp2_ = (GtkListBox*) gtk_list_box_new ();
	gtk_widget_set_hexpand ((GtkWidget*) _tmp2_, TRUE);
	gtk_widget_set_vexpand ((GtkWidget*) _tmp2_, TRUE);
	g_object_ref_sink (_tmp2_);
	_g_object_unref0 (self->priv->listbox);
	self->priv->listbox = _tmp2_;
	_tmp3_ = self->priv->listbox;
	gtk_widget_add_css_class ((GtkWidget*) _tmp3_, GRANITE_STYLE_CLASS_RICH_LIST);
	_tmp4_ = self->priv->listbox;
	gtk_list_box_set_filter_func (_tmp4_, _pc_widgets_app_chooser_filter_function_gtk_list_box_filter_func, g_object_ref (self), g_object_unref);
	_tmp5_ = self->priv->listbox;
	gtk_list_box_set_sort_func (_tmp5_, _pc_widgets_app_chooser_sort_function_gtk_list_box_sort_func, g_object_ref (self), g_object_unref);
	_tmp6_ = self->priv->listbox;
	_tmp7_ = _g_object_ref0 ((GtkWidget*) _tmp6_);
	_tmp8_ = (GtkScrolledWindow*) gtk_scrolled_window_new ();
	gtk_scrolled_window_set_child (_tmp8_, _tmp7_);
	_g_object_unref0 (_tmp7_);
	g_object_ref_sink (_tmp8_);
	scrolled = _tmp8_;
	_tmp9_ = scrolled;
	_tmp10_ = _g_object_ref0 ((GtkWidget*) _tmp9_);
	_tmp11_ = (GtkFrame*) gtk_frame_new (NULL);
	gtk_frame_set_child (_tmp11_, _tmp10_);
	_g_object_unref0 (_tmp10_);
	g_object_ref_sink (_tmp11_);
	frame = _tmp11_;
	_tmp12_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
	g_object_ref_sink (_tmp12_);
	box = _tmp12_;
	_tmp13_ = box;
	_tmp14_ = self->priv->search_entry;
	gtk_box_append (_tmp13_, (GtkWidget*) _tmp14_);
	_tmp15_ = box;
	_tmp16_ = frame;
	gtk_box_append (_tmp15_, (GtkWidget*) _tmp16_);
	g_object_set ((GtkWindow*) self, "default-height", 500, NULL);
	g_object_set ((GtkWindow*) self, "default-width", 400, NULL);
	_tmp17_ = self->priv->search_entry;
	g_object_set ((GtkWindow*) self, "focus-widget", (GtkWidget*) _tmp17_, NULL);
	gtk_window_set_modal ((GtkWindow*) self, TRUE);
	_tmp18_ = gtk_dialog_get_content_area ((GtkDialog*) self);
	_tmp19_ = box;
	gtk_box_append (_tmp18_, (GtkWidget*) _tmp19_);
	granite_dialog_add_button ((GraniteDialog*) self, _ ("Cancel"), (gint) GTK_RESPONSE_CANCEL);
	_tmp20_ = g_app_info_get_all ();
	{
		GList* _info_collection = NULL;
		GList* _info_it = NULL;
		_info_collection = _tmp20_;
		for (_info_it = _info_collection; _info_it != NULL; _info_it = _info_it->next) {
			GAppInfo* _tmp21_;
			GAppInfo* _info = NULL;
			_tmp21_ = _g_object_ref0 ((GAppInfo*) _info_it->data);
			_info = _tmp21_;
			{
				GAppInfo* _tmp22_;
				_tmp22_ = _info;
				if (g_app_info_should_show (_tmp22_)) {
					PCWidgetsAppRow* row = NULL;
					GAppInfo* _tmp23_;
					PCWidgetsAppRow* _tmp24_;
					GtkListBox* _tmp25_;
					PCWidgetsAppRow* _tmp26_;
					_tmp23_ = _info;
					_tmp24_ = pc_widgets_app_row_new (_tmp23_);
					g_object_ref_sink (_tmp24_);
					row = _tmp24_;
					_tmp25_ = self->priv->listbox;
					_tmp26_ = row;
					gtk_list_box_prepend (_tmp25_, (GtkWidget*) _tmp26_);
					_g_object_unref0 (row);
				}
				_g_object_unref0 (_info);
			}
		}
		(_info_collection == NULL) ? NULL : (_info_collection = (_g_list_free__g_object_unref0_ (_info_collection), NULL));
	}
	g_signal_connect_object ((GtkDialog*) self, "response", (GCallback) _gtk_widget_hide_gtk_dialog_response, (GtkWidget*) self, 0);
	_tmp27_ = self->priv->listbox;
	g_signal_connect_object (_tmp27_, "row-activated", (GCallback) _pc_widgets_app_chooser_on_app_selected_gtk_list_box_row_activated, self, 0);
	_tmp28_ = self->priv->search_entry;
	g_signal_connect_object (_tmp28_, "search-changed", (GCallback) __pc_widgets_app_chooser___lambda12__gtk_search_entry_search_changed, self, 0);
	_g_object_unref0 (box);
	_g_object_unref0 (frame);
	_g_object_unref0 (scrolled);
	return obj;
}

static void
pc_widgets_app_chooser_class_init (PCWidgetsAppChooserClass * klass,
                                   gpointer klass_data)
{
	pc_widgets_app_chooser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PCWidgetsAppChooser_private_offset);
	G_OBJECT_CLASS (klass)->constructor = pc_widgets_app_chooser_constructor;
	G_OBJECT_CLASS (klass)->finalize = pc_widgets_app_chooser_finalize;
	pc_widgets_app_chooser_signals[PC_WIDGETS_APP_CHOOSER_APP_CHOSEN_SIGNAL] = g_signal_new ("app-chosen", PC_WIDGETS_TYPE_APP_CHOOSER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, g_app_info_get_type ());
}

static void
pc_widgets_app_chooser_instance_init (PCWidgetsAppChooser * self,
                                      gpointer klass)
{
	self->priv = pc_widgets_app_chooser_get_instance_private (self);
}

static void
pc_widgets_app_chooser_finalize (GObject * obj)
{
	PCWidgetsAppChooser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PC_WIDGETS_TYPE_APP_CHOOSER, PCWidgetsAppChooser);
	_g_object_unref0 (self->priv->listbox);
	_g_object_unref0 (self->priv->search_entry);
	G_OBJECT_CLASS (pc_widgets_app_chooser_parent_class)->finalize (obj);
}

static GType
pc_widgets_app_chooser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PCWidgetsAppChooserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pc_widgets_app_chooser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PCWidgetsAppChooser), 0, (GInstanceInitFunc) pc_widgets_app_chooser_instance_init, NULL };
	GType pc_widgets_app_chooser_type_id;
	pc_widgets_app_chooser_type_id = g_type_register_static (GRANITE_TYPE_DIALOG, "PCWidgetsAppChooser", &g_define_type_info, 0);
	PCWidgetsAppChooser_private_offset = g_type_add_instance_private (pc_widgets_app_chooser_type_id, sizeof (PCWidgetsAppChooserPrivate));
	return pc_widgets_app_chooser_type_id;
}

GType
pc_widgets_app_chooser_get_type (void)
{
	static volatile gsize pc_widgets_app_chooser_type_id__once = 0;
	if (g_once_init_enter (&pc_widgets_app_chooser_type_id__once)) {
		GType pc_widgets_app_chooser_type_id;
		pc_widgets_app_chooser_type_id = pc_widgets_app_chooser_get_type_once ();
		g_once_init_leave (&pc_widgets_app_chooser_type_id__once, pc_widgets_app_chooser_type_id);
	}
	return pc_widgets_app_chooser_type_id__once;
}

