1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <MsgProxyContact.h>
#include <MsgContact.h>
#include <MsgDebug.h>
#ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
extern "C"
{
#include <contacts.h>
}
#endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
/*==================================================================================================
VARIABLES
==================================================================================================*/
__thread bool isContactSvcConnected = false;
/*==================================================================================================
FUNCTION IMPLEMENTATION
==================================================================================================*/
msg_error_t MsgOpenContactSvc()
{
#ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
int errCode = CONTACTS_ERROR_NONE;
if (!isContactSvcConnected) {
errCode = contacts_connect();
if (errCode == CONTACTS_ERROR_NONE) {
MSG_DEBUG("Connect to Contact Service Success");
isContactSvcConnected = true;
} else {
MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
isContactSvcConnected = false;
return MSG_ERR_DB_CONNECT;
}
} else {
MSG_DEBUG("Already connected to Contact Service.");
}
#endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
return MSG_SUCCESS;
}
msg_error_t MsgCloseContactSvc()
{
#ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
int errCode = CONTACTS_ERROR_NONE;
if (isContactSvcConnected) {
errCode = contacts_disconnect();
if (errCode == CONTACTS_ERROR_NONE) {
MSG_DEBUG("Disconnect to Contact Service Success");
isContactSvcConnected = false;
} else {
MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
return MSG_ERR_DB_DISCONNECT;
}
}
#endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
return MSG_SUCCESS;
}
msg_error_t MsgGetContactSearchList(const char *pSearchVal, MSG_ADDRESS_INFO_S **pAddrInfo, int *count)
{
MSG_BEGIN();
#ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
msg_error_t err = MSG_SUCCESS;
*count = 0;
if (pSearchVal == NULL) {
MSG_DEBUG("pSearchVal is NULL.");
return MSG_ERR_NULL_POINTER;
}
if (pAddrInfo == NULL) {
MSG_DEBUG("pAddrInfo is NULL.");
return MSG_ERR_NULL_POINTER;
}
if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
MSG_DEBUG("MsgOpenContactSvc fail.");
return err;
}
if (!isContactSvcConnected) {
MSG_DEBUG("Contact Service Not Opened.");
return MSG_ERR_UNKNOWN;
}
MSG_SEC_DEBUG("pSearchVal [%s]", pSearchVal);
int ret = 0;
unsigned int index = 0;
contacts_query_h query = NULL;
contacts_filter_h filter = NULL;
contacts_list_h personNumbers = NULL;
ret = contacts_query_create(_contacts_person_number._uri, &query);
ret = contacts_filter_create(_contacts_person_number._uri, &filter);
ret = contacts_filter_add_str(filter, _contacts_person_number.display_name, CONTACTS_MATCH_CONTAINS, pSearchVal);
ret = contacts_query_set_filter(query, filter);
ret = contacts_db_get_records_with_query(query, 0, 0, &personNumbers);
if (ret != CONTACTS_ERROR_NONE) {
MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
contacts_query_destroy(query);
contacts_filter_destroy(filter);
contacts_list_destroy(personNumbers, true);
return MSG_ERR_UNKNOWN;
}
ret = contacts_list_get_count(personNumbers, count);
if (*count == 0 || ret != CONTACTS_ERROR_NONE) {
MSG_DEBUG("No Serach Data from Contact Service.");
*count = 0;
contacts_query_destroy(query);
contacts_filter_destroy(filter);
contacts_list_destroy(personNumbers, true);
return MSG_SUCCESS;
}
contacts_query_destroy(query);
contacts_filter_destroy(filter);
MSG_DEBUG(" *count [%d]", *count);
*pAddrInfo = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * (*count)];
memset(*pAddrInfo, 0x00, (sizeof(MSG_ADDRESS_INFO_S) * (*count)));
contacts_record_h personNumber = NULL;
while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(personNumbers, &personNumber)) {
char* normalizedNumber = NULL;
ret = contacts_record_get_str(personNumber, _contacts_person_number.normalized_number, &normalizedNumber);
if (ret != CONTACTS_ERROR_NONE) {
MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
} else if (normalizedNumber) {
MSG_DEBUG("normalizedNumber [%s]", normalizedNumber);
strncpy((*pAddrInfo)[index].addressVal, normalizedNumber, MAX_ADDRESS_VAL_LEN);
}
contacts_list_next(personNumbers);
index++;
}
contacts_list_destroy(personNumbers, true);
#endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
MSG_END();
return MSG_SUCCESS;
}
|