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

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

#include "gnome-games.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <grilo.h>
#include <glib-object.h>

#define GAMES_GRILO_MEDIA_SOURCE_NAME "grl-thegamesdb"
#define GAMES_GRILO_MEDIA_SOURCE_TAG "games"
#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_GRILO_MEDIA_0_PROPERTY,
	GAMES_GRILO_MEDIA_NUM_PROPERTIES
};
static GParamSpec* games_grilo_media_properties[GAMES_GRILO_MEDIA_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)))
enum  {
	GAMES_GRILO_MEDIA_RESOLVED_SIGNAL,
	GAMES_GRILO_MEDIA_NUM_SIGNALS
};
static guint games_grilo_media_signals[GAMES_GRILO_MEDIA_NUM_SIGNALS] = {0};

struct _GamesGriloMediaPrivate {
	GamesTitle* title;
	gchar* mime_type;
	gboolean resolving;
	GrlMedia* media;
};

static gint GamesGriloMedia_private_offset;
static gpointer games_grilo_media_parent_class = NULL;
static GrlRegistry* games_grilo_media_registry;
static GrlRegistry* games_grilo_media_registry = NULL;

static GrlRegistry* games_grilo_media_get_registry (GError** error);
static void __lambda70_ (GrlSource* entry);
static gchar** _vala_array_dup13 (gchar** self,
                           gssize length);
static gboolean _vala_string_array_contains (gchar* * stack,
                                      gssize stack_length,
                                      const gchar* needle);
static void ___lambda70__gfunc (gconstpointer data,
                         gpointer self);
static void games_grilo_media_resolve_media (GamesGriloMedia* self,
                                      GError** error);
VALA_EXTERN GrlMedia* games_grilo_media_get_media (GamesGriloMedia* self);
static void games_grilo_media_on_media_resolved (GamesGriloMedia* self,
                                          GrlSource* source,
                                          guint operation_id,
                                          GrlMedia* media,
                                          GError* _error_);
static void _games_grilo_media_on_media_resolved_grl_source_resolve_cb (GrlSource* source,
                                                                 guint operation_id,
                                                                 GrlMedia* media,
                                                                 gpointer self,
                                                                 GError* _error_);
static void games_grilo_media_finalize (GObject * obj);
static GType games_grilo_media_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 gssize _vala_array_length (gpointer array);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
games_grilo_media_get_instance_private (GamesGriloMedia* self)
{
	return G_STRUCT_MEMBER_P (self, GamesGriloMedia_private_offset);
}

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

