/* rygel-media-export-playlist-extractor.c generated by valac 0.40.8, the Vala compiler
 * generated from rygel-media-export-playlist-extractor.vala, do not modify */



#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <libgupnp-av/gupnp-av.h>


#define RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR (rygel_media_export_extractor_get_type ())
#define RYGEL_MEDIA_EXPORT_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor))
#define RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractorClass))
#define RYGEL_MEDIA_EXPORT_IS_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_IS_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_EXTRACTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractorClass))

typedef struct _RygelMediaExportExtractor RygelMediaExportExtractor;
typedef struct _RygelMediaExportExtractorClass RygelMediaExportExtractorClass;
typedef struct _RygelMediaExportExtractorPrivate RygelMediaExportExtractorPrivate;

#define RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR (rygel_media_export_playlist_extractor_get_type ())
#define RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR, RygelMediaExportPlaylistExtractor))
#define RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR, RygelMediaExportPlaylistExtractorClass))
#define RYGEL_MEDIA_EXPORT_IS_PLAYLIST_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_IS_PLAYLIST_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR, RygelMediaExportPlaylistExtractorClass))

typedef struct _RygelMediaExportPlaylistExtractor RygelMediaExportPlaylistExtractor;
typedef struct _RygelMediaExportPlaylistExtractorClass RygelMediaExportPlaylistExtractorClass;
typedef struct _RygelMediaExportPlaylistExtractorPrivate RygelMediaExportPlaylistExtractorPrivate;
enum  {
	RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR_0_PROPERTY,
	RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR_NUM_PROPERTIES
};
static GParamSpec* rygel_media_export_playlist_extractor_properties[RYGEL_MEDIA_EXPORT_PLAYLIST_EXTRACTOR_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))
typedef struct _RygelMediaExportPlaylistExtractorRunData RygelMediaExportPlaylistExtractorRunData;

struct _RygelMediaExportExtractor {
	GObject parent_instance;
	RygelMediaExportExtractorPrivate * priv;
	GVariantDict* serialized_info;
};

struct _RygelMediaExportExtractorClass {
	GObjectClass parent_class;
	void (*run) (RygelMediaExportExtractor* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*run_finish) (RygelMediaExportExtractor* self, GAsyncResult* _res_, GError** error);
};

struct _RygelMediaExportPlaylistExtractor {
	RygelMediaExportExtractor parent_instance;
	RygelMediaExportPlaylistExtractorPrivate * priv;
};

struct _RygelMediaExportPlaylistExtractorClass {
	RygelMediaExportExtractorClass parent_class;
};

typedef enum  {
	EXTRACTOR_ERROR_GENERAL,
	EXTRACTOR_ERROR_INVALID
} ExtractorError;
#define EXTRACTOR_ERROR extractor_error_quark ()
struct _RygelMediaExportPlaylistExtractorRunData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GAsyncReadyCallback _callback_;
	gboolean _task_complete_;
	RygelMediaExportPlaylistExtractor* self;
	guint8* contents;
	gint contents_length1;
	gint _contents_size_;
	gboolean _tmp0_;
	GFile* _tmp1_;
	GFile* _tmp2_;
	guint8* _tmp3_;
	gsize _tmp4_;
	gboolean _tmp5_;
	GError* _tmp6_;
	gint i;
	gboolean _tmp7_;
	guint8* _tmp8_;
	gint _tmp8__length1;
	gint _tmp9_;
	guint8 _tmp10_;
	gint _tmp11_;
	guint8* _tmp12_;
	gint _tmp12__length1;
	gint _tmp13_;
	guint8* _tmp14_;
	gint _tmp14__length1;
	gint _tmp15_;
	guint8 _tmp16_;
	GError* _tmp17_;
	GUPnPMediaCollection* didl_s;
	guint8* _tmp18_;
	gint _tmp18__length1;
	GUPnPMediaCollection* _tmp19_;
	gchar* author;
	GUPnPMediaCollection* _tmp20_;
	const gchar* _tmp21_;
	const gchar* _tmp22_;
	gchar* _tmp23_;
	gchar* title;
	GUPnPMediaCollection* _tmp24_;
	const gchar* _tmp25_;
	const gchar* _tmp26_;
	gchar* _tmp27_;
	gboolean _tmp28_;
	gboolean _tmp29_;
	const gchar* _tmp30_;
	const gchar* _tmp31_;
	GUPnPMediaCollection* _tmp32_;
	GList* _tmp33_;
	GList* _tmp34_;
	GError* _tmp35_;
	const gchar* _tmp36_;
	GVariantDict* _tmp37_;
	const gchar* _tmp38_;
	const gchar* _tmp39_;
	GVariantDict* _tmp40_;
	const gchar* _tmp41_;
	GVariantDict* _tmp42_;
	GVariantDict* _tmp43_;
	GError * _inner_error_;
};


