/* rygel-ruih-filter-entry.c generated by valac 0.44.9, the Vala compiler
 * generated from rygel-ruih-filter-entry.vala, do not modify */

/*
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: Neha Shanbhag <N.Shanbhag@cablelabs.com>
 * Contact: http://www.cablelabs.com/
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gobject/gvaluecollector.h>

#define TYPE_FILTER_ENTRY (filter_entry_get_type ())
#define FILTER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FILTER_ENTRY, FilterEntry))
#define FILTER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FILTER_ENTRY, FilterEntryClass))
#define IS_FILTER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FILTER_ENTRY))
#define IS_FILTER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FILTER_ENTRY))
#define FILTER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FILTER_ENTRY, FilterEntryClass))

typedef struct _FilterEntry FilterEntry;
typedef struct _FilterEntryClass FilterEntryClass;
typedef struct _FilterEntryPrivate FilterEntryPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ParamSpecFilterEntry ParamSpecFilterEntry;

struct _FilterEntry {
	GTypeInstance parent_instance;
	volatile int ref_count;
	FilterEntryPrivate * priv;
};

struct _FilterEntryClass {
	GTypeClass parent_class;
	void (*finalize) (FilterEntry *self);
	gboolean (*matches) (FilterEntry* self, const gchar* name, const gchar* value);
};

struct _FilterEntryPrivate {
	gchar* entry_name;
	gchar* entry_value;
};

struct _ParamSpecFilterEntry {
	GParamSpec parent_instance;
};

static gint FilterEntry_private_offset;
static gpointer filter_entry_parent_class = NULL;

gpointer filter_entry_ref (gpointer instance);
void filter_entry_unref (gpointer instance);
GParamSpec* param_spec_filter_entry (const gchar* name,
                                     const gchar* nick,
                                     const gchar* blurb,
                                     GType object_type,
                                     GParamFlags flags);
void value_set_filter_entry (GValue* value,
                             gpointer v_object);
void value_take_filter_entry (GValue* value,
                              gpointer v_object);
gpointer value_get_filter_entry (const GValue* value);
GType filter_entry_get_type (void) G_GNUC_CONST;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FilterEntry, filter_entry_unref)
#define FILTER_ENTRY_LIFETIME "lifetime"
FilterEntry* filter_entry_new (const gchar* name,
                               const gchar* value);
FilterEntry* filter_entry_construct (GType object_type,
                                     const gchar* name,
                                     const gchar* value);
gboolean filter_entry_matches (FilterEntry* self,
                               const gchar* name,
                               const gchar* value);
static gboolean filter_entry_real_matches (FilterEntry* self,
                                    const gchar* name,
                                    const gchar* value);
static void filter_entry_finalize (FilterEntry * obj);

static inline gpointer
filter_entry_get_instance_private (FilterEntry* self)
{
	return G_STRUCT_MEMBER_P (self, FilterEntry_private_offset);
}

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 = NULL;
	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:
	{
		GError* e = NULL;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		g_assert_not_reached ();
		_g_error_free0 (e);
	}
	__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;
}

FilterEntry*
filter_entry_construct (GType object_type,
                        const gchar* name,
                        const gchar* value)
{
	FilterEntry* self = NULL;
	gchar* temp = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (value != NULL, NULL);
	self = (FilterEntry*) g_type_create_instance (object_type);
	_tmp0_ = g_strdup (name);
	temp = _tmp0_;
	_tmp1_ = temp;
	_tmp2_ = string_replace (_tmp1_, "\"", "");
	_g_free0 (temp);
	temp = _tmp2_;
	_tmp3_ = temp;
	_tmp4_ = g_strdup (_tmp3_);
	_g_free0 (self->priv->entry_name);
	self->priv->entry_name = _tmp4_;
	_tmp5_ = g_strdup (value);
	_g_free0 (temp);
	temp = _tmp5_;
	_tmp6_ = temp;
	_tmp7_ = string_replace (_tmp6_, "\"", "");
	_g_free0 (temp);
	temp = _tmp7_;
	_tmp8_ = temp;
	_tmp9_ = g_regex_escape_string (_tmp8_, -1);
	_g_free0 (temp);
	temp = _tmp9_;
	_tmp10_ = temp;
	_tmp11_ = string_replace (_tmp10_, "\\*", ".*");
	_g_free0 (temp);
	temp = _tmp11_;
	_tmp12_ = temp;
	_tmp13_ = g_strdup (_tmp12_);
	_g_free0 (self->priv->entry_value);
	self->priv->entry_value = _tmp13_;
	_g_free0 (temp);
	return self;
}

FilterEntry*
filter_entry_new (const gchar* name,
                  const gchar* value)
{
	return filter_entry_construct (TYPE_FILTER_ENTRY, name, value);
}

static gboolean
filter_entry_real_matches (FilterEntry* self,
                           const gchar* name,
                           const gchar* value)
{
	gboolean _tmp0_ = FALSE;
	const gchar* _tmp1_;
	gboolean _tmp3_ = FALSE;
	const gchar* _tmp4_;
	gboolean result = FALSE;
	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);
	_tmp1_ = self->priv->entry_name;
	if (_tmp1_ == NULL) {
		const gchar* _tmp2_;
		_tmp2_ = self->priv->entry_value;
		_tmp0_ = _tmp2_ == NULL;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		result = FALSE;
		return result;
	}
	_tmp4_ = self->priv->entry_name;
	if (g_strcmp0 (_tmp4_, name) == 0) {
		_tmp3_ = TRUE;
	} else {
		const gchar* _tmp5_;
		_tmp5_ = self->priv->entry_name;
		_tmp3_ = g_strcmp0 (_tmp5_, "*") == 0;
	}
	if (_tmp3_) {
		const gchar* _tmp6_;
		_tmp6_ = self->priv->entry_value;
		if (_tmp6_ != NULL) {
			const gchar* _tmp7_;
			gboolean _result_ = FALSE;
			const gchar* _tmp9_;
			_tmp7_ = self->priv->entry_name;
			if (g_strcmp0 (_tmp7_, FILTER_ENTRY_LIFETIME) == 0) {
				const gchar* _tmp8_;
				_tmp8_ = self->priv->entry_value;
				result = atoi (_tmp8_) == atoi (value);
				return result;
			}
			_tmp9_ = self->priv->entry_value;
			_result_ = g_regex_match_simple (_tmp9_, value, G_REGEX_CASELESS, 0);
			result = _result_;
			return result;
		}
	}
	result = FALSE;
	return result;
}

gboolean
filter_entry_matches (FilterEntry* self,
                      const gchar* name,
                      const gchar* value)
{
	g_return_val_if_fail (self != NULL, FALSE);
	return FILTER_ENTRY_GET_CLASS (self)->matches (self, name, value);
}

static void
value_filter_entry_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
value_filter_entry_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		filter_entry_unref (value->data[0].v_pointer);
	}
}

static void
value_filter_entry_copy_value (const GValue* src_value,
                               GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = filter_entry_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
value_filter_entry_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
value_filter_entry_collect_value (GValue* value,
                                  guint n_collect_values,
                                  GTypeCValue* collect_values,
                                  guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		FilterEntry * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = filter_entry_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
value_filter_entry_lcopy_value (const GValue* value,
                                guint n_collect_values,
                                GTypeCValue* collect_values,
                                guint collect_flags)
{
	FilterEntry ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = filter_entry_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
param_spec_filter_entry (const gchar* name,
                         const gchar* nick,
                         const gchar* blurb,
                         GType object_type,
                         GParamFlags flags)
{
	ParamSpecFilterEntry* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_FILTER_ENTRY), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
value_get_filter_entry (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY), NULL);
	return value->data[0].v_pointer;
}

void
value_set_filter_entry (GValue* value,
                        gpointer v_object)
{
	FilterEntry * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FILTER_ENTRY));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		filter_entry_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		filter_entry_unref (old);
	}
}

void
value_take_filter_entry (GValue* value,
                         gpointer v_object)
{
	FilterEntry * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FILTER_ENTRY));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		filter_entry_unref (old);
	}
}

static void
filter_entry_class_init (FilterEntryClass * klass,
                         gpointer klass_data)
{
	filter_entry_parent_class = g_type_class_peek_parent (klass);
	((FilterEntryClass *) klass)->finalize = filter_entry_finalize;
	g_type_class_adjust_private_offset (klass, &FilterEntry_private_offset);
	((FilterEntryClass *) klass)->matches = (gboolean (*) (FilterEntry*, const gchar*, const gchar*)) filter_entry_real_matches;
}

static void
filter_entry_instance_init (FilterEntry * self,
                            gpointer klass)
{
	self->priv = filter_entry_get_instance_private (self);
	self->priv->entry_name = NULL;
	self->priv->entry_value = NULL;
	self->ref_count = 1;
}

static void
filter_entry_finalize (FilterEntry * obj)
{
	FilterEntry * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_FILTER_ENTRY, FilterEntry);
	g_signal_handlers_destroy (self);
	_g_free0 (self->priv->entry_name);
	_g_free0 (self->priv->entry_value);
}

GType
filter_entry_get_type (void)
{
	static volatile gsize filter_entry_type_id__volatile = 0;
	if (g_once_init_enter (&filter_entry_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_filter_entry_init, value_filter_entry_free_value, value_filter_entry_copy_value, value_filter_entry_peek_pointer, "p", value_filter_entry_collect_value, "p", value_filter_entry_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (FilterEntryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) filter_entry_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FilterEntry), 0, (GInstanceInitFunc) filter_entry_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType filter_entry_type_id;
		filter_entry_type_id = g_type_register_fundamental (g_type_fundamental_next (), "FilterEntry", &g_define_type_info, &g_define_type_fundamental_info, 0);
		FilterEntry_private_offset = g_type_add_instance_private (filter_entry_type_id, sizeof (FilterEntryPrivate));
		g_once_init_leave (&filter_entry_type_id__volatile, filter_entry_type_id);
	}
	return filter_entry_type_id__volatile;
}

gpointer
filter_entry_ref (gpointer instance)
{
	FilterEntry * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
filter_entry_unref (gpointer instance)
{
	FilterEntry * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		FILTER_ENTRY_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

