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

/*
 *  pamac-vala
 *
 *  Copyright (C) 2014-2023 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 <adwaita.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <pamac.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include "pamac-gtk.h"

#define PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG (pamac_choose_provider_dialog_get_type ())
#define PAMAC_CHOOSE_PROVIDER_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG, PamacChooseProviderDialog))
#define PAMAC_CHOOSE_PROVIDER_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG, PamacChooseProviderDialogClass))
#define PAMAC_IS_CHOOSE_PROVIDER_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG))
#define PAMAC_IS_CHOOSE_PROVIDER_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG))
#define PAMAC_CHOOSE_PROVIDER_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG, PamacChooseProviderDialogClass))

typedef struct _PamacChooseProviderDialog PamacChooseProviderDialog;
typedef struct _PamacChooseProviderDialogClass PamacChooseProviderDialogClass;
typedef struct _PamacChooseProviderDialogPrivate PamacChooseProviderDialogPrivate;
enum  {
	PAMAC_CHOOSE_PROVIDER_DIALOG_0_PROPERTY,
	PAMAC_CHOOSE_PROVIDER_DIALOG_NUM_PROPERTIES
};
static GParamSpec* pamac_choose_provider_dialog_properties[PAMAC_CHOOSE_PROVIDER_DIALOG_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))
typedef struct _PamacChooseProviderDialogChooseProviderData PamacChooseProviderDialogChooseProviderData;

struct _PamacChooseProviderDialog {
	AdwMessageDialog parent_instance;
	PamacChooseProviderDialogPrivate * priv;
};

struct _PamacChooseProviderDialogClass {
	AdwMessageDialogClass parent_class;
};

struct _PamacChooseProviderDialogPrivate {
	GtkBox* box;
};

struct _PamacChooseProviderDialogChooseProviderData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	PamacChooseProviderDialog* self;
	gint result;
	gint index;
	AdwMessageDialog* _tmp0_;
	GtkWidget* widget;
	GtkBox* _tmp1_;
	GtkWidget* _tmp2_;
	GtkWidget* _tmp3_;
	GtkCheckButton* radiobutton;
	GtkWidget* _tmp4_;
	GtkCheckButton* _tmp5_;
	gboolean _tmp6_;
	gboolean _tmp7_;
	gint _tmp8_;
	GtkCheckButton* _tmp9_;
	GtkWidget* _tmp10_;
};

static gint PamacChooseProviderDialog_private_offset;
static gpointer pamac_choose_provider_dialog_parent_class = NULL;

VALA_EXTERN GType pamac_choose_provider_dialog_get_type (void) G_GNUC_CONST ;
VALA_EXTERN PamacChooseProviderDialog* pamac_choose_provider_dialog_new (GtkWindow* window);
VALA_EXTERN PamacChooseProviderDialog* pamac_choose_provider_dialog_construct (GType object_type,
                                                                   GtkWindow* window);
VALA_EXTERN void pamac_choose_provider_dialog_add_providers (PamacChooseProviderDialog* self,
                                                 GPtrArray* pkgs);
static void pamac_choose_provider_dialog_choose_provider_data_free (gpointer _data);
VALA_EXTERN void pamac_choose_provider_dialog_choose_provider (PamacChooseProviderDialog* self,
                                                   GAsyncReadyCallback _callback_,
                                                   gpointer _user_data_);
VALA_EXTERN gint pamac_choose_provider_dialog_choose_provider_finish (PamacChooseProviderDialog* self,
                                                          GAsyncResult* _res_);
static gboolean pamac_choose_provider_dialog_choose_provider_co (PamacChooseProviderDialogChooseProviderData* _data_);
static void pamac_choose_provider_dialog_choose_provider_ready (GObject* source_object,
                                                         GAsyncResult* _res_,
                                                         gpointer _user_data_);
static void pamac_choose_provider_dialog_finalize (GObject * obj);
static GType pamac_choose_provider_dialog_get_type_once (void);

static inline gpointer
pamac_choose_provider_dialog_get_instance_private (PamacChooseProviderDialog* self)
{
	return G_STRUCT_MEMBER_P (self, PamacChooseProviderDialog_private_offset);
}

PamacChooseProviderDialog*
pamac_choose_provider_dialog_construct (GType object_type,
                                        GtkWindow* window)
{
	PamacChooseProviderDialog * self = NULL;
	self = (PamacChooseProviderDialog*) g_object_new (object_type, "transient-for", window, NULL);
	return self;
}

PamacChooseProviderDialog*
pamac_choose_provider_dialog_new (GtkWindow* window)
{
	return pamac_choose_provider_dialog_construct (PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG, window);
}

void
pamac_choose_provider_dialog_add_providers (PamacChooseProviderDialog* self,
                                            GPtrArray* pkgs)
{
	GtkCheckButton* last_radiobutton = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (pkgs != NULL);
	last_radiobutton = NULL;
	{
		GPtrArray* pkg_collection = NULL;
		guint pkg_index = 0U;
		pkg_collection = pkgs;
		for (pkg_index = 0; pkg_index < pkg_collection->len; pkg_index = pkg_index + 1) {
			PamacPackage* pkg = NULL;
			pkg = g_ptr_array_index (pkg_collection, pkg_index);
			{
				gchar* provider = NULL;
				PamacPackage* _tmp0_;
				const gchar* _tmp1_;
				const gchar* _tmp2_;
				PamacPackage* _tmp3_;
				const gchar* _tmp4_;
				const gchar* _tmp5_;
				PamacPackage* _tmp6_;
				const gchar* _tmp7_;
				const gchar* _tmp8_;
				gchar* _tmp9_;
				GtkCheckButton* radiobutton = NULL;
				GtkCheckButton* _tmp10_;
				GtkLabel* label = NULL;
				const gchar* _tmp11_;
				GtkLabel* _tmp12_;
				GtkLabel* _tmp13_;
				GtkCheckButton* _tmp14_;
				GtkLabel* _tmp15_;
				GtkCheckButton* _tmp16_;
				GtkCheckButton* _tmp17_;
				GtkCheckButton* _tmp21_;
				GtkBox* _tmp22_;
				GtkCheckButton* _tmp23_;
				_tmp0_ = pkg;
				_tmp1_ = pamac_package_get_name (_tmp0_);
				_tmp2_ = _tmp1_;
				_tmp3_ = pkg;
				_tmp4_ = pamac_package_get_version (_tmp3_);
				_tmp5_ = _tmp4_;
				_tmp6_ = pkg;
				_tmp7_ = pamac_package_get_repo (_tmp6_);
				_tmp8_ = _tmp7_;
				_tmp9_ = g_strdup_printf ("%s  %s  %s", _tmp2_, _tmp5_, _tmp8_);
				provider = _tmp9_;
				_tmp10_ = (GtkCheckButton*) gtk_check_button_new ();
				g_object_ref_sink (_tmp10_);
				radiobutton = _tmp10_;
				_tmp11_ = provider;
				_tmp12_ = (GtkLabel*) gtk_label_new (_tmp11_);
				g_object_ref_sink (_tmp12_);
				label = _tmp12_;
				_tmp13_ = label;
				gtk_label_set_wrap (_tmp13_, TRUE);
				_tmp14_ = radiobutton;
				_tmp15_ = label;
				gtk_check_button_set_child (_tmp14_, (GtkWidget*) _tmp15_);
				_tmp16_ = radiobutton;
				gtk_widget_add_css_class ((GtkWidget*) _tmp16_, "selection-mode");
				_tmp17_ = last_radiobutton;
				if (_tmp17_ == NULL) {
					GtkCheckButton* _tmp18_;
					_tmp18_ = radiobutton;
					gtk_check_button_set_active (_tmp18_, TRUE);
				} else {
					GtkCheckButton* _tmp19_;
					GtkCheckButton* _tmp20_;
					_tmp19_ = radiobutton;
					_tmp20_ = last_radiobutton;
					gtk_check_button_set_group (_tmp19_, _tmp20_);
				}
				_tmp21_ = radiobutton;
				last_radiobutton = _tmp21_;
				_tmp22_ = self->priv->box;
				_tmp23_ = radiobutton;
				gtk_box_append (_tmp22_, (GtkWidget*) _tmp23_);
				_g_object_unref0 (label);
				_g_object_unref0 (radiobutton);
				_g_free0 (provider);
			}
		}
	}
}

static void
pamac_choose_provider_dialog_choose_provider_data_free (gpointer _data)
{
	PamacChooseProviderDialogChooseProviderData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (PamacChooseProviderDialogChooseProviderData, _data_);
}

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

void
pamac_choose_provider_dialog_choose_provider (PamacChooseProviderDialog* self,
                                              GAsyncReadyCallback _callback_,
                                              gpointer _user_data_)
{
	PamacChooseProviderDialogChooseProviderData* _data_;
	PamacChooseProviderDialog* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (PamacChooseProviderDialogChooseProviderData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, pamac_choose_provider_dialog_choose_provider_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	pamac_choose_provider_dialog_choose_provider_co (_data_);
}

gint
pamac_choose_provider_dialog_choose_provider_finish (PamacChooseProviderDialog* self,
                                                     GAsyncResult* _res_)
{
	gint result;
	PamacChooseProviderDialogChooseProviderData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	result = _data_->result;
	return result;
}

static void
pamac_choose_provider_dialog_choose_provider_ready (GObject* source_object,
                                                    GAsyncResult* _res_,
                                                    gpointer _user_data_)
{
	PamacChooseProviderDialogChooseProviderData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	pamac_choose_provider_dialog_choose_provider_co (_data_);
}

static gboolean
pamac_choose_provider_dialog_choose_provider_co (PamacChooseProviderDialogChooseProviderData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->index = 0;
	_data_->_tmp0_ = _g_object_ref0 ((AdwMessageDialog*) _data_->self);
	_data_->_state_ = 1;
	adw_message_dialog_choose (_data_->_tmp0_, NULL, pamac_choose_provider_dialog_choose_provider_ready, _data_);
	return FALSE;
	_state_1:
	adw_message_dialog_choose_finish (_data_->_tmp0_, _data_->_res_);
	_data_->_tmp1_ = _data_->self->priv->box;
	_data_->_tmp2_ = gtk_widget_get_first_child ((GtkWidget*) _data_->_tmp1_);
	_data_->widget = _data_->_tmp2_;
	while (TRUE) {
		_data_->_tmp3_ = _data_->widget;
		if (!(_data_->_tmp3_ != NULL)) {
			break;
		}
		_data_->_tmp4_ = _data_->widget;
		_data_->radiobutton = G_TYPE_CHECK_INSTANCE_TYPE (_data_->_tmp4_, gtk_check_button_get_type ()) ? ((GtkCheckButton*) _data_->_tmp4_) : NULL;
		_data_->_tmp5_ = _data_->radiobutton;
		_data_->_tmp6_ = gtk_check_button_get_active (_data_->_tmp5_);
		_data_->_tmp7_ = _data_->_tmp6_;
		if (_data_->_tmp7_) {
			break;
		}
		_data_->_tmp8_ = _data_->index;
		_data_->index = _data_->_tmp8_ + 1;
		_data_->_tmp9_ = _data_->radiobutton;
		_data_->_tmp10_ = gtk_widget_get_next_sibling ((GtkWidget*) _data_->_tmp9_);
		_data_->widget = _data_->_tmp10_;
	}
	_data_->result = _data_->index;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
pamac_choose_provider_dialog_class_init (PamacChooseProviderDialogClass * klass,
                                         gpointer klass_data)
{
	pamac_choose_provider_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PamacChooseProviderDialog_private_offset);
	G_OBJECT_CLASS (klass)->finalize = pamac_choose_provider_dialog_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/manjaro/pamac/transaction/choose_provider_dialog.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "box", FALSE, PamacChooseProviderDialog_private_offset + G_STRUCT_OFFSET (PamacChooseProviderDialogPrivate, box));
}

static void
pamac_choose_provider_dialog_instance_init (PamacChooseProviderDialog * self,
                                            gpointer klass)
{
	self->priv = pamac_choose_provider_dialog_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
pamac_choose_provider_dialog_finalize (GObject * obj)
{
	PamacChooseProviderDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PAMAC_TYPE_CHOOSE_PROVIDER_DIALOG, PamacChooseProviderDialog);
	G_OBJECT_CLASS (pamac_choose_provider_dialog_parent_class)->finalize (obj);
}

static GType
pamac_choose_provider_dialog_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PamacChooseProviderDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pamac_choose_provider_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PamacChooseProviderDialog), 0, (GInstanceInitFunc) pamac_choose_provider_dialog_instance_init, NULL };
	GType pamac_choose_provider_dialog_type_id;
	pamac_choose_provider_dialog_type_id = g_type_register_static (adw_message_dialog_get_type (), "PamacChooseProviderDialog", &g_define_type_info, 0);
	PamacChooseProviderDialog_private_offset = g_type_add_instance_private (pamac_choose_provider_dialog_type_id, sizeof (PamacChooseProviderDialogPrivate));
	return pamac_choose_provider_dialog_type_id;
}

GType
pamac_choose_provider_dialog_get_type (void)
{
	static volatile gsize pamac_choose_provider_dialog_type_id__once = 0;
	if (g_once_init_enter (&pamac_choose_provider_dialog_type_id__once)) {
		GType pamac_choose_provider_dialog_type_id;
		pamac_choose_provider_dialog_type_id = pamac_choose_provider_dialog_get_type_once ();
		g_once_init_leave (&pamac_choose_provider_dialog_type_id__once, pamac_choose_provider_dialog_type_id);
	}
	return pamac_choose_provider_dialog_type_id__once;
}