static gpointer rygel_media_export_playlist_extractor_parent_class = NULL;

GType rygel_media_export_extractor_get_type (void) G_GNUC_CONST;
GType rygel_media_export_playlist_extractor_get_type (void) G_GNUC_CONST;
RygelMediaExportPlaylistExtractor* rygel_media_export_playlist_extractor_new (GFile* file);
RygelMediaExportPlaylistExtractor* rygel_media_export_playlist_extractor_construct (GType object_type,
                                                                                    GFile* file);
static void rygel_media_export_playlist_extractor_real_run_data_free (gpointer _data);
static void rygel_media_export_playlist_extractor_real_run_async_ready_wrapper (GObject *source_object,
                                                                         GAsyncResult *res,
                                                                         void *user_data);
static void rygel_media_export_playlist_extractor_real_run (RygelMediaExportExtractor* base,
                                                     GAsyncReadyCallback _callback_,
                                                     gpointer _user_data_);
static gboolean rygel_media_export_playlist_extractor_real_run_co (RygelMediaExportPlaylistExtractorRunData* _data_);
void rygel_media_export_extractor_run (RygelMediaExportExtractor* self,
                                       GAsyncReadyCallback _callback_,
                                       gpointer _user_data_);
void rygel_media_export_extractor_run_finish (RygelMediaExportExtractor* self,
                                              GAsyncResult* _res_,
                                              GError** error);
static void rygel_media_export_playlist_extractor_run_ready (GObject* source_object,
                                                      GAsyncResult* _res_,
                                                      gpointer _user_data_);
GFile* rygel_media_export_extractor_get_file (RygelMediaExportExtractor* self);
GQuark extractor_error_quark (void);
static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
#define RYGEL_MEDIA_EXPORT_SERIALIZER_TITLE "Title"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_ARTIST "Artist"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_DLNA_PROFILE "DLNAProfile"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_UPNP_CLASS "UPnPClass"
#define UPNP_CLASS_PLAYLIST "object.item.playlistItem"


RygelMediaExportPlaylistExtractor*
rygel_media_export_playlist_extractor_construct (GType object_type,
                                                 GFile* file)
{
	RygelMediaExportPlaylistExtractor * self = NULL;
	g_return_val_if_fail (file != NULL, NULL);
	self = (RygelMediaExportPlaylistExtractor*) g_object_new (object_type, "file", file, NULL);
	return self;
}


RygelMediaExportPlaylistExtractor*
rygel_media_export_playlist_extractor_new (GFile* file)
{
	return rygel_media_export_playlist_extractor_construct (RYGEL_MEDIA_EXPORT_TYPE_PLAYLIST_EXTRACTOR, file);
}


static void
rygel_media_export_playlist_extractor_real_run_data_free (gpointer _data)
{
	RygelMediaExportPlaylistExtractorRunData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (RygelMediaExportPlaylistExtractorRunData, _data_);
}


static void
rygel_media_export_playlist_extractor_real_run_async_ready_wrapper (GObject *source_object,
                                                                    GAsyncResult *res,
                                                                    void *user_data)
{
	RygelMediaExportPlaylistExtractorRunData* _task_data_;
	_task_data_ = g_task_get_task_data (G_TASK (res));
	if (_task_data_->_callback_ != NULL) {
		_task_data_->_callback_ (source_object, res, user_data);
	}
	_task_data_->_task_complete_ = TRUE;
}


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


static void
rygel_media_export_playlist_extractor_real_run (RygelMediaExportExtractor* base,
                                                GAsyncReadyCallback _callback_,
                                                gpointer _user_data_)
{
	RygelMediaExportPlaylistExtractor * self;
	RygelMediaExportPlaylistExtractorRunData* _data_;
	RygelMediaExportPlaylistExtractor* _tmp0_;
	self = (RygelMediaExportPlaylistExtractor*) base;
	_data_ = g_slice_new0 (RygelMediaExportPlaylistExtractorRunData);
	_data_->_callback_ = _callback_;
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, rygel_media_export_playlist_extractor_real_run_async_ready_wrapper, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, rygel_media_export_playlist_extractor_real_run_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	rygel_media_export_playlist_extractor_real_run_co (_data_);
}


static void
rygel_media_export_playlist_extractor_real_run_finish (RygelMediaExportExtractor* base,
                                                       GAsyncResult* _res_,
                                                       GError** error)
{
	RygelMediaExportPlaylistExtractorRunData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}


