/* rygel-m3u-playlist.c generated by valac 0.56.1, 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 <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include "rygel-server.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;

VALA_EXTERN GType rygel_m3_uplay_list_get_type (void) G_GNUC_CONST ;
VALA_EXTERN RygelM3UPlayList* rygel_m3_uplay_list_new (void);
VALA_EXTERN RygelM3UPlayList* rygel_m3_uplay_list_construct (GType object_type);
static void rygel_m3_uplay_list_real_constructed (GObject* base);
VALA_EXTERN GUPnPDIDLLiteItem* rygel_m3_uplay_list_add_item (RygelM3UPlayList* self);
VALA_EXTERN 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 GType rygel_m3_uplay_list_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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

GType
rygel_m3_uplay_list_get_type (void)
{
	static volatile gsize rygel_m3_uplay_list_type_id__once = 0;
	if (g_once_init_enter (&rygel_m3_uplay_list_type_id__once)) {
		GType rygel_m3_uplay_list_type_id;
		rygel_m3_uplay_list_type_id = rygel_m3_uplay_list_get_type_once ();
		g_once_init_leave (&rygel_m3_uplay_list_type_id__once, rygel_m3_uplay_list_type_id);
	}
	return rygel_m3_uplay_list_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

