diff options
author | Ted Gould <ted@canonical.com> | 2009-05-13 10:45:32 -0500 |
---|---|---|
committer | Ted Gould <ted@canonical.com> | 2009-05-13 10:45:32 -0500 |
commit | d8c84214190fc0630d28a922627e686622b46d35 (patch) | |
tree | 35eb5af8ccdadde392583e58b6d4be89fed108cf /libdbusmenu-glib | |
parent | 81f7009789863c2666c11474ec6ea0d6d875972d (diff) | |
download | libdbusmenu-d8c84214190fc0630d28a922627e686622b46d35.tar.gz libdbusmenu-d8c84214190fc0630d28a922627e686622b46d35.tar.bz2 libdbusmenu-d8c84214190fc0630d28a922627e686622b46d35.zip |
Add in a function to search the tree for an ID. This might need to be optimized later, I'm not sure how common this operation will be.
Diffstat (limited to 'libdbusmenu-glib')
-rw-r--r-- | libdbusmenu-glib/menuitem.c | 46 | ||||
-rw-r--r-- | libdbusmenu-glib/menuitem.h | 1 |
2 files changed, 47 insertions, 0 deletions
diff --git a/libdbusmenu-glib/menuitem.c b/libdbusmenu-glib/menuitem.c index 2563eb2..e017721 100644 --- a/libdbusmenu-glib/menuitem.c +++ b/libdbusmenu-glib/menuitem.c @@ -469,6 +469,52 @@ dbusmenu_menuitem_child_find (DbusmenuMenuitem * mi, guint id) return NULL; } +typedef struct { + DbusmenuMenuitem * mi; + guint id; +} find_id_t; + +/* Basically the heart of the find_id that matches the + API of GFunc. Unfortunately, this goes through all the + children, but it rejects them quickly. */ +static void +find_id_helper (gpointer in_mi, gpointer in_find_id) +{ + DbusmenuMenuitem * mi = (DbusmenuMenuitem *)in_mi; + find_id_t * find_id = (find_id_t *)in_find_id; + + if (find_id->mi != NULL) return; + if (find_id->id == dbusmenu_menuitem_get_id(mi)) { + find_id->mi = mi; + return; + } + + g_list_foreach(dbusmenu_menuitem_get_children(mi), find_id_helper, in_find_id); + return; +} + +/** + dbusmenu_menuitem_find_id: + @mi: #DbusmenuMenuitem at the top of the tree to search + @id: ID of the #DbusmenuMenuitem to search for + + This function searchs the whole tree of children that + are attached to @mi. This could be quite a few nodes, all + the way down the tree. It is a depth first search. + + Return value: The #DbusmenuMenuitem with the ID of @id + or #NULL if there isn't such a menu item in the tree + represented by @mi. +*/ +DbusmenuMenuitem * +dbusmenu_menuitem_find_id (DbusmenuMenuitem * mi, guint id) +{ + g_return_val_if_fail(DBUSMENU_IS_MENUITEM(mi), NULL); + find_id_t find_id = {mi: NULL, id: id}; + find_id_helper(mi, &find_id); + return find_id.mi; +} + /** dbusmenu_menuitem_property_set: @mi: The #DbusmenuMenuitem to set the property on. diff --git a/libdbusmenu-glib/menuitem.h b/libdbusmenu-glib/menuitem.h index 3ba886a..e2786cc 100644 --- a/libdbusmenu-glib/menuitem.h +++ b/libdbusmenu-glib/menuitem.h @@ -110,6 +110,7 @@ gboolean dbusmenu_menuitem_child_append (DbusmenuMenuitem * mi, DbusmenuMenuitem gboolean dbusmenu_menuitem_child_delete (DbusmenuMenuitem * mi, DbusmenuMenuitem * child); gboolean dbusmenu_menuitem_child_add_position (DbusmenuMenuitem * mi, DbusmenuMenuitem * child, guint position); DbusmenuMenuitem * dbusmenu_menuitem_child_find (DbusmenuMenuitem * mi, guint id); +DbusmenuMenuitem * dbusmenu_menuitem_find_id (DbusmenuMenuitem * mi, guint id); gboolean dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, const gchar * value); const gchar * dbusmenu_menuitem_property_get (DbusmenuMenuitem * mi, const gchar * property); |