revise packaging
[platform/core/pim/contacts-service.git] / common / ctsvc_record_addressbook.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 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 "contacts.h"
21
22 #include "ctsvc_internal.h"
23 #include "ctsvc_record.h"
24
25 static int __ctsvc_addressbook_create(contacts_record_h *out_record);
26 static int __ctsvc_addressbook_destroy(contacts_record_h record, bool delete_child);
27 static int __ctsvc_addressbook_clone(contacts_record_h record, contacts_record_h *out_record);
28 static int __ctsvc_addressbook_get_int(contacts_record_h record, unsigned int property_id, int *out);
29 static int __ctsvc_addressbook_get_str(contacts_record_h record, unsigned int property_id, char **out_str);
30 static int __ctsvc_addressbook_get_str_p(contacts_record_h record, unsigned int property_id, char **out_str);
31 static int __ctsvc_addressbook_set_int(contacts_record_h record, unsigned int property_id, int value, bool *is_dirty);
32 static int __ctsvc_addressbook_set_str(contacts_record_h record, unsigned int property_id, const char *str, bool *is_dirty);
33
34 ctsvc_record_plugin_cb_s addressbook_plugin_cbs = {
35         .create = __ctsvc_addressbook_create,
36         .destroy = __ctsvc_addressbook_destroy,
37         .clone = __ctsvc_addressbook_clone,
38         .get_str = __ctsvc_addressbook_get_str,
39         .get_str_p =  __ctsvc_addressbook_get_str_p,
40         .get_int = __ctsvc_addressbook_get_int,
41         .get_bool = NULL,
42         .get_lli = NULL,
43         .get_double = NULL,
44         .set_str = __ctsvc_addressbook_set_str,
45         .set_int = __ctsvc_addressbook_set_int,
46         .set_bool = NULL,
47         .set_lli = NULL,
48         .set_double = NULL,
49         .add_child_record = NULL,
50         .remove_child_record = NULL,
51         .get_child_record_count = NULL,
52         .get_child_record_at_p = NULL,
53         .clone_child_record_list = NULL,
54 };
55
56 static int __ctsvc_addressbook_create(contacts_record_h *out_record)
57 {
58         ctsvc_addressbook_s *addressbook;
59
60         addressbook = calloc(1, sizeof(ctsvc_addressbook_s));
61         RETVM_IF(NULL == addressbook, CONTACTS_ERROR_OUT_OF_MEMORY, "calloc() Fail");
62
63         *out_record = (contacts_record_h)addressbook;
64         return CONTACTS_ERROR_NONE;
65 }
66
67 static int __ctsvc_addressbook_destroy(contacts_record_h record, bool delete_child)
68 {
69         ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
70
71         addressbook->base.plugin_cbs = NULL;   /* help to find double destroy bug (refer to the contacts_record_destroy) */
72         free(addressbook->base.properties_flags);
73         free(addressbook->name);
74         free(addressbook);
75
76         return CONTACTS_ERROR_NONE;
77 }
78
79 static int __ctsvc_addressbook_clone(contacts_record_h record, contacts_record_h *out_record)
80 {
81         ctsvc_addressbook_s *src = (ctsvc_addressbook_s*)record;
82         ctsvc_addressbook_s *dest;
83
84         dest = calloc(1, sizeof(ctsvc_addressbook_s));
85         RETVM_IF(NULL == dest, CONTACTS_ERROR_OUT_OF_MEMORY,
86                         "calloc() Fail");
87
88         int ret = ctsvc_record_copy_base(&(dest->base), &(src->base));
89         if (CONTACTS_ERROR_NONE != ret) {
90                 /* LCOV_EXCL_START */
91                 ERR("ctsvc_record_copy_base() Fail");
92                 free(dest);
93                 return ret;
94                 /* LCOV_EXCL_STOP */
95         }
96
97         dest->id = src->id;
98         dest->account_id = src->account_id;
99         dest->mode = src->mode;
100         dest->name = SAFE_STRDUP(src->name);
101
102         *out_record = (contacts_record_h)dest;
103
104         return CONTACTS_ERROR_NONE;
105 }
106
107 static int __ctsvc_addressbook_get_str_real(contacts_record_h record,
108                 unsigned int property_id, char **out_str, bool copy)
109 {
110         ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
111
112         switch (property_id) {
113         case CTSVC_PROPERTY_ADDRESSBOOK_NAME:
114                 *out_str = GET_STR(copy, addressbook->name);
115                 break;
116         default:
117                 /* LCOV_EXCL_START */
118                 ERR("This field(%d) is not supported in value(addressbook)", property_id);
119                 return CONTACTS_ERROR_INVALID_PARAMETER;
120                 /* LCOV_EXCL_STOP */
121         }
122         return CONTACTS_ERROR_NONE;
123 }
124
125 static int __ctsvc_addressbook_get_str_p(contacts_record_h record, unsigned int property_id, char **out_str)
126 {
127         return __ctsvc_addressbook_get_str_real(record, property_id, out_str, false);
128 }
129
130 static int __ctsvc_addressbook_get_str(contacts_record_h record, unsigned int property_id, char **out_str)
131 {
132         return __ctsvc_addressbook_get_str_real(record, property_id, out_str, true);
133 }
134
135 static int __ctsvc_addressbook_set_str(contacts_record_h record,
136                 unsigned int property_id, const char *str, bool *is_dirty)
137 {
138         ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
139
140         switch (property_id) {
141         case CTSVC_PROPERTY_ADDRESSBOOK_NAME:
142                 CHECK_DIRTY_STR(addressbook->name, str, is_dirty);
143                 FREEandSTRDUP(addressbook->name, str);
144                 break;
145         default:
146                 /* LCOV_EXCL_START */
147                 ERR("This field(%d) is not supported in value(addressbook)", property_id);
148                 return CONTACTS_ERROR_INVALID_PARAMETER;
149                 /* LCOV_EXCL_STOP */
150         }
151         return CONTACTS_ERROR_NONE;
152 }
153
154 static int __ctsvc_addressbook_get_int(contacts_record_h record,
155                 unsigned int property_id, int *out)
156 {
157         ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
158
159         switch (property_id) {
160         case CTSVC_PROPERTY_ADDRESSBOOK_ID:
161                 *out = addressbook->id;
162                 break;
163         case CTSVC_PROPERTY_ADDRESSBOOK_ACCOUNT_ID:
164                 *out = addressbook->account_id;
165                 break;
166         case CTSVC_PROPERTY_ADDRESSBOOK_MODE:
167                 *out = addressbook->mode;
168                 break;
169         default:
170                 /* LCOV_EXCL_START */
171                 ERR("property_id(%d) is not supported in value(addressbook)", property_id);
172                 return CONTACTS_ERROR_INVALID_PARAMETER;
173                 /* LCOV_EXCL_STOP */
174         }
175         return CONTACTS_ERROR_NONE;
176 }
177
178 static int __ctsvc_addressbook_set_int(contacts_record_h record,
179                 unsigned int property_id, int value, bool *is_dirty)
180 {
181         ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
182
183         switch (property_id) {
184         case CTSVC_PROPERTY_ADDRESSBOOK_ID:
185                 CHECK_DIRTY_VAL(addressbook->id, value, is_dirty);
186                 addressbook->id = value;
187                 break;
188         case CTSVC_PROPERTY_ADDRESSBOOK_MODE:
189                 RETVM_IF(value != CONTACTS_ADDRESS_BOOK_MODE_NONE
190                                 && value != CONTACTS_ADDRESS_BOOK_MODE_READONLY,
191                                 CONTACTS_ERROR_INVALID_PARAMETER, "address_book mode is %d", value);
192                 CHECK_DIRTY_VAL(addressbook->mode, value, is_dirty);
193                 addressbook->mode = value;
194                 break;
195         case CTSVC_PROPERTY_ADDRESSBOOK_ACCOUNT_ID:
196                 RETVM_IF(0 < addressbook->id, CONTACTS_ERROR_INVALID_PARAMETER,
197                                 "property_id(%d) is a read-only value (addressbook)", property_id);
198                 CHECK_DIRTY_VAL(addressbook->account_id, value, is_dirty);
199                 addressbook->account_id = value;
200                 break;
201         default:
202                 /* LCOV_EXCL_START */
203                 ERR("property_id(%d) is not supported in value(addressbook)", property_id);
204                 return CONTACTS_ERROR_INVALID_PARAMETER;
205                 /* LCOV_EXCL_STOP */
206         }
207         return CONTACTS_ERROR_NONE;
208 }
209