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