/* rygel-tracker-category-container.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-tracker-category-container.vala, do not modify */

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

enum  {
	RYGEL_TRACKER_CATEGORY_CONTAINER_0_PROPERTY,
	RYGEL_TRACKER_CATEGORY_CONTAINER_NUM_PROPERTIES
};
static GParamSpec* rygel_tracker_category_container_properties[RYGEL_TRACKER_CATEGORY_CONTAINER_NUM_PROPERTIES];
#define _rygel_tracker_item_factory_unref0(var) ((var == NULL) ? NULL : (var = (rygel_tracker_item_factory_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block1Data Block1Data;

struct _RygelTrackerCategoryContainerPrivate {
	RygelTrackerCategoryAllContainer* all_container;
};

struct _Block1Data {
	int _ref_count_;
	RygelTrackerCategoryContainer* self;
	gulong signal_id;
};

static gint RygelTrackerCategoryContainer_private_offset;
static gpointer rygel_tracker_category_container_parent_class = NULL;

static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void __lambda6_ (Block1Data* _data1_);
static void rygel_tracker_category_container_on_all_container_updated (RygelTrackerCategoryContainer* self,
                                                                RygelMediaContainer* other);
static void _rygel_tracker_category_container_on_all_container_updated_rygel_media_container_container_updated (RygelMediaContainer* _sender,
                                                                                                         RygelMediaContainer* container,
                                                                                                         RygelMediaObject* object,
                                                                                                         RygelObjectEventType event_type,
                                                                                                         gboolean sub_tree_update,
                                                                                                         gpointer self);
static void ___lambda6__rygel_media_container_container_updated (RygelMediaContainer* _sender,
                                                          RygelMediaContainer* container,
                                                          RygelMediaObject* object,
                                                          RygelObjectEventType event_type,
                                                          gboolean sub_tree_update,
                                                          gpointer self);
static void rygel_tracker_category_container_trigger_child_update (RygelTrackerCategoryContainer* self,
                                                            RygelMediaObjects* children);
VALA_EXTERN void rygel_tracker_metadata_container_fetch_metadata_values (RygelTrackerMetadataContainer* self,
                                                             GAsyncReadyCallback _callback_,
                                                             gpointer _user_data_);
VALA_EXTERN void rygel_tracker_metadata_container_fetch_metadata_values_finish (RygelTrackerMetadataContainer* self,
                                                                    GAsyncResult* _res_);
VALA_EXTERN void rygel_tracker_search_container_get_children_count (RygelTrackerSearchContainer* self,
                                                        GAsyncReadyCallback _callback_,
                                                        gpointer _user_data_);
VALA_EXTERN void rygel_tracker_search_container_get_children_count_finish (RygelTrackerSearchContainer* self,
                                                               GAsyncResult* _res_);
static void rygel_tracker_category_container_finalize (GObject * obj);
static GType rygel_tracker_category_container_get_type_once (void);

static inline gpointer
rygel_tracker_category_container_get_instance_private (RygelTrackerCategoryContainer* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerCategoryContainer_private_offset);
}

static Block1Data*
block1_data_ref (Block1Data* _data1_)
{
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}

static void
block1_data_unref (void * _userdata_)
{
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		RygelTrackerCategoryContainer* self;
		self = _data1_->self;
		_g_object_unref0 (self);
		g_slice_free (Block1Data, _data1_);
	}
}

static gpointer
_rygel_tracker_item_factory_ref0 (gpointer self)
{
	return self ? rygel_tracker_item_factory_ref (self) : NULL;
}

static void
_rygel_tracker_category_container_on_all_container_updated_rygel_media_container_container_updated (RygelMediaContainer* _sender,
                                                                                                    RygelMediaContainer* container,
                                                                                                    RygelMediaObject* object,
                                                                                                    RygelObjectEventType event_type,
                                                                                                    gboolean sub_tree_update,
                                                                                                    gpointer self)
{
	rygel_tracker_category_container_on_all_container_updated ((RygelTrackerCategoryContainer*) self, container);
}

static void
__lambda6_ (Block1Data* _data1_)
{
	RygelTrackerCategoryContainer* self;
	RygelTrackerCategoryAllContainer* _tmp0_;
	RygelTrackerCategoryAllContainer* _tmp1_;
	self = _data1_->self;
	_tmp0_ = self->priv->all_container;
	g_signal_connect_object ((RygelMediaContainer*) _tmp0_, "container-updated", (GCallback) _rygel_tracker_category_container_on_all_container_updated_rygel_media_container_container_updated, self, 0);
	_tmp1_ = self->priv->all_container;
	g_signal_handler_disconnect ((GObject*) _tmp1_, _data1_->signal_id);
}

static void
___lambda6__rygel_media_container_container_updated (RygelMediaContainer* _sender,
                                                     RygelMediaContainer* container,
                                                     RygelMediaObject* object,
                                                     RygelObjectEventType event_type,
                                                     gboolean sub_tree_update,
                                                     gpointer self)
{
	__lambda6_ (self);
}

