/* rygel-tracker-key-chain-map.c generated by valac 0.15.1.36-84b2b, the Vala compiler
 * generated from rygel-tracker-key-chain-map.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 <gee.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>


#define RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP (rygel_tracker_key_chain_map_get_type ())
#define RYGEL_TRACKER_KEY_CHAIN_MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP, RygelTrackerKeyChainMap))
#define RYGEL_TRACKER_KEY_CHAIN_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP, RygelTrackerKeyChainMapClass))
#define RYGEL_TRACKER_IS_KEY_CHAIN_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP))
#define RYGEL_TRACKER_IS_KEY_CHAIN_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP))
#define RYGEL_TRACKER_KEY_CHAIN_MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP, RygelTrackerKeyChainMapClass))

typedef struct _RygelTrackerKeyChainMap RygelTrackerKeyChainMap;
typedef struct _RygelTrackerKeyChainMapClass RygelTrackerKeyChainMapClass;
typedef struct _RygelTrackerKeyChainMapPrivate RygelTrackerKeyChainMapPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _RygelTrackerKeyChainMap {
	GObject parent_instance;
	RygelTrackerKeyChainMapPrivate * priv;
};

struct _RygelTrackerKeyChainMapClass {
	GObjectClass parent_class;
};

struct _RygelTrackerKeyChainMapPrivate {
	GeeHashMap* key_chain_map;
	GeeHashMap* functions;
};


static gpointer rygel_tracker_key_chain_map_parent_class = NULL;
static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_instance;
static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_instance = NULL;

GType rygel_tracker_key_chain_map_get_type (void) G_GNUC_CONST;
#define RYGEL_TRACKER_KEY_CHAIN_MAP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP, RygelTrackerKeyChainMapPrivate))
enum  {
	RYGEL_TRACKER_KEY_CHAIN_MAP_DUMMY_PROPERTY
};
RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_get_key_chain_map (void);
static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_new (void);
static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_construct (GType object_type);
static void rygel_tracker_key_chain_map_add_key_chain (RygelTrackerKeyChainMap* self, const gchar* property, ...);
static void rygel_tracker_key_chain_map_add_function (RygelTrackerKeyChainMap* self, const gchar* property, const gchar* function);
gchar* rygel_tracker_key_chain_map_map_property (RygelTrackerKeyChainMap* self, const gchar* property);
#define RYGEL_TRACKER_SELECTION_QUERY_ITEM_VARIABLE "?item"
static void rygel_tracker_key_chain_map_finalize (GObject* obj);


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


RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_get_key_chain_map (void) {
	RygelTrackerKeyChainMap* result = NULL;
	RygelTrackerKeyChainMap* _tmp0_;
	gboolean _tmp1_ = FALSE;
	RygelTrackerKeyChainMap* _tmp3_;
	RygelTrackerKeyChainMap* _tmp4_;
	_tmp0_ = rygel_tracker_key_chain_map_instance;
	_tmp1_ = G_UNLIKELY (_tmp0_ == NULL);
	if (_tmp1_) {
		RygelTrackerKeyChainMap* _tmp2_;
		_tmp2_ = rygel_tracker_key_chain_map_new ();
		_g_object_unref0 (rygel_tracker_key_chain_map_instance);
		rygel_tracker_key_chain_map_instance = _tmp2_;
	}
	_tmp3_ = rygel_tracker_key_chain_map_instance;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	result = _tmp4_;
	return result;
}


static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_construct (GType object_type) {
	RygelTrackerKeyChainMap * self = NULL;
	GeeHashMap* _tmp0_;
	GeeHashMap* _tmp1_;
	self = (RygelTrackerKeyChainMap*) 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);
	_g_object_unref0 (self->priv->key_chain_map);
	self->priv->key_chain_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);
	_g_object_unref0 (self->priv->functions);
	self->priv->functions = _tmp1_;
	rygel_tracker_key_chain_map_add_key_chain (self, "res", "nie:url", NULL);
	rygel_tracker_key_chain_map_add_function (self, "place_holder", "tracker:coalesce((SELECT false WHERE { { %s a ?o } " "FILTER (?o IN (nfo:FileDataObject, " "nfo:RemoteDataObject)) }), true)");
	rygel_tracker_key_chain_map_add_key_chain (self, "fileName", "nfo:fileName", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "dc:title", "nie:title", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "dlnaProfile", "nmm:dlnaProfile", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "mimeType", "nie:mimeType", NULL);
	rygel_tracker_key_chain_map_add_function (self, "res@size", "tracker:coalesce(nfo:fileSize(%1$s)," "nie:byteSize(%1$s),\"\")");
	rygel_tracker_key_chain_map_add_function (self, "date", "tracker:coalesce(nie:contentCreated(%1$s)," "nfo:fileLastModified(%1$s))");
	rygel_tracker_key_chain_map_add_key_chain (self, "res@duration", "nfo:duration", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:artist", "nmm:performer", "nmm:artistName", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "dc:creator", "nmm:performer", "nmm:artistName", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:album", "nmm:musicAlbum", "nmm:albumTitle", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:originalTrackNumber", "nmm:trackNumber", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:genre", "nfo:genre", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "sampleRate", "nfo:sampleRate", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:nrAudioChannels", "nfo:channels", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:bitsPerSample", "nfo:bitsPerSample", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "upnp:bitrate", "nfo:averageBitrate", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "width", "nfo:width", NULL);
	rygel_tracker_key_chain_map_add_key_chain (self, "height", "nfo:height", NULL);
	return self;
}


static RygelTrackerKeyChainMap* rygel_tracker_key_chain_map_new (void) {
	return rygel_tracker_key_chain_map_construct (RYGEL_TRACKER_TYPE_KEY_CHAIN_MAP);
}


gchar* rygel_tracker_key_chain_map_map_property (RygelTrackerKeyChainMap* self, const gchar* property) {
	gchar* result = NULL;
	gchar* _tmp0_;
	gchar* str;
	GeeHashMap* _tmp1_;
	const gchar* _tmp2_;
	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->key_chain_map;
	_tmp2_ = property;
	_tmp3_ = gee_abstract_map_has_key ((GeeAbstractMap*) _tmp1_, _tmp2_);
	if (_tmp3_) {
		{
			GeeHashMap* _tmp4_;
			const gchar* _tmp5_;
			gpointer _tmp6_ = NULL;
			GeeArrayList* _key_list;
			GeeArrayList* _tmp7_;
			gint _tmp8_;
			gint _tmp9_;
			gint _key_size;
			gint _key_index;
			_tmp4_ = self->priv->key_chain_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_;
				gint _tmp11_;
				gint _tmp12_;
				GeeArrayList* _tmp13_;
				gint _tmp14_;
				gpointer _tmp15_ = NULL;
				gchar* key;
				const gchar* _tmp16_;
				gchar* _tmp17_;
				gchar* _tmp18_;
				const gchar* _tmp19_;
				gchar* _tmp20_;
				gchar* _tmp21_;
				gchar* _tmp22_;
				_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_;
		const gchar* _tmp24_;
		gboolean _tmp25_ = FALSE;
		_tmp23_ = self->priv->functions;
		_tmp24_ = property;
		_tmp25_ = gee_abstract_map_has_key ((GeeAbstractMap*) _tmp23_, _tmp24_);
		if (_tmp25_) {
			GeeHashMap* _tmp26_;
			const gchar* _tmp27_;
			gpointer _tmp28_ = NULL;
			gchar* _tmp29_;
			const gchar* _tmp30_;
			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_key_chain_map_add_key_chain (RygelTrackerKeyChainMap* self, const gchar* property, ...) {
	GeeArrayList* _tmp0_;
	GeeArrayList* key_chain;
	va_list list = {0};
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_;
	gchar* key;
	GeeHashMap* _tmp8_;
	const gchar* _tmp9_;
	GeeArrayList* _tmp10_;
	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);
	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_ = NULL;
		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->key_chain_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_key_chain_map_add_function (RygelTrackerKeyChainMap* self, const gchar* property, const gchar* function) {
	GeeHashMap* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	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_key_chain_map_class_init (RygelTrackerKeyChainMapClass * klass) {
	rygel_tracker_key_chain_map_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelTrackerKeyChainMapPrivate));
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_key_chain_map_finalize;
}


static void rygel_tracker_key_chain_map_instance_init (RygelTrackerKeyChainMap * self) {
	self->priv = RYGEL_TRACKER_KEY_CHAIN_MAP_GET_PRIVATE (self);
}


static void rygel_tracker_key_chain_map_finalize (GObject* obj) {
	RygelTrackerKeyChainMap * self;
	self = RYGEL_TRACKER_KEY_CHAIN_MAP (obj);
	_g_object_unref0 (self->priv->key_chain_map);
	_g_object_unref0 (self->priv->functions);
	G_OBJECT_CLASS (rygel_tracker_key_chain_map_parent_class)->finalize (obj);
}


/**
 * A map of upnp properties to tracker property key chains
 */
GType rygel_tracker_key_chain_map_get_type (void) {
	static volatile gsize rygel_tracker_key_chain_map_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_tracker_key_chain_map_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerKeyChainMapClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_key_chain_map_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerKeyChainMap), 0, (GInstanceInitFunc) rygel_tracker_key_chain_map_instance_init, NULL };
		GType rygel_tracker_key_chain_map_type_id;
		rygel_tracker_key_chain_map_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelTrackerKeyChainMap", &g_define_type_info, 0);
		g_once_init_leave (&rygel_tracker_key_chain_map_type_id__volatile, rygel_tracker_key_chain_map_type_id);
	}
	return rygel_tracker_key_chain_map_type_id__volatile;
}



