2 * Copyright 2012-2013 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://floralicense.org
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.
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgContact.h"
28 /*==================================================================================================
30 ==================================================================================================*/
31 __thread bool isContactSvcConnected = false;
33 MsgDbHandler ContactDbHandle;
35 MsgContactChangeCB cbFunction = NULL;
36 /*==================================================================================================
37 FUNCTION IMPLEMENTATION
38 ==================================================================================================*/
39 static void MsgContactSvcCallback(const char *view_uri, void *user_data)
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 = CONTACTS_ERROR_NONE;
54 if (!isContactSvcConnected) {
55 errCode = contacts_connect2();
57 if (errCode == CONTACTS_ERROR_NONE) {
58 MSG_DEBUG("Connect to Contact Service Success");
59 isContactSvcConnected = true;
61 MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
62 isContactSvcConnected = false;
63 return MSG_ERR_DB_CONNECT;
66 MSG_DEBUG("Already connected to Contact Service.");
73 msg_error_t MsgCloseContactSvc()
75 int errCode = CONTACTS_ERROR_NONE;
77 if (isContactSvcConnected) {
78 errCode = contacts_disconnect2();
80 if (errCode == CONTACTS_ERROR_NONE) {
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 = CONTACTS_ERROR_NONE;
96 if (!isContactSvcConnected) {
97 MSG_DEBUG("Contact Service Not Opened.");
98 return MSG_ERR_UNKNOWN;
104 // Register callback function
105 errCode = contacts_db_add_changed_cb(_contacts_contact._uri, MsgContactSvcCallback, NULL);
107 if (errCode == CONTACTS_ERROR_NONE)
108 MSG_DEBUG("Register Contact Service Callback");
110 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
116 msg_error_t MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
120 MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
122 memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
124 if (!isContactSvcConnected) {
125 MSG_DEBUG("Contact Service Not Opened.");
126 return MSG_ERR_UNKNOWN;
129 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1)) {
130 MSG_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
136 unsigned int count = 0;
137 contacts_query_h query = NULL;
138 contacts_filter_h filter = NULL;
139 contacts_list_h contacts = NULL;
141 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
142 ret = contacts_query_create(_contacts_contact_number._uri, &query);
143 ret = contacts_filter_create(_contacts_contact_number._uri, &filter);
145 ret = contacts_filter_add_str(filter, _contacts_contact_number.number_filter, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
147 } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
148 ret = contacts_query_create(_contacts_contact_email._uri, &query);
149 ret = contacts_filter_create(_contacts_contact_email._uri, &filter);
151 ret = contacts_filter_add_str(filter, _contacts_contact_email.email, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
154 MSG_DEBUG("Invalid pAddrInfo->addressType.");
158 ret = contacts_query_set_filter(query, filter);
159 ret = contacts_db_get_records_with_query(query, 0, 1, &contacts);
160 if (ret != CONTACTS_ERROR_NONE) {
161 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
162 contacts_query_destroy(query);
163 contacts_filter_destroy(filter);
164 contacts_list_destroy(contacts, true);
168 ret = contacts_list_get_count(contacts, &count);
170 if (count == 0 || ret != CONTACTS_ERROR_NONE) {
171 MSG_DEBUG("No Serach Data from Contact Service.");
172 contacts_query_destroy(query);
173 contacts_filter_destroy(filter);
174 contacts_list_destroy(contacts, true);
178 contacts_record_h contact = NULL;
180 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
181 contacts_record_h number = NULL;
183 ret = contacts_list_get_current_record_p(contacts, &number);
184 if (ret != CONTACTS_ERROR_NONE) {
185 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
186 contacts_list_destroy(contacts, true);
190 ret = contacts_record_get_int(number, _contacts_contact_number.contact_id, &index);
191 if (ret != CONTACTS_ERROR_NONE) {
192 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
193 contacts_list_destroy(contacts, true);
194 contacts_record_destroy(number, true);
198 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
199 if (ret != CONTACTS_ERROR_NONE) {
200 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
201 contacts_list_destroy(contacts, true);
202 contacts_record_destroy(contact, true);
203 contacts_record_destroy(number, true);
206 } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
207 contacts_record_h email = NULL;
209 ret = contacts_list_get_current_record_p(contacts, &email);
210 if (ret != CONTACTS_ERROR_NONE) {
211 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
212 contacts_list_destroy(contacts, true);
216 ret = contacts_record_get_int(email, _contacts_contact_email.contact_id, &index);
217 if (ret != CONTACTS_ERROR_NONE) {
218 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
219 contacts_list_destroy(contacts, true);
220 contacts_record_destroy(email, true);
224 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
225 if (ret != CONTACTS_ERROR_NONE) {
226 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
227 contacts_list_destroy(contacts, true);
228 contacts_record_destroy(contact, true);
229 contacts_record_destroy(email, true);
234 contacts_list_destroy(contacts, true);
237 contacts_record_h name = NULL;
239 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
240 if (ret != CONTACTS_ERROR_NONE) {
241 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
242 contacts_record_destroy(contact, true);
246 char* strFirstName = NULL;
247 ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
248 if (ret != CONTACTS_ERROR_NONE) {
249 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
250 contacts_record_destroy(contact, true);
254 char* strLastName = NULL;
255 ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
256 if (ret != CONTACTS_ERROR_NONE) {
257 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
258 contacts_record_destroy(contact, true);
262 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
264 if (strFirstName != NULL)
265 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
267 if (strLastName != NULL)
268 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
270 ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&pContactInfo->contactId);
271 if (ret != CONTACTS_ERROR_NONE) {
272 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
273 contacts_record_destroy(contact, true);
277 MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
279 char* strImagePath = NULL;
280 ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
281 if (ret != CONTACTS_ERROR_NONE) {
282 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
283 contacts_record_destroy(contact, true);
287 if (strImagePath != NULL)
288 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
290 MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
292 contacts_record_destroy(contact, true);
300 void MsgSyncContact()
303 unsigned int changed_count = 0;
304 int lastSyncTime = 0;
305 int finalSyncTime = 0;
307 /* get contact sync time */
308 lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
310 if (lastSyncTime < 0) {
311 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
315 contacts_list_h contactsList = NULL;
317 ret = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, -1, lastSyncTime, &contactsList, &finalSyncTime);
319 if (ret != CONTACTS_ERROR_NONE) {
320 MSG_DEBUG("contacts_db_get_changes_by_version() Error [%d]", ret);
324 ret = contacts_list_get_count(contactsList, &changed_count);
326 if (ret != CONTACTS_ERROR_NONE) {
327 MSG_DEBUG("contacts_list_get_count() Error [%d]", ret);
328 contacts_list_destroy(contactsList, true);
332 for (unsigned int i = 0; i < changed_count; i++)
336 contacts_record_h event = NULL;
338 ret = contacts_list_get_current_record_p(contactsList, &event);
339 if (ret != CONTACTS_ERROR_NONE) {
340 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
341 contacts_list_destroy(contactsList, true);
345 ret = contacts_record_get_int(event, _contacts_contact_updated_info.contact_id, &index_num);
346 if (ret != CONTACTS_ERROR_NONE) {
347 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
348 contacts_list_destroy(contactsList, true);
352 MSG_DEBUG("index (%d)", index_num);
354 ret = contacts_record_get_int(event, _contacts_contact_updated_info.type, &type);
355 if (ret != CONTACTS_ERROR_NONE) {
356 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
357 contacts_list_destroy(contactsList, true);
361 if (type == CONTACTS_CHANGE_UPDATED || type == CONTACTS_CHANGE_INSERTED) {
362 MsgUpdateContact(index_num, type);
364 MSG_DEBUG("Delete Contact");
365 MsgDeleteContact(index_num);
368 ret = contacts_list_next(contactsList);
369 if (ret != CONTACTS_ERROR_NONE) {
370 MSG_DEBUG("contacts_list_next() Error [%d]", ret);
374 if(MsgSettingSetInt(CONTACT_SYNC_TIME, finalSyncTime) != MSG_SUCCESS)
375 MSG_DEBUG("MsgSettingSetInt fail : CONTACT_SYNC_TIME");
376 MSG_DEBUG("lastSyncTime : %d", finalSyncTime);
378 contacts_list_destroy(contactsList, true);
380 if(changed_count > 0)
385 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
387 if (!pNumber || strlen(pNumber) <= 0)
390 if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS) {
391 MSG_DEBUG("Fail to add contact info.");
399 bool MsgUpdateContact(int index, int type)
401 int ret = CONTACTS_ERROR_NONE;
403 contacts_record_h contact = NULL;
405 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
406 if (ret != CONTACTS_ERROR_NONE) {
407 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
408 contacts_record_destroy(contact, true);
412 MSG_CONTACT_INFO_S contactInfo;
413 memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
415 ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&contactInfo.contactId);
416 if (ret != CONTACTS_ERROR_NONE) {
417 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
418 contacts_record_destroy(contact, true);
422 MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
424 char* strImagePath = NULL;
425 ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
426 if (ret != CONTACTS_ERROR_NONE) {
427 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
428 contacts_record_destroy(contact, true);
432 if (strImagePath != NULL)
433 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
435 MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
438 contacts_record_h name = NULL;
440 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
441 if (ret != CONTACTS_ERROR_NONE) {
442 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
443 contacts_record_destroy(contact, true);
447 char* strFirstName = NULL;
448 ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
449 if (ret != CONTACTS_ERROR_NONE) {
450 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
451 contacts_record_destroy(contact, true);
455 char* strLastName = NULL;
456 ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
457 if (ret != CONTACTS_ERROR_NONE) {
458 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
459 contacts_record_destroy(contact, true);
463 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
465 if (strFirstName != NULL)
466 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
468 if (strLastName != NULL)
469 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
471 MsgStoClearContactInfo(&ContactDbHandle, index);
473 unsigned int count = 0;
474 ret = contacts_record_get_child_record_count(contact, _contacts_contact.number, &count);
475 if (ret != CONTACTS_ERROR_NONE) {
476 MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
477 contacts_record_destroy(contact, true);
482 for(unsigned int i=0; i < count; i++)
484 MSG_DEBUG("Add Contact Data");
486 contacts_record_h number = NULL;
488 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &number);
489 if (ret != CONTACTS_ERROR_NONE) {
490 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
491 contacts_record_destroy(contact, true);
495 char* strNumber = NULL;
496 ret = contacts_record_get_str_p(number, _contacts_number.number, &strNumber);
497 if (ret != CONTACTS_ERROR_NONE) {
498 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
499 contacts_record_destroy(contact, true);
503 if (strNumber != NULL) {
504 MSG_DEBUG("Number = %s", strNumber);
505 if (!MsgInsertContact(&contactInfo, strNumber)) {
506 MSG_DEBUG("MsgInsertContact fail.");
510 } else {// No phone number in contact
511 contacts_record_destroy(contact, true);
515 MsgStoSetConversationDisplayName(&ContactDbHandle, index);
517 contacts_record_destroy(contact, true);
523 bool MsgDeleteContact(int index)
525 if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
532 int MsgGetContactNameOrder()
535 if (!isContactSvcConnected) {
536 MSG_DEBUG("Contact Service Not Opened.");
537 return 0; // return default value : FIRSTLAST
540 int ret = CONTACTS_ERROR_NONE;
542 contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
544 ret = contacts_setting_get_name_display_order(&order);
545 if (ret != CONTACTS_ERROR_NONE) {
546 MSG_DEBUG("contacts_setting_get_name_display_order() Error [%d]", ret);
550 if (order == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST)
557 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
559 if (!isContactSvcConnected) {
560 MSG_DEBUG("Contact Service Not Opened.");
564 if(pMsgInfo->nAddressCnt < 1) {
565 MSG_DEBUG("address count is [%d]", pMsgInfo->nAddressCnt);
569 for (int i = 0; pMsgInfo->nAddressCnt > i; i++) {
571 contacts_record_h plog = NULL;
573 ret = contacts_record_create(_contacts_phone_log._uri, &plog);
574 if (ret != CONTACTS_ERROR_NONE) {
575 MSG_DEBUG("contacts_record_create() Error [%d]", ret);
576 contacts_record_destroy(plog, true);
580 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[i].addressVal);
581 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
584 memset(strText, 0x00, sizeof(strText));
586 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
587 strncpy(strText, pMsgInfo->msgText, 100);
588 MSG_DEBUG("msgText : %s", strText);
589 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
590 if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
591 strncpy(strText, pMsgInfo->subject, 100);
592 MSG_DEBUG("subject : %s", strText);
594 strncpy(strText, pMsgInfo->msgText, 100);
595 MSG_DEBUG("msgText : %s", strText);
599 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
600 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
602 if (pMsgInfo->folderId == MSG_INBOX_ID) {
603 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
604 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMMING);
605 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
606 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMMING);
607 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
608 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
609 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
610 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
611 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
612 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
613 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
614 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
615 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
616 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
619 ret = contacts_db_insert_record(plog, NULL);
620 if (ret != CONTACTS_ERROR_NONE) {
621 MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
624 contacts_record_destroy(plog, true);
629 void MsgDeletePhoneLog(msg_message_id_t msgId)
631 MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
633 if (!isContactSvcConnected) {
634 MSG_DEBUG("Contact Service Not Opened.");
638 int ret = CONTACTS_ERROR_NONE;
640 unsigned int count = 0;
641 contacts_query_h query;
642 contacts_filter_h filter;
643 contacts_list_h plogs = NULL;
645 ret = contacts_query_create(_contacts_phone_log._uri, &query);
646 ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
648 ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
651 ret = contacts_query_set_filter(query, filter);
652 ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
654 ret = contacts_list_get_count(plogs, &count);
657 MSG_DEBUG("No Serach Data from Contact Service.");
659 contacts_record_h plog = NULL;
661 ret = contacts_list_get_current_record_p(plogs, &plog);
662 if (ret != CONTACTS_ERROR_NONE) {
663 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
666 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
667 if (ret != CONTACTS_ERROR_NONE) {
668 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
671 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
672 if (ret != CONTACTS_ERROR_NONE) {
673 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
675 MSG_DEBUG("contacts_db_delete_record() Success.");
679 contacts_query_destroy(query);
680 contacts_filter_destroy(filter);
681 contacts_list_destroy(plogs, true);
686 int MsgContactSVCBeginTrans()
688 //return contacts_svc_begin_trans();
693 int MsgContactSVCEndTrans(bool bSuccess)
695 //return contacts_svc_end_trans(bSuccess);