/* dlna150-hacks.c generated by valac 0.56.19, the Vala compiler
 * generated from dlna150-hacks.vala, do not modify */

/* SPDX-License-Identifier: LGPL-2.1-or-later*/
/* SPDX-FileCopyrightText: 2023 Jens Georg <mail@jensge.org>*/

#include "rygel-core.h"
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gee.h>
#include <gio/gio.h>
#include <libgupnp/gupnp.h>

#define RYGEL_DLNA150_HACKS_X_DLNADOC_MDMS_XPATH "//*[local-name()='X_DLNADOC'" " and contains(.,\"M-DMS-1.51\")]"
#define RYGEL_DLNA150_HACKS_X_DLNADOC_DMS_XPATH "//*[local-name()='X_DLNADOC'" " and contains(.,\"DMS-1.51\")]"
#define RYGEL_DLNA150_HACKS_X_DLNADOC_MDMR_XPATH "//*[local-name()='X_DLNADOC'" " and contains(.,\"M-DMR-1.51\")]"
#define RYGEL_DLNA150_HACKS_X_DLNADOC_DMR_XPATH "//*[local-name()='X_DLNADOC'" " and contains(.,\"DMR-1.51\")]"
#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  {
	RYGEL_DLNA150_HACKS_0_PROPERTY,
	RYGEL_DLNA150_HACKS_NUM_PROPERTIES
};
static GParamSpec* rygel_dlna150_hacks_properties[RYGEL_DLNA150_HACKS_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))

#define RYGEL_TYPE_AGENT_MATCHER (rygel_agent_matcher_get_type ())
#define RYGEL_AGENT_MATCHER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_AGENT_MATCHER, RygelAgentMatcher))
#define RYGEL_AGENT_MATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_AGENT_MATCHER, RygelAgentMatcherClass))
#define RYGEL_IS_AGENT_MATCHER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_AGENT_MATCHER))
#define RYGEL_IS_AGENT_MATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_AGENT_MATCHER))
#define RYGEL_AGENT_MATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_AGENT_MATCHER, RygelAgentMatcherClass))

typedef struct _RygelAgentMatcher RygelAgentMatcher;
typedef struct _RygelAgentMatcherClass RygelAgentMatcherClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

static gpointer rygel_dlna150_hacks_parent_class = NULL;
static RygelAgentMatcher* rygel_dlna150_hacks_agent_matcher;
static RygelAgentMatcher* rygel_dlna150_hacks_agent_matcher = NULL;

VALA_EXTERN GType rygel_agent_matcher_get_type (void) G_GNUC_CONST ;
static void rygel_dlna150_hacks_real_constructed (GObject* base);
static gchar** _vala_array_dup4 (gchar** self,
                          gssize length);
VALA_EXTERN RygelAgentMatcher* rygel_agent_matcher_new (const gchar* name,
                                            GeeArrayList* agents);
VALA_EXTERN RygelAgentMatcher* rygel_agent_matcher_construct (GType object_type,
                                                  const gchar* name,
                                                  GeeArrayList* agents);
VALA_EXTERN gboolean rygel_agent_matcher_empty (RygelAgentMatcher* self);
VALA_EXTERN GRegex* rygel_agent_matcher_get_agent_regex (RygelAgentMatcher* self);
static void rygel_dlna150_hacks_finalize (GObject * obj);
static GType rygel_dlna150_hacks_get_type_once (void);

static const gchar* RYGEL_DLNA150_HACKS_AGENTS[1] = {"Dart"};

