2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* 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 "sql-list.h"
|
2014-05-09 11:40:32 +00:00
|
|
|
#include "sql-holder.h"
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION: sql-list
|
|
|
|
* @Short_description: Container for a list of #SqlObject of a certain type
|
|
|
|
* @Title: SqlList
|
|
|
|
**/
|
|
|
|
G_DEFINE_TYPE (SqlList, sql_list, SQL_TYPE_OBJECT);
|
|
|
|
|
|
|
|
SqlList * sql_list_new (GType gtype)
|
|
|
|
{
|
|
|
|
return g_object_new (SQL_TYPE_LIST, "gtype", gtype, NULL);
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlList * sql_list_new_with_items (GType gtype, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
SqlObject * item;
|
|
|
|
SqlList * obj = g_object_new (SQL_TYPE_LIST, "gtype", gtype, NULL);
|
|
|
|
|
|
|
|
va_start (vl, gtype);
|
|
|
|
|
|
|
|
while ((item = va_arg (vl, SqlObject *)))
|
|
|
|
sql_list_add (obj, item);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
|
|
|
static void sql_list_render (SqlList * obj, SqlRender * render)
|
|
|
|
{
|
|
|
|
GList * i;
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
for (i = obj->items.head; i; i = i->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
sql_render_add_object (render, i->data);
|
|
|
|
|
|
|
|
if (i->next)
|
|
|
|
sql_render_append (render, ", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean sql_list_is_ready (SqlList * obj)
|
|
|
|
{
|
|
|
|
GList * i;
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
for (i = obj->items.head; i; i = i->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
if (!sql_object_is_ready (i->data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void sql_list_find_holders (SqlList * obj, SqlBatch * batch)
|
2013-12-16 11:11:47 +00:00
|
|
|
{
|
|
|
|
GList * i;
|
|
|
|
|
|
|
|
for (i = obj->items.head; i; i = i->next)
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_object_get_holders (i->data, batch);
|
2013-12-16 11:11:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
static void sql_list_item_changed (SqlObject * item, SqlObject * obj)
|
|
|
|
{
|
|
|
|
sql_object_changed (obj);
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
static void sql_list_item_unref (SqlList * obj, SqlObject * item)
|
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (item,
|
|
|
|
sql_list_item_changed, obj);
|
|
|
|
g_object_unref (item);
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_add:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
* @item: (allow-none): the #SqlObject
|
|
|
|
*
|
|
|
|
* Adds an item to the end of the list.
|
|
|
|
**/
|
|
|
|
void sql_list_add (SqlList * obj, gpointer item)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_LIST (obj));
|
|
|
|
|
|
|
|
sql_list_insert (obj, item, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_insert:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
* @item: (allow-none): the #SqlObject
|
2014-05-09 11:40:32 +00:00
|
|
|
* @index: the position in which to place the item
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Adds an item to the list. If position is a negative number the element will
|
|
|
|
* be added to the end of the list.
|
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
void sql_list_insert (SqlList * obj, gpointer item, guint index)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_LIST (obj));
|
|
|
|
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, obj->gtype) || SQL_IS_HOLDER (item) || !item);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (!g_queue_find (&obj->items, item))
|
|
|
|
g_signal_connect (item, "changed",
|
|
|
|
G_CALLBACK (sql_list_item_changed), obj);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
if (index > 0)
|
|
|
|
g_queue_push_nth (&obj->items, g_object_ref_sink (item), index);
|
2013-10-11 23:07:35 +00:00
|
|
|
else
|
2013-12-16 11:11:47 +00:00
|
|
|
g_queue_push_tail (&obj->items, g_object_ref_sink (item));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_get:
|
|
|
|
* @obj: the #SqlList
|
2014-05-09 11:40:32 +00:00
|
|
|
* @index: the index of the item
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Gets an item from the list.
|
|
|
|
*
|
|
|
|
* Return value: (transfer none) (allow-none): the selected #SqlObject
|
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
gpointer sql_list_get (SqlList * obj, guint index)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_LIST (obj), NULL);
|
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
return g_queue_peek_nth (&obj->items, index);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_remove:
|
|
|
|
* @obj: the #SqlList
|
2014-05-09 11:40:32 +00:00
|
|
|
* @index: the index of the item to remove
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Removes an item from the list.
|
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
void sql_list_remove (SqlList * obj, guint index)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
SqlObject * item;
|
|
|
|
|
|
|
|
g_return_val_if_fail (SQL_IS_LIST (obj), NULL);
|
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
item = g_queue_pop_nth (&obj->items, index);
|
|
|
|
|
|
|
|
if (!g_queue_find (&obj->items, item))
|
|
|
|
sql_list_item_unref (obj, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_remove_item:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
* @item: the item to remove
|
|
|
|
*
|
|
|
|
* Removes an item from the list.
|
|
|
|
**/
|
|
|
|
void sql_list_remove_item (SqlList * obj, SqlObject * item)
|
|
|
|
{
|
|
|
|
g_return_if_fail (SQL_IS_LIST (obj));
|
|
|
|
g_return_if_fail (SQL_IS_OBJECT (item));
|
|
|
|
|
|
|
|
g_queue_remove_all (&obj->items, item);
|
|
|
|
sql_list_item_unref (obj, item);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_get_items:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
*
|
|
|
|
* Gets all list elements.
|
|
|
|
*
|
2014-06-20 13:46:39 +00:00
|
|
|
* Return value: (transfer none) (allow-none) (element-type Sql.Object): the
|
|
|
|
* list items
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
|
|
|
GList * sql_list_get_items (SqlList * obj)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_LIST (obj), NULL);
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
return obj->items.head;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_get_items_type:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
*
|
|
|
|
* Gets the allowed type for the list elements.
|
|
|
|
*
|
|
|
|
* Return value: the #GType
|
|
|
|
**/
|
|
|
|
GType sql_list_get_items_type (SqlList * obj)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_LIST (obj), 0);
|
|
|
|
|
|
|
|
return obj->gtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sql_list_lenght:
|
|
|
|
* @obj: the #SqlList
|
|
|
|
*
|
|
|
|
* Gets the number of items in the list.
|
|
|
|
*
|
|
|
|
* Return value: the number of items
|
|
|
|
**/
|
|
|
|
guint sql_list_length (SqlList * obj)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (SQL_IS_LIST (obj), 0);
|
|
|
|
|
|
|
|
return obj->items.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_GTYPE = 1
|
|
|
|
,PROP_LENGTH
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sql_list_set_property (SqlList * obj, guint id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_GTYPE:
|
|
|
|
obj->gtype = g_value_get_gtype (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_list_get_property (SqlList * obj, guint id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_GTYPE:
|
|
|
|
g_value_set_gtype (value, obj->gtype);
|
|
|
|
break;
|
|
|
|
case PROP_LENGTH:
|
|
|
|
g_value_set_uint (value, sql_list_length (obj));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
|
|
|
static void sql_list_init (SqlList * obj)
|
|
|
|
{
|
|
|
|
g_queue_init (&obj->items);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_list_finalize (SqlList * obj)
|
|
|
|
{
|
|
|
|
GList * i;
|
|
|
|
|
2013-12-16 11:11:47 +00:00
|
|
|
for (i = obj->items.head; i; i = i->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (i->data,
|
|
|
|
sql_list_item_changed, obj);
|
|
|
|
g_object_unref (i->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_queue_clear (&obj->items);
|
2014-05-27 07:55:01 +00:00
|
|
|
G_OBJECT_CLASS (sql_list_parent_class)->finalize (G_OBJECT (obj));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sql_list_class_init (SqlListClass * k)
|
|
|
|
{
|
|
|
|
GObjectClass * klass = G_OBJECT_CLASS (k);
|
|
|
|
klass->finalize = (GObjectFinalizeFunc) sql_list_finalize;
|
|
|
|
klass->set_property = (GObjectSetPropertyFunc) sql_list_set_property;
|
|
|
|
klass->get_property = (GObjectGetPropertyFunc) sql_list_get_property;
|
|
|
|
SQL_OBJECT_CLASS (klass)->render = (SqlRenderFunc) sql_list_render;
|
|
|
|
SQL_OBJECT_CLASS (klass)->is_ready = (SqlObjectIsReadyFunc) sql_list_is_ready;
|
2013-12-16 11:11:47 +00:00
|
|
|
SQL_OBJECT_CLASS (klass)->find_holders = (SqlObjectFindHoldersFunc) sql_list_find_holders;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_LENGTH,
|
|
|
|
g_param_spec_uint ("length"
|
|
|
|
,_("Length")
|
|
|
|
,_("The length of the list")
|
|
|
|
,0, G_MAXUINT, 0
|
|
|
|
,G_PARAM_READABLE
|
|
|
|
));
|
|
|
|
g_object_class_install_property (klass, PROP_GTYPE,
|
|
|
|
g_param_spec_gtype ("gtype"
|
|
|
|
,_("GType")
|
|
|
|
,_("The allowed type for the items")
|
|
|
|
,SQL_TYPE_OBJECT
|
2014-05-27 07:55:01 +00:00
|
|
|
,G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
}
|