use sd_get_active_uids() instead of sd_get_uids() for getting active user id
[platform/core/messaging/msg-service.git] / utils / MsgUtilFunction.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 <systemd/sd-login.h>
18
19 #include "MsgDebug.h"
20 #include "MsgContact.h"
21 #include "MsgGconfWrapper.h"
22 #include "MsgUtilFile.h"
23 #include "MsgUtilFunction.h"
24 #include "MsgUtilStorage.h"
25
26 #include <system_info.h>
27 #include <libintl.h>
28 #include <locale.h>
29 #include <vconf.h>
30 #include <ctype.h>
31 #include <aul.h>
32 #include <aul_svc.h>
33
34 typedef struct _msg_launch_app_data {
35         char *app_id;
36         bundle *bundle_data;
37 } msg_launch_app_data;
38
39 #define DEFAULT_MIN_MATCH_DIGIT 8
40
41 enum _FEATURE_INDEX_E {
42         FEATURE_INDEX_SMS = 0,
43         FEATURE_INDEX_MMS = 1,
44 };
45
46 static bool b_feature_cache_flag = false;
47 static bool b_feature_support[] = {
48                 [FEATURE_INDEX_SMS] = false,
49                 [FEATURE_INDEX_MMS] = false,
50 };
51
52 static int dpm_policy_enable[] = {
53                 [MSG_UNKNOWN_TYPE] = 0,
54                 [MSG_SMS_TYPE] = 1,
55                 [MSG_MMS_TYPE] = 1,
56 };
57
58 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
59 static int phonenumberMinMatchDigit = -1;
60 #endif
61
62 /*==================================================================================================
63                                      FUNCTION IMPLEMENTATION
64 ==================================================================================================*/
65
66 bool MsgCheckFeatureSupport(const char *feature_name)
67 {
68         bool result = false;
69
70         if (b_feature_cache_flag == false) {
71                 if (system_info_get_platform_bool(MSG_TELEPHONY_SMS_FEATURE, &b_feature_support[FEATURE_INDEX_SMS]) != SYSTEM_INFO_ERROR_NONE)
72                         MSG_WARN("fail to system_info_get_platform_bool [%s]", MSG_TELEPHONY_SMS_FEATURE);
73
74                 if (system_info_get_platform_bool(MSG_TELEPHONY_MMS_FEATURE, &b_feature_support[FEATURE_INDEX_MMS]) != SYSTEM_INFO_ERROR_NONE)
75                         MSG_WARN("fail to system_info_get_platform_bool [%s]", MSG_TELEPHONY_MMS_FEATURE);
76
77                 MSG_INFO("[%s] feature is [%d]", MSG_TELEPHONY_SMS_FEATURE, b_feature_support[FEATURE_INDEX_SMS]);
78                 MSG_INFO("[%s] feature is [%d]", MSG_TELEPHONY_MMS_FEATURE, b_feature_support[FEATURE_INDEX_MMS]);
79
80                 b_feature_cache_flag = true;
81         }
82
83         if (!g_strcmp0(feature_name, MSG_TELEPHONY_SMS_FEATURE)) {
84                 result = b_feature_support[FEATURE_INDEX_SMS];
85         } else if (!g_strcmp0(feature_name, MSG_TELEPHONY_MMS_FEATURE)) {
86                 result = b_feature_support[FEATURE_INDEX_MMS];
87         }
88
89         return result;
90 }
91
92
93 int MsgContactGetMinMatchDigit()
94 {
95 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
96         if (phonenumberMinMatchDigit <= 0) {
97                 if (MsgSettingGetInt(VCONFKEY_CONTACTS_SVC_PHONENUMBER_MIN_MATCH_DIGIT, &phonenumberMinMatchDigit) != MSG_SUCCESS) {
98                         MSG_INFO("MsgSettingGetInt() is failed");
99                 }
100                 MSG_DEBUG("phonenumberMinMatchDigit [%d]", phonenumberMinMatchDigit);
101
102                 if (phonenumberMinMatchDigit < 1) {
103                         phonenumberMinMatchDigit = DEFAULT_MIN_MATCH_DIGIT;
104                 }
105         }
106
107         return phonenumberMinMatchDigit;
108 #else
109         return DEFAULT_MIN_MATCH_DIGIT;
110 #endif
111 }
112
113 /* Encoders */
114 int MsgEncodeCountInfo(MSG_COUNT_INFO_S *pCountInfo, char **ppDest)
115 {
116         int dataSize = 0;
117
118         dataSize = sizeof(MSG_COUNT_INFO_S);
119
120         *ppDest = (char*)new char[dataSize];
121
122         void* p = (void*)*ppDest;
123
124         memcpy(p, pCountInfo, dataSize);
125
126         return dataSize;
127 }
128
129
130 int MsgEncodeRecipientList(MSG_RECIPIENTS_LIST_S *pRecipientList, char **ppDest)
131 {
132         int count = 0, dataSize = 0;
133
134         count = pRecipientList->recipientCnt;
135         dataSize = sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*count);
136
137         *ppDest = (char*)new char[dataSize];
138
139         void* p = (void*)*ppDest;
140
141         memcpy(p, &count, sizeof(int));
142         p = (void*)((char*)p + sizeof(int));
143
144         for (int i = 0; i < count; i++) {
145                 memcpy(p, &(pRecipientList->recipientAddr[i]), sizeof(MSG_ADDRESS_INFO_S));
146                 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
147         }
148
149         return dataSize;
150 }
151
152
153 int MsgEncodeCountByMsgType(int MsgCount, char **ppDest)
154 {
155         int dataSize = 0;
156
157         dataSize = sizeof(int);
158
159         *ppDest = (char*)new char[dataSize];
160
161         void* p = (void*)*ppDest;
162
163         memcpy(p, &MsgCount, dataSize);
164
165         return dataSize;
166 }
167
168
169 int MsgEncodeMsgId(msg_message_id_t *pMsgId, char **ppDest)
170 {
171         int dataSize = 0;
172
173         dataSize = (sizeof(msg_message_id_t));
174
175         *ppDest = (char*)new char[dataSize];
176
177         void* p = (void*)*ppDest;
178
179         memcpy(p, pMsgId, dataSize);
180
181         return dataSize;
182 }
183
184
185 int MsgEncodeMsgInfo(const MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
186 {
187         int dataSize = 0;
188
189         dataSize = sizeof(MSG_MESSAGE_INFO_S) + (sizeof(MSG_ADDRESS_INFO_S)*pMsgInfo->nAddressCnt);
190
191         *ppDest = (char*)new char[dataSize];
192
193         void* p = (void*)*ppDest;
194
195         memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
196
197         p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
198
199         for (int i=0; i < pMsgInfo->nAddressCnt; i++) {
200                 memcpy(p, &(pMsgInfo->addressList[i]), sizeof(MSG_ADDRESS_INFO_S));
201                 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
202         }
203
204         return dataSize;
205 }
206
207
208 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo, char **ppDest)
209 {
210         int dataSize = 0;
211
212         dataSize = (sizeof(MSG_MESSAGE_INFO_S) + sizeof(MSG_SENDINGOPT_INFO_S) + (sizeof(MSG_ADDRESS_INFO_S)*pMsgInfo->nAddressCnt));
213
214         *ppDest = (char*)new char[dataSize];
215
216         void* p = (void*)*ppDest;
217
218         memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
219
220         p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
221
222         memcpy(p, pSendOptInfo, sizeof(MSG_SENDINGOPT_INFO_S));
223
224         p = (void*)((char*)p + sizeof(MSG_SENDINGOPT_INFO_S));
225
226         for (int i=0; i < pMsgInfo->nAddressCnt; i++) {
227                 memcpy(p, &(pMsgInfo->addressList[i]), sizeof(MSG_ADDRESS_INFO_S));
228                 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
229         }
230
231         return dataSize;
232 }
233
234
235 int MsgEncodeFolderList(msg_struct_list_s *pFolderList, char **ppDest)
236 {
237         int count = 0, dataSize = 0;
238
239         count = pFolderList->nCount;
240         dataSize = sizeof(int) + (sizeof(MSG_FOLDER_INFO_S)*count);
241
242         *ppDest = (char*)new char[dataSize];
243
244         void* p = (void*)*ppDest;
245
246         memcpy(p, &count, sizeof(int));
247         p = (void*)((char*)p + sizeof(int));
248
249         msg_struct_s *folder_info = NULL;
250
251         for (int i = 0; i < count; i++) {
252                 folder_info = (msg_struct_s *)pFolderList->msg_struct_info[i];
253                 memcpy(p, folder_info->data, sizeof(MSG_FOLDER_INFO_S));
254                 p = (void*)((char*)p + sizeof(MSG_FOLDER_INFO_S));
255         }
256
257         return dataSize;
258 }
259
260
261 int MsgEncodeFilterList(msg_struct_list_s *pFilterList, char **ppDest)
262 {
263         int count = 0, dataSize = 0;
264
265         count = pFilterList->nCount;
266         dataSize = sizeof(int) + (sizeof(MSG_FILTER_S)*count);
267
268         *ppDest = (char*)new char[dataSize];
269
270         void* p = (void*)*ppDest;
271
272         memcpy(p, &count, sizeof(int));
273         p = (void*)((char*)p + sizeof(int));
274
275         msg_struct_s *filter_info = NULL;
276
277         for (int i = 0; i < count; i++) {
278                 filter_info = (msg_struct_s *)pFilterList->msg_struct_info[i];
279                 memcpy(p, filter_info->data, sizeof(MSG_FILTER_S));
280                 p = (void*)((char*)p + sizeof(MSG_FILTER_S));
281         }
282
283         return dataSize;
284 }
285
286
287 int MsgEncodeFilterFlag(bool *pSetFlag, char **ppDest)
288 {
289         int dataSize = 0;
290
291         dataSize = (sizeof(bool));
292
293         *ppDest = (char*)new char[dataSize];
294
295         void* p = (void*)*ppDest;
296
297         memcpy(p, pSetFlag, dataSize);
298
299         return dataSize;
300 }
301
302
303 int MsgEncodeThreadViewList(msg_struct_list_s *pThreadViewList, char **ppDest)
304 {
305         int count = 0, dataSize = 0;
306
307         count = pThreadViewList->nCount;
308
309         dataSize = sizeof(int) + (sizeof(MSG_THREAD_VIEW_S)*count);
310
311         *ppDest = (char*)new char[dataSize];
312
313         void* p = (void*)*ppDest;
314
315         memcpy(p, &count, sizeof(int));
316         p = (void*)((char*)p + sizeof(int));
317
318         msg_struct_s *thread_info = NULL;
319
320         for (int i = 0; i < count; i++) {
321                 thread_info = (msg_struct_s *)pThreadViewList->msg_struct_info[i];
322                 memcpy(p, thread_info->data, sizeof(MSG_THREAD_VIEW_S));
323                 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
324         }
325
326         return dataSize;
327 }
328
329
330 int MsgEncodeConversationViewList(msg_struct_list_s *pConvViewList, char **ppDest)
331 {
332         int count = 0, dataSize = 0;
333
334         count = pConvViewList->nCount;
335
336         dataSize = sizeof(int) + (sizeof(msg_struct_list_s)*count);
337
338         *ppDest = (char*)new char[dataSize];
339
340         void* p = (void*)*ppDest;
341
342         memcpy(p, &count, sizeof(int));
343         p = (void*)((char*)p + sizeof(int));
344
345         for (int i = 0; i < count; i++) {
346                 memcpy(p, &(pConvViewList->msg_struct_info[i]), sizeof(msg_struct_list_s));
347                 p = (void*)((char*)p + sizeof(msg_struct_list_s));
348         }
349
350         return dataSize;
351 }
352
353
354 int MsgEncodeMsgGetContactCount(MSG_THREAD_COUNT_INFO_S *threadCountInfo, char **ppDest)
355 {
356         int dataSize = sizeof(int) + sizeof(int) + sizeof(int) + sizeof(int);
357
358         MSG_DEBUG("datasize = [%d] \n", dataSize);
359
360         *ppDest = (char*)new char[dataSize];
361
362         void* p = (void*)*ppDest;
363
364         memcpy(p, &(threadCountInfo->totalCount), sizeof(int));
365         p = (void*)((char*)p + sizeof(int));
366
367         memcpy(p, &(threadCountInfo->unReadCount), sizeof(int));
368         p = (void*)((char*)p + sizeof(int));
369
370         memcpy(p, &(threadCountInfo->mmsMsgCount), sizeof(int));
371         p = (void*)((char*)p + sizeof(int));
372
373         memcpy(p, &(threadCountInfo->smsMsgCount), sizeof(int));
374         p = (void*)((char*)p + sizeof(int));
375
376         return dataSize;
377 }
378
379 int MsgEncodeMemSize(unsigned int *memsize, char **ppDest)
380 {
381         int dataSize = 0;
382
383         dataSize = sizeof(unsigned int);
384
385         *ppDest = (char*)new char[dataSize];
386
387         void* p = (void*)*ppDest;
388
389         memcpy(p, memsize, dataSize);
390
391         return dataSize;
392 }
393
394
395 int MsgEncodeSyncMLOperationData(int msgId, int extId, char **ppDest)
396 {
397         int dataSize = 0;
398
399         dataSize = sizeof(int) + sizeof(int);
400
401         *ppDest = (char*)new char[dataSize];
402
403         void* p = (void*)*ppDest;
404
405         memcpy(p, &msgId, sizeof(int));
406         p = (void*)((char*)p + sizeof(int));
407
408         memcpy(p, &extId, sizeof(int));
409
410         return dataSize;
411 }
412
413
414 int MsgEncodeStorageChangeData(const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList, char **ppDest)
415 {
416         int dataSize = 0;
417         int count = 0;
418
419         count = pMsgIdList->nCount;
420
421         dataSize = sizeof(msg_storage_change_type_t) + sizeof(int) + (sizeof(msg_message_id_t)*count);
422
423         *ppDest = (char*)new char[dataSize];
424
425         void* p = (void*)*ppDest;
426
427         memcpy(p, &storageChangeType, sizeof(msg_storage_change_type_t));
428         p = (void*)((char*)p + sizeof(msg_storage_change_type_t));
429
430         memcpy(p, &count, sizeof(int));
431         p = (void*)((char*)p + sizeof(int));
432
433         for (int i = 0; i < count; i++) {
434                 memcpy(p, &(pMsgIdList->msgIdList[i]), sizeof(msg_message_id_t));
435                 p = (void*)((char*)p + sizeof(msg_message_id_t));
436         }
437
438         return dataSize;
439 }
440
441
442 int MsgEncodeThreadChangeData(const msg_storage_change_type_t storageChangeType, const msg_thread_id_t threadId, char **ppDest)
443 {
444         int dataSize = 0;
445
446         dataSize = sizeof(msg_storage_change_type_t) + sizeof(msg_thread_id_t);
447
448         *ppDest = (char*)new char[dataSize];
449
450         void* p = (void*)*ppDest;
451
452         memcpy(p, &storageChangeType, sizeof(msg_storage_change_type_t));
453         p = (void*)((char*)p + sizeof(msg_storage_change_type_t));
454
455         memcpy(p, &threadId, sizeof(msg_thread_id_t));
456
457         return dataSize;
458 }
459
460
461 int MsgEncodeReportMsgData(const msg_report_type_t msgReportType, const MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
462 {
463         int dataSize = 0;
464         int addr_len = 0;
465
466         addr_len = strlen(pMsgInfo->addressList->addressVal);
467
468         dataSize = sizeof(msg_report_type_t) + sizeof(msg_message_id_t) + sizeof(int) + addr_len;
469
470         *ppDest = (char*)new char[dataSize];
471
472         void* p = (void*)*ppDest;
473
474         memcpy(p, &msgReportType, sizeof(msg_report_type_t));
475         p = (void*)((char*)p + sizeof(msg_report_type_t));
476
477         memcpy(p, &(pMsgInfo->msgId), sizeof(msg_message_id_t));
478         p = (void*)((char*)p + sizeof(msg_message_id_t));
479
480         memcpy(p, &addr_len, sizeof(int));
481         p = (void*)((char*)p + sizeof(int));
482
483         memcpy(p, &(pMsgInfo->addressList->addressVal), addr_len);
484         p = (void*)((char*)p + addr_len);
485
486         return dataSize;
487 }
488
489
490 int MsgEncodeReportStatus(MSG_REPORT_STATUS_INFO_S* pReportStatus, int count, char **ppDest)
491 {
492         int dataSize = 0;
493
494         dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S)*count + sizeof(int));
495
496         *ppDest = (char*)new char[dataSize];
497
498         void* p = (void*)*ppDest;
499
500         memcpy(p, &count, sizeof(int));
501
502         p = (void*)((char*)p + sizeof(int));
503
504         memcpy(p, pReportStatus, sizeof(MSG_REPORT_STATUS_INFO_S)*count);
505
506         return dataSize;
507 }
508
509
510 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
511 {
512         int dataSize = 0;
513
514         dataSize = (sizeof(msg_thread_id_t));
515
516         *ppDest = (char*)new char[dataSize];
517
518         void* p = (void*)*ppDest;
519
520         memcpy(p, pThreadId, dataSize);
521
522         return dataSize;
523 }
524
525
526 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
527 {
528         int dataSize = 0;
529
530         dataSize = sizeof(MSG_THREAD_VIEW_S);
531
532         *ppDest = (char*)new char[dataSize];
533
534         void* p = (void*)*ppDest;
535
536         memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
537
538         p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
539
540         return dataSize;
541 }
542
543
544 /* Decoders */
545 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
546 {
547         memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
548 }
549
550
551 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
552 {
553         memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
554 }
555
556
557 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
558 {
559         memcpy(memsize, pSrc, sizeof(unsigned int));
560 }
561
562
563 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo)
564 {
565         memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
566
567         pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
568
569         pMsgInfo->addressList = NULL;
570
571         pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt];
572         memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt);
573
574         for (int i = 0; i < pMsgInfo->nAddressCnt; i++) {
575                 memcpy(&(pMsgInfo->addressList[i]), pSrc + (sizeof(MSG_ADDRESS_INFO_S)*i), sizeof(MSG_ADDRESS_INFO_S));
576         }
577 }
578
579
580 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
581 {
582         memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
583
584         pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
585
586         memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
587
588         pSrc = pSrc + sizeof(MSG_SENDINGOPT_INFO_S);
589
590         if(pMsgInfo->nAddressCnt > 0) {
591                 pMsgInfo->addressList = NULL;
592
593                 pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt];
594                 memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt);
595
596                 for (int i = 0; i < pMsgInfo->nAddressCnt; i++) {
597                         memcpy(&(pMsgInfo->addressList[i]), pSrc + (sizeof(MSG_ADDRESS_INFO_S)*i), sizeof(MSG_ADDRESS_INFO_S));
598                 }
599         }
600 }
601
602
603 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
604 {
605         int count = 0;
606
607         memcpy(&count, pSrc, sizeof(int));
608         pSrc = pSrc + sizeof(int);
609
610         pRecipientList->recipientCnt = count;
611         pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
612
613         MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
614
615         for (int i = 0; i < count; i++) {
616                 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
617                 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
618                 pInfoTmp++;
619         }
620 }
621
622
623 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
624 {
625         int count = 0;
626
627         memcpy(&count, pSrc, sizeof(int));
628         pSrc = pSrc + sizeof(int);
629
630         if( count > 0 ) {
631                 pFolderList->nCount = count;
632                 pFolderList->msg_struct_info = (msg_struct_t *)calloc(count, sizeof(msg_struct_t));
633                 if (pFolderList->msg_struct_info == NULL) {
634                         pFolderList->nCount = 0;
635                         return;
636                 }
637                 msg_struct_s *pInfoTmp = NULL;
638
639                 for (int i = 0; i < count; i++) {
640                         pFolderList->msg_struct_info[i] = (msg_struct_t )new msg_struct_s;
641                         pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
642                         pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
643                         pInfoTmp->data = new MSG_FOLDER_INFO_S;
644                         memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
645                         pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
646                 }
647         } else if ( count == 0 ) {
648                 pFolderList->nCount = count;
649                 pFolderList->msg_struct_info = NULL;
650         }
651 }
652
653
654 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
655 {
656         int count = 0;
657
658         memcpy(&count, pSrc, sizeof(int));
659         pSrc = pSrc + sizeof(int);
660
661         if( count > 0 ) {
662                 pFilterList->nCount = count;
663                 pFilterList->msg_struct_info = (msg_struct_t *)calloc(count, sizeof(MSG_FILTER_S *));
664
665                 if (pFilterList->msg_struct_info == NULL) {
666                         pFilterList->nCount = 0;
667                         return;
668                 }
669
670                 msg_struct_s *pStructTmp = NULL;
671
672                 for (int i = 0; i < count; i++) {
673                         pFilterList->msg_struct_info[i] = (msg_struct_t )new msg_struct_s;
674                         pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
675                         pStructTmp->type = MSG_STRUCT_FILTER;
676                         pStructTmp->data = new MSG_FILTER_S;
677                         memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
678                         pSrc = pSrc + sizeof(MSG_FILTER_S);
679                 }
680         } else if ( count == 0 ) {
681                 pFilterList->nCount = count;
682                 pFilterList->msg_struct_info = NULL;
683         }
684 }
685
686
687 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
688 {
689         memcpy(pSetFlag, pSrc, sizeof(bool));
690 }
691
692
693 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
694 {
695         memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
696 }
697
698
699 void    MsgDecodeContactCount(char *pSrc,  MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
700 {
701         int count = 0;
702
703         if(pSrc == NULL)
704                 return;
705
706         memcpy(&count, pSrc, sizeof(int));
707         pSrc = pSrc + sizeof(int);
708         pMsgThreadCountList->totalCount = count;
709
710
711         memcpy(&count, pSrc, sizeof(int));
712         pSrc = pSrc + sizeof(int);
713         pMsgThreadCountList->unReadCount        = count;
714
715
716         memcpy(&count, pSrc, sizeof(int));
717         pSrc = pSrc + sizeof(int);
718         pMsgThreadCountList->mmsMsgCount        = count;
719
720
721         memcpy(&count, pSrc, sizeof(int));
722         pSrc = pSrc + sizeof(int);
723         pMsgThreadCountList->smsMsgCount        = count;
724
725
726         return;
727 }
728
729
730 void MsgDecodeReportStatus(char *pSrc,  msg_struct_list_s *report_list)
731 {
732         int count = 0;
733
734         if(pSrc == NULL)
735                 return;
736
737         memcpy(&count, pSrc, sizeof(int));
738         pSrc = pSrc + sizeof(int);
739
740         report_list->nCount = count;
741
742         msg_struct_t *report_status =  (msg_struct_t *)new char[sizeof(msg_struct_t)*count];
743         for (int i = 0; i < count; i++) {
744                 msg_struct_s *report_status_item = new msg_struct_s;
745                 report_status_item->type = MSG_STRUCT_REPORT_STATUS_INFO;
746                 report_status_item->data = new MSG_REPORT_STATUS_INFO_S;
747                 memset(report_status_item->data, 0x00, sizeof(MSG_REPORT_STATUS_INFO_S));
748
749                 MSG_REPORT_STATUS_INFO_S *report_status_info =  (MSG_REPORT_STATUS_INFO_S *)report_status_item->data;
750                 memcpy(report_status_info, pSrc, sizeof(MSG_REPORT_STATUS_INFO_S));
751
752                 pSrc = pSrc + sizeof(MSG_REPORT_STATUS_INFO_S);
753
754                 report_status[i] = (msg_struct_t)report_status_item;
755
756                 MSG_DEBUG("Report_type = %d, status addr = %s, status = %d, time = %d",
757                                 report_status_info->type, report_status_info->addressVal,
758                                 report_status_info->status, report_status_info->statusTime);
759         }
760
761         report_list->msg_struct_info = report_status;
762         return;
763 }
764
765 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
766 {
767         memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
768 }
769
770 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
771 {
772         memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
773 }
774
775 /* Event Encoder */
776 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
777 {
778         MSG_EVENT_S* pMsgEvent = NULL;
779
780         if (*ppEvent) {
781                 MSG_DEBUG("*ppEvent is not NULL.");
782                 delete [] (char *)*ppEvent;
783         }
784
785         *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
786
787         pMsgEvent = (MSG_EVENT_S*)*ppEvent;
788
789         pMsgEvent->eventType = MsgEvent;
790         pMsgEvent->result = MsgError;
791
792         MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
793         MSG_DEBUG("result [%d]", pMsgEvent->result);
794
795         if (DataSize > 0)
796                 memcpy((void*)pMsgEvent->data, pData, DataSize);
797
798         return (sizeof(MSG_EVENT_S) + DataSize);
799 }
800
801 int msg_verify_number(const char *raw, char *trimmed)
802 {
803         if (!(raw && trimmed)) {
804                 MSG_DEBUG("Phone Number is NULL");
805                 return MSG_ERR_NULL_POINTER;
806         }
807
808         for (int i=0, j=0 ; raw[i] ; i++) {
809                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
810                                 || raw[i] == '*' ||  raw[i] == '#') {
811                         trimmed[j++] = raw[i];
812                 } else if (raw[i] == '-') {
813                         continue;
814                 } else {
815                         MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
816                         return MSG_ERR_INVALID_PARAMETER;
817                 }
818         }
819
820         MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
821         return MSG_SUCCESS;
822 }
823
824 int msg_verify_email(const char *raw)
825 {
826         bool onlyNum = true;
827         bool atExist = false;
828
829         if (!raw) {
830                 MSG_DEBUG("Email is NULL");
831                 return MSG_ERR_NULL_POINTER;
832         }
833
834         for (int i = 0; raw[i]; i++) {
835                 if (raw[i] == '@') {
836                         onlyNum = false;
837
838                         if (atExist == false) {
839                                 atExist = true;
840                                 continue;
841                         } else {
842                                 MSG_DEBUG("Character [@] is included more than twice in email address.");
843                                 return MSG_ERR_INVALID_PARAMETER;
844                         }
845                 }
846
847                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' ||  raw[i] == '#') {
848                         continue;
849                 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
850                         onlyNum = false;
851                         continue;
852                 } else if (raw[i] == ',') {
853                         if (onlyNum == false && atExist == false) {
854                                 MSG_DEBUG("Unacceptable type in address.");
855                                 return MSG_ERR_INVALID_PARAMETER;
856                         }
857                         atExist = false;
858                         onlyNum = true;
859                         continue;
860                 } else {
861                         MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
862                         return MSG_ERR_INVALID_PARAMETER;
863                 }
864         }
865
866         return MSG_SUCCESS;
867 }
868
869
870 char* msg_clean_country_code(char *src)
871 {
872         int ret = 1;
873
874         switch (src[ret++]-'0') {
875                 case 1:
876                 case 7:
877                         break;
878                 case 2:
879                         switch (src[ret++]-'0') {
880                                 case 0:
881                                 case 7:
882                                         break;
883                                 case 1:
884                                 case 2:
885                                 case 3:
886                                 case 4:
887                                 case 5:
888                                 case 6:
889                                 case 8:
890                                 case 9:
891                                         ret += 1;
892                                         break;
893                                 default:
894                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
895                                         break;
896                         }
897                         break;
898                 case 3:
899                         switch (src[ret++]-'0') {
900                                 case 0:
901                                 case 1:
902                                 case 2:
903                                 case 3:
904                                 case 4:
905                                 case 6:
906                                 case 9:
907                                         break;
908                                 case 5:
909                                 case 7:
910                                 case 8:
911                                         ret += 1;
912                                         break;
913                                 default:
914                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
915                                         break;
916                         }
917                         break;
918                 case 4:
919                         switch (src[ret++]-'0') {
920                                 case 0:
921                                 case 1:
922                                 case 3:
923                                 case 4:
924                                 case 5:
925                                 case 6:
926                                 case 7:
927                                 case 8:
928                                 case 9:
929                                         break;
930                                 case 2:
931                                         ret += 1;
932                                         break;
933                                 default:
934                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
935                                         break;
936                         }
937                         break;
938                 case 5:
939                         switch (src[ret++]-'0') {
940                                 case 1:
941                                 case 2:
942                                 case 3:
943                                 case 4:
944                                 case 5:
945                                 case 6:
946                                 case 7:
947                                 case 8:
948                                         break;
949                                 case 0:
950                                 case 9:
951                                         ret += 1;
952                                         break;
953                                 default:
954                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
955                                         break;
956                         }
957                         break;
958                 case 6:
959                         switch (src[ret++]-'0') {
960                                 case 0:
961                                 case 1:
962                                 case 2:
963                                 case 3:
964                                 case 4:
965                                 case 5:
966                                 case 6:
967                                         break;
968                                 case 7:
969                                 case 8:
970                                 case 9:
971                                         ret += 1;
972                                         break;
973                                 default:
974                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
975                                         break;
976                         }
977                         break;
978                 case 8:
979                         switch (src[ret++]-'0') {
980                                 case 1:
981                                 case 2:
982                                 case 4:
983                                 case 6:
984                                         break;
985                                 case 0:
986                                 case 3:
987                                 case 5:
988                                 case 7:
989                                 case 8:
990                                 case 9:
991                                         ret += 1;
992                                         break;
993                                 default:
994                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
995                                         break;
996                         }
997                         break;
998                 case 9:
999                         switch (src[ret++]-'0') {
1000                                 case 0:
1001                                 case 1:
1002                                 case 2:
1003                                 case 3:
1004                                 case 4:
1005                                 case 5:
1006                                 case 8:
1007                                         break;
1008                                 case 6:
1009                                 case 7:
1010                                 case 9:
1011                                         ret += 1;
1012                                         break;
1013                                 default:
1014                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
1015                                         break;
1016                         }
1017                         break;
1018                 case 0:
1019                 default:
1020                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
1021                         return src;
1022         }
1023
1024         return &src[ret];
1025 }
1026
1027
1028 char* msg_normalize_number(char *src)
1029 {
1030         char *normalized_number;
1031
1032         if ('+' == src[0])
1033                 normalized_number = msg_clean_country_code(src);
1034         else if ('0' == src[0])
1035                 normalized_number = src+1;
1036         else
1037                 normalized_number = src;
1038
1039         MSG_DEBUG("src = %s, normalized = %s", src, normalized_number);
1040
1041         return normalized_number;
1042 }
1043
1044
1045 char *getTranslateText(const char *pkg_name, const char *locale_dir, const char *text)
1046 {
1047         char *notiMsg = NULL;
1048         char *lang = NULL;
1049
1050         lang = vconf_get_str(VCONFKEY_LANGSET);
1051
1052         setlocale(LC_MESSAGES, lang);
1053
1054         bindtextdomain(pkg_name, locale_dir);
1055
1056         notiMsg = dgettext(pkg_name, text);
1057
1058         if (lang) {
1059                 free(lang);
1060                 lang = NULL;
1061         }
1062
1063         return g_strdup(notiMsg);
1064 }
1065
1066
1067 msg_error_t MsgMakeSortRule(const MSG_SORT_RULE_S *pSortRule, char *pSqlSort)
1068 {
1069         char sql[128];
1070         char order[6];
1071
1072         memset(sql, 0x00, sizeof(sql));
1073         memset(order, 0x00, sizeof(order));
1074
1075         if (pSortRule->bAscending == true)
1076                 strncpy(order, "ASC", 5);
1077         else
1078                 strncpy(order, "DESC", 5);
1079
1080         switch (pSortRule->sortType) {
1081                 case MSG_SORT_BY_DISPLAY_FROM :
1082                         snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
1083                         break;
1084                 case MSG_SORT_BY_DISPLAY_TO :
1085                         snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
1086                         break;
1087                 case MSG_SORT_BY_DISPLAY_TIME :
1088                         snprintf(sql, sizeof(sql), "ORDER BY DISPLAY_TIME %s;", order);
1089                         break;
1090                 case MSG_SORT_BY_MSG_TYPE :
1091                         snprintf(sql, sizeof(sql), "ORDER BY MAIN_TYPE %s, DISPLAY_TIME DESC;", order);
1092                         break;
1093                 case MSG_SORT_BY_READ_STATUS :
1094                         snprintf(sql, sizeof(sql), "ORDER BY READ_STATUS %s, DISPLAY_TIME DESC;", order);
1095                         break;
1096                 case MSG_SORT_BY_STORAGE_TYPE :
1097                         snprintf(sql, sizeof(sql), "ORDER BY A.STORAGE_ID %s, A.DISPLAY_TIME DESC;", order);
1098                         break;
1099                 case MSG_SORT_BY_THREAD_NAME :
1100                         snprintf(sql, sizeof(sql), "ORDER BY FIRST_NAME %s, LAST_NAME %s;", order, order);
1101                         break;
1102                 case MSG_SORT_BY_THREAD_DATE :
1103                         snprintf(sql, sizeof(sql), "ORDER BY MSG_TIME %s;", order);
1104                         break;
1105                 case MSG_SORT_BY_THREAD_COUNT :
1106                         snprintf(sql, sizeof(sql), "ORDER BY UNREAD_CNT %s;", order);
1107                         break;
1108                 default :
1109                         snprintf(sql, sizeof(sql), "ORDER BY A.DISPLAY_TIME %s;", order);
1110                         break;
1111         }
1112
1113         memcpy(pSqlSort, sql, strlen(sql));
1114
1115         return MSG_SUCCESS;
1116 }
1117 bool msg_is_valid_email(char *pAddress)
1118 {
1119         if (!pAddress || pAddress[0] == 0)
1120                 return false;
1121         if (!strchr (pAddress, MSG_UTIL_CH_EMAIL_AT))
1122                 return false;
1123         return true;
1124 }
1125
1126 msg_error_t msg_write_text_to_msg_info(MSG_MESSAGE_INFO_S *pMsgInfo, char *text)
1127 {
1128         if (pMsgInfo->dataSize > MAX_MSG_TEXT_LEN) {
1129                 pMsgInfo->bTextSms = false;
1130
1131                 /* Save Message Data into File */
1132                 char fileName[MSG_FILENAME_LEN_MAX+1];
1133                 memset(fileName, 0x00, sizeof(fileName));
1134
1135                 if(MsgCreateFileName(fileName) == false) {
1136                         MSG_DEBUG("MsgCreateFileName error");
1137                         return MSG_ERR_STORAGE_ERROR;
1138                 }
1139
1140                 MSG_SEC_DEBUG("Save text into file : size[%d] name[%s]", pMsgInfo->dataSize, fileName);
1141
1142                 if (MsgWriteIpcFile(fileName, text, pMsgInfo->dataSize) == false) {
1143                         MSG_DEBUG("MsgWriteIpcFile error");
1144                         return MSG_ERR_STORAGE_ERROR;
1145                 }
1146
1147                 memset(pMsgInfo->msgData, 0x00, sizeof(pMsgInfo->msgData));
1148                 strncpy(pMsgInfo->msgData, fileName, MAX_MSG_DATA_LEN);
1149         } else {
1150                 pMsgInfo->bTextSms = true;
1151
1152                 memset(pMsgInfo->msgText, 0x00, sizeof(pMsgInfo->msgText));
1153                 memcpy(pMsgInfo->msgText, text, pMsgInfo->dataSize);
1154         }
1155
1156         return MSG_SUCCESS;
1157 }
1158
1159 /* change illegal filename character to '_' */
1160 void msg_replace_available_file_name(char *fileName)
1161 {
1162         int idx = 0;
1163         int len = 0;
1164         bool is_converted = false;
1165
1166         if (fileName) {
1167                 len = strlen(fileName);
1168
1169                 while (fileName[idx] != 0) {
1170                         if (idx >= len) {
1171                                 MSG_WARN("idx : %d, len : %d", idx, len);
1172                                 break;
1173                         }
1174
1175                         if (fileName[idx] == '\\' || fileName[idx] == '/' || fileName[idx] == '?' || fileName[idx] == '%' || fileName[idx] == '*' ||
1176                                 fileName[idx] == ':' || fileName[idx] == '|' || fileName[idx] == '"' || fileName[idx] == '<' || fileName[idx] == '>') {
1177                                 fileName[idx++] = '_';
1178                                 is_converted = true;
1179                         } else {
1180                                 idx++;
1181                         }
1182                 }
1183         }
1184
1185         if (is_converted)
1186                 MSG_SEC_DEBUG("converted filename : [%s]", fileName);
1187 }
1188
1189 /* change character ' ' to '_' */
1190 void msg_replace_space_char(char *pszText)
1191 {
1192         if (!pszText) {
1193                 MSG_ERR("pszText is NULL");
1194                 return;
1195         }
1196
1197         char *spaceCharPtr = strchr(pszText, ' ');
1198
1199         while (spaceCharPtr) {
1200                 *spaceCharPtr = '_';
1201                 spaceCharPtr = strchr(pszText, ' ');
1202         }
1203 }
1204
1205 /* change non-ascii character to underscore */
1206 gchar * msg_replace_non_ascii_char(const gchar *pszText, gunichar replacementChar)
1207 {
1208         if (!pszText) {
1209                 MSG_ERR(" msg_replace_non_ascii_char error : pszText is NULL");
1210                 return NULL;
1211         }
1212         gchar *res;
1213         gsize result_len = 0;
1214         const gchar *p;
1215         result_len = g_utf8_strlen(pszText, -1) + 1; /* +1 for malloc of non-terminating chracter */
1216         res = (gchar *)g_malloc (result_len * sizeof (gchar));
1217         int i = 0;
1218         for (p = pszText, i = 0; *p != '\0'; p = g_utf8_next_char(p), i++) {
1219                 res[i] = isascii(g_utf8_get_char(p)) ? *p : replacementChar;
1220         }
1221         res[i] = '\0';
1222         return res;
1223 }
1224
1225
1226 static int __find_login_user(uid_t *uid)
1227 {
1228         uid_t *uids = NULL;
1229
1230         int uids_len = sd_get_active_uids(&uids);
1231         if (uids == NULL)
1232                 return -1;
1233
1234         if (uids_len != 1) {
1235                 free(uids);
1236                 return -1;
1237         }
1238
1239         *uid = uids[0];
1240
1241         free(uids);
1242         return 0;
1243 }
1244
1245
1246 uid_t msg_get_login_user()
1247 {
1248         uid_t uid = -1;
1249
1250         if (__find_login_user(&uid) < 0) {
1251                 MSG_WARN("Cannot find login user");
1252         }
1253
1254         MSG_DEBUG("login user id [%d]", uid);
1255
1256         return uid;
1257 }
1258
1259
1260 void* _msg_launch_app(void *data)
1261 {
1262         if (data) {
1263                 msg_launch_app_data *ad = (msg_launch_app_data *)data;
1264                 int ret = aul_launch_app_for_uid(ad->app_id, ad->bundle_data, msg_get_login_user());
1265                 if (ret <= 0) {
1266                         MSG_DEBUG("aul_launch_app_for_uid() is failed : %d", ret);
1267                 }
1268
1269                 g_free(ad->app_id);
1270                 bundle_free(ad->bundle_data);
1271                 g_free(ad);
1272         }
1273
1274         return NULL;
1275 }
1276
1277 msg_error_t msg_launch_app(const char *app_id, bundle *bundle_data)
1278 {
1279         msg_launch_app_data *data = (msg_launch_app_data *)calloc(1, sizeof(msg_launch_app_data));
1280         if (data == NULL) {
1281                 MSG_ERR("Memory alloc failed!");
1282                 return MSG_ERR_MEMORY_ERROR;
1283         }
1284
1285         data->app_id = g_strdup(app_id);
1286         data->bundle_data = bundle_dup(bundle_data);
1287         pthread_t thd;
1288
1289         if (pthread_create(&thd, NULL, &_msg_launch_app, data) < 0) {
1290                 MSG_DEBUG("pthread_create() error");
1291         }
1292
1293         pthread_detach(thd);
1294         return MSG_SUCCESS;
1295 }
1296
1297
1298 msg_error_t msg_aul_svc_set_operation(bundle *bundle_data, const char *operation)
1299 {
1300         int ret = aul_svc_set_operation(bundle_data, operation);
1301         if (ret < 0) {
1302                 MSG_DEBUG("aul_svc_set_operation() is failed : %d", ret);
1303                 return MSG_ERR_UNKNOWN;
1304         }
1305
1306         return MSG_SUCCESS;
1307 }
1308
1309
1310 msg_error_t msg_aul_svc_set_uri(bundle *bundle_data, char *uri)
1311 {
1312         int ret = aul_svc_set_uri(bundle_data, uri);
1313         if (ret < 0) {
1314                 MSG_DEBUG("aul_svc_set_uri() is failed : %d", ret);
1315                 return MSG_ERR_UNKNOWN;
1316         }
1317
1318         return MSG_SUCCESS;
1319 }
1320
1321
1322 void msg_set_dpm_policy(int type, int state)
1323 {
1324         dpm_policy_enable[type] = state;
1325 }
1326
1327
1328 bool msg_check_dpm_policy(int type)
1329 {
1330         return dpm_policy_enable[MSG_SMS_TYPE];
1331 //      return dpm_policy_enable[type];
1332 }
1333