RygelTrackerCategoryContainer*
rygel_tracker_category_container_construct (GType object_type,
                                            const gchar* id,
                                            RygelMediaContainer* parent,
                                            const gchar* title,
                                            RygelTrackerItemFactory* item_factory)
{
	RygelTrackerCategoryContainer * self = NULL;
	Block1Data* _data1_;
	RygelTrackerItemFactory* _tmp0_;
	RygelTrackerCategoryAllContainer* _tmp1_;
	RygelTrackerCategoryAllContainer* _tmp2_;
	RygelTrackerTags* _tmp3_;
	RygelTrackerTags* _tmp4_;
	RygelTrackerItemFactory* _tmp5_;
	RygelTrackerTitles* _tmp6_;
	RygelTrackerTitles* _tmp7_;
	RygelTrackerItemFactory* _tmp8_;
	RygelTrackerNew* _tmp9_;
	RygelTrackerNew* _tmp10_;
	RygelTrackerCategoryAllContainer* _tmp11_;
	gulong _tmp12_;
	g_return_val_if_fail (id != NULL, NULL);
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (title != NULL, NULL);
	g_return_val_if_fail (item_factory != NULL, NULL);
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	self = (RygelTrackerCategoryContainer*) rygel_simple_container_construct (object_type, id, parent, title);
	_data1_->self = g_object_ref (self);
	_tmp0_ = _rygel_tracker_item_factory_ref0 (item_factory);
	_rygel_tracker_item_factory_unref0 (self->item_factory);
	self->item_factory = _tmp0_;
	_tmp1_ = rygel_tracker_category_all_container_new (self);
	_g_object_unref0 (self->priv->all_container);
	self->priv->all_container = _tmp1_;
	_tmp2_ = self->priv->all_container;
	rygel_simple_container_add_child_container ((RygelSimpleContainer*) self, (RygelMediaContainer*) _tmp2_);
	_tmp3_ = rygel_tracker_tags_new ((RygelMediaContainer*) self, item_factory);
	_tmp4_ = _tmp3_;
	rygel_simple_container_add_child_container ((RygelSimpleContainer*) self, (RygelMediaContainer*) _tmp4_);
	_g_object_unref0 (_tmp4_);
	_tmp5_ = self->item_factory;
	_tmp6_ = rygel_tracker_titles_new ((RygelMediaContainer*) self, _tmp5_);
	_tmp7_ = _tmp6_;
	rygel_simple_container_add_child_container ((RygelSimpleContainer*) self, (RygelMediaContainer*) _tmp7_);
	_g_object_unref0 (_tmp7_);
	_tmp8_ = self->item_factory;
	_tmp9_ = rygel_tracker_new_new ((RygelMediaContainer*) self, _tmp8_);
	_tmp10_ = _tmp9_;
	rygel_simple_container_add_child_container ((RygelSimpleContainer*) self, (RygelMediaContainer*) _tmp10_);
	_g_object_unref0 (_tmp10_);
	_data1_->signal_id = (gulong) 0;
	_tmp11_ = self->priv->all_container;
	_tmp12_ = g_signal_connect_data ((RygelMediaContainer*) _tmp11_, "container-updated", (GCallback) ___lambda6__rygel_media_container_container_updated, block1_data_ref (_data1_), (GClosureNotify) block1_data_unref, 0);
	_data1_->signal_id = _tmp12_;
	block1_data_unref (_data1_);
	_data1_ = NULL;
	return self;
}

void
rygel_tracker_category_container_add_create_class (RygelTrackerCategoryContainer* self,
                                                   const gchar* create_class)
{
	RygelTrackerCategoryAllContainer* _tmp0_;
	GeeArrayList* _tmp1_;
	GeeArrayList* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (create_class != NULL);
	_tmp0_ = self->priv->all_container;
	_tmp1_ = rygel_writable_container_get_create_classes ((RygelWritableContainer*) _tmp0_);
	_tmp2_ = _tmp1_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp2_, create_class);
}

