b438b9ac99266ffb752de13b5e9b28dd1d58ce7d
[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_int(plog, _contacts_phone_log.sim_slot_no, (int)pMsgInfo->sim_idx-1);
696                 contacts_record_set_str(plog, _contacts_phone_log.address, (char*)pMsgInfo->addressList[i].addressVal);
697                 contacts_record_set_int(plog, _contacts_phone_log.log_time, (int)time(NULL));
698
699                 char strText[101];
700                 memset(strText, 0x00, sizeof(strText));
701
702                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
703                         strncpy(strText, pMsgInfo->msgText, 100);
704                         MSG_SEC_DEBUG("msgText : %s", strText);
705                 } else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
706                         if (strlen(pMsgInfo->subject) > 0 || pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
707                                 strncpy(strText, pMsgInfo->subject, 100);
708                                 MSG_SEC_DEBUG("subject : %s", strText);
709                         } else {
710                                 char *pFileData = NULL;
711                                 gsize fileSize = 0;
712
713                                 if (pMsgInfo->msgText[0] != '\0' && g_file_get_contents(pMsgInfo->msgText, &pFileData, &fileSize, NULL) == true) {
714                                         if (pFileData)
715                                                 strncpy(strText, pFileData, 100);
716                                 }
717
718                                 if (pFileData)
719                                         g_free(pFileData);
720
721                                 MSG_SEC_DEBUG("msgText : %s", strText);
722                         }
723                 }
724
725                 contacts_record_set_str(plog, _contacts_phone_log.extra_data2, strText);
726                 contacts_record_set_int(plog, _contacts_phone_log.extra_data1, (int)pMsgInfo->msgId);
727
728                 if (pMsgInfo->folderId == MSG_INBOX_ID) {
729                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
730                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_INCOMING);
731                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
732                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_INCOMING);
733                 } else if (pMsgInfo->folderId == MSG_OUTBOX_ID) {
734                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
735                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_OUTGOING);
736                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
737                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_OUTGOING);
738                 } else if (pMsgInfo->folderId == MSG_SPAMBOX_ID) {
739                         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE)
740                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_SMS_BLOCKED);
741                         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
742                                 contacts_record_set_int(plog, _contacts_phone_log.log_type, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
743                 }
744
745                 ret = contacts_db_insert_record(plog, NULL);
746                 if (ret != CONTACTS_ERROR_NONE) {
747                         MSG_DEBUG("contacts_db_insert_record() Error [%d]", ret);
748                 }
749
750                 contacts_record_destroy(plog, true);
751         }
752 }
753
754
755 void MsgDeletePhoneLog(msg_message_id_t msgId)
756 {
757         msg_error_t err = MSG_SUCCESS;
758
759         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
760                 MSG_DEBUG("MsgOpenContactSvc fail.");
761                 return;
762         }
763
764         MSG_DEBUG("MsgDeletePhoneLog [%d]", msgId);
765
766         if (!isContactSvcConnected) {
767                 MSG_DEBUG("Contact Service Not Opened.");
768                 return;
769         }
770
771         int ret = CONTACTS_ERROR_NONE;
772         int index = 0;
773         int count = 0;
774         contacts_query_h query;
775         contacts_filter_h filter;
776         contacts_list_h plogs = NULL;
777
778         ret = contacts_query_create(_contacts_phone_log._uri, &query);
779         ret = contacts_filter_create(_contacts_phone_log._uri, &filter);
780
781         ret = contacts_filter_add_int(filter, _contacts_phone_log.extra_data1, CONTACTS_MATCH_EQUAL, (int)msgId);
782
783
784         ret = contacts_query_set_filter(query, filter);
785         ret = contacts_db_get_records_with_query(query, 0, 1, &plogs);
786
787         ret = contacts_list_get_count(plogs, &count);
788
789         if (count == 0) {
790                 MSG_DEBUG("No Serach Data from Contact Service.");
791         } else {
792                 contacts_record_h plog = NULL;
793
794                 ret = contacts_list_get_current_record_p(plogs, &plog);
795                 if (ret != CONTACTS_ERROR_NONE) {
796                         MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
797                 }
798
799                 ret = contacts_record_get_int(plog, _contacts_phone_log.id, &index);
800                 if (ret != CONTACTS_ERROR_NONE) {
801                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
802                 }
803
804                 ret = contacts_db_delete_record(_contacts_phone_log._uri, index);
805                 if (ret != CONTACTS_ERROR_NONE) {
806                         MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
807                 } else {
808                         MSG_DEBUG("contacts_db_delete_record() Success.");
809                 }
810         }
811
812         contacts_query_destroy(query);
813         contacts_filter_destroy(filter);
814         contacts_list_destroy(plogs, true);
815 }
816
817
818 bool checkBlockingMode(char *address, bool *pisFavorites)
819 {
820 #if 0
821         msg_error_t err = MSG_SUCCESS;
822
823         if (pisFavorites != NULL) *pisFavorites = false;
824
825         bool isBlockModeOn = false;
826         bool isblock = true;
827
828         MsgSettingGetBool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &isBlockModeOn);
829
830         int blockModeType = -1;
831
832         blockModeType = MsgSettingGetInt(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE);
833
834         if (!isBlockModeOn)
835                 isblock = false;
836         else if (blockModeType < 0)
837                 isblock = false;
838
839
840         if ((err = MsgOpenContactSvc()) != MSG_SUCCESS) {
841                 MSG_DEBUG("MsgOpenContactSvc fail.");
842                 return isblock;
843         }
844
845         if (!isContactSvcConnected) {
846                 MSG_DEBUG("Contact Service Not Opened.");
847                 return isblock;
848         }
849
850         MSG_SEC_DEBUG("Address Value [%s]", address);
851
852         if (strlen(address) > (MAX_PHONE_NUMBER_LEN+1)) {
853                 MSG_SEC_DEBUG("Phone Number is too long [%s]", address);
854                 return isblock;
855         }
856
857         int ret = 0;
858         int personId = 0;
859         bool isFavorites = false;
860         int count = 0;
861         contacts_query_h query = NULL;
862         contacts_filter_h filter = NULL;
863         contacts_list_h personList = NULL;
864
865         ret = contacts_query_create(_contacts_person_number._uri, &query);
866         ret = contacts_filter_create(_contacts_person_number._uri, &filter);
867
868         ret = contacts_filter_add_str(filter, _contacts_person_number.number_filter, CONTACTS_MATCH_EXACTLY, address);
869
870         ret = contacts_query_set_filter(query, filter);
871         ret = contacts_db_get_records_with_query(query, 0, 1, &personList);
872
873         contacts_query_destroy(query);
874         contacts_filter_destroy(filter);
875
876         if (ret != CONTACTS_ERROR_NONE) {
877                 MSG_DEBUG("contacts_db_get_records_with_query() Error [%d]", ret);
878                 contacts_list_destroy(personList, true);
879                 return isblock;
880         }
881
882         ret = contacts_list_get_count(personList, &count);
883
884         if (count == 0 || ret != CONTACTS_ERROR_NONE) {
885                 MSG_DEBUG("No Serach Data from Contact Service.");
886                 contacts_list_destroy(personList, true);
887                 return isblock;
888         } else if (ret == CONTACTS_ERROR_NONE && count > 0
889                         && blockModeType == 1) { /* For All contacts allow in blocking mode. */
890                 isblock = false;
891         }
892
893         contacts_record_h person = NULL;
894
895         ret = contacts_list_get_current_record_p(personList, &person);
896         if (ret != CONTACTS_ERROR_NONE) {
897                 MSG_DEBUG("contacts_list_get_current_record_p() Error [%d]", ret);
898                 contacts_list_destroy(personList, true);
899                 return isblock;
900         }
901
902         ret = contacts_record_get_int(person, _contacts_person_number.person_id, &personId);
903         if (ret != CONTACTS_ERROR_NONE) {
904                 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
905                 contacts_list_destroy(personList, true);
906                 return isblock;
907         }
908
909         MSG_DEBUG("personId [%d]", personId);
910
911         ret = contacts_record_get_bool(person, _contacts_person_number.is_favorite, &isFavorites);
912         if (ret != CONTACTS_ERROR_NONE) {
913                 MSG_DEBUG("contacts_record_get_int() Error [%d]", ret);
914                 contacts_list_destroy(personList, true);
915                 return isblock;
916         }
917
918         contacts_list_destroy(personList, true);
919
920         switch (blockModeType) {
921         case 2: { /* For Favorites allow in blocking mode. */
922                 if (isFavorites) isblock = false;
923                 break;
924         }
925         case 3: { /* For Custom allow in blocking mode. */
926                 char *allowList = MsgSettingGetString(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_LIST);
927                 char *temp = NULL;
928                 char *personIdStr = strtok_r(allowList, " ,", &temp);
929                 while (personIdStr != NULL) {
930                         MSG_DEBUG("personIdStr [%s]", personIdStr);
931                         if (personId == atoi(personIdStr)) {
932                                 MSG_DEBUG("In allow list.");
933                                 isblock = false;
934                                 break;
935                         }
936                         personIdStr = strtok_r(NULL, " ,", &temp);
937                 }
938
939                 if (allowList) {
940                         free(allowList);
941                         allowList = NULL;
942                 }
943
944                 break;
945         }
946         default: /* Wrong blocking mode type. */
947                 break;
948         }
949
950         if (pisFavorites != NULL) *pisFavorites = isFavorites;
951
952         return isblock;
953 #else
954         if (pisFavorites != NULL)
955                 *pisFavorites = false;
956
957         return false;
958 #endif
959 }
960
961 int MsgContactGetMinMatchDigit()
962 {
963         return phonenumberMinMatchDigit;
964 }
965
966
967 void MsgConvertNumber(const char* pSrcNum, char* pDestNum, int destSize)
968 {
969         int len;
970         const char *temp_number;
971
972         if ('+' == pSrcNum[0]) {
973                 len = countryCodeLength(&pSrcNum[1]);
974                 temp_number = pSrcNum + len +1;
975         } else if ('0' == pSrcNum[0]) {
976                 if ('0' == pSrcNum[1]) {
977                         len = countryCodeLength(&pSrcNum[2]);
978                         temp_number = pSrcNum + len +2;
979                 } else {
980                         temp_number = pSrcNum+1;
981                 }
982         } else {
983                 temp_number = pSrcNum;
984         }
985
986         strncpy(pDestNum, temp_number, destSize);
987 }
988
989
990 bool MsgIsNumber(const char* pSrc)
991 {
992         int len = strlen(pSrc);
993
994         for (int i = 0; i < len; ++i) {
995                 if (i == 0 && pSrc[i] == '+')
996                         continue;
997
998                 if (pSrc[i] < '0' || pSrc[i] > '9') {
999                         return false;
1000                 }
1001         }
1002
1003         return true;
1004 }
1005