modify code to get contact info on proxy side for search_msg and get_message_list
[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, msg_struct_list_s *pThreadViewList)
893 {
894         msg_error_t err = MSG_SUCCESS;
895         /*
896         err = MsgStoConnectDB();
897
898         if (err != MSG_SUCCESS) {
899                 MSG_DEBUG("MsgStoConnectDB() Error!!");
900                 return err;
901         }
902         */
903
904         err = MsgStoGetThreadViewList(pSortRule, pThreadViewList);
905
906         if (err != MSG_SUCCESS) {
907                 MSG_DEBUG("MsgStoGetThreadViewList() Error!!");
908                 return err;
909         }
910
911         /* MsgStoDisconnectDB(); */
912
913 #if 0
914         /* Allocate Memory to Command Data */
915         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_SORT_RULE_S);
916
917         char cmdBuf[cmdSize];
918         bzero(cmdBuf, cmdSize);
919         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
920
921         /* Set Command Parameters */
922         pCmd->cmdType = MSG_CMD_GET_THREADVIEWLIST;
923
924         /* Copy Command Data */
925         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)), pSortRule, sizeof(MSG_SORT_RULE_S));
926
927         /* Send Command to Messaging FW */
928         char* pEventData = NULL;
929         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
930
931         write((char*)pCmd, cmdSize, &pEventData);
932
933         /* Get Return Data */
934         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
935
936         if (pEvent == NULL)
937                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
938
939         if (pEvent->eventType != MSG_EVENT_GET_THREADVIEWLIST) {
940                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
941         }
942
943         if (pEvent->result != MSG_SUCCESS)
944                 return pEvent->result;
945
946         /* Decode Return Data */
947         MsgDecodeThreadViewList(pEvent->data, pMsgThreadViewList);
948 #endif
949
950         return err;
951 }
952
953 msg_error_t MsgHandle::getConversationViewItem(msg_message_id_t MsgId, MSG_CONVERSATION_VIEW_S *pConv)
954 {
955         MSG_BEGIN();
956
957         msg_error_t err = MSG_SUCCESS;
958
959         /* MsgStoConnectDB(); */
960         err = MsgStoGetConversationViewItem(MsgId, pConv);
961         /* MsgStoDisconnectDB(); */
962
963         return err;
964 }
965
966 msg_error_t MsgHandle::getConversationViewList(msg_thread_id_t ThreadId, msg_struct_list_s *pConvViewList)
967 {
968         MSG_BEGIN();
969
970         msg_error_t err = MSG_SUCCESS;
971
972         /* MsgStoConnectDB(); */
973         err = MsgStoGetConversationViewList(ThreadId, pConvViewList);
974         /* MsgStoDisconnectDB(); */
975
976         if (err != MSG_SUCCESS)
977                 return err;
978
979         MSG_END();
980
981         return err;
982 }
983
984
985 msg_error_t MsgHandle::deleteThreadMessageList(msg_thread_id_t ThreadId, bool include_protected_msg)
986 {
987         /* Allocate Memory to Command Data */
988         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t) + sizeof(bool);
989
990         char cmdBuf[cmdSize];
991         bzero(cmdBuf, cmdSize);
992         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
993
994         /* Set Command Parameters */
995         pCmd->cmdType = MSG_CMD_DELETE_THREADMESSAGELIST;
996
997         /* Copy Cookie */
998         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
999
1000         /* Copy Command Data */
1001         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &ThreadId, sizeof(msg_thread_id_t));
1002         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_thread_id_t)), &include_protected_msg, sizeof(bool));
1003
1004         /* Send Command to Messaging FW */
1005         char* pEventData = NULL;
1006         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1007
1008         write((char*)pCmd, cmdSize, &pEventData);
1009
1010         /* Get Return Data */
1011         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1012
1013         if (pEvent == NULL)
1014                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1015
1016         if (pEvent->eventType != MSG_EVENT_DELETE_THREADMESSAGELIST) {
1017                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1018         }
1019
1020         if (pEvent->result != MSG_SUCCESS)
1021                 return pEvent->result;
1022
1023         return MSG_SUCCESS;
1024 }
1025
1026
1027 msg_error_t MsgHandle::getQuickPanelData(msg_quickpanel_type_t Type, MSG_MESSAGE_HIDDEN_S *pMsg)
1028 {
1029         /* Allocate Memory to Command Data */
1030         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_quickpanel_type_t);
1031
1032         char cmdBuf[cmdSize];
1033         bzero(cmdBuf, cmdSize);
1034         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1035
1036         /* Set Command Parameters */
1037         pCmd->cmdType = MSG_CMD_GET_QUICKPANEL_DATA;
1038
1039         /* Copy Cookie */
1040         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1041
1042         /* Copy Command Data */
1043         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &Type, sizeof(msg_quickpanel_type_t));
1044
1045         /* Send Command to Messaging FW */
1046         char* pEventData = NULL;
1047         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1048
1049         write((char*)pCmd, cmdSize, &pEventData);
1050
1051         /* Get Return Data */
1052         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1053
1054         if (pEvent == NULL)
1055                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1056
1057         if (pEvent->eventType != MSG_EVENT_GET_QUICKPANEL_DATA) {
1058                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1059         }
1060
1061         if (pEvent->result != MSG_SUCCESS)
1062                 return pEvent->result;
1063
1064         /* Decode Return Data */
1065         MSG_MESSAGE_INFO_S msgInfo;
1066
1067         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1068
1069         msgInfo.addressList = NULL;
1070         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1071
1072         MsgDecodeMsgInfo((char *)pEvent->data, &msgInfo);
1073
1074         /* Covert MSG_MESSAGE_INFO_S to MSG_MESSAGE_S */
1075         convertMsgStruct(&msgInfo, pMsg);
1076
1077         /* Delete Temp File */
1078         if (msgInfo.bTextSms == false) {
1079                 /* Delete Temp File */
1080                 MsgDeleteFile(msgInfo.msgData); /* ipc */
1081         }
1082
1083         return MSG_SUCCESS;
1084 }
1085
1086
1087 msg_error_t MsgHandle::resetDatabase()
1088 {
1089         /* Allocate Memory to Command Data */
1090         int cmdSize = sizeof(MSG_CMD_S);
1091
1092         char cmdBuf[cmdSize];
1093         bzero(cmdBuf, cmdSize);
1094         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1095
1096         /* Set Command Parameters */
1097         pCmd->cmdType = MSG_CMD_RESET_DB;
1098
1099         /* Copy Cookie */
1100         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1101
1102         /* Send Command to Messaging FW */
1103         char* pEventData = NULL;
1104         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1105
1106         write((char*)pCmd, cmdSize, &pEventData);
1107
1108         /* Get Return Data */
1109         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1110
1111         if (pEvent == NULL)
1112                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1113
1114         if (pEvent->eventType != MSG_EVENT_RESET_DB) {
1115                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1116         }
1117
1118         return pEvent->result;
1119 }
1120
1121
1122 msg_error_t MsgHandle::getMemSize(unsigned int* memsize)
1123 {
1124         /* Allocate Memory to Command Data */
1125         int cmdSize = sizeof(MSG_CMD_S);
1126
1127         char cmdBuf[cmdSize];
1128         bzero(cmdBuf, cmdSize);
1129         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1130
1131         /* Set Command Parameters */
1132         pCmd->cmdType = MSG_CMD_GET_MEMSIZE;
1133
1134         /* Copy Cookie */
1135         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1136
1137         /* Send Command to Messaging FW */
1138         char* pEventData = NULL;
1139         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1140
1141         write((char*)pCmd, cmdSize, &pEventData);
1142
1143         /* Get Return Data */
1144         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1145
1146         if (pEvent == NULL)
1147                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1148
1149         if (pEvent->eventType != MSG_EVENT_GET_MEMSIZE) {
1150                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1151         }
1152
1153         if (pEvent->result != MSG_SUCCESS)
1154                 return pEvent->result;
1155
1156         /* Decode Return Data */
1157         MsgDecodeMemSize(pEvent->data, memsize);
1158
1159         return MSG_SUCCESS;
1160 }
1161
1162
1163 msg_error_t MsgHandle::backupMessage(msg_message_backup_type_t type, const char *backup_filepath)
1164 {
1165         if (backup_filepath == NULL)
1166                 return MSG_ERR_NULL_POINTER;
1167
1168         /* Create an empty file for writing. */
1169         /* If a file with the same name already exists its content is erased */
1170         /* and the file is treated as a new empty file. */
1171
1172         FILE *pFile = MsgOpenFile(backup_filepath, "w");
1173         if (pFile == NULL) {
1174                 MSG_DEBUG("File Open error");
1175                 return MSG_ERR_STORAGE_ERROR;
1176         }
1177         MsgCloseFile(pFile);
1178
1179         char path[MSG_FILEPATH_LEN_MAX+1] = {0, };
1180         strncpy(path, backup_filepath, MSG_FILEPATH_LEN_MAX);
1181
1182         /* Allocate Memory to Command Data */
1183         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_backup_type_t) + sizeof(path);
1184
1185         char cmdBuf[cmdSize];
1186         bzero(cmdBuf, cmdSize);
1187         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1188
1189         /* Set Command Parameters */
1190         pCmd->cmdType = MSG_CMD_BACKUP_MESSAGE;
1191
1192         /* Copy Cookie */
1193         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1194         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &type, sizeof(msg_message_backup_type_t));
1195         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_backup_type_t)), (char *)path, sizeof(path));
1196
1197         /* Send Command to Messaging FW */
1198         char* pEventData = NULL;
1199         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1200
1201         write((char*)pCmd, cmdSize, &pEventData);
1202
1203         /* Get Return Data */
1204         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1205
1206         if (pEvent == NULL)
1207                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1208
1209         if (pEvent->eventType != MSG_EVENT_BACKUP_MESSAGE) {
1210                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1211         }
1212
1213         return pEvent->result;
1214 }
1215
1216
1217 msg_error_t MsgHandle::restoreMessage(const char *backup_filepath)
1218 {
1219         if (backup_filepath == NULL)
1220                 return MSG_ERR_NULL_POINTER;
1221
1222         if (MsgAccessFile(backup_filepath, R_OK) == false) {
1223                 MSG_DEBUG("File access error");
1224                 return MSG_ERR_UNKNOWN;
1225         }
1226
1227         char path[MSG_FILEPATH_LEN_MAX+1] = {0, };
1228         strncpy(path, backup_filepath, MSG_FILEPATH_LEN_MAX);
1229
1230         /* Allocate Memory to Command Data */
1231         int cmdSize = sizeof(MSG_CMD_S) + sizeof(path);
1232
1233         char cmdBuf[cmdSize];
1234         bzero(cmdBuf, cmdSize);
1235         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1236
1237         /* Set Command Parameters */
1238         pCmd->cmdType = MSG_CMD_RESTORE_MESSAGE;
1239
1240         /* Copy Cookie */
1241         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1242         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), (char *)path, sizeof(path));
1243
1244         /* Send Command to Messaging FW */
1245         char* pEventData = NULL;
1246         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1247
1248         write((char*)pCmd, cmdSize, &pEventData);
1249
1250         /* Get Return Data */
1251         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1252
1253         if (pEvent == NULL)
1254                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1255
1256         if (pEvent->eventType != MSG_EVENT_RESTORE_MESSAGE) {
1257                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1258         }
1259
1260         return pEvent->result;
1261 }
1262
1263
1264 msg_error_t MsgHandle::searchMessage(const char *pSearchString, msg_struct_list_s *pThreadViewList)
1265 {
1266         msg_error_t err = MSG_SUCCESS;
1267
1268         int count = 0;
1269
1270         MSG_ADDRESS_INFO_S *pAddrInfo = NULL;
1271         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> buf(&pAddrInfo, unique_ptr_deleter);
1272
1273         /* get contact search list */
1274         if (MsgGetContactSearchList(pSearchString, &pAddrInfo, &count) != MSG_SUCCESS) {
1275                 MSG_DEBUG("MsgGetContactSearchList fail.");
1276                 count = 0;
1277         }
1278
1279         if (count > 0) {
1280                 /* Allocate Memory to Command Data */
1281                 int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + sizeof(MSG_ADDRESS_INFO_S) * count;
1282
1283                 char cmdBuf[cmdSize];
1284                 bzero(cmdBuf, cmdSize);
1285                 MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1286
1287                 /* Set Command Parameters */
1288                 pCmd->cmdType = MSG_CMD_SET_TEMP_ADDRESS_TABLE;
1289
1290                 /* Copy Cookie */
1291                 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1292
1293                 /* Copy Command Data */
1294                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &count, sizeof(int));
1295                 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), pAddrInfo, sizeof(MSG_ADDRESS_INFO_S) * count);
1296
1297                 /* Send Command to Messaging FW */
1298                 char* pEventData = NULL;
1299                 unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1300
1301                 write((char*)pCmd, cmdSize, &pEventData);
1302
1303                 /* Get Return Data */
1304                 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1305
1306                 if (pEvent == NULL)
1307                         THROW(MsgException::INVALID_RESULT, "Event is NULL");
1308
1309                 if (pEvent->eventType != MSG_EVENT_SET_TEMP_ADDRESS_TABLE)
1310                         THROW(MsgException::INVALID_RESULT, "Event Data Error");
1311
1312                 if (pEvent->result != MSG_SUCCESS)
1313                         count = 0;
1314         }
1315
1316         err = MsgStoSearchMessage(pSearchString, pThreadViewList, count);
1317
1318         if (err != MSG_SUCCESS) {
1319                 MSG_DEBUG("MsgStoSearchMessage() Error!!");
1320                 return err;
1321         }
1322
1323 /*      MsgStoDisconnectDB(); */
1324
1325         return err;
1326 }
1327
1328
1329 msg_error_t MsgHandle::getRejectMsgList(const char *pNumber, msg_struct_list_s *pRejectMsgList)
1330 {
1331         msg_error_t err = MSG_SUCCESS;
1332         /*
1333         err = MsgStoConnectDB();
1334
1335         if (err != MSG_SUCCESS) {
1336                 MSG_DEBUG("MsgStoConnectDB() Error!!");
1337                 return err;
1338         }
1339         */
1340         err = MsgStoGetRejectMsgList(pNumber, pRejectMsgList);
1341
1342         if (err != MSG_SUCCESS) {
1343                 MSG_DEBUG("MsgStoGetRejectMsgList() Error!!");
1344                 return err;
1345         }
1346
1347         /* MsgStoDisconnectDB(); */
1348
1349         return err;
1350 }
1351
1352
1353 msg_error_t MsgHandle::regStorageChangeCallback(msg_storage_change_cb onStorageChange, void *pUserParam)
1354 {
1355         if (!onStorageChange)
1356                 THROW(MsgException::INVALID_PARAM, "onStorageChange is null");
1357
1358         MsgProxyListener* eventListener = MsgProxyListener::instance();
1359
1360         eventListener->start(this);
1361
1362         int remoteFd = eventListener->getRemoteFd(); /* fd that is reserved to the "listener thread" by msgfw daemon */
1363
1364         if (remoteFd == -1 )
1365                 return MSG_ERR_INVALID_MSGHANDLE;
1366
1367         if (eventListener->regStorageChangeEventCB(this, remoteFd, onStorageChange, pUserParam) == false)
1368                 return MSG_ERR_INVALID_PARAMETER;
1369
1370         /* Allocate Memory to Command Data */
1371         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); /* cmd type, listenerFd */
1372         char cmdBuf[cmdSize];
1373         bzero(cmdBuf, cmdSize);
1374         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
1375
1376         /* Set Command Parameters */
1377         pCmd->cmdType = MSG_CMD_REG_STORAGE_CHANGE_CB;
1378
1379         /* Copy Cookie */
1380         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1381
1382         MSG_DEBUG("remote fd %d", remoteFd);
1383
1384         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &remoteFd, sizeof(remoteFd));
1385
1386         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), remoteFd);
1387
1388         /* Send Command to Messaging FW */
1389         char* pEventData = NULL;
1390         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1391
1392         write((char*)pCmd, cmdSize, &pEventData);
1393
1394         /* Get Return Data */
1395         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1396
1397         if (pEvent == NULL)
1398                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1399
1400         if (pEvent->eventType != MSG_EVENT_REG_STORAGE_CHANGE_CB) {
1401                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
1402         }
1403
1404         return pEvent->result;
1405 }
1406
1407
1408 msg_error_t MsgHandle::getReportStatus(msg_message_id_t msg_id, msg_struct_list_s *report_list)
1409 {
1410         /* Allocate Memory to Command Data */
1411         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1412
1413         char cmdBuf[cmdSize];
1414         bzero(cmdBuf, cmdSize);
1415         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1416
1417         report_list->nCount = 0;
1418         report_list->msg_struct_info = NULL;
1419
1420         /* Set Command Parameters */
1421         pCmd->cmdType = MSG_CMD_GET_REPORT_STATUS;
1422
1423         /* Copy Cookie */
1424         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1425
1426         /* Copy Command Data */
1427         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msg_id, sizeof(msg_message_id_t));
1428
1429         /* Send Command to Messaging FW */
1430         char* pEventData = NULL;
1431         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1432
1433         write((char*)pCmd, cmdSize, &pEventData);
1434
1435         /* Get Return Data */
1436         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1437
1438         if (pEvent == NULL)
1439                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1440
1441         if (pEvent->eventType != MSG_EVENT_GET_REPORT_STATUS) {
1442                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1443         }
1444
1445         if (pEvent->result != MSG_SUCCESS)
1446                 return pEvent->result;
1447
1448         /* Decode Return Data */
1449         MsgDecodeReportStatus(pEvent->data, report_list);
1450
1451         return MSG_SUCCESS;
1452 }
1453
1454
1455 msg_error_t MsgHandle::getAddressList(const msg_thread_id_t threadId, msg_struct_list_s *pAddrList)
1456 {
1457         msg_error_t err = MSG_SUCCESS;
1458         /*
1459         err = MsgStoConnectDB();
1460
1461         if (err != MSG_SUCCESS) {
1462                 MSG_DEBUG("MsgStoConnectDB() Error!!");
1463                 return err;
1464         }
1465         */
1466         err = MsgStoGetAddressList(threadId, (msg_struct_list_s *)pAddrList);
1467
1468         if (err != MSG_SUCCESS) {
1469                 MSG_DEBUG("MsgStoGetThreadViewList() Error!!");
1470                 return err;
1471         }
1472
1473         /* MsgStoDisconnectDB(); */
1474
1475         return err;
1476 }
1477
1478
1479 msg_error_t MsgHandle::getThreadIdByAddress(msg_struct_list_s *pAddrList, msg_thread_id_t *pThreadId)
1480 {
1481         /* Allocate Memory to Command Data */
1482         int cmdSize = sizeof(MSG_CMD_S) + sizeof(pAddrList->nCount) + (sizeof(MSG_ADDRESS_INFO_S)*pAddrList->nCount);
1483
1484         char cmdBuf[cmdSize];
1485         bzero(cmdBuf, cmdSize);
1486         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1487
1488         /* Set Command Parameters */
1489         pCmd->cmdType = MSG_CMD_GET_THREAD_ID_BY_ADDRESS;
1490
1491         /* Copy Cookie */
1492         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1493
1494         /* Copy Command Data */
1495         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &pAddrList->nCount, sizeof(pAddrList->nCount));
1496         int addSize = sizeof(MSG_ADDRESS_INFO_S);
1497         for (int i = 0; i < pAddrList->nCount; i++) {
1498                 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));
1499         }
1500
1501         /* Send Command to Messaging FW */
1502         char* pEventData = NULL;
1503         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1504
1505         write((char*)pCmd, cmdSize, &pEventData);
1506
1507         /* Get Return Data */
1508         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1509
1510         if (pEvent == NULL)
1511                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1512
1513         if (pEvent->eventType != MSG_EVENT_GET_THREAD_ID_BY_ADDRESS) {
1514                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1515         }
1516
1517         if (pEvent->result != MSG_SUCCESS)
1518                 return pEvent->result;
1519
1520         /* Decode Return Data */
1521         MsgDecodeThreadId(pEvent->data, pThreadId);
1522
1523         return MSG_SUCCESS;
1524 }
1525
1526
1527 msg_error_t MsgHandle::getThreadIdByAddress(msg_list_handle_t msg_address_list, msg_thread_id_t *pThreadId)
1528 {
1529         int addrCnt = (int)g_list_length((GList *)msg_address_list);
1530
1531         /* Allocate Memory to Command Data */
1532         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*addrCnt);
1533
1534         char cmdBuf[cmdSize];
1535         bzero(cmdBuf, cmdSize);
1536         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1537
1538         /* Set Command Parameters */
1539         pCmd->cmdType = MSG_CMD_GET_THREAD_ID_BY_ADDRESS;
1540
1541         /* Copy Cookie */
1542         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1543
1544         /* Copy Command Data */
1545         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &addrCnt, sizeof(int));
1546         int addSize = sizeof(MSG_ADDRESS_INFO_S);
1547         for (int i = 0; i < addrCnt; i++) {
1548                 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));
1549         }
1550
1551         /* Send Command to Messaging FW */
1552         char* pEventData = NULL;
1553         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1554
1555         write((char*)pCmd, cmdSize, &pEventData);
1556
1557         /* Get Return Data */
1558         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1559
1560         if (pEvent == NULL)
1561                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1562
1563         if (pEvent->eventType != MSG_EVENT_GET_THREAD_ID_BY_ADDRESS) {
1564                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1565         }
1566
1567         if (pEvent->result != MSG_SUCCESS)
1568                 return pEvent->result;
1569
1570         /* Decode Return Data */
1571         MsgDecodeThreadId(pEvent->data, pThreadId);
1572
1573         return MSG_SUCCESS;
1574 }
1575
1576
1577 msg_error_t MsgHandle::getThread(msg_thread_id_t threadId, MSG_THREAD_VIEW_S* pThreadInfo)
1578 {
1579         /* Allocate Memory to Command Data */
1580         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t);
1581
1582         char cmdBuf[cmdSize];
1583         bzero(cmdBuf, cmdSize);
1584         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1585
1586         /* Set Command Parameters */
1587         pCmd->cmdType = MSG_CMD_GET_THREAD_INFO;
1588
1589         /* Copy Cookie */
1590         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1591
1592         /* Copy Command Data */
1593         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &threadId, sizeof(msg_thread_id_t));
1594
1595         /* Send Command to Messaging FW */
1596         char* pEventData = NULL;
1597         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1598
1599         write((char*)pCmd, cmdSize, &pEventData);
1600
1601         /* Get Return Data */
1602         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1603
1604         if (pEvent == NULL)
1605                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1606
1607         if (pEvent->eventType != MSG_EVENT_GET_THREAD_INFO) {
1608                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1609         }
1610
1611         /* Decode Return Data */
1612         MsgDecodeThreadInfo(pEvent->data, pThreadInfo);
1613
1614         return MSG_SUCCESS;
1615 }
1616
1617
1618 msg_error_t MsgHandle::getMessageList(const MSG_LIST_CONDITION_S *pListCond, msg_struct_list_s *pMsgList)
1619 {
1620         msg_error_t err = MSG_SUCCESS;
1621         /*
1622         err = MsgStoConnectDB();
1623
1624         if (err != MSG_SUCCESS) {
1625                 MSG_DEBUG("MsgStoConnectDB() Error!!");
1626                 return err;
1627         }
1628         */
1629         int count = 0;
1630
1631         if (pListCond->pAddressVal) {
1632                 MSG_ADDRESS_INFO_S *pAddrInfo = NULL;
1633                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> buf(&pAddrInfo, unique_ptr_deleter);
1634
1635                 /* get contact search list */
1636                 if (MsgGetContactSearchList(pListCond->pAddressVal, &pAddrInfo, &count) != MSG_SUCCESS) {
1637                         MSG_DEBUG("MsgGetContactSearchList fail.");
1638                         count = 0;
1639                 }
1640
1641                 if (count > 0) {
1642                         /* Allocate Memory to Command Data */
1643                         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int) + sizeof(MSG_ADDRESS_INFO_S) * count;
1644
1645                         char cmdBuf[cmdSize];
1646                         bzero(cmdBuf, cmdSize);
1647                         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1648
1649                         /* Set Command Parameters */
1650                         pCmd->cmdType = MSG_CMD_SET_TEMP_ADDRESS_TABLE;
1651
1652                         /* Copy Cookie */
1653                         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1654
1655                         /* Copy Command Data */
1656                         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &count, sizeof(int));
1657                         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), pAddrInfo, sizeof(MSG_ADDRESS_INFO_S) * count);
1658
1659                         /* Send Command to Messaging FW */
1660                         char* pEventData = NULL;
1661                         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1662
1663                         write((char*)pCmd, cmdSize, &pEventData);
1664
1665                         /* Get Return Data */
1666                         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1667
1668                         if (pEvent == NULL)
1669                                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1670
1671                         if (pEvent->eventType != MSG_EVENT_SET_TEMP_ADDRESS_TABLE)
1672                                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1673
1674                         if (pEvent->result != MSG_SUCCESS)
1675                                 count = 0;
1676                 }
1677         }
1678
1679         err = MsgStoGetMessageList(pListCond, pMsgList, count);
1680
1681         if (err != MSG_SUCCESS) {
1682                 MSG_DEBUG("MsgStoGetMessageList() Error!!");
1683                 return err;
1684         }
1685
1686         /* MsgStoDisconnectDB(); */
1687
1688         return err;
1689 }
1690
1691
1692 msg_error_t MsgHandle::getMediaList(const msg_thread_id_t thread_id, msg_list_handle_t *pMediaList)
1693 {
1694         msg_error_t err = MSG_SUCCESS;
1695
1696         err = MsgStoGetMediaList(thread_id, pMediaList);
1697
1698         if (err != MSG_SUCCESS) {
1699                 MSG_DEBUG("MsgStoGetFmMediaList() Error!!");
1700                 return err;
1701         }
1702
1703         return err;
1704 }
1705
1706
1707 int MsgHandle::addPushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1708 {
1709         /* Allocate Memory to Command Data */
1710         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1711
1712         char cmdBuf[cmdSize];
1713         bzero(cmdBuf, cmdSize);
1714         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1715
1716         /* Set Command Parameters */
1717         pCmd->cmdType = MSG_CMD_ADD_PUSH_EVENT;
1718
1719         /* Copy Cookie */
1720         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1721
1722         /* Copy Command Data */
1723         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1724
1725         /* Send Command to Messaging FW */
1726         char* pEventData = NULL;
1727         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1728
1729         write((char*)pCmd, cmdSize, &pEventData);
1730
1731         /* Get Return Data */
1732         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1733
1734         if (pEvent == NULL)
1735                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1736
1737         if (pEvent->eventType != MSG_EVENT_ADD_PUSH_EVENT) {
1738                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1739         }
1740
1741         return pEvent->result;
1742 }
1743
1744
1745 int MsgHandle::deletePushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1746 {
1747         /* Allocate Memory to Command Data */
1748         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1749
1750         char cmdBuf[cmdSize];
1751         bzero(cmdBuf, cmdSize);
1752         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1753
1754         /* Set Command Parameters */
1755         pCmd->cmdType = MSG_CMD_DELETE_PUSH_EVENT;
1756
1757         /* Copy Cookie */
1758         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1759
1760         /* Copy Command Data */
1761         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1762
1763         /* Send Command to Messaging FW */
1764         char* pEventData = NULL;
1765         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1766
1767         write((char*)pCmd, cmdSize, &pEventData);
1768
1769         /* Get Return Data */
1770         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1771
1772         if (pEvent == NULL)
1773                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1774
1775         if (pEvent->eventType != MSG_EVENT_DELETE_PUSH_EVENT) {
1776                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1777         }
1778
1779         return pEvent->result;
1780 }
1781
1782
1783 int MsgHandle::updatePushEvent(MSG_PUSH_EVENT_INFO_S *pSrc, MSG_PUSH_EVENT_INFO_S *pDst)
1784 {
1785         /* Allocate Memory to Command Data */
1786         int cmdSize = sizeof(MSG_CMD_S) +  2 * sizeof(MSG_PUSH_EVENT_INFO_S);
1787
1788         char cmdBuf[cmdSize];
1789         bzero(cmdBuf, cmdSize);
1790         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1791
1792         /* Set Command Parameters */
1793         pCmd->cmdType = MSG_CMD_UPDATE_PUSH_EVENT;
1794
1795         /* Copy Cookie */
1796         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1797
1798         /* Copy Command Data */
1799         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pSrc, sizeof(MSG_PUSH_EVENT_INFO_S));
1800         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));
1801
1802         /* Send Command to Messaging FW */
1803         char* pEventData = NULL;
1804         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1805
1806         write((char*)pCmd, cmdSize, &pEventData);
1807
1808         /* Get Return Data */
1809         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1810
1811         if (pEvent == NULL)
1812                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1813
1814         if (pEvent->eventType != MSG_EVENT_UPDATE_PUSH_EVENT) {
1815                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1816         }
1817
1818         return pEvent->result;
1819 }
1820
1821
1822 msg_error_t MsgHandle::getVobject(msg_message_id_t MsgId, void** encodedData)
1823 {
1824         /* Allocate Memory to Command Data */
1825         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1826         char *encode_data = NULL;
1827         char cmdBuf[cmdSize];
1828         bzero(cmdBuf, cmdSize);
1829         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1830
1831         /* Set Command Parameters */
1832         pCmd->cmdType = MSG_CMD_GET_MSG;
1833
1834         /* Copy Cookie */
1835         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1836
1837         /* Copy Command Data */
1838         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
1839
1840         /* Send Command to Messaging FW */
1841         char* pEventData = NULL;
1842         unique_ptr<char*, void(*)(char**)> eventBuf(&pEventData, unique_ptr_deleter);
1843
1844
1845         write((char*)pCmd, cmdSize, &pEventData);
1846
1847         /* Get Return Data */
1848         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1849
1850         if (pEvent == NULL)
1851                 THROW(MsgException::INVALID_RESULT, "Event is NULL");
1852
1853         if (pEvent->eventType != MSG_EVENT_GET_MSG) {
1854                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1855         }
1856
1857         if (pEvent->result != MSG_SUCCESS)
1858                 return pEvent->result;
1859
1860         /* Decode Return Data */
1861         MSG_MESSAGE_INFO_S msgInfo = {0, };
1862         MSG_SENDINGOPT_INFO_S sendOptInfo = {0, };
1863
1864         msgInfo.addressList = NULL;
1865         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1866
1867         MsgDecodeMsgInfo(pEvent->data, &msgInfo, &sendOptInfo);
1868
1869         /*Convert MSG_MESSAGE_INFO_S to */
1870         encode_data = MsgVMessageEncode(&msgInfo);
1871         if (encode_data) {
1872                 *encodedData = (void*)encode_data;
1873         } else {
1874                 MSG_DEBUG("Error Encode data");
1875                 *encodedData = NULL;
1876         }
1877
1878         /* Delete Temp File */
1879         if (msgInfo.bTextSms == false) {
1880                 /* Delete Temp File */
1881                 MsgDeleteFile(msgInfo.msgData); /* ipc */
1882         }
1883
1884         return MSG_SUCCESS;
1885 }