/* rygel-tracker-metadata-values.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-tracker-metadata-values.vala, do not modify */

/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 * Copyright (C) 2008-2012 Nokia Corporation.
 *
 * Author: Zeeshan Ali <zeenix@gmail.com>
 *         Jens Georg <jensg@openismus.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 "rygel-tracker3.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "rygel-server.h"
#include <gee.h>
#include <glib-object.h>
#include <gio/gio.h>

enum  {
	RYGEL_TRACKER_METADATA_VALUES_0_PROPERTY,
	RYGEL_TRACKER_METADATA_VALUES_NUM_PROPERTIES
};
static GParamSpec* rygel_tracker_metadata_values_properties[RYGEL_TRACKER_METADATA_VALUES_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _rygel_tracker_query_triplet_unref0(var) ((var == NULL) ? NULL : (var = (rygel_tracker_query_triplet_unref (var), NULL)))

struct _RygelTrackerMetadataValuesPrivate {
	gchar* property;
};

static gint RygelTrackerMetadataValues_private_offset;
static gpointer rygel_tracker_metadata_values_parent_class = NULL;

VALA_EXTERN void rygel_tracker_metadata_container_fetch_metadata_values (RygelTrackerMetadataContainer* self,
                                                             GAsyncReadyCallback _callback_,
                                                             gpointer _user_data_);
VALA_EXTERN void rygel_tracker_metadata_container_fetch_metadata_values_finish (RygelTrackerMetadataContainer* self,
                                                                    GAsyncResult* _res_);
static RygelTrackerSelectionQuery* rygel_tracker_metadata_values_real_create_query (RygelTrackerMetadataContainer* base);
static RygelTrackerSearchContainer* rygel_tracker_metadata_values_real_create_container (RygelTrackerMetadataContainer* base,
                                                                                  const gchar* id,
                                                                                  const gchar* title,
                                                                                  const gchar* value);
static void rygel_tracker_metadata_values_finalize (GObject * obj);
static GType rygel_tracker_metadata_values_get_type_once (void);

static inline gpointer
rygel_tracker_metadata_values_get_instance_private (RygelTrackerMetadataValues* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerMetadataValues_private_offset);
}

RygelTrackerMetadataValues*
rygel_tracker_metadata_values_construct (GType object_type,
                                         const gchar* id,
                                         RygelMediaContainer* parent,
                                         const gchar* title,
                                         RygelTrackerItemFactory* item_factory,
                                         const gchar* property,
                                         const gchar* child_class)
{
	RygelTrackerMetadataValues * self = NULL;
	gchar* _tmp0_;
	RygelTrackerQueryTriplets* _tmp1_;
	RygelTrackerQueryTriplets* _tmp2_;
	RygelTrackerItemFactory* _tmp3_;
	const gchar* _tmp4_;
	RygelTrackerQueryTriplet* _tmp5_;
	RygelTrackerQueryTriplet* _tmp6_;
	RygelTrackerQueryTriplets* _tmp7_;
	RygelTrackerQueryTriplet* _tmp8_;
	RygelTrackerQueryTriplet* _tmp9_;
	g_return_val_if_fail (id != NULL, NULL);
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (title != NULL, NULL);
	g_return_val_if_fail (item_factory != NULL, NULL);
	g_return_val_if_fail (property != NULL, NULL);
	self = (RygelTrackerMetadataValues*) rygel_tracker_metadata_container_construct (object_type, id, parent, title, item_factory, child_class);
	_tmp0_ = g_strdup (property);
	_g_free0 (self->priv->property);
	self->priv->property = _tmp0_;
	_tmp1_ = rygel_tracker_query_triplets_new ();
	_g_object_unref0 (((RygelTrackerMetadataContainer*) self)->triplets);
	((RygelTrackerMetadataContainer*) self)->triplets = _tmp1_;
	_tmp2_ = ((RygelTrackerMetadataContainer*) self)->triplets;
	_tmp3_ = ((RygelTrackerMetadataContainer*) self)->item_factory;
	_tmp4_ = _tmp3_->category;
	_tmp5_ = rygel_tracker_query_triplet_new (RYGEL_TRACKER_SELECTION_QUERY_ITEM_VARIABLE, "a", _tmp4_);
	_tmp6_ = _tmp5_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp2_, _tmp6_);
	_rygel_tracker_query_triplet_unref0 (_tmp6_);
	_tmp7_ = ((RygelTrackerMetadataContainer*) self)->triplets;
	_tmp8_ = rygel_tracker_query_triplet_new (RYGEL_TRACKER_SELECTION_QUERY_ITEM_VARIABLE, "nie:isStoredAs", RYGEL_TRACKER_SELECTION_QUERY_STORAGE_VARIABLE);
	_tmp9_ = _tmp8_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp7_, _tmp9_);
	_rygel_tracker_query_triplet_unref0 (_tmp9_);
	rygel_tracker_metadata_container_fetch_metadata_values ((RygelTrackerMetadataContainer*) self, NULL, NULL);
	return self;
}

