/* rygel-object-removal-queue.c generated by valac 0.56.19, the Vala compiler
 * generated from rygel-object-removal-queue.vala, do not modify */

/*
 * Copyright (C) 2010 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include "rygel-server.h"
#include <gio/gio.h>

#define RYGEL_OBJECT_REMOVAL_QUEUE_TIMEOUT ((guint) 35)
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define RYGEL_TYPE_OBJECT_REMOVAL_QUEUE (rygel_object_removal_queue_get_type ())
#define RYGEL_OBJECT_REMOVAL_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_OBJECT_REMOVAL_QUEUE, RygelObjectRemovalQueue))
#define RYGEL_OBJECT_REMOVAL_QUEUE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_OBJECT_REMOVAL_QUEUE, RygelObjectRemovalQueueClass))
#define RYGEL_IS_OBJECT_REMOVAL_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_OBJECT_REMOVAL_QUEUE))
#define RYGEL_IS_OBJECT_REMOVAL_QUEUE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_OBJECT_REMOVAL_QUEUE))
#define RYGEL_OBJECT_REMOVAL_QUEUE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_OBJECT_REMOVAL_QUEUE, RygelObjectRemovalQueueClass))

typedef struct _RygelObjectRemovalQueue RygelObjectRemovalQueue;
typedef struct _RygelObjectRemovalQueueClass RygelObjectRemovalQueueClass;
typedef struct _RygelObjectRemovalQueuePrivate RygelObjectRemovalQueuePrivate;
enum  {
	RYGEL_OBJECT_REMOVAL_QUEUE_0_PROPERTY,
	RYGEL_OBJECT_REMOVAL_QUEUE_NUM_PROPERTIES
};
static GParamSpec* rygel_object_removal_queue_properties[RYGEL_OBJECT_REMOVAL_QUEUE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block10Data Block10Data;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _RygelObjectRemovalQueueRemoveNowData RygelObjectRemovalQueueRemoveNowData;

struct _RygelObjectRemovalQueue {
	GObject parent_instance;
	RygelObjectRemovalQueuePrivate * priv;
};

struct _RygelObjectRemovalQueueClass {
	GObjectClass parent_class;
};

struct _RygelObjectRemovalQueuePrivate {
	GeeHashMap* object_timeouts;
};

struct _Block10Data {
	int _ref_count_;
	RygelObjectRemovalQueue* self;
	RygelMediaObject* object;
	GCancellable* cancellable;
};

struct _RygelObjectRemovalQueueRemoveNowData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	RygelObjectRemovalQueue* self;
	RygelMediaObject* object;
	GCancellable* cancellable;
	GeeHashMap* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	RygelWritableContainer* parent;
	RygelMediaContainer* _tmp3_;
	RygelMediaContainer* _tmp4_;
	RygelWritableContainer* _tmp5_;
	RygelWritableContainer* _tmp6_;
	const gchar* _tmp7_;
	const gchar* _tmp8_;
	RygelWritableContainer* _tmp9_;
	const gchar* _tmp10_;
	const gchar* _tmp11_;
	const gchar* _tmp12_;
	const gchar* _tmp13_;
	GError* err;
	const gchar* _tmp14_;
	const gchar* _tmp15_;
	GError* _tmp16_;
	const gchar* _tmp17_;
	GError* _inner_error0_;
};

static gint RygelObjectRemovalQueue_private_offset;
static gpointer rygel_object_removal_queue_parent_class = NULL;
static RygelObjectRemovalQueue* rygel_object_removal_queue_removal_queue;
static RygelObjectRemovalQueue* rygel_object_removal_queue_removal_queue = NULL;

VALA_EXTERN GType rygel_object_removal_queue_get_type (void) G_GNUC_CONST ;
VALA_EXTERN RygelObjectRemovalQueue* rygel_object_removal_queue_get_default (void);
static RygelObjectRemovalQueue* rygel_object_removal_queue_new (void);
static RygelObjectRemovalQueue* rygel_object_removal_queue_construct (GType object_type);
VALA_EXTERN void rygel_object_removal_queue_queue (RygelObjectRemovalQueue* self,
                                       RygelMediaObject* object,
                                       GCancellable* cancellable);
static Block10Data* block10_data_ref (Block10Data* _data10_);
static void block10_data_unref (void * _userdata_);
static gboolean __lambda7_ (Block10Data* _data10_);
VALA_EXTERN void rygel_object_removal_queue_remove_now (RygelObjectRemovalQueue* self,
                                            RygelMediaObject* object,
                                            GCancellable* cancellable,
                                            GAsyncReadyCallback _callback_,
                                            gpointer _user_data_);
VALA_EXTERN void rygel_object_removal_queue_remove_now_finish (RygelObjectRemovalQueue* self,
                                                   GAsyncResult* _res_);
static gboolean ___lambda7__gsource_func (gpointer self);
VALA_EXTERN gboolean rygel_object_removal_queue_dequeue (RygelObjectRemovalQueue* self,
                                             RygelMediaObject* object);
static void rygel_object_removal_queue_remove_now_data_free (gpointer _data);
static gboolean rygel_object_removal_queue_remove_now_co (RygelObjectRemovalQueueRemoveNowData* _data_);
static void rygel_object_removal_queue_remove_now_ready (GObject* source_object,
                                                  GAsyncResult* _res_,
                                                  gpointer _user_data_);
static void rygel_object_removal_queue_finalize (GObject * obj);
static GType rygel_object_removal_queue_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
rygel_object_removal_queue_get_instance_private (RygelObjectRemovalQueue* self)
{
	return G_STRUCT_MEMBER_P (self, RygelObjectRemovalQueue_private_offset);
}

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

RygelObjectRemovalQueue*
rygel_object_removal_queue_get_default (void)
{
	RygelObjectRemovalQueue* _tmp0_;
	RygelObjectRemovalQueue* _tmp2_;
	RygelObjectRemovalQueue* _tmp3_;
	RygelObjectRemovalQueue* result;
	_tmp0_ = rygel_object_removal_queue_removal_queue;
	if (G_UNLIKELY (_tmp0_ == NULL)) {
		RygelObjectRemovalQueue* _tmp1_;
		_tmp1_ = rygel_object_removal_queue_new ();
		_g_object_unref0 (rygel_object_removal_queue_removal_queue);
		rygel_object_removal_queue_removal_queue = _tmp1_;
	}
	_tmp2_ = rygel_object_removal_queue_removal_queue;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static Block10Data*
block10_data_ref (Block10Data* _data10_)
{
	g_atomic_int_inc (&_data10_->_ref_count_);
	return _data10_;
}

static void
block10_data_unref (void * _userdata_)
{
	Block10Data* _data10_;
	_data10_ = (Block10Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data10_->_ref_count_)) {
		RygelObjectRemovalQueue* self;
		self = _data10_->self;
		_g_object_unref0 (_data10_->object);
		_g_object_unref0 (_data10_->cancellable);
		_g_object_unref0 (self);
		g_slice_free (Block10Data, _data10_);
	}
}

static gboolean
__lambda7_ (Block10Data* _data10_)
{
	RygelObjectRemovalQueue* self;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gboolean result;
	self = _data10_->self;
	_tmp0_ = rygel_media_object_get_id (_data10_->object);
	_tmp1_ = _tmp0_;
	g_debug ("rygel-object-removal-queue.vala:51: Timeout on temporary object '%s'.", _tmp1_);
	rygel_object_removal_queue_remove_now (self, _data10_->object, _data10_->cancellable, NULL, NULL);
	result = FALSE;
	return result;
}

static gboolean
___lambda7__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda7_ (self);
	return result;
}

void
rygel_object_removal_queue_queue (RygelObjectRemovalQueue* self,
                                  RygelMediaObject* object,
                                  GCancellable* cancellable)
{
	Block10Data* _data10_;
	RygelMediaObject* _tmp0_;
	GCancellable* _tmp1_;
	RygelMediaContainer* _tmp2_;
	RygelMediaContainer* _tmp3_;
	guint timeout = 0U;
	GeeHashMap* _tmp6_;
	const gchar* _tmp7_;
	const gchar* _tmp8_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (object != NULL);
	_data10_ = g_slice_new0 (Block10Data);
	_data10_->_ref_count_ = 1;
	_data10_->self = g_object_ref (self);
	_tmp0_ = _g_object_ref0 (object);
	_g_object_unref0 (_data10_->object);
	_data10_->object = _tmp0_;
	_tmp1_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data10_->cancellable);
	_data10_->cancellable = _tmp1_;
	_tmp2_ = rygel_media_object_get_parent_ref (_data10_->object);
	_tmp3_ = _tmp2_;
	if (_tmp3_ == NULL) {
		RygelMediaContainer* _tmp4_;
		RygelMediaContainer* _tmp5_;
		_tmp4_ = rygel_media_object_get_parent (_data10_->object);
		_tmp5_ = _tmp4_;
		rygel_media_object_set_parent_ref (_data10_->object, _tmp5_);
	}
	timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, RYGEL_OBJECT_REMOVAL_QUEUE_TIMEOUT, ___lambda7__gsource_func, block10_data_ref (_data10_), block10_data_unref);
	_tmp6_ = self->priv->object_timeouts;
	_tmp7_ = rygel_media_object_get_id (_data10_->object);
	_tmp8_ = _tmp7_;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp6_, _tmp8_, (gpointer) ((guintptr) timeout));
	block10_data_unref (_data10_);
	_data10_ = NULL;
}

gboolean
rygel_object_removal_queue_dequeue (RygelObjectRemovalQueue* self,
                                    RygelMediaObject* object)
{
	guint timeout = 0U;
	GeeHashMap* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gpointer _tmp3_ = NULL;
	gboolean _tmp4_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (object != NULL, FALSE);
	_tmp0_ = self->priv->object_timeouts;
	_tmp1_ = rygel_media_object_get_id (object);
	_tmp2_ = _tmp1_;
	_tmp4_ = gee_abstract_map_unset ((GeeAbstractMap*) _tmp0_, _tmp2_, &_tmp3_);
	timeout = (guint) ((guintptr) _tmp3_);
	if (_tmp4_) {
		g_source_remove (timeout);
		result = TRUE;
		return result;
	} else {
		result = FALSE;
		return result;
	}
}

static void
rygel_object_removal_queue_remove_now_data_free (gpointer _data)
{
	RygelObjectRemovalQueueRemoveNowData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->object);
	_g_object_unref0 (_data_->cancellable);
	_g_object_unref0 (_data_->self);
	g_slice_free (RygelObjectRemovalQueueRemoveNowData, _data_);
}

void
rygel_object_removal_queue_remove_now (RygelObjectRemovalQueue* self,
                                       RygelMediaObject* object,
                                       GCancellable* cancellable,
                                       GAsyncReadyCallback _callback_,
                                       gpointer _user_data_)
{
	RygelObjectRemovalQueueRemoveNowData* _data_;
	RygelObjectRemovalQueue* _tmp0_;
	RygelMediaObject* _tmp1_;
	GCancellable* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (object != NULL);
	_data_ = g_slice_new0 (RygelObjectRemovalQueueRemoveNowData);
	_data_->_async_result = g_task_new (G_OBJECT (self), cancellable, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, rygel_object_removal_queue_remove_now_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (object);
	_g_object_unref0 (_data_->object);
	_data_->object = _tmp1_;
	_tmp2_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp2_;
	rygel_object_removal_queue_remove_now_co (_data_);
}

void
rygel_object_removal_queue_remove_now_finish (RygelObjectRemovalQueue* self,
                                              GAsyncResult* _res_)
{
	RygelObjectRemovalQueueRemoveNowData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	if (NULL == _data_) {
		return;
	}
}

static void
rygel_object_removal_queue_remove_now_ready (GObject* source_object,
                                             GAsyncResult* _res_,
                                             gpointer _user_data_)
{
	RygelObjectRemovalQueueRemoveNowData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	rygel_object_removal_queue_remove_now_co (_data_);
}

static gboolean
rygel_object_removal_queue_remove_now_co (RygelObjectRemovalQueueRemoveNowData* _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_->_tmp0_ = _data_->self->priv->object_timeouts;
	_data_->_tmp1_ = rygel_media_object_get_id (_data_->object);
	_data_->_tmp2_ = _data_->_tmp1_;
	gee_abstract_map_unset ((GeeAbstractMap*) _data_->_tmp0_, _data_->_tmp2_, NULL);
	_data_->_tmp3_ = rygel_media_object_get_parent (_data_->object);
	_data_->_tmp4_ = _data_->_tmp3_;
	_data_->_tmp5_ = _g_object_ref0 (RYGEL_IS_WRITABLE_CONTAINER (_data_->_tmp4_) ? ((RygelWritableContainer*) _data_->_tmp4_) : NULL);
	_data_->parent = _data_->_tmp5_;
	{
		if (RYGEL_IS_MEDIA_ITEM (_data_->object)) {
			_data_->_tmp6_ = _data_->parent;
			_data_->_tmp7_ = rygel_media_object_get_id (_data_->object);
			_data_->_tmp8_ = _data_->_tmp7_;
			_data_->_state_ = 1;
			rygel_writable_container_remove_item (_data_->_tmp6_, _data_->_tmp8_, _data_->cancellable, rygel_object_removal_queue_remove_now_ready, _data_);
			return FALSE;
			_state_1:
			rygel_writable_container_remove_item_finish (_data_->_tmp6_, _data_->_res_, &_data_->_inner_error0_);
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		} else {
			_data_->_tmp9_ = _data_->parent;
			_data_->_tmp10_ = rygel_media_object_get_id (_data_->object);
			_data_->_tmp11_ = _data_->_tmp10_;
			_data_->_state_ = 2;
			rygel_writable_container_remove_container (_data_->_tmp9_, _data_->_tmp11_, _data_->cancellable, rygel_object_removal_queue_remove_now_ready, _data_);
			return FALSE;
			_state_2:
			rygel_writable_container_remove_container_finish (_data_->_tmp9_, _data_->_res_, &_data_->_inner_error0_);
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		_data_->_tmp12_ = rygel_media_object_get_id (_data_->object);
		_data_->_tmp13_ = _data_->_tmp12_;
		g_debug ("rygel-object-removal-queue.vala:84: Auto-destroyed object '%s'!", _data_->_tmp13_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->err = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp14_ = rygel_media_object_get_id (_data_->object);
		_data_->_tmp15_ = _data_->_tmp14_;
		_data_->_tmp16_ = _data_->err;
		_data_->_tmp17_ = _data_->_tmp16_->message;
		g_warning ("rygel-object-removal-queue.vala:86: Failed to auto-destroy temporary o" \
"bject '%s': %s", _data_->_tmp15_, _data_->_tmp17_);
		_g_error_free0 (_data_->err);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_g_object_unref0 (_data_->parent);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_g_object_unref0 (_data_->parent);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static RygelObjectRemovalQueue*
rygel_object_removal_queue_construct (GType object_type)
{
	RygelObjectRemovalQueue * self = NULL;
	GeeHashMap* _tmp0_;
	self = (RygelObjectRemovalQueue*) g_object_new (object_type, NULL);
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, G_TYPE_UINT, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->object_timeouts);
	self->priv->object_timeouts = _tmp0_;
	return self;
}

static RygelObjectRemovalQueue*
rygel_object_removal_queue_new (void)
{
	return rygel_object_removal_queue_construct (RYGEL_TYPE_OBJECT_REMOVAL_QUEUE);
}

static void
rygel_object_removal_queue_class_init (RygelObjectRemovalQueueClass * klass,
                                       gpointer klass_data)
{
	rygel_object_removal_queue_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelObjectRemovalQueue_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_object_removal_queue_finalize;
}

static void
rygel_object_removal_queue_instance_init (RygelObjectRemovalQueue * self,
                                          gpointer klass)
{
	self->priv = rygel_object_removal_queue_get_instance_private (self);
}

static void
rygel_object_removal_queue_finalize (GObject * obj)
{
	RygelObjectRemovalQueue * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_OBJECT_REMOVAL_QUEUE, RygelObjectRemovalQueue);
	_g_object_unref0 (self->priv->object_timeouts);
	G_OBJECT_CLASS (rygel_object_removal_queue_parent_class)->finalize (obj);
}

/**
 * Queues objects for removal after 35 seconds or immediately.
 *
 * The 35s timeout comes from the DLNA documentation.
 */
 G_GNUC_NO_INLINE static GType
rygel_object_removal_queue_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelObjectRemovalQueueClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_object_removal_queue_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelObjectRemovalQueue), 0, (GInstanceInitFunc) rygel_object_removal_queue_instance_init, NULL };
	GType rygel_object_removal_queue_type_id;
	rygel_object_removal_queue_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelObjectRemovalQueue", &g_define_type_info, 0);
	RygelObjectRemovalQueue_private_offset = g_type_add_instance_private (rygel_object_removal_queue_type_id, sizeof (RygelObjectRemovalQueuePrivate));
	return rygel_object_removal_queue_type_id;
}

GType
rygel_object_removal_queue_get_type (void)
{
	static volatile gsize rygel_object_removal_queue_type_id__once = 0;
	if (g_once_init_enter (&rygel_object_removal_queue_type_id__once)) {
		GType rygel_object_removal_queue_type_id;
		rygel_object_removal_queue_type_id = rygel_object_removal_queue_get_type_once ();
		g_once_init_leave (&rygel_object_removal_queue_type_id__once, rygel_object_removal_queue_type_id);
	}
	return rygel_object_removal_queue_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;
}

