resolve TSAM-6816: update DB after DPM state changed
[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 MsgEncodeReportMsgData(const msg_report_type_t msgReportType, const MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
443 {
444         int dataSize = 0;
445         int addr_len = 0;
446
447         addr_len = strlen(pMsgInfo->addressList->addressVal);
448
449         dataSize = sizeof(msg_report_type_t) + sizeof(msg_message_id_t) + sizeof(int) + addr_len;
450
451         *ppDest = (char*)new char[dataSize];
452
453         void* p = (void*)*ppDest;
454
455         memcpy(p, &msgReportType, sizeof(msg_report_type_t));
456         p = (void*)((char*)p + sizeof(msg_report_type_t));
457
458         memcpy(p, &(pMsgInfo->msgId), sizeof(msg_message_id_t));
459         p = (void*)((char*)p + sizeof(msg_message_id_t));
460
461         memcpy(p, &addr_len, sizeof(int));
462         p = (void*)((char*)p + sizeof(int));
463
464         memcpy(p, &(pMsgInfo->addressList->addressVal), addr_len);
465         p = (void*)((char*)p + addr_len);
466
467         return dataSize;
468 }
469
470
471 int MsgEncodeReportStatus(MSG_REPORT_STATUS_INFO_S* pReportStatus, int count, char **ppDest)
472 {
473         int dataSize = 0;
474
475         dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S)*count + sizeof(int));
476
477         *ppDest = (char*)new char[dataSize];
478
479         void* p = (void*)*ppDest;
480
481         memcpy(p, &count, sizeof(int));
482
483         p = (void*)((char*)p + sizeof(int));
484
485         memcpy(p, pReportStatus, sizeof(MSG_REPORT_STATUS_INFO_S)*count);
486
487         return dataSize;
488 }
489
490
491 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
492 {
493         int dataSize = 0;
494
495         dataSize = (sizeof(msg_thread_id_t));
496
497         *ppDest = (char*)new char[dataSize];
498
499         void* p = (void*)*ppDest;
500
501         memcpy(p, pThreadId, dataSize);
502
503         return dataSize;
504 }
505
506
507 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
508 {
509         int dataSize = 0;
510
511         dataSize = sizeof(MSG_THREAD_VIEW_S);
512
513         *ppDest = (char*)new char[dataSize];
514
515         void* p = (void*)*ppDest;
516
517         memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
518
519         p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
520
521         return dataSize;
522 }
523
524
525 /* Decoders */
526 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
527 {
528         memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
529 }
530
531
532 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
533 {
534         memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
535 }
536
537
538 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
539 {
540         memcpy(memsize, pSrc, sizeof(unsigned int));
541 }
542
543
544 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo)
545 {
546         memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
547
548         pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
549
550         pMsgInfo->addressList = NULL;
551
552         pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt];
553         memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt);
554
555         for (int i = 0; i < pMsgInfo->nAddressCnt; i++) {
556                 memcpy(&(pMsgInfo->addressList[i]), pSrc + (sizeof(MSG_ADDRESS_INFO_S)*i), sizeof(MSG_ADDRESS_INFO_S));
557         }
558 }
559
560
561 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
562 {
563         memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
564
565         pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
566
567         memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
568
569         pSrc = pSrc + sizeof(MSG_SENDINGOPT_INFO_S);
570
571         if(pMsgInfo->nAddressCnt > 0) {
572                 pMsgInfo->addressList = NULL;
573
574                 pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt];
575                 memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * pMsgInfo->nAddressCnt);
576
577                 for (int i = 0; i < pMsgInfo->nAddressCnt; i++) {
578                         memcpy(&(pMsgInfo->addressList[i]), pSrc + (sizeof(MSG_ADDRESS_INFO_S)*i), sizeof(MSG_ADDRESS_INFO_S));
579                 }
580         }
581 }
582
583
584 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
585 {
586         int count = 0;
587
588         memcpy(&count, pSrc, sizeof(int));
589         pSrc = pSrc + sizeof(int);
590
591         pRecipientList->recipientCnt = count;
592         pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
593
594         MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
595
596         for (int i = 0; i < count; i++) {
597                 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
598                 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
599                 pInfoTmp++;
600         }
601 }
602
603
604 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
605 {
606         int count = 0;
607
608         memcpy(&count, pSrc, sizeof(int));
609         pSrc = pSrc + sizeof(int);
610
611         if( count > 0 ) {
612                 pFolderList->nCount = count;
613                 pFolderList->msg_struct_info = (msg_struct_t *)calloc(count, sizeof(msg_struct_t));
614                 if (pFolderList->msg_struct_info == NULL) {
615                         pFolderList->nCount = 0;
616                         return;
617                 }
618                 msg_struct_s *pInfoTmp = NULL;
619
620                 for (int i = 0; i < count; i++) {
621                         pFolderList->msg_struct_info[i] = (msg_struct_t )new msg_struct_s;
622                         pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
623                         pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
624                         pInfoTmp->data = new MSG_FOLDER_INFO_S;
625                         memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
626                         pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
627                 }
628         } else if ( count == 0 ) {
629                 pFolderList->nCount = count;
630                 pFolderList->msg_struct_info = NULL;
631         }
632 }
633
634
635 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
636 {
637         int count = 0;
638
639         memcpy(&count, pSrc, sizeof(int));
640         pSrc = pSrc + sizeof(int);
641
642         if( count > 0 ) {
643                 pFilterList->nCount = count;
644                 pFilterList->msg_struct_info = (msg_struct_t *)calloc(count, sizeof(MSG_FILTER_S *));
645
646                 if (pFilterList->msg_struct_info == NULL) {
647                         pFilterList->nCount = 0;
648                         return;
649                 }
650
651                 msg_struct_s *pStructTmp = NULL;
652
653                 for (int i = 0; i < count; i++) {
654                         pFilterList->msg_struct_info[i] = (msg_struct_t )new msg_struct_s;
655                         pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
656                         pStructTmp->type = MSG_STRUCT_FILTER;
657                         pStructTmp->data = new MSG_FILTER_S;
658                         memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
659                         pSrc = pSrc + sizeof(MSG_FILTER_S);
660                 }
661         } else if ( count == 0 ) {
662                 pFilterList->nCount = count;
663                 pFilterList->msg_struct_info = NULL;
664         }
665 }
666
667
668 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
669 {
670         memcpy(pSetFlag, pSrc, sizeof(bool));
671 }
672
673
674 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
675 {
676         memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
677 }
678
679
680 void    MsgDecodeContactCount(char *pSrc,  MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
681 {
682         int count = 0;
683
684         if(pSrc == NULL)
685                 return;
686
687         memcpy(&count, pSrc, sizeof(int));
688         pSrc = pSrc + sizeof(int);
689         pMsgThreadCountList->totalCount = count;
690
691
692         memcpy(&count, pSrc, sizeof(int));
693         pSrc = pSrc + sizeof(int);
694         pMsgThreadCountList->unReadCount        = count;
695
696
697         memcpy(&count, pSrc, sizeof(int));
698         pSrc = pSrc + sizeof(int);
699         pMsgThreadCountList->mmsMsgCount        = count;
700
701
702         memcpy(&count, pSrc, sizeof(int));
703         pSrc = pSrc + sizeof(int);
704         pMsgThreadCountList->smsMsgCount        = count;
705
706
707         return;
708 }
709
710
711 void MsgDecodeReportStatus(char *pSrc,  msg_struct_list_s *report_list)
712 {
713         int count = 0;
714
715         if(pSrc == NULL)
716                 return;
717
718         memcpy(&count, pSrc, sizeof(int));
719         pSrc = pSrc + sizeof(int);
720
721         report_list->nCount = count;
722
723         msg_struct_t *report_status =  (msg_struct_t *)new char[sizeof(msg_struct_t)*count];
724         for (int i = 0; i < count; i++) {
725                 msg_struct_s *report_status_item = new msg_struct_s;
726                 report_status_item->type = MSG_STRUCT_REPORT_STATUS_INFO;
727                 report_status_item->data = new MSG_REPORT_STATUS_INFO_S;
728                 memset(report_status_item->data, 0x00, sizeof(MSG_REPORT_STATUS_INFO_S));
729
730                 MSG_REPORT_STATUS_INFO_S *report_status_info =  (MSG_REPORT_STATUS_INFO_S *)report_status_item->data;
731                 memcpy(report_status_info, pSrc, sizeof(MSG_REPORT_STATUS_INFO_S));
732
733                 pSrc = pSrc + sizeof(MSG_REPORT_STATUS_INFO_S);
734
735                 report_status[i] = (msg_struct_t)report_status_item;
736
737                 MSG_DEBUG("Report_type = %d, status addr = %s, status = %d, time = %d",
738                                 report_status_info->type, report_status_info->addressVal,
739                                 report_status_info->status, report_status_info->statusTime);
740         }
741
742         report_list->msg_struct_info = report_status;
743         return;
744 }
745
746 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
747 {
748         memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
749 }
750
751 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
752 {
753         memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
754 }
755
756 /* Event Encoder */
757 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
758 {
759         MSG_EVENT_S* pMsgEvent = NULL;
760
761         if (*ppEvent) {
762                 MSG_DEBUG("*ppEvent is not NULL.");
763                 delete [] (char *)*ppEvent;
764         }
765
766         *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
767
768         pMsgEvent = (MSG_EVENT_S*)*ppEvent;
769
770         pMsgEvent->eventType = MsgEvent;
771         pMsgEvent->result = MsgError;
772
773         MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
774         MSG_DEBUG("result [%d]", pMsgEvent->result);
775
776         if (DataSize > 0)
777                 memcpy((void*)pMsgEvent->data, pData, DataSize);
778
779         return (sizeof(MSG_EVENT_S) + DataSize);
780 }
781
782 int msg_verify_number(const char *raw, char *trimmed)
783 {
784         if (!(raw && trimmed)) {
785                 MSG_DEBUG("Phone Number is NULL");
786                 return MSG_ERR_NULL_POINTER;
787         }
788
789         for (int i=0, j=0 ; raw[i] ; i++) {
790                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
791                                 || raw[i] == '*' ||  raw[i] == '#') {
792                         trimmed[j++] = raw[i];
793                 } else if (raw[i] == '-') {
794                         continue;
795                 } else {
796                         MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
797                         return MSG_ERR_INVALID_PARAMETER;
798                 }
799         }
800
801         MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
802         return MSG_SUCCESS;
803 }
804
805 int msg_verify_email(const char *raw)
806 {
807         bool onlyNum = true;
808         bool atExist = false;
809
810         if (!raw) {
811                 MSG_DEBUG("Email is NULL");
812                 return MSG_ERR_NULL_POINTER;
813         }
814
815         for (int i = 0; raw[i]; i++) {
816                 if (raw[i] == '@') {
817                         onlyNum = false;
818
819                         if (atExist == false) {
820                                 atExist = true;
821                                 continue;
822                         } else {
823                                 MSG_DEBUG("Character [@] is included more than twice in email address.");
824                                 return MSG_ERR_INVALID_PARAMETER;
825                         }
826                 }
827
828                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' ||  raw[i] == '#') {
829                         continue;
830                 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
831                         onlyNum = false;
832                         continue;
833                 } else if (raw[i] == ',') {
834                         if (onlyNum == false && atExist == false) {
835                                 MSG_DEBUG("Unacceptable type in address.");
836                                 return MSG_ERR_INVALID_PARAMETER;
837                         }
838                         atExist = false;
839                         onlyNum = true;
840                         continue;
841                 } else {
842                         MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
843                         return MSG_ERR_INVALID_PARAMETER;
844                 }
845         }
846
847         return MSG_SUCCESS;
848 }
849
850
851 char* msg_clean_country_code(char *src)
852 {
853         int ret = 1;
854
855         switch (src[ret++]-'0') {
856                 case 1:
857                 case 7:
858                         break;
859                 case 2:
860                         switch (src[ret++]-'0') {
861                                 case 0:
862                                 case 7:
863                                         break;
864                                 case 1:
865                                 case 2:
866                                 case 3:
867                                 case 4:
868                                 case 5:
869                                 case 6:
870                                 case 8:
871                                 case 9:
872                                         ret += 1;
873                                         break;
874                                 default:
875                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
876                                         break;
877                         }
878                         break;
879                 case 3:
880                         switch (src[ret++]-'0') {
881                                 case 0:
882                                 case 1:
883                                 case 2:
884                                 case 3:
885                                 case 4:
886                                 case 6:
887                                 case 9:
888                                         break;
889                                 case 5:
890                                 case 7:
891                                 case 8:
892                                         ret += 1;
893                                         break;
894                                 default:
895                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
896                                         break;
897                         }
898                         break;
899                 case 4:
900                         switch (src[ret++]-'0') {
901                                 case 0:
902                                 case 1:
903                                 case 3:
904                                 case 4:
905                                 case 5:
906                                 case 6:
907                                 case 7:
908                                 case 8:
909                                 case 9:
910                                         break;
911                                 case 2:
912                                         ret += 1;
913                                         break;
914                                 default:
915                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
916                                         break;
917                         }
918                         break;
919                 case 5:
920                         switch (src[ret++]-'0') {
921                                 case 1:
922                                 case 2:
923                                 case 3:
924                                 case 4:
925                                 case 5:
926                                 case 6:
927                                 case 7:
928                                 case 8:
929                                         break;
930                                 case 0:
931                                 case 9:
932                                         ret += 1;
933                                         break;
934                                 default:
935                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
936                                         break;
937                         }
938                         break;
939                 case 6:
940                         switch (src[ret++]-'0') {
941                                 case 0:
942                                 case 1:
943                                 case 2:
944                                 case 3:
945                                 case 4:
946                                 case 5:
947                                 case 6:
948                                         break;
949                                 case 7:
950                                 case 8:
951                                 case 9:
952                                         ret += 1;
953                                         break;
954                                 default:
955                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
956                                         break;
957                         }
958                         break;
959                 case 8:
960                         switch (src[ret++]-'0') {
961                                 case 1:
962                                 case 2:
963                                 case 4:
964                                 case 6:
965                                         break;
966                                 case 0:
967                                 case 3:
968                                 case 5:
969                                 case 7:
970                                 case 8:
971                                 case 9:
972                                         ret += 1;
973                                         break;
974                                 default:
975                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
976                                         break;
977                         }
978                         break;
979                 case 9:
980                         switch (src[ret++]-'0') {
981                                 case 0:
982                                 case 1:
983                                 case 2:
984                                 case 3:
985                                 case 4:
986                                 case 5:
987                                 case 8:
988                                         break;
989                                 case 6:
990                                 case 7:
991                                 case 9:
992                                         ret += 1;
993                                         break;
994                                 default:
995                                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
996                                         break;
997                         }
998                         break;
999                 case 0:
1000                 default:
1001                         MSG_DEBUG("The parameter(src:%s) has invalid character set", src);
1002                         return src;
1003         }
1004
1005         return &src[ret];
1006 }
1007
1008
1009 char* msg_normalize_number(char *src)
1010 {
1011         char *normalized_number;
1012
1013         if ('+' == src[0])
1014                 normalized_number = msg_clean_country_code(src);
1015         else if ('0' == src[0])
1016                 normalized_number = src+1;
1017         else
1018                 normalized_number = src;
1019
1020         MSG_DEBUG("src = %s, normalized = %s", src, normalized_number);
1021
1022         return normalized_number;
1023 }
1024
1025
1026 char *getTranslateText(const char *pkg_name, const char *locale_dir, const char *text)
1027 {
1028         char *notiMsg = NULL;
1029         char *lang = NULL;
1030
1031         lang = vconf_get_str(VCONFKEY_LANGSET);
1032
1033         setlocale(LC_MESSAGES, lang);
1034
1035         bindtextdomain(pkg_name, locale_dir);
1036
1037         notiMsg = dgettext(pkg_name, text);
1038
1039         if (lang) {
1040                 free(lang);
1041                 lang = NULL;
1042         }
1043
1044         return g_strdup(notiMsg);
1045 }
1046
1047
1048 msg_error_t MsgMakeSortRule(const MSG_SORT_RULE_S *pSortRule, char *pSqlSort)
1049 {
1050         char sql[128];
1051         char order[6];
1052
1053         memset(sql, 0x00, sizeof(sql));
1054         memset(order, 0x00, sizeof(order));
1055
1056         if (pSortRule->bAscending == true)
1057                 strncpy(order, "ASC", 5);
1058         else
1059                 strncpy(order, "DESC", 5);
1060
1061         switch (pSortRule->sortType) {
1062                 case MSG_SORT_BY_DISPLAY_FROM :
1063                         snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
1064                         break;
1065                 case MSG_SORT_BY_DISPLAY_TO :
1066                         snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
1067                         break;
1068                 case MSG_SORT_BY_DISPLAY_TIME :
1069                         snprintf(sql, sizeof(sql), "ORDER BY DISPLAY_TIME %s;", order);
1070                         break;
1071                 case MSG_SORT_BY_MSG_TYPE :
1072                         snprintf(sql, sizeof(sql), "ORDER BY MAIN_TYPE %s, DISPLAY_TIME DESC;", order);
1073                         break;
1074                 case MSG_SORT_BY_READ_STATUS :
1075                         snprintf(sql, sizeof(sql), "ORDER BY READ_STATUS %s, DISPLAY_TIME DESC;", order);
1076                         break;
1077                 case MSG_SORT_BY_STORAGE_TYPE :
1078                         snprintf(sql, sizeof(sql), "ORDER BY A.STORAGE_ID %s, A.DISPLAY_TIME DESC;", order);
1079                         break;
1080                 case MSG_SORT_BY_THREAD_NAME :
1081                         snprintf(sql, sizeof(sql), "ORDER BY FIRST_NAME %s, LAST_NAME %s;", order, order);
1082                         break;
1083                 case MSG_SORT_BY_THREAD_DATE :
1084                         snprintf(sql, sizeof(sql), "ORDER BY MSG_TIME %s;", order);
1085                         break;
1086                 case MSG_SORT_BY_THREAD_COUNT :
1087                         snprintf(sql, sizeof(sql), "ORDER BY UNREAD_CNT %s;", order);
1088                         break;
1089                 default :
1090                         snprintf(sql, sizeof(sql), "ORDER BY A.DISPLAY_TIME %s;", order);
1091                         break;
1092         }
1093
1094         memcpy(pSqlSort, sql, strlen(sql));
1095
1096         return MSG_SUCCESS;
1097 }
1098 bool msg_is_valid_email(char *pAddress)
1099 {
1100         if (!pAddress || pAddress[0] == 0)
1101                 return false;
1102         if (!strchr (pAddress, MSG_UTIL_CH_EMAIL_AT))
1103                 return false;
1104         return true;
1105 }
1106
1107 msg_error_t msg_write_text_to_msg_info(MSG_MESSAGE_INFO_S *pMsgInfo, char *text)
1108 {
1109         if (pMsgInfo->dataSize > MAX_MSG_TEXT_LEN) {
1110                 pMsgInfo->bTextSms = false;
1111
1112                 /* Save Message Data into File */
1113                 char fileName[MSG_FILENAME_LEN_MAX+1];
1114                 memset(fileName, 0x00, sizeof(fileName));
1115
1116                 if(MsgCreateFileName(fileName) == false) {
1117                         MSG_DEBUG("MsgCreateFileName error");
1118                         return MSG_ERR_STORAGE_ERROR;
1119                 }
1120
1121                 MSG_SEC_DEBUG("Save text into file : size[%d] name[%s]", pMsgInfo->dataSize, fileName);
1122
1123                 if (MsgWriteIpcFile(fileName, text, pMsgInfo->dataSize) == false) {
1124                         MSG_DEBUG("MsgWriteIpcFile error");
1125                         return MSG_ERR_STORAGE_ERROR;
1126                 }
1127
1128                 memset(pMsgInfo->msgData, 0x00, sizeof(pMsgInfo->msgData));
1129                 strncpy(pMsgInfo->msgData, fileName, MAX_MSG_DATA_LEN);
1130         } else {
1131                 pMsgInfo->bTextSms = true;
1132
1133                 memset(pMsgInfo->msgText, 0x00, sizeof(pMsgInfo->msgText));
1134                 memcpy(pMsgInfo->msgText, text, pMsgInfo->dataSize);
1135         }
1136
1137         return MSG_SUCCESS;
1138 }
1139
1140 /* change illegal filename character to '_' */
1141 void msg_replace_available_file_name(char *fileName)
1142 {
1143         int idx = 0;
1144         int len = 0;
1145         bool is_converted = false;
1146
1147         if (fileName) {
1148                 len = strlen(fileName);
1149
1150                 while (fileName[idx] != 0) {
1151                         if (idx >= len) {
1152                                 MSG_WARN("idx : %d, len : %d", idx, len);
1153                                 break;
1154                         }
1155
1156                         if (fileName[idx] == '\\' || fileName[idx] == '/' || fileName[idx] == '?' || fileName[idx] == '%' || fileName[idx] == '*' ||
1157                                 fileName[idx] == ':' || fileName[idx] == '|' || fileName[idx] == '"' || fileName[idx] == '<' || fileName[idx] == '>') {
1158                                 fileName[idx++] = '_';
1159                                 is_converted = true;
1160                         } else {
1161                                 idx++;
1162                         }
1163                 }
1164         }
1165
1166         if (is_converted)
1167                 MSG_SEC_DEBUG("converted filename : [%s]", fileName);
1168 }
1169
1170 /* change character ' ' to '_' */
1171 void msg_replace_space_char(char *pszText)
1172 {
1173         if (!pszText) {
1174                 MSG_ERR("pszText is NULL");
1175                 return;
1176         }
1177
1178         char *spaceCharPtr = strchr(pszText, ' ');
1179
1180         while (spaceCharPtr) {
1181                 *spaceCharPtr = '_';
1182                 spaceCharPtr = strchr(pszText, ' ');
1183         }
1184 }
1185
1186 /* change non-ascii character to underscore */
1187 gchar * msg_replace_non_ascii_char(const gchar *pszText, gunichar replacementChar)
1188 {
1189         if (!pszText) {
1190                 MSG_ERR(" msg_replace_non_ascii_char error : pszText is NULL");
1191                 return NULL;
1192         }
1193         gchar *res;
1194         gsize result_len = 0;
1195         const gchar *p;
1196         result_len = g_utf8_strlen(pszText, -1) + 1; /* +1 for malloc of non-terminating chracter */
1197         res = (gchar *)g_malloc (result_len * sizeof (gchar));
1198         int i = 0;
1199         for (p = pszText, i = 0; *p != '\0'; p = g_utf8_next_char(p), i++) {
1200                 res[i] = isascii(g_utf8_get_char(p)) ? *p : replacementChar;
1201         }
1202         res[i] = '\0';
1203         return res;
1204 }
1205
1206
1207 static int __find_login_user(uid_t *uid)
1208 {
1209         uid_t *uids = NULL;
1210         char *state = NULL;
1211
1212         int uids_len = sd_get_uids(&uids);
1213         if (uids_len <= 0)
1214                 return -1;
1215
1216         for (int i = 0; i < uids_len; i++) {
1217                 if (sd_uid_get_state(uids[i], &state) < 0) {
1218                         free(uids);
1219                         return -1;
1220                 } else {
1221                         if (g_strcmp0(state, "online") == 0) {
1222                                 *uid = uids[i];
1223                                 free(uids);
1224                                 free(state);
1225                                 return 0;
1226                         }
1227                 }
1228
1229                 free(state);
1230         }
1231
1232         free(uids);
1233         return -1;
1234 }
1235
1236
1237 uid_t msg_get_login_user()
1238 {
1239         uid_t uid = -1;
1240
1241         if (__find_login_user(&uid) < 0) {
1242                 MSG_WARN("Cannot find login user");
1243         }
1244
1245         MSG_DEBUG("login user id [%d]", uid);
1246
1247         return uid;
1248 }
1249
1250
1251 void* _msg_launch_app(void *data)
1252 {
1253         if (data) {
1254                 msg_launch_app_data *ad = (msg_launch_app_data *)data;
1255                 int ret = aul_launch_app_for_uid(ad->app_id, ad->bundle_data, msg_get_login_user());
1256                 if (ret <= 0) {
1257                         MSG_DEBUG("aul_launch_app_for_uid() is failed : %d", ret);
1258                 }
1259
1260                 g_free(ad->app_id);
1261                 bundle_free(ad->bundle_data);
1262                 g_free(ad);
1263         }
1264
1265         return NULL;
1266 }
1267
1268 msg_error_t msg_launch_app(const char *app_id, bundle *bundle_data)
1269 {
1270         msg_launch_app_data *data = (msg_launch_app_data *)calloc(1, sizeof(msg_launch_app_data));
1271         data->app_id = g_strdup(app_id);
1272         data->bundle_data = bundle_dup(bundle_data);
1273         pthread_t thd;
1274
1275         if (pthread_create(&thd, NULL, &_msg_launch_app, data) < 0) {
1276                 MSG_DEBUG("pthread_create() error");
1277         }
1278
1279         pthread_detach(thd);
1280         return MSG_SUCCESS;
1281 }
1282
1283
1284 msg_error_t msg_aul_svc_set_operation(bundle *bundle_data, const char *operation)
1285 {
1286         int ret = aul_svc_set_operation(bundle_data, operation);
1287         if (ret < 0) {
1288                 MSG_DEBUG("aul_svc_set_operation() is failed : %d", ret);
1289                 return MSG_ERR_UNKNOWN;
1290         }
1291
1292         return MSG_SUCCESS;
1293 }
1294
1295
1296 msg_error_t msg_aul_svc_set_uri(bundle *bundle_data, char *uri)
1297 {
1298         int ret = aul_svc_set_uri(bundle_data, uri);
1299         if (ret < 0) {
1300                 MSG_DEBUG("aul_svc_set_uri() is failed : %d", ret);
1301                 return MSG_ERR_UNKNOWN;
1302         }
1303
1304         return MSG_SUCCESS;
1305 }
1306
1307
1308 void msg_set_dpm_policy(int type, int state)
1309 {
1310         dpm_policy_enable[type] = state;
1311 }
1312
1313
1314 bool msg_check_dpm_policy(int type)
1315 {
1316         return dpm_policy_enable[MSG_SMS_TYPE];
1317 //      return dpm_policy_enable[type];
1318 }
1319