static RygelTrackerSelectionQuery*
rygel_tracker_metadata_values_real_create_query (RygelTrackerMetadataContainer* base)
{
	RygelTrackerMetadataValues * self;
	RygelTrackerUPnPPropertyMap* property_map = NULL;
	RygelTrackerUPnPPropertyMap* _tmp0_;
	GeeArrayList* selected = NULL;
	GeeArrayList* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	RygelTrackerSelectionQuery* q = NULL;
	RygelTrackerQueryTriplets* _tmp9_;
	RygelTrackerItemFactory* _tmp10_;
	const gchar* _tmp11_;
	RygelTrackerSelectionQuery* _tmp12_;
	RygelTrackerSelectionQuery* result;
	self = (RygelTrackerMetadataValues*) base;
	_tmp0_ = rygel_tracker_upn_pproperty_map_get_property_map ();
	property_map = _tmp0_;
	_tmp1_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	selected = _tmp1_;
	_tmp2_ = self->priv->property;
	_tmp3_ = rygel_tracker_upn_pproperty_map_get (property_map, _tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_strconcat ("DISTINCT ", _tmp4_, NULL);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strconcat (_tmp6_, " AS ?x", NULL);
	_tmp8_ = _tmp7_;
	gee_abstract_collection_add ((GeeAbstractCollection*) selected, _tmp8_);
	_g_free0 (_tmp8_);
	_g_free0 (_tmp6_);
	_g_free0 (_tmp4_);
	_tmp9_ = ((RygelTrackerMetadataContainer*) self)->triplets;
	_tmp10_ = ((RygelTrackerMetadataContainer*) self)->item_factory;
	_tmp11_ = _tmp10_->graph;
	_tmp12_ = rygel_tracker_selection_query_new (selected, _tmp9_, NULL, _tmp11_, "?x", 0, -1);
	q = _tmp12_;
	result = q;
	_g_object_unref0 (selected);
	_g_object_unref0 (property_map);
	return result;
}

static RygelTrackerSearchContainer*
rygel_tracker_metadata_values_real_create_container (RygelTrackerMetadataContainer* base,
                                                     const gchar* id,
                                                     const gchar* title,
                                                     const gchar* value)
{
	RygelTrackerMetadataValues * self;
	RygelTrackerQueryTriplets* child_triplets = NULL;
	RygelTrackerQueryTriplets* _tmp0_;
	RygelTrackerQueryTriplets* _tmp1_;
	GeeArrayList* filters = NULL;
	GeeArrayList* _tmp2_;
	RygelTrackerUPnPPropertyMap* property_map = NULL;
	RygelTrackerUPnPPropertyMap* _tmp3_;
	gchar* property = NULL;
	RygelTrackerUPnPPropertyMap* _tmp4_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* filter = NULL;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	GeeArrayList* _tmp9_;
	const gchar* _tmp10_;
	RygelTrackerSearchContainer* child = NULL;
	RygelTrackerItemFactory* _tmp11_;
	RygelTrackerQueryTriplets* _tmp12_;
	GeeArrayList* _tmp13_;
	RygelTrackerSearchContainer* _tmp14_;
	const gchar* _tmp15_;
	RygelTrackerSearchContainer* result;
	self = (RygelTrackerMetadataValues*) base;
	g_return_val_if_fail (id != NULL, NULL);
	g_return_val_if_fail (title != NULL, NULL);
	g_return_val_if_fail (value != NULL, NULL);
	_tmp0_ = ((RygelTrackerMetadataContainer*) self)->triplets;
	_tmp1_ = rygel_tracker_query_triplets_new_clone (_tmp0_);
	child_triplets = _tmp1_;
	_tmp2_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	filters = _tmp2_;
	_tmp3_ = rygel_tracker_upn_pproperty_map_get_property_map ();
	property_map = _tmp3_;
	_tmp4_ = property_map;
	_tmp5_ = self->priv->property;
	_tmp6_ = rygel_tracker_upn_pproperty_map_get (_tmp4_, _tmp5_);
	property = _tmp6_;
	_tmp7_ = property;
	_tmp8_ = rygel_tracker_metadata_container_create_filter ((RygelTrackerMetadataContainer*) self, _tmp7_, value);
	filter = _tmp8_;
	_tmp9_ = filters;
	_tmp10_ = filter;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp9_, _tmp10_);
	_tmp11_ = ((RygelTrackerMetadataContainer*) self)->item_factory;
	_tmp12_ = child_triplets;
	_tmp13_ = filters;
	_tmp14_ = rygel_tracker_search_container_new (id, (RygelMediaContainer*) self, title, _tmp11_, _tmp12_, _tmp13_);
	child = _tmp14_;
	_tmp15_ = self->priv->property;
	if (g_strcmp0 (_tmp15_, "upnp:album") == 0) {
		RygelTrackerSearchContainer* _tmp16_;
		_tmp16_ = child;
		rygel_media_container_set_sort_criteria ((RygelMediaContainer*) _tmp16_, RYGEL_MEDIA_CONTAINER_ALBUM_SORT_CRITERIA);
	}
	result = child;
	_g_free0 (filter);
	_g_free0 (property);
	_g_object_unref0 (property_map);
	_g_object_unref0 (filters);
	_g_object_unref0 (child_triplets);
	return result;
}

