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