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.
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(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 (!isContactSvcOpened) {
55 errCode = contacts_connect2();
57 if (errCode == CONTACTS_ERROR_NONE) {
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 = CONTACTS_ERROR_NONE;
77 // if (isContactSvcOpened) {
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;
99 // Register callback function
100 errCode = contacts_db_add_changed_cb(_contacts_contact._uri, MsgContactSvcCallback, NULL);
102 if (errCode == CONTACTS_ERROR_NONE)
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);
126 unsigned int count = 0;
127 contacts_query_h query = NULL;
128 contacts_filter_h filter = NULL;
129 contacts_list_h contacts = NULL;
131 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN) {
132 ret = contacts_query_create(_contacts_contact_number._uri, &query);
133 ret = contacts_filter_create(_contacts_contact_number._uri, &filter);
135 ret = contacts_filter_add_str(filter, _contacts_contact_number.number_filter, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
137 } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
138 ret = contacts_query_create(_contacts_contact_email._uri, &query);
139 ret = contacts_filter_create(_contacts_contact_email._uri, &filter);
141 ret = contacts_filter_add_str(filter, _contacts_contact_email.email, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
144 MSG_DEBUG("Invalid pAddrInfo->addressType.");
148 ret = contacts_query_set_filter(query, filter);
149 ret = contacts_db_get_records_with_query(query, 0, 1, &contacts);
150 if (ret != CONTACTS_ERROR_NONE) {
151 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
152 contacts_query_destroy(query);
153 contacts_filter_destroy(filter);
154 contacts_list_destroy(contacts, true);
158 ret = contacts_list_get_count(contacts, &count);
160 if (count == 0 || ret != CONTACTS_ERROR_NONE) {
161 MSG_DEBUG("No Serach Data from Contact Service.");
162 contacts_query_destroy(query);
163 contacts_filter_destroy(filter);
164 contacts_list_destroy(contacts, true);
168 contacts_record_h contact = NULL;
170 if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN) {
171 contacts_record_h number = NULL;
173 ret = contacts_list_get_current_record_p(contacts, &number);
174 if (ret != CONTACTS_ERROR_NONE) {
175 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
176 contacts_list_destroy(contacts, true);
180 ret = contacts_record_get_int(number, _contacts_contact_number.contact_id, &index);
181 if (ret != CONTACTS_ERROR_NONE) {
182 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
183 contacts_list_destroy(contacts, true);
184 contacts_record_destroy(number, true);
188 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
189 if (ret != CONTACTS_ERROR_NONE) {
190 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
191 contacts_list_destroy(contacts, true);
192 contacts_record_destroy(contact, true);
193 contacts_record_destroy(number, true);
196 } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
197 contacts_record_h email = NULL;
199 ret = contacts_list_get_current_record_p(contacts, &email);
200 if (ret != CONTACTS_ERROR_NONE) {
201 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
202 contacts_list_destroy(contacts, true);
206 ret = contacts_record_get_int(email, _contacts_contact_email.contact_id, &index);
207 if (ret != CONTACTS_ERROR_NONE) {
208 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
209 contacts_list_destroy(contacts, true);
210 contacts_record_destroy(email, true);
214 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
215 if (ret != CONTACTS_ERROR_NONE) {
216 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
217 contacts_list_destroy(contacts, true);
218 contacts_record_destroy(contact, true);
219 contacts_record_destroy(email, true);
224 contacts_list_destroy(contacts, true);
227 contacts_record_h name = NULL;
229 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
230 if (ret != CONTACTS_ERROR_NONE) {
231 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
232 contacts_record_destroy(contact, true);
236 char* strFirstName = NULL;
237 ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
238 if (ret != CONTACTS_ERROR_NONE) {
239 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
240 contacts_record_destroy(contact, true);
244 char* strLastName = NULL;
245 ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
246 if (ret != CONTACTS_ERROR_NONE) {
247 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
248 contacts_record_destroy(contact, true);
252 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
254 if (strFirstName != NULL)
255 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
257 if (strLastName != NULL)
258 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
260 ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&pContactInfo->contactId);
261 if (ret != CONTACTS_ERROR_NONE) {
262 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
263 contacts_record_destroy(contact, true);
267 MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
269 char* strImagePath = NULL;
270 ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
271 if (ret != CONTACTS_ERROR_NONE) {
272 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
273 contacts_record_destroy(contact, true);
277 if (strImagePath != NULL)
278 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
280 MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
282 contacts_record_destroy(contact, true);
290 void MsgSyncContact()
293 unsigned int changed_count = 0;
294 int lastSyncTime = 0;
295 int finalSyncTime = 0;
297 /* get contact sync time */
298 lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
300 if (lastSyncTime < 0) {
301 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
305 contacts_list_h contactsList = NULL;
307 ret = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, -1, lastSyncTime, &contactsList, &finalSyncTime);
309 if (ret != CONTACTS_ERROR_NONE) {
310 MSG_DEBUG("contacts_db_get_changes_by_version() Error [%d]", ret);
314 ret = contacts_list_get_count(contactsList, &changed_count);
316 if (ret != CONTACTS_ERROR_NONE) {
317 MSG_DEBUG("contacts_list_get_count() Error [%d]", ret);
318 contacts_list_destroy(contactsList, true);
322 for (unsigned int i = 0; i < changed_count; i++);
326 contacts_record_h event = NULL;
328 ret = contacts_list_get_current_record_p(contactsList, &event);
329 if (ret != CONTACTS_ERROR_NONE) {
330 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
331 contacts_list_destroy(contactsList, true);
335 ret = contacts_record_get_int(event, _contacts_contact_updated_info.contact_id, &index_num);
336 if (ret != CONTACTS_ERROR_NONE) {
337 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
338 contacts_list_destroy(contactsList, true);
342 MSG_DEBUG("index (%d)", index_num);
344 ret = contacts_record_get_int(event, _contacts_contact_updated_info.type, &type);
345 if (ret != CONTACTS_ERROR_NONE) {
346 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
347 contacts_list_destroy(contactsList, true);
351 if (type == CONTACTS_CHANGE_UPDATED || type == CONTACTS_CHANGE_INSERTED) {
352 MsgUpdateContact(index_num, type);
354 MSG_DEBUG("Delete Contact");
355 MsgDeleteContact(index_num);
358 ret = contacts_list_next(contactsList);
359 if (ret != CONTACTS_ERROR_NONE) {
360 MSG_DEBUG("contacts_list_next() Error [%d]", ret);
364 MsgSettingSetInt(CONTACT_SYNC_TIME, finalSyncTime);
365 MSG_DEBUG("lastSyncTime : %d", finalSyncTime);
367 contacts_list_destroy(contactsList, true);
369 if(changed_count > 0)
374 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
376 if (!pNumber || strlen(pNumber) <= 0)
379 if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS) {
380 MSG_DEBUG("Fail to add contact info.");
388 bool MsgUpdateContact(int index, int type)
390 int ret = CONTACTS_ERROR_NONE;
392 contacts_record_h contact = NULL;
394 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
395 if (ret != CONTACTS_ERROR_NONE) {
396 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
397 contacts_record_destroy(contact, true);
401 MSG_CONTACT_INFO_S contactInfo;
402 memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
404 ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&contactInfo.contactId);
405 if (ret != CONTACTS_ERROR_NONE) {
406 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
407 contacts_record_destroy(contact, true);
411 MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
413 char* strImagePath = NULL;
414 ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
415 if (ret != CONTACTS_ERROR_NONE) {
416 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
417 contacts_record_destroy(contact, true);
421 if (strImagePath != NULL)
422 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
424 MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
427 contacts_record_h name = NULL;
429 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
430 if (ret != CONTACTS_ERROR_NONE) {
431 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
432 contacts_record_destroy(contact, true);
436 char* strFirstName = NULL;
437 ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
438 if (ret != CONTACTS_ERROR_NONE) {
439 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
440 contacts_record_destroy(contact, true);
444 char* strLastName = NULL;
445 ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
446 if (ret != CONTACTS_ERROR_NONE) {
447 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
448 contacts_record_destroy(contact, true);
452 MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
454 if (strFirstName != NULL)
455 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
457 if (strLastName != NULL)
458 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
460 MsgStoClearContactInfo(&ContactDbHandle, index);
462 unsigned int count = 0;
463 ret = contacts_record_get_child_record_count(contact, _contacts_contact.number, &count);
464 if (ret != CONTACTS_ERROR_NONE) {
465 MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
466 contacts_record_destroy(contact, true);
471 for(unsigned int i=0; i < count; i++)
473 MSG_DEBUG("Add Contact Data");
475 contacts_record_h number = NULL;
477 ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &number);
478 if (ret != CONTACTS_ERROR_NONE) {
479 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
480 contacts_record_destroy(contact, true);
484 char* strNumber = NULL;
485 ret = contacts_record_get_str_p(number, _contacts_number.number, &strNumber);
486 if (ret != CONTACTS_ERROR_NONE) {
487 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
488 contacts_record_destroy(contact, true);
492 if (strNumber != NULL) {
493 MSG_DEBUG("Number = %s", strNumber);
494 if (!MsgInsertContact(&contactInfo, strNumber)) {
495 MSG_DEBUG("MsgInsertContact fail.");
499 } else {// No phone number in contact
500 contacts_record_destroy(contact, true);
504 MsgStoSetConversationDisplayName(&ContactDbHandle, index);
506 contacts_record_destroy(contact, true);
512 bool MsgDeleteContact(int index)
514 if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
521 int MsgGetContactNameOrder()
523 int ret = CONTACTS_ERROR_NONE;
525 contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
527 ret = contacts_setting_get_name_display_order(&order);
528 if (ret != CONTACTS_ERROR_NONE) {
529 MSG_DEBUG("contacts_setting_get_name_display_order() Error [%d]", ret);
533 if (order == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST)
540 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
544 MSG_DEBUG("folderId [%d], number [%s]", pMsgInfo->folderId, pMsgInfo->addressList[0].addressVal);
546 contacts_record_h plog = NULL;
548 ret = contacts_record_create(_contacts_phone_log._uri, &plog);
549 if (ret != CONTACTS_ERROR_NONE) {
550 MSG_DEBUG("contacts_record_create() Error [%d]", ret);
551 contacts_record_destroy(plog, true);
554 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[0].addressVal);
555 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
558 memset(strText, 0x00, sizeof(strText));
560 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
561 strncpy(strText, pMsgInfo->msgText, 100);
562 MSG_DEBUG("msgText : %s", strText);
563 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
564 if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
565 strncpy(strText, pMsgInfo->subject, 100);
566 MSG_DEBUG("subject : %s", strText);
568 strncpy(strText, pMsgInfo->msgText, 100);
569 MSG_DEBUG("msgText : %s", strText);
573 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
574 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
576 if (pMsgInfo->folderId == MSG_INBOX_ID) {
577 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
578 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMMING);
579 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
580 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMMING);
581 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
582 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
583 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
584 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
585 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
586 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
587 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
588 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
589 else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
590 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
593 ret = contacts_db_insert_record(plog, NULL);
594 if (ret != CONTACTS_ERROR_NONE) {
595 MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
598 contacts_record_destroy(plog, true);
602 void MsgDeletePhoneLog(msg_message_id_t msgId)
604 MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
606 int ret = CONTACTS_ERROR_NONE;
608 unsigned int count = 0;
609 contacts_query_h query;
610 contacts_filter_h filter;
611 contacts_list_h plogs = NULL;
613 ret = contacts_query_create(_contacts_phone_log._uri, &query);
614 ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
616 ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
619 ret = contacts_query_set_filter(query, filter);
620 ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
622 ret = contacts_list_get_count(plogs, &count);
625 MSG_DEBUG("No Serach Data from Contact Service.");
627 contacts_record_h plog = NULL;
629 ret = contacts_list_get_current_record_p(plogs, &plog);
630 if (ret != CONTACTS_ERROR_NONE) {
631 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
634 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
635 if (ret != CONTACTS_ERROR_NONE) {
636 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
639 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
640 if (ret != CONTACTS_ERROR_NONE) {
641 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
643 MSG_DEBUG("contacts_db_delete_record() Success.");
647 contacts_query_destroy(query);
648 contacts_filter_destroy(filter);
649 contacts_list_destroy(plogs, true);
654 int MsgContactSVCBeginTrans()
656 //return contacts_svc_begin_trans();
661 int MsgContactSVCEndTrans(bool bSuccess)
663 //return contacts_svc_end_trans(bSuccess);