/* mame-game-uri-adapter.c generated by valac 0.35.5, the Vala compiler
 * generated from mame-game-uri-adapter.vala, do not modify */

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

#include <glib.h>
#include <glib-object.h>
#include <libgnome-games.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <glib/gi18n-lib.h>


#define GAMES_TYPE_MAME_GAME_URI_ADAPTER (games_mame_game_uri_adapter_get_type ())
#define GAMES_MAME_GAME_URI_ADAPTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_MAME_GAME_URI_ADAPTER, GamesMameGameUriAdapter))
#define GAMES_MAME_GAME_URI_ADAPTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_MAME_GAME_URI_ADAPTER, GamesMameGameUriAdapterClass))
#define GAMES_IS_MAME_GAME_URI_ADAPTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_MAME_GAME_URI_ADAPTER))
#define GAMES_IS_MAME_GAME_URI_ADAPTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_MAME_GAME_URI_ADAPTER))
#define GAMES_MAME_GAME_URI_ADAPTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_MAME_GAME_URI_ADAPTER, GamesMameGameUriAdapterClass))

typedef struct _GamesMameGameUriAdapter GamesMameGameUriAdapter;
typedef struct _GamesMameGameUriAdapterClass GamesMameGameUriAdapterClass;
typedef struct _GamesMameGameUriAdapterPrivate GamesMameGameUriAdapterPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define GAMES_TYPE_MAME_GAME_INFO (games_mame_game_info_get_type ())
typedef struct _GamesMameGameInfo GamesMameGameInfo;
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _games_mame_game_info_free0(var) ((var == NULL) ? NULL : (var = (games_mame_game_info_free (var), NULL)))
typedef struct _GamesMameGameUriAdapterGameForUriData GamesMameGameUriAdapterGameForUriData;

struct _GamesMameGameUriAdapter {
	GObject parent_instance;
	GamesMameGameUriAdapterPrivate * priv;
};

struct _GamesMameGameUriAdapterClass {
	GObjectClass parent_class;
};

struct _GamesMameGameInfo {
	gchar* id;
	gchar* name;
};

typedef enum  {
	GAMES_MAME_ERROR_INVALID_GAME_ID
} GamesMameError;
#define GAMES_MAME_ERROR games_mame_error_quark ()
struct _GamesMameGameUriAdapterGameForUriData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	gboolean _task_complete_;
	GamesMameGameUriAdapter* self;
	gchar* uri;
	GamesGame* result;
	GHashTable* supported_games;
	GHashTable* _tmp0_;
	GFile* file;
	const gchar* _tmp1_;
	GFile* _tmp2_;
	gchar* game_id;
	GFile* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	gint _tmp8_;
	gint _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	GHashTable* _tmp12_;
	const gchar* _tmp13_;
	gboolean _tmp14_;
	const gchar* _tmp15_;
	const gchar* _tmp16_;
	GError* _tmp17_;
	gchar* uid_string;
	const gchar* _tmp18_;
	const gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	GamesGenericUid* uid;
	const gchar* _tmp24_;
	GamesGenericUid* _tmp25_;
	GamesMameGameInfo* info;
	GHashTable* _tmp26_;
	const gchar* _tmp27_;
	gconstpointer _tmp28_;
	GamesMameGameInfo* _tmp29_;
	gchar* title_string;
	GamesMameGameInfo* _tmp30_;
	const gchar* _tmp31_;
	gchar* _tmp32_;
	const gchar* _tmp33_;
	gchar** _tmp34_;
	gchar** _tmp35_;
	gchar** _tmp36_;
	gint _tmp36__length1;
	const gchar* _tmp37_;
	gchar* _tmp38_;
	const gchar* _tmp39_;
	gchar* _tmp40_;
	GamesGenericTitle* title;
	const gchar* _tmp41_;
	GamesGenericTitle* _tmp42_;
	GamesDummyIcon* icon;
	GamesDummyIcon* _tmp43_;
	GamesLocalCover* cover;
	const gchar* _tmp44_;
	GamesLocalCover* _tmp45_;
	GamesRetroCoreSource* core_source;
	gchar* _tmp46_;
	gchar* _tmp47_;
	gchar** _tmp48_;
	gchar** _tmp49_;
	gint _tmp49__length1;
	GamesRetroCoreSource* _tmp50_;
	GamesRetroCoreSource* _tmp51_;
	GamesRetroRunner* runner;
	GamesRetroCoreSource* _tmp52_;
	const gchar* _tmp53_;
	GamesGenericUid* _tmp54_;
	GamesGenericTitle* _tmp55_;
	GamesRetroRunner* _tmp56_;
	GamesGenericTitle* _tmp57_;
	GamesDummyIcon* _tmp58_;
	GamesLocalCover* _tmp59_;
	GamesRetroRunner* _tmp60_;
	GamesGenericGame* _tmp61_;
	GError * _inner_error_;
};


