/*
 * Copyright (C) 2012 - Juan Ferrer Toribio
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "vn-batch.h"

/**
 * SECTION: vn-batch
 * @Short_description: a group of objects
 * @Title: VnBatch
 *
 * A group of GObjects.
 */

static void vn_batch_buildable_interface_init (GtkBuildableIface * iface);

G_DEFINE_TYPE_WITH_CODE (VnBatch, vn_batch, G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
		vn_batch_buildable_interface_init)
);

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Methods

/**
 * vn_batch_add:
 * @obj: a #VnBatch
 * @child: a #GObject
 * 
 * Adds a child identified whith @id to the group.
 **/
void vn_batch_add (VnBatch * obj, GObject * child)
{
	g_return_if_fail (VN_IS_BATCH (obj));
	g_return_if_fail (G_IS_OBJECT (child));

	obj->objects = g_list_prepend (obj->objects, child);
	obj->count++;
}

/**
 * vn_batch_remove:
 * @obj: a #VnBatch
 * @child: a #GObject contained by @obj
 * 
 * Removes a child from the group.
 **/
void vn_batch_remove (VnBatch * obj, GObject * child)
{
	g_return_if_fail (VN_IS_BATCH (obj));

	obj->objects = g_list_remove (obj->objects, child);
	obj->count--;
}

/**
 * vn_batch_get_objects_list:
 * @obj: a #VnBatch
 * 
 * Returns all the #GObject<!-- -->s in @obj, copying the list. This method is
 * mainly for internal use, use vn_batch_get_objects() instead if you don't need
 * to do further use of the list of objects. 
 * 
 * Return value: (transfer container) (element-type GObject): a #GList with all
 * the objects, that must be freed with #g_list_free
 **/
GList * vn_batch_get_objects_list (VnBatch * obj)
{
	g_return_val_if_fail (VN_IS_BATCH (obj), NULL);

	return g_list_copy (obj->objects);
}

/**
 * vn_batch_get_objects:
 * @obj: a #VnBatch
 * 
 * Returns all the #GObject<!-- -->s in @obj.
 * 
 * Return value: (transfer none) (element-type GObject): a #GList with all
 * the objects, that must not be freed
 **/
const GList * vn_batch_get_objects (VnBatch * obj)
{
	g_return_val_if_fail (VN_IS_BATCH (obj), NULL);

	return obj->objects;
}

/**
 * vn_batch_get_length:
 * @obj: a #VnBatch
 * 
 * Returns the number of childs currently contained in @obj.
 * 
 * Return value: the number of childs
 **/
guint vn_batch_get_length (VnBatch * obj)
{
	g_return_val_if_fail (VN_IS_BATCH (obj), 0);

	return obj->count;
}

static void vn_batch_buildable_add_child (GtkBuildable * obj,
	GtkBuilder * builder, GObject * child, const gchar * type)
{
	vn_batch_add (VN_BATCH (obj), child);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class

static void vn_batch_init (VnBatch * obj)
{
	obj->objects = NULL;
	obj->count = 0;
}

static void vn_batch_finalize (VnBatch * obj)
{
	GObjectClass * parent = g_type_class_peek_parent (VN_BATCH_GET_CLASS (obj));

	g_list_free (obj->objects);

	parent->finalize (G_OBJECT (obj));
}

static void vn_batch_class_init (VnBatchClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) vn_batch_finalize;
}

static void vn_batch_buildable_interface_init (GtkBuildableIface * iface)
{
	iface->add_child = vn_batch_buildable_add_child;
}