/* rygel-tracker-upnp-property-map.c generated by valac 0.44.9, 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.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 <gee.h>
#include <stdlib.h>
#include <string.h>
#include <glib.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;
enum  {
	RYGEL_TRACKER_UPN_PPROPERTY_MAP_0_PROPERTY,
	RYGEL_TRACKER_UPN_PPROPERTY_MAP_NUM_PROPERTIES
};
static GParamSpec* rygel_tracker_upn_pproperty_map_properties[RYGEL_TRACKER_UPN_PPROPERTY_MAP_NUM_PROPERTIES];
#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 gint RygelTrackerUPnPPropertyMap_private_offset;
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;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (RygelTrackerUPnPPropertyMap, g_object_unref)
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 inline gpointer
rygel_tracker_upn_pproperty_map_get_instance_private (RygelTrackerUPnPPropertyMap* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerUPnPPropertyMap_private_offset);
}

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* _tmp0_;
	RygelTrackerUPnPPropertyMap* _tmp2_;
	RygelTrackerUPnPPropertyMap* _tmp3_;
	RygelTrackerUPnPPropertyMap* result = NULL;
	_tmp0_ = rygel_tracker_upn_pproperty_map_instance;
	if (G_UNLIKELY (_tmp0_ == NULL)) {
		RygelTrackerUPnPPropertyMap* _tmp1_;
		_tmp1_ = rygel_tracker_upn_pproperty_map_new ();
		_g_object_unref0 (rygel_tracker_upn_pproperty_map_instance);
		rygel_tracker_upn_pproperty_map_instance = _tmp1_;
	}
	_tmp2_ = rygel_tracker_upn_pproperty_map_instance;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static RygelTrackerUPnPPropertyMap*
rygel_tracker_upn_pproperty_map_construct (GType object_type)
{
	RygelTrackerUPnPPropertyMap * self = NULL;
	GeeHashMap* _tmp0_;
	GeeHashMap* _tmp1_;
	self = (RygelTrackerUPnPPropertyMap*) g_object_new (object_type, NULL);
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, GEE_TYPE_ARRAY_LIST, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) 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, (GDestroyNotify) g_free, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) 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* str = NULL;
	gchar* _tmp0_;
	GeeHashMap* _tmp1_;
	gchar* result = NULL;
	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;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp1_, property)) {
		{
			GeeArrayList* _key_list = NULL;
			GeeHashMap* _tmp2_;
			gpointer _tmp3_;
			gint _key_size = 0;
			GeeArrayList* _tmp4_;
			gint _tmp5_;
			gint _tmp6_;
			gint _key_index = 0;
			_tmp2_ = self->priv->property_map;
			_tmp3_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp2_, property);
			_key_list = (GeeArrayList*) _tmp3_;
			_tmp4_ = _key_list;
			_tmp5_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp4_);
			_tmp6_ = _tmp5_;
			_key_size = _tmp6_;
			_key_index = -1;
			while (TRUE) {
				gchar* key = NULL;
				GeeArrayList* _tmp7_;
				gpointer _tmp8_;
				const gchar* _tmp9_;
				gchar* _tmp10_;
				gchar* _tmp11_;
				const gchar* _tmp12_;
				gchar* _tmp13_;
				gchar* _tmp14_;
				gchar* _tmp15_;
				_key_index = _key_index + 1;
				if (!(_key_index < _key_size)) {
					break;
				}
				_tmp7_ = _key_list;
				_tmp8_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _key_index);
				key = (gchar*) _tmp8_;
				_tmp9_ = key;
				_tmp10_ = g_strconcat (_tmp9_, "(", NULL);
				_tmp11_ = _tmp10_;
				_tmp12_ = str;
				_tmp13_ = g_strconcat (_tmp11_, _tmp12_, NULL);
				_tmp14_ = _tmp13_;
				_tmp15_ = g_strconcat (_tmp14_, ")", NULL);
				_g_free0 (str);
				str = _tmp15_;
				_g_free0 (_tmp14_);
				_g_free0 (_tmp11_);
				_g_free0 (key);
			}
			_g_object_unref0 (_key_list);
		}
	} else {
		GeeHashMap* _tmp16_;
		_tmp16_ = self->priv->functions;
		if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp16_, property)) {
			GeeHashMap* _tmp17_;
			gpointer _tmp18_;
			gchar* _tmp19_;
			const gchar* _tmp20_;
			gchar* _tmp21_;
			_tmp17_ = self->priv->functions;
			_tmp18_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp17_, property);
			_tmp19_ = (gchar*) _tmp18_;
			_tmp20_ = str;
			_tmp21_ = g_strdup_printf (_tmp19_, _tmp20_);
			_g_free0 (str);
			str = _tmp21_;
			_g_free0 (_tmp19_);
		}
	}
	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_;
	va_list list = {0};
	gchar* key = NULL;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	GeeHashMap* _tmp8_;
	GeeArrayList* _tmp9_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (property != NULL);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) 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_;
		GeeArrayList* _tmp4_;
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		_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_ = key_chain;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp8_, property, _tmp9_);
	_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_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (property != NULL);
	g_return_if_fail (function != NULL);
	_tmp0_ = self->priv->functions;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp0_, property, function);
}

static void
rygel_tracker_upn_pproperty_map_add_alternative (RygelTrackerUPnPPropertyMap* self,
                                                 const gchar* property,
                                                 ...)
{
	va_list list = {0};
	GString* str = NULL;
	GString* _tmp0_;
	gchar* alternative = NULL;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	GString* _tmp8_;
	GString* _tmp9_;
	GString* _tmp10_;
	GString* _tmp11_;
	const gchar* _tmp12_;
	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_;
		GString* _tmp4_;
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		_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;
	g_string_truncate (_tmp8_, (gsize) (_tmp9_->len - 1));
	_tmp10_ = str;
	g_string_append (_tmp10_, ")");
	_tmp11_ = str;
	_tmp12_ = _tmp11_->str;
	rygel_tracker_upn_pproperty_map_add_function (self, property, _tmp12_);
	_g_free0 (alternative);
	_g_string_free0 (str);
	va_end (list);
}

static void
rygel_tracker_upn_pproperty_map_class_init (RygelTrackerUPnPPropertyMapClass * klass,
                                            gpointer klass_data)
{
	rygel_tracker_upn_pproperty_map_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelTrackerUPnPPropertyMap_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_upn_pproperty_map_finalize;
}

static void
rygel_tracker_upn_pproperty_map_instance_init (RygelTrackerUPnPPropertyMap * self,
                                               gpointer klass)
{
	self->priv = rygel_tracker_upn_pproperty_map_get_instance_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);
		RygelTrackerUPnPPropertyMap_private_offset = g_type_add_instance_private (rygel_tracker_upn_pproperty_map_type_id, sizeof (RygelTrackerUPnPPropertyMapPrivate));
		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;
}

