RSA sync with private
[platform/core/messaging/msg-service.git] / proxy / MsgHandleStorage.cpp
1 /*
2 * Copyright 2012  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://www.tizenopensource.org/license
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         if (eventListener->regStorageChangeEventCB(this, onStorageChange, pUserParam) == false) // callback was already registered, just return SUCCESS
1262                 return MSG_SUCCESS;
1263
1264         // Allocate Memory to Command Data
1265         int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
1266         char cmdBuf[cmdSize];
1267         bzero(cmdBuf, cmdSize);
1268         MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
1269
1270         // Set Command Parameters
1271         pCmd->cmdType = MSG_CMD_REG_STORAGE_CHANGE_CB;
1272
1273         // Copy Cookie
1274         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1275
1276         int listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
1277
1278         MSG_DEBUG("remote fd %d", listenerFd);
1279
1280         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
1281
1282         MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
1283
1284         // Send Command to Messaging FW
1285         char* pEventData = NULL;
1286         AutoPtr<char> eventBuf(&pEventData);
1287
1288         write((char*)pCmd, cmdSize, &pEventData);
1289
1290         // Get Return Data
1291         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1292
1293         if (pEvent->eventType != MSG_EVENT_REG_STORAGE_CHANGE_CB)
1294         {
1295                 THROW(MsgException::INVALID_PARAM, "Event Data Error");
1296         }
1297
1298         return pEvent->result;
1299 }
1300
1301 msg_error_t MsgHandle::getReportStatus(msg_message_id_t msg_id, msg_struct_list_s *report_list)
1302 {
1303         // Allocate Memory to Command Data
1304         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1305
1306         char cmdBuf[cmdSize];
1307         bzero(cmdBuf, cmdSize);
1308         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1309
1310         // Set Command Parameters
1311         pCmd->cmdType = MSG_CMD_GET_REPORT_STATUS;
1312
1313         // Copy Cookie
1314         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1315
1316         // Copy Command Data
1317         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msg_id, sizeof(msg_message_id_t));
1318
1319         // Send Command to Messaging FW
1320         char* pEventData = NULL;
1321         AutoPtr<char> eventBuf(&pEventData);
1322
1323
1324         write((char*)pCmd, cmdSize, &pEventData);
1325
1326         // Get Return Data
1327         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1328
1329         if (pEvent->eventType != MSG_EVENT_GET_REPORT_STATUS)
1330         {
1331                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1332         }
1333
1334         if(pEvent->result != MSG_SUCCESS) return pEvent->result;
1335
1336         // Decode Return Data
1337         MsgDecodeReportStatus(pEvent->data, report_list);
1338
1339         return MSG_SUCCESS;
1340 }
1341
1342
1343 msg_error_t MsgHandle::getAddressList(const msg_thread_id_t threadId, msg_struct_list_s *pAddrList)
1344 {
1345         msg_error_t err =  MSG_SUCCESS;
1346
1347         err = MsgStoConnectDB();
1348
1349         if (err != MSG_SUCCESS)
1350         {
1351                 MSG_DEBUG("MsgStoConnectDB() Error!!");
1352                 return err;
1353         }
1354
1355         err = MsgStoGetAddressList(threadId, (msg_struct_list_s *)pAddrList);
1356
1357         if (err != MSG_SUCCESS)
1358         {
1359                 MSG_DEBUG("MsgStoGetThreadViewList() Error!!");
1360                 return err;
1361         }
1362
1363         MsgStoDisconnectDB();
1364
1365         return err;
1366 }
1367
1368
1369 msg_error_t MsgHandle::getThreadIdByAddress(msg_struct_list_s *pAddrList, msg_thread_id_t *pThreadId)
1370 {
1371         // Allocate Memory to Command Data
1372         int cmdSize = sizeof(MSG_CMD_S) + sizeof(pAddrList->nCount) + (sizeof(MSG_ADDRESS_INFO_S)*pAddrList->nCount);
1373
1374         char cmdBuf[cmdSize];
1375         bzero(cmdBuf, cmdSize);
1376         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1377
1378         // Set Command Parameters
1379         pCmd->cmdType = MSG_CMD_GET_THREAD_ID_BY_ADDRESS;
1380
1381         // Copy Cookie
1382         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1383
1384         // Copy Command Data
1385         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &pAddrList->nCount, sizeof(pAddrList->nCount));
1386         int addSize = sizeof(MSG_ADDRESS_INFO_S);
1387         for(int i=0; i<pAddrList->nCount; i++) {
1388                 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));
1389         }
1390
1391         // Send Command to Messaging FW
1392         char* pEventData = NULL;
1393         AutoPtr<char> eventBuf(&pEventData);
1394
1395
1396         write((char*)pCmd, cmdSize, &pEventData);
1397
1398         // Get Return Data
1399         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1400
1401         if (pEvent->eventType != MSG_EVENT_GET_THREAD_ID_BY_ADDRESS)
1402         {
1403                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1404         }
1405
1406         if(pEvent->result != MSG_SUCCESS) return pEvent->result;
1407
1408         // Decode Return Data
1409         MsgDecodeThreadId(pEvent->data, pThreadId);
1410
1411         return MSG_SUCCESS;
1412 }
1413
1414
1415 msg_error_t MsgHandle::getThread(msg_thread_id_t threadId, MSG_THREAD_VIEW_S* pThreadInfo)
1416 {
1417         // Allocate Memory to Command Data
1418         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_thread_id_t);
1419
1420         char cmdBuf[cmdSize];
1421         bzero(cmdBuf, cmdSize);
1422         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1423
1424         // Set Command Parameters
1425         pCmd->cmdType = MSG_CMD_GET_THREAD_INFO;
1426
1427         // Copy Cookie
1428         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1429
1430         // Copy Command Data
1431         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &threadId, sizeof(msg_thread_id_t));
1432
1433         // Send Command to Messaging FW
1434         char* pEventData = NULL;
1435         AutoPtr<char> eventBuf(&pEventData);
1436
1437         write((char*)pCmd, cmdSize, &pEventData);
1438
1439         // Get Return Data
1440         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1441
1442         if (pEvent->eventType != MSG_EVENT_GET_THREAD_INFO)
1443         {
1444                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1445         }
1446
1447         // Decode Return Data
1448         MsgDecodeThreadInfo(pEvent->data, pThreadInfo);
1449
1450         return MSG_SUCCESS;
1451 }
1452
1453
1454 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)
1455 {
1456         msg_error_t err =  MSG_SUCCESS;
1457
1458         err = MsgStoConnectDB();
1459
1460         if (err != MSG_SUCCESS) {
1461                 MSG_DEBUG("MsgStoConnectDB() Error!!");
1462                 return err;
1463         }
1464
1465         err = MsgStoGetMessageList(folderId, threadId, msgType, storageId, pMsgList);
1466
1467         if (err != MSG_SUCCESS) {
1468                 MSG_DEBUG("MsgStoSearchMessage() Error!!");
1469                 return err;
1470         }
1471
1472         MsgStoDisconnectDB();
1473
1474         return err;
1475 }
1476
1477
1478 int MsgHandle::addPushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1479 {
1480         // Allocate Memory to Command Data
1481         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1482
1483         char cmdBuf[cmdSize];
1484         bzero(cmdBuf, cmdSize);
1485         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1486
1487         // Set Command Parameters
1488         pCmd->cmdType = MSG_CMD_ADD_PUSH_EVENT;
1489
1490         // Copy Cookie
1491         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1492
1493         // Copy Command Data
1494         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1495
1496         // Send Command to Messaging FW
1497         char* pEventData = NULL;
1498         AutoPtr<char> eventBuf(&pEventData);
1499
1500         write((char*)pCmd, cmdSize, &pEventData);
1501
1502         // Get Return Data
1503         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1504
1505         if (pEvent->eventType != MSG_EVENT_ADD_PUSH_EVENT)
1506         {
1507                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1508         }
1509
1510         return pEvent->result;
1511 }
1512
1513
1514 int MsgHandle::deletePushEvent(MSG_PUSH_EVENT_INFO_S *pPushEvent)
1515 {
1516         // Allocate Memory to Command Data
1517         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_PUSH_EVENT_INFO_S);
1518
1519         char cmdBuf[cmdSize];
1520         bzero(cmdBuf, cmdSize);
1521         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1522
1523         // Set Command Parameters
1524         pCmd->cmdType = MSG_CMD_DELETE_PUSH_EVENT;
1525
1526         // Copy Cookie
1527         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1528
1529         // Copy Command Data
1530         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pPushEvent, sizeof(MSG_PUSH_EVENT_INFO_S));
1531
1532         // Send Command to Messaging FW
1533         char* pEventData = NULL;
1534         AutoPtr<char> eventBuf(&pEventData);
1535
1536         write((char*)pCmd, cmdSize, &pEventData);
1537
1538         // Get Return Data
1539         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1540
1541         if (pEvent->eventType != MSG_EVENT_DELETE_PUSH_EVENT)
1542         {
1543                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1544         }
1545
1546         return pEvent->result;
1547 }
1548
1549 int MsgHandle::updatePushEvent(MSG_PUSH_EVENT_INFO_S *pSrc, MSG_PUSH_EVENT_INFO_S *pDst)
1550 {
1551         // Allocate Memory to Command Data
1552         int cmdSize = sizeof(MSG_CMD_S) +  2 * sizeof(MSG_PUSH_EVENT_INFO_S);
1553
1554         char cmdBuf[cmdSize];
1555         bzero(cmdBuf, cmdSize);
1556         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1557
1558         // Set Command Parameters
1559         pCmd->cmdType = MSG_CMD_UPDATE_PUSH_EVENT;
1560
1561         // Copy Cookie
1562         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1563
1564         // Copy Command Data
1565         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), pSrc, sizeof(MSG_PUSH_EVENT_INFO_S));
1566         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));
1567
1568         // Send Command to Messaging FW
1569         char* pEventData = NULL;
1570         AutoPtr<char> eventBuf(&pEventData);
1571
1572         write((char*)pCmd, cmdSize, &pEventData);
1573
1574         // Get Return Data
1575         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1576
1577         if (pEvent->eventType != MSG_EVENT_UPDATE_PUSH_EVENT)
1578         {
1579                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1580         }
1581
1582         return pEvent->result;
1583 }
1584
1585 msg_error_t MsgHandle::getVobject(msg_message_id_t MsgId, void** encodedData)
1586 {
1587         // Allocate Memory to Command Data
1588         int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
1589         char *encode_data = NULL;
1590         char cmdBuf[cmdSize];
1591         bzero(cmdBuf, cmdSize);
1592         MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
1593
1594         // Set Command Parameters
1595         pCmd->cmdType = MSG_CMD_GET_MSG;
1596
1597         // Copy Cookie
1598         memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
1599
1600         // Copy Command Data
1601         memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &MsgId, sizeof(msg_message_id_t));
1602
1603         // Send Command to Messaging FW
1604         char* pEventData = NULL;
1605         AutoPtr<char> eventBuf(&pEventData);
1606
1607
1608         write((char*)pCmd, cmdSize, &pEventData);
1609
1610         // Get Return Data
1611         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
1612
1613         if (pEvent->eventType != MSG_EVENT_GET_MSG)
1614         {
1615                 THROW(MsgException::INVALID_RESULT, "Event Data Error");
1616         }
1617
1618         if(pEvent->result != MSG_SUCCESS)
1619                 return pEvent->result;
1620
1621         // Decode Return Data
1622         MSG_MESSAGE_INFO_S msgInfo = {0,};
1623         MSG_SENDINGOPT_INFO_S sendOptInfo = {0,};
1624
1625         MsgDecodeMsgInfo(pEvent->data, &msgInfo, &sendOptInfo);
1626
1627         //Convert MSG_MESSAGE_INFO_S to
1628         encode_data = MsgVMessageEncode(&msgInfo);
1629         if (encode_data) {
1630                 *encodedData = (void*)encode_data;
1631         } else {
1632                 MSG_DEBUG("Error Encode data");
1633                 *encodedData = NULL;
1634         }
1635
1636         // Delete Temp File
1637         if (msgInfo.bTextSms == false)
1638         {
1639                 // Delete Temp File
1640                 MsgDeleteFile(msgInfo.msgData); //ipc
1641         }
1642
1643         return MSG_SUCCESS;
1644 }