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