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