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