/* cdrom-row.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from cdrom-row.vala, do not modify */

/* This file is part of GNOME Boxes. License: LGPLv2+*/

#include <handy.h>
#include <glib-object.h>
#include <libvirt-gconfig/libvirt-gconfig.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include "common.h"
#include <libvirt-gobject/libvirt-gobject.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define BOXES_TYPE_CDROM_ROW (boxes_cdrom_row_get_type ())
#define BOXES_CDROM_ROW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BOXES_TYPE_CDROM_ROW, BoxesCdromRow))
#define BOXES_CDROM_ROW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BOXES_TYPE_CDROM_ROW, BoxesCdromRowClass))
#define BOXES_IS_CDROM_ROW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BOXES_TYPE_CDROM_ROW))
#define BOXES_IS_CDROM_ROW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BOXES_TYPE_CDROM_ROW))
#define BOXES_CDROM_ROW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BOXES_TYPE_CDROM_ROW, BoxesCdromRowClass))

typedef struct _BoxesCdromRow BoxesCdromRow;
typedef struct _BoxesCdromRowClass BoxesCdromRowClass;
typedef struct _BoxesCdromRowPrivate BoxesCdromRowPrivate;

#define BOXES_TYPE_COLLECTION_ITEM (boxes_collection_item_get_type ())
#define BOXES_COLLECTION_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BOXES_TYPE_COLLECTION_ITEM, BoxesCollectionItem))
#define BOXES_COLLECTION_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BOXES_TYPE_COLLECTION_ITEM, BoxesCollectionItemClass))
#define BOXES_IS_COLLECTION_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BOXES_TYPE_COLLECTION_ITEM))
#define BOXES_IS_COLLECTION_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BOXES_TYPE_COLLECTION_ITEM))
#define BOXES_COLLECTION_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BOXES_TYPE_COLLECTION_ITEM, BoxesCollectionItemClass))

typedef struct _BoxesCollectionItem BoxesCollectionItem;
typedef struct _BoxesCollectionItemClass BoxesCollectionItemClass;

#define BOXES_TYPE_MACHINE (boxes_machine_get_type ())
#define BOXES_MACHINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BOXES_TYPE_MACHINE, BoxesMachine))
#define BOXES_MACHINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BOXES_TYPE_MACHINE, BoxesMachineClass))
#define BOXES_IS_MACHINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BOXES_TYPE_MACHINE))
#define BOXES_IS_MACHINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BOXES_TYPE_MACHINE))
#define BOXES_MACHINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BOXES_TYPE_MACHINE, BoxesMachineClass))

typedef struct _BoxesMachine BoxesMachine;
typedef struct _BoxesMachineClass BoxesMachineClass;

#define BOXES_TYPE_LIBVIRT_MACHINE (boxes_libvirt_machine_get_type ())
#define BOXES_LIBVIRT_MACHINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BOXES_TYPE_LIBVIRT_MACHINE, BoxesLibvirtMachine))
#define BOXES_LIBVIRT_MACHINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BOXES_TYPE_LIBVIRT_MACHINE, BoxesLibvirtMachineClass))
#define BOXES_IS_LIBVIRT_MACHINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BOXES_TYPE_LIBVIRT_MACHINE))
#define BOXES_IS_LIBVIRT_MACHINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BOXES_TYPE_LIBVIRT_MACHINE))
#define BOXES_LIBVIRT_MACHINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BOXES_TYPE_LIBVIRT_MACHINE, BoxesLibvirtMachineClass))

typedef struct _BoxesLibvirtMachine BoxesLibvirtMachine;
typedef struct _BoxesLibvirtMachineClass BoxesLibvirtMachineClass;
enum  {
	BOXES_CDROM_ROW_0_PROPERTY,
	BOXES_CDROM_ROW_NUM_PROPERTIES
};
static GParamSpec* boxes_cdrom_row_properties[BOXES_CDROM_ROW_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))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _BoxesCdromRow {
	HdyActionRow parent_instance;
	BoxesCdromRowPrivate * priv;
};

struct _BoxesCdromRowClass {
	HdyActionRowClass parent_class;
};

struct _BoxesCdromRowPrivate {
	BoxesLibvirtMachine* machine;
	GVirConfigDomainDisk* cdrom_config;
	GtkStack* stack;
	GtkButton* select_button;
	GtkButton* remove_button;
};

