/* rygel-tracker-upnp-property-map.c generated by valac 0.25.1.16-16e7a, the Vala compiler
 * generated from rygel-tracker-upnp-property-map.vala, do not modify */

/*
 * Copyright (C) 2010 MediaNet Inh.
 * Copyright (C) 2012 Jens Georg <mail@jensge.org>
 *
 * 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 <gee.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>


#define RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP (rygel_tracker_upn_pproperty_map_get_type ())
#define RYGEL_TRACKER_UPN_PPROPERTY_MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP, RygelTrackerUPnPPropertyMap))
#define RYGEL_TRACKER_UPN_PPROPERTY_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP, RygelTrackerUPnPPropertyMapClass))
#define RYGEL_TRACKER_IS_UPN_PPROPERTY_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP))
#define RYGEL_TRACKER_IS_UPN_PPROPERTY_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP))
#define RYGEL_TRACKER_UPN_PPROPERTY_MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP, RygelTrackerUPnPPropertyMapClass))

typedef struct _RygelTrackerUPnPPropertyMap RygelTrackerUPnPPropertyMap;
typedef struct _RygelTrackerUPnPPropertyMapClass RygelTrackerUPnPPropertyMapClass;
typedef struct _RygelTrackerUPnPPropertyMapPrivate RygelTrackerUPnPPropertyMapPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _RygelTrackerUPnPPropertyMap {
	GObject parent_instance;
	RygelTrackerUPnPPropertyMapPrivate * priv;
};

struct _RygelTrackerUPnPPropertyMapClass {
	GObjectClass parent_class;
};

struct _RygelTrackerUPnPPropertyMapPrivate {
	GeeHashMap* property_map;
	GeeHashMap* functions;
};


static gpointer rygel_tracker_upn_pproperty_map_parent_class = NULL;
static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_instance;
static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_instance = NULL;

GType rygel_tracker_upn_pproperty_map_get_type (void) G_GNUC_CONST;
#define RYGEL_TRACKER_UPN_PPROPERTY_MAP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP, RygelTrackerUPnPPropertyMapPrivate))
enum  {
	RYGEL_TRACKER_UPN_PPROPERTY_MAP_DUMMY_PROPERTY
};
RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_get_property_map (void);
static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_new (void);
static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_construct (GType object_type);
static void rygel_tracker_upn_pproperty_map_add_key_chain (RygelTrackerUPnPPropertyMap* self, const gchar* property, ...);
static void rygel_tracker_upn_pproperty_map_add_function (RygelTrackerUPnPPropertyMap* self, const gchar* property, const gchar* function);
static void rygel_tracker_upn_pproperty_map_add_alternative (RygelTrackerUPnPPropertyMap* self, const gchar* property, ...);
gchar* rygel_tracker_upn_pproperty_map_get (RygelTrackerUPnPPropertyMap* self, const gchar* property);
#define RYGEL_TRACKER_SELECTION_QUERY_ITEM_VARIABLE "?item"
static void rygel_tracker_upn_pproperty_map_finalize (GObject* obj);


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


RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_get_property_map (void) {
	RygelTrackerUPnPPropertyMap* result = NULL;
	RygelTrackerUPnPPropertyMap* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	RygelTrackerUPnPPropertyMap* _tmp3_ = NULL;
	RygelTrackerUPnPPropertyMap* _tmp4_ = NULL;
	_tmp0_ = rygel_tracker_upn_pproperty_map_instance;
	_tmp1_ = G_UNLIKELY (_tmp0_ == NULL);
	if (_tmp1_) {
		RygelTrackerUPnPPropertyMap* _tmp2_ = NULL;
		_tmp2_ = rygel_tracker_upn_pproperty_map_new ();
		_g_object_unref0 (rygel_tracker_upn_pproperty_map_instance);
		rygel_tracker_upn_pproperty_map_instance = _tmp2_;
	}
	_tmp3_ = rygel_tracker_upn_pproperty_map_instance;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	result = _tmp4_;
	return result;
}


static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_construct (GType object_type) {
	RygelTrackerUPnPPropertyMap * self = NULL;
	GeeHashMap* _tmp0_ = NULL;
	GeeHashMap* _tmp1_ = NULL;
	self = (RygelTrackerUPnPPropertyMap*) g_object_new (object_type, NULL);
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, GEE_TYPE_ARRAY_LIST, (GBoxedCopyFunc) g_object_ref, g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->property_map);
	self->priv->property_map = _tmp0_;
	_tmp1_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->functions);
	self->priv->functions = _tmp1_;
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "res", "nie:url", NULL);
	rygel_tracker_upn_pproperty_map_add_function (self, "place_holder", "tracker:coalesce((SELECT false WHERE { { %s a ?o } " "FILTER (?o IN (nfo:FileDataObject, " "nfo:RemoteDataObject)) }), true)");
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "fileName", "nfo:fileName", NULL);
	rygel_tracker_upn_pproperty_map_add_alternative (self, "dc:title", "nie:title", "nfo:fileName", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "dlnaProfile", "nmm:dlnaProfile", NULL);
	rygel_tracker_upn_pproperty_map_add_alternative (self, "mimeType", "nmm:dlnaMime", "nie:mimeType", NULL);
	rygel_tracker_upn_pproperty_map_add_alternative (self, "res@size", "nfo:fileSize", "nie:byteSize", NULL);
	rygel_tracker_upn_pproperty_map_add_alternative (self, "date", "nie:contentCreated", "nfo:fileLastModified", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "res@duration", "nfo:duration", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:artist", "nmm:performer", "nmm:artistName", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "dc:creator", "nmm:performer", "nmm:artistName", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:album", "nmm:musicAlbum", "nmm:albumTitle", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:originalTrackNumber", "nmm:trackNumber", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:genre", "nfo:genre", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "sampleRate", "nfo:sampleRate", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:nrAudioChannels", "nfo:channels", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:bitsPerSample", "nfo:bitsPerSample", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "upnp:bitrate", "nfo:averageBitrate", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "width", "nfo:width", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "height", "nfo:height", NULL);
	rygel_tracker_upn_pproperty_map_add_key_chain (self, "rygel:originalVolumeNumber", "nmm:musicAlbumDisc", "nmm:setNumber", NULL);
	return self;
}


static RygelTrackerUPnPPropertyMap* rygel_tracker_upn_pproperty_map_new (void) {
	return rygel_tracker_upn_pproperty_map_construct (RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP);
}


gchar* rygel_tracker_upn_pproperty_map_get (RygelTrackerUPnPPropertyMap* self, const gchar* property) {
	gchar* result = NULL;
	gchar* str = NULL;
	gchar* _tmp0_ = NULL;
	GeeHashMap* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	gboolean _tmp3_ = FALSE;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (property != NULL, NULL);
	_tmp0_ = g_strdup (RYGEL_TRACKER_SELECTION_QUERY_ITEM_VARIABLE);
	str = _tmp0_;
	_tmp1_ = self->priv->property_map;
	_tmp2_ = property;
	_tmp3_ = gee_abstract_map_has_key ((GeeAbstractMap*) _tmp1_, _tmp2_);
	if (_tmp3_) {
		{
			GeeArrayList* _key_list = NULL;
			GeeHashMap* _tmp4_ = NULL;
			const gchar* _tmp5_ = NULL;
			gpointer _tmp6_ = NULL;
			gint _key_size = 0;
			GeeArrayList* _tmp7_ = NULL;
			gint _tmp8_ = 0;
			gint _tmp9_ = 0;
			gint _key_index = 0;
			_tmp4_ = self->priv->property_map;
			_tmp5_ = property;
			_tmp6_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp4_, _tmp5_);
			_key_list = (GeeArrayList*) _tmp6_;
			_tmp7_ = _key_list;
			_tmp8_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp7_);
			_tmp9_ = _tmp8_;
			_key_size = _tmp9_;
			_key_index = -1;
			while (TRUE) {
				gint _tmp10_ = 0;
				gint _tmp11_ = 0;
				gint _tmp12_ = 0;
				gchar* key = NULL;
				GeeArrayList* _tmp13_ = NULL;
				gint _tmp14_ = 0;
				gpointer _tmp15_ = NULL;
				const gchar* _tmp16_ = NULL;
				gchar* _tmp17_ = NULL;
				gchar* _tmp18_ = NULL;
				const gchar* _tmp19_ = NULL;
				gchar* _tmp20_ = NULL;
				gchar* _tmp21_ = NULL;
				gchar* _tmp22_ = NULL;
				_tmp10_ = _key_index;
				_key_index = _tmp10_ + 1;
				_tmp11_ = _key_index;
				_tmp12_ = _key_size;
				if (!(_tmp11_ < _tmp12_)) {
					break;
				}
				_tmp13_ = _key_list;
				_tmp14_ = _key_index;
				_tmp15_ = gee_abstract_list_get ((GeeAbstractList*) _tmp13_, _tmp14_);
				key = (gchar*) _tmp15_;
				_tmp16_ = key;
				_tmp17_ = g_strconcat (_tmp16_, "(", NULL);
				_tmp18_ = _tmp17_;
				_tmp19_ = str;
				_tmp20_ = g_strconcat (_tmp18_, _tmp19_, NULL);
				_tmp21_ = _tmp20_;
				_tmp22_ = g_strconcat (_tmp21_, ")", NULL);
				_g_free0 (str);
				str = _tmp22_;
				_g_free0 (_tmp21_);
				_g_free0 (_tmp18_);
				_g_free0 (key);
			}
			_g_object_unref0 (_key_list);
		}
	} else {
		GeeHashMap* _tmp23_ = NULL;
		const gchar* _tmp24_ = NULL;
		gboolean _tmp25_ = FALSE;
		_tmp23_ = self->priv->functions;
		_tmp24_ = property;
		_tmp25_ = gee_abstract_map_has_key ((GeeAbstractMap*) _tmp23_, _tmp24_);
		if (_tmp25_) {
			GeeHashMap* _tmp26_ = NULL;
			const gchar* _tmp27_ = NULL;
			gpointer _tmp28_ = NULL;
			gchar* _tmp29_ = NULL;
			const gchar* _tmp30_ = NULL;
			gchar* _tmp31_ = NULL;
			_tmp26_ = self->priv->functions;
			_tmp27_ = property;
			_tmp28_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp26_, _tmp27_);
			_tmp29_ = (gchar*) _tmp28_;
			_tmp30_ = str;
			_tmp31_ = g_strdup_printf (_tmp29_, _tmp30_);
			_g_free0 (str);
			str = _tmp31_;
			_g_free0 (_tmp29_);
		}
	}
	result = str;
	return result;
}


static void rygel_tracker_upn_pproperty_map_add_key_chain (RygelTrackerUPnPPropertyMap* self, const gchar* property, ...) {
	GeeArrayList* key_chain = NULL;
	GeeArrayList* _tmp0_ = NULL;
	va_list list = {0};
	gchar* key = NULL;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	GeeHashMap* _tmp8_ = NULL;
	const gchar* _tmp9_ = NULL;
	GeeArrayList* _tmp10_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (property != NULL);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL, NULL, NULL);
	key_chain = _tmp0_;
	va_start (list, property);
	_tmp1_ = va_arg (list, gchar*);
	_tmp2_ = g_strdup (_tmp1_);
	key = _tmp2_;
	while (TRUE) {
		const gchar* _tmp3_ = NULL;
		GeeArrayList* _tmp4_ = NULL;
		const gchar* _tmp5_ = NULL;
		const gchar* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		_tmp3_ = key;
		if (!(_tmp3_ != NULL)) {
			break;
		}
		_tmp4_ = key_chain;
		_tmp5_ = key;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp4_, _tmp5_);
		_tmp6_ = va_arg (list, gchar*);
		_tmp7_ = g_strdup (_tmp6_);
		_g_free0 (key);
		key = _tmp7_;
	}
	_tmp8_ = self->priv->property_map;
	_tmp9_ = property;
	_tmp10_ = key_chain;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp8_, _tmp9_, _tmp10_);
	_g_free0 (key);
	va_end (list);
	_g_object_unref0 (key_chain);
}


static void rygel_tracker_upn_pproperty_map_add_function (RygelTrackerUPnPPropertyMap* self, const gchar* property, const gchar* function) {
	GeeHashMap* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (property != NULL);
	g_return_if_fail (function != NULL);
	_tmp0_ = self->priv->functions;
	_tmp1_ = property;
	_tmp2_ = function;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp0_, _tmp1_, _tmp2_);
}


static void rygel_tracker_upn_pproperty_map_add_alternative (RygelTrackerUPnPPropertyMap* self, const gchar* property, ...) {
	va_list list = {0};
	GString* str = NULL;
	GString* _tmp0_ = NULL;
	gchar* alternative = NULL;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	GString* _tmp8_ = NULL;
	GString* _tmp9_ = NULL;
	gssize _tmp10_ = 0L;
	GString* _tmp11_ = NULL;
	const gchar* _tmp12_ = NULL;
	GString* _tmp13_ = NULL;
	const gchar* _tmp14_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (property != NULL);
	va_start (list, property);
	_tmp0_ = g_string_new ("tracker:coalesce(");
	str = _tmp0_;
	_tmp1_ = va_arg (list, gchar*);
	_tmp2_ = g_strdup (_tmp1_);
	alternative = _tmp2_;
	while (TRUE) {
		const gchar* _tmp3_ = NULL;
		GString* _tmp4_ = NULL;
		const gchar* _tmp5_ = NULL;
		const gchar* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		_tmp3_ = alternative;
		if (!(_tmp3_ != NULL)) {
			break;
		}
		_tmp4_ = str;
		_tmp5_ = alternative;
		g_string_append_printf (_tmp4_, "%s(%%1$s),", _tmp5_);
		_tmp6_ = va_arg (list, gchar*);
		_tmp7_ = g_strdup (_tmp6_);
		_g_free0 (alternative);
		alternative = _tmp7_;
	}
	_tmp8_ = str;
	_tmp9_ = str;
	_tmp10_ = _tmp9_->len;
	g_string_truncate (_tmp8_, (gsize) (_tmp10_ - 1));
	_tmp11_ = str;
	g_string_append (_tmp11_, ")");
	_tmp12_ = property;
	_tmp13_ = str;
	_tmp14_ = _tmp13_->str;
	rygel_tracker_upn_pproperty_map_add_function (self, _tmp12_, _tmp14_);
	_g_free0 (alternative);
	_g_string_free0 (str);
	va_end (list);
}


static void rygel_tracker_upn_pproperty_map_class_init (RygelTrackerUPnPPropertyMapClass * klass) {
	rygel_tracker_upn_pproperty_map_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelTrackerUPnPPropertyMapPrivate));
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_upn_pproperty_map_finalize;
}


static void rygel_tracker_upn_pproperty_map_instance_init (RygelTrackerUPnPPropertyMap * self) {
	self->priv = RYGEL_TRACKER_UPN_PPROPERTY_MAP_GET_PRIVATE (self);
}


static void rygel_tracker_upn_pproperty_map_finalize (GObject* obj) {
	RygelTrackerUPnPPropertyMap * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_UPN_PPROPERTY_MAP, RygelTrackerUPnPPropertyMap);
	_g_object_unref0 (self->priv->property_map);
	_g_object_unref0 (self->priv->functions);
	G_OBJECT_CLASS (rygel_tracker_upn_pproperty_map_parent_class)->finalize (obj);
}


/**
 * A map of UPnP properties to tracker property functions, coalesces,
 * subqueries or other custom functions
 */
GType rygel_tracker_upn_pproperty_map_get_type (void) {
	static volatile gsize rygel_tracker_upn_pproperty_map_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_tracker_upn_pproperty_map_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerUPnPPropertyMapClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_upn_pproperty_map_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerUPnPPropertyMap), 0, (GInstanceInitFunc) rygel_tracker_upn_pproperty_map_instance_init, NULL };
		GType rygel_tracker_upn_pproperty_map_type_id;
		rygel_tracker_upn_pproperty_map_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelTrackerUPnPPropertyMap", &g_define_type_info, 0);
		g_once_init_leave (&rygel_tracker_upn_pproperty_map_type_id__volatile, rygel_tracker_upn_pproperty_map_type_id);
	}
	return rygel_tracker_upn_pproperty_map_type_id__volatile;
}



