Chnage Copyright Year from 2012 to 2012-2013
[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.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
7 *
8 *    http://www.tizenopensource.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         int errCode = CONTACTS_ERROR_NONE;
95
96         if (!isContactSvcConnected) {
97                 MSG_DEBUG("Contact Service Not Opened.");
98                 return MSG_ERR_UNKNOWN;
99         }
100
101         if (cb != NULL)
102                 cbFunction = cb;
103
104         // Register callback function
105         errCode = contacts_db_add_changed_cb(_contacts_contact._uri, MsgContactSvcCallback, NULL);
106
107         if (errCode == CONTACTS_ERROR_NONE)
108                 MSG_DEBUG("Register Contact Service Callback");
109         else
110                 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
111
112         return MSG_SUCCESS;
113 }
114
115
116 msg_error_t MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
117 {
118         MSG_BEGIN();
119
120         MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
121
122         memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
123
124         if (!isContactSvcConnected) {
125                 MSG_DEBUG("Contact Service Not Opened.");
126                 return MSG_ERR_UNKNOWN;
127         }
128
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);
131                 return MSG_SUCCESS;
132         }
133
134         int ret = 0;
135         int index = 0;
136         unsigned int count = 0;
137         contacts_query_h query = NULL;
138         contacts_filter_h filter = NULL;
139         contacts_list_h contacts = NULL;
140
141         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN) {
142                 ret = contacts_query_create(_contacts_contact_number._uri, &query);
143                 ret = contacts_filter_create(_contacts_contact_number._uri, &filter);
144
145                 ret = contacts_filter_add_str(filter, _contacts_contact_number.number_filter, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
146
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);
150
151                 ret = contacts_filter_add_str(filter, _contacts_contact_email.email, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
152
153         } else {
154                 MSG_DEBUG("Invalid pAddrInfo->addressType.");
155                 return MSG_SUCCESS;
156         }
157
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);
165                 return MSG_SUCCESS;
166         }
167
168         ret = contacts_list_get_count(contacts, &count);
169
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);
175                 return MSG_SUCCESS;
176         }
177
178         contacts_record_h contact = NULL;
179
180         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN) {
181                 contacts_record_h number = NULL;
182
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);
187                         return MSG_SUCCESS;
188                 }
189
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);
195                         return MSG_SUCCESS;
196                 }
197
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);
204                         return MSG_SUCCESS;
205                 }
206         } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
207                 contacts_record_h email = NULL;
208
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);
213                         return MSG_SUCCESS;
214                 }
215
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);
221                         return MSG_SUCCESS;
222                 }
223
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);
230                         return MSG_SUCCESS;
231                 }
232         }
233
234         contacts_list_destroy(contacts, true);
235
236         // Name Info
237         contacts_record_h name = NULL;
238
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);
243                 return MSG_SUCCESS;
244         }
245
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);
251                 return MSG_SUCCESS;
252         }
253
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);
259                 return MSG_SUCCESS;
260         }
261
262         MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
263
264         if (strFirstName != NULL)
265                 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
266
267         if (strLastName != NULL)
268                 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
269
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);
274                 return MSG_SUCCESS;
275         }
276
277         MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
278
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);
284                 return MSG_SUCCESS;
285         }
286
287         if (strImagePath != NULL)
288                 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
289
290         MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
291
292         contacts_record_destroy(contact, true);
293
294         MSG_END();
295
296         return MSG_SUCCESS;
297 }
298
299
300 void MsgSyncContact()
301 {
302         int ret = -1;
303         unsigned int changed_count = 0;
304         int lastSyncTime = 0;
305         int finalSyncTime = 0;
306
307         /* get contact sync time */
308         lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
309
310         if (lastSyncTime < 0) {
311                 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
312                 lastSyncTime = 0;
313         }
314
315         contacts_list_h contactsList = NULL;
316
317         ret = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, -1, lastSyncTime, &contactsList, &finalSyncTime);
318
319         if (ret != CONTACTS_ERROR_NONE) {
320                 MSG_DEBUG("contacts_db_get_changes_by_version() Error [%d]", ret);
321                 return;
322         }
323
324         ret = contacts_list_get_count(contactsList, &changed_count);
325
326         if (ret != CONTACTS_ERROR_NONE) {
327                 MSG_DEBUG("contacts_list_get_count() Error [%d]", ret);
328                 contacts_list_destroy(contactsList, true);
329                 return;
330         }
331
332         for (unsigned int i = 0; i < changed_count; i++);
333         {
334                 int index_num = 0;
335                 int type = 0;
336                 contacts_record_h event = NULL;
337
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);
342                         return;
343                 }
344
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);
349                         return;
350                 }
351
352                 MSG_DEBUG("index (%d)", index_num);
353
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);
358                         return;
359                 }
360
361                 if (type == CONTACTS_CHANGE_UPDATED || type == CONTACTS_CHANGE_INSERTED) {
362                         MsgUpdateContact(index_num, type);
363                 } else {// Delete
364                         MSG_DEBUG("Delete Contact");
365                         MsgDeleteContact(index_num);
366                 }
367
368                 ret = contacts_list_next(contactsList);
369                 if (ret != CONTACTS_ERROR_NONE) {
370                         MSG_DEBUG("contacts_list_next() Error [%d]", ret);
371                 }
372         }
373
374         if(MsgSettingSetInt(CONTACT_SYNC_TIME, finalSyncTime) != MSG_SUCCESS)
375                 MSG_DEBUG("MsgSettingSetInt fail : CONTACT_SYNC_TIME");
376         MSG_DEBUG("lastSyncTime : %d", finalSyncTime);
377
378         contacts_list_destroy(contactsList, true);
379
380         if(changed_count > 0)
381                 cbFunction();
382 }
383
384
385 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
386 {
387         if (!pNumber || strlen(pNumber) <= 0)
388                 return false;
389
390         if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS) {
391                 MSG_DEBUG("Fail to add contact info.");
392                 return false;
393         }
394
395         return true;
396 }
397
398
399 bool MsgUpdateContact(int index, int type)
400 {
401         int ret = CONTACTS_ERROR_NONE;
402
403         contacts_record_h contact = NULL;
404
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);
409                 return false;
410         }
411
412         MSG_CONTACT_INFO_S contactInfo;
413         memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
414
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);
419                 return false;
420         }
421
422         MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
423
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);
429                 return false;
430         }
431
432         if (strImagePath != NULL)
433                 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
434
435         MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
436
437         // Name Info
438         contacts_record_h name = NULL;
439
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);
444                 return false;
445         }
446
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);
452                 return false;
453         }
454
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);
460                 return false;
461         }
462
463         MSG_DEBUG("First Name : [%s], Last Name : [%s]", strFirstName, strLastName);
464
465         if (strFirstName != NULL)
466                 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
467
468         if (strLastName != NULL)
469                 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
470
471         MsgStoClearContactInfo(&ContactDbHandle, index);
472
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);
478                 return false;
479         }
480
481         if (count > 0) {
482                 for(unsigned int i=0; i < count; i++)
483                 {
484                         MSG_DEBUG("Add Contact Data");
485
486                         contacts_record_h number = NULL;
487
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);
492                                 return false;
493                         }
494
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);
500                                 return false;
501                         }
502
503                         if (strNumber != NULL) {
504                                 MSG_DEBUG("Number = %s", strNumber);
505                                 if (!MsgInsertContact(&contactInfo, strNumber)) {
506                                         MSG_DEBUG("MsgInsertContact fail.");
507                                 }
508                         }
509                 }
510         } else {// No phone number in contact
511                 contacts_record_destroy(contact, true);
512                 return true;
513         }
514
515         MsgStoSetConversationDisplayName(&ContactDbHandle, index);
516
517         contacts_record_destroy(contact, true);
518
519         return true;
520 }
521
522
523 bool MsgDeleteContact(int index)
524 {
525         if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
526                 return false;
527
528         return true;
529 }
530
531
532 int MsgGetContactNameOrder()
533 {
534
535         if (!isContactSvcConnected) {
536                 MSG_DEBUG("Contact Service Not Opened.");
537                 return 0; // return default value : FIRSTLAST
538         }
539
540         int ret = CONTACTS_ERROR_NONE;
541
542         contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
543
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);
547                 return 0;
548         }
549
550         if (order == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST)
551                 return 0;
552         else
553                 return 1;
554 }
555
556
557 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
558 {
559         if (!isContactSvcConnected) {
560                 MSG_DEBUG("Contact Service Not Opened.");
561                 return;
562         }
563
564         if(pMsgInfo->nAddressCnt < 1) {
565                 MSG_DEBUG("address count is [%d]", pMsgInfo->nAddressCnt);
566                 return;
567         }
568
569         for (int i = 0; pMsgInfo->nAddressCnt > i; i++) {
570                 int ret = 0;
571                 contacts_record_h plog = NULL;
572
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);
577                         break;
578                 }
579
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));
582
583                 char strText[101];
584                 memset(strText, 0x00, sizeof(strText));
585
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);
593                         } else {
594                                 strncpy(strText, pMsgInfo->msgText, 100);
595                                 MSG_DEBUG("msgText : %s", strText);
596                         }
597                 }
598
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);
601
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);
617                 }
618
619                 ret = contacts_db_insert_record(plog, NULL);
620                 if (ret != CONTACTS_ERROR_NONE) {
621                         MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
622                 }
623
624                 contacts_record_destroy(plog, true);
625         }
626 }
627
628
629 void MsgDeletePhoneLog(msg_message_id_t msgId)
630 {
631         MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
632
633         if (!isContactSvcConnected) {
634                 MSG_DEBUG("Contact Service Not Opened.");
635                 return;
636         }
637
638         int ret = CONTACTS_ERROR_NONE;
639         int index = 0;
640         unsigned int count = 0;
641         contacts_query_h query;
642         contacts_filter_h filter;
643         contacts_list_h plogs = NULL;
644
645         ret = contacts_query_create(_contacts_phone_log._uri, &query);
646         ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
647
648         ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
649
650
651         ret = contacts_query_set_filter(query, filter);
652         ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
653
654         ret = contacts_list_get_count(plogs, &count);
655
656         if (count == 0) {
657                 MSG_DEBUG("No Serach Data from Contact Service.");
658         } else {
659                 contacts_record_h plog = NULL;
660
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);
664                 }
665
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);
669                 }
670
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);
674                 } else {
675                         MSG_DEBUG("contacts_db_delete_record() Success.");
676                 }
677         }
678
679         contacts_query_destroy(query);
680         contacts_filter_destroy(filter);
681         contacts_list_destroy(plogs, true);
682
683 }
684
685
686 int MsgContactSVCBeginTrans()
687 {
688         //return contacts_svc_begin_trans();
689         return 0;
690 }
691
692
693 int MsgContactSVCEndTrans(bool bSuccess)
694 {
695         //return contacts_svc_end_trans(bSuccess);
696         return 0;
697 }