/* rygel-last-change.c generated by valac 0.30.0, the Vala compiler
 * generated from rygel-last-change.vala, do not modify */

/*
 * Copyright (C) 2012 Openismus GmbH
 *
 * Author: Krzesimir Nowak <krnowak@openismus.com>
 *
 * 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 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>


#define RYGEL_TYPE_LAST_CHANGE (rygel_last_change_get_type ())
#define RYGEL_LAST_CHANGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_LAST_CHANGE, RygelLastChange))
#define RYGEL_LAST_CHANGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_LAST_CHANGE, RygelLastChangeClass))
#define RYGEL_IS_LAST_CHANGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_LAST_CHANGE))
#define RYGEL_IS_LAST_CHANGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_LAST_CHANGE))
#define RYGEL_LAST_CHANGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_LAST_CHANGE, RygelLastChangeClass))

typedef struct _RygelLastChange RygelLastChange;
typedef struct _RygelLastChangeClass RygelLastChangeClass;
typedef struct _RygelLastChangePrivate RygelLastChangePrivate;

#define RYGEL_TYPE_LAST_CHANGE_ENTRY (rygel_last_change_entry_get_type ())
#define RYGEL_LAST_CHANGE_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_LAST_CHANGE_ENTRY, RygelLastChangeEntry))
#define RYGEL_LAST_CHANGE_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_LAST_CHANGE_ENTRY, RygelLastChangeEntryClass))
#define RYGEL_IS_LAST_CHANGE_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_LAST_CHANGE_ENTRY))
#define RYGEL_IS_LAST_CHANGE_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_LAST_CHANGE_ENTRY))
#define RYGEL_LAST_CHANGE_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_LAST_CHANGE_ENTRY, RygelLastChangeEntryClass))

typedef struct _RygelLastChangeEntry RygelLastChangeEntry;
typedef struct _RygelLastChangeEntryClass RygelLastChangeEntryClass;
#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)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _RygelLastChange {
	GObject parent_instance;
	RygelLastChangePrivate * priv;
};

struct _RygelLastChangeClass {
	GObjectClass parent_class;
};

struct _RygelLastChangePrivate {
	GeeLinkedList* entries;
	GString* str;
	gboolean update;
	gboolean clear_on_add;
};


static gpointer rygel_last_change_parent_class = NULL;

GType rygel_last_change_get_type (void) G_GNUC_CONST;
GType rygel_last_change_entry_get_type (void) G_GNUC_CONST;
#define RYGEL_LAST_CHANGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TYPE_LAST_CHANGE, RygelLastChangePrivate))
enum  {
	RYGEL_LAST_CHANGE_DUMMY_PROPERTY
};
#define RYGEL_LAST_CHANGE_HEADER "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<StateEvent " "xmlns=\"urn:schemas-upnp-org:av:cds-event\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xsi:schemaLocation=\"" "urn:schemas-upnp-org:av:cds-event " "http://www.upnp.org/schemas/av/cds-events.xsd\">"
#define RYGEL_LAST_CHANGE_FOOTER "</StateEvent>"
RygelLastChange* rygel_last_change_new (void);
RygelLastChange* rygel_last_change_construct (GType object_type);
void rygel_last_change_add_event (RygelLastChange* self, RygelLastChangeEntry* entry);
void rygel_last_change_clear_on_new_event (RygelLastChange* self);
gchar* rygel_last_change_get_log (RygelLastChange* self);
gchar* rygel_last_change_entry_to_string (RygelLastChangeEntry* self);
static void rygel_last_change_finalize (GObject* obj);


RygelLastChange* rygel_last_change_construct (GType object_type) {
	RygelLastChange * self = NULL;
	GeeLinkedList* _tmp0_ = NULL;
	GString* _tmp1_ = NULL;
	self = (RygelLastChange*) g_object_new (object_type, NULL);
	_tmp0_ = gee_linked_list_new (RYGEL_TYPE_LAST_CHANGE_ENTRY, (GBoxedCopyFunc) g_object_ref, g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->entries);
	self->priv->entries = _tmp0_;
	_tmp1_ = g_string_new ("");
	_g_string_free0 (self->priv->str);
	self->priv->str = _tmp1_;
	self->priv->update = TRUE;
	self->priv->clear_on_add = FALSE;
	return self;
}


RygelLastChange* rygel_last_change_new (void) {
	return rygel_last_change_construct (RYGEL_TYPE_LAST_CHANGE);
}


void rygel_last_change_add_event (RygelLastChange* self, RygelLastChangeEntry* entry) {
	gboolean _tmp0_ = FALSE;
	GeeLinkedList* _tmp2_ = NULL;
	RygelLastChangeEntry* _tmp3_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (entry != NULL);
	_tmp0_ = self->priv->clear_on_add;
	if (_tmp0_) {
		GeeLinkedList* _tmp1_ = NULL;
		self->priv->clear_on_add = FALSE;
		_tmp1_ = self->priv->entries;
		gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp1_);
	}
	_tmp2_ = self->priv->entries;
	_tmp3_ = entry;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp2_, _tmp3_);
	self->priv->update = TRUE;
}


void rygel_last_change_clear_on_new_event (RygelLastChange* self) {
	g_return_if_fail (self != NULL);
	self->priv->clear_on_add = TRUE;
}


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


gchar* rygel_last_change_get_log (RygelLastChange* self) {
	gchar* result = NULL;
	gboolean _tmp0_ = FALSE;
	GString* _tmp19_ = NULL;
	const gchar* _tmp20_ = NULL;
	gchar* _tmp21_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->update;
	if (_tmp0_) {
		GString* _tmp1_ = NULL;
		GString* _tmp2_ = NULL;
		GString* _tmp18_ = NULL;
		_tmp1_ = self->priv->str;
		g_string_erase (_tmp1_, (gssize) 0, (gssize) -1);
		_tmp2_ = self->priv->str;
		g_string_append (_tmp2_, RYGEL_LAST_CHANGE_HEADER);
		{
			GeeLinkedList* _entry_list = NULL;
			GeeLinkedList* _tmp3_ = NULL;
			GeeLinkedList* _tmp4_ = NULL;
			gint _entry_size = 0;
			GeeLinkedList* _tmp5_ = NULL;
			gint _tmp6_ = 0;
			gint _tmp7_ = 0;
			gint _entry_index = 0;
			_tmp3_ = self->priv->entries;
			_tmp4_ = _g_object_ref0 (_tmp3_);
			_entry_list = _tmp4_;
			_tmp5_ = _entry_list;
			_tmp6_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp5_);
			_tmp7_ = _tmp6_;
			_entry_size = _tmp7_;
			_entry_index = -1;
			while (TRUE) {
				gint _tmp8_ = 0;
				gint _tmp9_ = 0;
				gint _tmp10_ = 0;
				RygelLastChangeEntry* entry = NULL;
				GeeLinkedList* _tmp11_ = NULL;
				gint _tmp12_ = 0;
				gpointer _tmp13_ = NULL;
				GString* _tmp14_ = NULL;
				RygelLastChangeEntry* _tmp15_ = NULL;
				gchar* _tmp16_ = NULL;
				gchar* _tmp17_ = NULL;
				_tmp8_ = _entry_index;
				_entry_index = _tmp8_ + 1;
				_tmp9_ = _entry_index;
				_tmp10_ = _entry_size;
				if (!(_tmp9_ < _tmp10_)) {
					break;
				}
				_tmp11_ = _entry_list;
				_tmp12_ = _entry_index;
				_tmp13_ = gee_abstract_list_get ((GeeAbstractList*) _tmp11_, _tmp12_);
				entry = (RygelLastChangeEntry*) _tmp13_;
				_tmp14_ = self->priv->str;
				_tmp15_ = entry;
				_tmp16_ = rygel_last_change_entry_to_string (_tmp15_);
				_tmp17_ = _tmp16_;
				g_string_append (_tmp14_, _tmp17_);
				_g_free0 (_tmp17_);
				_g_object_unref0 (entry);
			}
			_g_object_unref0 (_entry_list);
		}
		_tmp18_ = self->priv->str;
		g_string_append (_tmp18_, RYGEL_LAST_CHANGE_FOOTER);
		self->priv->update = FALSE;
	}
	_tmp19_ = self->priv->str;
	_tmp20_ = _tmp19_->str;
	_tmp21_ = g_strdup (_tmp20_);
	result = _tmp21_;
	return result;
}


static void rygel_last_change_class_init (RygelLastChangeClass * klass) {
	rygel_last_change_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelLastChangePrivate));
	G_OBJECT_CLASS (klass)->finalize = rygel_last_change_finalize;
}


static void rygel_last_change_instance_init (RygelLastChange * self) {
	self->priv = RYGEL_LAST_CHANGE_GET_PRIVATE (self);
}


static void rygel_last_change_finalize (GObject* obj) {
	RygelLastChange * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_LAST_CHANGE, RygelLastChange);
	_g_object_unref0 (self->priv->entries);
	_g_string_free0 (self->priv->str);
	G_OBJECT_CLASS (rygel_last_change_parent_class)->finalize (obj);
}


GType rygel_last_change_get_type (void) {
	static volatile gsize rygel_last_change_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_last_change_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelLastChangeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_last_change_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelLastChange), 0, (GInstanceInitFunc) rygel_last_change_instance_init, NULL };
		GType rygel_last_change_type_id;
		rygel_last_change_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelLastChange", &g_define_type_info, 0);
		g_once_init_leave (&rygel_last_change_type_id__volatile, rygel_last_change_type_id);
	}
	return rygel_last_change_type_id__volatile;
}