static void
rygel_tracker_metadata_values_class_init (RygelTrackerMetadataValuesClass * klass,
                                          gpointer klass_data)
{
	rygel_tracker_metadata_values_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelTrackerMetadataValues_private_offset);
	((RygelTrackerMetadataContainerClass *) klass)->create_query = (RygelTrackerSelectionQuery* (*) (RygelTrackerMetadataContainer*)) rygel_tracker_metadata_values_real_create_query;
	((RygelTrackerMetadataContainerClass *) klass)->create_container = (RygelTrackerSearchContainer* (*) (RygelTrackerMetadataContainer*, const gchar*, const gchar*, const gchar*)) rygel_tracker_metadata_values_real_create_container;
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_metadata_values_finalize;
}

static void
rygel_tracker_metadata_values_instance_init (RygelTrackerMetadataValues * self,
                                             gpointer klass)
{
	self->priv = rygel_tracker_metadata_values_get_instance_private (self);
}

static void
rygel_tracker_metadata_values_finalize (GObject * obj)
{
	RygelTrackerMetadataValues * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_METADATA_VALUES, RygelTrackerMetadataValues);
	_g_free0 (self->priv->property);
	G_OBJECT_CLASS (rygel_tracker_metadata_values_parent_class)->finalize (obj);
}

/**
 * Container listing possible values of a particuler Tracker metadata key.
 * The key needs to be single-valued.
 */
static GType
rygel_tracker_metadata_values_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerMetadataValuesClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_metadata_values_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerMetadataValues), 0, (GInstanceInitFunc) rygel_tracker_metadata_values_instance_init, NULL };
	GType rygel_tracker_metadata_values_type_id;
	rygel_tracker_metadata_values_type_id = g_type_register_static (RYGEL_TRACKER_TYPE_METADATA_CONTAINER, "RygelTrackerMetadataValues", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
	RygelTrackerMetadataValues_private_offset = g_type_add_instance_private (rygel_tracker_metadata_values_type_id, sizeof (RygelTrackerMetadataValuesPrivate));
	return rygel_tracker_metadata_values_type_id;
}

GType
rygel_tracker_metadata_values_get_type (void)
{
	static volatile gsize rygel_tracker_metadata_values_type_id__once = 0;
	if (g_once_init_enter (&rygel_tracker_metadata_values_type_id__once)) {
		GType rygel_tracker_metadata_values_type_id;
		rygel_tracker_metadata_values_type_id = rygel_tracker_metadata_values_get_type_once ();
		g_once_init_leave (&rygel_tracker_metadata_values_type_id__once, rygel_tracker_metadata_values_type_id);
	}
	return rygel_tracker_metadata_values_type_id__once;
}