static gchar**
_vala_array_dup4 (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 void
rygel_dlna150_hacks_real_constructed (GObject* base)
{
	RygelDlna150Hacks * self;
	RygelAgentMatcher* _tmp0_;
	self = (RygelDlna150Hacks*) base;
	G_OBJECT_CLASS (rygel_dlna150_hacks_parent_class)->constructed (G_TYPE_CHECK_INSTANCE_CAST (self, G_TYPE_OBJECT, GObject));
	_tmp0_ = rygel_dlna150_hacks_agent_matcher;
	if (_tmp0_ == NULL) {
		GeeArrayList* defaults = NULL;
		gchar** _tmp1_;
		gint _tmp1__length1;
		GEqualFunc _tmp2_;
		GeeArrayList* _tmp3_;
		RygelMetaConfig* config = NULL;
		RygelMetaConfig* _tmp4_;
		GeeArrayList* agents = NULL;
		RygelMetaConfig* _tmp5_;
		GeeArrayList* _tmp6_;
		GeeArrayList* _tmp7_;
		GeeArrayList* _tmp8_;
		RygelAgentMatcher* _tmp9_;
		_tmp1_ = _vala_array_dup4 (RYGEL_DLNA150_HACKS_AGENTS, G_N_ELEMENTS (RYGEL_DLNA150_HACKS_AGENTS));
		_tmp1__length1 = G_N_ELEMENTS (RYGEL_DLNA150_HACKS_AGENTS);
		_tmp2_ = g_str_equal;
		_tmp3_ = gee_array_list_new_wrap (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, _tmp1_, _tmp1__length1, (GeeEqualDataFunc) _tmp2_, NULL, NULL);
		defaults = _tmp3_;
		_tmp4_ = rygel_meta_config_get_default ();
		config = _tmp4_;
		_tmp5_ = config;
		_tmp6_ = defaults;
		_tmp7_ = rygel_configuration_get_string_list_with_default ((RygelConfiguration*) _tmp5_, "general", "force-downgrade-for", _tmp6_);
		agents = _tmp7_;
		_tmp8_ = agents;
		_tmp9_ = rygel_agent_matcher_new ("V1 hacks", _tmp8_);
		_g_object_unref0 (rygel_dlna150_hacks_agent_matcher);
		rygel_dlna150_hacks_agent_matcher = _tmp9_;
		_g_object_unref0 (agents);
		_g_object_unref0 (config);
		_g_object_unref0 (defaults);
	}
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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 NULL;
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

void
rygel_dlna150_hacks_apply_on_device (RygelDlna150Hacks* self,
                                     RygelRootDevice* device,
                                     const gchar* template_path,
                                     GError** error)
{
	GFile* file = NULL;
	GFile* _tmp0_;
	guint8* contents = NULL;
	gint contents_length1 = 0;
	gint _contents_size_ = 0;
	GFile* _tmp1_;
	guint8* _tmp2_ = NULL;
	gsize _tmp3_ = 0;
	const gchar* data = NULL;
	guint8* _tmp4_;
	gint _tmp4__length1;
	gchar* patched_contents = NULL;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GFile* description_file = NULL;
	const gchar* _tmp8_;
	GFile* _tmp9_;
	GFile* _tmp10_;
	const gchar* _tmp11_;
	guint8* _tmp12_;
	gint _tmp12__length1;
	gint _tmp13_ = 0;
	guint8* _tmp14_;
	gint _tmp14__length1;
	gchar* server_path = NULL;
	const gchar* _tmp15_;
	gchar* _tmp16_;
	RygelAgentMatcher* _tmp17_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (device != NULL);
	if (template_path == NULL) {
		return;
	}
	_tmp0_ = g_file_new_for_path (template_path);
	file = _tmp0_;
	_tmp1_ = file;
	g_file_load_contents (_tmp1_, NULL, &_tmp2_, &_tmp3_, NULL, &_inner_error0_);
	contents = (g_free (contents), NULL);
	contents = _tmp2_;
	contents_length1 = _tmp3_;
	_contents_size_ = contents_length1;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		contents = (g_free (contents), NULL);
		_g_object_unref0 (file);
		return;
	}
	_tmp4_ = contents;
	_tmp4__length1 = contents_length1;
	data = (const gchar*) _tmp4_;
	_tmp5_ = data;
	_tmp6_ = string_replace (_tmp5_, "-1.51", "-1.50");
	patched_contents = _tmp6_;
	_tmp7_ = string_replace (template_path, ".xml", "-dlna150.xml");
	_g_free0 (self->description_path);
	self->description_path = _tmp7_;
	_tmp8_ = self->description_path;
	_tmp9_ = g_file_new_for_path (_tmp8_);
	description_file = _tmp9_;
	_tmp10_ = description_file;
	_tmp11_ = patched_contents;
	_tmp12_ = string_get_data (_tmp11_, &_tmp13_);
	_tmp12__length1 = _tmp13_;
	_tmp14_ = _tmp12_;
	_tmp14__length1 = _tmp12__length1;
	g_file_replace_contents (_tmp10_, (const char*) _tmp14_, (gsize) _tmp14__length1, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (description_file);
		_g_free0 (patched_contents);
		contents = (g_free (contents), NULL);
		_g_object_unref0 (file);
		return;
	}
	_tmp15_ = gupnp_root_device_get_description_document_name ((GUPnPRootDevice*) device);
	_tmp16_ = g_strconcat ("/", _tmp15_, NULL);
	server_path = _tmp16_;
	_tmp17_ = rygel_dlna150_hacks_agent_matcher;
	if (!rygel_agent_matcher_empty (_tmp17_)) {
		GUPnPContext* _tmp18_;
		GUPnPContext* _tmp19_;
		const gchar* _tmp20_;
		const gchar* _tmp21_;
		RygelAgentMatcher* _tmp22_;
		GRegex* _tmp23_;
		GRegex* _tmp24_;
		_tmp18_ = gupnp_device_info_get_context ((GUPnPDeviceInfo*) device);
		_tmp19_ = _tmp18_;
		_tmp20_ = self->description_path;
		_tmp21_ = server_path;
		_tmp22_ = rygel_dlna150_hacks_agent_matcher;
		_tmp23_ = rygel_agent_matcher_get_agent_regex (_tmp22_);
		_tmp24_ = _tmp23_;
		gupnp_context_host_path_for_agent (_tmp19_, _tmp20_, _tmp21_, _tmp24_);
	}
	_g_free0 (server_path);
	_g_object_unref0 (description_file);
	_g_free0 (patched_contents);
	contents = (g_free (contents), NULL);
	_g_object_unref0 (file);
}

RygelDlna150Hacks*
rygel_dlna150_hacks_construct (GType object_type)
{
	RygelDlna150Hacks * self = NULL;
	self = (RygelDlna150Hacks*) g_object_new (object_type, NULL);
	return self;
}

RygelDlna150Hacks*
rygel_dlna150_hacks_new (void)
{
	return rygel_dlna150_hacks_construct (RYGEL_TYPE_DLNA150_HACKS);
}

static void
rygel_dlna150_hacks_class_init (RygelDlna150HacksClass * klass,
                                gpointer klass_data)
{
	rygel_dlna150_hacks_parent_class = g_type_class_peek_parent (klass);
	((GObjectClass *) klass)->constructed = (void (*) (GObject*)) rygel_dlna150_hacks_real_constructed;
	G_OBJECT_CLASS (klass)->finalize = rygel_dlna150_hacks_finalize;
}

static void
rygel_dlna150_hacks_instance_init (RygelDlna150Hacks * self,
                                   gpointer klass)
{
}

static void
rygel_dlna150_hacks_finalize (GObject * obj)
{
	RygelDlna150Hacks * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_DLNA150_HACKS, RygelDlna150Hacks);
	_g_free0 (self->description_path);
	G_OBJECT_CLASS (rygel_dlna150_hacks_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
rygel_dlna150_hacks_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelDlna150HacksClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_dlna150_hacks_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelDlna150Hacks), 0, (GInstanceInitFunc) rygel_dlna150_hacks_instance_init, NULL };
	GType rygel_dlna150_hacks_type_id;
	rygel_dlna150_hacks_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelDlna150Hacks", &g_define_type_info, 0);
	return rygel_dlna150_hacks_type_id;
}

GType
rygel_dlna150_hacks_get_type (void)
{
	static volatile gsize rygel_dlna150_hacks_type_id__once = 0;
	if (g_once_init_enter (&rygel_dlna150_hacks_type_id__once)) {
		GType rygel_dlna150_hacks_type_id;
		rygel_dlna150_hacks_type_id = rygel_dlna150_hacks_get_type_once ();
		g_once_init_leave (&rygel_dlna150_hacks_type_id__once, rygel_dlna150_hacks_type_id);
	}
	return rygel_dlna150_hacks_type_id__once;
}