static gpointer games_mame_game_uri_adapter_parent_class = NULL;
static GamesGameUriAdapterIface * games_mame_game_uri_adapter_games_game_uri_adapter_parent_iface = NULL;
static GType games_mame_game_uri_adapter_type_id = 0;

GType games_mame_game_uri_adapter_get_type (void) G_GNUC_CONST;
enum  {
	GAMES_MAME_GAME_URI_ADAPTER_DUMMY_PROPERTY
};
#define GAMES_MAME_GAME_URI_ADAPTER_SEARCHED_MIME_TYPE "application/zip"
#define GAMES_MAME_GAME_URI_ADAPTER_SPECIFIC_MIME_TYPE "application/x-mame-rom"
#define GAMES_MAME_GAME_URI_ADAPTER_PLATFORM "MAME"
static void games_mame_game_uri_adapter_real_game_for_uri_data_free (gpointer _data);
static void games_mame_game_uri_adapter_real_game_for_uri (GamesGameUriAdapter* base, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean games_mame_game_uri_adapter_real_game_for_uri_co (GamesMameGameUriAdapterGameForUriData* _data_);
GType games_mame_game_info_get_type (void) G_GNUC_CONST;
GamesMameGameInfo* games_mame_game_info_dup (const GamesMameGameInfo* self);
void games_mame_game_info_free (GamesMameGameInfo* self);
void games_mame_game_info_copy (const GamesMameGameInfo* self, GamesMameGameInfo* dest);
void games_mame_game_info_destroy (GamesMameGameInfo* self);
void games_mame_game_info_get_supported_games (GAsyncReadyCallback _callback_, gpointer _user_data_);
GHashTable* games_mame_game_info_get_supported_games_finish (GAsyncResult* _res_, GError** error);
static void games_mame_game_uri_adapter_game_for_uri_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
GQuark games_mame_error_quark (void);
GamesMameGameUriAdapter* games_mame_game_uri_adapter_new (void);
GamesMameGameUriAdapter* games_mame_game_uri_adapter_construct (GType object_type);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);

static GRegex* _tmp_regex_0 = NULL;

static void games_mame_game_uri_adapter_real_game_for_uri_data_free (gpointer _data) {
	GamesMameGameUriAdapterGameForUriData* _data_;
	_data_ = _data;
	_g_free0 (_data_->uri);
	_g_object_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (GamesMameGameUriAdapterGameForUriData, _data_);
}


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


