2008-08-25 23:19:38 +00:00
|
|
|
/*
|
2009-03-24 00:55:46 +00:00
|
|
|
Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
|
2008-08-25 23:19:38 +00:00
|
|
|
|
|
|
|
This library 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.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "katze-array.h"
|
|
|
|
|
|
|
|
#include "katze-utils.h"
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:katze-array
|
|
|
|
* @short_description: A type aware item container
|
2008-10-01 02:00:16 +00:00
|
|
|
* @see_also: #KatzeList
|
2008-08-25 23:19:38 +00:00
|
|
|
*
|
|
|
|
* #KatzeArray is a type aware container for items.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _KatzeArray
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
KatzeItem parent_instance;
|
2008-08-25 23:19:38 +00:00
|
|
|
|
|
|
|
GType type;
|
2009-03-24 00:55:46 +00:00
|
|
|
GList* items;
|
2008-08-25 23:19:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _KatzeArrayClass
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
KatzeItemClass parent_class;
|
|
|
|
|
|
|
|
/* Signals */
|
|
|
|
void
|
|
|
|
(*add_item) (KatzeArray* array,
|
|
|
|
gpointer item);
|
|
|
|
void
|
|
|
|
(*remove_item) (KatzeArray* array,
|
|
|
|
gpointer item);
|
|
|
|
void
|
|
|
|
(*clear) (KatzeArray* array);
|
2008-08-25 23:19:38 +00:00
|
|
|
};
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
G_DEFINE_TYPE (KatzeArray, katze_array, KATZE_TYPE_ITEM);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ADD_ITEM,
|
|
|
|
REMOVE_ITEM,
|
|
|
|
CLEAR,
|
|
|
|
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL];
|
2008-08-25 23:19:38 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
katze_array_finalize (GObject* object);
|
|
|
|
|
|
|
|
static void
|
2009-03-24 00:55:46 +00:00
|
|
|
_katze_array_add_item (KatzeArray* array,
|
|
|
|
gpointer item)
|
2008-08-25 23:19:38 +00:00
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
if (katze_array_is_a (array, G_TYPE_OBJECT))
|
2008-10-01 02:00:16 +00:00
|
|
|
{
|
2008-12-24 03:44:02 +00:00
|
|
|
GType type = G_OBJECT_TYPE (item);
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_if_fail (katze_array_is_a (array, type));
|
2008-08-25 23:19:38 +00:00
|
|
|
g_object_ref (item);
|
2008-12-24 03:44:02 +00:00
|
|
|
if (g_type_is_a (type, KATZE_TYPE_ITEM))
|
2009-03-24 00:55:46 +00:00
|
|
|
katze_item_set_parent (item, array);
|
2008-10-01 02:00:16 +00:00
|
|
|
}
|
2009-03-24 00:55:46 +00:00
|
|
|
|
|
|
|
array->items = g_list_append (array->items, item);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-24 00:55:46 +00:00
|
|
|
_katze_array_remove_item (KatzeArray* array,
|
2008-08-25 23:19:38 +00:00
|
|
|
gpointer item)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
array->items = g_list_remove (array->items, item);
|
|
|
|
|
|
|
|
if (katze_array_is_a (array, G_TYPE_OBJECT))
|
2008-10-01 02:00:16 +00:00
|
|
|
{
|
|
|
|
if (KATZE_IS_ITEM (item))
|
|
|
|
katze_item_set_parent (item, NULL);
|
2008-08-25 23:19:38 +00:00
|
|
|
g_object_unref (item);
|
2008-10-01 02:00:16 +00:00
|
|
|
}
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
static void
|
|
|
|
_katze_array_clear (KatzeArray* array)
|
|
|
|
{
|
|
|
|
guint n;
|
|
|
|
guint i;
|
|
|
|
GObject* item;
|
|
|
|
|
|
|
|
n = g_list_length (array->items);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if ((item = g_list_nth_data (array->items, i)))
|
|
|
|
katze_array_remove_item (array, item);
|
|
|
|
}
|
|
|
|
g_list_free (array->items);
|
|
|
|
array->items = NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-25 23:19:38 +00:00
|
|
|
static void
|
|
|
|
katze_array_class_init (KatzeArrayClass* class)
|
|
|
|
{
|
|
|
|
GObjectClass* gobject_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
gobject_class->finalize = katze_array_finalize;
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
signals[ADD_ITEM] = g_signal_new (
|
|
|
|
"add-item",
|
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
|
|
|
|
G_STRUCT_OFFSET (KatzeArrayClass, add_item),
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
g_cclosure_marshal_VOID__POINTER,
|
|
|
|
G_TYPE_NONE, 1,
|
|
|
|
G_TYPE_POINTER);
|
|
|
|
|
|
|
|
signals[REMOVE_ITEM] = g_signal_new (
|
|
|
|
"remove-item",
|
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
|
|
|
|
G_STRUCT_OFFSET (KatzeArrayClass, remove_item),
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
g_cclosure_marshal_VOID__POINTER,
|
|
|
|
G_TYPE_NONE, 1,
|
|
|
|
G_TYPE_POINTER);
|
|
|
|
|
|
|
|
signals[CLEAR] = g_signal_new (
|
|
|
|
"clear",
|
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
|
|
|
|
G_STRUCT_OFFSET (KatzeArrayClass, clear),
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID,
|
|
|
|
G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
gobject_class->finalize = katze_array_finalize;
|
|
|
|
|
|
|
|
class->add_item = _katze_array_add_item;
|
|
|
|
class->remove_item = _katze_array_remove_item;
|
|
|
|
class->clear = _katze_array_clear;
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_array_init (KatzeArray* array)
|
|
|
|
{
|
|
|
|
array->type = G_TYPE_NONE;
|
2009-03-24 00:55:46 +00:00
|
|
|
array->items = NULL;
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_array_finalize (GObject* object)
|
|
|
|
{
|
|
|
|
KatzeArray* array;
|
|
|
|
guint n, i;
|
|
|
|
|
|
|
|
array = KATZE_ARRAY (object);
|
|
|
|
if (katze_array_is_a (array, G_TYPE_OBJECT))
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
n = g_list_length (array->items);
|
2008-08-25 23:19:38 +00:00
|
|
|
for (i = 0; i < n; i++)
|
2009-03-24 00:55:46 +00:00
|
|
|
g_object_unref (g_list_nth_data (array->items, i));
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
g_list_free (array->items);
|
|
|
|
|
2008-08-25 23:19:38 +00:00
|
|
|
G_OBJECT_CLASS (katze_array_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_new:
|
|
|
|
* @type: the expected item type
|
|
|
|
*
|
|
|
|
* Creates a new #KatzeArray for @type items.
|
|
|
|
*
|
|
|
|
* You may only add items of the given type or inherited
|
2008-12-24 03:44:02 +00:00
|
|
|
* from it to this array *if* @type is an #GObject type.
|
2008-08-25 23:19:38 +00:00
|
|
|
* The array will keep a reference on each object until
|
|
|
|
* it is removed from the array.
|
|
|
|
*
|
|
|
|
* Return value: a new #KatzeArray
|
|
|
|
**/
|
|
|
|
KatzeArray*
|
|
|
|
katze_array_new (GType type)
|
|
|
|
{
|
|
|
|
KatzeArray* array = g_object_new (KATZE_TYPE_ARRAY, NULL);
|
|
|
|
array->type = type;
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* katze_array_is_a:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
* @is_a_type: type to compare with
|
|
|
|
*
|
|
|
|
* Checks whether the array is compatible
|
|
|
|
* with items of the specified type.
|
|
|
|
*
|
|
|
|
* Retur value: %TRUE if @array is compatible with @is_a_type
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
katze_array_is_a (KatzeArray* array,
|
|
|
|
GType is_a_type)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), FALSE);
|
|
|
|
|
|
|
|
return g_type_is_a (array->type, is_a_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_add_item:
|
|
|
|
* @array: a #KatzeArray
|
2009-03-24 00:55:46 +00:00
|
|
|
* @item: an item
|
2008-08-25 23:19:38 +00:00
|
|
|
*
|
|
|
|
* Adds an item to the array.
|
2008-10-01 02:00:16 +00:00
|
|
|
*
|
|
|
|
* If @item is a #KatzeItem its parent is set accordingly.
|
2008-08-25 23:19:38 +00:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
katze_array_add_item (KatzeArray* array,
|
|
|
|
gpointer item)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_if_fail (KATZE_IS_ARRAY (array));
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
g_signal_emit (array, signals[ADD_ITEM], 0, item);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-07 18:31:09 +00:00
|
|
|
* katze_array_remove_item:
|
2008-08-25 23:19:38 +00:00
|
|
|
* @array: a #KatzeArray
|
2009-03-24 00:55:46 +00:00
|
|
|
* @item: an item
|
2008-08-25 23:19:38 +00:00
|
|
|
*
|
|
|
|
* Removes an item from the array.
|
2008-10-01 02:00:16 +00:00
|
|
|
*
|
|
|
|
* If @item is a #KatzeItem its parent is unset accordingly.
|
2008-08-25 23:19:38 +00:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
katze_array_remove_item (KatzeArray* array,
|
|
|
|
gpointer item)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_if_fail (KATZE_IS_ARRAY (array));
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
g_signal_emit (array, signals[REMOVE_ITEM], 0, item);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_get_nth_item:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
* @n: an index in the array
|
|
|
|
*
|
|
|
|
* Retrieves the item in @array at the position @n.
|
|
|
|
*
|
|
|
|
* Return value: an item, or %NULL
|
|
|
|
**/
|
|
|
|
gpointer
|
|
|
|
katze_array_get_nth_item (KatzeArray* array,
|
|
|
|
guint n)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
return g_list_nth_data (array->items, n);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_is_empty:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
*
|
2009-03-24 00:55:46 +00:00
|
|
|
* Determines whether @array is empty.
|
2008-08-25 23:19:38 +00:00
|
|
|
*
|
|
|
|
* Return value: an item, or %NULL
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
katze_array_is_empty (KatzeArray* array)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), TRUE);
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
return !g_list_nth_data (array->items, 0);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-25 22:55:54 +00:00
|
|
|
* katze_array_get_item_index:
|
2008-08-25 23:19:38 +00:00
|
|
|
* @array: a #KatzeArray
|
|
|
|
* @item: an item in the array
|
|
|
|
*
|
|
|
|
* Retrieves the index of the item in @array.
|
|
|
|
*
|
|
|
|
* Return value: an item, or -1
|
|
|
|
**/
|
|
|
|
gint
|
|
|
|
katze_array_get_item_index (KatzeArray* array,
|
|
|
|
gpointer item)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), -1);
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
return g_list_index (array->items, item);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_find_token:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
* @token: a token string
|
|
|
|
*
|
|
|
|
* Looks up an item in the array which has the specified token.
|
|
|
|
*
|
|
|
|
* This function will silently fail if the type of the list
|
|
|
|
* is not based on #GObject and only #KatzeItem children
|
|
|
|
* are checked for their token, any other objects are skipped.
|
|
|
|
*
|
|
|
|
* Note that @token is by definition unique to one item.
|
|
|
|
*
|
|
|
|
* Return value: an item, or %NULL
|
|
|
|
**/
|
|
|
|
gpointer
|
|
|
|
katze_array_find_token (KatzeArray* array,
|
|
|
|
const gchar* token)
|
|
|
|
{
|
|
|
|
guint n, i;
|
|
|
|
gpointer item;
|
|
|
|
const gchar* found_token;
|
|
|
|
|
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
|
|
|
|
|
|
|
|
if (!katze_array_is_a (array, G_TYPE_OBJECT))
|
|
|
|
return NULL;
|
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
n = g_list_length (array->items);
|
2008-08-25 23:19:38 +00:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
item = g_list_nth_data (array->items, i);
|
2008-08-25 23:19:38 +00:00
|
|
|
if (!g_type_is_a (G_OBJECT_TYPE (item), KATZE_TYPE_ITEM))
|
|
|
|
continue;
|
|
|
|
found_token = katze_item_get_token ((KatzeItem*)item);
|
2009-03-24 00:55:46 +00:00
|
|
|
if (!g_strcmp0 (found_token, token))
|
2008-08-25 23:19:38 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_get_length:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
*
|
|
|
|
* Retrieves the number of items in @array.
|
|
|
|
*
|
|
|
|
* Return value: the length of the list
|
|
|
|
**/
|
|
|
|
guint
|
|
|
|
katze_array_get_length (KatzeArray* array)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_val_if_fail (KATZE_IS_ARRAY (array), 0);
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
return g_list_length (array->items);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_array_clear:
|
|
|
|
* @array: a #KatzeArray
|
|
|
|
*
|
|
|
|
* Deletes all items currently contained in @array.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
katze_array_clear (KatzeArray* array)
|
|
|
|
{
|
2009-03-24 00:55:46 +00:00
|
|
|
g_return_if_fail (KATZE_IS_ARRAY (array));
|
2008-08-25 23:19:38 +00:00
|
|
|
|
2009-03-24 00:55:46 +00:00
|
|
|
g_signal_emit (array, signals[CLEAR], 0);
|
2008-08-25 23:19:38 +00:00
|
|
|
}
|