/* rygel-tracker-logical-filter.c generated by valac 0.15.2.4-51044, the Vala compiler
 * generated from rygel-tracker-logical-filter.vala, do not modify */

/*
 * Copyright (C) 2010 MediaNet Inh.
 *
 * Author: Sunil Mohan Adapa <sunil@medhas.org>
 *
 * 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 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

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


#define RYGEL_TRACKER_TYPE_QUERY_FILTER (rygel_tracker_query_filter_get_type ())
#define RYGEL_TRACKER_QUERY_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_QUERY_FILTER, RygelTrackerQueryFilter))
#define RYGEL_TRACKER_IS_QUERY_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_QUERY_FILTER))
#define RYGEL_TRACKER_QUERY_FILTER_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), RYGEL_TRACKER_TYPE_QUERY_FILTER, RygelTrackerQueryFilterIface))

typedef struct _RygelTrackerQueryFilter RygelTrackerQueryFilter;
typedef struct _RygelTrackerQueryFilterIface RygelTrackerQueryFilterIface;

#define RYGEL_TRACKER_TYPE_LOGICAL_FILTER (rygel_tracker_logical_filter_get_type ())
#define RYGEL_TRACKER_LOGICAL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_LOGICAL_FILTER, RygelTrackerLogicalFilter))
#define RYGEL_TRACKER_LOGICAL_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_LOGICAL_FILTER, RygelTrackerLogicalFilterClass))
#define RYGEL_TRACKER_IS_LOGICAL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_LOGICAL_FILTER))
#define RYGEL_TRACKER_IS_LOGICAL_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_LOGICAL_FILTER))
#define RYGEL_TRACKER_LOGICAL_FILTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_LOGICAL_FILTER, RygelTrackerLogicalFilterClass))

typedef struct _RygelTrackerLogicalFilter RygelTrackerLogicalFilter;
typedef struct _RygelTrackerLogicalFilterClass RygelTrackerLogicalFilterClass;
typedef struct _RygelTrackerLogicalFilterPrivate RygelTrackerLogicalFilterPrivate;

#define RYGEL_TRACKER_LOGICAL_FILTER_TYPE_OPERATOR (rygel_tracker_logical_filter_operator_get_type ())
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define RYGEL_TRACKER_TYPE_BOOLEAN_FILTER (rygel_tracker_boolean_filter_get_type ())
#define RYGEL_TRACKER_BOOLEAN_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_BOOLEAN_FILTER, RygelTrackerBooleanFilter))
#define RYGEL_TRACKER_BOOLEAN_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_BOOLEAN_FILTER, RygelTrackerBooleanFilterClass))
#define RYGEL_TRACKER_IS_BOOLEAN_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_BOOLEAN_FILTER))
#define RYGEL_TRACKER_IS_BOOLEAN_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_BOOLEAN_FILTER))
#define RYGEL_TRACKER_BOOLEAN_FILTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_BOOLEAN_FILTER, RygelTrackerBooleanFilterClass))

typedef struct _RygelTrackerBooleanFilter RygelTrackerBooleanFilter;
typedef struct _RygelTrackerBooleanFilterClass RygelTrackerBooleanFilterClass;
typedef struct _RygelTrackerBooleanFilterPrivate RygelTrackerBooleanFilterPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _RygelTrackerQueryFilterIface {
	GTypeInterface parent_iface;
	gchar* (*to_string) (RygelTrackerQueryFilter* self);
};

typedef enum  {
	RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_AND,
	RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_OR,
	RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_NOT
} RygelTrackerLogicalFilterOperator;

struct _RygelTrackerLogicalFilter {
	GObject parent_instance;
	RygelTrackerLogicalFilterPrivate * priv;
	RygelTrackerLogicalFilterOperator op;
	RygelTrackerQueryFilter* operand1;
	RygelTrackerQueryFilter* operand2;
};

struct _RygelTrackerLogicalFilterClass {
	GObjectClass parent_class;
};

struct _RygelTrackerBooleanFilter {
	GObject parent_instance;
	RygelTrackerBooleanFilterPrivate * priv;
	gboolean value;
};

struct _RygelTrackerBooleanFilterClass {
	GObjectClass parent_class;
};


static gpointer rygel_tracker_logical_filter_parent_class = NULL;
static RygelTrackerQueryFilterIface* rygel_tracker_logical_filter_rygel_tracker_query_filter_parent_iface = NULL;

GType rygel_tracker_query_filter_get_type (void) G_GNUC_CONST;
GType rygel_tracker_logical_filter_get_type (void) G_GNUC_CONST;
GType rygel_tracker_logical_filter_operator_get_type (void) G_GNUC_CONST;
enum  {
	RYGEL_TRACKER_LOGICAL_FILTER_DUMMY_PROPERTY
};
RygelTrackerLogicalFilter* rygel_tracker_logical_filter_new (RygelTrackerLogicalFilterOperator op, RygelTrackerQueryFilter* operand1, RygelTrackerQueryFilter* operand2);
RygelTrackerLogicalFilter* rygel_tracker_logical_filter_construct (GType object_type, RygelTrackerLogicalFilterOperator op, RygelTrackerQueryFilter* operand1, RygelTrackerQueryFilter* operand2);
RygelTrackerQueryFilter* rygel_tracker_logical_filter_simplify (RygelTrackerLogicalFilter* self);
GType rygel_tracker_boolean_filter_get_type (void) G_GNUC_CONST;
static gchar* rygel_tracker_logical_filter_real_to_string (RygelTrackerQueryFilter* base);
gchar* rygel_tracker_query_filter_to_string (RygelTrackerQueryFilter* self);
static void rygel_tracker_logical_filter_finalize (GObject* obj);


GType rygel_tracker_logical_filter_operator_get_type (void) {
	static volatile gsize rygel_tracker_logical_filter_operator_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_tracker_logical_filter_operator_type_id__volatile)) {
		static const GEnumValue values[] = {{RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_AND, "RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_AND", "and"}, {RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_OR, "RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_OR", "or"}, {RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_NOT, "RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_NOT", "not"}, {0, NULL, NULL}};
		GType rygel_tracker_logical_filter_operator_type_id;
		rygel_tracker_logical_filter_operator_type_id = g_enum_register_static ("RygelTrackerLogicalFilterOperator", values);
		g_once_init_leave (&rygel_tracker_logical_filter_operator_type_id__volatile, rygel_tracker_logical_filter_operator_type_id);
	}
	return rygel_tracker_logical_filter_operator_type_id__volatile;
}


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


RygelTrackerLogicalFilter* rygel_tracker_logical_filter_construct (GType object_type, RygelTrackerLogicalFilterOperator op, RygelTrackerQueryFilter* operand1, RygelTrackerQueryFilter* operand2) {
	RygelTrackerLogicalFilter * self = NULL;
	RygelTrackerLogicalFilterOperator _tmp0_;
	RygelTrackerQueryFilter* _tmp1_;
	RygelTrackerQueryFilter* _tmp2_;
	RygelTrackerQueryFilter* _tmp3_;
	RygelTrackerQueryFilter* _tmp4_;
	g_return_val_if_fail (operand1 != NULL, NULL);
	self = (RygelTrackerLogicalFilter*) g_object_new (object_type, NULL);
	_tmp0_ = op;
	self->op = _tmp0_;
	_tmp1_ = operand1;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	_g_object_unref0 (self->operand1);
	self->operand1 = _tmp2_;
	_tmp3_ = operand2;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_g_object_unref0 (self->operand2);
	self->operand2 = _tmp4_;
	return self;
}


RygelTrackerLogicalFilter* rygel_tracker_logical_filter_new (RygelTrackerLogicalFilterOperator op, RygelTrackerQueryFilter* operand1, RygelTrackerQueryFilter* operand2) {
	return rygel_tracker_logical_filter_construct (RYGEL_TRACKER_TYPE_LOGICAL_FILTER, op, operand1, operand2);
}


/**
     * Creates a simplified version of this expressions if it involves boolean
     * constants.
     */
