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