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