static gint BoxesCdromRow_private_offset;
static gpointer boxes_cdrom_row_parent_class = NULL;

VALA_EXTERN GType boxes_cdrom_row_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BoxesCdromRow, g_object_unref)
VALA_EXTERN GType boxes_collection_item_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BoxesCollectionItem, g_object_unref)
VALA_EXTERN GType boxes_machine_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BoxesMachine, g_object_unref)
VALA_EXTERN GType boxes_libvirt_machine_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BoxesLibvirtMachine, g_object_unref)
VALA_EXTERN void boxes_cdrom_row_setup (BoxesCdromRow* self,
                            BoxesLibvirtMachine* machine);
VALA_EXTERN GVirConfigDomain* boxes_libvirt_machine_get_domain_config (BoxesLibvirtMachine* self);
static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
static void boxes_cdrom_row_set_cdrom_source_path (BoxesCdromRow* self,
                                            const gchar* path);
VALA_EXTERN GVirDomain* boxes_libvirt_machine_get_domain (BoxesLibvirtMachine* self);
VALA_EXTERN const gchar* boxes_collection_item_get_name (BoxesCollectionItem* self);
static void boxes_cdrom_row_on_select_button_clicked (BoxesCdromRow* self);
static void _boxes_cdrom_row_on_select_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                   gpointer self);
static void boxes_cdrom_row_on_remove_button_clicked (BoxesCdromRow* self);
static void _boxes_cdrom_row_on_remove_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                   gpointer self);
VALA_EXTERN BoxesCdromRow* boxes_cdrom_row_new (void);
VALA_EXTERN BoxesCdromRow* boxes_cdrom_row_construct (GType object_type);
static void boxes_cdrom_row_finalize (GObject * obj);
static GType boxes_cdrom_row_get_type_once (void);

static inline gpointer
boxes_cdrom_row_get_instance_private (BoxesCdromRow* self)
{
	return G_STRUCT_MEMBER_P (self, BoxesCdromRow_private_offset);
}

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

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_);
}

void
boxes_cdrom_row_setup (BoxesCdromRow* self,
                       BoxesLibvirtMachine* machine)
{
	BoxesLibvirtMachine* _tmp0_;
	GVirConfigDomain* _tmp1_;
	GVirConfigDomain* _tmp2_;
	GList* _tmp3_;
	gchar* source = NULL;
	GVirConfigDomainDisk* _tmp11_;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	gboolean _tmp14_ = FALSE;
	const gchar* _tmp15_;
	GtkStack* _tmp19_;
	GtkButton* _tmp20_;
	const gchar* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (machine != NULL);
	_tmp0_ = _g_object_ref0 (machine);
	_g_object_unref0 (self->priv->machine);
	self->priv->machine = _tmp0_;
	_tmp1_ = boxes_libvirt_machine_get_domain_config (machine);
	_tmp2_ = _tmp1_;
	_tmp3_ = gvir_config_domain_get_devices (_tmp2_);
	{
		GList* device_config_collection = NULL;
		GList* device_config_it = NULL;
		device_config_collection = _tmp3_;
		for (device_config_it = device_config_collection; device_config_it != NULL; device_config_it = device_config_it->next) {
			GVirConfigDomainDevice* _tmp4_;
			GVirConfigDomainDevice* device_config = NULL;
			_tmp4_ = _g_object_ref0 ((GVirConfigDomainDevice*) device_config_it->data);
			device_config = _tmp4_;
			{
				GVirConfigDomainDevice* _tmp5_;
				_tmp5_ = device_config;
				if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp5_, gvir_config_domain_disk_get_type ())) {
					GVirConfigDomainDisk* disk_config = NULL;
					GVirConfigDomainDevice* _tmp6_;
					GVirConfigDomainDisk* _tmp7_;
					GVirConfigDomainDiskGuestDeviceType disk_type = 0;
					GVirConfigDomainDisk* _tmp8_;
					_tmp6_ = device_config;
					_tmp7_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (_tmp6_, gvir_config_domain_disk_get_type ()) ? ((GVirConfigDomainDisk*) _tmp6_) : NULL);
					disk_config = _tmp7_;
					_tmp8_ = disk_config;
					disk_type = gvir_config_domain_disk_get_guest_device_type (_tmp8_);
					if (disk_type == GVIR_CONFIG_DOMAIN_DISK_GUEST_DEVICE_CDROM) {
						GVirConfigDomainDisk* _tmp9_;
						GVirConfigDomainDisk* _tmp10_;
						_tmp9_ = disk_config;
						_tmp10_ = _g_object_ref0 (_tmp9_);
						_g_object_unref0 (self->priv->cdrom_config);
						self->priv->cdrom_config = _tmp10_;
						_g_object_unref0 (disk_config);
						_g_object_unref0 (device_config);
						break;
					}
					_g_object_unref0 (disk_config);
				}
				_g_object_unref0 (device_config);
			}
		}
		(device_config_collection == NULL) ? NULL : (device_config_collection = (_g_list_free__g_object_unref0_ (device_config_collection), NULL));
	}
	_tmp11_ = self->priv->cdrom_config;
	_tmp12_ = gvir_config_domain_disk_get_source (_tmp11_);
	_tmp13_ = g_strdup (_tmp12_);
	source = _tmp13_;
	_tmp15_ = source;
	if (_tmp15_ == NULL) {
		_tmp14_ = TRUE;
	} else {
		const gchar* _tmp16_;
		_tmp16_ = source;
		_tmp14_ = g_strcmp0 (_tmp16_, "") == 0;
	}
	if (_tmp14_) {
		GtkStack* _tmp17_;
		GtkButton* _tmp18_;
		hdy_preferences_row_set_title ((HdyPreferencesRow*) self, _ ("No CD/DVD image"));
		_tmp17_ = self->priv->stack;
		_tmp18_ = self->priv->select_button;
		gtk_stack_set_visible_child (_tmp17_, (GtkWidget*) _tmp18_);
		_g_free0 (source);
		return;
	}
	_tmp19_ = self->priv->stack;
	_tmp20_ = self->priv->remove_button;
	gtk_stack_set_visible_child (_tmp19_, (GtkWidget*) _tmp20_);
	_tmp21_ = source;
	_tmp22_ = boxes_get_utf8_basename (_tmp21_);
	_tmp23_ = _tmp22_;
	hdy_preferences_row_set_title ((HdyPreferencesRow*) self, _tmp23_);
	_g_free0 (_tmp23_);
	_g_free0 (source);
}

