Fix Nabi issues. (Email contact sync & Noti icon)
[platform/core/messaging/msg-service.git] / utils / MsgContact.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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
7 *
8 *    http://floralicense.org/license/
9 *
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.
15 */
16
17 extern "C"
18 {
19         #include <contacts.h>
20 }
21
22 #include "MsgDebug.h"
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgContact.h"
26
27
28 /*==================================================================================================
29                                      VARIABLES
30 ==================================================================================================*/
31 __thread bool isContactSvcConnected = false;
32
33 MsgDbHandler ContactDbHandle;
34
35 MsgContactChangeCB cbFunction = NULL;
36 /*==================================================================================================
37                                      FUNCTION IMPLEMENTATION
38 ==================================================================================================*/
39 static void MsgContactSvcCallback(const char *view_uri, void *user_data)
40 {
41         MSG_DEBUG("Contact Data is Changed!!!");
42
43         MsgSyncContact();
44
45         if (ContactDbHandle.disconnect() != MSG_SUCCESS)
46                 MSG_DEBUG("DB Disconnect Fail");
47 }
48
49
50 msg_error_t MsgOpenContactSvc()
51 {
52         int errCode = CONTACTS_ERROR_NONE;
53
54         if (!isContactSvcConnected) {
55                 errCode = contacts_connect2();
56
57                 if (errCode == CONTACTS_ERROR_NONE) {
58                         MSG_DEBUG("Connect to Contact Service Success");
59                         isContactSvcConnected = true;
60                 } else {
61                         MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
62                         isContactSvcConnected = false;
63                         return MSG_ERR_DB_CONNECT;
64                 }
65         } else {
66                 MSG_DEBUG("Already connected to Contact Service.");
67         }
68
69         return MSG_SUCCESS;
70 }
71
72
73 msg_error_t MsgCloseContactSvc()
74 {
75         int errCode = CONTACTS_ERROR_NONE;
76
77         if (isContactSvcConnected) {
78                 errCode = contacts_disconnect2();
79
80                 if (errCode == CONTACTS_ERROR_NONE) {
81                         MSG_DEBUG("Disconnect to Contact Service Success");
82                 } else {
83                         MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
84                         return MSG_ERR_DB_DISCONNECT;
85                 }
86         }
87
88         return MSG_SUCCESS;
89 }
90
91
92 msg_error_t MsgInitContactSvc(MsgContactChangeCB cb)
93 {
94         msg_error_t err = MSG_SUCCESS;
95
96         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
97                 MSG_DEBUG("MsgOpenContactSvc fail.");
98                 return err;
99         }
100
101         int errCode = CONTACTS_ERROR_NONE;
102
103         if (!isContactSvcConnected) {
104                 MSG_DEBUG("Contact Service Not Opened.");
105                 return MSG_ERR_UNKNOWN;
106         }
107
108         if (cb != NULL)
109                 cbFunction = cb;
110
111         // Register callback function
112         errCode = contacts_db_add_changed_cb(_contacts_contact._uri, MsgContactSvcCallback, NULL);
113
114         if (errCode == CONTACTS_ERROR_NONE)
115                 MSG_DEBUG("Register Contact Service Callback");
116         else
117                 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
118
119         return MSG_SUCCESS;
120 }
121
122
123 msg_error_t MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
124 {
125         MSG_BEGIN();
126
127         msg_error_t err = MSG_SUCCESS;
128
129         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
130                 MSG_DEBUG("MsgOpenContactSvc fail.");
131                 return err;
132         }
133
134         if (!isContactSvcConnected) {
135                 MSG_DEBUG("Contact Service Not Opened.");
136                 return MSG_ERR_UNKNOWN;
137         }
138
139         MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
140
141         memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
142
143         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1)) {
144                 MSG_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
145                 return MSG_SUCCESS;
146         }
147
148         int ret = 0;
149         int index = 0;
150         unsigned int count = 0;
151         contacts_query_h query = NULL;
152         contacts_filter_h filter = NULL;
153         contacts_list_h contacts = NULL;
154
155         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
156                 ret = contacts_query_create(_contacts_contact_number._uri, &query);
157                 ret = contacts_filter_create(_contacts_contact_number._uri, &filter);
158
159                 ret = contacts_filter_add_str(filter, _contacts_contact_number.number, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
160
161         } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
162                 ret = contacts_query_create(_contacts_contact_email._uri, &query);
163                 ret = contacts_filter_create(_contacts_contact_email._uri, &filter);
164
165                 ret = contacts_filter_add_str(filter, _contacts_contact_email.email, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
166
167         } else {
168                 MSG_DEBUG("Invalid pAddrInfo->addressType.");
169                 return MSG_SUCCESS;
170         }
171
172         ret = contacts_query_set_filter(query, filter);
173         ret = contacts_db_get_records_with_query(query, 0, 1, &contacts);
174         if (ret != CONTACTS_ERROR_NONE) {
175                 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
176                 contacts_query_destroy(query);
177                 contacts_filter_destroy(filter);
178                 contacts_list_destroy(contacts, true);
179                 return MSG_SUCCESS;
180         }
181
182         ret = contacts_list_get_count(contacts, &count);
183
184         if (count == 0 || ret != CONTACTS_ERROR_NONE) {
185                 MSG_DEBUG("No Serach Data from Contact Service.");
186                 contacts_query_destroy(query);
187                 contacts_filter_destroy(filter);
188                 contacts_list_destroy(contacts, true);
189                 return MSG_SUCCESS;
190         }
191
192         contacts_record_h contact = NULL;
193
194         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
195                 contacts_record_h number = NULL;
196
197                 ret = contacts_list_get_current_record_p(contacts, &number);
198                 if (ret != CONTACTS_ERROR_NONE) {
199                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
200                         contacts_list_destroy(contacts, true);
201                         return MSG_SUCCESS;
202                 }
203
204                 ret = contacts_record_get_int(number, _contacts_contact_number.contact_id, &index);
205                 if (ret != CONTACTS_ERROR_NONE) {
206                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
207                         contacts_list_destroy(contacts, true);
208                         contacts_record_destroy(number, true);
209                         return MSG_SUCCESS;
210                 }
211
212                 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
213                 if (ret != CONTACTS_ERROR_NONE) {
214                         MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
215                         contacts_list_destroy(contacts, true);
216                         contacts_record_destroy(contact, true);
217                         contacts_record_destroy(number, true);
218                         return MSG_SUCCESS;
219                 }
220         } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
221                 contacts_record_h email = NULL;
222
223                 ret = contacts_list_get_current_record_p(contacts, &email);
224                 if (ret != CONTACTS_ERROR_NONE) {
225                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
226                         contacts_list_destroy(contacts, true);
227                         return MSG_SUCCESS;
228                 }
229
230                 ret = contacts_record_get_int(email, _contacts_contact_email.contact_id, &index);
231                 if (ret != CONTACTS_ERROR_NONE) {
232                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
233                         contacts_list_destroy(contacts, true);
234                         contacts_record_destroy(email, true);
235                         return MSG_SUCCESS;
236                 }
237
238                 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
239                 if (ret != CONTACTS_ERROR_NONE) {
240                         MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
241                         contacts_list_destroy(contacts, true);
242                         contacts_record_destroy(contact, true);
243                         contacts_record_destroy(email, true);
244                         return MSG_SUCCESS;
245                 }
246         }
247
248         contacts_list_destroy(contacts, true);
249
250         // Name Info
251         contacts_record_h name = NULL;
252
253         ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
254         if (ret != CONTACTS_ERROR_NONE) {
255                 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
256                 contacts_record_destroy(contact, true);
257                 return MSG_SUCCESS;
258         }
259
260         char* strFirstName = NULL;
261         ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
262         if (ret != CONTACTS_ERROR_NONE) {
263                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
264                 contacts_record_destroy(contact, true);
265                 return MSG_SUCCESS;
266         }
267
268         char* strLastName = NULL;
269         ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
270         if (ret != CONTACTS_ERROR_NONE) {
271                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
272                 contacts_record_destroy(contact, true);
273                 return MSG_SUCCESS;
274         }
275
276         MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
277
278         if (strFirstName != NULL)
279                 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
280
281         if (strLastName != NULL)
282                 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
283
284         ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&pContactInfo->contactId);
285         if (ret != CONTACTS_ERROR_NONE) {
286                 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
287                 contacts_record_destroy(contact, true);
288                 return MSG_SUCCESS;
289         }
290
291         MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
292
293         char* strImagePath = NULL;
294         ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
295         if (ret != CONTACTS_ERROR_NONE) {
296                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
297                 contacts_record_destroy(contact, true);
298                 return MSG_SUCCESS;
299         }
300
301         if (strImagePath != NULL)
302                 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
303
304         MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
305
306         contacts_record_destroy(contact, true);
307
308         MSG_END();
309
310         return MSG_SUCCESS;
311 }
312
313
314 void MsgSyncContact()
315 {
316         int ret = -1;
317         unsigned int changed_count = 0;
318         int lastSyncTime = 0;
319         int finalSyncTime = 0;
320
321         /* get contact sync time */
322         lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
323
324         if (lastSyncTime < 0) {
325                 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
326                 lastSyncTime = 0;
327         }
328
329         contacts_list_h contactsList = NULL;
330
331         ret = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, -1, lastSyncTime, &contactsList, &finalSyncTime);
332
333         if (ret != CONTACTS_ERROR_NONE) {
334                 MSG_DEBUG("contacts_db_get_changes_by_version() Error [%d]", ret);
335                 return;
336         }
337
338         ret = contacts_list_get_count(contactsList, &changed_count);
339
340         if (ret != CONTACTS_ERROR_NONE) {
341                 MSG_DEBUG("contacts_list_get_count() Error [%d]", ret);
342                 contacts_list_destroy(contactsList, true);
343                 return;
344         }
345
346         for (unsigned int i = 0; i < changed_count; i++)
347         {
348                 int index_num = 0;
349                 int type = 0;
350                 contacts_record_h event = NULL;
351
352                 ret = contacts_list_get_current_record_p(contactsList, &event);
353                 if (ret != CONTACTS_ERROR_NONE) {
354                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
355                         contacts_list_destroy(contactsList, true);
356                         return;
357                 }
358
359                 ret = contacts_record_get_int(event, _contacts_contact_updated_info.contact_id, &index_num);
360                 if (ret != CONTACTS_ERROR_NONE) {
361                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
362                         contacts_list_destroy(contactsList, true);
363                         return;
364                 }
365
366                 MSG_DEBUG("index (%d)", index_num);
367
368                 ret = contacts_record_get_int(event, _contacts_contact_updated_info.type, &type);
369                 if (ret != CONTACTS_ERROR_NONE) {
370                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
371                         contacts_list_destroy(contactsList, true);
372                         return;
373                 }
374
375                 if (type == CONTACTS_CHANGE_UPDATED || type == CONTACTS_CHANGE_INSERTED) {
376                         MsgUpdateContact(index_num, type);
377                 } else {// Delete
378                         MSG_DEBUG("Delete Contact");
379                         MsgDeleteContact(index_num);
380                 }
381
382                 ret = contacts_list_next(contactsList);
383                 if (ret != CONTACTS_ERROR_NONE) {
384                         MSG_DEBUG("contacts_list_next() Error [%d]", ret);
385                 }
386         }
387
388         if(MsgSettingSetInt(CONTACT_SYNC_TIME, finalSyncTime) != MSG_SUCCESS)
389                 MSG_DEBUG("MsgSettingSetInt fail : CONTACT_SYNC_TIME");
390         MSG_DEBUG("lastSyncTime : %d", finalSyncTime);
391
392         contacts_list_destroy(contactsList, true);
393
394         if(changed_count > 0)
395                 cbFunction();
396 }
397
398
399 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
400 {
401         if (!pNumber || strlen(pNumber) <= 0)
402                 return false;
403
404         if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS) {
405                 MSG_DEBUG("Fail to add contact info.");
406                 return false;
407         }
408
409         return true;
410 }
411
412
413 bool MsgUpdateContact(int index, int type)
414 {
415         int ret = CONTACTS_ERROR_NONE;
416
417         contacts_record_h contact = NULL;
418
419         ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
420         if (ret != CONTACTS_ERROR_NONE) {
421                 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
422                 contacts_record_destroy(contact, true);
423                 return false;
424         }
425
426         MSG_CONTACT_INFO_S contactInfo;
427         memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
428
429         ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&contactInfo.contactId);
430         if (ret != CONTACTS_ERROR_NONE) {
431                 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
432                 contacts_record_destroy(contact, true);
433                 return false;
434         }
435
436         MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
437
438         char* strImagePath = NULL;
439         ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
440         if (ret != CONTACTS_ERROR_NONE) {
441                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
442                 contacts_record_destroy(contact, true);
443                 return false;
444         }
445
446         if (strImagePath != NULL)
447                 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
448
449         MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
450
451         // Name Info
452         contacts_record_h name = NULL;
453
454         ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
455         if (ret != CONTACTS_ERROR_NONE) {
456                 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
457                 contacts_record_destroy(contact, true);
458                 return false;
459         }
460
461         char* strFirstName = NULL;
462         ret = contacts_record_get_str_p(name, _contacts_name.first, &strFirstName);
463         if (ret != CONTACTS_ERROR_NONE) {
464                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
465                 contacts_record_destroy(contact, true);
466                 return false;
467         }
468
469         char* strLastName = NULL;
470         ret = contacts_record_get_str_p(name, _contacts_name.last, &strLastName);
471         if (ret != CONTACTS_ERROR_NONE) {
472                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
473                 contacts_record_destroy(contact, true);
474                 return false;
475         }
476
477         MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
478
479         if (strFirstName != NULL)
480                 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
481
482         if (strLastName != NULL)
483                 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
484
485         MsgStoClearContactInfo(&ContactDbHandle, index);
486
487         unsigned int count = 0;
488         ret = contacts_record_get_child_record_count(contact, _contacts_contact.number, &count);
489         if (ret != CONTACTS_ERROR_NONE) {
490                 MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
491                 contacts_record_destroy(contact, true);
492                 return false;
493         }
494
495         MSG_DEBUG("_contacts_contact.number count [%d]", count);
496
497         if (count > 0) {
498                 for(unsigned int i=0; i < count; i++)
499                 {
500                         MSG_DEBUG("Add Number Contact Data");
501
502                         contacts_record_h number = NULL;
503
504                         ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &number);
505                         if (ret != CONTACTS_ERROR_NONE) {
506                                 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
507                                 contacts_record_destroy(contact, true);
508                                 return false;
509                         }
510
511                         char* strNumber = NULL;
512                         ret = contacts_record_get_str_p(number, _contacts_number.number, &strNumber);
513                         if (ret != CONTACTS_ERROR_NONE) {
514                                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
515                                 contacts_record_destroy(contact, true);
516                                 return false;
517                         }
518
519                         if (strNumber != NULL) {
520                                 MSG_DEBUG("Number = %s", strNumber);
521                                 if (!MsgInsertContact(&contactInfo, strNumber)) {
522                                         MSG_DEBUG("MsgInsertContact fail.");
523                                 }
524                         }
525                 }
526         }
527
528         count = 0;
529         ret = contacts_record_get_child_record_count(contact, _contacts_contact.email, &count);
530         if (ret != CONTACTS_ERROR_NONE) {
531                 MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
532                 contacts_record_destroy(contact, true);
533                 return false;
534         }
535
536         MSG_DEBUG("_contacts_contact.email count [%d]", count);
537
538         if (count > 0) {
539                 for(unsigned int i=0; i < count; i++)
540                 {
541                         MSG_DEBUG("Add Email Contact Data");
542
543                         contacts_record_h email = NULL;
544
545                         ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.email, i, &email);
546                         if (ret != CONTACTS_ERROR_NONE) {
547                                 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
548                                 contacts_record_destroy(contact, true);
549                                 return false;
550                         }
551
552                         char* strNumber = NULL;
553                         ret = contacts_record_get_str_p(email, _contacts_email.email, &strNumber);
554                         if (ret != CONTACTS_ERROR_NONE) {
555                                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
556                                 contacts_record_destroy(contact, true);
557                                 return false;
558                         }
559
560                         if (strNumber != NULL) {
561                                 MSG_DEBUG("email = %s", strNumber);
562                                 if (!MsgInsertContact(&contactInfo, strNumber)) {
563                                         MSG_DEBUG("MsgInsertContact fail.");
564                                 }
565                         }
566                 }
567         }
568
569         MsgStoSetConversationDisplayName(&ContactDbHandle, index);
570
571         contacts_record_destroy(contact, true);
572
573         return true;
574 }
575
576
577 bool MsgDeleteContact(int index)
578 {
579         if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
580                 return false;
581
582         return true;
583 }
584
585
586 int MsgGetContactNameOrder()
587 {
588         msg_error_t err = MSG_SUCCESS;
589
590         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
591                 MSG_DEBUG("MsgOpenContactSvc fail.");
592                 return 0;
593         }
594
595         if (!isContactSvcConnected) {
596                 MSG_DEBUG("Contact Service Not Opened.");
597                 return 0; // return default value : FIRSTLAST
598         }
599
600         int ret = CONTACTS_ERROR_NONE;
601
602         contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
603
604         ret = contacts_setting_get_name_display_order(&order);
605         if (ret != CONTACTS_ERROR_NONE) {
606                 MSG_DEBUG("contacts_setting_get_name_display_order() Error [%d]", ret);
607                 return 0;
608         }
609
610         if (order == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST)
611                 return 0;
612         else
613                 return 1;
614 }
615
616
617 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
618 {
619         msg_error_t err = MSG_SUCCESS;
620
621         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
622                 MSG_DEBUG("MsgOpenContactSvc fail.");
623                 return;
624         }
625
626         if (!isContactSvcConnected) {
627                 MSG_DEBUG("Contact Service Not Opened.");
628                 return;
629         }
630
631         if(pMsgInfo->nAddressCnt < 1) {
632                 MSG_DEBUG("address count is [%d]", pMsgInfo->nAddressCnt);
633                 return;
634         }
635
636         for (int i = 0; pMsgInfo->nAddressCnt > i; i++) {
637                 int ret = 0;
638                 contacts_record_h plog = NULL;
639
640                 ret = contacts_record_create(_contacts_phone_log._uri, &plog);
641                 if (ret != CONTACTS_ERROR_NONE) {
642                         MSG_DEBUG("contacts_record_create() Error [%d]", ret);
643                         contacts_record_destroy(plog, true);
644                         break;
645                 }
646
647                 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[i].addressVal);
648                 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
649
650                 char strText[101];
651                 memset(strText, 0x00, sizeof(strText));
652
653                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
654                         strncpy(strText, pMsgInfo->msgText, 100);
655                         MSG_DEBUG("msgText : %s", strText);
656                 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
657                         if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
658                                 strncpy(strText, pMsgInfo->subject, 100);
659                                 MSG_DEBUG("subject : %s", strText);
660                         } else {
661                                 strncpy(strText, pMsgInfo->msgText, 100);
662                                 MSG_DEBUG("msgText : %s", strText);
663                         }
664                 }
665
666                 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
667                 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
668
669                 if (pMsgInfo->folderId == MSG_INBOX_ID) {
670                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
671                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMMING);
672                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
673                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMMING);
674                 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
675                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
676                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
677                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
678                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
679                 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
680                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
681                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
682                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
683                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
684                 }
685
686                 ret = contacts_db_insert_record(plog, NULL);
687                 if (ret != CONTACTS_ERROR_NONE) {
688                         MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
689                 }
690
691                 contacts_record_destroy(plog, true);
692         }
693 }
694
695
696 void MsgDeletePhoneLog(msg_message_id_t msgId)
697 {
698         msg_error_t err = MSG_SUCCESS;
699
700         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
701                 MSG_DEBUG("MsgOpenContactSvc fail.");
702                 return;
703         }
704
705         MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
706
707         if (!isContactSvcConnected) {
708                 MSG_DEBUG("Contact Service Not Opened.");
709                 return;
710         }
711
712         int ret = CONTACTS_ERROR_NONE;
713         int index = 0;
714         unsigned int count = 0;
715         contacts_query_h query;
716         contacts_filter_h filter;
717         contacts_list_h plogs = NULL;
718
719         ret = contacts_query_create(_contacts_phone_log._uri, &query);
720         ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
721
722         ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
723
724
725         ret = contacts_query_set_filter(query, filter);
726         ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
727
728         ret = contacts_list_get_count(plogs, &count);
729
730         if (count == 0) {
731                 MSG_DEBUG("No Serach Data from Contact Service.");
732         } else {
733                 contacts_record_h plog = NULL;
734
735                 ret = contacts_list_get_current_record_p(plogs, &plog);
736                 if (ret != CONTACTS_ERROR_NONE) {
737                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
738                 }
739
740                 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
741                 if (ret != CONTACTS_ERROR_NONE) {
742                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
743                 }
744
745                 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
746                 if (ret != CONTACTS_ERROR_NONE) {
747                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
748                 } else {
749                         MSG_DEBUG("contacts_db_delete_record() Success.");
750                 }
751         }
752
753         contacts_query_destroy(query);
754         contacts_filter_destroy(filter);
755         contacts_list_destroy(plogs, true);
756
757 }
758
759
760 int MsgContactSVCBeginTrans()
761 {
762         //return contacts_svc_begin_trans();
763         return 0;
764 }
765
766
767 int MsgContactSVCEndTrans(bool bSuccess)
768 {
769         //return contacts_svc_end_trans(bSuccess);
770         return 0;
771 }