RygelTrackerQueryFilter* rygel_tracker_logical_filter_simplify (RygelTrackerLogicalFilter* self) {
	RygelTrackerQueryFilter* result = NULL;
	gboolean _tmp0_ = FALSE;
	RygelTrackerQueryFilter* _tmp1_;
	gboolean _tmp3_;
	gboolean _tmp5_ = FALSE;
	RygelTrackerLogicalFilterOperator _tmp6_;
	gboolean _tmp8_;
	RygelTrackerBooleanFilter* bool_filter = NULL;
	RygelTrackerQueryFilter* operand = NULL;
	RygelTrackerQueryFilter* _tmp16_;
	gboolean _tmp25_ = FALSE;
	gboolean _tmp26_ = FALSE;
	RygelTrackerBooleanFilter* _tmp27_;
	gboolean _tmp28_;
	gboolean _tmp30_;
	gboolean _tmp36_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp1_ = self->operand1;
	if (RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp1_)) {
		_tmp0_ = TRUE;
	} else {
		RygelTrackerQueryFilter* _tmp2_;
		_tmp2_ = self->operand2;
		_tmp0_ = RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp2_);
	}
	_tmp3_ = _tmp0_;
	if (!_tmp3_) {
		RygelTrackerQueryFilter* _tmp4_;
		_tmp4_ = _g_object_ref0 ((RygelTrackerQueryFilter*) self);
		result = _tmp4_;
		return result;
	}
	_tmp6_ = self->op;
	if (_tmp6_ == RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_NOT) {
		RygelTrackerQueryFilter* _tmp7_;
		_tmp7_ = self->operand1;
		_tmp5_ = RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp7_);
	} else {
		_tmp5_ = FALSE;
	}
	_tmp8_ = _tmp5_;
	if (_tmp8_) {
		RygelTrackerQueryFilter* _tmp9_;
		RygelTrackerBooleanFilter* _tmp10_;
		RygelTrackerBooleanFilter* bool_filter;
		RygelTrackerBooleanFilter* _tmp11_;
		RygelTrackerBooleanFilter* _tmp12_;
		gboolean _tmp13_;
		RygelTrackerQueryFilter* _tmp14_;
		RygelTrackerQueryFilter* _tmp15_;
		_tmp9_ = self->operand1;
		_tmp10_ = _g_object_ref0 (RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp9_) ? ((RygelTrackerBooleanFilter*) _tmp9_) : NULL);
		bool_filter = _tmp10_;
		_tmp11_ = bool_filter;
		_tmp12_ = bool_filter;
		_tmp13_ = _tmp12_->value;
		_tmp11_->value = !_tmp13_;
		_tmp14_ = self->operand1;
		_tmp15_ = _g_object_ref0 (_tmp14_);
		result = _tmp15_;
		_g_object_unref0 (bool_filter);
		return result;
	}
	_tmp16_ = self->operand1;
	if (RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp16_)) {
		RygelTrackerQueryFilter* _tmp17_;
		RygelTrackerBooleanFilter* _tmp18_;
		RygelTrackerQueryFilter* _tmp19_;
		RygelTrackerQueryFilter* _tmp20_;
		_tmp17_ = self->operand1;
		_tmp18_ = _g_object_ref0 (RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp17_) ? ((RygelTrackerBooleanFilter*) _tmp17_) : NULL);
		_g_object_unref0 (bool_filter);
		bool_filter = _tmp18_;
		_tmp19_ = self->operand2;
		_tmp20_ = _g_object_ref0 (_tmp19_);
		_g_object_unref0 (operand);
		operand = _tmp20_;
	} else {
		RygelTrackerQueryFilter* _tmp21_;
		RygelTrackerBooleanFilter* _tmp22_;
		RygelTrackerQueryFilter* _tmp23_;
		RygelTrackerQueryFilter* _tmp24_;
		_tmp21_ = self->operand2;
		_tmp22_ = _g_object_ref0 (RYGEL_TRACKER_IS_BOOLEAN_FILTER (_tmp21_) ? ((RygelTrackerBooleanFilter*) _tmp21_) : NULL);
		_g_object_unref0 (bool_filter);
		bool_filter = _tmp22_;
		_tmp23_ = self->operand1;
		_tmp24_ = _g_object_ref0 (_tmp23_);
		_g_object_unref0 (operand);
		operand = _tmp24_;
	}
	_tmp27_ = bool_filter;
	_tmp28_ = _tmp27_->value;
	if (_tmp28_) {
		RygelTrackerLogicalFilterOperator _tmp29_;
		_tmp29_ = self->op;
		_tmp26_ = _tmp29_ == RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_OR;
	} else {
		_tmp26_ = FALSE;
	}
	_tmp30_ = _tmp26_;
	if (_tmp30_) {
		_tmp25_ = TRUE;
	} else {
		gboolean _tmp31_ = FALSE;
		RygelTrackerBooleanFilter* _tmp32_;
		gboolean _tmp33_;
		gboolean _tmp35_;
		_tmp32_ = bool_filter;
		_tmp33_ = _tmp32_->value;
		if (!_tmp33_) {
			RygelTrackerLogicalFilterOperator _tmp34_;
			_tmp34_ = self->op;
			_tmp31_ = _tmp34_ == RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_AND;
		} else {
			_tmp31_ = FALSE;
		}
		_tmp35_ = _tmp31_;
		_tmp25_ = _tmp35_;
	}
	_tmp36_ = _tmp25_;
	if (_tmp36_) {
		result = (RygelTrackerQueryFilter*) bool_filter;
		_g_object_unref0 (operand);
		return result;
	} else {
		result = operand;
		_g_object_unref0 (bool_filter);
		return result;
	}
	_g_object_unref0 (operand);
	_g_object_unref0 (bool_filter);
}


