2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <MsgProxyContact.h>
18 #include <MsgContact.h>
21 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
26 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
29 /*==================================================================================================
31 ==================================================================================================*/
32 __thread bool isContactSvcConnected = false;
35 /*==================================================================================================
36 FUNCTION IMPLEMENTATION
37 ==================================================================================================*/
38 msg_error_t MsgOpenContactSvc()
40 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
41 int errCode = CONTACTS_ERROR_NONE;
43 if (!isContactSvcConnected) {
44 errCode = contacts_connect();
46 if (errCode == CONTACTS_ERROR_NONE) {
47 MSG_DEBUG("Connect to Contact Service Success");
48 isContactSvcConnected = true;
50 MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
51 isContactSvcConnected = false;
52 return MSG_ERR_DB_CONNECT;
55 MSG_DEBUG("Already connected to Contact Service.");
57 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
62 msg_error_t MsgCloseContactSvc()
64 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
65 int errCode = CONTACTS_ERROR_NONE;
67 if (isContactSvcConnected) {
68 errCode = contacts_disconnect();
70 if (errCode == CONTACTS_ERROR_NONE) {
71 MSG_DEBUG("Disconnect to Contact Service Success");
72 isContactSvcConnected = false;
74 MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
75 return MSG_ERR_DB_DISCONNECT;
78 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
82 msg_error_t MsgGetContactSearchList(const char *pSearchVal, MSG_ADDRESS_INFO_S **pAddrInfo, int *count)
85 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
87 msg_error_t err = MSG_SUCCESS;
91 if (pSearchVal == NULL) {
92 MSG_DEBUG("pSearchVal is NULL.");
93 return MSG_ERR_NULL_POINTER;
96 if (pAddrInfo == NULL) {
97 MSG_DEBUG("pAddrInfo is NULL.");
98 return MSG_ERR_NULL_POINTER;
101 if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
102 MSG_DEBUG("MsgOpenContactSvc fail.");
106 if (!isContactSvcConnected) {
107 MSG_DEBUG("Contact Service Not Opened.");
108 return MSG_ERR_UNKNOWN;
111 MSG_SEC_DEBUG("pSearchVal [%s]", pSearchVal);
114 unsigned int index = 0;
115 contacts_query_h query = NULL;
116 contacts_filter_h filter = NULL;
117 contacts_list_h personNumbers = NULL;
119 ret = contacts_query_create(_contacts_person_number._uri, &query);
120 ret = contacts_filter_create(_contacts_person_number._uri, &filter);
122 ret = contacts_filter_add_str(filter, _contacts_person_number.display_name, CONTACTS_MATCH_CONTAINS, pSearchVal);
123 ret = contacts_query_set_filter(query, filter);
125 ret = contacts_db_get_records_with_query(query, 0, 0, &personNumbers);
126 if (ret != CONTACTS_ERROR_NONE) {
127 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
128 contacts_query_destroy(query);
129 contacts_filter_destroy(filter);
130 contacts_list_destroy(personNumbers, true);
131 return MSG_ERR_UNKNOWN;
134 ret = contacts_list_get_count(personNumbers, count);
135 if (*count == 0 || ret != CONTACTS_ERROR_NONE) {
136 MSG_DEBUG("No Serach Data from Contact Service.");
138 contacts_query_destroy(query);
139 contacts_filter_destroy(filter);
140 contacts_list_destroy(personNumbers, true);
144 contacts_query_destroy(query);
145 contacts_filter_destroy(filter);
147 MSG_DEBUG(" *count [%d]", *count);
149 *pAddrInfo = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * (*count)];
150 memset(*pAddrInfo, 0x00, (sizeof(MSG_ADDRESS_INFO_S) * (*count)));
152 contacts_record_h personNumber = NULL;
154 while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(personNumbers, &personNumber)) {
155 char* normalizedNumber = NULL;
156 ret = contacts_record_get_str(personNumber, _contacts_person_number.normalized_number, &normalizedNumber);
157 if (ret != CONTACTS_ERROR_NONE) {
158 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
159 } else if (normalizedNumber) {
160 MSG_DEBUG("normalizedNumber [%s]", normalizedNumber);
161 strncpy((*pAddrInfo)[index].addressVal, normalizedNumber, MAX_ADDRESS_VAL_LEN);
164 contacts_list_next(personNumbers);
168 contacts_list_destroy(personNumbers, true);
170 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */