Resolved Native API Reference issues for calendar-service
[platform/core/pim/calendar-service.git] / common / cal_record_search.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 #include <stdbool.h>
22 #include <string.h>
23
24 #include "cal_internal.h"
25 #include "cal_typedef.h"
26 #include "cal_view.h"
27 #include "cal_record.h"
28 #include "cal_utils.h"
29
30 static int _cal_record_search_create(calendar_record_h* out_record);
31 static int _cal_record_search_destroy(calendar_record_h record, bool delete_child);
32 static int _cal_record_search_clone(calendar_record_h record, calendar_record_h* out_record);
33 static int _cal_record_search_get_str(calendar_record_h record, unsigned int property_id, char** out_str);
34 static int _cal_record_search_get_str_p(calendar_record_h record, unsigned int property_id, char** out_str);
35 static int _cal_record_search_get_int(calendar_record_h record, unsigned int property_id, int* out_value);
36 static int _cal_record_search_get_double(calendar_record_h record, unsigned int property_id, double* out_value);
37 static int _cal_record_search_get_lli(calendar_record_h record, unsigned int property_id, long long int* out_value);
38 static int _cal_record_search_get_caltime(calendar_record_h record, unsigned int property_id, calendar_time_s* out_value);
39 static int _cal_record_search_set_str(calendar_record_h record, unsigned int property_id, const char* value);
40 static int _cal_record_search_set_int(calendar_record_h record, unsigned int property_id, int value);
41 static int _cal_record_search_set_double(calendar_record_h record, unsigned int property_id, double value);
42 static int _cal_record_search_set_lli(calendar_record_h record, unsigned int property_id, long long int value);
43 static int _cal_record_search_set_caltime(calendar_record_h record, unsigned int property_id, calendar_time_s value);
44
45 cal_record_plugin_cb_s cal_record_search_plugin_cb = {
46         .create = _cal_record_search_create,
47         .destroy = _cal_record_search_destroy,
48         .clone = _cal_record_search_clone,
49         .get_str = _cal_record_search_get_str,
50         .get_str_p = _cal_record_search_get_str_p,
51         .get_int = _cal_record_search_get_int,
52         .get_double = _cal_record_search_get_double,
53         .get_lli = _cal_record_search_get_lli,
54         .get_caltime = _cal_record_search_get_caltime,
55         .set_str = _cal_record_search_set_str,
56         .set_int = _cal_record_search_set_int,
57         .set_double = _cal_record_search_set_double,
58         .set_lli = _cal_record_search_set_lli,
59         .set_caltime = _cal_record_search_set_caltime,
60         .add_child_record = NULL,
61         .remove_child_record = NULL,
62         .get_child_record_count = NULL,
63         .get_child_record_at_p = NULL,
64         .clone_child_record_list = NULL
65 };
66
67 static int _cal_record_search_create(calendar_record_h* out_record)
68 {
69         cal_search_s *temp = NULL;
70         int ret = CALENDAR_ERROR_NONE;
71
72         temp = calloc(1, sizeof(cal_search_s));
73         RETVM_IF(NULL == temp, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
74
75         *out_record = (calendar_record_h)temp;
76
77         return ret;
78 }
79
80 static int _cal_record_search_destroy(calendar_record_h record, bool delete_child)
81 {
82         int ret = CALENDAR_ERROR_NONE;
83         GSList *cursor;
84
85         cal_search_s *temp = (cal_search_s*)(record);
86
87         for (cursor = temp->values; cursor; cursor = cursor->next) {
88                 cal_search_value_s *data = cursor->data;
89                 if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_STR) == true)
90                         CAL_FREE(data->value.s);
91
92                 CAL_FREE(data);
93         }
94
95         g_slist_free(temp->values);
96         CAL_FREE(temp);
97
98         return ret;
99 }
100
101 static int _cal_record_search_clone(calendar_record_h record, calendar_record_h* out_record)
102 {
103         cal_search_s *out_data = NULL;
104         cal_search_s *src_data = NULL;
105         GSList *cursor;
106
107         src_data = (cal_search_s*)(record);
108
109         out_data = calloc(1, sizeof(cal_search_s));
110         RETVM_IF(NULL == out_data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
111
112         CAL_RECORD_COPY_COMMON(&(out_data->common), &(src_data->common));
113
114         for (cursor = src_data->values; cursor; cursor = cursor->next) {
115                 cal_search_value_s *src = cursor->data;
116                 cal_search_value_s *dest = calloc(1, sizeof(cal_search_value_s));
117                 if (NULL == dest) {
118                         CAL_FREE(out_data);
119                         return CALENDAR_ERROR_OUT_OF_MEMORY;
120                 }
121                 dest->property_id = src->property_id;
122                 if (CAL_PROPERTY_CHECK_DATA_TYPE(src->property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
123                         dest->value.s = cal_strdup(src->value.s);
124                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(src->property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
125                         dest->value.i = src->value.i;
126                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(src->property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
127                         dest->value.d = src->value.d;
128                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(src->property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
129                         dest->value.lli = src->value.lli;
130                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(src->property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
131                         dest->value.caltime = src->value.caltime;
132                 } else {
133                         CAL_FREE(dest);
134                         continue;
135                 }
136
137                 out_data->values = g_slist_append(out_data->values, dest);
138         }
139
140         *out_record = (calendar_record_h)out_data;
141
142         return CALENDAR_ERROR_NONE;
143 }
144
145 static int _cal_record_search_get_str(calendar_record_h record, unsigned int property_id, char** out_str)
146 {
147         cal_search_s *rec = (cal_search_s*)(record);
148         GSList *cursor;
149
150         for (cursor = rec->values; cursor; cursor = cursor->next) {
151                 cal_search_value_s *data = cursor->data;
152                 if (data->property_id == property_id) {
153                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
154                                 *out_str = cal_strdup(data->value.s);
155                                 break;
156                         } else {
157                                 /* LCOV_EXCL_START */
158                                 ERR("invalid parameter (property:0x%x)", data->property_id);
159                                 return CALENDAR_ERROR_INVALID_PARAMETER;
160                                 /* LCOV_EXCL_STOP */
161                         }
162                 }
163         }
164
165         return CALENDAR_ERROR_NONE;
166 }
167
168 static int _cal_record_search_get_str_p(calendar_record_h record, unsigned int property_id, char** out_str)
169 {
170         cal_search_s *rec = (cal_search_s*)(record);
171         GSList *cursor;
172
173         for (cursor = rec->values; cursor; cursor = cursor->next) {
174                 cal_search_value_s *data = cursor->data;
175                 if (data->property_id == property_id) {
176                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
177                                 *out_str = (data->value.s);
178                                 break;
179                         } else {
180                                 /* LCOV_EXCL_START */
181                                 ERR("invalid parameter (property:0x%x)", data->property_id);
182                                 return CALENDAR_ERROR_INVALID_PARAMETER;
183                                 /* LCOV_EXCL_STOP */
184                         }
185                 }
186         }
187
188         return CALENDAR_ERROR_NONE;
189 }
190
191 static int _cal_record_search_get_int(calendar_record_h record, unsigned int property_id, int* out_value)
192 {
193         cal_search_s *rec = (cal_search_s*)(record);
194         GSList *cursor;
195
196         for (cursor = rec->values; cursor; cursor = cursor->next) {
197                 cal_search_value_s *data = cursor->data;
198                 if (data->property_id == property_id) {
199                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
200                                 *out_value = (data->value.i);
201                                 break;
202                         } else {
203                                 /* LCOV_EXCL_START */
204                                 ERR("invalid parameter (property:0x%x)", data->property_id);
205                                 return CALENDAR_ERROR_INVALID_PARAMETER;
206                                 /* LCOV_EXCL_STOP */
207                         }
208                 }
209         }
210
211         return CALENDAR_ERROR_NONE;
212 }
213
214 static int _cal_record_search_get_double(calendar_record_h record, unsigned int property_id, double* out_value)
215 {
216         cal_search_s *rec = (cal_search_s*)(record);
217         GSList *cursor;
218
219         for (cursor = rec->values; cursor; cursor = cursor->next) {
220                 cal_search_value_s *data = cursor->data;
221                 if (data->property_id == property_id) {
222                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
223                                 *out_value = (data->value.d);
224                                 break;
225                         } else {
226                                 /* LCOV_EXCL_START */
227                                 ERR("invalid parameter (property:0x%x)", data->property_id);
228                                 return CALENDAR_ERROR_INVALID_PARAMETER;
229                                 /* LCOV_EXCL_STOP */
230                         }
231                 }
232         }
233
234         return CALENDAR_ERROR_NONE;
235 }
236
237 static int _cal_record_search_get_lli(calendar_record_h record, unsigned int property_id, long long int* out_value)
238 {
239         cal_search_s *rec = (cal_search_s*)(record);
240         GSList *cursor;
241
242         for (cursor = rec->values; cursor; cursor = cursor->next) {
243                 cal_search_value_s *data = cursor->data;
244                 if (data->property_id == property_id) {
245                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
246                                 *out_value = (data->value.lli);
247                                 break;
248                         } else {
249                                 /* LCOV_EXCL_START */
250                                 ERR("invalid parameter (property:0x%x)", data->property_id);
251                                 return CALENDAR_ERROR_INVALID_PARAMETER;
252                                 /* LCOV_EXCL_STOP */
253                         }
254                 }
255         }
256
257         return CALENDAR_ERROR_NONE;
258 }
259
260 static int _cal_record_search_get_caltime(calendar_record_h record, unsigned int property_id, calendar_time_s* out_value)
261 {
262         cal_search_s *rec = (cal_search_s*)(record);
263         GSList *cursor;
264
265         for (cursor = rec->values; cursor; cursor = cursor->next) {
266                 cal_search_value_s *data = cursor->data;
267                 if (data->property_id == property_id) {
268                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
269                                 *out_value = (data->value.caltime);
270                                 break;
271                         } else {
272                                 /* LCOV_EXCL_START */
273                                 ERR("invalid parameter (property:0x%x)", data->property_id);
274                                 return CALENDAR_ERROR_INVALID_PARAMETER;
275                                 /* LCOV_EXCL_STOP */
276                         }
277                 }
278         }
279
280         return CALENDAR_ERROR_NONE;
281 }
282
283 static int _cal_record_search_set_str(calendar_record_h record, unsigned int property_id, const char* value)
284 {
285         cal_search_s *rec = (cal_search_s*)(record);
286         GSList *cursor;
287         cal_search_value_s *data = NULL;
288
289         for (cursor = rec->values; cursor; cursor = cursor->next) {
290                 data = cursor->data;
291                 if (data->property_id == property_id) {
292                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
293                                 CAL_FREE(data->value.s);
294                                 (data->value.s) = cal_strdup(value);
295                                 return CALENDAR_ERROR_NONE;
296                         } else {
297                                 /* LCOV_EXCL_START */
298                                 ERR("invalid parameter (property:0x%x)", data->property_id);
299                                 return CALENDAR_ERROR_INVALID_PARAMETER;
300                                 /* LCOV_EXCL_STOP */
301                         }
302                 }
303         }
304
305         data = calloc(1, sizeof(cal_search_value_s));
306         RETVM_IF(NULL == data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
307         data->property_id = property_id;
308         data->value.s = cal_strdup(value);
309         rec->values = g_slist_append(rec->values, data);
310
311         return CALENDAR_ERROR_NONE;
312 }
313
314 static int _cal_record_search_set_int(calendar_record_h record, unsigned int property_id, int value)
315 {
316         cal_search_s *rec = (cal_search_s*)(record);
317         GSList *cursor;
318         cal_search_value_s *data = NULL;
319
320         for (cursor = rec->values; cursor; cursor = cursor->next) {
321                 data = cursor->data;
322                 if (data->property_id == property_id) {
323                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
324                                 (data->value.i) = value;
325                                 return CALENDAR_ERROR_NONE;
326                         } else {
327                                 /* LCOV_EXCL_START */
328                                 ERR("invalid parameter (property:0x%x)", data->property_id);
329                                 return CALENDAR_ERROR_INVALID_PARAMETER;
330                                 /* LCOV_EXCL_STOP */
331                         }
332                 }
333         }
334
335         data = calloc(1, sizeof(cal_search_value_s));
336         RETVM_IF(NULL == data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
337         data->property_id = property_id;
338         (data->value.i) = value;
339         rec->values = g_slist_append(rec->values, data);
340
341         return CALENDAR_ERROR_NONE;
342 }
343
344 static int _cal_record_search_set_double(calendar_record_h record, unsigned int property_id, double value)
345 {
346         cal_search_s *rec = (cal_search_s*)(record);
347         GSList *cursor;
348         cal_search_value_s *data = NULL;
349
350         for (cursor = rec->values; cursor; cursor = cursor->next) {
351                 data = cursor->data;
352                 if (data->property_id == property_id) {
353                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
354                                 (data->value.d) = value;
355                                 return CALENDAR_ERROR_NONE;
356                         } else {
357                                 /* LCOV_EXCL_START */
358                                 ERR("invalid parameter (property:0x%x)", data->property_id);
359                                 return CALENDAR_ERROR_INVALID_PARAMETER;
360                                 /* LCOV_EXCL_STOP */
361                         }
362                 }
363         }
364
365         data = calloc(1, sizeof(cal_search_value_s));
366         RETVM_IF(NULL == data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc(cal_search_s) Fail");
367         data->property_id = property_id;
368         (data->value.d) = value;
369         rec->values = g_slist_append(rec->values, data);
370
371         return CALENDAR_ERROR_NONE;
372 }
373
374 static int _cal_record_search_set_lli(calendar_record_h record, unsigned int property_id, long long int value)
375 {
376         cal_search_s *rec = (cal_search_s*)(record);
377         GSList *cursor;
378         cal_search_value_s *data = NULL;
379
380         for (cursor = rec->values; cursor; cursor = cursor->next) {
381                 data = cursor->data;
382                 if (data->property_id == property_id) {
383                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
384                                 (data->value.lli) = value;
385                                 return CALENDAR_ERROR_NONE;
386                         } else {
387                                 /* LCOV_EXCL_START */
388                                 ERR("invalid parameter (property:0x%x)", data->property_id);
389                                 return CALENDAR_ERROR_INVALID_PARAMETER;
390                                 /* LCOV_EXCL_STOP */
391                         }
392                 }
393         }
394
395         data = calloc(1, sizeof(cal_search_value_s));
396         RETVM_IF(NULL == data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
397         data->property_id = property_id;
398         (data->value.lli) = value;
399         rec->values = g_slist_append(rec->values, data);
400
401         return CALENDAR_ERROR_NONE;
402 }
403
404 static int _cal_record_search_set_caltime(calendar_record_h record, unsigned int property_id, calendar_time_s value)
405 {
406         cal_search_s *rec = (cal_search_s*)(record);
407         GSList *cursor;
408         cal_search_value_s *data = NULL;
409
410         for (cursor = rec->values; cursor; cursor = cursor->next) {
411                 data = cursor->data;
412                 if (data->property_id == property_id) {
413                         if (CAL_PROPERTY_CHECK_DATA_TYPE(data->property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
414                                 (data->value.caltime) = value;
415                                 return CALENDAR_ERROR_NONE;
416                         } else {
417                                 /* LCOV_EXCL_START */
418                                 ERR("invalid parameter (property:0x%x)", data->property_id);
419                                 return CALENDAR_ERROR_INVALID_PARAMETER;
420                                 /* LCOV_EXCL_STOP */
421                         }
422                 }
423         }
424
425         data = calloc(1, sizeof(cal_search_value_s));
426         RETVM_IF(NULL == data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
427         data->property_id = property_id;
428         (data->value.caltime) = value;
429         rec->values = g_slist_append(rec->values, data);
430
431         return CALENDAR_ERROR_NONE;
432 }
433