3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
5 * This file is part of msg-service.
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 * Sangkoo Kim <sangkoo.kim@samsung.com>
9 * Seunghwan Lee <sh.cat.lee@samsung.com>
10 * SoonMin Jung <sm0415.jung@samsung.com>
11 * Jae-Young Lee <jy4710.lee@samsung.com>
12 * KeeBum Kim <keebum.kim@samsung.com>
14 * PROPRIETARY/CONFIDENTIAL
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
33 #include <contacts-svc.h>
37 #include "MsgUtilStorage.h"
38 #include "MsgGconfWrapper.h"
39 #include "MsgContact.h"
42 /*==================================================================================================
44 ==================================================================================================*/
45 static bool isContactSvcOpened = false;
47 MsgDbHandler ContactDbHandle;
49 MsgContactChangeCB cbFunction = NULL;
50 /*==================================================================================================
51 FUNCTION IMPLEMENTATION
52 ==================================================================================================*/
53 static void MsgContactSvcCallback(void *pData)
55 MSG_DEBUG("Contact Data is Changed!!!");
59 if (ContactDbHandle.disconnect() != MSG_SUCCESS)
61 MSG_DEBUG("DB Disconnect Fail");
66 MSG_ERROR_T MsgOpenContactSvc()
68 int errCode = CTS_SUCCESS;
70 if(!isContactSvcOpened)
72 errCode = contacts_svc_connect();
74 if (errCode == CTS_SUCCESS)
76 MSG_DEBUG("Connect to Contact Service Success");
77 isContactSvcOpened = true;
81 MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
82 isContactSvcOpened = false;
83 return MSG_ERR_DB_CONNECT;
88 MSG_DEBUG("Already connected to Contact Service.");
95 MSG_ERROR_T MsgCloseContactSvc()
97 int errCode = CTS_SUCCESS;
99 if(isContactSvcOpened)
101 errCode = contacts_svc_disconnect();
103 if (errCode == CTS_SUCCESS)
105 MSG_DEBUG("Disconnect to Contact Service Success");
109 MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
110 return MSG_ERR_DB_DISCONNECT;
118 MSG_ERROR_T MsgInitContactSvc(MsgContactChangeCB cb)
120 int errCode = CTS_SUCCESS;
125 // Register callback function
126 errCode = contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, MsgContactSvcCallback, NULL);
128 if (errCode == CTS_SUCCESS)
130 MSG_DEBUG("Register Contact Service Callback");
134 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
141 MSG_ERROR_T MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
145 MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
147 memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
149 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1))
151 MSG_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
152 return MSG_ERR_INVALID_PARAMETER;
157 CTSstruct* contact = NULL;
159 cts_find_op recordType;
161 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL)
162 recordType = CTS_FIND_BY_EMAIL;
164 recordType = CTS_FIND_BY_NUMBER;
166 index = contacts_svc_find_contact_by(recordType, (char*)pAddrInfo->addressVal);
168 if (index > CTS_SUCCESS)
170 MSG_DEBUG("Index : [%d]", index);
171 ret = contacts_svc_get_contact(index, &contact);
176 MSG_DEBUG("No Contact Info");
180 CTSvalue* name = NULL;
182 ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
184 if (ret != CTS_SUCCESS)
186 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
190 const char* strDisplayName = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);
191 const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
192 const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
194 MSG_DEBUG("Display Name : [%s], First Name : [%s], Last Name : [%s]", strDisplayName, strFirstName, strLastName);
196 if (strDisplayName != NULL)
198 strncpy(pContactInfo->displayName, strDisplayName, MAX_DISPLAY_NAME_LEN);
201 if (strFirstName != NULL)
203 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
206 if (strLastName != NULL)
208 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
211 CTSvalue* base = NULL;
213 ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
215 if (ret != CTS_SUCCESS)
217 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
221 pContactInfo->contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
223 MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
225 const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
227 if (strImagePath != NULL)
229 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
232 MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
234 contacts_svc_value_free(base);
235 contacts_svc_value_free(name);
237 contacts_svc_struct_free(contact);
245 void MsgSyncContact()
249 int changed_count = 0;
250 int lastSyncTime = 0;
252 /* get contact sync time */
253 lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
255 if (lastSyncTime < 0) {
256 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
262 ret = contacts_svc_get_updated_contacts(0, lastSyncTime, &pIter);
264 if (ret != CTS_SUCCESS)
266 MSG_DEBUG("contacts_svc_get_updated_contacts() Error [%d]", ret);
270 while (contacts_svc_iter_next(pIter) == CTS_SUCCESS)
272 CTSvalue *row_info = NULL;
274 row_info = contacts_svc_iter_get_info(pIter);
276 index_num = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
278 MSG_DEBUG("index (%d)", index_num);
280 int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
282 if (type == CTS_OPERATION_UPDATED || type == CTS_OPERATION_INSERTED)
284 MsgUpdateContact(index_num, type);
288 MSG_DEBUG("Delete Contact");
290 MsgDeleteContact(index_num);
293 if(lastSyncTime < contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT))
294 lastSyncTime = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT);
296 contacts_svc_value_free(row_info);
302 MsgSettingSetInt(CONTACT_SYNC_TIME, lastSyncTime);
303 MSG_DEBUG("lastSyncTime : %d", lastSyncTime);
305 contacts_svc_iter_remove(pIter);
307 if(changed_count > 0)
312 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
314 if (!pNumber || strlen(pNumber) <= 0)
317 if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS)
319 MSG_DEBUG("Fail to add contact info.");
327 bool MsgUpdateContact(int index, int type)
331 CTSstruct *contact = NULL;
333 ret = contacts_svc_get_contact(index, &contact);
335 if (ret != CTS_SUCCESS)
337 MSG_DEBUG("contacts_svc_get_contact() Error [%d]", ret);
342 CTSvalue *base = NULL;
344 ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
346 if (ret != CTS_SUCCESS)
348 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
352 MSG_CONTACT_INFO_S contactInfo = {0};
354 contactInfo.contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
356 MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
358 const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
360 if (strImagePath != NULL)
362 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
365 MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
368 CTSvalue* name = NULL;
370 ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
372 if (ret != CTS_SUCCESS)
374 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
378 const char* strDisplayName = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);
379 const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
380 const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
382 MSG_DEBUG("Display Name : [%s], First Name : [%s], Last Name : [%s]", strDisplayName, strFirstName, strLastName);
384 if (strDisplayName != NULL)
386 strncpy(contactInfo.displayName, strDisplayName, MAX_DISPLAY_NAME_LEN);
389 if (strFirstName != NULL)
391 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
394 if (strLastName != NULL)
396 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
399 MsgStoClearContactInfo(&ContactDbHandle, index);
401 GSList *get_list, *cursor;
405 contacts_svc_struct_get_list(contact, CTS_CF_NUMBER_LIST, &get_list);
409 // No phone number in contact
412 contacts_svc_struct_free(contact);
417 for(; cursor; cursor = g_slist_next(cursor))
419 MSG_DEBUG("Add Contact Data");
421 const char* strNumber = contacts_svc_value_get_str((CTSvalue*)cursor->data, CTS_NUM_VAL_NUMBER_STR);
423 MSG_DEBUG("Number = %s", strNumber);
425 if (MsgInsertContact(&contactInfo, strNumber) == false)
431 contacts_svc_struct_free(contact);
437 bool MsgDeleteContact(int index)
439 if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
448 int MsgGetContactNameOrder()
452 cts_order_type order = CTS_ORDER_NAME_FIRSTLAST;
454 order = contacts_svc_get_order(CTS_ORDER_OF_DISPLAY);
456 if (order == CTS_ORDER_NAME_FIRSTLAST)
458 else if (order == CTS_ORDER_NAME_LASTFIRST)
465 int MsgContactSVCBeginTrans()
467 return contacts_svc_begin_trans();
471 int MsgContactSVCEndTrans(bool bSuccess)
473 return contacts_svc_end_trans(bSuccess);