static void games_mame_game_uri_adapter_real_game_for_uri (GamesGameUriAdapter* base, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	GamesMameGameUriAdapter * self;
	GamesMameGameUriAdapterGameForUriData* _data_;
	GamesMameGameUriAdapter* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	self = (GamesMameGameUriAdapter*) base;
	_data_ = g_slice_new0 (GamesMameGameUriAdapterGameForUriData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, games_mame_game_uri_adapter_real_game_for_uri_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = uri;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (_data_->uri);
	_data_->uri = _tmp2_;
	games_mame_game_uri_adapter_real_game_for_uri_co (_data_);
}


static GamesGame* games_mame_game_uri_adapter_real_game_for_uri_finish (GamesGameUriAdapter* base, GAsyncResult* _res_, GError** error) {
	GamesGame* result;
	GamesMameGameUriAdapterGameForUriData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	_data_->_task_complete_ = TRUE;
	result = _data_->result;
	_data_->result = NULL;
	return result;
}


static void games_mame_game_uri_adapter_game_for_uri_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	GamesMameGameUriAdapterGameForUriData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	_data_->_task_complete_ = TRUE;
	games_mame_game_uri_adapter_real_game_for_uri_co (_data_);
}


static inline GRegex* _thread_safe_regex_init (GRegex** re, const gchar * pattern, GRegexMatchFlags match_options) {
	if (g_once_init_enter ((volatile gsize*) re)) {
		GRegex* val = g_regex_new (pattern, match_options, 0, NULL);
		g_once_init_leave ((volatile gsize*) re, (gsize) val);
	}
	return *re;
}


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


static gpointer _games_mame_game_info_dup0 (gpointer self) {
	return self ? games_mame_game_info_dup (self) : NULL;
}


static gchar* string_strip (const gchar* self) {
	gchar* result = NULL;
	gchar* _result_;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	g_strstrip (_tmp1_);
	result = _result_;
	return result;
}