static void
rygel_tracker_category_container_trigger_child_update (RygelTrackerCategoryContainer* self,
                                                       RygelMediaObjects* children)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (children != NULL);
	{
		RygelMediaObjects* _container_list = NULL;
		gint _container_size = 0;
		RygelMediaObjects* _tmp0_;
		gint _tmp1_;
		gint _tmp2_;
		gint _container_index = 0;
		_container_list = children;
		_tmp0_ = _container_list;
		_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
		_tmp2_ = _tmp1_;
		_container_size = _tmp2_;
		_container_index = -1;
		while (TRUE) {
			gint _tmp3_;
			gint _tmp4_;
			RygelMediaObject* container = NULL;
			RygelMediaObjects* _tmp5_;
			gpointer _tmp6_;
			gboolean _tmp7_ = FALSE;
			RygelMediaObject* _tmp8_;
			RygelTrackerCategoryAllContainer* _tmp9_;
			RygelMediaObject* _tmp11_;
			_container_index = _container_index + 1;
			_tmp3_ = _container_index;
			_tmp4_ = _container_size;
			if (!(_tmp3_ < _tmp4_)) {
				break;
			}
			_tmp5_ = _container_list;
			_tmp6_ = gee_abstract_list_get ((GeeAbstractList*) _tmp5_, _container_index);
			container = (RygelMediaObject*) _tmp6_;
			_tmp8_ = container;
			_tmp9_ = self->priv->all_container;
			if (_tmp8_ == G_TYPE_CHECK_INSTANCE_CAST (_tmp9_, RYGEL_TYPE_MEDIA_OBJECT, RygelMediaObject)) {
				_tmp7_ = TRUE;
			} else {
				RygelMediaObject* _tmp10_;
				_tmp10_ = container;
				_tmp7_ = _tmp10_ == NULL;
			}
			if (_tmp7_) {
				_g_object_unref0 (container);
				continue;
			}
			_tmp11_ = container;
			if (RYGEL_TRACKER_IS_METADATA_VALUES (_tmp11_)) {
				RygelMediaObject* _tmp12_;
				_tmp12_ = container;
				rygel_tracker_metadata_container_fetch_metadata_values ((RygelTrackerMetadataContainer*) G_TYPE_CHECK_INSTANCE_CAST (_tmp12_, RYGEL_TRACKER_TYPE_METADATA_VALUES, RygelTrackerMetadataValues), NULL, NULL);
			} else {
				RygelMediaObject* _tmp13_;
				_tmp13_ = container;
				if (RYGEL_TRACKER_IS_SEARCH_CONTAINER (_tmp13_)) {
					RygelMediaObject* _tmp14_;
					_tmp14_ = container;
					rygel_tracker_search_container_get_children_count (G_TYPE_CHECK_INSTANCE_CAST (_tmp14_, RYGEL_TRACKER_TYPE_SEARCH_CONTAINER, RygelTrackerSearchContainer), NULL, NULL);
				}
			}
			_g_object_unref0 (container);
		}
	}
}

static void
rygel_tracker_category_container_on_all_container_updated (RygelTrackerCategoryContainer* self,
                                                           RygelMediaContainer* other)
{
	RygelTrackerCategoryAllContainer* _tmp0_;
	RygelMediaObjects* _tmp1_;
	RygelMediaObjects* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (other != NULL);
	_tmp0_ = self->priv->all_container;
	if (other != G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, RYGEL_TYPE_MEDIA_CONTAINER, RygelMediaContainer)) {
		return;
	}
	_tmp1_ = rygel_simple_container_get_all_children ((RygelSimpleContainer*) self);
	_tmp2_ = _tmp1_;
	rygel_tracker_category_container_trigger_child_update (self, _tmp2_);
	_g_object_unref0 (_tmp2_);
}

static void
rygel_tracker_category_container_class_init (RygelTrackerCategoryContainerClass * klass,
                                             gpointer klass_data)
{
	rygel_tracker_category_container_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelTrackerCategoryContainer_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_tracker_category_container_finalize;
}

static void
rygel_tracker_category_container_instance_init (RygelTrackerCategoryContainer * self,
                                                gpointer klass)
{
	self->priv = rygel_tracker_category_container_get_instance_private (self);
}

static void
rygel_tracker_category_container_finalize (GObject * obj)
{
	RygelTrackerCategoryContainer * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_CATEGORY_CONTAINER, RygelTrackerCategoryContainer);
	_rygel_tracker_item_factory_unref0 (self->item_factory);
	_g_object_unref0 (self->priv->all_container);
	G_OBJECT_CLASS (rygel_tracker_category_container_parent_class)->finalize (obj);
}

/**
 * Container listing content hierarchy for a specific category.
 */
static GType
rygel_tracker_category_container_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerCategoryContainerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_category_container_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerCategoryContainer), 0, (GInstanceInitFunc) rygel_tracker_category_container_instance_init, NULL };
	GType rygel_tracker_category_container_type_id;
	rygel_tracker_category_container_type_id = g_type_register_static (RYGEL_TYPE_SIMPLE_CONTAINER, "RygelTrackerCategoryContainer", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
	RygelTrackerCategoryContainer_private_offset = g_type_add_instance_private (rygel_tracker_category_container_type_id, sizeof (RygelTrackerCategoryContainerPrivate));
	return rygel_tracker_category_container_type_id;
}

GType
rygel_tracker_category_container_get_type (void)
{
	static volatile gsize rygel_tracker_category_container_type_id__once = 0;
	if (g_once_init_enter (&rygel_tracker_category_container_type_id__once)) {
		GType rygel_tracker_category_container_type_id;
		rygel_tracker_category_container_type_id = rygel_tracker_category_container_get_type_once ();
		g_once_init_leave (&rygel_tracker_category_container_type_id__once, rygel_tracker_category_container_type_id);
	}
	return rygel_tracker_category_container_type_id__once;
}

