Upgrade ofono to 1.11 and merge to 2.0alpha
[profile/ivi/ofono.git] / src / watch.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <glib.h>
27 #include "ofono.h"
28
29 struct ofono_watchlist *__ofono_watchlist_new(ofono_destroy_func destroy)
30 {
31         struct ofono_watchlist *watchlist;
32
33         watchlist = g_new0(struct ofono_watchlist, 1);
34         watchlist->destroy = destroy;
35
36         return watchlist;
37 }
38
39 unsigned int __ofono_watchlist_add_item(struct ofono_watchlist *watchlist,
40                                         struct ofono_watchlist_item *item)
41 {
42         item->id = ++watchlist->next_id;
43
44         watchlist->items = g_slist_prepend(watchlist->items, item);
45
46         return item->id;
47 }
48
49 gboolean __ofono_watchlist_remove_item(struct ofono_watchlist *watchlist,
50                                         unsigned int id)
51 {
52         struct ofono_watchlist_item *item;
53         GSList *p;
54         GSList *c;
55
56         p = NULL;
57         c = watchlist->items;
58
59         while (c) {
60                 item = c->data;
61
62                 if (item->id != id) {
63                         p = c;
64                         c = c->next;
65                         continue;
66                 }
67
68                 if (p)
69                         p->next = c->next;
70                 else
71                         watchlist->items = c->next;
72
73                 if (item->destroy)
74                         item->destroy(item->notify_data);
75
76                 if (watchlist->destroy)
77                         watchlist->destroy(item);
78                 g_slist_free_1(c);
79
80                 return TRUE;
81         }
82
83         return FALSE;
84 }
85
86 void __ofono_watchlist_free(struct ofono_watchlist *watchlist)
87 {
88         struct ofono_watchlist_item *item;
89         GSList *l;
90
91         for (l = watchlist->items; l; l = l->next) {
92                 item = l->data;
93
94                 if (item->destroy)
95                         item->destroy(item->notify_data);
96
97                 if (watchlist->destroy)
98                         watchlist->destroy(item);
99         }
100
101         g_slist_free(watchlist->items);
102         watchlist->items = NULL;
103         g_free(watchlist);
104 }