modify terminology:calendar->book,allday->localtime,normal->utime,svc->service
[platform/core/pim/calendar-service.git] / common / cal_record_book.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_book_create(calendar_record_h* out_record);
31 static int _cal_record_book_destroy(calendar_record_h record, bool delete_child);
32 static int _cal_record_book_clone(calendar_record_h record, calendar_record_h* out_record);
33 static int _cal_record_book_get_str(calendar_record_h record, unsigned int property_id, char** out_str);
34 static int _cal_record_book_get_str_p(calendar_record_h record, unsigned int property_id, char** out_str);
35 static int _cal_record_book_get_int(calendar_record_h record, unsigned int property_id, int* out_value);
36 static int _cal_record_book_set_str(calendar_record_h record, unsigned int property_id, const char* value);
37 static int _cal_record_book_set_int(calendar_record_h record, unsigned int property_id, int value);
38
39 cal_record_plugin_cb_s cal_record_book_plugin_cb = {
40         .create = _cal_record_book_create,
41         .destroy = _cal_record_book_destroy,
42         .clone = _cal_record_book_clone,
43         .get_str = _cal_record_book_get_str,
44         .get_str_p = _cal_record_book_get_str_p,
45         .get_int = _cal_record_book_get_int,
46         .get_double = NULL,
47         .get_lli = NULL,
48         .get_caltime = NULL,
49         .set_str = _cal_record_book_set_str,
50         .set_int = _cal_record_book_set_int,
51         .set_double = NULL,
52         .set_lli = NULL,
53         .set_caltime = NULL,
54         .add_child_record = NULL,
55         .remove_child_record = NULL,
56         .get_child_record_count = NULL,
57         .get_child_record_at_p = NULL,
58         .clone_child_record_list = NULL
59 };
60
61 static void _cal_record_book_struct_init(cal_book_s *record)
62 {
63         memset(record, 0, sizeof(cal_book_s));
64         record->index = -1;
65         record->visibility = true;
66         record->account_id = LOCAL_ACCOUNT_ID;
67         record->sync_event = 1;
68 }
69
70 static int _cal_record_book_create(calendar_record_h* out_record)
71 {
72         cal_book_s *temp = NULL;
73         int ret = CALENDAR_ERROR_NONE;
74
75         temp = calloc(1, sizeof(cal_book_s));
76         RETVM_IF(NULL == temp, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
77
78         _cal_record_book_struct_init(temp);
79
80         *out_record = (calendar_record_h)temp;
81
82         return ret;
83 }
84
85 static void _cal_record_book_struct_free(cal_book_s *record)
86 {
87         CAL_FREE(record->uid);
88         CAL_FREE(record->name);
89         CAL_FREE(record->description);
90         CAL_FREE(record->color);
91         CAL_FREE(record->location);
92         CAL_FREE(record->sync_data1);
93         CAL_FREE(record->sync_data2);
94         CAL_FREE(record->sync_data3);
95         CAL_FREE(record->sync_data4);
96         CAL_FREE(record);
97 }
98
99 static int _cal_record_book_destroy(calendar_record_h record, bool delete_child)
100 {
101         int ret = CALENDAR_ERROR_NONE;
102
103         cal_book_s *temp = (cal_book_s*)(record);
104
105         _cal_record_book_struct_free(temp);
106
107         return ret;
108 }
109
110 static int _cal_record_book_clone(calendar_record_h record, calendar_record_h* out_record)
111 {
112         cal_book_s *out_data = NULL;
113         cal_book_s *src_data = NULL;
114
115         src_data = (cal_book_s*)(record);
116
117         out_data = calloc(1, sizeof(cal_book_s));
118         RETVM_IF(NULL == out_data, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
119
120
121         CAL_RECORD_COPY_COMMON(&(out_data->common), &(src_data->common));
122
123         out_data->index = src_data->index;
124         out_data->store_type = src_data->store_type;
125         out_data->uid = cal_strdup(src_data->uid);
126         out_data->updated = src_data->updated;
127         out_data->name = cal_strdup(src_data->name);
128         out_data->description = cal_strdup(src_data->description);
129         out_data->color = cal_strdup(src_data->color);
130         out_data->location = cal_strdup(src_data->location);
131         out_data->visibility = src_data->visibility;
132         out_data->sync_event = src_data->sync_event;
133         out_data->is_deleted = src_data->is_deleted;
134         out_data->account_id = src_data->account_id;
135         out_data->sync_data1 = cal_strdup(src_data->sync_data1);
136         out_data->sync_data2 = cal_strdup(src_data->sync_data2);
137         out_data->sync_data3 = cal_strdup(src_data->sync_data3);
138         out_data->sync_data4 = cal_strdup(src_data->sync_data4);
139         out_data->mode = src_data->mode;
140
141         *out_record = (calendar_record_h)out_data;
142
143         return CALENDAR_ERROR_NONE;
144 }
145
146 static int _cal_record_book_get_str(calendar_record_h record, unsigned int property_id, char** out_str)
147 {
148         cal_book_s *cal_rec = (cal_book_s*)(record);
149         switch (property_id) {
150         case CAL_PROPERTY_BOOK_UID:
151                 *out_str = cal_strdup(cal_rec->uid);
152                 break;
153         case CAL_PROPERTY_BOOK_NAME:
154                 *out_str = cal_strdup(cal_rec->name);
155                 break;
156         case CAL_PROPERTY_BOOK_DESCRIPTION:
157                 *out_str = cal_strdup(cal_rec->description);
158                 break;
159         case CAL_PROPERTY_BOOK_COLOR:
160                 *out_str = cal_strdup(cal_rec->color);
161                 break;
162         case CAL_PROPERTY_BOOK_LOCATION:
163                 *out_str = cal_strdup(cal_rec->location);
164                 break;
165         case CAL_PROPERTY_BOOK_SYNC_DATA1:
166                 *out_str = cal_strdup(cal_rec->sync_data1);
167                 break;
168         case CAL_PROPERTY_BOOK_SYNC_DATA2:
169                 *out_str = cal_strdup(cal_rec->sync_data2);
170                 break;
171         case CAL_PROPERTY_BOOK_SYNC_DATA3:
172                 *out_str = cal_strdup(cal_rec->sync_data3);
173                 break;
174         case CAL_PROPERTY_BOOK_SYNC_DATA4:
175                 *out_str = cal_strdup(cal_rec->sync_data4);
176                 break;
177         default:
178                 /* LCOV_EXCL_START */
179                 ERR("invalid parameter (property:0x%x)", property_id);
180                 return CALENDAR_ERROR_INVALID_PARAMETER;
181                 /* LCOV_EXCL_STOP */
182         }
183
184         return CALENDAR_ERROR_NONE;
185 }
186
187 static int _cal_record_book_get_str_p(calendar_record_h record, unsigned int property_id, char** out_str)
188 {
189         cal_book_s *cal_rec = (cal_book_s*)(record);
190         switch (property_id) {
191         case CAL_PROPERTY_BOOK_UID:
192                 *out_str = (cal_rec->uid);
193                 break;
194         case CAL_PROPERTY_BOOK_NAME:
195                 *out_str = (cal_rec->name);
196                 break;
197         case CAL_PROPERTY_BOOK_DESCRIPTION:
198                 *out_str = (cal_rec->description);
199                 break;
200         case CAL_PROPERTY_BOOK_COLOR:
201                 *out_str = (cal_rec->color);
202                 break;
203         case CAL_PROPERTY_BOOK_LOCATION:
204                 *out_str = (cal_rec->location);
205                 break;
206         case CAL_PROPERTY_BOOK_SYNC_DATA1:
207                 *out_str = (cal_rec->sync_data1);
208                 break;
209         case CAL_PROPERTY_BOOK_SYNC_DATA2:
210                 *out_str = (cal_rec->sync_data2);
211                 break;
212         case CAL_PROPERTY_BOOK_SYNC_DATA3:
213                 *out_str = (cal_rec->sync_data3);
214                 break;
215         case CAL_PROPERTY_BOOK_SYNC_DATA4:
216                 *out_str = (cal_rec->sync_data4);
217                 break;
218         default:
219                 /* LCOV_EXCL_START */
220                 ERR("invalid parameter (property:0x%x)", property_id);
221                 return CALENDAR_ERROR_INVALID_PARAMETER;
222                 /* LCOV_EXCL_STOP */
223         }
224
225         return CALENDAR_ERROR_NONE;
226 }
227
228 static int _cal_record_book_get_int(calendar_record_h record, unsigned int property_id, int* out_value)
229 {
230         cal_book_s *cal_rec = (cal_book_s*)(record);
231         switch (property_id) {
232         case CAL_PROPERTY_BOOK_ID:
233                 *out_value = (cal_rec->index);
234                 break;
235         case CAL_PROPERTY_BOOK_VISIBILITY:
236                 *out_value = (cal_rec->visibility);
237                 break;
238         case CAL_PROPERTY_BOOK_ACCOUNT_ID:
239                 *out_value = (cal_rec->account_id);
240                 break;
241         case CAL_PROPERTY_BOOK_STORE_TYPE:
242                 *out_value = (cal_rec->store_type);
243                 break;
244         case CAL_PROPERTY_BOOK_SYNC_EVENT:
245                 *out_value = (cal_rec->sync_event);
246                 break;
247         case CAL_PROPERTY_BOOK_MODE:
248                 *out_value = (cal_rec->mode);
249                 break;
250         default:
251                 /* LCOV_EXCL_START */
252                 ERR("invalid parameter (property:0x%x)", property_id);
253                 return CALENDAR_ERROR_INVALID_PARAMETER;
254                 /* LCOV_EXCL_STOP */
255         }
256
257         return CALENDAR_ERROR_NONE;
258 }
259
260 static int _cal_record_book_set_str(calendar_record_h record, unsigned int property_id, const char* value)
261 {
262         cal_book_s *cal_rec = (cal_book_s*)(record);
263         switch (property_id) {
264         case CAL_PROPERTY_BOOK_UID:
265                 CAL_FREE(cal_rec->uid);
266                 cal_rec->uid = cal_strdup(value);
267                 break;
268         case CAL_PROPERTY_BOOK_NAME:
269                 CAL_FREE(cal_rec->name);
270                 cal_rec->name = cal_strdup(value);
271                 break;
272         case CAL_PROPERTY_BOOK_DESCRIPTION:
273                 CAL_FREE(cal_rec->description);
274                 cal_rec->description = cal_strdup(value);
275                 break;
276         case CAL_PROPERTY_BOOK_COLOR:
277                 CAL_FREE(cal_rec->color);
278                 cal_rec->color = cal_strdup(value);
279                 break;
280         case CAL_PROPERTY_BOOK_LOCATION:
281                 CAL_FREE(cal_rec->location);
282                 cal_rec->location = cal_strdup(value);
283                 break;
284         case CAL_PROPERTY_BOOK_SYNC_DATA1:
285                 CAL_FREE(cal_rec->sync_data1);
286                 cal_rec->sync_data1 = cal_strdup(value);
287                 break;
288         case CAL_PROPERTY_BOOK_SYNC_DATA2:
289                 CAL_FREE(cal_rec->sync_data2);
290                 cal_rec->sync_data2 = cal_strdup(value);
291                 break;
292         case CAL_PROPERTY_BOOK_SYNC_DATA3:
293                 CAL_FREE(cal_rec->sync_data3);
294                 cal_rec->sync_data3 = cal_strdup(value);
295                 break;
296         case CAL_PROPERTY_BOOK_SYNC_DATA4:
297                 CAL_FREE(cal_rec->sync_data4);
298                 cal_rec->sync_data4 = cal_strdup(value);
299                 break;
300         default:
301                 /* LCOV_EXCL_START */
302                 ERR("invalid parameter (property:0x%x)", property_id);
303                 return CALENDAR_ERROR_INVALID_PARAMETER;
304                 /* LCOV_EXCL_STOP */
305         }
306
307         return CALENDAR_ERROR_NONE;
308 }
309
310 static int _cal_record_book_set_int(calendar_record_h record, unsigned int property_id, int value)
311 {
312         cal_book_s *cal_rec = (cal_book_s*)(record);
313         switch (property_id) {
314         case CAL_PROPERTY_BOOK_ID:
315                 (cal_rec->index) = value;
316                 break;
317         case CAL_PROPERTY_BOOK_VISIBILITY:
318                 (cal_rec->visibility) = value;
319                 break;
320         case CAL_PROPERTY_BOOK_ACCOUNT_ID:
321                 RETVM_IF(0 < cal_rec->index, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d) is a write-once value (calendar)", property_id);
322                 (cal_rec->account_id) = value;
323                 break;
324         case CAL_PROPERTY_BOOK_STORE_TYPE:
325                 switch (value) {
326                 case CALENDAR_BOOK_TYPE_NONE:
327                 case CALENDAR_BOOK_TYPE_EVENT:
328                 case CALENDAR_BOOK_TYPE_TODO:
329                         (cal_rec->store_type) = value;
330                         break;
331                 default:
332                         /* LCOV_EXCL_START */
333                         ERR("Invalid parameter : store type is invalid value (%d)", value);
334                         return CALENDAR_ERROR_INVALID_PARAMETER;
335                         /* LCOV_EXCL_STOP */
336                 }
337                 break;
338         case CAL_PROPERTY_BOOK_SYNC_EVENT:
339                 switch (value) {
340                 case CALENDAR_BOOK_SYNC_EVENT_FOR_ME:
341                 case CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN:
342                 case CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_DELETE:
343                         (cal_rec->sync_event) = value;
344                         break;
345                 default:
346                         /* LCOV_EXCL_START */
347                         ERR("Invalid parameter : sync event is invalid value (%d)", value);
348                         return CALENDAR_ERROR_INVALID_PARAMETER;
349                         /* LCOV_EXCL_STOP */
350                 }
351                 break;
352         case CAL_PROPERTY_BOOK_MODE:
353                 RETVM_IF(value != CALENDAR_BOOK_MODE_NONE && value != CALENDAR_BOOK_MODE_RECORD_READONLY,
354                                 CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter : mode type is invalid value (%d)", value);
355                 (cal_rec->mode) = value;
356                 break;
357         default:
358                 /* LCOV_EXCL_START */
359                 ERR("invalid parameter (property:0x%x)", property_id);
360                 return CALENDAR_ERROR_INVALID_PARAMETER;
361                 /* LCOV_EXCL_STOP */
362         }
363
364         return CALENDAR_ERROR_NONE;
365 }