Manage LdLibrary children in a GSList.

The previous method of containment, that is using GHashTable,
did not list children in any particular order.

Also names of children were duplicated. One copy has been
stored as a hash table index and another was present in the
child itself.
This commit is contained in:
Přemysl Eric Janouch 2010-10-26 16:18:15 +02:00
parent 9a242786b1
commit 979308aa7f
3 changed files with 75 additions and 19 deletions

View File

@ -29,11 +29,13 @@
/*
* LdLibraryPrivate:
* @script_state: State of the scripting language.
* @lua: State of the scripting language.
* @children: Child objects of the library.
*/
struct _LdLibraryPrivate
{
LdLua *lua;
GSList *children;
};
G_DEFINE_TYPE (LdLibrary, ld_library, G_TYPE_OBJECT);
@ -85,9 +87,7 @@ ld_library_init (LdLibrary *self)
(self, LD_TYPE_LIBRARY, LdLibraryPrivate);
self->priv->lua = ld_lua_new ();
self->categories = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
self->priv->children = NULL;
}
static void
@ -98,7 +98,9 @@ ld_library_finalize (GObject *gobject)
self = LD_LIBRARY (gobject);
g_object_unref (self->priv->lua);
g_hash_table_destroy (self->categories);
g_slist_foreach (self->priv->children, (GFunc) g_object_unref, NULL);
g_slist_free (self->priv->children);
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_library_parent_class)->finalize (gobject);
@ -288,8 +290,7 @@ ld_library_load_cb (const gchar *base, const gchar *filename, gpointer userdata)
cat = load_category (data->self, filename, base);
if (cat)
g_hash_table_insert (data->self->categories,
g_strdup (ld_symbol_category_get_name (cat)), cat);
ld_library_insert_child (data->self, G_OBJECT (cat), -1);
data->changed = TRUE;
return TRUE;
@ -306,9 +307,60 @@ ld_library_clear (LdLibrary *self)
{
g_return_if_fail (LD_IS_LIBRARY (self));
g_hash_table_remove_all (self->categories);
g_slist_foreach (self->priv->children, (GFunc) g_object_unref, NULL);
g_slist_free (self->priv->children);
self->priv->children = NULL;
g_signal_emit (self,
LD_LIBRARY_GET_CLASS (self)->changed_signal, 0);
}
/**
* ld_library_insert_child:
* @self: An #LdLibrary object.
* @child: The child to be inserted.
* @pos: The position at which the child will be inserted.
* Negative values will append to the end of list.
*
* Insert a child into the library.
*/
void
ld_library_insert_child (LdLibrary *self, GObject *child, gint pos)
{
g_return_if_fail (LD_IS_LIBRARY (self));
g_return_if_fail (G_IS_OBJECT (child));
g_object_ref (child);
self->priv->children = g_slist_insert (self->priv->children, child, pos);
}
/**
* ld_library_remove_child:
* @self: An #LdLibrary object.
* @child: The child to be removed.
*
* Removes a child from the library.
*/
void
ld_library_remove_child (LdLibrary *self, GObject *child)
{
g_return_if_fail (LD_IS_LIBRARY (self));
g_return_if_fail (G_IS_OBJECT (child));
g_object_unref (child);
self->priv->children = g_slist_remove (self->priv->children, child);
}
/**
* ld_library_get_children:
* @self: An #LdLibrary object.
*
* Return value: The internal list of children. Do not modify.
*/
const GSList *
ld_library_get_children (LdLibrary *self)
{
g_return_val_if_fail (LD_IS_LIBRARY (self), NULL);
return self->priv->children;
}

View File

@ -42,9 +42,6 @@ struct _LdLibrary
/*< private >*/
GObject parent_instance;
LdLibraryPrivate *priv;
/*< public >*/
GHashTable *categories;
};
struct _LdLibraryClass
@ -62,6 +59,10 @@ LdLibrary *ld_library_new (void);
gboolean ld_library_load (LdLibrary *self, const gchar *directory);
void ld_library_clear (LdLibrary *self);
void ld_library_insert_child (LdLibrary *self, GObject *child, gint pos);
void ld_library_remove_child (LdLibrary *self, GObject *child);
const GSList *ld_library_get_children (LdLibrary *self);
G_END_DECLS

View File

@ -69,7 +69,7 @@ static void
ld_window_main_finalize (GObject *gobject);
static void
cb_load_category (gpointer key, gpointer value, gpointer user_data);
cb_load_category (gpointer data, gpointer user_data);
static void
load_toolbar (LdWindowMain *self);
@ -271,7 +271,7 @@ ld_window_main_finalize (GObject *gobject)
* A hashtable foreach callback for adding categories into the toolbar.
*/
static void
cb_load_category (gpointer key, gpointer value, gpointer user_data)
cb_load_category (gpointer data, gpointer user_data)
{
const gchar *name;
LdSymbolCategory *cat;
@ -280,12 +280,13 @@ cb_load_category (gpointer key, gpointer value, gpointer user_data)
GtkWidget *img;
GtkToolItem *item;
name = key;
cat = value;
g_return_if_fail (LD_IS_WINDOW_MAIN (user_data));
self = user_data;
g_return_if_fail (key != NULL);
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (cat));
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (data));
cat = data;
name = ld_symbol_category_get_name (cat);
pbuf = gdk_pixbuf_new_from_file_at_size
(ld_symbol_category_get_image_path (cat), TOOLBAR_ICON_WIDTH, -1, NULL);
@ -307,12 +308,14 @@ cb_load_category (gpointer key, gpointer value, gpointer user_data)
static void
load_toolbar (LdWindowMain *self)
{
GSList *categories;
/* Clear the toolbar first, if there was already something in it. */
gtk_container_foreach (GTK_CONTAINER (self->priv->toolbar),
(GtkCallback) gtk_widget_destroy, NULL);
g_hash_table_foreach (self->priv->library->categories,
cb_load_category, self);
categories = (GSList *) ld_library_get_children (self->priv->library);
g_slist_foreach (categories, cb_load_category, self);
}
/*