static void
boxes_cdrom_row_set_cdrom_source_path (BoxesCdromRow* self,
                                       const gchar* path)
{
	const gchar* _tmp0_ = NULL;
	GVirConfigDomainDisk* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	if (path != NULL) {
		_tmp0_ = path;
	} else {
		_tmp0_ = "";
	}
	_tmp1_ = self->priv->cdrom_config;
	gvir_config_domain_disk_set_source (_tmp1_, _tmp0_);
	{
		BoxesLibvirtMachine* _tmp2_;
		GVirDomain* _tmp3_;
		GVirDomain* _tmp4_;
		GVirConfigDomainDisk* _tmp5_;
		BoxesLibvirtMachine* _tmp6_;
		_tmp2_ = self->priv->machine;
		_tmp3_ = boxes_libvirt_machine_get_domain (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = self->priv->cdrom_config;
		gvir_domain_update_device (_tmp4_, (GVirConfigDomainDevice*) _tmp5_, (guint) GVIR_DOMAIN_UPDATE_DEVICE_CURRENT, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp6_ = self->priv->machine;
		boxes_cdrom_row_setup (self, _tmp6_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		BoxesLibvirtMachine* _tmp7_;
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		GError* _tmp10_;
		const gchar* _tmp11_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp7_ = self->priv->machine;
		_tmp8_ = boxes_collection_item_get_name ((BoxesCollectionItem*) _tmp7_);
		_tmp9_ = _tmp8_;
		_tmp10_ = _error_;
		_tmp11_ = _tmp10_->message;
		g_debug ("cdrom-row.vala:53: Error inserting '%s' as CD into '%s': %s", path, _tmp9_, _tmp11_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
boxes_cdrom_row_on_select_button_clicked (BoxesCdromRow* self)
{
	GtkFileChooserNative* file_chooser = NULL;
	GtkWidget* _tmp0_;
	GtkFileChooserNative* _tmp1_;
	gint response = 0;
	GtkFileChooserNative* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = gtk_widget_get_toplevel ((GtkWidget*) self);
	_tmp1_ = gtk_file_chooser_native_new (_ ("Select a device or ISO file"), G_TYPE_CHECK_INSTANCE_TYPE (_tmp0_, gtk_window_get_type ()) ? ((GtkWindow*) _tmp0_) : NULL, GTK_FILE_CHOOSER_ACTION_OPEN, _ ("Open"), _ ("Cancel"));
	file_chooser = _tmp1_;
	_tmp2_ = file_chooser;
	response = gtk_native_dialog_run ((GtkNativeDialog*) _tmp2_);
	if (response == ((gint) GTK_RESPONSE_ACCEPT)) {
		GtkFileChooserNative* _tmp3_;
		gchar* _tmp4_;
		gchar* _tmp5_;
		_tmp3_ = file_chooser;
		_tmp4_ = gtk_file_chooser_get_filename ((GtkFileChooser*) _tmp3_);
		_tmp5_ = _tmp4_;
		boxes_cdrom_row_set_cdrom_source_path (self, _tmp5_);
		_g_free0 (_tmp5_);
	}
	_g_object_unref0 (file_chooser);
}

static void
_boxes_cdrom_row_on_select_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                              gpointer self)
{
	boxes_cdrom_row_on_select_button_clicked ((BoxesCdromRow*) self);
}

static void
boxes_cdrom_row_on_remove_button_clicked (BoxesCdromRow* self)
{
	g_return_if_fail (self != NULL);
	boxes_cdrom_row_set_cdrom_source_path (self, NULL);
}

static void
_boxes_cdrom_row_on_remove_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                              gpointer self)
{
	boxes_cdrom_row_on_remove_button_clicked ((BoxesCdromRow*) self);
}

BoxesCdromRow*
boxes_cdrom_row_construct (GType object_type)
{
	BoxesCdromRow * self = NULL;
	self = (BoxesCdromRow*) g_object_new (object_type, NULL);
	return self;
}

BoxesCdromRow*
boxes_cdrom_row_new (void)
{
	return boxes_cdrom_row_construct (BOXES_TYPE_CDROM_ROW);
}

static void
boxes_cdrom_row_class_init (BoxesCdromRowClass * klass,
                            gpointer klass_data)
{
	boxes_cdrom_row_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BoxesCdromRow_private_offset);
	G_OBJECT_CLASS (klass)->finalize = boxes_cdrom_row_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Boxes/ui/preferences/cdrom-row.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "stack", FALSE, BoxesCdromRow_private_offset + G_STRUCT_OFFSET (BoxesCdromRowPrivate, stack));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "select_button", FALSE, BoxesCdromRow_private_offset + G_STRUCT_OFFSET (BoxesCdromRowPrivate, select_button));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "remove_button", FALSE, BoxesCdromRow_private_offset + G_STRUCT_OFFSET (BoxesCdromRowPrivate, remove_button));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_select_button_clicked", G_CALLBACK(_boxes_cdrom_row_on_select_button_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_remove_button_clicked", G_CALLBACK(_boxes_cdrom_row_on_remove_button_clicked_gtk_button_clicked));
}

