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