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

/* This file is part of GNOME Games. License: GPLv3*/

#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)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_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)))

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

struct _GamesMediaSelectorClass {
	GtkPopoverClass parent_class;
};

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

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_DUMMY_PROPERTY,
	GAMES_MEDIA_SELECTOR_MEDIA_SET
};
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 __lambda7_ (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);
gint games_media_set_get_selected_media_number (GamesMediaSet* self);
static void games_media_selector_add_row (GamesMediaSelector* self, GtkListBoxRow* row);
static void ___lambda7__games_media_set_media_callback (GamesMedia* media, gpointer self);
static void games_media_selector_remove_media (GamesMediaSelector* self);
static void __lambda8_ (GamesMediaSelector* self, GtkWidget* child);
static void ___lambda8__gtk_callback (GtkWidget* widget, gpointer self);
static void games_media_selector_on_row_selected (GamesMediaSelector* self, GtkListBoxRow* row);
void games_media_set_set_selected_media_number (GamesMediaSet* self, gint value);
static void _games_media_selector_on_row_selected_gtk_list_box_row_selected (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 GObject * games_media_selector_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void _g_object_unref0_ (gpointer var);
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 gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void __lambda7_ (Block10Data* _data10_, GamesMedia* media) {
	GamesMediaSelector* self;
	gchar* media_name = NULL;
	GamesMedia* _tmp0_;
	GamesTitle* _tmp1_;
	GamesTitle* _tmp2_;
	GtkListBoxRow* row;
	GtkListBoxRow* _tmp14_;
	GtkLabel* label;
	const gchar* _tmp15_;
	GtkLabel* _tmp16_;
	GtkListBoxRow* _tmp17_;
	GtkLabel* _tmp18_;
	gint _tmp19_;
	GamesMediaSet* _tmp20_;
	gint _tmp21_;
	gint _tmp22_;
	GHashTable* _tmp25_;
	GtkListBoxRow* _tmp26_;
	GtkListBoxRow* _tmp27_;
	gint _tmp28_;
	GtkListBoxRow* _tmp29_;
	gint _tmp30_;
	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_;
			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 __catch37_g_error;
			}
			_tmp10_ = _tmp5_;
			_tmp5_ = NULL;
			_g_free0 (media_name);
			media_name = _tmp10_;
			_g_free0 (_tmp5_);
		}
		goto __finally37;
		__catch37_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:39: %s", _tmp12_);
			_tmp13_ = g_strdup ("");
			_g_free0 (media_name);
			media_name = _tmp13_;
			_g_error_free0 (e);
		}
		__finally37:
		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_ = (GtkListBoxRow*) gtk_list_box_row_new ();
	g_object_ref_sink (_tmp14_);
	row = _tmp14_;
	_tmp15_ = media_name;
	_tmp16_ = (GtkLabel*) gtk_label_new (_tmp15_);
	g_object_ref_sink (_tmp16_);
	label = _tmp16_;
	_tmp17_ = row;
	_tmp18_ = label;
	gtk_container_add ((GtkContainer*) _tmp17_, (GtkWidget*) _tmp18_);
	_tmp19_ = _data10_->media_number;
	_tmp20_ = self->priv->_media_set;
	_tmp21_ = games_media_set_get_selected_media_number (_tmp20_);
	_tmp22_ = _tmp21_;
	if (_tmp19_ == _tmp22_) {
		GtkListBox* _tmp23_;
		GtkListBoxRow* _tmp24_;
		_tmp23_ = self->priv->list_box;
		_tmp24_ = row;
		gtk_list_box_select_row (_tmp23_, _tmp24_);
	}
	_tmp25_ = self->priv->row_to_int;
	_tmp26_ = row;
	_tmp27_ = _g_object_ref0 (_tmp26_);
	_tmp28_ = _data10_->media_number;
	g_hash_table_insert (_tmp25_, _tmp27_, (gpointer) ((gintptr) _tmp28_));
	_tmp29_ = row;
	games_media_selector_add_row (self, _tmp29_);
	_tmp30_ = _data10_->media_number;
	_data10_->media_number = _tmp30_ + 1;
	_g_object_unref0 (label);
	_g_object_unref0 (row);
	_g_free0 (media_name);
}


