1. Merge latest code from tizen 2.4
[platform/core/api/notification.git] / src / notification_list.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23
24 #include <tizen.h>
25
26 #include <notification.h>
27 #include <notification_list.h>
28 #include <notification_noti.h>
29 #include <notification_debug.h>
30 #include <notification_private.h>
31
32 struct _notification_list {
33         notification_list_h prev;
34         notification_list_h next;
35
36         notification_h noti;
37 };
38
39 notification_list_h _notification_list_create(void)
40 {
41         notification_list_h list = NULL;
42
43         list = (notification_list_h) malloc(sizeof(struct _notification_list));
44         if (list == NULL) {
45                 NOTIFICATION_ERR("NO MEMORY");
46                 return NULL;
47         }
48
49         list->prev = NULL;
50         list->next = NULL;
51
52         list->noti = NULL;
53
54         return list;
55 }
56
57 EXPORT_API notification_list_h notification_list_get_head(notification_list_h list)
58 {
59         notification_list_h cur_list = NULL;
60
61         if (list == NULL) {
62                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
63                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
64                 return NULL;
65         }
66
67         cur_list = list;
68
69         while (cur_list->prev != NULL) {
70                 cur_list = cur_list->prev;
71         }
72
73         set_last_result(NOTIFICATION_ERROR_NONE);
74         return cur_list;
75 }
76
77 EXPORT_API notification_list_h notification_list_get_tail(notification_list_h list)
78 {
79         notification_list_h cur_list = NULL;
80
81         if (list == NULL) {
82                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
83                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
84                 return NULL;
85         }
86
87         cur_list = list;
88
89         while (cur_list->next != NULL) {
90                 cur_list = cur_list->next;
91         }
92
93         set_last_result(NOTIFICATION_ERROR_NONE);
94         return cur_list;
95 }
96
97 EXPORT_API notification_list_h notification_list_get_prev(notification_list_h list)
98 {
99         notification_list_h cur_list = NULL;
100
101         if (list == NULL) {
102                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
103                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
104                 return NULL;
105         }
106
107         cur_list = list;
108
109         set_last_result(NOTIFICATION_ERROR_NONE);
110         return cur_list->prev;
111 }
112
113 EXPORT_API notification_list_h notification_list_get_next(notification_list_h list)
114 {
115         notification_list_h cur_list = NULL;
116
117         if (list == NULL) {
118                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
119                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
120                 return NULL;
121         }
122
123         cur_list = list;
124
125         set_last_result(NOTIFICATION_ERROR_NONE);
126         return cur_list->next;
127 }
128
129 EXPORT_API notification_h notification_list_get_data(notification_list_h list)
130 {
131         notification_list_h cur_list = NULL;
132
133         if (list == NULL) {
134                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
135                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
136                 return NULL;
137         }
138
139         cur_list = list;
140
141         set_last_result(NOTIFICATION_ERROR_NONE);
142         return cur_list->noti;
143 }
144
145 EXPORT_API notification_list_h notification_list_append(notification_list_h list,
146                                                         notification_h noti)
147 {
148         notification_list_h new_list = NULL;
149         notification_list_h cur_list = NULL;
150
151         if (noti == NULL) {
152                 NOTIFICATION_ERR("INVALID DATA : data == NULL");
153                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
154                 return NULL;
155         }
156
157         if (list != NULL) {
158                 cur_list = notification_list_get_tail(list);
159
160                 new_list = _notification_list_create();
161                 if (new_list == NULL) {
162                         NOTIFICATION_ERR("NO MEMORY");
163                         set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
164                         return NULL;
165                 }
166
167                 cur_list->next = new_list;
168                 new_list->prev = cur_list;
169
170                 new_list->noti = noti;
171         } else {
172                 cur_list = _notification_list_create();
173                 if (cur_list == NULL) {
174                         NOTIFICATION_ERR("NO MEMORY");
175                         set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
176                         return NULL;
177                 }
178
179                 new_list = cur_list;
180                 new_list->noti = noti;
181         }
182
183         set_last_result(NOTIFICATION_ERROR_NONE);
184         return new_list;
185 }
186
187 EXPORT_API notification_list_h notification_list_remove(notification_list_h list,
188                                                         notification_h noti)
189 {
190         notification_list_h cur_list = NULL;
191         notification_list_h prev_list = NULL;
192         notification_list_h next_list = NULL;
193
194         cur_list = notification_list_get_head(list);
195         while (cur_list != NULL) {
196                 if (cur_list->noti == noti) {
197                         //remove
198                         prev_list = cur_list->prev;
199                         next_list = cur_list->next;
200
201                         if (next_list != NULL) {
202                                 if (prev_list != NULL) {
203                                         prev_list->next = next_list;
204                                         next_list->prev = prev_list;
205                                 } else {
206                                         next_list->prev = NULL;
207                                 }
208                         } else {
209                                 if (prev_list != NULL) {
210                                         prev_list->next = NULL;
211                                 }
212                         }
213
214                         free(cur_list);
215                         break;
216                 }
217
218                 cur_list = cur_list->next;
219         }
220
221         if (prev_list != NULL) {
222                 return notification_list_get_head(prev_list);
223         } else if (next_list != NULL) {
224                 return next_list;
225         }
226
227         return NULL;
228 }
229
230 EXPORT_API int notification_get_list(notification_type_e type,
231                                                       int count,
232                                                       notification_list_h *list)
233 {
234         notification_list_h get_list = NULL;
235         int ret = 0;
236
237         if (list == NULL) {
238                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
239         }
240
241         ret = notification_noti_get_grouping_list(type, count, &get_list);
242         if (ret != NOTIFICATION_ERROR_NONE) {
243                 return ret;
244         }
245
246         *list = get_list;
247
248         return NOTIFICATION_ERROR_NONE;
249 }
250
251 EXPORT_API int notification_get_detail_list(const char *pkgname,
252                                                              int group_id,
253                                                              int priv_id,
254                                                              int count,
255                                                              notification_list_h *list)
256 {
257         notification_list_h get_list = NULL;
258         int ret = 0;
259
260         if (list == NULL) {
261                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
262         }
263
264         ret =
265             notification_noti_get_detail_list(pkgname, group_id, priv_id, count,
266                                               &get_list);
267         if (ret != NOTIFICATION_ERROR_NONE) {
268                 return ret;
269         }
270
271         *list = get_list;
272
273         return NOTIFICATION_ERROR_NONE;
274 }
275
276 EXPORT_API int notification_free_list(notification_list_h list)
277 {
278         notification_list_h cur_list = NULL;
279         notification_h noti = NULL;
280
281         if (list == NULL) {
282                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
283         }
284
285         cur_list = notification_list_get_head(list);
286
287         while (cur_list != NULL) {
288                 noti = notification_list_get_data(cur_list);
289                 cur_list = notification_list_remove(cur_list, noti);
290
291                 notification_free(noti);
292         }
293
294         return NOTIFICATION_ERROR_NONE;
295 }