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