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

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

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


#define GAMES_TYPE_GRILO_MEDIA (games_grilo_media_get_type ())
#define GAMES_GRILO_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GRILO_MEDIA, GamesGriloMedia))
#define GAMES_GRILO_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GRILO_MEDIA, GamesGriloMediaClass))
#define GAMES_IS_GRILO_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GRILO_MEDIA))
#define GAMES_IS_GRILO_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GRILO_MEDIA))
#define GAMES_GRILO_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GRILO_MEDIA, GamesGriloMediaClass))

typedef struct _GamesGriloMedia GamesGriloMedia;
typedef struct _GamesGriloMediaClass GamesGriloMediaClass;
typedef struct _GamesGriloMediaPrivate GamesGriloMediaPrivate;

#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_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)))
#define _g_list_free0(var) ((var == NULL) ? NULL : (var = (g_list_free (var), NULL)))
enum  {
	GAMES_GRILO_MEDIA_RESOLVED_SIGNAL,
	GAMES_GRILO_MEDIA_LAST_SIGNAL
};
static guint games_grilo_media_signals[GAMES_GRILO_MEDIA_LAST_SIGNAL] = {0};

struct _GamesGriloMedia {
	GObject parent_instance;
	GamesGriloMediaPrivate * priv;
};

struct _GamesGriloMediaClass {
	GObjectClass parent_class;
};

struct _GamesTitleIface {
	GTypeInterface parent_iface;
	gchar* (*get_title) (GamesTitle* self, GError** error);
};

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


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

GType games_grilo_media_get_type (void) G_GNUC_CONST;
GType games_title_get_type (void) G_GNUC_CONST;
#define GAMES_GRILO_MEDIA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_GRILO_MEDIA, GamesGriloMediaPrivate))
enum  {
	GAMES_GRILO_MEDIA_DUMMY_PROPERTY
};
GamesGriloMedia* games_grilo_media_new (GamesTitle* title, const gchar* mime_type);
GamesGriloMedia* games_grilo_media_construct (GType object_type, GamesTitle* title, const gchar* mime_type);
static GrlRegistry* games_grilo_media_get_registry (GError** error);
void games_grilo_media_try_resolve_media (GamesGriloMedia* self);
static void games_grilo_media_resolve_media (GamesGriloMedia* self, GError** error);
GrlMedia* games_grilo_media_get_media (GamesGriloMedia* self);
gchar* games_title_get_title (GamesTitle* self, GError** error);
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 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_;
	GamesTitle* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	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_ = title;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->title);
	self->priv->title = _tmp1_;
	_tmp2_ = mime_type;
	_tmp3_ = g_strdup (_tmp2_);
	_g_free0 (self->priv->mime_type);
	self->priv->mime_type = _tmp3_;
	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 GrlRegistry* games_grilo_media_get_registry (GError** error) {
	GrlRegistry* result = NULL;
	GrlRegistry* _tmp0_;
	GrlRegistry* _tmp3_;
	GrlRegistry* _tmp4_;
	GrlRegistry* _tmp5_;
	GrlRegistry* _tmp6_;
	GrlRegistry* _tmp7_;
	GError * _inner_error_ = NULL;
	_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_error_);
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		return NULL;
	}
	_tmp6_ = games_grilo_media_registry;
	_tmp7_ = _g_object_ref0 (_tmp6_);
	result = _tmp7_;
	return result;
}


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


GrlMedia* games_grilo_media_get_media (GamesGriloMedia* self) {
	GrlMedia* result = NULL;
	GrlMedia* _tmp0_;
	GrlMedia* _tmp1_;
	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) {
	gboolean _tmp0_;
	GrlRegistry* registry;
	GrlRegistry* _tmp1_;
	GrlSource* source;
	GrlRegistry* _tmp2_;
	GrlSource* _tmp3_;
	GrlSource* _tmp4_;
	GrlSource* _tmp5_;
	GrlMedia* base_media;
	GrlMedia* _tmp6_;
	gchar* title_string;
	GamesTitle* _tmp7_;
	gchar* _tmp8_;
	GrlMedia* _tmp9_;
	const gchar* _tmp10_;
	GrlMedia* _tmp11_;
	const gchar* _tmp12_;
	GList* keys;
	GrlKeyID _tmp13_;
	GList* _tmp14_;
	GrlOperationOptions* options;
	GrlOperationOptions* _tmp15_;
	GrlOperationOptions* _tmp16_;
	GrlSource* _tmp17_;
	GrlMedia* _tmp18_;
	GrlMedia* _tmp19_;
	GList* _tmp20_;
	GrlOperationOptions* _tmp21_;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->resolving;
	if (_tmp0_) {
		return;
	}
	self->priv->resolving = TRUE;
	_tmp1_ = games_grilo_media_get_registry (&_inner_error_);
	registry = _tmp1_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		return;
	}
	_tmp2_ = registry;
	_tmp3_ = grl_registry_lookup_source (_tmp2_, "grl-thegamesdb");
	_tmp4_ = _g_object_ref0 (_tmp3_);
	source = _tmp4_;
	_tmp5_ = source;
	if (_tmp5_ == NULL) {
		_g_object_unref0 (source);
		_g_object_unref0 (registry);
		return;
	}
	_tmp6_ = grl_media_new ();
	base_media = _tmp6_;
	_tmp7_ = self->priv->title;
	_tmp8_ = games_title_get_title (_tmp7_, &_inner_error_);
	title_string = _tmp8_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (base_media);
		_g_object_unref0 (source);
		_g_object_unref0 (registry);
		return;
	}
	_tmp9_ = base_media;
	_tmp10_ = title_string;
	grl_media_set_title (_tmp9_, _tmp10_);
	_tmp11_ = base_media;
	_tmp12_ = self->priv->mime_type;
	grl_media_set_mime (_tmp11_, _tmp12_);
	_tmp13_ = GRL_METADATA_KEY_THUMBNAIL;
	_tmp14_ = grl_metadata_key_list_new (_tmp13_, NULL, NULL);
	keys = _tmp14_;
	_tmp15_ = grl_operation_options_new (NULL);
	options = _tmp15_;
	_tmp16_ = options;
	grl_operation_options_set_resolution_flags (_tmp16_, GRL_RESOLVE_FULL);
	_tmp17_ = source;
	_tmp18_ = base_media;
	_tmp19_ = _g_object_ref0 (_tmp18_);
	_tmp20_ = keys;
	_tmp21_ = options;
	grl_source_resolve (_tmp17_, _tmp19_, _tmp20_, _tmp21_, _games_grilo_media_on_media_resolved_grl_source_resolve_cb, self);
	_g_object_unref0 (options);
	_g_list_free0 (keys);
	_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_;
	GrlMedia* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (source != NULL);
	g_return_if_fail (media != NULL);
	_tmp0_ = media;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->media);
	self->priv->media = _tmp1_;
	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) {
	games_grilo_media_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesGriloMediaPrivate));
	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) {
	self->priv = GAMES_GRILO_MEDIA_GET_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);
}


GType games_grilo_media_get_type (void) {
	static volatile gsize games_grilo_media_type_id__volatile = 0;
	if (g_once_init_enter (&games_grilo_media_type_id__volatile)) {
		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);
		g_once_init_leave (&games_grilo_media_type_id__volatile, games_grilo_media_type_id);
	}
	return games_grilo_media_type_id__volatile;
}



