/* rygel-m3u-playlist.c generated by valac 0.44.9, the Vala compiler
 * generated from rygel-m3u-playlist.vala, do not modify */

/*
 * Copyright (C) 2013 Jens Georg.
 *
 * Authors: Jens Georg <mail@jensge.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 <libgupnp-av/gupnp-av.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>

#define RYGEL_TYPE_M3_UPLAY_LIST (rygel_m3_uplay_list_get_type ())
#define RYGEL_M3_UPLAY_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_M3_UPLAY_LIST, RygelM3UPlayList))
#define RYGEL_M3_UPLAY_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_M3_UPLAY_LIST, RygelM3UPlayListClass))
#define RYGEL_IS_M3_UPLAY_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_M3_UPLAY_LIST))
#define RYGEL_IS_M3_UPLAY_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_M3_UPLAY_LIST))
#define RYGEL_M3_UPLAY_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_M3_UPLAY_LIST, RygelM3UPlayListClass))

typedef struct _RygelM3UPlayList RygelM3UPlayList;
typedef struct _RygelM3UPlayListClass RygelM3UPlayListClass;
typedef struct _RygelM3UPlayListPrivate RygelM3UPlayListPrivate;
enum  {
	RYGEL_M3_UPLAY_LIST_0_PROPERTY,
	RYGEL_M3_UPLAY_LIST_NUM_PROPERTIES
};
static GParamSpec* rygel_m3_uplay_list_properties[RYGEL_M3_UPLAY_LIST_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _RygelM3UPlayList {
	GObject parent_instance;
	RygelM3UPlayListPrivate * priv;
};

struct _RygelM3UPlayListClass {
	GObjectClass parent_class;
};

struct _RygelM3UPlayListPrivate {
	GeeLinkedList* items;
	GUPnPDIDLLiteWriter* writer;
};

static gint RygelM3UPlayList_private_offset;
static gpointer rygel_m3_uplay_list_parent_class = NULL;

GType rygel_m3_uplay_list_get_type (void) G_GNUC_CONST;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (RygelM3UPlayList, g_object_unref)
RygelM3UPlayList* rygel_m3_uplay_list_new (void);
RygelM3UPlayList* rygel_m3_uplay_list_construct (GType object_type);
static void rygel_m3_uplay_list_real_constructed (GObject* base);
GUPnPDIDLLiteItem* rygel_m3_uplay_list_add_item (RygelM3UPlayList* self);
gchar* rygel_m3_uplay_list_get_string (RygelM3UPlayList* self);
static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
static void rygel_m3_uplay_list_finalize (GObject * obj);

static inline gpointer
rygel_m3_uplay_list_get_instance_private (RygelM3UPlayList* self)
{
	return G_STRUCT_MEMBER_P (self, RygelM3UPlayList_private_offset);
}

RygelM3UPlayList*
rygel_m3_uplay_list_construct (GType object_type)
{
	RygelM3UPlayList * self = NULL;
	self = (RygelM3UPlayList*) g_object_new (object_type, NULL);
	return self;
}

RygelM3UPlayList*
rygel_m3_uplay_list_new (void)
{
	return rygel_m3_uplay_list_construct (RYGEL_TYPE_M3_UPLAY_LIST);
}

static void
rygel_m3_uplay_list_real_constructed (GObject* base)
{
	RygelM3UPlayList * self;
	GeeLinkedList* _tmp0_;
	GUPnPDIDLLiteWriter* _tmp1_;
	self = (RygelM3UPlayList*) base;
	_tmp0_ = gee_linked_list_new (gupnp_didl_lite_item_get_type (), (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->items);
	self->priv->items = _tmp0_;
	_tmp1_ = gupnp_didl_lite_writer_new (NULL);
	_g_object_unref0 (self->priv->writer);
	self->priv->writer = _tmp1_;
}

GUPnPDIDLLiteItem*
rygel_m3_uplay_list_add_item (RygelM3UPlayList* self)
{
	GeeLinkedList* _tmp0_;
	GUPnPDIDLLiteWriter* _tmp1_;
	GUPnPDIDLLiteItem* _tmp2_;
	GUPnPDIDLLiteItem* _tmp3_;
	GeeLinkedList* _tmp4_;
	gpointer _tmp5_;
	GUPnPDIDLLiteItem* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->items;
	_tmp1_ = self->priv->writer;
	_tmp2_ = gupnp_didl_lite_writer_add_item (_tmp1_);
	_tmp3_ = _tmp2_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp0_, _tmp3_);
	_g_object_unref0 (_tmp3_);
	_tmp4_ = self->priv->items;
	_tmp5_ = gee_linked_list_last (_tmp4_);
	result = (GUPnPDIDLLiteItem*) _tmp5_;
	return result;
}

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

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static inline void
_g_list_free__g_object_unref0_ (GList* self)
{
	g_list_free_full (self, (GDestroyNotify) _g_object_unref0_);
}

gchar*
rygel_m3_uplay_list_get_string (RygelM3UPlayList* self)
{
	GString* builder = NULL;
	GString* _tmp0_;
	GString* _tmp36_;
	const gchar* _tmp37_;
	gchar* _tmp38_;
	gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_string_new ("#EXTM3U\r\n");
	builder = _tmp0_;
	{
		GeeLinkedList* _item_list = NULL;
		GeeLinkedList* _tmp1_;
		GeeLinkedList* _tmp2_;
		gint _item_size = 0;
		GeeLinkedList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _item_index = 0;
		_tmp1_ = self->priv->items;
		_tmp2_ = _g_object_ref0 (_tmp1_);
		_item_list = _tmp2_;
		_tmp3_ = _item_list;
		_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_item_size = _tmp5_;
		_item_index = -1;
		while (TRUE) {
			GUPnPDIDLLiteItem* item = NULL;
			GeeLinkedList* _tmp6_;
			gpointer _tmp7_;
			GList* resources = NULL;
			GUPnPDIDLLiteItem* _tmp8_;
			GList* _tmp9_;
			GList* _tmp10_;
			_item_index = _item_index + 1;
			if (!(_item_index < _item_size)) {
				break;
			}
			_tmp6_ = _item_list;
			_tmp7_ = gee_abstract_list_get ((GeeAbstractList*) _tmp6_, _item_index);
			item = (GUPnPDIDLLiteItem*) _tmp7_;
			_tmp8_ = item;
			_tmp9_ = gupnp_didl_lite_object_get_resources ((GUPnPDIDLLiteObject*) _tmp8_);
			resources = _tmp9_;
			_tmp10_ = resources;
			if (_tmp10_ != NULL) {
				GList* authors = NULL;
				GUPnPDIDLLiteItem* _tmp11_;
				GList* _tmp12_;
				GString* _tmp13_;
				GList* _tmp14_;
				gconstpointer _tmp15_;
				glong _tmp16_;
				glong _tmp17_;
				GList* _tmp18_;
				const gchar* _tmp24_ = NULL;
				GUPnPDIDLLiteItem* _tmp25_;
				const gchar* _tmp26_;
				const gchar* _tmp27_;
				GString* _tmp28_;
				GString* _tmp29_;
				GString* _tmp30_;
				GList* _tmp31_;
				gconstpointer _tmp32_;
				const gchar* _tmp33_;
				const gchar* _tmp34_;
				GString* _tmp35_;
				_tmp11_ = item;
				_tmp12_ = gupnp_didl_lite_object_get_artists ((GUPnPDIDLLiteObject*) _tmp11_);
				authors = _tmp12_;
				_tmp13_ = builder;
				_tmp14_ = resources;
				_tmp15_ = _tmp14_->data;
				_tmp16_ = gupnp_didl_lite_resource_get_duration ((GUPnPDIDLLiteResource*) _tmp15_);
				_tmp17_ = _tmp16_;
				g_string_append_printf (_tmp13_, "#EXTINF:%ld,", _tmp17_);
				_tmp18_ = authors;
				if (_tmp18_ != NULL) {
					const gchar* _tmp19_ = NULL;
					GList* _tmp20_;
					gconstpointer _tmp21_;
					const gchar* _tmp22_;
					GString* _tmp23_;
					_tmp20_ = authors;
					_tmp21_ = _tmp20_->data;
					_tmp22_ = gupnp_didl_lite_contributor_get_name ((GUPnPDIDLLiteContributor*) _tmp21_);
					_tmp19_ = _tmp22_;
					if (_tmp19_ == NULL) {
						_tmp19_ = _ ("Unknown");
					}
					_tmp23_ = builder;
					g_string_append_printf (_tmp23_, "%s - ", _tmp19_);
				}
				_tmp25_ = item;
				_tmp26_ = gupnp_didl_lite_object_get_title ((GUPnPDIDLLiteObject*) _tmp25_);
				_tmp27_ = _tmp26_;
				_tmp24_ = _tmp27_;
				if (_tmp24_ == NULL) {
					_tmp24_ = _ ("Unknown");
				}
				_tmp28_ = builder;
				g_string_append (_tmp28_, _tmp24_);
				_tmp29_ = builder;
				g_string_append (_tmp29_, "\r\n");
				_tmp30_ = builder;
				_tmp31_ = resources;
				_tmp32_ = _tmp31_->data;
				_tmp33_ = gupnp_didl_lite_resource_get_uri ((GUPnPDIDLLiteResource*) _tmp32_);
				_tmp34_ = _tmp33_;
				g_string_append (_tmp30_, _tmp34_);
				_tmp35_ = builder;
				g_string_append (_tmp35_, "\r\n");
				(authors == NULL) ? NULL : (authors = (_g_list_free__g_object_unref0_ (authors), NULL));
			}
			(resources == NULL) ? NULL : (resources = (_g_list_free__g_object_unref0_ (resources), NULL));
			_g_object_unref0 (item);
		}
		_g_object_unref0 (_item_list);
	}
	_tmp36_ = builder;
	_tmp37_ = _tmp36_->str;
	_tmp38_ = g_strdup (_tmp37_);
	result = _tmp38_;
	_g_string_free0 (builder);
	return result;
}

static void
rygel_m3_uplay_list_class_init (RygelM3UPlayListClass * klass,
                                gpointer klass_data)
{
	rygel_m3_uplay_list_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelM3UPlayList_private_offset);
	((GObjectClass *) klass)->constructed = (void (*) (GObject*)) rygel_m3_uplay_list_real_constructed;
	G_OBJECT_CLASS (klass)->finalize = rygel_m3_uplay_list_finalize;
}

static void
rygel_m3_uplay_list_instance_init (RygelM3UPlayList * self,
                                   gpointer klass)
{
	self->priv = rygel_m3_uplay_list_get_instance_private (self);
}

static void
rygel_m3_uplay_list_finalize (GObject * obj)
{
	RygelM3UPlayList * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_M3_UPLAY_LIST, RygelM3UPlayList);
	_g_object_unref0 (self->priv->items);
	_g_object_unref0 (self->priv->writer);
	G_OBJECT_CLASS (rygel_m3_uplay_list_parent_class)->finalize (obj);
}

/**
 * Serializer class that serializes to an EXTM3U playlist for use with normal
 * media players or UPnP Renderers that don't support DIDL_S.
 *
 * For the description of the EXTM3U format, see
 * http://en.wikipedia.org/wiki/M3U#Extended_M3U_directives
 */
GType
rygel_m3_uplay_list_get_type (void)
{
	static volatile gsize rygel_m3_uplay_list_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_m3_uplay_list_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelM3UPlayListClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_m3_uplay_list_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelM3UPlayList), 0, (GInstanceInitFunc) rygel_m3_uplay_list_instance_init, NULL };
		GType rygel_m3_uplay_list_type_id;
		rygel_m3_uplay_list_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelM3UPlayList", &g_define_type_info, 0);
		RygelM3UPlayList_private_offset = g_type_add_instance_private (rygel_m3_uplay_list_type_id, sizeof (RygelM3UPlayListPrivate));
		g_once_init_leave (&rygel_m3_uplay_list_type_id__volatile, rygel_m3_uplay_list_type_id);
	}
	return rygel_m3_uplay_list_type_id__volatile;
}

