dlog print error fix
[platform/core/messaging/msg-service.git] / proxy / MsgHandleStorage.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include "MsgDebug.h"
18 #include "MsgUtilFile.h"
19 #include "MsgCppTypes.h"
20 #include "MsgException.h"
21 #include "MsgUtilFunction.h"
22 #include "MsgProxyListener.h"
23 #include "MsgHandle.h"
24
25 #include "MsgContact.h"
26 #include "MsgVMessage.h"
27 /*==================================================================================================
28                                      IMPLEMENTATION OF MsgHandle - Storage Member Functions
29 ==================================================================================================*/
30 int MsgHandle::addMessage(MSG_MESSAGE_HIDDEN_S *pMsg, const MSG_SENDINGOPT_S *pSendOpt)
31 {
32         MSG_MESSAGE_INFO_S msgInfo = {0, };
33         MSG_SENDINGOPT_INFO_S sendOptInfo = {0, };
34
35         msgInfo.addressList = NULL;
36         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
37
38         /* Covert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S */
39         convertMsgStruct(pMsg, &msgInfo);
40
41         /* Covert MSG_SENDINGOPT_S to MSG_SENDINGOPT_INFO_S */
42         MSG_MESSAGE_TYPE_S msgType = {0, };
43
44         msgType.mainType = pMsg->mainType;
45         msgType.subType = pMsg->subType;
46         msgType.classType = pMsg->classType;
47
48         convertSendOptStruct(pSendOpt, &sendOptInfo, msgType);
49
50         /* Allocate Memory to Command Data */
51         char* encodedData = NULL;
52         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
53         int dataSize = MsgEncodeMsgInfo(&msgInfo, &sendOptInfo, &encodedData);
54
55         int cmdSize = sizeof(MSG_CMD_S) + dataSize;
56
57         char cmdBuf[cmdSize];
58         bzero(cmdBuf, cmdSize);
59         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
60
61         /* Set Command Parameters */
62         pCmd->cmdType = MSG_CMD_ADD_MSG;
63
64         /* Copy Cookie */
65         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
66
67         /* Copy Command Data */
68         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), encodedData, dataSize);
69
70         /* Send Command to Messaging FW */
71         char* pEventData = NULL;
72         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
73
74         write((char*)pCmd, cmdSize, &pEventData);
75
76         /* Get Return Data */
77         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
78
79         if (pEvent == NULL)
80                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
81
82         if (pEvent->eventType != MSG_EVENT_ADD_MSG) {
83                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
84         }
85
86         if (pEvent->result != MSG_SUCCESS)
87                 return pEvent->result;
88
89         msg_message_id_t msgId = 0;
90
91         /* Decode Return Data */
92         MsgDecodeMsgId(pEvent->data, &msgId);
93
94         return (int)msgId;
95 }
96
97
98 msg_error_t MsgHandle::addSyncMLMessage(const MSG_SYNCML_MESSAGE_S *pSyncMLMsg)
99 {
100         MSG_MESSAGE_INFO_S msgInfo;
101         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
102
103         msgInfo.addressList = NULL;
104         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
105
106         /* Covert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S */
107         msg_struct_s *msg = (msg_struct_s *)pSyncMLMsg->msg;
108         convertMsgStruct((MSG_MESSAGE_HIDDEN_S *)msg->data, &msgInfo);
109
110         /* Allocate Memory to Command Data */
111         char* encodedData = NULL;
112         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
113         int dataSize = MsgEncodeMsgInfo(&msgInfo, &encodedData);
114
115         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + sizeof(int) + dataSize;
116
117         char cmdBuf[cmdSize];
118         bzero(cmdBuf, cmdSize);
119         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
120
121         /* Set Command Parameters */
122         pCmd->cmdType = MSG_CMD_ADD_SYNCML_MSG;
123
124         /* Copy Cookie */
125         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
126
127         /* Copy Command Data */
128         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &pSyncMLMsg->extId, sizeof(int));
129         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), &pSyncMLMsg->pinCode, sizeof(int));
130         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)+sizeof(int)), encodedData, dataSize);
131
132         /* Send Command to Messaging FW */
133         char* pEventData = NULL;
134         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
135
136
137         write((char*)pCmd, cmdSize, &pEventData);
138
139         /* Get Return Data */
140         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
141
142         if (pEvent == NULL)
143                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
144
145         if (pEvent->eventType != MSG_EVENT_ADD_SYNCML_MSG) {
146                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
147         }
148
149         msg_message_id_t msgId = 0;
150
151         /* Decode Return Data */
152         MsgDecodeMsgId(pEvent->data, &msgId);
153
154         ((MSG_MESSAGE_HIDDEN_S *)msg->data)->msgId = msgId;
155
156         return pEvent->result;
157 }
158
159
160 msg_error_t MsgHandle::updateMessage(const MSG_MESSAGE_HIDDEN_S *pMsg, const MSG_SENDINGOPT_S *pSendOpt)
161 {
162         MSG_MESSAGE_INFO_S msgInfo = {0, };
163         MSG_SENDINGOPT_INFO_S sendOptInfo = {0, };
164
165         msgInfo.addressList = NULL;
166         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
167
168         /* Covert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S */
169         convertMsgStruct(pMsg, &msgInfo);
170
171         if (pSendOpt != NULL) {
172                 MSG_MESSAGE_TYPE_S msgType = {0, };
173
174                 msgType.mainType = pMsg->mainType;
175                 msgType.subType = pMsg->subType;
176                 msgType.classType = pMsg->classType;
177
178                 convertSendOptStruct(pSendOpt, &sendOptInfo, msgType);
179         }
180
181         /* Allocate Memory to Command Data */
182         char* encodedData = NULL;
183         unique_ptr<char*, void(*)(char**)> buf(&encodedData, unique_ptr_deleter);
184         int dataSize = MsgEncodeMsgInfo(&msgInfo, &sendOptInfo, &encodedData);
185
186         int cmdSize = sizeof(MSG_CMD_S) + dataSize;
187
188         char cmdBuf[cmdSize];
189         bzero(cmdBuf, cmdSize);
190         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
191
192         /* Set Command Parameters */
193         pCmd->cmdType = MSG_CMD_UPDATE_MSG;
194
195         /* Copy Cookie */
196         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
197
198         /* Copy Command Data */
199         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), encodedData, dataSize);
200
201         /* Send Command to Messaging FW */
202         char* pEventData = NULL;
203         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
204
205         write((char*)pCmd, cmdSize, &pEventData);
206
207         /* Get Return Data */
208         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
209
210         if (pEvent == NULL)
211                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
212
213         if (pEvent->eventType != MSG_EVENT_UPDATE_MSG) {
214                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
215         }
216
217         return pEvent->result;
218 }
219
220
221 msg_error_t MsgHandle::updateReadStatus(msg_message_id_t MsgId, bool bRead)
222 {
223         /* Allocate Memory to Command Data */
224         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t) + sizeof(bool);
225
226         char cmdBuf[cmdSize];
227         bzero(cmdBuf, cmdSize);
228         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
229
230         /* Set Command Parameters */
231         pCmd->cmdType = MSG_CMD_UPDATE_READ;
232
233         /* Copy Cookie */
234         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
235
236         /* Copy Command Data */
237         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
238         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), &bRead, sizeof(bool));
239
240         /* Send Command to Messaging FW */
241         char* pEventData = NULL;
242         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
243
244         write((char*)pCmd, cmdSize, &pEventData);
245
246         /* Get Return Data */
247         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
248
249         if (pEvent == NULL)
250                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
251
252         if (pEvent->eventType != MSG_EVENT_UPDATE_READ) {
253                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
254         }
255
256         return pEvent->result;
257 }
258
259
260 msg_error_t MsgHandle::setConversationToRead(msg_thread_id_t ThreadId)
261 {
262         /* Allocate Memory to Command Data */
263         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t);
264
265         char cmdBuf[cmdSize];
266         bzero(cmdBuf, cmdSize);
267         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
268
269         /* Set Command Parameters */
270         pCmd->cmdType = MSG_CMD_UPDATE_THREAD_READ;
271
272         /* Copy Cookie */
273         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
274
275         /* Copy Command Data */
276         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &ThreadId, sizeof(msg_thread_id_t));
277
278         /* Send Command to Messaging FW */
279         char* pEventData = NULL;
280         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
281
282         write((char*)pCmd, cmdSize, &pEventData);
283
284         /* Get Return Data */
285         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
286
287         if (pEvent == NULL)
288                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
289
290         if (pEvent->eventType != MSG_EVENT_UPDATE_THREAD_READ) {
291                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
292         }
293
294         return pEvent->result;
295 }
296
297
298 msg_error_t MsgHandle::updateProtectedStatus(msg_message_id_t MsgId, bool bProtected)
299 {
300         /* Allocate Memory to Command Data */
301         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t) + sizeof(bool);
302
303         char cmdBuf[cmdSize];
304         bzero(cmdBuf, cmdSize);
305         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
306
307         /* Set Command Parameters */
308         pCmd->cmdType = MSG_CMD_UPDATE_PROTECTED;
309
310         /* Copy Cookie */
311         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
312
313         /* Copy Command Data */
314         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
315         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), &bProtected, sizeof(bool));
316
317         /* Send Command to Messaging FW */
318         char* pEventData = NULL;
319         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
320
321         write((char*)pCmd, cmdSize, &pEventData);
322
323         /* Get Return Data */
324         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
325
326         if (pEvent == NULL)
327                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
328
329         if (pEvent->eventType != MSG_EVENT_UPDATE_PROTECTED) {
330                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
331         }
332
333         return pEvent->result;
334 }
335
336
337 msg_error_t MsgHandle::deleteMessage(msg_message_id_t MsgId)
338 {
339         /* Allocate Memory to Command Data */
340         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
341
342         char cmdBuf[cmdSize];
343         bzero(cmdBuf, cmdSize);
344         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
345
346         /* Set Command Parameters */
347         pCmd->cmdType = MSG_CMD_DELETE_MSG;
348
349         /* Copy Cookie */
350         memcpy((void*)pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
351
352         /* Copy Command Data */
353         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
354
355         /* Send Command to Messaging FW */
356         char* pEventData = NULL;
357         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
358
359         write((char*)pCmd, cmdSize, &pEventData);
360
361         /* Get Return Data */
362         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
363
364         if (pEvent == NULL)
365                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
366
367         if (pEvent->eventType != MSG_EVENT_DELETE_MSG) {
368                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
369         }
370
371         return pEvent->result;
372 }
373
374
375 msg_error_t MsgHandle::deleteAllMessagesInFolder(msg_folder_id_t FolderId, bool bOnlyDB)
376 {
377         /* Allocate Memory to Command Data */
378         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_folder_id_t);
379
380         char cmdBuf[cmdSize];
381         bzero(cmdBuf, cmdSize);
382         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
383
384         /* Set Command Parameters */
385         pCmd->cmdType = MSG_CMD_DELALL_MSGINFOLDER;
386
387         /* Copy Cookie */
388         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
389
390         /* Copy Command Data */
391         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &FolderId, sizeof(msg_folder_id_t));
392         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_folder_id_t)), &bOnlyDB, sizeof(bool));
393
394         /* Send Command to Messaging FW */
395         char* pEventData = NULL;
396         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
397
398         write((char*)pCmd, cmdSize, &pEventData);
399
400         /* Get Return Data */
401         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
402
403         if (pEvent == NULL)
404                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
405
406         if (pEvent->eventType != MSG_EVENT_DELALL_MSGINFOLDER) {
407                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
408         }
409
410         return pEvent->result;
411 }
412
413
414 msg_error_t MsgHandle::deleteMessagesByList(msg_id_list_s *pMsgIdList)
415 {
416         /* Allocate Memory to Command Data */
417         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + (sizeof(int)*pMsgIdList->nCount);
418
419         char cmdBuf[cmdSize];
420         bzero(cmdBuf, cmdSize);
421         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
422
423         /* Set Command Parameters */
424         pCmd->cmdType = MSG_CMD_DELETE_MESSAGE_BY_LIST;
425
426         /* Copy Cookie */
427         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
428
429         /* Copy Command Data */
430         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &(pMsgIdList->nCount), sizeof(int));
431         for (int i = 0; i < pMsgIdList->nCount; i++) {
432                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)+(sizeof(int)*i)), &(pMsgIdList->msgIdList[i]), sizeof(int));
433         }
434
435         /* Send Command to Messaging FW */
436         char* pEventData = NULL;
437         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
438
439         write((char*)pCmd, cmdSize, &pEventData);
440
441         /* Get Return Data */
442         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
443
444         if (pEvent == NULL)
445                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
446
447         if (pEvent->eventType != MSG_EVENT_DELETE_MESSAGE_BY_LIST) {
448                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
449         }
450
451         return pEvent->result;
452 }
453
454
455 msg_error_t MsgHandle::moveMessageToFolder(msg_message_id_t MsgId, msg_folder_id_t DestFolderId)
456 {
457         /* Allocate Memory to Command Data */
458         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t) + sizeof(msg_folder_id_t);
459
460         char cmdBuf[cmdSize];
461         bzero(cmdBuf, cmdSize);
462         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
463
464         /* Set Command Parameters */
465         pCmd->cmdType = MSG_CMD_MOVE_MSGTOFOLDER;
466
467         /* Copy Cookie */
468         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
469
470         /* Copy Command Data */
471         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
472         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+sizeof(msg_message_id_t)+MAX_COOKIE_LEN), &DestFolderId, sizeof(msg_folder_id_t));
473
474         /* Send Command to Messaging FW */
475         char* pEventData = NULL;
476         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
477
478         write((char*)pCmd, cmdSize, &pEventData);
479
480         /* Get Return Data */
481         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
482
483         if (pEvent == NULL)
484                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
485
486         if (pEvent->eventType != MSG_EVENT_MOVE_MSGTOFOLDER) {
487                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
488         }
489
490         return pEvent->result;
491 }
492
493
494 msg_error_t MsgHandle::moveMessageToStorage(msg_message_id_t MsgId, msg_storage_id_t DestStorageId)
495 {
496         /* Allocate Memory to Command Data */
497         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t) + sizeof(msg_storage_id_t);
498
499         char cmdBuf[cmdSize];
500         bzero(cmdBuf, cmdSize);
501         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
502
503         /* Set Command Parameters */
504         pCmd->cmdType = MSG_CMD_MOVE_MSGTOSTORAGE;
505
506         /* Copy Cookie */
507         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
508
509         /* Copy Command Data */
510         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
511         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), &DestStorageId, sizeof(msg_storage_id_t));
512
513         /* Send Command to Messaging FW */
514         char* pEventData = NULL;
515         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
516
517         write((char*)pCmd, cmdSize, &pEventData);
518
519         /* Get Return Data */
520         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
521
522         if (pEvent == NULL)
523                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
524
525         if (pEvent->eventType != MSG_EVENT_MOVE_MSGTOSTORAGE) {
526                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
527         }
528
529         return pEvent->result;
530 }
531
532
533 msg_error_t MsgHandle::countMessage(msg_folder_id_t FolderId, MSG_COUNT_INFO_S *pCountInfo)
534 {
535         /* Allocate Memory to Command Data */
536         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_folder_id_t);
537
538         char cmdBuf[cmdSize];
539         bzero(cmdBuf, cmdSize);
540         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
541
542         /* Set Command Parameters */
543         pCmd->cmdType = MSG_CMD_COUNT_MSG;
544
545         /* Copy Cookie */
546         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
547
548         /* Copy Command Data */
549         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &FolderId, sizeof(msg_folder_id_t));
550
551         /* Send Command to Messaging FW */
552         char* pEventData = NULL;
553         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
554
555         write((char*)pCmd, cmdSize, &pEventData);
556
557         /* Get Return Data */
558         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
559
560         if (pEvent == NULL)
561                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
562
563         if (pEvent->eventType != MSG_EVENT_COUNT_MSG) {
564                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
565         }
566
567         if (pEvent->result != MSG_SUCCESS)
568                 return pEvent->result;
569
570         /* Decode Return Data */
571         MsgDecodeCountInfo(pEvent->data, pCountInfo);
572
573         return MSG_SUCCESS;
574 }
575
576
577 msg_error_t MsgHandle::countMsgByType(const MSG_MESSAGE_TYPE_S *pMsgType, int *pMsgCount)
578 {
579         /* Allocate Memory to Command Data */
580         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_MESSAGE_TYPE_S);
581
582         char cmdBuf[cmdSize];
583         bzero(cmdBuf, cmdSize);
584         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
585
586         /* Set Command Parameters */
587         pCmd->cmdType = MSG_CMD_COUNT_BY_MSGTYPE;
588
589         /* Copy Cookie */
590         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
591
592         /* Copy Command Data */
593         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pMsgType, sizeof(MSG_MESSAGE_TYPE_S));
594
595         /* Send Command to Messaging FW */
596         char* pEventData = NULL;
597         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
598
599         write((char*)pCmd, cmdSize, &pEventData);
600
601         /* Get Return Data */
602         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
603
604         if (pEvent == NULL)
605                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
606
607         if (pEvent->eventType != MSG_EVENT_COUNT_BY_MSGTYPE) {
608                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
609         }
610
611         if (pEvent->result != MSG_SUCCESS)
612                 return pEvent->result;
613
614         /* Decode Return Data */
615         memcpy(pMsgCount, (void*)((char*)pEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
616
617         return MSG_SUCCESS;
618 }
619
620
621 msg_error_t MsgHandle::countMsgByContact(const MSG_THREAD_LIST_INDEX_INFO_S *pAddrInfo, MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
622 {
623         /* Allocate Memory to Command Data */
624         int cmdSize = sizeof(MSG_CMD_S) +  sizeof(MSG_THREAD_LIST_INDEX_S);
625
626         char cmdBuf[cmdSize];
627         bzero(cmdBuf, cmdSize);
628         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
629
630         /* Set Command Parameters */
631         pCmd->cmdType = MSG_CMD_GET_CONTACT_COUNT;
632
633         /* Copy Cookie */
634         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
635
636         /* Copy Command Data */
637         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pAddrInfo, sizeof(msg_contact_id_t));
638         msg_struct_s *pAddr = (msg_struct_s *)pAddrInfo->msgAddrInfo;
639
640         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN + sizeof(msg_contact_id_t)), pAddr->data, sizeof(MSG_ADDRESS_INFO_S));
641         /* Send Command to Messaging FW */
642         char* pEventData = NULL;
643         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
644
645         write((char*)pCmd, cmdSize, &pEventData);
646
647         /* Get Return Data */
648         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
649
650         if (pEvent == NULL)
651                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
652
653         if (pEvent->eventType != MSG_EVENT_GET_CONTACT_COUNT) {
654                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
655         }
656
657         if (pEvent->result != MSG_SUCCESS)
658                 return pEvent->result;
659
660         /* Decode Return Data */
661         MsgDecodeContactCount(pEvent->data, pMsgThreadCountList);
662
663         return MSG_SUCCESS;
664 }
665
666
667 msg_error_t MsgHandle::getMessage(msg_message_id_t MsgId, MSG_MESSAGE_HIDDEN_S *pMsg, MSG_SENDINGOPT_S *pSendOpt)
668 {
669         /* Allocate Memory to Command Data */
670         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
671
672         char cmdBuf[cmdSize];
673         bzero(cmdBuf, cmdSize);
674         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
675
676         /* Set Command Parameters */
677         pCmd->cmdType = MSG_CMD_GET_MSG;
678
679         /* Copy Cookie */
680         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
681
682         /* Copy Command Data */
683         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
684
685         /* Send Command to Messaging FW */
686         char* pEventData = NULL;
687         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
688
689
690         write((char*)pCmd, cmdSize, &pEventData);
691
692         /* Get Return Data */
693         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
694
695         if (pEvent == NULL)
696                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
697
698         if (pEvent->eventType != MSG_EVENT_GET_MSG) {
699                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
700         }
701
702         if (pEvent->result != MSG_SUCCESS)
703                 return pEvent->result;
704
705         /* Decode Return Data */
706         MSG_MESSAGE_INFO_S msgInfo;
707         MSG_SENDINGOPT_INFO_S sendOptInfo;
708
709         msgInfo.addressList = NULL;
710         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
711
712         MsgDecodeMsgInfo(pEvent->data, &msgInfo, &sendOptInfo);
713
714         /* Covert MSG_MESSAGE_INFO_S to MSG_MESSAGE_HIDDEN_S */
715         convertMsgStruct(&msgInfo, pMsg);
716
717         if (pSendOpt != NULL) {
718                 MSG_MESSAGE_TYPE_S msgType = {0, };
719
720                 msgType.mainType = pMsg->mainType;
721                 msgType.subType = pMsg->subType;
722                 msgType.classType = pMsg->classType;
723
724                 convertSendOptStruct(&sendOptInfo, pSendOpt, msgType);
725         }
726
727         /* Delete Temp File */
728         if (msgInfo.bTextSms == false) {
729                 /* Delete Temp File */
730                 MsgDeleteFile(msgInfo.msgData); /* ipc */
731         }
732
733         return MSG_SUCCESS;
734 }
735
736
737 msg_error_t MsgHandle::addFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
738 {
739         /* Allocate Memory to Command Data */
740         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_FOLDER_INFO_S);
741
742         char cmdBuf[cmdSize];
743         bzero(cmdBuf, cmdSize);
744         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
745
746         /* Set Command Parameters */
747         pCmd->cmdType = MSG_CMD_ADD_FOLDER;
748
749         /* Copy Cookie */
750         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
751
752         /* Copy Command Data */
753         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pFolderInfo, sizeof(MSG_FOLDER_INFO_S));
754
755         /* Send Command to Messaging FW */
756         char* pEventData = NULL;
757         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
758
759         write((char*)pCmd, cmdSize, &pEventData);
760
761         /* Get Return Data */
762         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
763
764         if (pEvent == NULL)
765                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
766
767         if (pEvent->eventType != MSG_EVENT_ADD_FOLDER) {
768                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
769         }
770
771         return pEvent->result;
772 }
773
774
775 msg_error_t MsgHandle::updateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
776 {
777         /* Allocate Memory to Command Data */
778         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_FOLDER_INFO_S);
779
780         char cmdBuf[cmdSize];
781         bzero(cmdBuf, cmdSize);
782         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
783
784         /* Set Command Parameters */
785         pCmd->cmdType = MSG_CMD_UPDATE_FOLDER;
786
787         /* Copy Cookie */
788         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
789
790         /* Copy Command Data */
791         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pFolderInfo, sizeof(MSG_FOLDER_INFO_S));
792
793         /* Send Command to Messaging FW */
794         char* pEventData = NULL;
795         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
796
797         write((char*)pCmd, cmdSize, &pEventData);
798
799         /* Get Return Data */
800         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
801
802         if (pEvent == NULL)
803                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
804
805         if (pEvent->eventType != MSG_EVENT_UPDATE_FOLDER) {
806                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
807         }
808
809         return pEvent->result;
810 }
811
812
813 msg_error_t MsgHandle::deleteFolder(msg_folder_id_t FolderId)
814 {
815         /* Allocate Memory to Command Data */
816         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_folder_id_t);
817
818         char cmdBuf[cmdSize];
819         bzero(cmdBuf, cmdSize);
820         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
821
822         /* Set Command Parameters */
823         pCmd->cmdType = MSG_CMD_DELETE_FOLDER;
824
825         /* Copy Cookie */
826         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
827
828         /* Copy Command Data */
829         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &FolderId, sizeof(msg_folder_id_t));
830
831         /* Send Command to Messaging FW */
832         char* pEventData = NULL;
833         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
834
835         write((char*)pCmd, cmdSize, &pEventData);
836
837         /* Get Return Data */
838         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
839
840         if (pEvent == NULL)
841                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
842
843         if (pEvent->eventType != MSG_EVENT_DELETE_FOLDER) {
844                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
845         }
846
847         return pEvent->result;
848 }
849
850
851 msg_error_t MsgHandle::getFolderList(msg_struct_list_s *pFolderList)
852 {
853         /* Allocate Memory to Command Data */
854         int cmdSize = sizeof(MSG_CMD_S);
855
856         char cmdBuf[cmdSize];
857         bzero(cmdBuf, cmdSize);
858         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
859
860         /* Set Command Parameters */
861         pCmd->cmdType = MSG_CMD_GET_FOLDERLIST;
862
863         /* Copy Cookie */
864         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
865
866         /* Send Command to Messaging FW */
867         char* pEventData = NULL;
868         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
869
870         write((char*)pCmd, cmdSize, &pEventData);
871
872         /* Get Return Data */
873         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
874
875         if (pEvent == NULL)
876                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
877
878         if (pEvent->eventType != MSG_EVENT_GET_FOLDERLIST) {
879                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
880         }
881
882         if (pEvent->result != MSG_SUCCESS)
883                 return pEvent->result;
884
885         /* Decode Return Data */
886         MsgDecodeFolderList(pEvent->data, pFolderList);
887
888         return MSG_SUCCESS;
889 }
890
891
892 msg_error_t MsgHandle::getThreadViewList(const MSG_SORT_RULE_S *pSortRule, const MSG_LIST_CONDITION_S *pListCond, msg_struct_list_s *pThreadViewList)
893 {
894         msg_error_t err = MSG_SUCCESS;
895
896         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
897                 MSG_ERR("Client doesn't have permission");
898                 return MSG_ERR_PERMISSION_DENIED;
899         }
900
901         err = MsgStoGetThreadViewListWithCondition(pSortRule, pListCond, pThreadViewList);
902         if (err != MSG_SUCCESS) {
903                 MSG_ERR("MsgStoGetThreadViewListWithCondition() Error!!");
904                 return err;
905         }
906
907 #if 0
908         /* Allocate Memory to Command Data */
909         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_SORT_RULE_S);
910
911         char cmdBuf[cmdSize];
912         bzero(cmdBuf, cmdSize);
913         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
914
915         /* Set Command Parameters */
916         pCmd->cmdType = MSG_CMD_GET_THREADVIEWLIST;
917
918         /* Copy Command Data */
919         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)), pSortRule, sizeof(MSG_SORT_RULE_S));
920
921         /* Send Command to Messaging FW */
922         char* pEventData = NULL;
923         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
924
925         write((char*)pCmd, cmdSize, &pEventData);
926
927         /* Get Return Data */
928         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
929
930         if (pEvent == NULL)
931                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
932
933         if (pEvent->eventType != MSG_EVENT_GET_THREADVIEWLIST) {
934                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
935         }
936
937         if (pEvent->result != MSG_SUCCESS)
938                 return pEvent->result;
939
940         /* Decode Return Data */
941         MsgDecodeThreadViewList(pEvent->data, pMsgThreadViewList);
942 #endif
943
944         return err;
945 }
946
947 msg_error_t MsgHandle::getConversationViewItem(msg_message_id_t MsgId, MSG_CONVERSATION_VIEW_S *pConv)
948 {
949         MSG_BEGIN();
950
951         msg_error_t err = MSG_SUCCESS;
952
953         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
954                 MSG_ERR("Client doesn't have permission");
955                 return MSG_ERR_PERMISSION_DENIED;
956         }
957
958         err = MsgStoGetConversationViewItem(MsgId, pConv);
959
960         return err;
961 }
962
963 msg_error_t MsgHandle::getConversationViewList(msg_thread_id_t ThreadId, msg_struct_list_s *pConvViewList)
964 {
965         MSG_BEGIN();
966
967         msg_error_t err = MSG_SUCCESS;
968
969         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
970                 MSG_ERR("Client doesn't have permission");
971                 return MSG_ERR_PERMISSION_DENIED;
972         }
973
974         err = MsgStoGetConversationViewList(ThreadId, pConvViewList);
975
976         MSG_END();
977
978         return err;
979 }
980
981
982 msg_error_t MsgHandle::deleteThreadMessageList(msg_thread_id_t ThreadId, bool include_protected_msg)
983 {
984         /* Allocate Memory to Command Data */
985         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t) + sizeof(bool);
986
987         char cmdBuf[cmdSize];
988         bzero(cmdBuf, cmdSize);
989         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
990
991         /* Set Command Parameters */
992         pCmd->cmdType = MSG_CMD_DELETE_THREADMESSAGELIST;
993
994         /* Copy Cookie */
995         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
996
997         /* Copy Command Data */
998         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &ThreadId, sizeof(msg_thread_id_t));
999         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_thread_id_t)), &include_protected_msg, sizeof(bool));
1000
1001         /* Send Command to Messaging FW */
1002         char* pEventData = NULL;
1003         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1004
1005         write((char*)pCmd, cmdSize, &pEventData);
1006
1007         /* Get Return Data */
1008         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1009
1010         if (pEvent == NULL)
1011                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1012
1013         if (pEvent->eventType != MSG_EVENT_DELETE_THREADMESSAGELIST) {
1014                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1015         }
1016
1017         if (pEvent->result != MSG_SUCCESS)
1018                 return pEvent->result;
1019
1020         return MSG_SUCCESS;
1021 }
1022
1023
1024 msg_error_t MsgHandle::getQuickPanelData(msg_quickpanel_type_t Type, MSG_MESSAGE_HIDDEN_S *pMsg)
1025 {
1026         /* Allocate Memory to Command Data */
1027         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_quickpanel_type_t);
1028
1029         char cmdBuf[cmdSize];
1030         bzero(cmdBuf, cmdSize);
1031         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1032
1033         /* Set Command Parameters */
1034         pCmd->cmdType = MSG_CMD_GET_QUICKPANEL_DATA;
1035
1036         /* Copy Cookie */
1037         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1038
1039         /* Copy Command Data */
1040         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &Type, sizeof(msg_quickpanel_type_t));
1041
1042         /* Send Command to Messaging FW */
1043         char* pEventData = NULL;
1044         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1045
1046         write((char*)pCmd, cmdSize, &pEventData);
1047
1048         /* Get Return Data */
1049         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1050
1051         if (pEvent == NULL)
1052                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1053
1054         if (pEvent->eventType != MSG_EVENT_GET_QUICKPANEL_DATA) {
1055                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1056         }
1057
1058         if (pEvent->result != MSG_SUCCESS)
1059                 return pEvent->result;
1060
1061         /* Decode Return Data */
1062         MSG_MESSAGE_INFO_S msgInfo;
1063
1064         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1065
1066         msgInfo.addressList = NULL;
1067         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1068
1069         MsgDecodeMsgInfo((char *)pEvent->data, &msgInfo);
1070
1071         /* Covert MSG_MESSAGE_INFO_S to MSG_MESSAGE_S */
1072         convertMsgStruct(&msgInfo, pMsg);
1073
1074         /* Delete Temp File */
1075         if (msgInfo.bTextSms == false) {
1076                 /* Delete Temp File */
1077                 MsgDeleteFile(msgInfo.msgData); /* ipc */
1078         }
1079
1080         return MSG_SUCCESS;
1081 }
1082
1083
1084 msg_error_t MsgHandle::resetDatabase()
1085 {
1086         /* Allocate Memory to Command Data */
1087         int cmdSize = sizeof(MSG_CMD_S);
1088
1089         char cmdBuf[cmdSize];
1090         bzero(cmdBuf, cmdSize);
1091         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1092
1093         /* Set Command Parameters */
1094         pCmd->cmdType = MSG_CMD_RESET_DB;
1095
1096         /* Copy Cookie */
1097         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1098
1099         /* Send Command to Messaging FW */
1100         char* pEventData = NULL;
1101         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1102
1103         write((char*)pCmd, cmdSize, &pEventData);
1104
1105         /* Get Return Data */
1106         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1107
1108         if (pEvent == NULL)
1109                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1110
1111         if (pEvent->eventType != MSG_EVENT_RESET_DB) {
1112                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1113         }
1114
1115         return pEvent->result;
1116 }
1117
1118
1119 msg_error_t MsgHandle::getMemSize(unsigned int* memsize)
1120 {
1121         /* Allocate Memory to Command Data */
1122         int cmdSize = sizeof(MSG_CMD_S);
1123
1124         char cmdBuf[cmdSize];
1125         bzero(cmdBuf, cmdSize);
1126         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1127
1128         /* Set Command Parameters */
1129         pCmd->cmdType = MSG_CMD_GET_MEMSIZE;
1130
1131         /* Copy Cookie */
1132         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1133
1134         /* Send Command to Messaging FW */
1135         char* pEventData = NULL;
1136         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1137
1138         write((char*)pCmd, cmdSize, &pEventData);
1139
1140         /* Get Return Data */
1141         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1142
1143         if (pEvent == NULL)
1144                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1145
1146         if (pEvent->eventType != MSG_EVENT_GET_MEMSIZE) {
1147                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1148         }
1149
1150         if (pEvent->result != MSG_SUCCESS)
1151                 return pEvent->result;
1152
1153         /* Decode Return Data */
1154         MsgDecodeMemSize(pEvent->data, memsize);
1155
1156         return MSG_SUCCESS;
1157 }
1158
1159
1160 msg_error_t MsgHandle::backupMessage(msg_message_backup_type_t type, const char *backup_filepath)
1161 {
1162         if (backup_filepath == NULL)
1163                 return MSG_ERR_NULL_POINTER;
1164
1165         /* Create an empty file for writing. */
1166         /* If a file with the same name already exists its content is erased */
1167         /* and the file is treated as a new empty file. */
1168
1169         FILE *pFile = MsgOpenFile(backup_filepath, "w");
1170         if (pFile == NULL) {
1171                 MSG_DEBUG("File Open error");
1172                 return MSG_ERR_STORAGE_ERROR;
1173         }
1174         MsgCloseFile(pFile);
1175
1176         char path[MSG_FILEPATH_LEN_MAX+1] = {0, };
1177         strncpy(path, backup_filepath, MSG_FILEPATH_LEN_MAX);
1178
1179         /* Allocate Memory to Command Data */
1180         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_backup_type_t) + sizeof(path);
1181
1182         char cmdBuf[cmdSize];
1183         bzero(cmdBuf, cmdSize);
1184         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1185
1186         /* Set Command Parameters */
1187         pCmd->cmdType = MSG_CMD_BACKUP_MESSAGE;
1188
1189         /* Copy Cookie */
1190         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1191         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &type, sizeof(msg_message_backup_type_t));
1192         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_backup_type_t)), (char *)path, sizeof(path));
1193
1194         /* Send Command to Messaging FW */
1195         char* pEventData = NULL;
1196         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1197
1198         write((char*)pCmd, cmdSize, &pEventData);
1199
1200         /* Get Return Data */
1201         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1202
1203         if (pEvent == NULL)
1204                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1205
1206         if (pEvent->eventType != MSG_EVENT_BACKUP_MESSAGE) {
1207                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1208         }
1209
1210         return pEvent->result;
1211 }
1212
1213
1214 msg_error_t MsgHandle::restoreMessage(const char *backup_filepath)
1215 {
1216         if (backup_filepath == NULL)
1217                 return MSG_ERR_NULL_POINTER;
1218
1219         if (MsgAccessFile(backup_filepath, R_OK) == false) {
1220                 MSG_DEBUG("File access error");
1221                 return MSG_ERR_UNKNOWN;
1222         }
1223
1224         char path[MSG_FILEPATH_LEN_MAX+1] = {0, };
1225         strncpy(path, backup_filepath, MSG_FILEPATH_LEN_MAX);
1226
1227         /* Allocate Memory to Command Data */
1228         int cmdSize = sizeof(MSG_CMD_S) + sizeof(path);
1229
1230         char cmdBuf[cmdSize];
1231         bzero(cmdBuf, cmdSize);
1232         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1233
1234         /* Set Command Parameters */
1235         pCmd->cmdType = MSG_CMD_RESTORE_MESSAGE;
1236
1237         /* Copy Cookie */
1238         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1239         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), (char *)path, sizeof(path));
1240
1241         /* Send Command to Messaging FW */
1242         char* pEventData = NULL;
1243         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1244
1245         write((char*)pCmd, cmdSize, &pEventData);
1246
1247         /* Get Return Data */
1248         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1249
1250         if (pEvent == NULL)
1251                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1252
1253         if (pEvent->eventType != MSG_EVENT_RESTORE_MESSAGE) {
1254                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1255         }
1256
1257         return pEvent->result;
1258 }
1259
1260
1261 msg_error_t MsgHandle::searchMessage(const char *pSearchString, msg_struct_list_s *pThreadViewList)
1262 {
1263         msg_error_t err = MSG_SUCCESS;
1264
1265         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1266                 MSG_ERR("Client doesn't have permission");
1267                 return MSG_ERR_PERMISSION_DENIED;
1268         }
1269
1270         int count = 0;
1271
1272         MSG_ADDRESS_INFO_S *pAddrInfo = NULL;
1273         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> buf(&pAddrInfo, unique_ptr_deleter);
1274
1275         /* get contact search list */
1276         if (MsgGetContactSearchList(pSearchString, &pAddrInfo, &count) != MSG_SUCCESS) {
1277                 MSG_DEBUG("MsgGetContactSearchList fail.");
1278                 count = 0;
1279         }
1280
1281         if (count > 0) {
1282                 /* Allocate Memory to Command Data */
1283                 int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + sizeof(MSG_ADDRESS_INFO_S) * count;
1284
1285                 char cmdBuf[cmdSize];
1286                 bzero(cmdBuf, cmdSize);
1287                 MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1288
1289                 /* Set Command Parameters */
1290                 pCmd->cmdType = MSG_CMD_SET_TEMP_ADDRESS_TABLE;
1291
1292                 /* Copy Cookie */
1293                 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1294
1295                 /* Copy Command Data */
1296                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &count, sizeof(int));
1297                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), pAddrInfo, sizeof(MSG_ADDRESS_INFO_S) * count);
1298
1299                 /* Send Command to Messaging FW */
1300                 char* pEventData = NULL;
1301                 unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1302
1303                 write((char*)pCmd, cmdSize, &pEventData);
1304
1305                 /* Get Return Data */
1306                 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1307
1308                 if (pEvent == NULL)
1309                         THROW(MsgException::INVALID_RESULT, "Event is NULL");
1310
1311                 if (pEvent->eventType != MSG_EVENT_SET_TEMP_ADDRESS_TABLE)
1312                         THROW(MsgException::INVALID_RESULT, "Event Data Error");
1313
1314                 if (pEvent->result != MSG_SUCCESS)
1315                         count = 0;
1316         }
1317
1318         err = MsgStoSearchMessage(pSearchString, pThreadViewList, count);
1319
1320         if (err != MSG_SUCCESS) {
1321                 MSG_DEBUG("MsgStoSearchMessage() Error!!");
1322                 return err;
1323         }
1324
1325 /*      MsgStoDisconnectDB(); */
1326
1327         return err;
1328 }
1329
1330
1331 msg_error_t MsgHandle::getRejectMsgList(const char *pNumber, msg_struct_list_s *pRejectMsgList)
1332 {
1333         msg_error_t err = MSG_SUCCESS;
1334
1335         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1336                 MSG_ERR("Client doesn't have permission");
1337                 return MSG_ERR_PERMISSION_DENIED;
1338         }
1339
1340         err = MsgStoGetRejectMsgList(pNumber, pRejectMsgList);
1341
1342         if (err != MSG_SUCCESS) {
1343                 MSG_DEBUG("MsgStoGetRejectMsgList() Error!!");
1344                 return err;
1345         }
1346
1347         return err;
1348 }
1349
1350
1351 msg_error_t MsgHandle::regStorageChangeCallback(msg_storage_change_cb onStorageChange, void *pUserParam)
1352 {
1353         if (!onStorageChange)
1354                 THROW(MsgException::INVALID_PARAM, "onStorageChange is null");
1355
1356         MsgProxyListener* eventListener = MsgProxyListener::instance();
1357
1358         eventListener->start(this);
1359
1360         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
1361
1362         if (remoteFd == -1 ) {
1363                 eventListener->stop();
1364                 return MSG_ERR_INVALID_MSGHANDLE;
1365         }
1366
1367         if (eventListener->regStorageChangeEventCB(this, remoteFd, onStorageChange, pUserParam) == false) {
1368                 eventListener->stop();
1369                 return MSG_ERR_INVALID_PARAMETER;
1370         }
1371
1372         /* Allocate Memory to Command Data */
1373         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); /* cmd type, listenerFd */
1374         char cmdBuf[cmdSize];
1375         bzero(cmdBuf, cmdSize);
1376         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
1377
1378         /* Set Command Parameters */
1379         pCmd->cmdType = MSG_CMD_REG_STORAGE_CHANGE_CB;
1380
1381         /* Copy Cookie */
1382         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1383
1384         MSG_DEBUG("remote fd %d", remoteFd);
1385
1386         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &remoteFd, sizeof(remoteFd));
1387
1388         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), remoteFd);
1389
1390         /* Send Command to Messaging FW */
1391         char* pEventData = NULL;
1392         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1393
1394         write((char*)pCmd, cmdSize, &pEventData);
1395
1396         /* Get Return Data */
1397         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1398
1399         if (pEvent == NULL)
1400                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1401
1402         if (pEvent->eventType != MSG_EVENT_REG_STORAGE_CHANGE_CB) {
1403                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
1404         }
1405
1406         return pEvent->result;
1407 }
1408
1409
1410 msg_error_t MsgHandle::regThreadChangeCallback(msg_thread_change_cb onThreadChange, void *pUserParam)
1411 {
1412         if (!onThreadChange)
1413                 THROW(MsgException::INVALID_PARAM, "onThreadChange is null");
1414
1415         MsgProxyListener* eventListener = MsgProxyListener::instance();
1416
1417         eventListener->start(this);
1418
1419         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
1420
1421         if (remoteFd == -1 ) {
1422                 eventListener->stop();
1423                 return MSG_ERR_INVALID_MSGHANDLE;
1424         }
1425
1426         if (eventListener->regThreadChangeEventCB(this, remoteFd, onThreadChange, pUserParam) == false) {
1427                 eventListener->stop();
1428                 return MSG_ERR_INVALID_PARAMETER;
1429         }
1430
1431         /* Allocate Memory to Command Data */
1432         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); /* cmd type, listenerFd */
1433         char cmdBuf[cmdSize];
1434         bzero(cmdBuf, cmdSize);
1435         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
1436
1437         /* Set Command Parameters */
1438         pCmd->cmdType = MSG_CMD_REG_THREAD_CHANGE_CB;
1439
1440         /* Copy Cookie */
1441         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1442
1443         MSG_DEBUG("remote fd %d", remoteFd);
1444
1445         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &remoteFd, sizeof(remoteFd));
1446
1447         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), remoteFd);
1448
1449         /* Send Command to Messaging FW */
1450         char* pEventData = NULL;
1451         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1452
1453         write((char*)pCmd, cmdSize, &pEventData);
1454
1455         /* Get Return Data */
1456         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1457
1458         if (pEvent == NULL)
1459                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1460
1461         if (pEvent->eventType != MSG_EVENT_REG_THREAD_CHANGE_CB) {
1462                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
1463         }
1464
1465         return pEvent->result;
1466 }
1467
1468
1469 msg_error_t MsgHandle::getReportStatus(msg_message_id_t msg_id, msg_struct_list_s *report_list)
1470 {
1471         /* Allocate Memory to Command Data */
1472         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1473
1474         char cmdBuf[cmdSize];
1475         bzero(cmdBuf, cmdSize);
1476         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1477
1478         report_list->nCount = 0;
1479         report_list->msg_struct_info = NULL;
1480
1481         /* Set Command Parameters */
1482         pCmd->cmdType = MSG_CMD_GET_REPORT_STATUS;
1483
1484         /* Copy Cookie */
1485         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1486
1487         /* Copy Command Data */
1488         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msg_id, sizeof(msg_message_id_t));
1489
1490         /* Send Command to Messaging FW */
1491         char* pEventData = NULL;
1492         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1493
1494         write((char*)pCmd, cmdSize, &pEventData);
1495
1496         /* Get Return Data */
1497         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1498
1499         if (pEvent == NULL)
1500                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1501
1502         if (pEvent->eventType != MSG_EVENT_GET_REPORT_STATUS) {
1503                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1504         }
1505
1506         if (pEvent->result != MSG_SUCCESS)
1507                 return pEvent->result;
1508
1509         /* Decode Return Data */
1510         MsgDecodeReportStatus(pEvent->data, report_list);
1511
1512         return MSG_SUCCESS;
1513 }
1514
1515
1516 msg_error_t MsgHandle::getAddressList(const msg_thread_id_t threadId, msg_struct_list_s *pAddrList)
1517 {
1518         msg_error_t err = MSG_SUCCESS;
1519
1520         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1521                 MSG_ERR("Client doesn't have permission");
1522                 return MSG_ERR_PERMISSION_DENIED;
1523         }
1524
1525         err = MsgStoGetAddressList(threadId, (msg_struct_list_s *)pAddrList);
1526
1527         if (err != MSG_SUCCESS) {
1528                 MSG_DEBUG("MsgStoGetThreadViewList() Error!!");
1529                 return err;
1530         }
1531
1532         return err;
1533 }
1534
1535
1536 msg_error_t MsgHandle::getThreadIdByAddress(msg_struct_list_s *pAddrList, msg_thread_id_t *pThreadId)
1537 {
1538         /* Allocate Memory to Command Data */
1539         int cmdSize = sizeof(MSG_CMD_S) + sizeof(pAddrList->nCount) + (sizeof(MSG_ADDRESS_INFO_S)*pAddrList->nCount);
1540
1541         char cmdBuf[cmdSize];
1542         bzero(cmdBuf, cmdSize);
1543         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1544
1545         /* Set Command Parameters */
1546         pCmd->cmdType = MSG_CMD_GET_THREAD_ID_BY_ADDRESS;
1547
1548         /* Copy Cookie */
1549         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1550
1551         /* Copy Command Data */
1552         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &pAddrList->nCount, sizeof(pAddrList->nCount));
1553         int addSize = sizeof(MSG_ADDRESS_INFO_S);
1554         for (int i = 0; i < pAddrList->nCount; i++) {
1555                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+sizeof(pAddrList->nCount)+(addSize*i)+MAX_COOKIE_LEN), ((msg_struct_s *)(pAddrList->msg_struct_info[i]))->data, sizeof(MSG_ADDRESS_INFO_S));
1556         }
1557
1558         /* Send Command to Messaging FW */
1559         char* pEventData = NULL;
1560         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1561
1562         write((char*)pCmd, cmdSize, &pEventData);
1563
1564         /* Get Return Data */
1565         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1566
1567         if (pEvent == NULL)
1568                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1569
1570         if (pEvent->eventType != MSG_EVENT_GET_THREAD_ID_BY_ADDRESS) {
1571                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1572         }
1573
1574         if (pEvent->result != MSG_SUCCESS)
1575                 return pEvent->result;
1576
1577         /* Decode Return Data */
1578         MsgDecodeThreadId(pEvent->data, pThreadId);
1579
1580         return MSG_SUCCESS;
1581 }
1582
1583
1584 msg_error_t MsgHandle::getThreadIdByAddress(msg_list_handle_t msg_address_list, msg_thread_id_t *pThreadId)
1585 {
1586         int addrCnt = (int)g_list_length((GList *)msg_address_list);
1587
1588         /* Allocate Memory to Command Data */
1589         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*addrCnt);
1590
1591         char cmdBuf[cmdSize];
1592         bzero(cmdBuf, cmdSize);
1593         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1594
1595         /* Set Command Parameters */
1596         pCmd->cmdType = MSG_CMD_GET_THREAD_ID_BY_ADDRESS;
1597
1598         /* Copy Cookie */
1599         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1600
1601         /* Copy Command Data */
1602         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &addrCnt, sizeof(int));
1603         int addSize = sizeof(MSG_ADDRESS_INFO_S);
1604         for (int i = 0; i < addrCnt; i++) {
1605                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+sizeof(addrCnt)+(addSize*i)+MAX_COOKIE_LEN), ((msg_struct_s *)(g_list_nth_data((GList *)msg_address_list, (guint)i)))->data, sizeof(MSG_ADDRESS_INFO_S));
1606         }
1607
1608         /* Send Command to Messaging FW */
1609         char* pEventData = NULL;
1610         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1611
1612         write((char*)pCmd, cmdSize, &pEventData);
1613
1614         /* Get Return Data */
1615         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1616
1617         if (pEvent == NULL)
1618                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1619
1620         if (pEvent->eventType != MSG_EVENT_GET_THREAD_ID_BY_ADDRESS) {
1621                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1622         }
1623
1624         if (pEvent->result != MSG_SUCCESS)
1625                 return pEvent->result;
1626
1627         /* Decode Return Data */
1628         MsgDecodeThreadId(pEvent->data, pThreadId);
1629
1630         return MSG_SUCCESS;
1631 }
1632
1633
1634 msg_error_t MsgHandle::getThread(msg_thread_id_t threadId, MSG_THREAD_VIEW_S* pThreadInfo)
1635 {
1636         /* Allocate Memory to Command Data */
1637         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t);
1638
1639         char cmdBuf[cmdSize];
1640         bzero(cmdBuf, cmdSize);
1641         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1642
1643         /* Set Command Parameters */
1644         pCmd->cmdType = MSG_CMD_GET_THREAD_INFO;
1645
1646         /* Copy Cookie */
1647         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1648
1649         /* Copy Command Data */
1650         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &threadId, sizeof(msg_thread_id_t));
1651
1652         /* Send Command to Messaging FW */
1653         char* pEventData = NULL;
1654         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1655
1656         write((char*)pCmd, cmdSize, &pEventData);
1657
1658         /* Get Return Data */
1659         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1660
1661         if (pEvent == NULL)
1662                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1663
1664         if (pEvent->eventType != MSG_EVENT_GET_THREAD_INFO) {
1665                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1666         }
1667
1668         /* Decode Return Data */
1669         MsgDecodeThreadInfo(pEvent->data, pThreadInfo);
1670
1671         return MSG_SUCCESS;
1672 }
1673
1674
1675 msg_error_t MsgHandle::getMessageList(const MSG_LIST_CONDITION_S *pListCond, msg_struct_list_s *pMsgList)
1676 {
1677         msg_error_t err = MSG_SUCCESS;
1678
1679         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1680                 MSG_ERR("Client doesn't have permission");
1681                 return MSG_ERR_PERMISSION_DENIED;
1682         }
1683
1684         int count = 0;
1685
1686         if (pListCond->pAddressVal) {
1687                 MSG_ADDRESS_INFO_S *pAddrInfo = NULL;
1688                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> buf(&pAddrInfo, unique_ptr_deleter);
1689
1690                 /* get contact search list */
1691                 if (MsgGetContactSearchList(pListCond->pAddressVal, &pAddrInfo, &count) != MSG_SUCCESS) {
1692                         MSG_DEBUG("MsgGetContactSearchList fail.");
1693                         count = 0;
1694                 }
1695
1696                 if (count > 0) {
1697                         /* Allocate Memory to Command Data */
1698                         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + sizeof(MSG_ADDRESS_INFO_S) * count;
1699
1700                         char cmdBuf[cmdSize];
1701                         bzero(cmdBuf, cmdSize);
1702                         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1703
1704                         /* Set Command Parameters */
1705                         pCmd->cmdType = MSG_CMD_SET_TEMP_ADDRESS_TABLE;
1706
1707                         /* Copy Cookie */
1708                         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1709
1710                         /* Copy Command Data */
1711                         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &count, sizeof(int));
1712                         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), pAddrInfo, sizeof(MSG_ADDRESS_INFO_S) * count);
1713
1714                         /* Send Command to Messaging FW */
1715                         char* pEventData = NULL;
1716                         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1717
1718                         write((char*)pCmd, cmdSize, &pEventData);
1719
1720                         /* Get Return Data */
1721                         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1722
1723                         if (pEvent == NULL)
1724                                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1725
1726                         if (pEvent->eventType != MSG_EVENT_SET_TEMP_ADDRESS_TABLE)
1727                                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1728
1729                         if (pEvent->result != MSG_SUCCESS)
1730                                 count = 0;
1731                 }
1732         }
1733
1734         err = MsgStoGetMessageList(pListCond, pMsgList, count);
1735
1736         if (err != MSG_SUCCESS) {
1737                 MSG_DEBUG("MsgStoGetMessageList() Error!!");
1738                 return err;
1739         }
1740
1741         return err;
1742 }
1743
1744
1745 msg_error_t MsgHandle::getMediaList(const msg_thread_id_t thread_id, msg_list_handle_t *pMediaList)
1746 {
1747         msg_error_t err = MSG_SUCCESS;
1748
1749         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1750                 MSG_ERR("Client doesn't have permission");
1751                 return MSG_ERR_PERMISSION_DENIED;
1752         }
1753
1754         err = MsgStoGetMediaList(thread_id, pMediaList);
1755
1756         if (err != MSG_SUCCESS) {
1757                 MSG_DEBUG("MsgStoGetFmMediaList() Error!!");
1758                 return err;
1759         }
1760
1761         return err;
1762 }
1763
1764
1765 int MsgHandle::addPushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1766 {
1767         /* Allocate Memory to Command Data */
1768         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1769
1770         char cmdBuf[cmdSize];
1771         bzero(cmdBuf, cmdSize);
1772         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1773
1774         /* Set Command Parameters */
1775         pCmd->cmdType = MSG_CMD_ADD_PUSH_EVENT;
1776
1777         /* Copy Cookie */
1778         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1779
1780         /* Copy Command Data */
1781         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1782
1783         /* Send Command to Messaging FW */
1784         char* pEventData = NULL;
1785         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1786
1787         write((char*)pCmd, cmdSize, &pEventData);
1788
1789         /* Get Return Data */
1790         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1791
1792         if (pEvent == NULL)
1793                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1794
1795         if (pEvent->eventType != MSG_EVENT_ADD_PUSH_EVENT) {
1796                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1797         }
1798
1799         return pEvent->result;
1800 }
1801
1802
1803 int MsgHandle::deletePushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1804 {
1805         /* Allocate Memory to Command Data */
1806         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1807
1808         char cmdBuf[cmdSize];
1809         bzero(cmdBuf, cmdSize);
1810         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1811
1812         /* Set Command Parameters */
1813         pCmd->cmdType = MSG_CMD_DELETE_PUSH_EVENT;
1814
1815         /* Copy Cookie */
1816         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1817
1818         /* Copy Command Data */
1819         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1820
1821         /* Send Command to Messaging FW */
1822         char* pEventData = NULL;
1823         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1824
1825         write((char*)pCmd, cmdSize, &pEventData);
1826
1827         /* Get Return Data */
1828         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1829
1830         if (pEvent == NULL)
1831                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1832
1833         if (pEvent->eventType != MSG_EVENT_DELETE_PUSH_EVENT) {
1834                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1835         }
1836
1837         return pEvent->result;
1838 }
1839
1840
1841 int MsgHandle::updatePushEvent(MSG_PUSH_EVENT_INFO_S *pSrc, MSG_PUSH_EVENT_INFO_S *pDst)
1842 {
1843         /* Allocate Memory to Command Data */
1844         int cmdSize = sizeof(MSG_CMD_S) +  2 * sizeof(MSG_PUSH_EVENT_INFO_S);
1845
1846         char cmdBuf[cmdSize];
1847         bzero(cmdBuf, cmdSize);
1848         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1849
1850         /* Set Command Parameters */
1851         pCmd->cmdType = MSG_CMD_UPDATE_PUSH_EVENT;
1852
1853         /* Copy Cookie */
1854         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1855
1856         /* Copy Command Data */
1857         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pSrc, sizeof(MSG_PUSH_EVENT_INFO_S));
1858         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_PUSH_EVENT_INFO_S)), pDst, sizeof(MSG_PUSH_EVENT_INFO_S));
1859
1860         /* Send Command to Messaging FW */
1861         char* pEventData = NULL;
1862         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1863
1864         write((char*)pCmd, cmdSize, &pEventData);
1865
1866         /* Get Return Data */
1867         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1868
1869         if (pEvent == NULL)
1870                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1871
1872         if (pEvent->eventType != MSG_EVENT_UPDATE_PUSH_EVENT) {
1873                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1874         }
1875
1876         return pEvent->result;
1877 }
1878
1879
1880 msg_error_t MsgHandle::getVobject(msg_message_id_t MsgId, void** encodedData)
1881 {
1882         /* Allocate Memory to Command Data */
1883         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1884         char *encode_data = NULL;
1885         char cmdBuf[cmdSize];
1886         bzero(cmdBuf, cmdSize);
1887         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1888
1889         /* Set Command Parameters */
1890         pCmd->cmdType = MSG_CMD_GET_MSG;
1891
1892         /* Copy Cookie */
1893         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1894
1895         /* Copy Command Data */
1896         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
1897
1898         /* Send Command to Messaging FW */
1899         char* pEventData = NULL;
1900         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1901
1902
1903         write((char*)pCmd, cmdSize, &pEventData);
1904
1905         /* Get Return Data */
1906         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1907
1908         if (pEvent == NULL)
1909                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1910
1911         if (pEvent->eventType != MSG_EVENT_GET_MSG) {
1912                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1913         }
1914
1915         if (pEvent->result != MSG_SUCCESS)
1916                 return pEvent->result;
1917
1918         /* Decode Return Data */
1919         MSG_MESSAGE_INFO_S msgInfo = {0, };
1920         MSG_SENDINGOPT_INFO_S sendOptInfo = {0, };
1921
1922         msgInfo.addressList = NULL;
1923         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1924
1925         MsgDecodeMsgInfo(pEvent->data, &msgInfo, &sendOptInfo);
1926
1927         /*Convert MSG_MESSAGE_INFO_S to */
1928         encode_data = MsgVMessageEncode(&msgInfo);
1929         if (encode_data) {
1930                 *encodedData = (void*)encode_data;
1931         } else {
1932                 MSG_DEBUG("Error Encode data");
1933                 *encodedData = NULL;
1934         }
1935
1936         /* Delete Temp File */
1937         if (msgInfo.bTextSms == false) {
1938                 /* Delete Temp File */
1939                 MsgDeleteFile(msgInfo.msgData); /* ipc */
1940         }
1941
1942         return MSG_SUCCESS;
1943 }
1944
1945
1946 msg_error_t MsgHandle::dbSelectWithQuery(const char *query, char ***db_res, int *row_count, int *col_count)
1947 {
1948         msg_error_t err = MSG_SUCCESS;
1949
1950         if (checkPermission() == MSG_ERR_PERMISSION_DENIED) {
1951                 MSG_ERR("Client doesn't have permission");
1952                 return MSG_ERR_PERMISSION_DENIED;
1953         }
1954
1955         err = MsgStoDbSelectWithQuery(query, db_res, row_count, col_count);
1956
1957         if (err != MSG_SUCCESS)
1958                 return err;
1959
1960         MSG_END();
1961
1962         return err;
1963 }
1964
1965
1966 void MsgHandle::dbFree(char **db_res)
1967 {
1968         MsgStoDbFree(db_res);
1969         MSG_END();
1970 }
1971
1972
1973 msg_error_t MsgHandle::allowTcsMessage(msg_message_id_t MsgId)
1974 {
1975         MSG_BEGIN();
1976         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1977
1978         char cmdBuf[cmdSize];
1979         bzero(cmdBuf, cmdSize);
1980         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1981
1982         /* Set Command Parameters */
1983         pCmd->cmdType = MSG_CMD_ALLOW_TCS_MESSAGE;
1984
1985         /* Copy Cookie */
1986         memcpy((void*)pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1987
1988         /* Copy Command Data */
1989         memcpy((void*)((char*)pCmd + sizeof(MSG_CMD_TYPE_T) + MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
1990
1991         /* Send Command to Messaging FW */
1992         char* pEventData = NULL;
1993         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1994
1995         write((char*)pCmd, cmdSize, &pEventData);
1996
1997         /* Get Return Data */
1998         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1999
2000         if (pEvent == NULL) {
2001                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
2002         }
2003
2004         if (pEvent->eventType != MSG_EVENT_ALLOW_TCS_MESSAGE) {
2005                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
2006         }
2007
2008         return pEvent->result;
2009 }