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

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

#include "gnome-games.h"
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.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_FILENAME_TITLE_0_PROPERTY,
	GAMES_FILENAME_TITLE_NUM_PROPERTIES
};
static GParamSpec* games_filename_title_properties[GAMES_FILENAME_TITLE_NUM_PROPERTIES];
#define _games_uri_unref0(var) ((var == NULL) ? NULL : (var = (games_uri_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

struct _GamesFilenameTitlePrivate {
	GamesUri* uri;
};

static gint GamesFilenameTitle_private_offset;
static gpointer games_filename_title_parent_class = NULL;
static GRegex* games_filename_title_filename_ext_regex;
static GRegex* games_filename_title_filename_ext_regex = NULL;
static GamesTitleIface * games_filename_title_games_title_parent_iface = NULL;

static gchar* games_filename_title_real_get_title (GamesTitle* base,
                                            GError** error);
static void games_filename_title_finalize (GObject * obj);
static GType games_filename_title_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 GRegex* _tmp_regex_0 = NULL;

static inline gpointer
games_filename_title_get_instance_private (GamesFilenameTitle* self)
{
	return G_STRUCT_MEMBER_P (self, GamesFilenameTitle_private_offset);
}

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

GamesFilenameTitle*
games_filename_title_construct (GType object_type,
                                GamesUri* uri)
{
	GamesFilenameTitle * self = NULL;
	GamesUri* _tmp0_;
	g_return_val_if_fail (uri != NULL, NULL);
	self = (GamesFilenameTitle*) g_object_new (object_type, NULL);
	_tmp0_ = _games_uri_ref0 (uri);
	_games_uri_unref0 (self->priv->uri);
	self->priv->uri = _tmp0_;
	return self;
}

GamesFilenameTitle*
games_filename_title_new (GamesUri* uri)
{
	return games_filename_title_construct (GAMES_TYPE_FILENAME_TITLE, uri);
}

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

static gchar*
games_filename_title_real_get_title (GamesTitle* base,
                                     GError** error)
{
	GamesFilenameTitle * self;
	GFile* file = NULL;
	GamesUri* _tmp0_;
	GFile* _tmp1_;
	gchar* name = NULL;
	gchar* _tmp8_ = NULL;
	GRegex* _tmp9_;
	const gchar* _tmp10_;
	const gchar* _tmp11_;
	gint _tmp12_;
	gint _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	const gchar* _tmp16_;
	gchar** _tmp17_;
	gchar** _tmp18_;
	gchar** _tmp19_;
	gint _tmp19__length1;
	const gchar* _tmp20_;
	gchar* _tmp21_;
	const gchar* _tmp22_;
	gchar* _tmp23_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	self = (GamesFilenameTitle*) base;
	_tmp0_ = self->priv->uri;
	_tmp1_ = games_uri_to_file (_tmp0_);
	file = _tmp1_;
	{
		GFileInfo* fileinfo = NULL;
		GFileInfo* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp2_ = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
		fileinfo = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = g_file_info_get_display_name (fileinfo);
		_tmp4_ = g_strdup (_tmp3_);
		_g_free0 (name);
		name = _tmp4_;
		_g_object_unref0 (fileinfo);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp5_;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		g_critical ("filename-title.vala:25: Couldn't retrieve filename: %s", _tmp6_);
		_tmp7_ = g_file_get_basename (file);
		_g_free0 (name);
		name = _tmp7_;
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (name);
		_g_object_unref0 (file);
		return NULL;
	}
	_tmp9_ = games_filename_title_filename_ext_regex;
	_tmp10_ = name;
	_tmp11_ = name;
	_tmp12_ = strlen (_tmp11_);
	_tmp13_ = _tmp12_;
	_tmp14_ = g_regex_replace (_tmp9_, _tmp10_, (gssize) _tmp13_, 0, "", 0, &_inner_error0_);
	_tmp8_ = _tmp14_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (name);
		_g_object_unref0 (file);
		return NULL;
	}
	_tmp15_ = _tmp8_;
	_tmp8_ = NULL;
	_g_free0 (name);
	name = _tmp15_;
	_tmp16_ = name;
	_tmp18_ = _tmp17_ = g_strsplit (_tmp16_, "(", 0);
	_tmp19_ = _tmp18_;
	_tmp19__length1 = _vala_array_length (_tmp17_);
	_tmp20_ = _tmp19_[0];
	_tmp21_ = g_strdup (_tmp20_);
	_g_free0 (name);
	name = _tmp21_;
	_tmp19_ = (_vala_array_free (_tmp19_, _tmp19__length1, (GDestroyNotify) g_free), NULL);
	_tmp22_ = name;
	_tmp23_ = string_strip (_tmp22_);
	_g_free0 (name);
	name = _tmp23_;
	result = name;
	_g_free0 (_tmp8_);
	_g_object_unref0 (file);
	return result;
}

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

static gpointer
_g_regex_ref0 (gpointer self)
{
	return self ? g_regex_ref (self) : NULL;
}

static void
games_filename_title_class_init (GamesFilenameTitleClass * klass,
                                 gpointer klass_data)
{
	GRegex* _tmp0_;
	games_filename_title_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesFilenameTitle_private_offset);
	G_OBJECT_CLASS (klass)->finalize = games_filename_title_finalize;
	_tmp0_ = _g_regex_ref0 (_thread_safe_regex_init (&_tmp_regex_0, "\\.\\w+$", 0));
	_g_regex_unref0 (games_filename_title_filename_ext_regex);
	games_filename_title_filename_ext_regex = _tmp0_;
}

static void
games_filename_title_games_title_interface_init (GamesTitleIface * iface,
                                                 gpointer iface_data)
{
	games_filename_title_games_title_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_title = (gchar* (*) (GamesTitle*, GError**)) games_filename_title_real_get_title;
}

static void
games_filename_title_instance_init (GamesFilenameTitle * self,
                                    gpointer klass)
{
	self->priv = games_filename_title_get_instance_private (self);
}

static void
games_filename_title_finalize (GObject * obj)
{
	GamesFilenameTitle * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_FILENAME_TITLE, GamesFilenameTitle);
	_games_uri_unref0 (self->priv->uri);
	G_OBJECT_CLASS (games_filename_title_parent_class)->finalize (obj);
}

static GType
games_filename_title_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesFilenameTitleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_filename_title_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesFilenameTitle), 0, (GInstanceInitFunc) games_filename_title_instance_init, NULL };
	static const GInterfaceInfo games_title_info = { (GInterfaceInitFunc) games_filename_title_games_title_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType games_filename_title_type_id;
	games_filename_title_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesFilenameTitle", &g_define_type_info, 0);
	g_type_add_interface_static (games_filename_title_type_id, GAMES_TYPE_TITLE, &games_title_info);
	GamesFilenameTitle_private_offset = g_type_add_instance_private (games_filename_title_type_id, sizeof (GamesFilenameTitlePrivate));
	return games_filename_title_type_id;
}

GType
games_filename_title_get_type (void)
{
	static volatile gsize games_filename_title_type_id__once = 0;
	if (g_once_init_enter (&games_filename_title_type_id__once)) {
		GType games_filename_title_type_id;
		games_filename_title_type_id = games_filename_title_get_type_once ();
		g_once_init_leave (&games_filename_title_type_id__once, games_filename_title_type_id);
	}
	return games_filename_title_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;
}

