/* media-selector.c generated by valac 0.36.12.13-3ae08, the Vala compiler
 * generated from media-selector.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>


#define GAMES_TYPE_MEDIA_SELECTOR (games_media_selector_get_type ())
#define GAMES_MEDIA_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelector))
#define GAMES_MEDIA_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelectorClass))
#define GAMES_IS_MEDIA_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_MEDIA_SELECTOR))
#define GAMES_IS_MEDIA_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_MEDIA_SELECTOR))
#define GAMES_MEDIA_SELECTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelectorClass))

typedef struct _GamesMediaSelector GamesMediaSelector;
typedef struct _GamesMediaSelectorClass GamesMediaSelectorClass;
typedef struct _GamesMediaSelectorPrivate GamesMediaSelectorPrivate;

#define GAMES_TYPE_MEDIA_SET (games_media_set_get_type ())
#define GAMES_MEDIA_SET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_MEDIA_SET, GamesMediaSet))
#define GAMES_MEDIA_SET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_MEDIA_SET, GamesMediaSetClass))
#define GAMES_IS_MEDIA_SET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_MEDIA_SET))
#define GAMES_IS_MEDIA_SET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_MEDIA_SET))
#define GAMES_MEDIA_SET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_MEDIA_SET, GamesMediaSetClass))

typedef struct _GamesMediaSet GamesMediaSet;
typedef struct _GamesMediaSetClass GamesMediaSetClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block10Data Block10Data;

#define GAMES_TYPE_MEDIA (games_media_get_type ())
#define GAMES_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_MEDIA, GamesMedia))
#define GAMES_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_MEDIA, GamesMediaClass))
#define GAMES_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_MEDIA))
#define GAMES_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_MEDIA))
#define GAMES_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_MEDIA, GamesMediaClass))

typedef struct _GamesMedia GamesMedia;
typedef struct _GamesMediaClass GamesMediaClass;

#define GAMES_TYPE_TITLE (games_title_get_type ())
#define GAMES_TITLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_TITLE, GamesTitle))
#define GAMES_IS_TITLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_TITLE))
#define GAMES_TITLE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GAMES_TYPE_TITLE, GamesTitleIface))

typedef struct _GamesTitle GamesTitle;
typedef struct _GamesTitleIface GamesTitleIface;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

#define GAMES_TYPE_CHECKMARK_ITEM (games_checkmark_item_get_type ())
#define GAMES_CHECKMARK_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_CHECKMARK_ITEM, GamesCheckmarkItem))
#define GAMES_CHECKMARK_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_CHECKMARK_ITEM, GamesCheckmarkItemClass))
#define GAMES_IS_CHECKMARK_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_CHECKMARK_ITEM))
#define GAMES_IS_CHECKMARK_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_CHECKMARK_ITEM))
#define GAMES_CHECKMARK_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_CHECKMARK_ITEM, GamesCheckmarkItemClass))

typedef struct _GamesCheckmarkItem GamesCheckmarkItem;
typedef struct _GamesCheckmarkItemClass GamesCheckmarkItemClass;

struct _GamesMediaSelector {
	GtkPopover parent_instance;
	GamesMediaSelectorPrivate * priv;
};

struct _GamesMediaSelectorClass {
	GtkPopoverClass parent_class;
};

struct _GamesMediaSelectorPrivate {
	GamesMediaSet* _media_set;
	GtkListBox* list_box;
};

struct _Block10Data {
	int _ref_count_;
	GamesMediaSelector* self;
	gint media_number;
};

typedef void (*GamesMediaSetMediaCallback) (GamesMedia* media, void* user_data);
struct _GamesTitleIface {
	GTypeInterface parent_iface;
	gchar* (*get_title) (GamesTitle* self, GError** error);
};


static gpointer games_media_selector_parent_class = NULL;

GType games_media_selector_get_type (void) G_GNUC_CONST;
GType games_media_set_get_type (void) G_GNUC_CONST;
#define GAMES_MEDIA_SELECTOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelectorPrivate))
enum  {
	GAMES_MEDIA_SELECTOR_0_PROPERTY,
	GAMES_MEDIA_SELECTOR_MEDIA_SET_PROPERTY
};
static void games_media_selector_update_media (GamesMediaSelector* self);
static Block10Data* block10_data_ref (Block10Data* _data10_);
static void block10_data_unref (void * _userdata_);
GType games_media_get_type (void) G_GNUC_CONST;
void games_media_set_foreach_media (GamesMediaSet* self, GamesMediaSetMediaCallback media_callback, void* media_callback_target);
static void __lambda8_ (Block10Data* _data10_, GamesMedia* media);
GType games_title_get_type (void) G_GNUC_CONST;
GamesTitle* games_media_get_title (GamesMedia* self);
gchar* games_title_get_title (GamesTitle* self, GError** error);
GType games_checkmark_item_get_type (void) G_GNUC_CONST;
GamesCheckmarkItem* games_checkmark_item_new (const gchar* name);
GamesCheckmarkItem* games_checkmark_item_construct (GType object_type, const gchar* name);
gint games_media_set_get_selected_media_number (GamesMediaSet* self);
void games_checkmark_item_set_checkmark_visible (GamesCheckmarkItem* self, gboolean value);
static void ___lambda8__games_media_set_media_callback (GamesMedia* media, gpointer self);
static void games_media_selector_remove_media (GamesMediaSelector* self);
static void __lambda9_ (GamesMediaSelector* self, GtkWidget* child);
static void ___lambda9__gtk_callback (GtkWidget* widget, gpointer self);
static void games_media_selector_on_row_activated (GamesMediaSelector* self, GtkListBoxRow* activated_row);
void games_media_set_set_selected_media_number (GamesMediaSet* self, gint value);
static void _games_media_selector_on_row_activated_gtk_list_box_row_activated (GtkListBox* _sender, GtkListBoxRow* row, gpointer self);
GamesMediaSelector* games_media_selector_new (void);
GamesMediaSelector* games_media_selector_construct (GType object_type);
void games_media_selector_set_media_set (GamesMediaSelector* self, GamesMediaSet* value);
static void games_media_selector_finalize (GObject * obj);
static void _vala_games_media_selector_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


static Block10Data* block10_data_ref (Block10Data* _data10_) {
	g_atomic_int_inc (&_data10_->_ref_count_);
	return _data10_;
}


static void block10_data_unref (void * _userdata_) {
	Block10Data* _data10_;
	_data10_ = (Block10Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data10_->_ref_count_)) {
		GamesMediaSelector* self;
		self = _data10_->self;
		_g_object_unref0 (self);
		g_slice_free (Block10Data, _data10_);
	}
}


static void __lambda8_ (Block10Data* _data10_, GamesMedia* media) {
	GamesMediaSelector* self;
	gchar* media_name = NULL;
	GamesMedia* _tmp0_;
	GamesTitle* _tmp1_;
	GamesTitle* _tmp2_;
	GamesCheckmarkItem* checkmark_item = NULL;
	const gchar* _tmp14_;
	GamesCheckmarkItem* _tmp15_;
	gboolean is_current_media = FALSE;
	GamesMediaSet* _tmp16_;
	gint _tmp17_;
	gint _tmp18_;
	gint _tmp19_;
	GtkListBox* _tmp20_;
	gint _tmp21_;
	GError * _inner_error_ = NULL;
	self = _data10_->self;
	g_return_if_fail (media != NULL);
	_tmp0_ = media;
	_tmp1_ = games_media_get_title (_tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ == NULL) {
		gint _tmp3_;
		gchar* _tmp4_;
		_tmp3_ = _data10_->media_number;
		_tmp4_ = g_strdup_printf (_ ("Media %d"), _tmp3_);
		_g_free0 (media_name);
		media_name = _tmp4_;
	} else {
		{
			gchar* _tmp5_ = NULL;
			GamesMedia* _tmp6_;
			GamesTitle* _tmp7_;
			GamesTitle* _tmp8_;
			gchar* _tmp9_;
			gchar* _tmp10_;
			_tmp6_ = media;
			_tmp7_ = games_media_get_title (_tmp6_);
			_tmp8_ = _tmp7_;
			_tmp9_ = games_title_get_title (_tmp8_, &_inner_error_);
			_tmp5_ = _tmp9_;
			if (G_UNLIKELY (_inner_error_ != NULL)) {
				goto __catch39_g_error;
			}
			_tmp10_ = _tmp5_;
			_tmp5_ = NULL;
			_g_free0 (media_name);
			media_name = _tmp10_;
			_g_free0 (_tmp5_);
		}
		goto __finally39;
		__catch39_g_error:
		{
			GError* e = NULL;
			GError* _tmp11_;
			const gchar* _tmp12_;
			gchar* _tmp13_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp11_ = e;
			_tmp12_ = _tmp11_->message;
			g_warning ("media-selector.vala:33: %s", _tmp12_);
			_tmp13_ = g_strdup ("");
			_g_free0 (media_name);
			media_name = _tmp13_;
			_g_error_free0 (e);
		}
		__finally39:
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_free0 (media_name);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return;
		}
	}
	_tmp14_ = media_name;
	_tmp15_ = games_checkmark_item_new (_tmp14_);
	g_object_ref_sink (_tmp15_);
	checkmark_item = _tmp15_;
	_tmp16_ = self->priv->_media_set;
	_tmp17_ = games_media_set_get_selected_media_number (_tmp16_);
	_tmp18_ = _tmp17_;
	_tmp19_ = _data10_->media_number;
	is_current_media = _tmp18_ == _tmp19_;
	games_checkmark_item_set_checkmark_visible (checkmark_item, is_current_media);
	_tmp20_ = self->priv->list_box;
	gtk_container_add ((GtkContainer*) _tmp20_, (GtkWidget*) checkmark_item);
	_tmp21_ = _data10_->media_number;
	_data10_->media_number = _tmp21_ + 1;
	_g_object_unref0 (checkmark_item);
	_g_free0 (media_name);
}


static void ___lambda8__games_media_set_media_callback (GamesMedia* media, gpointer self) {
	__lambda8_ (self, media);
}


static void games_media_selector_update_media (GamesMediaSelector* self) {
	Block10Data* _data10_;
	GamesMediaSet* _tmp0_;
	GamesMediaSet* _tmp1_;
	g_return_if_fail (self != NULL);
	_data10_ = g_slice_new0 (Block10Data);
	_data10_->_ref_count_ = 1;
	_data10_->self = g_object_ref (self);
	_data10_->media_number = 0;
	_tmp0_ = self->priv->_media_set;
	if (_tmp0_ == NULL) {
		block10_data_unref (_data10_);
		_data10_ = NULL;
		return;
	}
	_tmp1_ = self->priv->_media_set;
	games_media_set_foreach_media (_tmp1_, ___lambda8__games_media_set_media_callback, _data10_);
	block10_data_unref (_data10_);
	_data10_ = NULL;
}


static void __lambda9_ (GamesMediaSelector* self, GtkWidget* child) {
	GtkWidget* _tmp0_;
	g_return_if_fail (child != NULL);
	_tmp0_ = child;
	gtk_widget_destroy (_tmp0_);
}


static void ___lambda9__gtk_callback (GtkWidget* widget, gpointer self) {
	__lambda9_ ((GamesMediaSelector*) self, widget);
}


static void games_media_selector_remove_media (GamesMediaSelector* self) {
	GtkListBox* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->list_box;
	gtk_container_foreach ((GtkContainer*) _tmp0_, ___lambda9__gtk_callback, self);
}


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


static void games_media_selector_on_row_activated (GamesMediaSelector* self, GtkListBoxRow* activated_row) {
	gint media_number = 0;
	GtkListBoxRow* _tmp0_;
	gint _tmp1_;
	GamesMediaSet* _tmp2_;
	gint _tmp3_;
	gint i = 0;
	GtkListBoxRow* row = NULL;
	GtkListBox* _tmp4_;
	gint _tmp5_;
	GtkListBoxRow* _tmp6_;
	GtkListBoxRow* _tmp7_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (activated_row != NULL);
	_tmp0_ = activated_row;
	_tmp1_ = gtk_list_box_row_get_index (_tmp0_);
	media_number = _tmp1_;
	_tmp2_ = self->priv->_media_set;
	_tmp3_ = media_number;
	games_media_set_set_selected_media_number (_tmp2_, _tmp3_);
	i = 0;
	_tmp4_ = self->priv->list_box;
	_tmp5_ = i;
	_tmp6_ = gtk_list_box_get_row_at_index (_tmp4_, _tmp5_);
	_tmp7_ = _g_object_ref0 (_tmp6_);
	row = _tmp7_;
	while (TRUE) {
		GtkListBoxRow* _tmp8_;
		GamesCheckmarkItem* checkmark_item = NULL;
		GtkListBoxRow* _tmp9_;
		GtkWidget* _tmp10_;
		GamesCheckmarkItem* _tmp11_;
		GamesCheckmarkItem* _tmp12_;
		gint _tmp13_;
		gint _tmp14_;
		GtkListBox* _tmp15_;
		gint _tmp16_;
		gint _tmp17_;
		GtkListBoxRow* _tmp18_;
		GtkListBoxRow* _tmp19_;
		_tmp8_ = row;
		if (!(_tmp8_ != NULL)) {
			break;
		}
		_tmp9_ = row;
		_tmp10_ = gtk_bin_get_child ((GtkBin*) _tmp9_);
		_tmp11_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp10_, GAMES_TYPE_CHECKMARK_ITEM, GamesCheckmarkItem));
		checkmark_item = _tmp11_;
		_tmp12_ = checkmark_item;
		_tmp13_ = i;
		_tmp14_ = media_number;
		games_checkmark_item_set_checkmark_visible (_tmp12_, _tmp13_ == _tmp14_);
		_tmp15_ = self->priv->list_box;
		_tmp16_ = i;
		i = _tmp16_ + 1;
		_tmp17_ = i;
		_tmp18_ = gtk_list_box_get_row_at_index (_tmp15_, _tmp17_);
		_tmp19_ = _g_object_ref0 (_tmp18_);
		_g_object_unref0 (row);
		row = _tmp19_;
		_g_object_unref0 (checkmark_item);
	}
	gtk_popover_popdown ((GtkPopover*) self);
	_g_object_unref0 (row);
}


static void _games_media_selector_on_row_activated_gtk_list_box_row_activated (GtkListBox* _sender, GtkListBoxRow* row, gpointer self) {
	games_media_selector_on_row_activated ((GamesMediaSelector*) self, row);
}


GamesMediaSelector* games_media_selector_construct (GType object_type) {
	GamesMediaSelector * self = NULL;
	self = (GamesMediaSelector*) g_object_new (object_type, NULL);
	return self;
}


GamesMediaSelector* games_media_selector_new (void) {
	return games_media_selector_construct (GAMES_TYPE_MEDIA_SELECTOR);
}


void games_media_selector_set_media_set (GamesMediaSelector* self, GamesMediaSet* value) {
	GamesMediaSet* _tmp0_;
	GamesMediaSet* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->_media_set);
	self->priv->_media_set = _tmp1_;
	games_media_selector_remove_media (self);
	games_media_selector_update_media (self);
	g_object_notify ((GObject *) self, "media-set");
}


static void games_media_selector_class_init (GamesMediaSelectorClass * klass) {
	gint GamesMediaSelector_private_offset;
	games_media_selector_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesMediaSelectorPrivate));
	G_OBJECT_CLASS (klass)->set_property = _vala_games_media_selector_set_property;
	G_OBJECT_CLASS (klass)->finalize = games_media_selector_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_MEDIA_SELECTOR_MEDIA_SET_PROPERTY, g_param_spec_object ("media-set", "media-set", "media-set", GAMES_TYPE_MEDIA_SET, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_WRITABLE));
	GamesMediaSelector_private_offset = g_type_class_get_instance_private_offset (klass);
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Games/ui/media-selector.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "list_box", FALSE, GamesMediaSelector_private_offset + G_STRUCT_OFFSET (GamesMediaSelectorPrivate, list_box));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_row_activated", G_CALLBACK(_games_media_selector_on_row_activated_gtk_list_box_row_activated));
}


static void games_media_selector_instance_init (GamesMediaSelector * self) {
	self->priv = GAMES_MEDIA_SELECTOR_GET_PRIVATE (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}


static void games_media_selector_finalize (GObject * obj) {
	GamesMediaSelector * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelector);
	_g_object_unref0 (self->priv->_media_set);
	_g_object_unref0 (self->priv->list_box);
	G_OBJECT_CLASS (games_media_selector_parent_class)->finalize (obj);
}


GType games_media_selector_get_type (void) {
	static volatile gsize games_media_selector_type_id__volatile = 0;
	if (g_once_init_enter (&games_media_selector_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GamesMediaSelectorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_media_selector_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesMediaSelector), 0, (GInstanceInitFunc) games_media_selector_instance_init, NULL };
		GType games_media_selector_type_id;
		games_media_selector_type_id = g_type_register_static (gtk_popover_get_type (), "GamesMediaSelector", &g_define_type_info, 0);
		g_once_init_leave (&games_media_selector_type_id__volatile, games_media_selector_type_id);
	}
	return games_media_selector_type_id__volatile;
}


static void _vala_games_media_selector_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	GamesMediaSelector * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelector);
	switch (property_id) {
		case GAMES_MEDIA_SELECTOR_MEDIA_SET_PROPERTY:
		games_media_selector_set_media_set (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



