Modify flora license version.
[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         if (count > 0) {
496                 for(unsigned int i=0; i < count; i++)
497                 {
498                         MSG_DEBUG("Add Contact Data");
499
500                         contacts_record_h number = NULL;
501
502                         ret = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &number);
503                         if (ret != CONTACTS_ERROR_NONE) {
504                                 MSG_DEBUG("contacts_record_get_child_record_at_p() Error [%d]", ret);
505                                 contacts_record_destroy(contact, true);
506                                 return false;
507                         }
508
509                         char* strNumber = NULL;
510                         ret = contacts_record_get_str_p(number, _contacts_number.number, &strNumber);
511                         if (ret != CONTACTS_ERROR_NONE) {
512                                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
513                                 contacts_record_destroy(contact, true);
514                                 return false;
515                         }
516
517                         if (strNumber != NULL) {
518                                 MSG_DEBUG("Number = %s", strNumber);
519                                 if (!MsgInsertContact(&contactInfo, strNumber)) {
520                                         MSG_DEBUG("MsgInsertContact fail.");
521                                 }
522                         }
523                 }
524         } else {// No phone number in contact
525                 contacts_record_destroy(contact, true);
526                 return true;
527         }
528
529         MsgStoSetConversationDisplayName(&ContactDbHandle, index);
530
531         contacts_record_destroy(contact, true);
532
533         return true;
534 }
535
536
537 bool MsgDeleteContact(int index)
538 {
539         if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
540                 return false;
541
542         return true;
543 }
544
545
546 int MsgGetContactNameOrder()
547 {
548         msg_error_t err = MSG_SUCCESS;
549
550         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
551                 MSG_DEBUG("MsgOpenContactSvc fail.");
552                 return 0;
553         }
554
555         if (!isContactSvcConnected) {
556                 MSG_DEBUG("Contact Service Not Opened.");
557                 return 0; // return default value : FIRSTLAST
558         }
559
560         int ret = CONTACTS_ERROR_NONE;
561
562         contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
563
564         ret = contacts_setting_get_name_display_order(&order);
565         if (ret != CONTACTS_ERROR_NONE) {
566                 MSG_DEBUG("contacts_setting_get_name_display_order() Error [%d]", ret);
567                 return 0;
568         }
569
570         if (order == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST)
571                 return 0;
572         else
573                 return 1;
574 }
575
576
577 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
578 {
579         msg_error_t err = MSG_SUCCESS;
580
581         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
582                 MSG_DEBUG("MsgOpenContactSvc fail.");
583                 return;
584         }
585
586         if (!isContactSvcConnected) {
587                 MSG_DEBUG("Contact Service Not Opened.");
588                 return;
589         }
590
591         if(pMsgInfo->nAddressCnt < 1) {
592                 MSG_DEBUG("address count is [%d]", pMsgInfo->nAddressCnt);
593                 return;
594         }
595
596         for (int i = 0; pMsgInfo->nAddressCnt > i; i++) {
597                 int ret = 0;
598                 contacts_record_h plog = NULL;
599
600                 ret = contacts_record_create(_contacts_phone_log._uri, &plog);
601                 if (ret != CONTACTS_ERROR_NONE) {
602                         MSG_DEBUG("contacts_record_create() Error [%d]", ret);
603                         contacts_record_destroy(plog, true);
604                         break;
605                 }
606
607                 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[i].addressVal);
608                 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
609
610                 char strText[101];
611                 memset(strText, 0x00, sizeof(strText));
612
613                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
614                         strncpy(strText, pMsgInfo->msgText, 100);
615                         MSG_DEBUG("msgText : %s", strText);
616                 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
617                         if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
618                                 strncpy(strText, pMsgInfo->subject, 100);
619                                 MSG_DEBUG("subject : %s", strText);
620                         } else {
621                                 strncpy(strText, pMsgInfo->msgText, 100);
622                                 MSG_DEBUG("msgText : %s", strText);
623                         }
624                 }
625
626                 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
627                 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
628
629                 if (pMsgInfo->folderId == MSG_INBOX_ID) {
630                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
631                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMMING);
632                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
633                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMMING);
634                 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
635                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
636                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
637                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
638                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
639                 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
640                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
641                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
642                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
643                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
644                 }
645
646                 ret = contacts_db_insert_record(plog, NULL);
647                 if (ret != CONTACTS_ERROR_NONE) {
648                         MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
649                 }
650
651                 contacts_record_destroy(plog, true);
652         }
653 }
654
655
656 void MsgDeletePhoneLog(msg_message_id_t msgId)
657 {
658         msg_error_t err = MSG_SUCCESS;
659
660         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
661                 MSG_DEBUG("MsgOpenContactSvc fail.");
662                 return;
663         }
664
665         MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
666
667         if (!isContactSvcConnected) {
668                 MSG_DEBUG("Contact Service Not Opened.");
669                 return;
670         }
671
672         int ret = CONTACTS_ERROR_NONE;
673         int index = 0;
674         unsigned int count = 0;
675         contacts_query_h query;
676         contacts_filter_h filter;
677         contacts_list_h plogs = NULL;
678
679         ret = contacts_query_create(_contacts_phone_log._uri, &query);
680         ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
681
682         ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
683
684
685         ret = contacts_query_set_filter(query, filter);
686         ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
687
688         ret = contacts_list_get_count(plogs, &count);
689
690         if (count == 0) {
691                 MSG_DEBUG("No Serach Data from Contact Service.");
692         } else {
693                 contacts_record_h plog = NULL;
694
695                 ret = contacts_list_get_current_record_p(plogs, &plog);
696                 if (ret != CONTACTS_ERROR_NONE) {
697                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
698                 }
699
700                 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
701                 if (ret != CONTACTS_ERROR_NONE) {
702                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
703                 }
704
705                 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
706                 if (ret != CONTACTS_ERROR_NONE) {
707                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
708                 } else {
709                         MSG_DEBUG("contacts_db_delete_record() Success.");
710                 }
711         }
712
713         contacts_query_destroy(query);
714         contacts_filter_destroy(filter);
715         contacts_list_destroy(plogs, true);
716
717 }
718
719
720 int MsgContactSVCBeginTrans()
721 {
722         //return contacts_svc_begin_trans();
723         return 0;
724 }
725
726
727 int MsgContactSVCEndTrans(bool bSuccess)
728 {
729         //return contacts_svc_end_trans(bSuccess);
730         return 0;
731 }