apply tizen c++ coding rule
[platform/core/messaging/msg-service.git] / proxy / MsgProxyContact.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15 */
16
17 #include <MsgProxyContact.h>
18 #include <MsgContact.h>
19 #include <MsgDebug.h>
20
21 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
22 extern "C"
23 {
24         #include <contacts.h>
25 }
26 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
27
28
29 /*==================================================================================================
30                                      VARIABLES
31 ==================================================================================================*/
32 __thread bool isContactSvcConnected = false;
33
34
35 /*==================================================================================================
36                                      FUNCTION IMPLEMENTATION
37 ==================================================================================================*/
38 msg_error_t MsgOpenContactSvc()
39 {
40 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
41         int errCode = CONTACTS_ERROR_NONE;
42
43         if (!isContactSvcConnected) {
44                 errCode = contacts_connect();
45
46                 if (errCode == CONTACTS_ERROR_NONE) {
47                         MSG_DEBUG("Connect to Contact Service Success");
48                         isContactSvcConnected = true;
49                 } else {
50                         MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
51                         isContactSvcConnected = false;
52                         return MSG_ERR_DB_CONNECT;
53                 }
54         } else {
55                 MSG_DEBUG("Already connected to Contact Service.");
56         }
57 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
58         return MSG_SUCCESS;
59 }
60
61
62 msg_error_t MsgCloseContactSvc()
63 {
64 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
65         int errCode = CONTACTS_ERROR_NONE;
66
67         if (isContactSvcConnected) {
68                 errCode = contacts_disconnect();
69
70                 if (errCode == CONTACTS_ERROR_NONE) {
71                         MSG_DEBUG("Disconnect to Contact Service Success");
72                         isContactSvcConnected = false;
73                 } else {
74                         MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
75                         return MSG_ERR_DB_DISCONNECT;
76                 }
77         }
78 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
79         return MSG_SUCCESS;
80 }
81
82 msg_error_t MsgGetContactSearchList(const char *pSearchVal, MSG_ADDRESS_INFO_S **pAddrInfo, int *count)
83 {
84         MSG_BEGIN();
85 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
86
87         msg_error_t err = MSG_SUCCESS;
88
89         *count = 0;
90
91         if (pSearchVal == NULL) {
92                 MSG_DEBUG("pSearchVal is NULL.");
93                 return MSG_ERR_NULL_POINTER;
94         }
95
96         if (pAddrInfo == NULL) {
97                 MSG_DEBUG("pAddrInfo is NULL.");
98                 return MSG_ERR_NULL_POINTER;
99         }
100
101         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
102                 MSG_DEBUG("MsgOpenContactSvc fail.");
103                 return err;
104         }
105
106         if (!isContactSvcConnected) {
107                 MSG_DEBUG("Contact Service Not Opened.");
108                 return MSG_ERR_UNKNOWN;
109         }
110
111         MSG_SEC_DEBUG("pSearchVal [%s]", pSearchVal);
112
113         int ret = 0;
114         unsigned int index = 0;
115         contacts_query_h query = NULL;
116         contacts_filter_h filter = NULL;
117         contacts_list_h personNumbers = NULL;
118
119         ret = contacts_query_create(_contacts_person_number._uri, &query);
120         ret = contacts_filter_create(_contacts_person_number._uri, &filter);
121
122         ret = contacts_filter_add_str(filter, _contacts_person_number.display_name, CONTACTS_MATCH_CONTAINS, pSearchVal);
123         ret = contacts_query_set_filter(query, filter);
124
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;
132         }
133
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.");
137                 *count = 0;
138                 contacts_query_destroy(query);
139                 contacts_filter_destroy(filter);
140                 contacts_list_destroy(personNumbers, true);
141                 return MSG_SUCCESS;
142         }
143
144         contacts_query_destroy(query);
145         contacts_filter_destroy(filter);
146
147         MSG_DEBUG(" *count [%d]", *count);
148
149         *pAddrInfo = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * (*count)];
150         memset(*pAddrInfo, 0x00, (sizeof(MSG_ADDRESS_INFO_S) * (*count)));
151
152         contacts_record_h personNumber = NULL;
153
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);
162                 }
163
164                 contacts_list_next(personNumbers);
165                 index++;
166         }
167
168         contacts_list_destroy(personNumbers, true);
169
170 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
171         MSG_END();
172
173         return MSG_SUCCESS;
174 }