/* rygel-gst-launch-root-container.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-gst-launch-root-container.vala, do not modify */

/*
 * Copyright (C) 2009 Thijs Vermeir <thijsvermeir@gmail.com>
 *
 * Author: Thijs Vermeir <thijsvermeir@gmail.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-gst-launch.h"
#include "rygel-core.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "rygel-server.h"
#include <gee.h>
#include <glib-object.h>

#define RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP "GstLaunch"
#define RYGEL_GST_LAUNCH_ROOT_CONTAINER_ITEM_NAMES "launch-items"

enum  {
	RYGEL_GST_LAUNCH_ROOT_CONTAINER_0_PROPERTY,
	RYGEL_GST_LAUNCH_ROOT_CONTAINER_NUM_PROPERTIES
};
static GParamSpec* rygel_gst_launch_root_container_properties[RYGEL_GST_LAUNCH_ROOT_CONTAINER_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_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _RygelGstLaunchRootContainerPrivate {
	RygelMetaConfig* config;
	gboolean initialized;
};

static gint RygelGstLaunchRootContainer_private_offset;
static gpointer rygel_gst_launch_root_container_parent_class = NULL;

static void rygel_gst_launch_root_container_add_launch_item (RygelGstLaunchRootContainer* self,
                                                      const gchar* name);
static void rygel_gst_launch_root_container_finalize (GObject * obj);
static GType rygel_gst_launch_root_container_get_type_once (void);

static inline gpointer
rygel_gst_launch_root_container_get_instance_private (RygelGstLaunchRootContainer* self)
{
	return G_STRUCT_MEMBER_P (self, RygelGstLaunchRootContainer_private_offset);
}

RygelGstLaunchRootContainer*
rygel_gst_launch_root_container_construct (GType object_type,
                                           const gchar* title)
{
	RygelGstLaunchRootContainer * self = NULL;
	g_return_val_if_fail (title != NULL, NULL);
	self = (RygelGstLaunchRootContainer*) rygel_simple_container_construct_root (object_type, title);
	return self;
}

RygelGstLaunchRootContainer*
rygel_gst_launch_root_container_new (const gchar* title)
{
	return rygel_gst_launch_root_container_construct (RYGEL_GST_LAUNCH_TYPE_ROOT_CONTAINER, title);
}

void
rygel_gst_launch_root_container_init (RygelGstLaunchRootContainer* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	if (self->priv->initialized) {
		return;
	}
	self->priv->initialized = TRUE;
	{
		RygelMetaConfig* _tmp0_;
		GeeArrayList* item_names = NULL;
		RygelMetaConfig* _tmp1_;
		GeeArrayList* _tmp2_;
		_tmp0_ = rygel_meta_config_get_default ();
		_g_object_unref0 (self->priv->config);
		self->priv->config = _tmp0_;
		_tmp1_ = self->priv->config;
		_tmp2_ = rygel_configuration_get_string_list ((RygelConfiguration*) _tmp1_, RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP, RYGEL_GST_LAUNCH_ROOT_CONTAINER_ITEM_NAMES, &_inner_error0_);
		item_names = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		{
			GeeArrayList* _name_list = NULL;
			GeeArrayList* _tmp3_;
			gint _name_size = 0;
			GeeArrayList* _tmp4_;
			gint _tmp5_;
			gint _tmp6_;
			gint _name_index = 0;
			_tmp3_ = item_names;
			_name_list = _tmp3_;
			_tmp4_ = _name_list;
			_tmp5_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp4_);
			_tmp6_ = _tmp5_;
			_name_size = _tmp6_;
			_name_index = -1;
			while (TRUE) {
				gint _tmp7_;
				gint _tmp8_;
				gchar* name = NULL;
				GeeArrayList* _tmp9_;
				gpointer _tmp10_;
				const gchar* _tmp11_;
				_name_index = _name_index + 1;
				_tmp7_ = _name_index;
				_tmp8_ = _name_size;
				if (!(_tmp7_ < _tmp8_)) {
					break;
				}
				_tmp9_ = _name_list;
				_tmp10_ = gee_abstract_list_get ((GeeAbstractList*) _tmp9_, _name_index);
				name = (gchar*) _tmp10_;
				_tmp11_ = name;
				rygel_gst_launch_root_container_add_launch_item (self, _tmp11_);
				_g_free0 (name);
			}
		}
		_g_object_unref0 (item_names);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		GError* _tmp12_;
		const gchar* _tmp13_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp12_ = err;
		_tmp13_ = _tmp12_->message;
		g_debug ("rygel-gst-launch-root-container.vala:52: GstLaunch init failed: %s", _tmp13_);
		_g_error_free0 (err);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
rygel_gst_launch_root_container_add_launch_item (RygelGstLaunchRootContainer* self,
                                                 const gchar* name)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (name != NULL);
	{
		gchar* title = NULL;
		RygelMetaConfig* _tmp0_;
		gchar* _tmp1_;
		gchar* _tmp2_;
		gchar* _tmp3_;
		gchar* _tmp4_;
		gchar* mime_type = NULL;
		RygelMetaConfig* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		gchar* _tmp9_;
		gchar* launch_line = NULL;
		RygelMetaConfig* _tmp10_;
		gchar* _tmp11_;
		gchar* _tmp12_;
		gchar* _tmp13_;
		gchar* _tmp14_;
		gchar* dlna_profile = NULL;
		RygelMediaFileItem* item = NULL;
		const gchar* _tmp22_;
		RygelMediaFileItem* _tmp31_;
		_tmp0_ = self->priv->config;
		_tmp1_ = g_strdup_printf ("%s-title", name);
		_tmp2_ = _tmp1_;
		_tmp3_ = rygel_configuration_get_string ((RygelConfiguration*) _tmp0_, RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP, _tmp2_, &_inner_error0_);
		_tmp4_ = _tmp3_;
		_g_free0 (_tmp2_);
		title = _tmp4_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp5_ = self->priv->config;
		_tmp6_ = g_strdup_printf ("%s-mime", name);
		_tmp7_ = _tmp6_;
		_tmp8_ = rygel_configuration_get_string ((RygelConfiguration*) _tmp5_, RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP, _tmp7_, &_inner_error0_);
		_tmp9_ = _tmp8_;
		_g_free0 (_tmp7_);
		mime_type = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (title);
			goto __catch0_g_error;
		}
		_tmp10_ = self->priv->config;
		_tmp11_ = g_strdup_printf ("%s-launch", name);
		_tmp12_ = _tmp11_;
		_tmp13_ = rygel_configuration_get_string ((RygelConfiguration*) _tmp10_, RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP, _tmp12_, &_inner_error0_);
		_tmp14_ = _tmp13_;
		_g_free0 (_tmp12_);
		launch_line = _tmp14_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (mime_type);
			_g_free0 (title);
			goto __catch0_g_error;
		}
		dlna_profile = NULL;
		{
			gchar* _tmp15_ = NULL;
			RygelMetaConfig* _tmp16_;
			gchar* _tmp17_;
			gchar* _tmp18_;
			gchar* _tmp19_;
			gchar* _tmp20_;
			gchar* _tmp21_;
			_tmp16_ = self->priv->config;
			_tmp17_ = g_strdup_printf ("%s-dlnaprofile", name);
			_tmp18_ = _tmp17_;
			_tmp19_ = rygel_configuration_get_string ((RygelConfiguration*) _tmp16_, RYGEL_GST_LAUNCH_ROOT_CONTAINER_CONFIG_GROUP, _tmp18_, &_inner_error0_);
			_tmp20_ = _tmp19_;
			_g_free0 (_tmp18_);
			_tmp15_ = _tmp20_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			_tmp21_ = _tmp15_;
			_tmp15_ = NULL;
			_g_free0 (dlna_profile);
			dlna_profile = _tmp21_;
			_g_free0 (_tmp15_);
		}
		goto __finally1;
		__catch1_g_error:
		{
			g_clear_error (&_inner_error0_);
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (item);
			_g_free0 (dlna_profile);
			_g_free0 (launch_line);
			_g_free0 (mime_type);
			_g_free0 (title);
			goto __catch0_g_error;
		}
		_tmp22_ = mime_type;
		if (g_str_has_prefix (_tmp22_, "audio")) {
			const gchar* _tmp23_;
			const gchar* _tmp24_;
			const gchar* _tmp25_;
			RygelGstLaunchAudioItem* _tmp26_;
			_tmp23_ = title;
			_tmp24_ = mime_type;
			_tmp25_ = launch_line;
			_tmp26_ = rygel_gst_launch_audio_item_new (name, (RygelMediaContainer*) self, _tmp23_, _tmp24_, _tmp25_);
			_g_object_unref0 (item);
			item = (RygelMediaFileItem*) _tmp26_;
		} else {
			const gchar* _tmp27_;
			const gchar* _tmp28_;
			const gchar* _tmp29_;
			RygelGstLaunchVideoItem* _tmp30_;
			_tmp27_ = title;
			_tmp28_ = mime_type;
			_tmp29_ = launch_line;
			_tmp30_ = rygel_gst_launch_video_item_new (name, (RygelMediaContainer*) self, _tmp27_, _tmp28_, _tmp29_);
			_g_object_unref0 (item);
			item = (RygelMediaFileItem*) _tmp30_;
		}
		_tmp31_ = item;
		if (_tmp31_ != NULL) {
			const gchar* _tmp32_;
			RygelMediaFileItem* _tmp35_;
			RygelMediaFileItem* _tmp36_;
			_tmp32_ = dlna_profile;
			if (_tmp32_ != NULL) {
				RygelMediaFileItem* _tmp33_;
				const gchar* _tmp34_;
				_tmp33_ = item;
				_tmp34_ = dlna_profile;
				rygel_media_file_item_set_dlna_profile (_tmp33_, _tmp34_);
			}
			_tmp35_ = item;
			rygel_media_file_item_add_engine_resources (_tmp35_, NULL, NULL);
			_tmp36_ = item;
			rygel_simple_container_add_child_item ((RygelSimpleContainer*) self, (RygelMediaItem*) _tmp36_);
			rygel_media_container_updated ((RygelMediaContainer*) self, NULL, RYGEL_OBJECT_EVENT_TYPE_MODIFIED, FALSE);
		}
		_g_object_unref0 (item);
		_g_free0 (dlna_profile);
		_g_free0 (launch_line);
		_g_free0 (mime_type);
		_g_free0 (title);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		GError* _tmp37_;
		const gchar* _tmp38_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp37_ = err;
		_tmp38_ = _tmp37_->message;
		g_debug ("rygel-gst-launch-root-container.vala:96: GstLaunch failed item '%s': %" \
"s", name, _tmp38_);
		_g_error_free0 (err);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
rygel_gst_launch_root_container_class_init (RygelGstLaunchRootContainerClass * klass,
                                            gpointer klass_data)
{
	rygel_gst_launch_root_container_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelGstLaunchRootContainer_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_gst_launch_root_container_finalize;
}

static void
rygel_gst_launch_root_container_instance_init (RygelGstLaunchRootContainer * self,
                                               gpointer klass)
{
	self->priv = rygel_gst_launch_root_container_get_instance_private (self);
	self->priv->initialized = FALSE;
}

static void
rygel_gst_launch_root_container_finalize (GObject * obj)
{
	RygelGstLaunchRootContainer * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_GST_LAUNCH_TYPE_ROOT_CONTAINER, RygelGstLaunchRootContainer);
	_g_object_unref0 (self->priv->config);
	G_OBJECT_CLASS (rygel_gst_launch_root_container_parent_class)->finalize (obj);
}

/**
 * Represents the root container for GstLaunch content hierarchy.
 */
static GType
rygel_gst_launch_root_container_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelGstLaunchRootContainerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_gst_launch_root_container_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelGstLaunchRootContainer), 0, (GInstanceInitFunc) rygel_gst_launch_root_container_instance_init, NULL };
	GType rygel_gst_launch_root_container_type_id;
	rygel_gst_launch_root_container_type_id = g_type_register_static (RYGEL_TYPE_SIMPLE_CONTAINER, "RygelGstLaunchRootContainer", &g_define_type_info, 0);
	RygelGstLaunchRootContainer_private_offset = g_type_add_instance_private (rygel_gst_launch_root_container_type_id, sizeof (RygelGstLaunchRootContainerPrivate));
	return rygel_gst_launch_root_container_type_id;
}

GType
rygel_gst_launch_root_container_get_type (void)
{
	static volatile gsize rygel_gst_launch_root_container_type_id__once = 0;
	if (g_once_init_enter (&rygel_gst_launch_root_container_type_id__once)) {
		GType rygel_gst_launch_root_container_type_id;
		rygel_gst_launch_root_container_type_id = rygel_gst_launch_root_container_get_type_once ();
		g_once_init_leave (&rygel_gst_launch_root_container_type_id__once, rygel_gst_launch_root_container_type_id);
	}
	return rygel_gst_launch_root_container_type_id__once;
}