static void
boxes_cdrom_row_instance_init (BoxesCdromRow * self,
                               gpointer klass)
{
	self->priv = boxes_cdrom_row_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
boxes_cdrom_row_finalize (GObject * obj)
{
	BoxesCdromRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BOXES_TYPE_CDROM_ROW, BoxesCdromRow);
	_g_object_unref0 (self->priv->machine);
	_g_object_unref0 (self->priv->cdrom_config);
	G_OBJECT_CLASS (boxes_cdrom_row_parent_class)->finalize (obj);
}

static GType
boxes_cdrom_row_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BoxesCdromRowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) boxes_cdrom_row_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BoxesCdromRow), 0, (GInstanceInitFunc) boxes_cdrom_row_instance_init, NULL };
	GType boxes_cdrom_row_type_id;
	boxes_cdrom_row_type_id = g_type_register_static (hdy_action_row_get_type (), "BoxesCdromRow", &g_define_type_info, 0);
	BoxesCdromRow_private_offset = g_type_add_instance_private (boxes_cdrom_row_type_id, sizeof (BoxesCdromRowPrivate));
	return boxes_cdrom_row_type_id;
}

GType
boxes_cdrom_row_get_type (void)
{
	static volatile gsize boxes_cdrom_row_type_id__once = 0;
	if (g_once_init_enter (&boxes_cdrom_row_type_id__once)) {
		GType boxes_cdrom_row_type_id;
		boxes_cdrom_row_type_id = boxes_cdrom_row_get_type_once ();
		g_once_init_leave (&boxes_cdrom_row_type_id__once, boxes_cdrom_row_type_id);
	}
	return boxes_cdrom_row_type_id__once;
}

