Release notification lib for Tizen2.0 beta(tagging)
[apps/home/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>, Youngsub Ko <ys4610.ko@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 <notification.h>
25 #include <notification_list.h>
26 #include <notification_debug.h>
27 #include <notification_internal.h>
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                 return NULL;
61         }
62
63         cur_list = list;
64
65         while (cur_list->prev != NULL) {
66                 cur_list = cur_list->prev;
67         }
68
69         return cur_list;
70 }
71
72 EXPORT_API notification_list_h notification_list_get_tail(notification_list_h list)
73 {
74         notification_list_h cur_list = NULL;
75
76         if (list == NULL) {
77                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
78                 return NULL;
79         }
80
81         cur_list = list;
82
83         while (cur_list->next != NULL) {
84                 cur_list = cur_list->next;
85         }
86
87         return cur_list;
88 }
89
90 EXPORT_API notification_list_h notification_list_get_prev(notification_list_h list)
91 {
92         notification_list_h cur_list = NULL;
93
94         if (list == NULL) {
95                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
96                 return NULL;
97         }
98
99         cur_list = list;
100
101         return cur_list->prev;
102 }
103
104 EXPORT_API notification_list_h notification_list_get_next(notification_list_h list)
105 {
106         notification_list_h cur_list = NULL;
107
108         if (list == NULL) {
109                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
110                 return NULL;
111         }
112
113         cur_list = list;
114
115         return cur_list->next;
116 }
117
118 EXPORT_API notification_h notification_list_get_data(notification_list_h list)
119 {
120         notification_list_h cur_list = NULL;
121
122         if (list == NULL) {
123                 NOTIFICATION_ERR("INVALID DATA : list == NULL");
124                 return NULL;
125         }
126
127         cur_list = list;
128
129         return cur_list->noti;
130 }
131
132 EXPORT_API notification_list_h notification_list_append(notification_list_h list,
133                                                         notification_h noti)
134 {
135         notification_list_h new_list = NULL;
136         notification_list_h cur_list = NULL;
137
138         if (noti == NULL) {
139                 NOTIFICATION_ERR("INVALID DATA : data == NULL");
140                 return NULL;
141         }
142
143         if (list != NULL) {
144                 cur_list = notification_list_get_tail(list);
145
146                 new_list = _notification_list_create();
147                 if (new_list == NULL) {
148                         NOTIFICATION_ERR("NO MEMORY");
149                         return NULL;
150                 }
151
152                 cur_list->next = new_list;
153                 new_list->prev = cur_list;
154
155                 new_list->noti = noti;
156         } else {
157                 cur_list = _notification_list_create();
158                 if (cur_list == NULL) {
159                         NOTIFICATION_ERR("NO MEMORY");
160                         return NULL;
161                 }
162
163                 new_list = cur_list;
164                 new_list->noti = noti;
165         }
166
167         return new_list;
168 }
169
170 EXPORT_API notification_list_h notification_list_remove(notification_list_h list,
171                                                         notification_h noti)
172 {
173         notification_list_h cur_list = NULL;
174         notification_list_h prev_list = NULL;
175         notification_list_h next_list = NULL;
176
177         cur_list = notification_list_get_head(list);
178         while (cur_list != NULL) {
179                 if (cur_list->noti == noti) {
180                         //remove
181                         prev_list = cur_list->prev;
182                         next_list = cur_list->next;
183
184                         if (next_list != NULL) {
185                                 if (prev_list != NULL) {
186                                         prev_list->next = next_list;
187                                         next_list->prev = prev_list;
188                                 } else {
189                                         next_list->prev = NULL;
190                                 }
191                         } else {
192                                 if (prev_list != NULL) {
193                                         prev_list->next = NULL;
194                                 }
195                         }
196
197                         free(cur_list);
198                         break;
199                 }
200
201                 cur_list = cur_list->next;
202         }
203
204         if (prev_list != NULL) {
205                 return notification_list_get_head(prev_list);
206         } else if (next_list != NULL) {
207                 return next_list;
208         }
209
210         return NULL;
211 }