GamesGriloMedia*
games_grilo_media_construct (GType object_type,
                             GamesTitle* title,
                             const gchar* mime_type)
{
	GamesGriloMedia * self = NULL;
	GamesTitle* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (title != NULL, NULL);
	g_return_val_if_fail (mime_type != NULL, NULL);
	self = (GamesGriloMedia*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (title);
	_g_object_unref0 (self->priv->title);
	self->priv->title = _tmp0_;
	_tmp1_ = g_strdup (mime_type);
	_g_free0 (self->priv->mime_type);
	self->priv->mime_type = _tmp1_;
	self->priv->resolving = FALSE;
	return self;
}

GamesGriloMedia*
games_grilo_media_new (GamesTitle* title,
                       const gchar* mime_type)
{
	return games_grilo_media_construct (GAMES_TYPE_GRILO_MEDIA, title, mime_type);
}

static gchar**
_vala_array_dup13 (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 gboolean
_vala_string_array_contains (gchar* * stack,
                             gssize stack_length,
                             const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
__lambda70_ (GrlSource* entry)
{
	gchar** tags = NULL;
	gchar** _tmp0_;
	gchar** _tmp1_;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gint tags_length1;
	gint _tags_size_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (entry != NULL);
	_tmp1_ = _tmp0_ = grl_source_get_tags (entry);
	_tmp2_ = (_tmp1_ != NULL) ? _vala_array_dup13 (_tmp1_, _vala_array_length (_tmp0_)) : _tmp1_;
	_tmp2__length1 = _vala_array_length (_tmp0_);
	tags = _tmp2_;
	tags_length1 = _tmp2__length1;
	_tags_size_ = tags_length1;
	_tmp3_ = tags;
	_tmp3__length1 = tags_length1;
	if (!_vala_string_array_contains (_tmp3_, _tmp3__length1, GAMES_GRILO_MEDIA_SOURCE_TAG)) {
		{
			GrlRegistry* _tmp4_;
			_tmp4_ = games_grilo_media_registry;
			grl_registry_unregister_source (_tmp4_, entry, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			GError* _tmp5_;
			const gchar* _tmp6_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp5_ = e;
			_tmp6_ = _tmp5_->message;
			g_critical ("grilo-media.vala:38: %s", _tmp6_);
			_g_error_free0 (e);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			tags = (_vala_array_free (tags, tags_length1, (GDestroyNotify) g_free), 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;
		}
	}
	tags = (_vala_array_free (tags, tags_length1, (GDestroyNotify) g_free), NULL);
}

static void
___lambda70__gfunc (gconstpointer data,
                    gpointer self)
{
	__lambda70_ ((GrlSource*) data);
}

static GrlRegistry*
games_grilo_media_get_registry (GError** error)
{
	GrlRegistry* _tmp0_;
	GrlRegistry* _tmp3_;
	GrlRegistry* _tmp4_;
	GrlRegistry* _tmp5_;
	GList* source_list = NULL;
	GrlRegistry* _tmp6_;
	GList* _tmp7_;
	GList* _tmp8_;
	GrlRegistry* _tmp9_;
	GrlRegistry* _tmp10_;
	GError* _inner_error0_ = NULL;
	GrlRegistry* result;
	_tmp0_ = games_grilo_media_registry;
	if (_tmp0_ != NULL) {
		GrlRegistry* _tmp1_;
		GrlRegistry* _tmp2_;
		_tmp1_ = games_grilo_media_registry;
		_tmp2_ = _g_object_ref0 (_tmp1_);
		result = _tmp2_;
		return result;
	}
	_tmp3_ = grl_registry_get_default ();
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_g_object_unref0 (games_grilo_media_registry);
	games_grilo_media_registry = _tmp4_;
	_tmp5_ = games_grilo_media_registry;
	grl_registry_load_all_plugins (_tmp5_, TRUE, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return NULL;
	}
	_tmp6_ = games_grilo_media_registry;
	_tmp7_ = grl_registry_get_sources (_tmp6_, TRUE);
	source_list = _tmp7_;
	_tmp8_ = source_list;
	g_list_foreach (_tmp8_, ___lambda70__gfunc, NULL);
	_tmp9_ = games_grilo_media_registry;
	_tmp10_ = _g_object_ref0 (_tmp9_);
	result = _tmp10_;
	(source_list == NULL) ? NULL : (source_list = (g_list_free (source_list), NULL));
	return result;
}

void
games_grilo_media_try_resolve_media (GamesGriloMedia* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		games_grilo_media_resolve_media (self, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp0_;
		const gchar* _tmp1_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp0_ = e;
		_tmp1_ = _tmp0_->message;
		g_warning ("grilo-media.vala:50: %s", _tmp1_);
		_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;
	}
}

GrlMedia*
games_grilo_media_get_media (GamesGriloMedia* self)
{
	GrlMedia* _tmp0_;
	GrlMedia* _tmp1_;
	GrlMedia* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->media;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
_games_grilo_media_on_media_resolved_grl_source_resolve_cb (GrlSource* source,
                                                            guint operation_id,
                                                            GrlMedia* media,
                                                            gpointer self,
                                                            GError* _error_)
{
	games_grilo_media_on_media_resolved ((GamesGriloMedia*) self, source, operation_id, media, _error_);
}

static void
games_grilo_media_resolve_media (GamesGriloMedia* self,
                                 GError** error)
{
	GrlRegistry* registry = NULL;
	GrlRegistry* _tmp0_;
	GrlSource* source = NULL;
	GrlRegistry* _tmp1_;
	GrlSource* _tmp2_;
	GrlSource* _tmp3_;
	GrlSource* _tmp4_;
	GrlMedia* base_media = NULL;
	GrlMedia* _tmp5_;
	gchar* title_string = NULL;
	GamesTitle* _tmp6_;
	gchar* _tmp7_;
	GrlMedia* _tmp8_;
	const gchar* _tmp9_;
	GrlMedia* _tmp10_;
	const gchar* _tmp11_;
	GList* keys = NULL;
	GList* _tmp12_;
	GrlOperationOptions* options = NULL;
	GrlOperationOptions* _tmp13_;
	GrlOperationOptions* _tmp14_;
	GrlSource* _tmp15_;
	GrlMedia* _tmp16_;
	GrlMedia* _tmp17_;
	GList* _tmp18_;
	GrlOperationOptions* _tmp19_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	if (self->priv->resolving) {
		return;
	}
	self->priv->resolving = TRUE;
	_tmp0_ = games_grilo_media_get_registry (&_inner_error0_);
	registry = _tmp0_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp1_ = registry;
	_tmp2_ = grl_registry_lookup_source (_tmp1_, GAMES_GRILO_MEDIA_SOURCE_NAME);
	_tmp3_ = _g_object_ref0 (_tmp2_);
	source = _tmp3_;
	_tmp4_ = source;
	if (_tmp4_ == NULL) {
		_g_object_unref0 (source);
		_g_object_unref0 (registry);
		return;
	}
	_tmp5_ = grl_media_new ();
	base_media = _tmp5_;
	_tmp6_ = self->priv->title;
	_tmp7_ = games_title_get_title (_tmp6_, &_inner_error0_);
	title_string = _tmp7_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (base_media);
		_g_object_unref0 (source);
		_g_object_unref0 (registry);
		return;
	}
	_tmp8_ = base_media;
	_tmp9_ = title_string;
	grl_media_set_title (_tmp8_, _tmp9_);
	_tmp10_ = base_media;
	_tmp11_ = self->priv->mime_type;
	grl_media_set_mime (_tmp10_, _tmp11_);
	_tmp12_ = grl_metadata_key_list_new (GRL_METADATA_KEY_THUMBNAIL, GRL_METADATA_KEY_INVALID, NULL);
	keys = _tmp12_;
	_tmp13_ = grl_operation_options_new (NULL);
	options = _tmp13_;
	_tmp14_ = options;
	grl_operation_options_set_resolution_flags (_tmp14_, GRL_RESOLVE_FULL);
	_tmp15_ = source;
	_tmp16_ = base_media;
	_tmp17_ = _g_object_ref0 (_tmp16_);
	_tmp18_ = keys;
	_tmp19_ = options;
	grl_source_resolve (_tmp15_, _tmp17_, _tmp18_, _tmp19_, _games_grilo_media_on_media_resolved_grl_source_resolve_cb, self);
	_g_object_unref0 (options);
	(keys == NULL) ? NULL : (keys = (g_list_free (keys), NULL));
	_g_free0 (title_string);
	_g_object_unref0 (base_media);
	_g_object_unref0 (source);
	_g_object_unref0 (registry);
}

static void
games_grilo_media_on_media_resolved (GamesGriloMedia* self,
                                     GrlSource* source,
                                     guint operation_id,
                                     GrlMedia* media,
                                     GError* _error_)
{
	GrlMedia* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (source != NULL);
	g_return_if_fail (media != NULL);
	_tmp0_ = _g_object_ref0 (media);
	_g_object_unref0 (self->priv->media);
	self->priv->media = _tmp0_;
	g_signal_emit (self, games_grilo_media_signals[GAMES_GRILO_MEDIA_RESOLVED_SIGNAL], 0);
	_g_object_unref0 (media);
}

static void
games_grilo_media_class_init (GamesGriloMediaClass * klass,
                              gpointer klass_data)
{
	games_grilo_media_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesGriloMedia_private_offset);
	G_OBJECT_CLASS (klass)->finalize = games_grilo_media_finalize;
	games_grilo_media_signals[GAMES_GRILO_MEDIA_RESOLVED_SIGNAL] = g_signal_new ("resolved", GAMES_TYPE_GRILO_MEDIA, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
games_grilo_media_instance_init (GamesGriloMedia * self,
                                 gpointer klass)
{
	self->priv = games_grilo_media_get_instance_private (self);
}

static void
games_grilo_media_finalize (GObject * obj)
{
	GamesGriloMedia * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_GRILO_MEDIA, GamesGriloMedia);
	_g_object_unref0 (self->priv->title);
	_g_free0 (self->priv->mime_type);
	_g_object_unref0 (self->priv->media);
	G_OBJECT_CLASS (games_grilo_media_parent_class)->finalize (obj);
}

static GType
games_grilo_media_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesGriloMediaClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_grilo_media_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesGriloMedia), 0, (GInstanceInitFunc) games_grilo_media_instance_init, NULL };
	GType games_grilo_media_type_id;
	games_grilo_media_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesGriloMedia", &g_define_type_info, 0);
	GamesGriloMedia_private_offset = g_type_add_instance_private (games_grilo_media_type_id, sizeof (GamesGriloMediaPrivate));
	return games_grilo_media_type_id;
}

GType
games_grilo_media_get_type (void)
{
	static volatile gsize games_grilo_media_type_id__once = 0;
	if (g_once_init_enter (&games_grilo_media_type_id__once)) {
		GType games_grilo_media_type_id;
		games_grilo_media_type_id = games_grilo_media_get_type_once ();
		g_once_init_leave (&games_grilo_media_type_id__once, games_grilo_media_type_id);
	}
	return games_grilo_media_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 gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

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

