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