/* generic-uri-game-factory.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from generic-uri-game-factory.vala, do not modify */

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

#include "gnome-games.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.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  {
	GAMES_GENERIC_URI_GAME_FACTORY_0_PROPERTY,
	GAMES_GENERIC_URI_GAME_FACTORY_NUM_PROPERTIES
};
static GParamSpec* games_generic_uri_game_factory_properties[GAMES_GENERIC_URI_GAME_FACTORY_NUM_PROPERTIES];
#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)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _GamesGenericUriGameFactoryPrivate {
	GamesGameUriAdapter* game_uri_adapter;
	GHashTable* game_for_uri;
	gchar** mime_types;
	gint mime_types_length1;
	gint _mime_types_size_;
	gchar** schemes;
	gint schemes_length1;
	gint _schemes_size_;
	GamesGameCallback game_added_callback;
	gpointer game_added_callback_target;
};

static gint GamesGenericUriGameFactory_private_offset;
static gpointer games_generic_uri_game_factory_parent_class = NULL;
static GamesUriGameFactoryIface * games_generic_uri_game_factory_games_uri_game_factory_parent_iface = NULL;

static guint _games_uri_hash_ghash_func (gconstpointer key);
static gboolean _games_uri_equal_gequal_func (gconstpointer a,
                                       gconstpointer b);
static void _games_uri_unref0_ (gpointer var);
static void _g_object_unref0_ (gpointer var);
static gchar** games_generic_uri_game_factory_real_get_mime_types (GamesUriGameFactory* base,
                                                            gint* result_length1);
static gchar** _vala_array_dup11 (gchar** self,
                           gssize length);
static void _vala_array_add22 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static gchar** games_generic_uri_game_factory_real_get_schemes (GamesUriGameFactory* base,
                                                         gint* result_length1);
static gchar** _vala_array_dup12 (gchar** self,
                           gssize length);
static void _vala_array_add23 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void games_generic_uri_game_factory_real_add_uri (GamesUriGameFactory* base,
                                                  GamesUri* uri);
static GamesGame* games_generic_uri_game_factory_real_query_game_for_uri (GamesUriGameFactory* base,
                                                                   GamesUri* uri);
static void games_generic_uri_game_factory_real_foreach_game (GamesUriGameFactory* base,
                                                       GamesGameCallback game_callback,
                                                       gpointer game_callback_target);
static void games_generic_uri_game_factory_real_set_game_added_callback (GamesUriGameFactory* base,
                                                                  GamesGameCallback game_callback,
                                                                  gpointer game_callback_target);
static void games_generic_uri_game_factory_finalize (GObject * obj);
static GType games_generic_uri_game_factory_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
games_generic_uri_game_factory_get_instance_private (GamesGenericUriGameFactory* self)
{
	return G_STRUCT_MEMBER_P (self, GamesGenericUriGameFactory_private_offset);
}

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

static guint
_games_uri_hash_ghash_func (gconstpointer key)
{
	guint result;
	result = games_uri_hash ((GamesUri*) key);
	return result;
}

static gboolean
_games_uri_equal_gequal_func (gconstpointer a,
                              gconstpointer b)
{
	gboolean result;
	result = games_uri_equal ((GamesUri*) a, (GamesUri*) b);
	return result;
}

static void
_games_uri_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (games_uri_unref (var), NULL));
}

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

