Use g_slist_find_custom to search watchlist. Add method to remove all
watch items without freeing ofono_watchlist itself.
---
 src/ofono.h |    1 +
 src/watch.c |   75 ++++++++++++++++++++++++++++++----------------------------
 2 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/src/ofono.h b/src/ofono.h
index 7b13cce..f93760e 100644
--- a/src/ofono.h
+++ b/src/ofono.h
@@ -79,6 +79,7 @@ unsigned int __ofono_watchlist_add_item(struct 
ofono_watchlist *watchlist,
                                        struct ofono_watchlist_item *item);
 gboolean __ofono_watchlist_remove_item(struct ofono_watchlist *watchlist,
                                        unsigned int id);
+void __ofono_watchlist_remove_all(struct ofono_watchlist *watchlist);
 void __ofono_watchlist_free(struct ofono_watchlist *watchlist);
 
 #include <ofono/plugin.h>
diff --git a/src/watch.c b/src/watch.c
index f93b68c..0388669 100644
--- a/src/watch.c
+++ b/src/watch.c
@@ -46,59 +46,62 @@ unsigned int __ofono_watchlist_add_item(struct 
ofono_watchlist *watchlist,
        return item->id;
 }
 
-gboolean __ofono_watchlist_remove_item(struct ofono_watchlist *watchlist,
-                                       unsigned int id)
+static gint watchlist_item_compare_by_id(gconstpointer a, gconstpointer b)
 {
-       struct ofono_watchlist_item *item;
-       GSList *p;
-       GSList *c;
-
-       p = NULL;
-       c = watchlist->items;
-
-       while (c) {
-               item = c->data;
+       const struct ofono_watchlist_item *item = a;
+       unsigned int id = GPOINTER_TO_UINT(b);
 
-               if (item->id != id) {
-                       p = c;
-                       c = c->next;
-                       continue;
-               }
+       if (item->id < id)
+               return -1;
 
-               if (p)
-                       p->next = c->next;
-               else
-                       watchlist->items = c->next;
+       if (item->id > id)
+               return 1;
 
-               if (item->destroy)
-                       item->destroy(item->notify_data);
+       return 0;
+}
 
-               if (watchlist->destroy)
-                       watchlist->destroy(item);
-               g_slist_free_1(c);
+static void watchlist_remove_item(void *data, void *user_data)
+{
+       struct ofono_watchlist_item *item = data;
+       struct ofono_watchlist *watchlist = user_data;
 
-               return TRUE;
-       }
+       if (item->destroy)
+               item->destroy(item->notify_data);
 
-       return FALSE;
+       if (watchlist->destroy)
+               watchlist->destroy(item);
 }
 
-void __ofono_watchlist_free(struct ofono_watchlist *watchlist)
+gboolean __ofono_watchlist_remove_item(struct ofono_watchlist *watchlist,
+                                       unsigned int id)
 {
        struct ofono_watchlist_item *item;
        GSList *l;
 
-       for (l = watchlist->items; l; l = l->next) {
-               item = l->data;
+       l = g_slist_find_custom(watchlist->items, GUINT_TO_POINTER(id),
+                                               watchlist_item_compare_by_id);
+       if (l == NULL)
+               return FALSE;
+
+       item = l->data;
 
-               if (item->destroy)
-                       item->destroy(item->notify_data);
+       watchlist->items = g_slist_remove(watchlist->items, item);
 
-               if (watchlist->destroy)
-                       watchlist->destroy(item);
-       }
+       watchlist_remove_item(item, watchlist);
 
+       return TRUE;
+}
+
+void __ofono_watchlist_remove_all(struct ofono_watchlist *watchlist)
+{
+       g_slist_foreach(watchlist->items, watchlist_remove_item, watchlist);
        g_slist_free(watchlist->items);
        watchlist->items = NULL;
+       watchlist->next_id = 0;
+}
+
+void __ofono_watchlist_free(struct ofono_watchlist *watchlist)
+{
+       __ofono_watchlist_remove_all(watchlist);
        g_free(watchlist);
 }
-- 
1.6.3.3

_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono

Reply via email to