static gboolean games_mame_game_uri_adapter_real_game_for_uri_co (GamesMameGameUriAdapterGameForUriData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_state_ = 1;
	games_mame_game_info_get_supported_games (games_mame_game_uri_adapter_game_for_uri_ready, _data_);
	return FALSE;
	_state_1:
	_data_->_tmp0_ = games_mame_game_info_get_supported_games_finish (_data_->_res_, &_data_->_inner_error_);
	_data_->supported_games = _data_->_tmp0_;
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp1_ = _data_->uri;
	_data_->_tmp2_ = g_file_new_for_uri (_data_->_tmp1_);
	_data_->file = _data_->_tmp2_;
	_data_->_tmp3_ = _data_->file;
	_data_->_tmp4_ = g_file_get_basename (_data_->_tmp3_);
	_data_->game_id = _data_->_tmp4_;
	_data_->_tmp6_ = _data_->game_id;
	_data_->_tmp7_ = _data_->game_id;
	_data_->_tmp8_ = strlen (_data_->_tmp7_);
	_data_->_tmp9_ = _data_->_tmp8_;
	_data_->_tmp10_ = g_regex_replace (_thread_safe_regex_init (&_tmp_regex_0, "\\.zip$", 0), _data_->_tmp6_, (gssize) _data_->_tmp9_, 0, "", 0, &_data_->_inner_error_);
	_data_->_tmp5_ = _data_->_tmp10_;
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_g_free0 (_data_->game_id);
		_g_object_unref0 (_data_->file);
		_g_hash_table_unref0 (_data_->supported_games);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp11_ = _data_->_tmp5_;
	_data_->_tmp5_ = NULL;
	_g_free0 (_data_->game_id);
	_data_->game_id = _data_->_tmp11_;
	_data_->_tmp12_ = _data_->supported_games;
	_data_->_tmp13_ = _data_->game_id;
	_data_->_tmp14_ = g_hash_table_contains (_data_->_tmp12_, _data_->_tmp13_);
	if (!_data_->_tmp14_) {
		_data_->_tmp15_ = _data_->game_id;
		_data_->_tmp16_ = _data_->uri;
		_data_->_tmp17_ = g_error_new (GAMES_MAME_ERROR, GAMES_MAME_ERROR_INVALID_GAME_ID, _ ("Invalid MAME game id “%s” for “%s”."), _data_->_tmp15_, _data_->_tmp16_);
		_data_->_inner_error_ = _data_->_tmp17_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_g_free0 (_data_->_tmp5_);
		_g_free0 (_data_->game_id);
		_g_object_unref0 (_data_->file);
		_g_hash_table_unref0 (_data_->supported_games);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp18_ = _data_->game_id;
	_data_->_tmp19_ = string_to_string (_data_->_tmp18_);
	_data_->_tmp20_ = g_strconcat ("mame-", _data_->_tmp19_, NULL);
	_data_->_tmp21_ = _data_->_tmp20_;
	_data_->_tmp22_ = g_utf8_strdown (_data_->_tmp21_, (gssize) -1);
	_data_->_tmp23_ = _data_->_tmp22_;
	_g_free0 (_data_->_tmp21_);
	_data_->uid_string = _data_->_tmp23_;
	_data_->_tmp24_ = _data_->uid_string;
	_data_->_tmp25_ = games_generic_uid_new (_data_->_tmp24_);
	_data_->uid = _data_->_tmp25_;
	_data_->_tmp26_ = _data_->supported_games;
	_data_->_tmp27_ = _data_->game_id;
	_data_->_tmp28_ = g_hash_table_lookup (_data_->_tmp26_, _data_->_tmp27_);
	_data_->_tmp29_ = _games_mame_game_info_dup0 ((GamesMameGameInfo*) _data_->_tmp28_);
	_data_->info = _data_->_tmp29_;
	_data_->_tmp30_ = _data_->info;
	_data_->_tmp31_ = (*_data_->_tmp30_).name;
	_data_->_tmp32_ = g_strdup (_data_->_tmp31_);
	_data_->title_string = _data_->_tmp32_;
	_data_->_tmp33_ = _data_->title_string;
	_data_->_tmp35_ = _data_->_tmp34_ = g_strsplit (_data_->_tmp33_, "(", 0);
	_data_->_tmp36_ = _data_->_tmp35_;
	_data_->_tmp36__length1 = _vala_array_length (_data_->_tmp34_);
	_data_->_tmp37_ = _data_->_tmp36_[0];
	_data_->_tmp38_ = g_strdup (_data_->_tmp37_);
	_g_free0 (_data_->title_string);
	_data_->title_string = _data_->_tmp38_;
	_data_->_tmp36_ = (_vala_array_free (_data_->_tmp36_, _data_->_tmp36__length1, (GDestroyNotify) g_free), NULL);
	_data_->_tmp39_ = _data_->title_string;
	_data_->_tmp40_ = string_strip (_data_->_tmp39_);
	_g_free0 (_data_->title_string);
	_data_->title_string = _data_->_tmp40_;
	_data_->_tmp41_ = _data_->title_string;
	_data_->_tmp42_ = games_generic_title_new (_data_->_tmp41_);
	_data_->title = _data_->_tmp42_;
	_data_->_tmp43_ = games_dummy_icon_new ();
	_data_->icon = _data_->_tmp43_;
	_data_->_tmp44_ = _data_->uri;
	_data_->_tmp45_ = games_local_cover_new (_data_->_tmp44_);
	_data_->cover = _data_->_tmp45_;
	_data_->_tmp46_ = g_strdup (GAMES_MAME_GAME_URI_ADAPTER_SEARCHED_MIME_TYPE);
	_data_->_tmp47_ = g_strdup (GAMES_MAME_GAME_URI_ADAPTER_SPECIFIC_MIME_TYPE);
	_data_->_tmp48_ = g_new0 (gchar*, 2 + 1);
	_data_->_tmp48_[0] = _data_->_tmp46_;
	_data_->_tmp48_[1] = _data_->_tmp47_;
	_data_->_tmp49_ = _data_->_tmp48_;
	_data_->_tmp49__length1 = 2;
	_data_->_tmp50_ = games_retro_core_source_new (GAMES_MAME_GAME_URI_ADAPTER_PLATFORM, _data_->_tmp49_, 2);
	_data_->_tmp51_ = _data_->_tmp50_;
	_data_->_tmp49_ = (_vala_array_free (_data_->_tmp49_, _data_->_tmp49__length1, (GDestroyNotify) g_free), NULL);
	_data_->core_source = _data_->_tmp51_;
	_data_->_tmp52_ = _data_->core_source;
	_data_->_tmp53_ = _data_->uri;
	_data_->_tmp54_ = _data_->uid;
	_data_->_tmp55_ = _data_->title;
	_data_->_tmp56_ = games_retro_runner_new (_data_->_tmp52_, _data_->_tmp53_, (GamesUid*) _data_->_tmp54_, (GamesTitle*) _data_->_tmp55_);
	_data_->runner = _data_->_tmp56_;
	_data_->_tmp57_ = _data_->title;
	_data_->_tmp58_ = _data_->icon;
	_data_->_tmp59_ = _data_->cover;
	_data_->_tmp60_ = _data_->runner;
	_data_->_tmp61_ = games_generic_game_new ((GamesTitle*) _data_->_tmp57_, (GamesIcon*) _data_->_tmp58_, (GamesCover*) _data_->_tmp59_, (GamesRunner*) _data_->_tmp60_);
	_data_->result = (GamesGame*) _data_->_tmp61_;
	_g_object_unref0 (_data_->runner);
	_g_object_unref0 (_data_->core_source);
	_g_object_unref0 (_data_->cover);
	_g_object_unref0 (_data_->icon);
	_g_object_unref0 (_data_->title);
	_g_free0 (_data_->title_string);
	_games_mame_game_info_free0 (_data_->info);
	_g_object_unref0 (_data_->uid);
	_g_free0 (_data_->uid_string);
	_g_free0 (_data_->_tmp5_);
	_g_free0 (_data_->game_id);
	_g_object_unref0 (_data_->file);
	_g_hash_table_unref0 (_data_->supported_games);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


GamesMameGameUriAdapter* games_mame_game_uri_adapter_construct (GType object_type) {
	GamesMameGameUriAdapter * self = NULL;
	self = (GamesMameGameUriAdapter*) g_object_new (object_type, NULL);
	return self;
}


GamesMameGameUriAdapter* games_mame_game_uri_adapter_new (void) {
	return games_mame_game_uri_adapter_construct (GAMES_TYPE_MAME_GAME_URI_ADAPTER);
}


static void games_mame_game_uri_adapter_class_init (GamesMameGameUriAdapterClass * klass) {
	games_mame_game_uri_adapter_parent_class = g_type_class_peek_parent (klass);
}


static void games_mame_game_uri_adapter_games_game_uri_adapter_interface_init (GamesGameUriAdapterIface * iface) {
	games_mame_game_uri_adapter_games_game_uri_adapter_parent_iface = g_type_interface_peek_parent (iface);
	iface->game_for_uri = (GamesGame* (*) (GamesGameUriAdapter *, const gchar*, GError**)) games_mame_game_uri_adapter_real_game_for_uri;
	iface->game_for_uri_finish = games_mame_game_uri_adapter_real_game_for_uri_finish;
}


static void games_mame_game_uri_adapter_instance_init (GamesMameGameUriAdapter * self) {
}


GType games_mame_game_uri_adapter_get_type (void) {
	return games_mame_game_uri_adapter_type_id;
}


GType games_mame_game_uri_adapter_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (GamesMameGameUriAdapterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_mame_game_uri_adapter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesMameGameUriAdapter), 0, (GInstanceInitFunc) games_mame_game_uri_adapter_instance_init, NULL };
	static const GInterfaceInfo games_game_uri_adapter_info = { (GInterfaceInitFunc) games_mame_game_uri_adapter_games_game_uri_adapter_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	games_mame_game_uri_adapter_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "GamesMameGameUriAdapter", &g_define_type_info, 0);
	g_type_module_add_interface (module, games_mame_game_uri_adapter_type_id, GAMES_TYPE_GAME_URI_ADAPTER, &games_game_uri_adapter_info);
	return games_mame_game_uri_adapter_type_id;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int 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, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



