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