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