2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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.
19 #include <contacts-svc.h>
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgContact.h"
28 /*==================================================================================================
30 ==================================================================================================*/
31 static bool isContactSvcOpened = false;
33 MsgDbHandler ContactDbHandle;
35 MsgContactChangeCB cbFunction = NULL;
36 /*==================================================================================================
37 FUNCTION IMPLEMENTATION
38 ==================================================================================================*/
39 static void MsgContactSvcCallback(void *pData)
41 MSG_DEBUG("Contact Data is Changed!!!");
45 if (ContactDbHandle.disconnect() != MSG_SUCCESS)
46 MSG_DEBUG("DB Disconnect Fail");
50 msg_error_t MsgOpenContactSvc()
52 int errCode = CTS_SUCCESS;
54 if (!isContactSvcOpened) {
55 errCode = contacts_svc_connect();
57 if (errCode == CTS_SUCCESS) {
58 MSG_DEBUG("Connect to Contact Service Success");
59 isContactSvcOpened = true;
61 MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
62 isContactSvcOpened = false;
63 return MSG_ERR_DB_CONNECT;
66 MSG_DEBUG("Already connected to Contact Service.");
73 msg_error_t MsgCloseContactSvc()
75 int errCode = CTS_SUCCESS;
77 if (isContactSvcOpened) {
78 errCode = contacts_svc_disconnect();
80 if (errCode == CTS_SUCCESS) {
81 MSG_DEBUG("Disconnect to Contact Service Success");
83 MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
84 return MSG_ERR_DB_DISCONNECT;
92 msg_error_t MsgInitContactSvc(MsgContactChangeCB cb)
94 int errCode = CTS_SUCCESS;
99 // Register callback function
100 errCode = contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, MsgContactSvcCallback, NULL);
102 if (errCode == CTS_SUCCESS)
103 MSG_DEBUG("Register Contact Service Callback");
105 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
111 msg_error_t MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
115 MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
117 memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
119 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1)) {
120 MSG_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
121 return MSG_ERR_INVALID_PARAMETER;
126 CTSstruct* contact = NULL;
128 cts_find_op recordType;
130 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL)
131 recordType = CTS_FIND_BY_EMAIL;
133 recordType = CTS_FIND_BY_NUMBER;
135 index = contacts_svc_find_contact_by(recordType, (char*)pAddrInfo->addressVal);
137 if (index > CTS_SUCCESS) {
138 MSG_DEBUG("Index : [%d]", index);
139 ret = contacts_svc_get_contact(index, &contact);
143 MSG_DEBUG("No Contact Info");
147 CTSvalue* name = NULL;
149 ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
151 if (ret != CTS_SUCCESS) {
152 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
156 const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
157 const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
159 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
161 if (strFirstName != NULL)
162 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
164 if (strLastName != NULL)
165 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
167 CTSvalue* base = NULL;
169 ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
171 if (ret != CTS_SUCCESS) {
172 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
176 pContactInfo->contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
178 MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
180 const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
182 if (strImagePath != NULL)
183 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
185 MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
187 contacts_svc_value_free(base);
188 contacts_svc_value_free(name);
190 contacts_svc_struct_free(contact);
198 void MsgSyncContact()
202 int changed_count = 0;
203 int lastSyncTime = 0;
205 /* get contact sync time */
206 lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
208 if (lastSyncTime < 0) {
209 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
215 ret = contacts_svc_get_updated_contacts(-1, lastSyncTime, &pIter);
217 if (ret != CTS_SUCCESS) {
218 MSG_DEBUG("contacts_svc_get_updated_contacts() Error [%d]", ret);
222 while (contacts_svc_iter_next(pIter) == CTS_SUCCESS)
224 CTSvalue *row_info = NULL;
226 row_info = contacts_svc_iter_get_info(pIter);
228 index_num = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
230 MSG_DEBUG("index (%d)", index_num);
232 int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
234 if (type == CTS_OPERATION_UPDATED || type == CTS_OPERATION_INSERTED) {
235 MsgUpdateContact(index_num, type);
237 MSG_DEBUG("Delete Contact");
238 MsgDeleteContact(index_num);
241 if(lastSyncTime < contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT))
242 lastSyncTime = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT);
244 contacts_svc_value_free(row_info);
249 MsgSettingSetInt(CONTACT_SYNC_TIME, lastSyncTime);
250 MSG_DEBUG("lastSyncTime : %d", lastSyncTime);
252 contacts_svc_iter_remove(pIter);
254 if(changed_count > 0)
259 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
261 if (!pNumber || strlen(pNumber) <= 0)
264 if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS) {
265 MSG_DEBUG("Fail to add contact info.");
273 bool MsgUpdateContact(int index, int type)
277 CTSstruct *contact = NULL;
279 ret = contacts_svc_get_contact(index, &contact);
281 if (ret != CTS_SUCCESS) {
282 MSG_DEBUG("contacts_svc_get_contact() Error [%d]", ret);
287 CTSvalue *base = NULL;
289 ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
291 if (ret != CTS_SUCCESS) {
292 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
296 MSG_CONTACT_INFO_S contactInfo = {0};
298 contactInfo.contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
300 MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
302 const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
304 if (strImagePath != NULL)
305 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
307 MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
310 CTSvalue* name = NULL;
312 ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
314 if (ret != CTS_SUCCESS) {
315 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
319 const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
320 const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
322 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
324 if (strFirstName != NULL)
325 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
327 if (strLastName != NULL)
328 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
330 MsgStoClearContactInfo(&ContactDbHandle, index);
332 GSList *get_list, *cursor;
336 contacts_svc_struct_get_list(contact, CTS_CF_NUMBER_LIST, &get_list);
340 // No phone number in contact
341 if (cursor == NULL) {
342 contacts_svc_struct_free(contact);
346 for(; cursor; cursor = g_slist_next(cursor))
348 MSG_DEBUG("Add Contact Data");
350 const char* strNumber = contacts_svc_value_get_str((CTSvalue*)cursor->data, CTS_NUM_VAL_NUMBER_STR);
352 MSG_DEBUG("Number = %s", strNumber);
354 if (MsgInsertContact(&contactInfo, strNumber) == false)
358 MsgStoSetConversationDisplayName(&ContactDbHandle, index);
360 contacts_svc_struct_free(contact);
366 bool MsgDeleteContact(int index)
368 if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
375 int MsgGetContactNameOrder()
379 cts_order_type order = CTS_ORDER_NAME_FIRSTLAST;
381 order = contacts_svc_get_order(CTS_ORDER_OF_DISPLAY);
383 if (order == CTS_ORDER_NAME_FIRSTLAST)
385 else if (order == CTS_ORDER_NAME_LASTFIRST)
392 msg_error_t MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
394 msg_error_t err = MSG_SUCCESS;
396 MSG_DEBUG("folderId [%d], number [%s]", pMsgInfo->folderId, pMsgInfo->addressList[0].addressVal);
400 plog = contacts_svc_value_new(CTS_VALUE_PHONELOG);
402 contacts_svc_value_set_str(plog, CTS_PLOG_VAL_NUMBER_STR, (char*)pMsgInfo->addressList[0].addressVal);
403 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TIME_INT, (int)time(NULL));
406 memset(strText, 0x00, sizeof(strText));
408 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
409 strncpy(strText, pMsgInfo->msgText, 100);
410 MSG_DEBUG("msgText : %s", strText);
411 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
412 if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
413 strncpy(strText, pMsgInfo->subject, 100);
414 MSG_DEBUG("subject : %s", strText);
416 strncpy(strText, pMsgInfo->msgText, 100);
417 MSG_DEBUG("msgText : %s", strText);
421 contacts_svc_value_set_str(plog, CTS_PLOG_VAL_SHORTMSG_STR, strText);
422 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_MSGID_INT, (int)pMsgInfo->msgId);
424 if (pMsgInfo->folderId == MSG_INBOX_ID) {
425 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
426 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_SMS_INCOMMING);
427 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
428 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_MMS_INCOMMING);
429 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
430 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
431 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_SMS_OUTGOING);
432 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
433 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_MMS_OUTGOING);
434 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
435 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
436 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_SMS_BLOCKED);
437 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
438 contacts_svc_value_set_int(plog, CTS_PLOG_VAL_LOG_TYPE_INT, CTS_PLOG_TYPE_MMS_BLOCKED);
441 int ret = contacts_svc_insert_phonelog(plog);
443 if (ret != CTS_SUCCESS)
444 MSG_DEBUG("contacts_svc_insert_phonelog() Failed!!! [%d]", ret);
446 contacts_svc_value_free(plog);
452 msg_error_t MsgDeletePhoneLog(msg_message_id_t msgId)
454 msg_error_t err = MSG_SUCCESS;
456 MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
458 int ret = contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_MSGID, msgId);
460 if (ret != CTS_SUCCESS)
461 MSG_DEBUG("contacts_svc_delete_phonelog() Failed!!! [%d]", ret);
467 int MsgContactSVCBeginTrans()
469 return contacts_svc_begin_trans();
473 int MsgContactSVCEndTrans(bool bSuccess)
475 return contacts_svc_end_trans(bSuccess);