4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
26 API int calendar_list_create(calendar_list_h* out_list)
29 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
31 l = calloc(1, sizeof(cal_list_s));
32 RETVM_IF(NULL == l, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
38 *out_list = (calendar_list_h)l;
39 return CALENDAR_ERROR_NONE;
42 API int calendar_list_get_count(calendar_list_h list, int *count)
46 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
47 RETV_IF(NULL == count, CALENDAR_ERROR_INVALID_PARAMETER);
49 l = (cal_list_s *)list;
51 return CALENDAR_ERROR_NONE;
54 API int calendar_list_add(calendar_list_h list, calendar_record_h record)
58 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
59 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
61 l = (cal_list_s *)list;
64 l->record = g_list_append(l->record, record);
65 return CALENDAR_ERROR_NONE;
68 API int calendar_list_remove(calendar_list_h list, calendar_record_h record)
72 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
73 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
75 l = (cal_list_s *)list;
77 GList *cursor = l->record;
79 if (cursor->data == record) {
80 l->cursor = cursor->next;
81 l->record = g_list_remove(l->record, cursor->data);
83 return CALENDAR_ERROR_NONE;
85 cursor = cursor->next;
87 return CALENDAR_ERROR_NO_DATA;
90 API int calendar_list_get_current_record_p(calendar_list_h list, calendar_record_h* record)
94 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
95 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
97 l = (cal_list_s *)list;
98 if (NULL == l->cursor) {
100 return CALENDAR_ERROR_NO_DATA;
103 *record = l->cursor->data;
104 return CALENDAR_ERROR_NONE;
107 API int calendar_list_prev(calendar_list_h list)
109 cal_list_s *l = NULL;
111 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
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;
118 return CALENDAR_ERROR_NONE;
121 API int calendar_list_next(calendar_list_h list)
123 cal_list_s *l = NULL;
125 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
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;
132 return CALENDAR_ERROR_NONE;
135 API int calendar_list_first(calendar_list_h list)
137 cal_list_s *l = NULL;
139 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
141 l = (cal_list_s *)list;
142 l->cursor = g_list_first(l->record);
144 return CALENDAR_ERROR_NONE;
147 API int calendar_list_last(calendar_list_h list)
149 cal_list_s *l = NULL;
151 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
153 l = (cal_list_s *)list;
154 l->cursor = g_list_last(l->record);
156 return CALENDAR_ERROR_NONE;
159 API int calendar_list_destroy(calendar_list_h list, bool delete_record)
162 cal_list_s *l = NULL;
164 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
166 l = (cal_list_s *)list;
168 if (delete_record == true) {
173 calendar_record_destroy((calendar_record_h)(cursor->data), true);
174 cursor = cursor->next;
178 g_list_free(l->record);
182 return CALENDAR_ERROR_NONE;
185 int cal_list_clone(calendar_list_h list, calendar_list_h *out_list)
187 int ret = CALENDAR_ERROR_NONE;
188 int count = 0, i = 0;
189 calendar_list_h l = NULL;
191 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
192 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
194 ret = calendar_list_get_count(list, &count);
195 if (CALENDAR_ERROR_NONE != ret)
198 ret = calendar_list_first(list);
199 if (CALENDAR_ERROR_NONE != ret)
202 ret = calendar_list_create(&l);
203 if (CALENDAR_ERROR_NONE != ret)
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);
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);
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);
235 calendar_list_next(list);
239 return CALENDAR_ERROR_NONE;
242 int cal_list_get_nth_record_p(cal_list_s *list_s, int index, calendar_record_h *record)
244 RETV_IF(index < 0, CALENDAR_ERROR_INVALID_PARAMETER);
245 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
249 RETV_IF(NULL == list_s, CALENDAR_ERROR_INVALID_PARAMETER);
251 if (index < list_s->count) {
252 *record = g_list_nth_data(list_s->record, index);
253 return CALENDAR_ERROR_NONE;
256 /* LCOV_EXCL_START */
257 ERR("Check count(%d) < index(%d)", list_s->count, index);
258 return CALENDAR_ERROR_NO_DATA;
262 int cal_list_clear(cal_list_s *list_s)
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);
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);