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

/*
 * Copyright (C) 2010 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.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 <glib/gi18n-lib.h>

enum  {
	RYGEL_TRACKER_TITLES_0_PROPERTY,
	RYGEL_TRACKER_TITLES_NUM_PROPERTIES
};
static GParamSpec* rygel_tracker_titles_properties[RYGEL_TRACKER_TITLES_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))

static gpointer rygel_tracker_titles_parent_class = NULL;

static gchar* rygel_tracker_titles_real_create_title_for_value (RygelTrackerMetadataContainer* base,
                                                         const gchar* value);
static gchar* rygel_tracker_titles_real_create_filter (RygelTrackerMetadataContainer* base,
                                                const gchar* variable,
                                                const gchar* value);
static GType rygel_tracker_titles_get_type_once (void);

static const gchar* RYGEL_TRACKER_TITLES_KEY_CHAIN[2] = {"dc:title", NULL};

RygelTrackerTitles*
rygel_tracker_titles_construct (GType object_type,
                                RygelMediaContainer* parent,
                                RygelTrackerItemFactory* item_factory)
{
	RygelTrackerTitles * self = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (item_factory != NULL, NULL);
	_tmp0_ = rygel_media_object_get_id ((RygelMediaObject*) parent);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat (_tmp1_, "Titles", NULL);
	_tmp3_ = _tmp2_;
	self = (RygelTrackerTitles*) rygel_tracker_metadata_values_construct (object_type, _tmp3_, parent, _ ("Titles"), item_factory, "dc:title", NULL);
	_g_free0 (_tmp3_);
	return self;
}

RygelTrackerTitles*
rygel_tracker_titles_new (RygelMediaContainer* parent,
                          RygelTrackerItemFactory* item_factory)
{
	return rygel_tracker_titles_construct (RYGEL_TRACKER_TYPE_TITLES, parent, item_factory);
}

static gchar*
g_unichar_to_string (gunichar self)
{
	gchar* str = NULL;
	gchar* _tmp0_;
	gchar* result;
	_tmp0_ = g_new0 (gchar, 7);
	str = (gchar*) _tmp0_;
	g_unichar_to_utf8 (self, str);
	result = str;
	return result;
}

static gchar*
rygel_tracker_titles_real_create_title_for_value (RygelTrackerMetadataContainer* base,
                                                  const gchar* value)
{
	RygelTrackerTitles * self;
	gunichar c = 0U;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	self = (RygelTrackerTitles*) base;
	g_return_val_if_fail (value != NULL, NULL);
	c = g_utf8_get_char_validated (value, (gssize) -1);
	if (G_UNLIKELY (c < ((gunichar) 0))) {
		result = NULL;
		return result;
	}
	_tmp0_ = g_unichar_to_string (c);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_utf8_strup (_tmp1_, (gssize) -1);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	result = _tmp3_;
	return result;
}

static gchar*
rygel_tracker_titles_real_create_filter (RygelTrackerMetadataContainer* base,
                                         const gchar* variable,
                                         const gchar* value)
{
	RygelTrackerTitles * self;
	gchar* title = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* result;
	self = (RygelTrackerTitles*) base;
	g_return_val_if_fail (variable != NULL, NULL);
	g_return_val_if_fail (value != NULL, NULL);
	_tmp0_ = rygel_tracker_metadata_container_create_title_for_value ((RygelTrackerMetadataContainer*) self, value);
	_tmp1_ = _tmp0_;
	_tmp2_ = rygel_tracker_query_escape_regex (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	title = _tmp3_;
	_tmp4_ = g_strconcat ("regex(", variable, NULL);
	_tmp5_ = _tmp4_;
	_tmp6_ = g_strconcat (_tmp5_, ", \"^", NULL);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_strconcat (_tmp7_, title, NULL);
	_tmp9_ = _tmp8_;
	_tmp10_ = g_strconcat (_tmp9_, "\", \"i\")", NULL);
	_tmp11_ = _tmp10_;
	_g_free0 (_tmp9_);
	_g_free0 (_tmp7_);
	_g_free0 (_tmp5_);
	result = _tmp11_;
	_g_free0 (title);
	return result;
}

static void
rygel_tracker_titles_class_init (RygelTrackerTitlesClass * klass,
                                 gpointer klass_data)
{
	rygel_tracker_titles_parent_class = g_type_class_peek_parent (klass);
	((RygelTrackerMetadataContainerClass *) klass)->create_title_for_value = (gchar* (*) (RygelTrackerMetadataContainer*, const gchar*)) rygel_tracker_titles_real_create_title_for_value;
	((RygelTrackerMetadataContainerClass *) klass)->create_filter = (gchar* (*) (RygelTrackerMetadataContainer*, const gchar*, const gchar*)) rygel_tracker_titles_real_create_filter;
}

static void
rygel_tracker_titles_instance_init (RygelTrackerTitles * self,
                                    gpointer klass)
{
}

/**
 * Container providing a title-based hierarchy.
 *
 * Under each category container, add a container providing a title-based
 * hierarchy: One container for each unique starting character of all the titles
 * available for the contegory in question. Something like this:
 *
 * Music
 *   |
 *   |----> Genre
 *           |..
 *   ^
 *   |----> Titles
 *           |
 *           |---> A
 *                 |
 *                 |--> Alpha
 *                 |--> alright
 *           ^
 *           |---> B
 *                 |
 *                 |--> Bravo
 *                 |--> brave
 *                 |..
 *           ^
 *   ^       |..
 *   |..
 */
static GType
rygel_tracker_titles_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerTitlesClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_titles_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerTitles), 0, (GInstanceInitFunc) rygel_tracker_titles_instance_init, NULL };
	GType rygel_tracker_titles_type_id;
	rygel_tracker_titles_type_id = g_type_register_static (RYGEL_TRACKER_TYPE_METADATA_VALUES, "RygelTrackerTitles", &g_define_type_info, 0);
	return rygel_tracker_titles_type_id;
}

GType
rygel_tracker_titles_get_type (void)
{
	static volatile gsize rygel_tracker_titles_type_id__once = 0;
	if (g_once_init_enter (&rygel_tracker_titles_type_id__once)) {
		GType rygel_tracker_titles_type_id;
		rygel_tracker_titles_type_id = rygel_tracker_titles_get_type_once ();
		g_once_init_leave (&rygel_tracker_titles_type_id__once, rygel_tracker_titles_type_id);
	}
	return rygel_tracker_titles_type_id__once;
}

