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