add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / common / cal_list.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_list.h"
25
26 API int calendar_list_create(calendar_list_h* out_list)
27 {
28         cal_list_s *l = NULL;
29         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
30
31         l = calloc(1, sizeof(cal_list_s));
32         RETVM_IF(NULL == l, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
33
34         l->count = 0;
35         l->record = NULL;
36         l->cursor = NULL;
37
38         *out_list = (calendar_list_h)l;
39         return CALENDAR_ERROR_NONE;
40 }
41
42 API int calendar_list_get_count(calendar_list_h list, int *count)
43 {
44         cal_list_s *l = NULL;
45
46         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
47         RETV_IF(NULL == count, CALENDAR_ERROR_INVALID_PARAMETER);
48
49         l = (cal_list_s *)list;
50         *count = l->count;
51         return CALENDAR_ERROR_NONE;
52 }
53
54 API int calendar_list_add(calendar_list_h list, calendar_record_h record)
55 {
56         cal_list_s *l = NULL;
57
58         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
59         RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
60
61         l = (cal_list_s *)list;
62
63         l->count++;
64         l->record = g_list_append(l->record, record);
65         return CALENDAR_ERROR_NONE;
66 }
67
68 API int calendar_list_remove(calendar_list_h list, calendar_record_h record)
69 {
70         cal_list_s *l = NULL;
71
72         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
73         RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
74
75         l = (cal_list_s *)list;
76
77         GList *cursor = l->record;
78         while (cursor) {
79                 if (cursor->data == record) {
80                         l->cursor = cursor->next;
81                         l->record = g_list_remove(l->record, cursor->data);
82                         l->count--;
83                         return CALENDAR_ERROR_NONE;
84                 }
85                 cursor = cursor->next;
86         }
87         return CALENDAR_ERROR_NO_DATA;
88 }
89
90 API int calendar_list_get_current_record_p(calendar_list_h list, calendar_record_h* record)
91 {
92         cal_list_s *l = NULL;
93
94         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
95         RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
96
97         l = (cal_list_s *)list;
98         if (NULL == l->cursor) {
99                 *record = NULL;
100                 return CALENDAR_ERROR_NO_DATA;
101         }
102
103         *record = l->cursor->data;
104         return CALENDAR_ERROR_NONE;
105 }
106
107 API int calendar_list_prev(calendar_list_h list)
108 {
109         cal_list_s *l = NULL;
110
111         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
112
113         l = (cal_list_s *)list;
114         l->cursor = g_list_previous(l->cursor);
115         if (NULL == l->cursor)
116                 return CALENDAR_ERROR_NO_DATA;
117
118         return CALENDAR_ERROR_NONE;
119 }
120
121 API int calendar_list_next(calendar_list_h list)
122 {
123         cal_list_s *l = NULL;
124
125         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
126
127         l = (cal_list_s *)list;
128         l->cursor = g_list_next(l->cursor);
129         if (NULL == l->cursor)
130                 return CALENDAR_ERROR_NO_DATA;
131
132         return CALENDAR_ERROR_NONE;
133 }
134
135 API int calendar_list_first(calendar_list_h list)
136 {
137         cal_list_s *l = NULL;
138
139         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
140
141         l = (cal_list_s *)list;
142         l->cursor = g_list_first(l->record);
143
144         return CALENDAR_ERROR_NONE;
145 }
146
147 API int calendar_list_last(calendar_list_h list)
148 {
149         cal_list_s *l = NULL;
150
151         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
152
153         l = (cal_list_s *)list;
154         l->cursor = g_list_last(l->record);
155
156         return CALENDAR_ERROR_NONE;
157 }
158
159 API int calendar_list_destroy(calendar_list_h list, bool delete_record)
160 {
161         GList *cursor;
162         cal_list_s *l = NULL;
163
164         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
165
166         l = (cal_list_s *)list;
167
168         if (delete_record == true) {
169                 cursor = l->record;
170
171                 while (cursor) {
172                         if (cursor->data)
173                                 calendar_record_destroy((calendar_record_h)(cursor->data), true);
174                         cursor = cursor->next;
175                 }
176         }
177         if (l->record)
178                 g_list_free(l->record);
179
180         free(l);
181
182         return CALENDAR_ERROR_NONE;
183 }
184
185 int cal_list_clone(calendar_list_h list, calendar_list_h *out_list)
186 {
187         int ret = CALENDAR_ERROR_NONE;
188         int count = 0, i = 0;
189         calendar_list_h l = NULL;
190
191         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
192         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
193
194         ret = calendar_list_get_count(list, &count);
195         if (CALENDAR_ERROR_NONE != ret)
196                 return ret;
197
198         ret = calendar_list_first(list);
199         if (CALENDAR_ERROR_NONE != ret)
200                 return ret;
201
202         ret = calendar_list_create(&l);
203         if (CALENDAR_ERROR_NONE != ret)
204                 return ret;
205
206         for (i = 0; i < count; i++) {
207                 calendar_record_h record = NULL;
208                 calendar_record_h clone_record = NULL;
209                 ret = calendar_list_get_current_record_p(list, &record);
210                 if (CALENDAR_ERROR_NONE != ret) {
211                         /* LCOV_EXCL_START */
212                         ERR("calendar_list_get_current_record_p() Fail(%d)", ret);
213                         calendar_list_destroy(l, true);
214                         return ret;
215                         /* LCOV_EXCL_STOP */
216                 }
217
218                 ret = calendar_record_clone(record, &clone_record);
219                 if (CALENDAR_ERROR_NONE != ret) {
220                         /* LCOV_EXCL_START */
221                         ERR("calendar_record_clone() Fail(%d)", ret);
222                         calendar_list_destroy(l, true);
223                         return ret;
224                         /* LCOV_EXCL_STOP */
225                 }
226
227                 ret = calendar_list_add(l, clone_record);
228                 if (CALENDAR_ERROR_NONE != ret) {
229                         /* LCOV_EXCL_START */
230                         ERR("calendar_list_add() Fail(%d)", ret);
231                         calendar_list_destroy(l, true);
232                         return ret;
233                         /* LCOV_EXCL_STOP */
234                 }
235                 calendar_list_next(list);
236         }
237
238         *out_list = l;
239         return CALENDAR_ERROR_NONE;
240 }
241
242 int cal_list_get_nth_record_p(cal_list_s *list_s, int index, calendar_record_h *record)
243 {
244         RETV_IF(index < 0, CALENDAR_ERROR_INVALID_PARAMETER);
245         RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
246
247         *record = NULL;
248
249         RETV_IF(NULL == list_s, CALENDAR_ERROR_INVALID_PARAMETER);
250
251         if (index < list_s->count) {
252                 *record = g_list_nth_data(list_s->record, index);
253                 return CALENDAR_ERROR_NONE;
254         }
255
256         /* LCOV_EXCL_START */
257         ERR("Check count(%d) < index(%d)", list_s->count, index);
258         return CALENDAR_ERROR_NO_DATA;
259         /* LCOV_EXCL_STOP */
260 }
261
262 int cal_list_clear(cal_list_s *list_s)
263 {
264         int ret = CALENDAR_ERROR_NONE;
265         calendar_record_h record = NULL;
266         calendar_list_h list = (calendar_list_h)list_s;
267         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
268
269         calendar_list_first(list);
270         while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
271                 ret = calendar_list_remove(list, record);
272                 if (CALENDAR_ERROR_NONE != ret) {
273                         /* LCOV_EXCL_START */
274                         ERR("calendar_list_remove() Fail(%d)", ret);
275                         break;
276                         /* LCOV_EXCL_STOP */
277                 }
278         }
279         return ret;
280 }