GamesGenericUriGameFactory*
games_generic_uri_game_factory_construct (GType object_type,
                                          GamesGameUriAdapter* game_uri_adapter)
{
	GamesGenericUriGameFactory * self = NULL;
	GamesGameUriAdapter* _tmp0_;
	GHashTable* _tmp1_;
	gchar** _tmp2_;
	gchar** _tmp3_;
	g_return_val_if_fail (game_uri_adapter != NULL, NULL);
	self = (GamesGenericUriGameFactory*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (game_uri_adapter);
	_g_object_unref0 (self->priv->game_uri_adapter);
	self->priv->game_uri_adapter = _tmp0_;
	_tmp1_ = g_hash_table_new_full (_games_uri_hash_ghash_func, _games_uri_equal_gequal_func, _games_uri_unref0_, _g_object_unref0_);
	_g_hash_table_unref0 (self->priv->game_for_uri);
	self->priv->game_for_uri = _tmp1_;
	_tmp2_ = g_new0 (gchar*, 0 + 1);
	self->priv->mime_types = (_vala_array_free (self->priv->mime_types, self->priv->mime_types_length1, (GDestroyNotify) g_free), NULL);
	self->priv->mime_types = _tmp2_;
	self->priv->mime_types_length1 = 0;
	self->priv->_mime_types_size_ = self->priv->mime_types_length1;
	_tmp3_ = g_new0 (gchar*, 0 + 1);
	self->priv->schemes = (_vala_array_free (self->priv->schemes, self->priv->schemes_length1, (GDestroyNotify) g_free), NULL);
	self->priv->schemes = _tmp3_;
	self->priv->schemes_length1 = 0;
	self->priv->_schemes_size_ = self->priv->schemes_length1;
	return self;
}

GamesGenericUriGameFactory*
games_generic_uri_game_factory_new (GamesGameUriAdapter* game_uri_adapter)
{
	return games_generic_uri_game_factory_construct (GAMES_TYPE_GENERIC_URI_GAME_FACTORY, game_uri_adapter);
}

static gchar**
_vala_array_dup11 (gchar** self,
                   gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

static gchar**
games_generic_uri_game_factory_real_get_mime_types (GamesUriGameFactory* base,
                                                    gint* result_length1)
{
	GamesGenericUriGameFactory * self;
	gchar** _tmp0_;
	gint _tmp0__length1;
	gchar** _tmp1_;
	gint _tmp1__length1;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gchar** result;
	self = (GamesGenericUriGameFactory*) base;
	_tmp0_ = self->priv->mime_types;
	_tmp0__length1 = self->priv->mime_types_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup11 (_tmp0_, _tmp0__length1) : _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	_tmp2_ = _tmp1_;
	_tmp2__length1 = _tmp1__length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static void
_vala_array_add22 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

void
games_generic_uri_game_factory_add_mime_type (GamesGenericUriGameFactory* self,
                                              const gchar* mime_type)
{
	gchar* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (mime_type != NULL);
	_tmp0_ = g_strdup (mime_type);
	_vala_array_add22 (&self->priv->mime_types, &self->priv->mime_types_length1, &self->priv->_mime_types_size_, _tmp0_);
}

static gchar**
_vala_array_dup12 (gchar** self,
                   gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

static gchar**
games_generic_uri_game_factory_real_get_schemes (GamesUriGameFactory* base,
                                                 gint* result_length1)
{
	GamesGenericUriGameFactory * self;
	gchar** _tmp0_;
	gint _tmp0__length1;
	gchar** _tmp1_;
	gint _tmp1__length1;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gchar** result;
	self = (GamesGenericUriGameFactory*) base;
	_tmp0_ = self->priv->schemes;
	_tmp0__length1 = self->priv->schemes_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup12 (_tmp0_, _tmp0__length1) : _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	_tmp2_ = _tmp1_;
	_tmp2__length1 = _tmp1__length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static void
_vala_array_add23 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

void
games_generic_uri_game_factory_add_scheme (GamesGenericUriGameFactory* self,
                                           const gchar* scheme)
{
	gchar* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (scheme != NULL);
	_tmp0_ = g_strdup (scheme);
	_vala_array_add23 (&self->priv->schemes, &self->priv->schemes_length1, &self->priv->_schemes_size_, _tmp0_);
}

static gpointer
_games_uri_ref0 (gpointer self)
{
	return self ? games_uri_ref (self) : NULL;
}

static void
games_generic_uri_game_factory_real_add_uri (GamesUriGameFactory* base,
                                             GamesUri* uri)
{
	GamesGenericUriGameFactory * self;
	GHashTable* _tmp0_;
	GError* _inner_error0_ = NULL;
	self = (GamesGenericUriGameFactory*) base;
	g_return_if_fail (uri != NULL);
	_tmp0_ = self->priv->game_for_uri;
	if (g_hash_table_contains (_tmp0_, uri)) {
		return;
	}
	{
		GamesGame* game = NULL;
		GamesGameUriAdapter* _tmp1_;
		GamesGame* _tmp2_;
		GHashTable* _tmp3_;
		GamesUri* _tmp4_;
		GamesGame* _tmp5_;
		GamesGame* _tmp6_;
		GamesGameCallback _tmp7_;
		gpointer _tmp7__target;
		_tmp1_ = self->priv->game_uri_adapter;
		_tmp2_ = games_game_uri_adapter_game_for_uri (_tmp1_, uri, &_inner_error0_);
		game = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = self->priv->game_for_uri;
		_tmp4_ = _games_uri_ref0 (uri);
		_tmp5_ = game;
		_tmp6_ = _g_object_ref0 (_tmp5_);
		g_hash_table_insert (_tmp3_, _tmp4_, _tmp6_);
		_tmp7_ = self->priv->game_added_callback;
		_tmp7__target = self->priv->game_added_callback_target;
		if (_tmp7_ != NULL) {
			GamesGameCallback _tmp8_;
			gpointer _tmp8__target;
			GamesGame* _tmp9_;
			_tmp8_ = self->priv->game_added_callback;
			_tmp8__target = self->priv->game_added_callback_target;
			_tmp9_ = game;
			_tmp8_ (_tmp9_, _tmp8__target);
		}
		_g_object_unref0 (game);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp10_;
		const gchar* _tmp11_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp10_ = e;
		_tmp11_ = _tmp10_->message;
		g_debug ("generic-uri-game-factory.vala:45: %s", _tmp11_);
		_g_error_free0 (e);
	}
	__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 GamesGame*
games_generic_uri_game_factory_real_query_game_for_uri (GamesUriGameFactory* base,
                                                        GamesUri* uri)
{
	GamesGenericUriGameFactory * self;
	GHashTable* _tmp0_;
	GamesGame* result;
	self = (GamesGenericUriGameFactory*) base;
	g_return_val_if_fail (uri != NULL, NULL);
	_tmp0_ = self->priv->game_for_uri;
	if (g_hash_table_contains (_tmp0_, uri)) {
		GHashTable* _tmp1_;
		gconstpointer _tmp2_;
		GamesGame* _tmp3_;
		_tmp1_ = self->priv->game_for_uri;
		_tmp2_ = g_hash_table_lookup (_tmp1_, uri);
		_tmp3_ = _g_object_ref0 ((GamesGame*) _tmp2_);
		result = _tmp3_;
		return result;
	}
	result = NULL;
	return result;
}

static void
games_generic_uri_game_factory_real_foreach_game (GamesUriGameFactory* base,
                                                  GamesGameCallback game_callback,
                                                  gpointer game_callback_target)
{
	GamesGenericUriGameFactory * self;
	GList* games = NULL;
	GHashTable* _tmp0_;
	GList* _tmp1_;
	GList* _tmp2_;
	self = (GamesGenericUriGameFactory*) base;
	_tmp0_ = self->priv->game_for_uri;
	_tmp1_ = g_hash_table_get_values (_tmp0_);
	games = _tmp1_;
	_tmp2_ = games;
	{
		GList* game_collection = NULL;
		GList* game_it = NULL;
		game_collection = _tmp2_;
		for (game_it = game_collection; game_it != NULL; game_it = game_it->next) {
			GamesGame* game = NULL;
			game = (GamesGame*) game_it->data;
			{
				GamesGame* _tmp3_;
				_tmp3_ = game;
				game_callback (_tmp3_, game_callback_target);
			}
		}
	}
	(games == NULL) ? NULL : (games = (g_list_free (games), NULL));
}

static void
games_generic_uri_game_factory_real_set_game_added_callback (GamesUriGameFactory* base,
                                                             GamesGameCallback game_callback,
                                                             gpointer game_callback_target)
{
	GamesGenericUriGameFactory * self;
	self = (GamesGenericUriGameFactory*) base;
	self->priv->game_added_callback = game_callback;
	self->priv->game_added_callback_target = game_callback_target;
}

static void
games_generic_uri_game_factory_class_init (GamesGenericUriGameFactoryClass * klass,
                                           gpointer klass_data)
{
	games_generic_uri_game_factory_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesGenericUriGameFactory_private_offset);
	G_OBJECT_CLASS (klass)->finalize = games_generic_uri_game_factory_finalize;
}

static void
games_generic_uri_game_factory_games_uri_game_factory_interface_init (GamesUriGameFactoryIface * iface,
                                                                      gpointer iface_data)
{
	games_generic_uri_game_factory_games_uri_game_factory_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_mime_types = (gchar** (*) (GamesUriGameFactory*, gint*)) games_generic_uri_game_factory_real_get_mime_types;
	iface->get_schemes = (gchar** (*) (GamesUriGameFactory*, gint*)) games_generic_uri_game_factory_real_get_schemes;
	iface->add_uri = (void (*) (GamesUriGameFactory*, GamesUri*)) games_generic_uri_game_factory_real_add_uri;
	iface->query_game_for_uri = (GamesGame* (*) (GamesUriGameFactory*, GamesUri*)) games_generic_uri_game_factory_real_query_game_for_uri;
	iface->foreach_game = (void (*) (GamesUriGameFactory*, GamesGameCallback, gpointer)) games_generic_uri_game_factory_real_foreach_game;
	iface->set_game_added_callback = (void (*) (GamesUriGameFactory*, GamesGameCallback, gpointer)) games_generic_uri_game_factory_real_set_game_added_callback;
}

static void
games_generic_uri_game_factory_instance_init (GamesGenericUriGameFactory * self,
                                              gpointer klass)
{
	self->priv = games_generic_uri_game_factory_get_instance_private (self);
}

static void
games_generic_uri_game_factory_finalize (GObject * obj)
{
	GamesGenericUriGameFactory * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_GENERIC_URI_GAME_FACTORY, GamesGenericUriGameFactory);
	_g_object_unref0 (self->priv->game_uri_adapter);
	_g_hash_table_unref0 (self->priv->game_for_uri);
	self->priv->mime_types = (_vala_array_free (self->priv->mime_types, self->priv->mime_types_length1, (GDestroyNotify) g_free), NULL);
	self->priv->schemes = (_vala_array_free (self->priv->schemes, self->priv->schemes_length1, (GDestroyNotify) g_free), NULL);
	G_OBJECT_CLASS (games_generic_uri_game_factory_parent_class)->finalize (obj);
}

static GType
games_generic_uri_game_factory_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesGenericUriGameFactoryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_generic_uri_game_factory_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesGenericUriGameFactory), 0, (GInstanceInitFunc) games_generic_uri_game_factory_instance_init, NULL };
	static const GInterfaceInfo games_uri_game_factory_info = { (GInterfaceInitFunc) games_generic_uri_game_factory_games_uri_game_factory_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType games_generic_uri_game_factory_type_id;
	games_generic_uri_game_factory_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesGenericUriGameFactory", &g_define_type_info, 0);
	g_type_add_interface_static (games_generic_uri_game_factory_type_id, GAMES_TYPE_URI_GAME_FACTORY, &games_uri_game_factory_info);
	GamesGenericUriGameFactory_private_offset = g_type_add_instance_private (games_generic_uri_game_factory_type_id, sizeof (GamesGenericUriGameFactoryPrivate));
	return games_generic_uri_game_factory_type_id;
}

GType
games_generic_uri_game_factory_get_type (void)
{
	static volatile gsize games_generic_uri_game_factory_type_id__once = 0;
	if (g_once_init_enter (&games_generic_uri_game_factory_type_id__once)) {
		GType games_generic_uri_game_factory_type_id;
		games_generic_uri_game_factory_type_id = games_generic_uri_game_factory_get_type_once ();
		g_once_init_leave (&games_generic_uri_game_factory_type_id__once, games_generic_uri_game_factory_type_id);
	}
	return games_generic_uri_game_factory_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

