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