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