2.0_beta
[platform/core/messaging/msg-service.git] / utils / MsgUtilFunction.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 "MsgUtilFunction.h"
19
20  /*==================================================================================================
21                                      FUNCTION IMPLEMENTATION
22 ==================================================================================================*/
23
24 // Encoders
25 int MsgEncodeCountInfo(MSG_COUNT_INFO_S *pCountInfo, char **ppDest)
26 {
27         int dataSize = 0;
28
29         dataSize = sizeof(MSG_COUNT_INFO_S);
30
31         *ppDest = (char*)new char[dataSize];
32
33         void* p = (void*)*ppDest;
34
35         memcpy(p, pCountInfo, dataSize);
36
37         return dataSize;
38 }
39
40
41 int MsgEncodeRecipientList(MSG_RECIPIENTS_LIST_S *pRecipientList, char **ppDest)
42 {
43         int count = 0, dataSize = 0;
44
45         count = pRecipientList->recipientCnt;
46         dataSize = sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*count);
47
48         *ppDest = (char*)new char[dataSize];
49
50         void* p = (void*)*ppDest;
51
52         memcpy(p, &count, sizeof(int));
53         p = (void*)((char*)p + sizeof(int));
54
55         for (int i = 0; i < count; i++)
56         {
57                 memcpy(p, &(pRecipientList->recipientAddr[i]), sizeof(MSG_ADDRESS_INFO_S));
58                 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
59         }
60
61         return dataSize;
62 }
63
64
65 int MsgEncodeCountByMsgType(int MsgCount, char **ppDest)
66 {
67         int dataSize = 0;
68
69         dataSize = sizeof(int);
70
71         *ppDest = (char*)new char[dataSize];
72
73         void* p = (void*)*ppDest;
74
75         memcpy(p, &MsgCount, dataSize);
76
77         return dataSize;
78 }
79
80
81 int MsgEncodeMsgId(msg_message_id_t *pMsgId, char **ppDest)
82 {
83         int dataSize = 0;
84
85         dataSize = (sizeof(msg_message_id_t));
86
87         *ppDest = (char*)new char[dataSize];
88
89         void* p = (void*)*ppDest;
90
91         memcpy(p, pMsgId, dataSize);
92
93         return dataSize;
94 }
95
96
97 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
98 {
99         int dataSize = 0;
100
101         dataSize = sizeof(MSG_MESSAGE_INFO_S);
102
103         *ppDest = (char*)new char[dataSize];
104
105         void* p = (void*)*ppDest;
106
107         memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
108
109         p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
110
111         return dataSize;
112 }
113
114
115 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo, char **ppDest)
116 {
117         int dataSize = 0;
118
119         dataSize = (sizeof(MSG_MESSAGE_INFO_S) + sizeof(MSG_SENDINGOPT_INFO_S));
120
121         *ppDest = (char*)new char[dataSize];
122
123         void* p = (void*)*ppDest;
124
125         memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
126
127         p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
128
129         memcpy(p, pSendOptInfo, sizeof(MSG_SENDINGOPT_INFO_S));
130
131         return dataSize;
132 }
133
134 int MsgEncodeFolderList(msg_struct_list_s *pFolderList, char **ppDest)
135 {
136         int count = 0, dataSize = 0;
137
138         count = pFolderList->nCount;
139         dataSize = sizeof(int) + (sizeof(MSG_FOLDER_INFO_S)*count);
140
141         *ppDest = (char*)new char[dataSize];
142
143         void* p = (void*)*ppDest;
144
145         memcpy(p, &count, sizeof(int));
146         p = (void*)((char*)p + sizeof(int));
147
148         msg_struct_s *folder_info = NULL;
149
150         for (int i = 0; i < count; i++)
151         {
152                 folder_info = (msg_struct_s *)pFolderList->msg_struct_info[i];
153                 memcpy(p, folder_info->data, sizeof(MSG_FOLDER_INFO_S));
154                 p = (void*)((char*)p + sizeof(MSG_FOLDER_INFO_S));
155         }
156
157         return dataSize;
158 }
159
160 int MsgEncodeFilterList(msg_struct_list_s *pFilterList, char **ppDest)
161 {
162         int count = 0, dataSize = 0;
163
164         count = pFilterList->nCount;
165         dataSize = sizeof(int) + (sizeof(MSG_FILTER_S)*count);
166
167         *ppDest = (char*)new char[dataSize];
168
169         void* p = (void*)*ppDest;
170
171         memcpy(p, &count, sizeof(int));
172         p = (void*)((char*)p + sizeof(int));
173
174         msg_struct_s *filter_info = NULL;
175
176         for (int i = 0; i < count; i++)
177         {
178                 filter_info = (msg_struct_s *)pFilterList->msg_struct_info[i];
179                 memcpy(p, filter_info->data, sizeof(MSG_FILTER_S));
180                 p = (void*)((char*)p + sizeof(MSG_FILTER_S));
181         }
182
183         return dataSize;
184 }
185
186
187 int MsgEncodeFilterFlag(bool *pSetFlag, char **ppDest)
188 {
189         int dataSize = 0;
190
191         dataSize = (sizeof(bool));
192
193         *ppDest = (char*)new char[dataSize];
194
195         void* p = (void*)*ppDest;
196
197         memcpy(p, pSetFlag, dataSize);
198
199         return dataSize;
200 }
201
202
203 int MsgEncodeMsgType(MSG_MESSAGE_TYPE_S *pMsgType, char **ppDest)
204 {
205         int dataSize = 0;
206
207         dataSize = (sizeof(MSG_MESSAGE_TYPE_S));
208
209         *ppDest = (char*)new char[dataSize];
210
211         void* p = (void*)*ppDest;
212
213         memcpy(p, pMsgType, dataSize);
214
215         return dataSize;
216 }
217
218
219 int MsgEncodeThreadViewList(msg_struct_list_s *pThreadViewList, char **ppDest)
220 {
221         int count = 0, dataSize = 0;
222
223         count = pThreadViewList->nCount;
224
225         dataSize = sizeof(int) + (sizeof(MSG_THREAD_VIEW_S)*count);
226
227         *ppDest = (char*)new char[dataSize];
228
229         void* p = (void*)*ppDest;
230
231         memcpy(p, &count, sizeof(int));
232         p = (void*)((char*)p + sizeof(int));
233
234         msg_struct_s *thread_info = NULL;
235
236         for (int i = 0; i < count; i++)
237         {
238                 thread_info = (msg_struct_s *)pThreadViewList->msg_struct_info[i];
239                 memcpy(p, thread_info->data, sizeof(MSG_THREAD_VIEW_S));
240                 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
241         }
242
243         return dataSize;
244 }
245
246
247 int MsgEncodeConversationViewList(msg_struct_list_s *pConvViewList, char **ppDest)
248 {
249         int count = 0, dataSize = 0;
250
251         count = pConvViewList->nCount;
252
253         dataSize = sizeof(int) + (sizeof(msg_struct_list_s)*count);
254
255         *ppDest = (char*)new char[dataSize];
256
257         void* p = (void*)*ppDest;
258
259         memcpy(p, &count, sizeof(int));
260         p = (void*)((char*)p + sizeof(int));
261
262         for (int i = 0; i < count; i++)
263         {
264                 memcpy(p, &(pConvViewList->msg_struct_info[i]), sizeof(msg_struct_list_s));
265                 p = (void*)((char*)p + sizeof(msg_struct_list_s));
266         }
267
268         return dataSize;
269 }
270
271
272 int MsgEncodeMsgGetContactCount(MSG_THREAD_COUNT_INFO_S *threadCountInfo, char **ppDest)
273 {
274         int dataSize = sizeof(int) + sizeof(int) + sizeof(int) + sizeof(int);
275
276         MSG_DEBUG("datasize = [%d] \n", dataSize);
277
278         *ppDest = (char*)new char[dataSize];
279
280         void* p = (void*)*ppDest;
281
282         memcpy(p, &(threadCountInfo->totalCount), sizeof(int));
283         p = (void*)((char*)p + sizeof(int));
284
285         memcpy(p, &(threadCountInfo->unReadCount), sizeof(int));
286         p = (void*)((char*)p + sizeof(int));
287
288         memcpy(p, &(threadCountInfo->mmsMsgCount), sizeof(int));
289         p = (void*)((char*)p + sizeof(int));
290
291         memcpy(p, &(threadCountInfo->smsMsgCount), sizeof(int));
292         p = (void*)((char*)p + sizeof(int));
293
294         return dataSize;
295 }
296
297 int MsgEncodeMemSize(unsigned int *memsize, char **ppDest)
298 {
299         int dataSize = 0;
300
301         dataSize = sizeof(unsigned int);
302
303         *ppDest = (char*)new char[dataSize];
304
305         void* p = (void*)*ppDest;
306
307         memcpy(p, memsize, dataSize);
308
309         return dataSize;
310 }
311
312
313 int MsgEncodeSyncMLOperationData(int msgId, int extId, char **ppDest)
314 {
315         int dataSize = 0;
316
317         dataSize = sizeof(int) + sizeof(int);
318
319         *ppDest = (char*)new char[dataSize];
320
321         void* p = (void*)*ppDest;
322
323         memcpy(p, &msgId, sizeof(int));
324         p = (void*)((char*)p + sizeof(int));
325
326         memcpy(p, &extId, sizeof(int));
327
328         return dataSize;
329 }
330
331
332 int MsgEncodeStorageChangeData(const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList, char **ppDest)
333 {
334         int dataSize = 0;
335         int count = 0;
336
337         count = pMsgIdList->nCount;
338
339         dataSize = sizeof(msg_storage_change_type_t) + sizeof(int) + (sizeof(msg_message_id_t)*count);
340
341         *ppDest = (char*)new char[dataSize];
342
343         void* p = (void*)*ppDest;
344
345         memcpy(p, &storageChangeType, sizeof(msg_storage_change_type_t));
346         p = (void*)((char*)p + sizeof(msg_storage_change_type_t));
347
348         memcpy(p, &count, sizeof(int));
349         p = (void*)((char*)p + sizeof(int));
350
351         for (int i = 0; i < count; i++) {
352                 memcpy(p, &(pMsgIdList->msgIdList[i]), sizeof(msg_message_id_t));
353                 p = (void*)((char*)p + sizeof(msg_message_id_t));
354         }
355
356         return dataSize;
357 }
358
359
360 int MsgEncodeReportStatus(MSG_REPORT_STATUS_INFO_S* pReportStatus, char **ppDest)
361 {
362         int dataSize = 0;
363
364         dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S));
365
366         *ppDest = (char*)new char[dataSize];
367
368         void* p = (void*)*ppDest;
369
370         memcpy(p, pReportStatus, dataSize);
371
372         return dataSize;
373 }
374
375
376 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
377 {
378         int dataSize = 0;
379
380         dataSize = (sizeof(msg_thread_id_t));
381
382         *ppDest = (char*)new char[dataSize];
383
384         void* p = (void*)*ppDest;
385
386         memcpy(p, pThreadId, dataSize);
387
388         return dataSize;
389 }
390
391
392 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
393 {
394         int dataSize = 0;
395
396         dataSize = sizeof(MSG_THREAD_VIEW_S);
397
398         *ppDest = (char*)new char[dataSize];
399
400         void* p = (void*)*ppDest;
401
402         memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
403
404         p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
405
406         return dataSize;
407 }
408
409
410
411 // Decoders
412 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
413 {
414         memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
415 }
416
417
418 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
419 {
420         memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
421 }
422
423
424 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
425 {
426         memcpy(memsize, pSrc, sizeof(unsigned int));
427 }
428
429
430 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
431 {
432         memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
433
434         pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
435
436         memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
437 }
438
439 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
440 {
441         int count = 0;
442
443         memcpy(&count, pSrc, sizeof(int));
444         pSrc = pSrc + sizeof(int);
445
446         pRecipientList->recipientCnt= count;
447         pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
448
449         MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
450
451         for (int i = 0; i < count; i++)
452         {
453                 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
454                 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
455                 pInfoTmp++;
456         }
457 }
458
459
460 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
461 {
462         int count = 0;
463
464         memcpy(&count, pSrc, sizeof(int));
465         pSrc = pSrc + sizeof(int);
466
467         if( count > 0 )
468         {
469                 pFolderList->nCount = count;
470                 pFolderList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FOLDER_INFO_S *)*count];
471
472                 msg_struct_s *pInfoTmp = NULL;
473
474                 for (int i = 0; i < count; i++)
475                 {
476
477                         pFolderList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
478                         pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
479                         pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
480                         pInfoTmp->data = new char[sizeof(MSG_FOLDER_INFO_S)];
481                         memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
482                         pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
483                 }
484         }
485         else if ( count == 0 )
486         {
487                 pFolderList->nCount = count;
488                 pFolderList->msg_struct_info = NULL;
489         }
490 }
491
492 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
493 {
494         int count = 0;
495
496         memcpy(&count, pSrc, sizeof(int));
497         pSrc = pSrc + sizeof(int);
498
499         if( count > 0 )
500         {
501                 pFilterList->nCount = count;
502                 pFilterList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FILTER_S *)*count];
503
504                 msg_struct_s *pStructTmp = NULL;
505
506                 for (int i = 0; i < count; i++)
507                 {
508                         pFilterList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
509                         pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
510                         pStructTmp->type = MSG_STRUCT_FILTER;
511                         pStructTmp->data = new char[sizeof(MSG_FILTER_S)];
512                         memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
513                         pSrc = pSrc + sizeof(MSG_FILTER_S);
514                 }
515         }
516         else if ( count == 0 )
517         {
518                 pFilterList->nCount = count;
519                 pFilterList->msg_struct_info = NULL;
520         }
521
522 }
523
524
525 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
526 {
527         memcpy(pSetFlag, pSrc, sizeof(bool));
528 }
529
530
531 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
532 {
533         memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
534 }
535
536 void    MsgDecodeContactCount(char *pSrc,  MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
537 {
538         int count = 0;
539
540         if(pSrc == NULL)
541                 return;
542
543         memcpy(&count, pSrc, sizeof(int));
544         pSrc = pSrc + sizeof(int);
545         pMsgThreadCountList->totalCount = count;
546
547
548         memcpy(&count, pSrc, sizeof(int));
549         pSrc = pSrc + sizeof(int);
550         pMsgThreadCountList->unReadCount        = count;
551
552
553         memcpy(&count, pSrc, sizeof(int));
554         pSrc = pSrc + sizeof(int);
555         pMsgThreadCountList->mmsMsgCount        = count;
556
557
558         memcpy(&count, pSrc, sizeof(int));
559         pSrc = pSrc + sizeof(int);
560         pMsgThreadCountList->smsMsgCount        = count;
561
562
563         return;
564 }
565
566
567 void    MsgDecodeReportStatus(char *pSrc,  MSG_REPORT_STATUS_INFO_S *pReportStatus)
568 {
569         int count = 0;
570
571         if(pSrc == NULL)
572                 return;
573
574         memcpy(&count, pSrc, sizeof(msg_delivery_report_status_t));
575         pSrc = pSrc + sizeof(msg_delivery_report_status_t);
576         pReportStatus->deliveryStatus = count;
577
578
579         memcpy(&count, pSrc, sizeof(time_t));
580         pSrc = pSrc + sizeof(time_t);
581         pReportStatus->deliveryStatusTime = count;
582
583
584         memcpy(&count, pSrc, sizeof(msg_read_report_status_t));
585         pSrc = pSrc + sizeof(msg_read_report_status_t);
586         pReportStatus->readStatus = count;
587
588
589         memcpy(&count, pSrc, sizeof(time_t));
590         pSrc = pSrc + sizeof(time_t);
591         pReportStatus->readStatusTime = count;
592
593
594         return;
595 }
596
597 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
598 {
599         memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
600 }
601
602 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
603 {
604         memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
605 }
606
607 // Event Encoder
608 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
609 {
610         MSG_EVENT_S* pMsgEvent = NULL;
611
612         *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
613
614         pMsgEvent = (MSG_EVENT_S*)*ppEvent;
615
616         pMsgEvent->eventType = MsgEvent;
617         pMsgEvent->result = MsgError;
618
619         MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
620         MSG_DEBUG("result [%d]", pMsgEvent->result);
621
622         if (DataSize > 0)
623                 memcpy((void*)pMsgEvent->data, pData, DataSize);
624
625         return (sizeof(MSG_EVENT_S) + DataSize);
626 }
627
628 int msg_verify_number(const char *raw, char *trimmed)
629 {
630         if (!(raw && trimmed)) {
631                 MSG_DEBUG("Phone Number is NULL");
632                 return MSG_ERR_NULL_POINTER;
633         }
634
635         for (int i=0, j=0 ; raw[i] ; i++) {
636                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
637                                 || raw[i] == '*' ||  raw[i] == '#') {
638                         trimmed[j++] = raw[i];
639                 } else if (raw[i] == '-') {
640                         continue;
641                 } else {
642                         MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
643                         return MSG_ERR_INVALID_PARAMETER;
644                 }
645         }
646
647         MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
648         return MSG_SUCCESS;
649 }
650
651 int msg_verify_email(const char *raw)
652 {
653         bool onlyNum = true;
654         bool atExist = false;
655
656         if (!raw) {
657                 MSG_DEBUG("Email is NULL");
658                 return MSG_ERR_NULL_POINTER;
659         }
660
661         for (int i = 0; raw[i]; i++) {
662
663                 if (raw[i] == '@') {
664                         onlyNum = false;
665
666                         if (atExist == false) {
667                                 atExist = true;
668                                 continue;
669                         } else {
670                                 MSG_DEBUG("Character [@] is included more than twice in email address.");
671                                 return MSG_ERR_INVALID_PARAMETER;
672                         }
673                 }
674
675                 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' ||  raw[i] == '#') {
676                         continue;
677                 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
678                         onlyNum = false;
679                         continue;
680                 } else if (raw[i] == ',') {
681                         if (onlyNum == false && atExist == false) {
682                                 MSG_DEBUG("Unacceptable type in address.");
683                                 return MSG_ERR_INVALID_PARAMETER;
684                         }
685                         atExist = false;
686                         onlyNum = true;
687                         continue;
688                 } else {
689                         MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
690                         return MSG_ERR_INVALID_PARAMETER;
691                 }
692         }
693
694         return MSG_SUCCESS;
695 }