static void ___lambda7__games_media_set_media_callback (GamesMedia* media, gpointer self) {
	__lambda7_ (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_, ___lambda7__games_media_set_media_callback, _data10_);
	block10_data_unref (_data10_);
	_data10_ = NULL;
}


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


static void ___lambda8__gtk_callback (GtkWidget* widget, gpointer self) {
	__lambda8_ ((GamesMediaSelector*) self, widget);
}


static void games_media_selector_remove_media (GamesMediaSelector* self) {
	GHashTable* _tmp0_;
	GtkListBox* _tmp1_;
	GtkListBox* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->row_to_int;
	g_hash_table_remove_all (_tmp0_);
	_tmp1_ = self->priv->list_box;
	gtk_list_box_unselect_all (_tmp1_);
	_tmp2_ = self->priv->list_box;
	gtk_container_foreach ((GtkContainer*) _tmp2_, ___lambda8__gtk_callback, self);
}


static void games_media_selector_add_row (GamesMediaSelector* self, GtkListBoxRow* row) {
	GtkListBox* _tmp0_;
	GtkListBoxRow* _tmp1_;
	GtkListBox* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (row != NULL);
	_tmp0_ = self->priv->list_box;
	_tmp1_ = row;
	gtk_list_box_insert (_tmp0_, (GtkWidget*) _tmp1_, -1);
	_tmp2_ = self->priv->list_box;
	gtk_widget_show_all ((GtkWidget*) _tmp2_);
}


static void games_media_selector_on_row_selected (GamesMediaSelector* self, GtkListBoxRow* row) {
	GtkListBoxRow* _tmp0_;
	GHashTable* _tmp1_;
	GtkListBoxRow* _tmp2_;
	gboolean _tmp3_;
	gint media_number;
	GHashTable* _tmp4_;
	GtkListBoxRow* _tmp5_;
	gconstpointer _tmp6_;
	gint _tmp7_;
	GamesMediaSet* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	GamesMediaSet* _tmp11_;
	gint _tmp12_;
	g_return_if_fail (self != NULL);
	_tmp0_ = row;
	if (_tmp0_ == NULL) {
		return;
	}
	_tmp1_ = self->priv->row_to_int;
	_tmp2_ = row;
	_tmp3_ = g_hash_table_contains (_tmp1_, _tmp2_);
	if (!_tmp3_) {
		return;
	}
	_tmp4_ = self->priv->row_to_int;
	_tmp5_ = row;
	_tmp6_ = g_hash_table_lookup (_tmp4_, _tmp5_);
	media_number = (gint) ((gintptr) _tmp6_);
	_tmp7_ = media_number;
	_tmp8_ = self->priv->_media_set;
	_tmp9_ = games_media_set_get_selected_media_number (_tmp8_);
	_tmp10_ = _tmp9_;
	if (_tmp7_ == _tmp10_) {
		return;
	}
	_tmp11_ = self->priv->_media_set;
	_tmp12_ = media_number;
	games_media_set_set_selected_media_number (_tmp11_, _tmp12_);
	gtk_widget_hide ((GtkWidget*) self);
}


static void _games_media_selector_on_row_selected_gtk_list_box_row_selected (GtkListBox* _sender, GtkListBoxRow* row, gpointer self) {
	games_media_selector_on_row_selected ((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 _g_object_unref0_ (gpointer var) {
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}


static GObject * games_media_selector_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	GamesMediaSelector * self;
	GHashFunc _tmp0_;
	GEqualFunc _tmp1_;
	GHashTable* _tmp2_;
	parent_class = G_OBJECT_CLASS (games_media_selector_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_MEDIA_SELECTOR, GamesMediaSelector);
	_tmp0_ = g_direct_hash;
	_tmp1_ = g_direct_equal;
	_tmp2_ = g_hash_table_new_full (_tmp0_, _tmp1_, _g_object_unref0_, NULL);
	_g_hash_table_unref0 (self->priv->row_to_int);
	self->priv->row_to_int = _tmp2_;
	return obj;
}


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)->constructor = games_media_selector_constructor;
	G_OBJECT_CLASS (klass)->finalize = games_media_selector_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_MEDIA_SELECTOR_MEDIA_SET, 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_selected", G_CALLBACK(_games_media_selector_on_row_selected_gtk_list_box_row_selected));
}


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_hash_table_unref0 (self->priv->row_to_int);
	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:
		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;
	}
}



