Fix build err for wearable
[platform/core/messaging/msg-service.git] / utils / MsgContact.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <ctype.h>
18
19 #include "MsgDebug.h"
20 #include "MsgUtilStorage.h"
21 #include "MsgUtilFile.h"
22 #include "MsgGconfWrapper.h"
23 #include "MsgContact.h"
24
25 extern "C"
26 {
27         #include <contacts.h>
28 }
29
30 /*==================================================================================================
31                                      VARIABLES
32 ==================================================================================================*/
33 __thread bool isContactSvcConnected = false;
34
35 MsgDbHandler ContactDbHandle;
36
37 /* phonenumber minimum match digit. */
38 #define PHONENUMBER_MIN_MATCH_DIGIT VCONFKEY_CONTACTS_SVC_PHONENUMBER_MIN_MATCH_DIGIT
39 #define DEFAULT_MIN_MATCH_DIGIT 8
40
41 static int phonenumberMinMatchDigit = -1;
42
43 /*==================================================================================================
44                                      INTERNAL FUNCTION IMPLEMENTATION
45 ==================================================================================================*/
46 int countryCodeLength(const char *src)
47 {
48         int ret = 0;
49         switch (src[ret++]-'0') {
50         case 1:
51         case 7:
52                 break;
53         case 2:
54                 switch (src[ret++]-'0') {
55                 case 0:
56                 case 7:
57                         break;
58                 case 1:
59                 case 2:
60                 case 3:
61                 case 4:
62                 case 5:
63                 case 6:
64                 case 8:
65                 case 9:
66                         ret += 1;
67                         break;
68                 default:
69                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
70                 }
71                 break;
72         case 3:
73                 switch (src[ret++]-'0') {
74                 case 0:
75                 case 1:
76                 case 2:
77                 case 3:
78                 case 4:
79                 case 6:
80                 case 9:
81                         break;
82                 case 5:
83                 case 7:
84                 case 8:
85                         ret += 1;
86                         break;
87                 default:
88                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
89                 }
90                 break;
91         case 4:
92                 switch (src[ret++]-'0') {
93                 case 0:
94                 case 1:
95                 case 3:
96                 case 4:
97                 case 5:
98                 case 6:
99                 case 7:
100                 case 8:
101                 case 9:
102                         break;
103                 case 2:
104                         ret += 1;
105                         break;
106                 default:
107                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
108                 }
109                 break;
110         case 5:
111                 switch (src[ret++]-'0') {
112                 case 1:
113                 case 2:
114                 case 3:
115                 case 4:
116                 case 5:
117                 case 6:
118                 case 7:
119                 case 8:
120                         break;
121                 case 0:
122                 case 9:
123                         ret += 1;
124                         break;
125                 default:
126                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
127                 }
128                 break;
129         case 6:
130                 switch (src[ret++]-'0') {
131                 case 0:
132                 case 1:
133                 case 2:
134                 case 3:
135                 case 4:
136                 case 5:
137                 case 6:
138                         break;
139                 case 7:
140                 case 8:
141                 case 9:
142                         ret += 1;
143                         break;
144                 default:
145                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
146                 }
147                 break;
148         case 8:
149                 switch (src[ret++]-'0') {
150                 case 1:
151                 case 2:
152                 case 4:
153                 case 6:
154                         break;
155                 case 0:
156                 case 3:
157                 case 5:
158                 case 7:
159                 case 8:
160                 case 9:
161                         ret += 1;
162                         break;
163                 default:
164                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
165                 }
166                 break;
167         case 9:
168                 switch (src[ret++]-'0') {
169                 case 0:
170                 case 1:
171                 case 2:
172                 case 3:
173                 case 4:
174                 case 5:
175                 case 8:
176                         break;
177                 case 6:
178                 case 7:
179                 case 9:
180                         ret += 1;
181                         break;
182                 default:
183                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
184                 }
185                 break;
186         case 0:
187         default:
188                 MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
189                 return 0;
190         }
191
192         return ret;
193 }
194
195
196 void normalizeNumber(const char *orig, char* dest, unsigned int destSize)
197 {
198         unsigned int pos = 0;
199         for (unsigned int i = 0; (orig[i] && i < destSize); i++) {
200                 if (isdigit(orig[i]) || (orig[i] == '+')) {
201                         dest[pos++] = orig[i];
202                 }
203         }
204 }
205
206
207 /*==================================================================================================
208                                      FUNCTION IMPLEMENTATION
209 ==================================================================================================*/
210 msg_error_t MsgOpenContactSvc()
211 {
212         int errCode = CONTACTS_ERROR_NONE;
213
214         if (!isContactSvcConnected) {
215                 errCode = contacts_connect();
216
217                 if (errCode == CONTACTS_ERROR_NONE) {
218                         MSG_DEBUG("Connect to Contact Service Success");
219                         isContactSvcConnected = true;
220                 } else {
221                         MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
222                         isContactSvcConnected = false;
223                         return MSG_ERR_DB_CONNECT;
224                 }
225         } else {
226                 MSG_DEBUG("Already connected to Contact Service.");
227         }
228
229         return MSG_SUCCESS;
230 }
231
232
233 msg_error_t MsgCloseContactSvc()
234 {
235         int errCode = CONTACTS_ERROR_NONE;
236
237         if (isContactSvcConnected) {
238                 errCode = contacts_disconnect();
239
240                 if (errCode == CONTACTS_ERROR_NONE) {
241                         MSG_DEBUG("Disconnect to Contact Service Success");
242                         isContactSvcConnected = false;
243                 } else {
244                         MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
245                         return MSG_ERR_DB_DISCONNECT;
246                 }
247         }
248
249         return MSG_SUCCESS;
250 }
251
252
253 msg_error_t MsgInitContactSvc()
254 {
255         phonenumberMinMatchDigit = MsgSettingGetInt(PHONENUMBER_MIN_MATCH_DIGIT);
256         MSG_DEBUG("phonenumberMinMatchDigit [%d]", phonenumberMinMatchDigit);
257
258         if (phonenumberMinMatchDigit < 1) {
259                 phonenumberMinMatchDigit = DEFAULT_MIN_MATCH_DIGIT;
260         }
261
262         return MSG_SUCCESS;
263 }
264
265
266 msg_error_t MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
267 {
268         MSG_BEGIN();
269
270         msg_error_t err = MSG_SUCCESS;
271
272         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
273                 MSG_DEBUG("MsgOpenContactSvc fail.");
274                 return err;
275         }
276
277         if (!isContactSvcConnected) {
278                 MSG_DEBUG("Contact Service Not Opened.");
279                 return MSG_ERR_UNKNOWN;
280         }
281
282         MSG_SEC_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
283
284         memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
285
286         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1)) {
287                 MSG_SEC_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
288                 return MSG_ERR_UNKNOWN;
289         }
290
291         int ret = 0;
292         int index = 0;
293         int count = 0;
294         contacts_query_h query = NULL;
295         contacts_filter_h filter = NULL;
296         contacts_list_h contacts = NULL;
297
298         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
299                 ret = contacts_query_create(_contacts_contact_number._uri, &query);
300                 ret = contacts_filter_create(_contacts_contact_number._uri, &filter);
301
302                 ret = contacts_filter_add_str(filter, _contacts_contact_number.number_filter, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
303
304         } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
305                 ret = contacts_query_create(_contacts_contact_email._uri, &query);
306                 ret = contacts_filter_create(_contacts_contact_email._uri, &filter);
307
308                 ret = contacts_filter_add_str(filter, _contacts_contact_email.email, CONTACTS_MATCH_EXACTLY, pAddrInfo->addressVal);
309
310         } else {
311                 MSG_DEBUG("Invalid pAddrInfo->addressType.");
312                 return MSG_ERR_UNKNOWN;
313         }
314
315         ret = contacts_query_set_filter(query, filter);
316         ret = contacts_db_get_records_with_query(query, 0, 1, &contacts);
317         if (ret != CONTACTS_ERROR_NONE) {
318                 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
319                 contacts_query_destroy(query);
320                 contacts_filter_destroy(filter);
321                 contacts_list_destroy(contacts, true);
322                 return MSG_ERR_UNKNOWN;
323         }
324
325         ret = contacts_list_get_count(contacts, &count);
326
327         if (count == 0 || ret != CONTACTS_ERROR_NONE) {
328                 MSG_DEBUG("No Serach Data from Contact Service.");
329                 contacts_query_destroy(query);
330                 contacts_filter_destroy(filter);
331                 contacts_list_destroy(contacts, true);
332                 return MSG_SUCCESS;
333         }
334
335         contacts_query_destroy(query);
336         contacts_filter_destroy(filter);
337
338         contacts_record_h contact = NULL;
339
340         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN || pAddrInfo->addressType == MSG_ADDRESS_TYPE_UNKNOWN) {
341                 contacts_record_h number = NULL;
342
343                 ret = contacts_list_get_current_record_p(contacts, &number);
344                 if (ret != CONTACTS_ERROR_NONE) {
345                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
346                         contacts_list_destroy(contacts, true);
347                         return MSG_ERR_UNKNOWN;
348                 }
349
350                 ret = contacts_record_get_int(number, _contacts_contact_number.contact_id, &index);
351                 if (ret != CONTACTS_ERROR_NONE) {
352                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
353                         contacts_list_destroy(contacts, true);
354                         return MSG_ERR_UNKNOWN;
355                 }
356
357                 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
358                 if (ret != CONTACTS_ERROR_NONE) {
359                         MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
360                         contacts_list_destroy(contacts, true);
361                         return MSG_ERR_UNKNOWN;
362                 }
363         } else if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL) {
364                 contacts_record_h email = NULL;
365
366                 ret = contacts_list_get_current_record_p(contacts, &email);
367                 if (ret != CONTACTS_ERROR_NONE) {
368                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
369                         contacts_list_destroy(contacts, true);
370                         return MSG_ERR_UNKNOWN;
371                 }
372
373                 ret = contacts_record_get_int(email, _contacts_contact_email.contact_id, &index);
374                 if (ret != CONTACTS_ERROR_NONE) {
375                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
376                         contacts_list_destroy(contacts, true);
377                         return MSG_ERR_UNKNOWN;
378                 }
379
380                 ret = contacts_db_get_record(_contacts_contact._uri, index, &contact);
381                 if (ret != CONTACTS_ERROR_NONE) {
382                         MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
383                         contacts_list_destroy(contacts, true);
384                         return MSG_ERR_UNKNOWN;
385                 }
386         }
387
388         contacts_list_destroy(contacts, true);
389
390         ret = contacts_record_get_int(contact, _contacts_contact.id, (int*)&pContactInfo->contactId);
391         if (ret != CONTACTS_ERROR_NONE) {
392                 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
393                 contacts_record_destroy(contact, true);
394                 return MSG_ERR_UNKNOWN;
395         }
396
397         MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
398
399         ret = contacts_record_get_int(contact, _contacts_contact.address_book_id, (int*)&pContactInfo->addrbookId);
400         if (ret != CONTACTS_ERROR_NONE) {
401                 MSG_DEBUG("contacts_db_get_record() Error [%d]", ret);
402                 contacts_record_destroy(contact, true);
403                 return MSG_ERR_UNKNOWN;
404         }
405
406         MSG_DEBUG("Address Book ID [%d]", pContactInfo->addrbookId);
407
408         char* strImagePath = NULL;
409         ret = contacts_record_get_str_p(contact, _contacts_contact.image_thumbnail_path, &strImagePath);
410         if (ret != CONTACTS_ERROR_NONE) {
411                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
412         }
413         if (strImagePath != NULL) {
414                 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
415                 MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
416         }
417
418         char* alerttonePath = NULL;
419         ret = contacts_record_get_str_p(contact, _contacts_contact.message_alert, &alerttonePath);
420         if (ret != CONTACTS_ERROR_NONE) {
421                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
422         }
423
424         if (MsgAccessFile(alerttonePath, F_OK) == false) {
425                 alerttonePath = NULL;
426         }
427
428         if (alerttonePath != NULL) {
429                 MSG_DEBUG("alert tone Path [%s]", alerttonePath);
430                 strncpy(pContactInfo->alerttonePath , alerttonePath, MSG_FILEPATH_LEN_MAX);
431         } else {
432                 MSG_DEBUG("alert tone Path for this contact is default");
433                 count = 0;
434                 ret = contacts_record_get_child_record_count(contact, _contacts_contact.group_relation, &count);
435                 if (ret != CONTACTS_ERROR_NONE) {
436                         MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
437                 }
438
439                 contacts_record_h group_relation_record;
440
441                 for (int i = 0; i < count; i++) {
442                         int group_id = 0;
443                         contacts_record_get_child_record_at_p(contact, _contacts_contact.group_relation, i, &group_relation_record);
444                         contacts_record_get_int(group_relation_record, _contacts_group_relation.group_id, &group_id);
445
446                         contacts_record_h group_record;
447                         contacts_db_get_record(_contacts_group._uri, group_id, &group_record);
448
449                         MSG_DEBUG("Group ID = [%d]", group_id);
450
451                         char *group_ringtone_path;
452                         ret = contacts_record_get_str_p(group_record, _contacts_group.message_alert, &group_ringtone_path);
453                         if (ret != CONTACTS_ERROR_NONE) {
454                                 MSG_DEBUG("contacts_record_get_child_record_count() Error [%d]", ret);
455                         } else {
456                                 if (group_ringtone_path) {
457                                         MSG_DEBUG("Msg alert_tone is change to [%s] as contact group", group_ringtone_path);
458                                         memset(pContactInfo->alerttonePath, 0x00, sizeof(pContactInfo->alerttonePath));
459                                         snprintf(pContactInfo->alerttonePath, sizeof(pContactInfo->alerttonePath), "%s", group_ringtone_path);
460                                         contacts_record_destroy(group_record, true);
461                                         break;
462                                 }
463                         }
464                         contacts_record_destroy(group_record, true);
465                 }
466         }
467
468         char* vibrationPath = NULL;
469         ret = contacts_record_get_str_p(contact, _contacts_contact.vibration, &vibrationPath);
470         if (ret != CONTACTS_ERROR_NONE) {
471                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
472         }
473         if (vibrationPath != NULL) {
474                 MSG_DEBUG("vibration Path [%s]", vibrationPath);
475                 strncpy(pContactInfo->vibrationPath , vibrationPath, MSG_FILEPATH_LEN_MAX);
476         }
477
478         char* displayName = NULL;
479         ret = contacts_record_get_str_p(contact, _contacts_contact.display_name, &displayName);
480         if (ret != CONTACTS_ERROR_NONE) {
481                 MSG_DEBUG("contacts_record_get_str_p() Error [%d]", ret);
482         }
483         if (displayName != NULL) {
484                 MSG_DEBUG("displayName [%s]", displayName);
485                 strncpy(pContactInfo->firstName , displayName, MAX_DISPLAY_NAME_LEN);
486         }
487
488         contacts_record_destroy(contact, true);
489
490         MSG_END();
491
492         return MSG_SUCCESS;
493 }
494
495
496 msg_error_t MsgGetContactSearchList(const char *pSearchVal, MSG_ADDRESS_INFO_S **pAddrInfo, int *count)
497 {
498         MSG_BEGIN();
499
500         msg_error_t err = MSG_SUCCESS;
501
502         *count = 0;
503
504         if (pSearchVal == NULL) {
505                 MSG_DEBUG("pSearchVal is NULL.");
506                 return MSG_ERR_NULL_POINTER;
507         }
508
509         if (pAddrInfo == NULL) {
510                 MSG_DEBUG("pAddrInfo is NULL.");
511                 return MSG_ERR_NULL_POINTER;
512         }
513
514         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
515                 MSG_DEBUG("MsgOpenContactSvc fail.");
516                 return err;
517         }
518
519         if (!isContactSvcConnected) {
520                 MSG_DEBUG("Contact Service Not Opened.");
521                 return MSG_ERR_UNKNOWN;
522         }
523
524         MSG_SEC_DEBUG("pSearchVal [%s]", pSearchVal);
525
526         int ret = 0;
527         unsigned int index = 0;
528         contacts_query_h query = NULL;
529         contacts_filter_h filter = NULL;
530         contacts_list_h personNumbers = NULL;
531
532         ret = contacts_query_create(_contacts_person_number._uri, &query);
533         ret = contacts_filter_create(_contacts_person_number._uri, &filter);
534
535         ret = contacts_filter_add_str(filter, _contacts_person_number.display_name, CONTACTS_MATCH_CONTAINS, pSearchVal);
536         ret = contacts_query_set_filter(query, filter);
537
538         ret = contacts_db_get_records_with_query(query, 0, 0, &personNumbers);
539         if (ret != CONTACTS_ERROR_NONE) {
540                 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
541                 contacts_query_destroy(query);
542                 contacts_filter_destroy(filter);
543                 contacts_list_destroy(personNumbers, true);
544                 return MSG_ERR_UNKNOWN;
545         }
546
547         ret = contacts_list_get_count(personNumbers, count);
548         if (*count == 0 || ret != CONTACTS_ERROR_NONE) {
549                 MSG_DEBUG("No Serach Data from Contact Service.");
550                 *count = 0;
551                 contacts_query_destroy(query);
552                 contacts_filter_destroy(filter);
553                 contacts_list_destroy(personNumbers, true);
554                 return MSG_SUCCESS;
555         }
556
557         contacts_query_destroy(query);
558         contacts_filter_destroy(filter);
559
560         MSG_DEBUG(" *count [%d]", *count);
561
562         *pAddrInfo = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * (*count)];
563         memset(*pAddrInfo, 0x00, (sizeof(MSG_ADDRESS_INFO_S) * (*count)));
564
565         contacts_record_h personNumber = NULL;
566
567         while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(personNumbers, &personNumber)) {
568                 char* normalizedNumber = NULL;
569                 ret = contacts_record_get_str(personNumber, _contacts_person_number.normalized_number, &normalizedNumber);
570                 if (ret != CONTACTS_ERROR_NONE) {
571                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
572                 }
573                 else if (normalizedNumber) {
574                         MSG_DEBUG("normalizedNumber [%s]", normalizedNumber);
575                         strncpy((*pAddrInfo)[index].addressVal, normalizedNumber, MAX_ADDRESS_VAL_LEN);
576                 }
577
578                 contacts_list_next(personNumbers);
579                 index++;
580         }
581
582         contacts_list_destroy(personNumbers, true);
583
584         MSG_END();
585
586         return MSG_SUCCESS;
587 }
588
589
590 int MsgGetContactNameOrder()
591 {
592         contacts_name_display_order_e order = CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST;
593
594         return (int)order;
595 }
596
597
598 msg_error_t MsgGetContactStyleDisplayName(const char *first, const char *last, const char *middle, const char *prefix, const char *suffix, int contactNameOrder, char *displayName, unsigned int size)
599 {
600         if (first == NULL || last == NULL || middle == NULL || prefix == NULL || suffix == NULL || displayName == NULL || size ==0) {
601                 MSG_DEBUG("Invalid parameter.");
602                 return MSG_ERR_INVALID_PARAMETER;
603         }
604
605         if (contactNameOrder == CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST) {
606                 if (strlen(prefix) > 0) {
607                         strncpy(displayName, prefix, size);
608                 }
609
610                 if (strlen(first) > 0) {
611                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
612                         strncat(displayName, first, size-strlen(displayName));
613                 }
614
615                 if (strlen(middle) > 0) {
616                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
617
618                         strncat(displayName, middle, size-strlen(displayName));
619                 }
620
621                 if (strlen(last) > 0) {
622                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
623                         strncat(displayName, last, size-strlen(displayName));
624                 }
625
626                 if (strlen(suffix) > 0) {
627                         if (strlen(displayName) > 0) strncat(displayName, ", ", size-strlen(displayName));
628                         strncat(displayName, suffix, size-strlen(displayName));
629                 }
630         } else {
631                 if (strlen(prefix) > 0) {
632                         strncpy(displayName, prefix, size);
633                 }
634
635                 if (strlen(last) > 0) {
636                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
637                         strncat(displayName, last, size-strlen(displayName));
638
639                         if (strlen(first) > 0 || strlen(middle) > 0 || strlen(suffix) > 0)
640                                 strncat(displayName, ",", size-strlen(displayName));
641                 }
642
643                 if (strlen(first) > 0) {
644                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
645                         strncat(displayName, first, size-strlen(displayName));
646                 }
647
648                 if (strlen(middle) > 0) {
649                         if (strlen(displayName) > 0) strncat(displayName, " ", size-strlen(displayName));
650                         strncat(displayName, middle, size-strlen(displayName));
651                 }
652
653                 if (strlen(suffix) > 0) {
654                         if (strlen(displayName) > 0) strncat(displayName, ", ", size-strlen(displayName));
655                         strncat(displayName, suffix, size-strlen(displayName));
656                 }
657         }
658
659         MSG_SEC_DEBUG("displayName [%s]", displayName);
660
661         return MSG_SUCCESS;
662 }
663
664
665 void MsgAddPhoneLog(const MSG_MESSAGE_INFO_S *pMsgInfo)
666 {
667         msg_error_t err = MSG_SUCCESS;
668
669         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
670                 MSG_DEBUG("MsgOpenContactSvc fail.");
671                 return;
672         }
673
674         if (!isContactSvcConnected) {
675                 MSG_DEBUG("Contact Service Not Opened.");
676                 return;
677         }
678
679         if (pMsgInfo->nAddressCnt < 1) {
680                 MSG_DEBUG("address count is [%d]", pMsgInfo->nAddressCnt);
681                 return;
682         }
683
684         for (int i = 0; pMsgInfo->nAddressCnt > i; i++) {
685                 int ret = 0;
686                 contacts_record_h plog = NULL;
687
688                 ret = contacts_record_create(_contacts_phone_log._uri, &plog);
689                 if (ret != CONTACTS_ERROR_NONE) {
690                         MSG_DEBUG("contacts_record_create() Error [%d]", ret);
691                         contacts_record_destroy(plog, true);
692                         break;
693                 }
694
695                 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[i].addressVal);
696                 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
697
698                 char strText[101];
699                 memset(strText, 0x00, sizeof(strText));
700
701                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
702                         strncpy(strText, pMsgInfo->msgText, 100);
703                         MSG_SEC_DEBUG("msgText : %s", strText);
704                 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
705                         if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
706                                 strncpy(strText, pMsgInfo->subject, 100);
707                                 MSG_SEC_DEBUG("subject : %s", strText);
708                         } else {
709                                 char *pFileData = NULL;
710                                 gsize fileSize = 0;
711
712                                 if (pMsgInfo->msgText[0] != '\0' && g_file_get_contents(pMsgInfo->msgText, &pFileData, &fileSize, NULL) == true) {
713                                         if (pFileData)
714                                                 strncpy(strText, pFileData, 100);
715                                 }
716
717                                 if (pFileData)
718                                         g_free(pFileData);
719
720                                 MSG_SEC_DEBUG("msgText : %s", strText);
721                         }
722                 }
723
724                 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
725                 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
726
727                 if (pMsgInfo->folderId == MSG_INBOX_ID) {
728                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
729                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMMING);
730                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
731                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMMING);
732                 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
733                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
734                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
735                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
736                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
737                 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
738                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
739                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
740                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
741                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
742                 }
743
744                 ret = contacts_db_insert_record(plog, NULL);
745                 if (ret != CONTACTS_ERROR_NONE) {
746                         MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
747                 }
748
749                 contacts_record_destroy(plog, true);
750         }
751 }
752
753
754 void MsgDeletePhoneLog(msg_message_id_t msgId)
755 {
756         msg_error_t err = MSG_SUCCESS;
757
758         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
759                 MSG_DEBUG("MsgOpenContactSvc fail.");
760                 return;
761         }
762
763         MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
764
765         if (!isContactSvcConnected) {
766                 MSG_DEBUG("Contact Service Not Opened.");
767                 return;
768         }
769
770         int ret = CONTACTS_ERROR_NONE;
771         int index = 0;
772         int count = 0;
773         contacts_query_h query;
774         contacts_filter_h filter;
775         contacts_list_h plogs = NULL;
776
777         ret = contacts_query_create(_contacts_phone_log._uri, &query);
778         ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
779
780         ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
781
782
783         ret = contacts_query_set_filter(query, filter);
784         ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
785
786         ret = contacts_list_get_count(plogs, &count);
787
788         if (count == 0) {
789                 MSG_DEBUG("No Serach Data from Contact Service.");
790         } else {
791                 contacts_record_h plog = NULL;
792
793                 ret = contacts_list_get_current_record_p(plogs, &plog);
794                 if (ret != CONTACTS_ERROR_NONE) {
795                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
796                 }
797
798                 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
799                 if (ret != CONTACTS_ERROR_NONE) {
800                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
801                 }
802
803                 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
804                 if (ret != CONTACTS_ERROR_NONE) {
805                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
806                 } else {
807                         MSG_DEBUG("contacts_db_delete_record() Success.");
808                 }
809         }
810
811         contacts_query_destroy(query);
812         contacts_filter_destroy(filter);
813         contacts_list_destroy(plogs, true);
814 }
815
816
817 bool checkBlockingMode(char *address, bool *pisFavorites)
818 {
819 #if 0
820         msg_error_t err = MSG_SUCCESS;
821
822         if (pisFavorites != NULL) *pisFavorites = false;
823
824         bool isBlockModeOn = false;
825         bool isblock = true;
826
827         MsgSettingGetBool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &isBlockModeOn);
828
829         int blockModeType = -1;
830
831         blockModeType = MsgSettingGetInt(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE);
832
833         if (!isBlockModeOn)
834                 isblock = false;
835         else if (blockModeType < 0)
836                 isblock = false;
837
838
839         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
840                 MSG_DEBUG("MsgOpenContactSvc fail.");
841                 return isblock;
842         }
843
844         if (!isContactSvcConnected) {
845                 MSG_DEBUG("Contact Service Not Opened.");
846                 return isblock;
847         }
848
849         MSG_SEC_DEBUG("Address Value [%s]", address);
850
851         if (strlen(address) > (MAX_PHONE_NUMBER_LEN+1)) {
852                 MSG_SEC_DEBUG("Phone Number is too long [%s]", address);
853                 return isblock;
854         }
855
856         int ret = 0;
857         int personId = 0;
858         bool isFavorites = false;
859         int count = 0;
860         contacts_query_h query = NULL;
861         contacts_filter_h filter = NULL;
862         contacts_list_h personList = NULL;
863
864         ret = contacts_query_create(_contacts_person_number._uri, &query);
865         ret = contacts_filter_create(_contacts_person_number._uri, &filter);
866
867         ret = contacts_filter_add_str(filter, _contacts_person_number.number_filter, CONTACTS_MATCH_EXACTLY, address);
868
869         ret = contacts_query_set_filter(query, filter);
870         ret = contacts_db_get_records_with_query(query, 0, 1, &personList);
871
872         contacts_query_destroy(query);
873         contacts_filter_destroy(filter);
874
875         if (ret != CONTACTS_ERROR_NONE) {
876                 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
877                 contacts_list_destroy(personList, true);
878                 return isblock;
879         }
880
881         ret = contacts_list_get_count(personList, &count);
882
883         if (count == 0 || ret != CONTACTS_ERROR_NONE) {
884                 MSG_DEBUG("No Serach Data from Contact Service.");
885                 contacts_list_destroy(personList, true);
886                 return isblock;
887         } else if (ret == CONTACTS_ERROR_NONE && count > 0
888                         && blockModeType == 1) { /* For All contacts allow in blocking mode. */
889                 isblock = false;
890         }
891
892         contacts_record_h person = NULL;
893
894         ret = contacts_list_get_current_record_p(personList, &person);
895         if (ret != CONTACTS_ERROR_NONE) {
896                 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
897                 contacts_list_destroy(personList, true);
898                 return isblock;
899         }
900
901         ret = contacts_record_get_int(person, _contacts_person_number.person_id, &personId);
902         if (ret != CONTACTS_ERROR_NONE) {
903                 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
904                 contacts_list_destroy(personList, true);
905                 return isblock;
906         }
907
908         MSG_DEBUG("personId [%d]", personId);
909
910         ret = contacts_record_get_bool(person, _contacts_person_number.is_favorite, &isFavorites);
911         if (ret != CONTACTS_ERROR_NONE) {
912                 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
913                 contacts_list_destroy(personList, true);
914                 return isblock;
915         }
916
917         contacts_list_destroy(personList, true);
918
919         switch (blockModeType) {
920         case 2: { /* For Favorites allow in blocking mode. */
921                 if (isFavorites) isblock = false;
922                 break;
923         }
924         case 3: { /* For Custom allow in blocking mode. */
925                 char *allowList = MsgSettingGetString(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_LIST);
926                 char *temp = NULL;
927                 char *personIdStr = strtok_r(allowList, " ,", &temp);
928                 while (personIdStr != NULL) {
929                         MSG_DEBUG("personIdStr [%s]", personIdStr);
930                         if (personId == atoi(personIdStr)) {
931                                 MSG_DEBUG("In allow list.");
932                                 isblock = false;
933                                 break;
934                         }
935                         personIdStr = strtok_r(NULL, " ,", &temp);
936                 }
937
938                 if (allowList) {
939                         free(allowList);
940                         allowList = NULL;
941                 }
942
943                 break;
944         }
945         default: /* Wrong blocking mode type. */
946                 break;
947         }
948
949         if (pisFavorites != NULL) *pisFavorites = isFavorites;
950
951         return isblock;
952 #else
953         if (pisFavorites != NULL)
954                 *pisFavorites = false;
955
956         return false;
957 #endif
958 }
959
960 int MsgContactGetMinMatchDigit()
961 {
962         return phonenumberMinMatchDigit;
963 }
964
965
966 void MsgConvertNumber(const char* pSrcNum, char* pDestNum, int destSize)
967 {
968         int len;
969         const char *temp_number;
970
971         if ('+' == pSrcNum[0]) {
972                 len = countryCodeLength(&pSrcNum[1]);
973                 temp_number = pSrcNum + len +1;
974         } else if ('0' == pSrcNum[0]) {
975                 if ('0' == pSrcNum[1]) {
976                         len = countryCodeLength(&pSrcNum[2]);
977                         temp_number = pSrcNum + len +2;
978                 } else {
979                         temp_number = pSrcNum+1;
980                 }
981         } else {
982                 temp_number = pSrcNum;
983         }
984
985         strncpy(pDestNum, temp_number, destSize);
986 }
987
988
989 bool MsgIsNumber(const char* pSrc)
990 {
991         int len = strlen(pSrc);
992
993         for (int i = 0; i < len; ++i) {
994                 if (i == 0 && pSrc[i] == '+')
995                         continue;
996
997                 if (pSrc[i] < '0' || pSrc[i] > '9') {
998                         return false;
999                 }
1000         }
1001
1002         return true;
1003 }
1004