4 * Copyright (c) 2010 - 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 "ctsvc_internal.h"
23 #include "ctsvc_record.h"
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);
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,
44 .set_str = __ctsvc_addressbook_set_str,
45 .set_int = __ctsvc_addressbook_set_int,
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,
56 static int __ctsvc_addressbook_create(contacts_record_h *out_record)
58 ctsvc_addressbook_s *addressbook;
60 addressbook = calloc(1, sizeof(ctsvc_addressbook_s));
61 RETVM_IF(NULL == addressbook, CONTACTS_ERROR_OUT_OF_MEMORY, "calloc() Fail");
63 *out_record = (contacts_record_h)addressbook;
64 return CONTACTS_ERROR_NONE;
67 static int __ctsvc_addressbook_destroy(contacts_record_h record, bool delete_child)
69 ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
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);
76 return CONTACTS_ERROR_NONE;
79 static int __ctsvc_addressbook_clone(contacts_record_h record, contacts_record_h *out_record)
81 ctsvc_addressbook_s *src = (ctsvc_addressbook_s*)record;
82 ctsvc_addressbook_s *dest;
84 dest = calloc(1, sizeof(ctsvc_addressbook_s));
85 RETVM_IF(NULL == dest, CONTACTS_ERROR_OUT_OF_MEMORY,
88 int ret = ctsvc_record_copy_base(&(dest->base), &(src->base));
89 if (CONTACTS_ERROR_NONE != ret) {
91 ERR("ctsvc_record_copy_base() Fail");
98 dest->account_id = src->account_id;
99 dest->mode = src->mode;
100 dest->name = SAFE_STRDUP(src->name);
102 *out_record = (contacts_record_h)dest;
104 return CONTACTS_ERROR_NONE;
107 static int __ctsvc_addressbook_get_str_real(contacts_record_h record,
108 unsigned int property_id, char **out_str, bool copy)
110 ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
112 switch (property_id) {
113 case CTSVC_PROPERTY_ADDRESSBOOK_NAME:
114 *out_str = GET_STR(copy, addressbook->name);
117 /* LCOV_EXCL_START */
118 ERR("This field(%d) is not supported in value(addressbook)", property_id);
119 return CONTACTS_ERROR_INVALID_PARAMETER;
122 return CONTACTS_ERROR_NONE;
125 static int __ctsvc_addressbook_get_str_p(contacts_record_h record, unsigned int property_id, char **out_str)
127 return __ctsvc_addressbook_get_str_real(record, property_id, out_str, false);
130 static int __ctsvc_addressbook_get_str(contacts_record_h record, unsigned int property_id, char **out_str)
132 return __ctsvc_addressbook_get_str_real(record, property_id, out_str, true);
135 static int __ctsvc_addressbook_set_str(contacts_record_h record,
136 unsigned int property_id, const char *str, bool *is_dirty)
138 ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
140 switch (property_id) {
141 case CTSVC_PROPERTY_ADDRESSBOOK_NAME:
142 CHECK_DIRTY_STR(addressbook->name, str, is_dirty);
143 FREEandSTRDUP(addressbook->name, str);
146 /* LCOV_EXCL_START */
147 ERR("This field(%d) is not supported in value(addressbook)", property_id);
148 return CONTACTS_ERROR_INVALID_PARAMETER;
151 return CONTACTS_ERROR_NONE;
154 static int __ctsvc_addressbook_get_int(contacts_record_h record,
155 unsigned int property_id, int *out)
157 ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
159 switch (property_id) {
160 case CTSVC_PROPERTY_ADDRESSBOOK_ID:
161 *out = addressbook->id;
163 case CTSVC_PROPERTY_ADDRESSBOOK_ACCOUNT_ID:
164 *out = addressbook->account_id;
166 case CTSVC_PROPERTY_ADDRESSBOOK_MODE:
167 *out = addressbook->mode;
170 /* LCOV_EXCL_START */
171 ERR("property_id(%d) is not supported in value(addressbook)", property_id);
172 return CONTACTS_ERROR_INVALID_PARAMETER;
175 return CONTACTS_ERROR_NONE;
178 static int __ctsvc_addressbook_set_int(contacts_record_h record,
179 unsigned int property_id, int value, bool *is_dirty)
181 ctsvc_addressbook_s *addressbook = (ctsvc_addressbook_s*)record;
183 switch (property_id) {
184 case CTSVC_PROPERTY_ADDRESSBOOK_ID:
185 CHECK_DIRTY_VAL(addressbook->id, value, is_dirty);
186 addressbook->id = value;
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;
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;
202 /* LCOV_EXCL_START */
203 ERR("property_id(%d) is not supported in value(addressbook)", property_id);
204 return CONTACTS_ERROR_INVALID_PARAMETER;
207 return CONTACTS_ERROR_NONE;