static void
rygel_media_export_playlist_extractor_run_ready (GObject* source_object,
                                                 GAsyncResult* _res_,
                                                 gpointer _user_data_)
{
	RygelMediaExportPlaylistExtractorRunData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	_data_->_task_complete_ = TRUE;
	rygel_media_export_playlist_extractor_real_run_co (_data_);
}


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_);
}


static gboolean
rygel_media_export_playlist_extractor_real_run_co (RygelMediaExportPlaylistExtractorRunData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_state_ = 1;
	RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS (rygel_media_export_playlist_extractor_parent_class)->run (G_TYPE_CHECK_INSTANCE_CAST (_data_->self, RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor), rygel_media_export_playlist_extractor_run_ready, _data_);
	return FALSE;
	_state_1:
	RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS (rygel_media_export_playlist_extractor_parent_class)->run_finish (G_TYPE_CHECK_INSTANCE_CAST (_data_->self, RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor), _data_->_res_, &_data_->_inner_error_);
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->contents_length1 = 0;
	_data_->_contents_size_ = 0;
	_data_->_tmp1_ = rygel_media_export_extractor_get_file ((RygelMediaExportExtractor*) _data_->self);
	_data_->_tmp2_ = _data_->_tmp1_;
	_data_->_tmp3_ = NULL;
	_data_->_state_ = 2;
	g_file_load_contents_async (_data_->_tmp2_, NULL, rygel_media_export_playlist_extractor_run_ready, _data_);
	return FALSE;
	_state_2:
	_data_->_tmp5_ = g_file_load_contents_finish (_data_->_tmp2_, _data_->_res_, &_data_->_tmp3_, &_data_->_tmp4_, NULL, &_data_->_inner_error_);
	_data_->contents = (g_free (_data_->contents), NULL);
	_data_->contents = _data_->_tmp3_;
	_data_->contents_length1 = _data_->_tmp4_;
	_data_->_contents_size_ = _data_->contents_length1;
	_data_->_tmp0_ = _data_->_tmp5_;
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_data_->contents = (g_free (_data_->contents), NULL);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	if (!_data_->_tmp0_) {
		_data_->_tmp6_ = g_error_new_literal (EXTRACTOR_ERROR, EXTRACTOR_ERROR_INVALID, "Failed to read file");
		_data_->_inner_error_ = _data_->_tmp6_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_data_->contents = (g_free (_data_->contents), NULL);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->i = 0;
	while (TRUE) {
		_data_->_tmp8_ = _data_->contents;
		_data_->_tmp8__length1 = _data_->contents_length1;
		_data_->_tmp9_ = _data_->i;
		_data_->_tmp10_ = _data_->_tmp8_[_data_->_tmp9_];
		if (g_ascii_isspace ((gchar) _data_->_tmp10_)) {
			_data_->_tmp11_ = _data_->i;
			_data_->_tmp12_ = _data_->contents;
			_data_->_tmp12__length1 = _data_->contents_length1;
			_data_->_tmp7_ = _data_->_tmp11_ < _data_->_tmp12__length1;
		} else {
			_data_->_tmp7_ = FALSE;
		}
		if (!_data_->_tmp7_) {
			break;
		}
		_data_->_tmp13_ = _data_->i;
		_data_->i = _data_->_tmp13_ + 1;
	}
	_data_->_tmp14_ = _data_->contents;
	_data_->_tmp14__length1 = _data_->contents_length1;
	_data_->_tmp15_ = _data_->i;
	_data_->_tmp16_ = _data_->_tmp14_[_data_->_tmp15_];
	if (_data_->_tmp16_ != ((guint8) '<')) {
		_data_->_tmp17_ = g_error_new_literal (EXTRACTOR_ERROR, EXTRACTOR_ERROR_INVALID, "Not an XML file");
		_data_->_inner_error_ = _data_->_tmp17_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_data_->contents = (g_free (_data_->contents), NULL);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp18_ = _data_->contents;
	_data_->_tmp18__length1 = _data_->contents_length1;
	_data_->_tmp19_ = gupnp_media_collection_new_from_string ((const gchar*) _data_->_tmp18_);
	_data_->didl_s = _data_->_tmp19_;
	_data_->_tmp20_ = _data_->didl_s;
	_data_->_tmp21_ = gupnp_media_collection_get_author (_data_->_tmp20_);
	_data_->_tmp22_ = _data_->_tmp21_;
	_data_->_tmp23_ = g_strdup (_data_->_tmp22_);
	_data_->author = _data_->_tmp23_;
	_data_->_tmp24_ = _data_->didl_s;
	_data_->_tmp25_ = gupnp_media_collection_get_title (_data_->_tmp24_);
	_data_->_tmp26_ = _data_->_tmp25_;
	_data_->_tmp27_ = g_strdup (_data_->_tmp26_);
	_data_->title = _data_->_tmp27_;
	_data_->_tmp30_ = _data_->author;
	if (_data_->_tmp30_ == NULL) {
		_data_->_tmp31_ = _data_->title;
		_data_->_tmp29_ = _data_->_tmp31_ == NULL;
	} else {
		_data_->_tmp29_ = FALSE;
	}
	if (_data_->_tmp29_) {
		_data_->_tmp32_ = _data_->didl_s;
		_data_->_tmp33_ = gupnp_media_collection_get_items (_data_->_tmp32_);
		_data_->_tmp34_ = _data_->_tmp33_;
		_data_->_tmp28_ = _data_->_tmp34_ == NULL;
		(_data_->_tmp34_ == NULL) ? NULL : (_data_->_tmp34_ = (_g_list_free__g_object_unref0_ (_data_->_tmp34_), NULL));
	} else {
		_data_->_tmp28_ = FALSE;
	}
	if (_data_->_tmp28_) {
		_data_->_tmp35_ = g_error_new_literal (EXTRACTOR_ERROR, EXTRACTOR_ERROR_INVALID, "Not a DIDL-Lite playlist");
		_data_->_inner_error_ = _data_->_tmp35_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_g_free0 (_data_->title);
		_g_free0 (_data_->author);
		_g_object_unref0 (_data_->didl_s);
		_data_->contents = (g_free (_data_->contents), NULL);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp36_ = _data_->title;
	if (_data_->_tmp36_ != NULL) {
		_data_->_tmp37_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
		_data_->_tmp38_ = _data_->title;
		g_variant_dict_insert (_data_->_tmp37_, RYGEL_MEDIA_EXPORT_SERIALIZER_TITLE, "s", _data_->_tmp38_, NULL);
	}
	_data_->_tmp39_ = _data_->author;
	if (_data_->_tmp39_ != NULL) {
		_data_->_tmp40_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
		_data_->_tmp41_ = _data_->author;
		g_variant_dict_insert (_data_->_tmp40_, RYGEL_MEDIA_EXPORT_SERIALIZER_ARTIST, "s", _data_->_tmp41_, NULL);
	}
	_data_->_tmp42_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	g_variant_dict_insert (_data_->_tmp42_, RYGEL_MEDIA_EXPORT_SERIALIZER_DLNA_PROFILE, "s", "DIDL_S", NULL);
	_data_->_tmp43_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	g_variant_dict_insert (_data_->_tmp43_, RYGEL_MEDIA_EXPORT_SERIALIZER_UPNP_CLASS, "s", UPNP_CLASS_PLAYLIST, NULL);
	_g_free0 (_data_->title);
	_g_free0 (_data_->author);
	_g_object_unref0 (_data_->didl_s);
	_data_->contents = (g_free (_data_->contents), NULL);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void
rygel_media_export_playlist_extractor_class_init (RygelMediaExportPlaylistExtractorClass * klass)
{
	rygel_media_export_playlist_extractor_parent_class = g_type_class_peek_parent (klass);
	((RygelMediaExportExtractorClass *) klass)->run = (void (*) (RygelMediaExportExtractor *, GAsyncReadyCallback, gpointer)) rygel_media_export_playlist_extractor_real_run;
	((RygelMediaExportExtractorClass *) klass)->run_finish = (void (*) (RygelMediaExportExtractor *, GAsyncResult*, GError**)) rygel_media_export_playlist_extractor_real_run_finish;
}


static void
rygel_media_export_playlist_extractor_instance_init (RygelMediaExportPlaylistExtractor * self)
{
}


GType
rygel_media_export_playlist_extractor_get_type (void)
{
	static volatile gsize rygel_media_export_playlist_extractor_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_media_export_playlist_extractor_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelMediaExportPlaylistExtractorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_media_export_playlist_extractor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelMediaExportPlaylistExtractor), 0, (GInstanceInitFunc) rygel_media_export_playlist_extractor_instance_init, NULL };
		GType rygel_media_export_playlist_extractor_type_id;
		rygel_media_export_playlist_extractor_type_id = g_type_register_static (RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, "RygelMediaExportPlaylistExtractor", &g_define_type_info, 0);
		g_once_init_leave (&rygel_media_export_playlist_extractor_type_id__volatile, rygel_media_export_playlist_extractor_type_id);
	}
	return rygel_media_export_playlist_extractor_type_id__volatile;
}



