Using gdbus for IPC instead of com-core package
[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 #include <notification_ipc.h>
32
33
34 struct _notification_list {
35         notification_list_h prev;
36         notification_list_h next;
37
38         notification_h noti;
39 };
40
41 notification_list_h _notification_list_create(void)
42 {
43         notification_list_h list = NULL;
44
45         list = (notification_list_h) malloc(sizeof(struct _notification_list));
46         if (list == NULL) {
47                 NOTIFICATION_ERR("NO MEMORY");
48                 return NULL;
49         }
50
51         list->prev = NULL;
52         list->next = NULL;
53
54         list->noti = NULL;
55
56         return list;
57 }
58
59 EXPORT_API notification_list_h notification_list_get_head(notification_list_h list)
60 {
61         notification_list_h cur_list = NULL;
62
63         if (list == NULL) {
64                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
65                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
66                 return NULL;
67         }
68
69         cur_list = list;
70
71         while (cur_list->prev != NULL)
72                 cur_list = cur_list->prev;
73
74         set_last_result(NOTIFICATION_ERROR_NONE);
75         return cur_list;
76 }
77
78 EXPORT_API notification_list_h notification_list_get_tail(notification_list_h list)
79 {
80         notification_list_h cur_list = NULL;
81
82         if (list == NULL) {
83                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
84                 set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
85                 return NULL;
86         }
87
88         cur_list = list;
89
90         while (cur_list->next != NULL)
91                 cur_list = cur_list->next;
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                         free(cur_list);
214                         break;
215                 }
216
217                 cur_list = cur_list->next;
218         }
219
220         if (prev_list != NULL)
221                 return notification_list_get_head(prev_list);
222         else if (next_list != NULL)
223                 return next_list;
224
225         return NULL;
226 }
227
228 EXPORT_API int notification_get_list(notification_type_e type,
229                                                       int count,
230                                                       notification_list_h *list)
231 {
232         notification_list_h get_list = NULL;
233         int ret = 0;
234
235         if (list == NULL)
236                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
237
238         ret = notification_ipc_request_load_noti_grouping_list(type, count, &get_list);
239         if (ret != NOTIFICATION_ERROR_NONE)
240                 return ret;
241
242         if (get_list != NULL)
243                 *list = notification_list_get_head(get_list);
244
245         return NOTIFICATION_ERROR_NONE;
246 }
247
248 EXPORT_API int notification_get_detail_list(const char *pkgname,
249                                                              int group_id,
250                                                              int priv_id,
251                                                              int count,
252                                                              notification_list_h *list)
253 {
254         notification_list_h get_list = NULL;
255         int ret = 0;
256
257         if (list == NULL)
258                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
259
260         ret =
261             notification_ipc_request_load_noti_detail_list(pkgname, group_id, priv_id, count,
262                                               &get_list);
263         if (ret != NOTIFICATION_ERROR_NONE)
264                 return ret;
265
266         if (get_list != NULL)
267                 *list = notification_list_get_head(get_list);
268
269         return NOTIFICATION_ERROR_NONE;
270 }
271
272 EXPORT_API int notification_free_list(notification_list_h list)
273 {
274         notification_list_h cur_list = NULL;
275         notification_h noti = NULL;
276
277         if (list == NULL)
278                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
279
280         cur_list = notification_list_get_head(list);
281
282         while (cur_list != NULL) {
283                 noti = notification_list_get_data(cur_list);
284                 cur_list = notification_list_remove(cur_list, noti);
285
286                 notification_free(noti);
287         }
288
289         return NOTIFICATION_ERROR_NONE;
290 }