static gchar* rygel_tracker_logical_filter_real_to_string (RygelTrackerQueryFilter* base) {
	RygelTrackerLogicalFilter * self;
	gchar* result = NULL;
	RygelTrackerQueryFilter* _tmp0_;
	gchar* _tmp1_ = NULL;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* str;
	RygelTrackerLogicalFilterOperator _tmp7_;
	RygelTrackerLogicalFilterOperator _tmp10_;
	const gchar* _tmp15_;
	RygelTrackerQueryFilter* _tmp16_;
	gchar* _tmp17_ = NULL;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	self = (RygelTrackerLogicalFilter*) base;
	_tmp0_ = self->operand1;
	_tmp1_ = rygel_tracker_query_filter_to_string (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strconcat ("(", _tmp2_, NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_strconcat (_tmp4_, ")", NULL);
	_tmp6_ = _tmp5_;
	_g_free0 (_tmp4_);
	_g_free0 (_tmp2_);
	str = _tmp6_;
	_tmp7_ = self->op;
	if (_tmp7_ == RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_NOT) {
		const gchar* _tmp8_;
		gchar* _tmp9_;
		_tmp8_ = str;
		_tmp9_ = g_strconcat ("!", _tmp8_, NULL);
		result = _tmp9_;
		_g_free0 (str);
		return result;
	}
	_tmp10_ = self->op;
	switch (_tmp10_) {
		case RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_AND:
		{
			const gchar* _tmp11_;
			gchar* _tmp12_;
			_tmp11_ = str;
			_tmp12_ = g_strconcat (_tmp11_, " && ", NULL);
			_g_free0 (str);
			str = _tmp12_;
			break;
		}
		case RYGEL_TRACKER_LOGICAL_FILTER_OPERATOR_OR:
		{
			const gchar* _tmp13_;
			gchar* _tmp14_;
			_tmp13_ = str;
			_tmp14_ = g_strconcat (_tmp13_, " || ", NULL);
			_g_free0 (str);
			str = _tmp14_;
			break;
		}
		default:
		{
			g_assert_not_reached ();
		}
	}
	_tmp15_ = str;
	_tmp16_ = self->operand2;
	_tmp17_ = rygel_tracker_query_filter_to_string (_tmp16_);
	_tmp18_ = _tmp17_;
	_tmp19_ = g_strconcat ("(", _tmp18_, NULL);
	_tmp20_ = _tmp19_;
	_tmp21_ = g_strconcat (_tmp20_, ")", NULL);
	_tmp22_ = _tmp21_;
	_tmp23_ = g_strconcat (_tmp15_, _tmp22_, NULL);
	_g_free0 (str);
	str = _tmp23_;
	_g_free0 (_tmp22_);
	_g_free0 (_tmp20_);
	_g_free0 (_tmp18_);
	result = str;
	return result;
}


static void rygel_tracker_logical_filter_class_init (RygelTrackerLogicalFilterClass * klass) {
	rygel_tracker_logical_filter_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_logical_filter_finalize;
}


static void rygel_tracker_logical_filter_rygel_tracker_query_filter_interface_init (RygelTrackerQueryFilterIface * iface) {
	rygel_tracker_logical_filter_rygel_tracker_query_filter_parent_iface = g_type_interface_peek_parent (iface);
	iface->to_string = (gchar* (*)(RygelTrackerQueryFilter*)) rygel_tracker_logical_filter_real_to_string;
}


static void rygel_tracker_logical_filter_instance_init (RygelTrackerLogicalFilter * self) {
}


static void rygel_tracker_logical_filter_finalize (GObject* obj) {
	RygelTrackerLogicalFilter * self;
	self = RYGEL_TRACKER_LOGICAL_FILTER (obj);
	_g_object_unref0 (self->operand1);
	_g_object_unref0 (self->operand2);
	G_OBJECT_CLASS (rygel_tracker_logical_filter_parent_class)->finalize (obj);
}


/**
 * Logical expressions in SPARQL query filter
 */
GType rygel_tracker_logical_filter_get_type (void) {
	static volatile gsize rygel_tracker_logical_filter_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_tracker_logical_filter_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerLogicalFilterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_logical_filter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerLogicalFilter), 0, (GInstanceInitFunc) rygel_tracker_logical_filter_instance_init, NULL };
		static const GInterfaceInfo rygel_tracker_query_filter_info = { (GInterfaceInitFunc) rygel_tracker_logical_filter_rygel_tracker_query_filter_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType rygel_tracker_logical_filter_type_id;
		rygel_tracker_logical_filter_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelTrackerLogicalFilter", &g_define_type_info, 0);
		g_type_add_interface_static (rygel_tracker_logical_filter_type_id, RYGEL_TRACKER_TYPE_QUERY_FILTER, &rygel_tracker_query_filter_info);
		g_once_init_leave (&rygel_tracker_logical_filter_type_id__volatile, rygel_tracker_logical_filter_type_id);
	}
	return rygel_tracker_logical_filter_type_id__volatile;
}



