Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgCmdHandlerStorage.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <stdlib.h>
18
19 #include "MsgDebug.h"
20 #include "MsgUtilFile.h"
21 #include "MsgException.h"
22 #include "MsgCppTypes.h"
23 //#include "MsgSoundPlayer.h"
24 #include "MsgUtilFunction.h"
25 #include "MsgStorageHandler.h"
26 #include "MsgPluginManager.h"
27 #include "MsgTransManager.h"
28 #include "MsgCmdHandler.h"
29 #include "MsgNotificationWrapper.h"
30 #include "MsgMutex.h"
31 #include <glib.h>
32
33 /*==================================================================================================
34                                      FUNCTION IMPLEMENTATION
35 ==================================================================================================*/
36
37 Mutex mx;
38 int g_job_cnt = 0;
39
40 static gboolean  __refresh_noti(void *data)
41 {
42         mx.lock();
43
44         g_job_cnt--;
45
46         if (g_job_cnt <= 0) {
47                 MSG_DEBUG("## Refresh notification ##");
48                 if (MsgRefreshNotification(MSG_NOTI_TYPE_NORMAL, false, false) != MSG_SUCCESS) {
49                         MSG_DEBUG("MsgRefreshNoti is failed");
50                 }
51                 g_job_cnt = 0;
52         }
53
54         mx.unlock();
55
56         return FALSE;
57
58 }
59
60 int MsgAddMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
61 {
62         msg_error_t err = MSG_SUCCESS;
63
64         char* encodedData = NULL;
65         AutoPtr<char> buf(&encodedData);
66
67         int dataSize = 0, eventSize = 0;
68
69         MSG_MESSAGE_INFO_S msgInfo;
70         MSG_SENDINGOPT_INFO_S sendOptInfo;
71         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
72         memset(&sendOptInfo, 0x00, sizeof(MSG_SENDINGOPT_INFO_S));
73
74         msgInfo.addressList = NULL;
75         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
76
77         MsgDecodeMsgInfo((char *)pCmd->cmdData, &msgInfo, &sendOptInfo);
78
79         if(msgInfo.threadId) // threadId is not 0 : message restore from S-Cloud
80         {
81                 err = MsgStoRestoreMessage(&msgInfo, &sendOptInfo);
82
83                 if (err == MSG_SUCCESS) {
84                                 MSG_DEBUG("Command Handle Success : MsgStoRestoreMessage()");
85
86                                 // Encoding Message ID
87                                 dataSize = MsgEncodeMsgId(&(msgInfo.msgId), &encodedData);
88                 } else {
89                         MSG_DEBUG("Command Handle Fail : MsgStoRestoreMessage()");
90                 }
91         }
92         else
93         {
94                 // Add Message
95                 msgInfo.msgId = 0;
96                 err = MsgStoAddMessage(&msgInfo, &sendOptInfo);
97
98                 if (err == MSG_SUCCESS) {
99                         MSG_DEBUG("Command Handle Success : MsgStoAddMessage()");
100                         // Encoding Message ID
101                         dataSize = MsgEncodeMsgId(&(msgInfo.msgId), &encodedData);
102                 } else {
103                         MSG_DEBUG("Command Handle Fail : MsgStoAddMessage()");
104                 }
105         }
106
107         //storage change CB
108         msg_id_list_s msgIdList;
109         msg_message_id_t msgIds[1];
110         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
111
112         msgIdList.nCount = 1;
113         msgIds[0] = msgInfo.msgId;
114         msgIdList.msgIdList = msgIds;
115
116         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_INSERT, &msgIdList);
117
118         // Update unread message status
119         if (msgInfo.folderId == MSG_INBOX_ID && msgInfo.bRead == false) {
120                 mx.lock();
121                 g_job_cnt++;
122                 g_timeout_add(300, __refresh_noti, NULL);
123                 mx.unlock();
124         }
125
126         // Delete Temp File for Message Data
127         if (msgInfo.bTextSms == false)
128                 MsgDeleteFile(msgInfo.msgData); //ipc
129
130         // Make Event Data
131         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_ADD_MSG, err, (void**)ppEvent);
132
133         return eventSize;
134 }
135
136
137 int MsgAddSyncMLMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
138 {
139         msg_error_t err = MSG_SUCCESS;
140
141         char* encodedData = NULL;
142         AutoPtr<char> buf(&encodedData);
143
144         int dataSize = 0, eventSize = 0;
145
146         int extId = 0, pinCode = 0;
147
148         MSG_MESSAGE_INFO_S msgInfo;
149         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
150
151         msgInfo.addressList = NULL;
152         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
153
154         memcpy(&extId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(int));
155         memcpy(&pinCode, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)), sizeof(int));
156
157         MsgDecodeMsgInfo((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(int)+sizeof(int), &msgInfo);
158
159         msgInfo.msgId = 0;
160         // Add Message
161         err = MsgStoAddSyncMLMessage(&msgInfo, extId, pinCode);
162
163         if (err == MSG_SUCCESS)
164         {
165                 MSG_DEBUG("Command Handle Success : MsgStoAddSyncMLMessage()");
166
167                 // Encoding Message ID
168                 dataSize = MsgEncodeMsgId(&(msgInfo.msgId), &encodedData);
169
170                 // broadcast to listener threads, here
171                 MsgTransactionManager::instance()->broadcastIncomingMsgCB(err, &msgInfo);
172                 //storage change CB
173                 msg_id_list_s msgIdList;
174                 msg_message_id_t msgIds[1];
175                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
176
177                 msgIdList.nCount = 1;
178                 msgIds[0] = msgInfo.msgId;
179                 msgIdList.msgIdList = msgIds;
180
181                 MsgTransactionManager::instance()->broadcastStorageChangeCB(err, MSG_STORAGE_CHANGE_INSERT, &msgIdList);
182         }
183         else
184         {
185                 MSG_DEBUG("Command Handle Fail : MsgStoAddSyncMLMessage()");
186         }
187
188         // Make Event Data
189         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_ADD_SYNCML_MSG, err, (void**)ppEvent);
190
191         return eventSize;
192 }
193
194
195 int MsgUpdateMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
196 {
197         msg_error_t err = MSG_SUCCESS;
198
199         int eventSize = 0;
200
201         MSG_MESSAGE_INFO_S msgInfo;
202         MSG_SENDINGOPT_INFO_S sendOptInfo;
203         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
204         memset(&sendOptInfo, 0x00, sizeof(MSG_SENDINGOPT_INFO_S));
205
206         msgInfo.addressList = NULL;
207         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
208
209         MsgDecodeMsgInfo((char *)pCmd->cmdData, &msgInfo, &sendOptInfo);
210
211         // Update Message
212         err = MsgStoUpdateMessage(&msgInfo, &sendOptInfo);
213
214         if (err == MSG_SUCCESS)
215                 MSG_DEBUG("Command Handle Success : MsgStoUpdateMessage()");
216         else
217                 MSG_DEBUG("Command Handle Fail : MsgStoUpdateMessage()");
218
219         // Delete Temp File for Message Data
220         if (msgInfo.bTextSms == false)
221                 MsgDeleteFile(msgInfo.msgData); //ipc
222
223         //storage change CB
224         msg_id_list_s msgIdList;
225         msg_message_id_t msgIds[1];
226         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
227
228         msgIdList.nCount = 1;
229         msgIds[0] = msgInfo.msgId;
230         msgIdList.msgIdList = msgIds;
231
232         MsgTransactionManager::instance()->broadcastStorageChangeCB(err, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
233
234         // Make Event Data
235         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_MSG, err, (void**)ppEvent);
236
237         return eventSize;
238 }
239
240
241 int MsgUpdateReadStatusHandler(const MSG_CMD_S *pCmd, char **ppEvent)
242 {
243         msg_error_t err = MSG_SUCCESS;
244
245         int eventSize = 0;
246
247         msg_message_id_t msgId;
248         bool readStatus;
249
250         memcpy(&msgId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_id_t));
251         memcpy(&readStatus, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), sizeof(bool));
252
253         // Update Read Status
254         err = MsgStoUpdateReadStatus(msgId, readStatus);
255
256         MSG_DEBUG("message id = [%d], readStatus = [%d]", msgId, readStatus);
257
258         if (err == MSG_SUCCESS)
259         {
260                 MSG_DEBUG("Command Handle Success : MsgStoUpdateReadStatus()");
261         }
262         else
263         {
264                 MSG_DEBUG("Command Handle Fail : MsgStoUpdateReadStatus()");
265         }
266
267         //storage change CB
268         msg_id_list_s msgIdList;
269         msg_message_id_t msgIds[1];
270         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
271
272         msgIdList.nCount = 1;
273         msgIds[0] = msgId;
274         msgIdList.msgIdList = msgIds;
275
276         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
277
278         // Make Event Data
279         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_READ, err, (void**)ppEvent);
280
281         return eventSize;
282 }
283
284
285 int MsgUpdateThreadReadStatusHandler(const MSG_CMD_S *pCmd, char **ppEvent)
286 {
287         msg_error_t err = MSG_SUCCESS;
288
289         int eventSize = 0;
290
291         msg_thread_id_t threadId;
292         int unReadCnt = 0;
293
294         memcpy(&threadId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_thread_id_t));
295
296         MsgStoGetThreadUnreadCnt(threadId, &unReadCnt);
297
298         MSG_DEBUG("unReadCnt [%d]", unReadCnt);
299
300         if (unReadCnt > 0) {
301
302                 msg_id_list_s msgIdList;
303                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
304
305                 err = MsgStoUpdateThreadReadStatus(threadId, &msgIdList);
306
307                 if (err == MSG_SUCCESS) {
308                         MSG_DEBUG("Command Handle Success : MsgStoUpdateThreadReadStatus()");
309                         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
310                 } else {
311                         MSG_DEBUG("Command Handle Fail : MsgStoUpdateThreadReadStatus()");
312                 }
313
314                 if(msgIdList.msgIdList != NULL)
315                         delete [] (char*)msgIdList.msgIdList;
316         }
317
318         // delete report notification
319         if (err == MSG_SUCCESS) {
320                 MsgDbHandler *dbHandle = getDbHandle();
321                 char tempAddr[MAX_ADDRESS_VAL_LEN+1];
322                 char sqlQuery[MAX_QUERY_LEN+1];
323
324                 memset(tempAddr, 0x00, sizeof(tempAddr));
325                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
326                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT B.ADDRESS_VAL FROM %s A, %s B WHERE A.CONV_ID = B.CONV_ID AND A.CONV_ID = %d;"
327                                 , MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, threadId);
328
329                 err = dbHandle->prepareQuery(sqlQuery);
330
331                 if (err == MSG_SUCCESS) {
332                         err = dbHandle->stepQuery();
333
334                         if (err == MSG_ERR_DB_ROW) {
335                                 snprintf(tempAddr, sizeof(tempAddr), "%s", dbHandle->columnText(0));
336                                 MSG_SEC_DEBUG("Updated address = %s", tempAddr);
337                                 MsgDeleteReportNotification(tempAddr);
338                                 err = MSG_SUCCESS;
339                         }
340                 }
341
342                 dbHandle->finalizeQuery();
343         }
344
345         // Make Event Data
346         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_THREAD_READ, err, (void**)ppEvent);
347
348         return eventSize;
349
350 }
351
352
353 int MsgUpdateProtectedStatusHandler(const MSG_CMD_S *pCmd, char **ppEvent)
354 {
355         msg_error_t err = MSG_SUCCESS;
356
357         int eventSize = 0;
358
359         msg_message_id_t msgId;
360         bool protectedStatus;
361
362         memcpy(&msgId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_id_t));
363         memcpy(&protectedStatus, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), sizeof(bool));
364
365         // Update Protected Status
366         err = MsgStoUpdateProtectedStatus(msgId, protectedStatus);
367
368         if (err == MSG_SUCCESS)
369         {
370                 MSG_DEBUG("Command Handle Success : MsgStoUpdateProtectedStatus()");
371         }
372         else
373         {
374                 MSG_DEBUG("Command Handle Fail : MsgStoUpdateProtectedStatus()");
375         }
376
377         //storage change CB
378         msg_id_list_s msgIdList;
379         msg_message_id_t msgIds[1];
380         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
381
382         msgIdList.nCount = 1;
383         msgIds[0] = msgId;
384         msgIdList.msgIdList = msgIds;
385
386         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
387
388         // Make Event Data
389         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_PROTECTED, err, (void**)ppEvent);
390
391         return eventSize;
392 }
393
394
395 int MsgDeleteMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
396 {
397         msg_error_t err = MSG_SUCCESS;
398
399         int eventSize = 0;
400
401         int extId = 0;
402
403         msg_message_id_t* msgId = (msg_message_id_t*)pCmd->cmdData;
404
405         MsgStoGetSyncMLExtId(*msgId, &extId);
406
407         // Delete Message
408         err = MsgStoDeleteMessage(*msgId, true);
409
410         if (err == MSG_SUCCESS) {
411                 MSG_DEBUG("Command Handle Success : MsgStoDeleteMessage()");
412
413                 if(extId > 0) {
414                         // broadcast to listener threads, here
415                         MsgTransactionManager::instance()->broadcastSyncMLMsgOperationCB(err, -1, extId);
416                 }
417
418                 msg_id_list_s msgIdList;
419                 msg_message_id_t msgIds[1];
420                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
421
422                 msgIdList.nCount = 1;
423                 msgIds[0] = *msgId;
424                 msgIdList.msgIdList = msgIds;
425
426                 MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_DELETE, &msgIdList);
427         } else {
428                 MSG_DEBUG("Command Handle Fail : MsgStoDeleteMessage()");
429         }
430
431         // Make Event Data
432         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELETE_MSG, err, (void**)ppEvent);
433
434         return eventSize;
435 }
436
437
438 int MsgDeleteAllMessageInFolderHandler(const MSG_CMD_S *pCmd, char **ppEvent)
439 {
440         msg_error_t err = MSG_SUCCESS;
441
442         int eventSize = 0;
443
444         msg_folder_id_t* folderId = (msg_folder_id_t*)pCmd->cmdData;
445
446         bool bOnlyDB;
447         memcpy(&bOnlyDB, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_folder_id_t)), sizeof(bool));
448
449
450         msg_id_list_s msgIdList;
451         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
452
453         // Delete Message
454         err = MsgStoDeleteAllMessageInFolder(*folderId, bOnlyDB, &msgIdList);
455
456         if (err == MSG_SUCCESS) {
457                 MSG_DEBUG("Command Handle Success : MsgStoDeleteAllMessageInFolder()");
458                 MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_DELETE, &msgIdList);
459                 if(msgIdList.msgIdList != NULL)
460                         delete [] (char*)msgIdList.msgIdList;
461         } else {
462                 MSG_DEBUG("Command Handle Fail : MsgStoDeleteAllMessageInFolder()");
463         }
464
465         // Make Event Data
466         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELALL_MSGINFOLDER, err, (void**)ppEvent);
467
468         return eventSize;
469
470 }
471
472
473 int MsgDeleteMessageByListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
474 {
475         msg_error_t err = MSG_SUCCESS;
476
477         int eventSize = 0;
478
479         msg_id_list_s msgIdList;
480         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
481
482         msgIdList.nCount = *((int *)pCmd->cmdData);
483
484         MSG_DEBUG("msgIdList.nCount [%d]", msgIdList.nCount);
485
486         msg_message_id_t msgIds[msgIdList.nCount];
487         memset(msgIds, 0x00, sizeof(msgIds));
488
489         msgIdList.msgIdList = msgIds;
490
491         for (int i=0; i<msgIdList.nCount; i++) {
492                 msgIds[i] = *(msg_message_id_t *)(((char*)pCmd->cmdData) + (sizeof(int)*(i+1)));
493         }
494
495         // Delete Message
496         err = MsgStoDeleteMessageByList(&msgIdList);
497
498         if (err == MSG_SUCCESS) {
499                 MSG_DEBUG("Command Handle Success : MsgStoDeleteMessageByList()");
500                 MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_DELETE, &msgIdList);
501         } else {
502                 MSG_DEBUG("Command Handle Fail : MsgStoDeleteMessageByList()");
503         }
504
505         // Make Event Data
506         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELETE_MESSAGE_BY_LIST, err, (void**)ppEvent);
507
508         return eventSize;
509 }
510
511
512 int MsgMoveMessageToFolderHandler(const MSG_CMD_S *pCmd, char **ppEvent)
513 {
514         msg_error_t err = MSG_SUCCESS;
515
516         int eventSize = 0;
517
518         msg_message_id_t msgId;
519         msg_folder_id_t folderId;
520
521         memcpy(&msgId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_id_t));
522         memcpy(&folderId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), sizeof(msg_folder_id_t));
523
524         // Move Message
525         err = MsgStoMoveMessageToFolder(msgId, folderId);
526
527         if (err == MSG_SUCCESS) {
528                 MSG_DEBUG("Command Handle Success : MsgStoMoveMessageToFolder()");
529
530                 msg_id_list_s msgIdList;
531                 msg_message_id_t msgIds[1];
532                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
533
534                 msgIdList.nCount = 1;
535                 msgIds[0] = msgId;
536                 msgIdList.msgIdList = msgIds;
537
538                 MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
539         } else {
540                 MSG_DEBUG("Command Handle Fail : MsgStoMoveMessageToFolder()");
541         }
542
543         // Make Event Data
544         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_MOVE_MSGTOFOLDER, err, (void**)ppEvent);
545
546         return eventSize;
547 }
548
549
550 int MsgMoveMessageToStorageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
551 {
552         msg_error_t err = MSG_SUCCESS;
553
554         int eventSize = 0;
555
556         msg_message_id_t msgId;
557         msg_storage_id_t storageId;
558
559         memcpy(&msgId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_id_t));
560         memcpy(&storageId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_id_t)), sizeof(msg_storage_id_t));
561
562         // Move Message
563         err = MsgStoMoveMessageToStorage(msgId, storageId);
564
565         if (err == MSG_SUCCESS)
566         {
567                 MSG_DEBUG("Command Handle Success : MsgStoMoveMessageToStorage()");
568         }
569         else
570         {
571                 MSG_DEBUG("Command Handle Fail : MsgStoMoveMessageToStorage()");
572         }
573
574         // Make Event Data
575         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_MOVE_MSGTOSTORAGE, err, (void**)ppEvent);
576
577         return eventSize;
578 }
579
580
581 int MsgCountMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
582 {
583         msg_error_t err = MSG_SUCCESS;
584
585         char* encodedData = NULL;
586         AutoPtr<char> buf(&encodedData);
587
588         int dataSize = 0, eventSize = 0;
589
590         // Get Folder ID
591         msg_folder_id_t* folderId = (msg_folder_id_t*)pCmd->cmdData;
592
593         // Get Message Count
594         MSG_COUNT_INFO_S countInfo;
595
596         err = MsgStoCountMessage(*folderId, &countInfo);
597
598         if (err == MSG_SUCCESS)
599         {
600                 MSG_DEBUG("Command Handle Success : MsgStoCountMessage()");
601
602                 // Encoding Messaging Count Data
603                 dataSize = MsgEncodeCountInfo(&countInfo, &encodedData);
604         }
605         else
606         {
607                 MSG_DEBUG("Command Handle Fail : MsgStoCountMessage()");
608         }
609
610         // Make Event Data
611         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_COUNT_MSG, err, (void**)ppEvent);
612
613         return eventSize;
614 }
615
616
617 int MsgCountMsgByTypeHandler(const MSG_CMD_S *pCmd, char **ppEvent)
618 {
619         msg_error_t err = MSG_SUCCESS;
620
621         char* encodedData = NULL;
622         AutoPtr<char> buf(&encodedData);
623
624         int dataSize = 0, eventSize = 0;
625
626         // Get Folder ID
627         MSG_MESSAGE_TYPE_S* pMsgType = (MSG_MESSAGE_TYPE_S*)pCmd->cmdData;
628
629         int nMsgCnt = 0;
630
631         // Get Message Count
632         err = MsgStoCountMsgByType(pMsgType, &nMsgCnt);
633
634         if (err == MSG_SUCCESS)
635         {
636                 MSG_DEBUG("Command Handle Success : MsgStoCountMsgByType()");
637
638                 // Encoding Messaging Count Data
639                 dataSize = MsgEncodeCountByMsgType(nMsgCnt, &encodedData);
640         }
641         else
642         {
643                 MSG_DEBUG("Command Handle Fail : MsgStoCountMsgByType()");
644         }
645
646         // Make Event Data
647         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_COUNT_BY_MSGTYPE, err, (void**)ppEvent);
648
649         return eventSize;
650 }
651
652
653 int MsgGetMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
654 {
655         msg_error_t err = MSG_SUCCESS;
656
657         // Get Message ID
658         msg_message_id_t* msgId = (msg_message_id_t*)pCmd->cmdData;
659
660         char* encodedData = NULL;
661         AutoPtr<char> buf(&encodedData);
662
663         int dataSize = 0, eventSize = 0;
664
665         // Get Message
666         MSG_MESSAGE_INFO_S msgInfo;
667         MSG_SENDINGOPT_INFO_S sendOptInfo;
668
669         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
670         memset(&sendOptInfo, 0x00, sizeof(MSG_SENDINGOPT_INFO_S));
671
672         msgInfo.addressList = NULL;
673         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
674
675         err = MsgStoGetMessage(*msgId, &msgInfo, &sendOptInfo);
676
677         if (err == MSG_SUCCESS)
678         {
679                 MSG_DEBUG("Command Handle Success : MsgStoGetMessage()");
680
681                 // Encoding Message Info  Data
682                 dataSize = MsgEncodeMsgInfo(&msgInfo, &sendOptInfo, &encodedData);
683         }
684         else
685         {
686                 MSG_DEBUG("Command Handle Fail : MsgStoGetMessage()");
687         }
688
689 //      MsgSoundPlayStop();
690
691         // Make Event Data
692         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_MSG, err, (void**)ppEvent);
693
694         return eventSize;
695 }
696
697
698 int MsgGetFolderViewListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
699 {
700         // Get Folder ID
701         msg_folder_id_t folderId;
702         MSG_SORT_RULE_S sortRule;
703
704         memcpy(&folderId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_folder_id_t));
705         memcpy(&sortRule, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_folder_id_t)), sizeof(MSG_SORT_RULE_S));
706
707         char* encodedData = NULL;
708         AutoPtr<char> buf(&encodedData);
709
710         int dataSize = 0, eventSize = 0;
711
712 #if 0
713         // Get Message Common Info
714         msg_struct_list_s folderViewList;
715
716 //      err = MsgStoGetFolderViewList(folderId, &sortRule, &folderViewList);
717
718         if (err == MSG_SUCCESS)
719         {
720                 MSG_DEBUG("Command Handle Success : MsgStoGetFolderViewList()");
721
722                 // Encoding Folder View List Data
723 //              dataSize = MsgEncodeFolderViewList(&folderViewList, &encodedData);
724
725                 MSG_DEBUG("dataSize [%d]", dataSize);
726
727                 if (folderViewList.msg_struct_info) {
728                         msg_struct_s *msg_struct;
729                         for (int i = 0; i < folderViewList.nCount; i++) {
730                                 msg_struct = (msg_struct_s *)folderViewList.msg_struct_info[i];
731                                 delete (MSG_MESSAGE_HIDDEN_S *)msg_struct->data;
732                                 delete msg_struct;
733                         }
734                         g_free(folderViewList.msg_struct_info);
735                 }
736         }
737         else
738         {
739                 MSG_DEBUG("Command Handle Fail : MsgStoGetFolderViewList()");
740                 return err;
741         }
742 #endif
743         // Make Event Data
744         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_FOLDERVIEWLIST, MSG_SUCCESS, (void**)ppEvent);
745
746         return eventSize;
747 }
748
749
750 int MsgAddFolderHandler(const MSG_CMD_S *pCmd, char **ppEvent)
751 {
752         msg_error_t err = MSG_SUCCESS;
753
754         int eventSize = 0;
755
756         // Get Folder Info
757         MSG_FOLDER_INFO_S* pFolderInfo = (MSG_FOLDER_INFO_S*)pCmd->cmdData;
758
759         // Add Folder
760         err = MsgStoAddFolder(pFolderInfo);
761
762         if (err == MSG_SUCCESS)
763         {
764                 MSG_DEBUG("Command Handle Success : MsgStoAddFolder()");
765         }
766         else
767         {
768                 MSG_DEBUG("Command Handle Fail : MsgStoAddFolder()");
769         }
770
771         // Make Event Data
772         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_ADD_FOLDER, err, (void**)ppEvent);
773
774         return eventSize;
775 }
776
777
778 int MsgUpdateFolderHandler(const MSG_CMD_S *pCmd, char **ppEvent)
779 {
780         msg_error_t err = MSG_SUCCESS;
781
782         int eventSize = 0;
783
784         // Get Folder Info
785         MSG_FOLDER_INFO_S* pFolderInfo = (MSG_FOLDER_INFO_S*)pCmd->cmdData;
786
787         // Update Folder
788         err = MsgStoUpdateFolder(pFolderInfo);
789
790         if (err == MSG_SUCCESS)
791         {
792                 MSG_DEBUG("Command Handle Success : MsgStoUpdateFolder()");
793         }
794         else
795         {
796                 MSG_DEBUG("Command Handle Fail : MsgStoUpdateFolder()");
797         }
798
799         // Make Event Data
800         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_FOLDER, err, (void**)ppEvent);
801
802         return eventSize;
803 }
804
805
806 int MsgDeleteFolderHandler(const MSG_CMD_S *pCmd, char **ppEvent)
807 {
808         msg_error_t err = MSG_SUCCESS;
809
810         int eventSize = 0;
811
812         // Get Folder Info
813         msg_folder_id_t* pFolderInfo = (msg_folder_id_t*)pCmd->cmdData;
814
815         // Delete Folder
816         err = MsgStoDeleteFolder(*pFolderInfo);
817
818         if (err == MSG_SUCCESS)
819         {
820                 MSG_DEBUG("Command Handle Success : MsgStoDeleteFolder()");
821         }
822         else
823         {
824                 MSG_DEBUG("Command Handle Fail : MsgStoDeleteFolder()");
825         }
826
827         // Make Event Data
828         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELETE_FOLDER, err, (void**)ppEvent);
829
830         return eventSize;
831 }
832
833
834 int MsgGetFolderListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
835 {
836         msg_error_t err = MSG_SUCCESS;
837
838         char* encodedData = NULL;
839         AutoPtr<char> buf(&encodedData);
840
841         int dataSize = 0, eventSize = 0;
842
843         // Get Storage List
844         msg_struct_list_s folderList;
845
846         err = MsgStoGetFolderList(&folderList);
847
848         if (err == MSG_SUCCESS)
849         {
850                 MSG_DEBUG("Command Handle Success : MsgStoGetFolderList()");
851
852                 // Encoding Folder List Data
853                 dataSize = MsgEncodeFolderList(&folderList, &encodedData);
854
855                 if (folderList.msg_struct_info) {
856                         msg_struct_s *msg_struct;
857                         for (int i = 0; i < folderList.nCount; i++) {
858                                 msg_struct = (msg_struct_s *)folderList.msg_struct_info[i];
859                                 delete (MSG_FOLDER_INFO_S *)msg_struct->data;
860                                 delete msg_struct;
861                         }
862                         g_free(folderList.msg_struct_info);
863                 }
864         }
865         else
866         {
867                 MSG_DEBUG("Command Handle Fail : MsgStoGetFolderList()");
868         }
869
870         // Make Event Data
871         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_FOLDERLIST, err, (void**)ppEvent);
872
873         return eventSize;
874 }
875
876
877 int MsgInitSimBySatHandler(const MSG_CMD_S *pCmd, char **ppEvent)
878 {
879         msg_error_t err = MSG_SUCCESS;
880
881         int eventSize = 0;
882
883         // Sim Init - Later
884         //Run msg-init-app
885
886         // Make Event Data
887         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_PLG_INIT_SIM_BY_SAT, err, (void**)ppEvent);
888
889         return eventSize;
890 }
891
892
893 int MsgGetMsgTypeHandler(const MSG_CMD_S *pCmd, char **ppEvent)
894 {
895         msg_error_t err = MSG_SUCCESS;
896
897         char* encodedData = NULL;
898         AutoPtr<char> buf(&encodedData);
899
900
901         int dataSize = 0, eventSize = 0;
902
903         // Get Message ID
904         msg_message_id_t msgId;
905
906         memcpy(&msgId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_id_t));
907
908         // Get Msg Type
909         MSG_MESSAGE_TYPE_S msgType;
910
911         err = MsgStoGetMsgType(msgId, &msgType);
912
913         if (err == MSG_SUCCESS)
914         {
915                 MSG_DEBUG("Command Handle Success : MsgStoGetMsgType()");
916
917                 // Encoding Storage List Data
918                 dataSize = MsgEncodeMsgType(&msgType, &encodedData);
919
920         }
921         else
922         {
923                 MSG_DEBUG("Command Handle Fail : MsgStoGetMsgType()");
924         }
925
926         // Make Event Data
927         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_MSG_TYPE, err, (void**)ppEvent);
928
929         return eventSize;
930 }
931
932
933 int MsgGetThreadViewListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
934 {
935
936         msg_error_t err = MSG_SUCCESS;
937
938         MSG_SORT_RULE_S sortRule = {0};
939
940         memcpy(&sortRule, pCmd->cmdData, sizeof(MSG_SORT_RULE_S));
941
942         char* encodedData = NULL;
943         AutoPtr<char> buf(&encodedData);
944
945         int dataSize = 0, eventSize = 0;
946 #if 0
947         // Get Thread View List
948         msg_struct_list_s msgThreadViewList;
949
950         //err = MsgStoGetThreadViewList(&sortRule, &msgThreadViewList);
951
952         if (err == MSG_SUCCESS)
953         {
954                 MSG_DEBUG("Command Handle Success : MsgStoGetThreadViewList()");
955
956                 // Encoding Folder View List Data
957                 dataSize = MsgEncodeThreadViewList(&msgThreadViewList, &encodedData);
958
959                 MSG_DEBUG("dataSize [%d]", dataSize);
960
961                 if (msgThreadViewList.msg_struct_info) {
962                         msg_struct_s *msg_struct;
963                         for (int i = 0; i < folderList.nCount; i++) {
964                                 msg_struct = (msg_struct_s *)msgThreadViewList.msg_struct_info[i];
965                                 delete (MSG_THREAD_VIEW_S *)msg_struct->data;
966                                 delete msg_struct;
967                         }
968                         g_free(msgThreadViewList.msg_struct_info);
969                 }
970         }
971         else
972         {
973                 MSG_DEBUG("Command Handle Fail : MsgStoGetThreadViewList()");
974                 return err;
975         }
976 #endif
977         // Make Event Data
978         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_THREADVIEWLIST, err, (void**)ppEvent);
979
980         return eventSize;
981 }
982
983
984 int MsgGetConversationViewListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
985 {
986         msg_error_t err = MSG_SUCCESS;
987
988         msg_thread_id_t threadId;
989
990         memcpy(&threadId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_thread_id_t));
991
992         char* encodedData = NULL;
993         AutoPtr<char> buf(&encodedData);
994
995         int dataSize = 0, eventSize = 0;
996 #if 0
997         msg_struct_list_s convViewList;
998
999         //err = MsgStoGetConversationViewList(threadId, &convViewList);
1000
1001         if (err == MSG_SUCCESS)
1002         {
1003                 MSG_DEBUG("Command Handle Success : MsgStoGetConversationViewList()");
1004
1005                 // Encoding Folder View List Data
1006                 dataSize = MsgEncodeConversationViewList(&convViewList, &encodedData);
1007
1008                 MSG_DEBUG("dataSize [%d]", dataSize);
1009
1010                 if (convViewList.msg_struct_info) {
1011                         msg_struct_s *msg_struct;
1012                         for (int i = 0; i < convViewList.nCount; i++) {
1013                                 msg_struct = (msg_struct_s *)convViewList.msg_struct_info[i];
1014                                 delete (MSG_CONVERSATION_VIEW_S *)msg_struct->data;
1015                                 delete msg_struct;
1016                         }
1017                         g_free(convViewList.msg_struct_info);
1018                 }
1019         }
1020         else
1021         {
1022                 MSG_DEBUG("Command Handle Fail : MsgStoGetConversationViewList()");
1023         }
1024 #endif
1025         // Make Event Data
1026         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_CONVERSATIONVIEWLIST, err, (void**)ppEvent);
1027
1028         return eventSize;
1029 }
1030
1031
1032 int MsgDeleteThreadMessageListHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1033 {
1034         msg_error_t err = MSG_SUCCESS;
1035
1036         msg_thread_id_t threadId;
1037         bool bIncludeProtect = false;
1038         bool isSyncMLMsg = false;
1039
1040         memcpy(&threadId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_thread_id_t));
1041         memcpy(&bIncludeProtect, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_thread_id_t)), sizeof(bool));
1042
1043         int eventSize = 0;
1044
1045         isSyncMLMsg = MsgStoCheckSyncMLMsgInThread(threadId);
1046
1047         msg_id_list_s msgIdList;
1048         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
1049
1050         err = MsgStoDeleteThreadMessageList(threadId, bIncludeProtect, &msgIdList);
1051
1052         if (err == MSG_SUCCESS) {
1053                 MSG_DEBUG("Command Handle Success : MsgStoDeleteThreadMessageList()");
1054
1055                 if(isSyncMLMsg == true) {
1056                         // broadcast to listener threads, here
1057                         MsgTransactionManager::instance()->broadcastSyncMLMsgOperationCB(err, -1, -1);
1058                 }
1059
1060                 MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_DELETE, &msgIdList);
1061                 if(msgIdList.msgIdList != NULL)
1062                         delete [] (char*)msgIdList.msgIdList;
1063         }
1064         else
1065         {
1066                 MSG_DEBUG("Command Handle Fail : MsgStoDeleteThreadMessageList()");
1067                 if(msgIdList.msgIdList != NULL)
1068                         delete [] (char*)msgIdList.msgIdList;
1069         }
1070
1071         // Make Event Data
1072         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELETE_THREADMESSAGELIST, err, (void**)ppEvent);
1073
1074         return eventSize;
1075 }
1076
1077
1078 int MsgCountMsgByContactHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1079 {
1080         msg_error_t err = MSG_SUCCESS;
1081
1082         // Get From address
1083         MSG_THREAD_LIST_INDEX_S addrInfo;
1084
1085         memcpy(&addrInfo, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(MSG_THREAD_LIST_INDEX_S));
1086
1087         char* encodedData = NULL;
1088         AutoPtr<char> buf(&encodedData);
1089
1090         int dataSize = 0, eventSize = 0;
1091
1092         // Get Message Common Info
1093         MSG_THREAD_COUNT_INFO_S threadCountInfo = {0};
1094
1095         err = MsgStoCountMsgByContact(&addrInfo, &threadCountInfo);
1096
1097         if (err == MSG_SUCCESS)
1098         {
1099                 MSG_DEBUG("Command Handle Success : MsgStoCountMsgByContact()");
1100
1101                 // Encoding Folder View List Data
1102                 dataSize = MsgEncodeMsgGetContactCount(&threadCountInfo, &encodedData);
1103
1104                 MSG_DEBUG("dataSize [%d]", dataSize);
1105         }
1106         else
1107         {
1108                 MSG_DEBUG("Command Handle Fail : MsgStoCountMsgByContact()");
1109         }
1110
1111         // Make Event Data
1112         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_CONTACT_COUNT, err, (void**)ppEvent);
1113
1114         return eventSize;
1115 }
1116
1117
1118 int MsgGetQuickPanelDataHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1119 {
1120         msg_error_t err = MSG_SUCCESS;
1121
1122         // Get Message ID
1123         msg_quickpanel_type_t* type = (msg_quickpanel_type_t*)pCmd->cmdData;
1124
1125         char* encodedData = NULL;
1126         AutoPtr<char> buf(&encodedData);
1127
1128         int dataSize = 0, eventSize = 0;
1129
1130         // Get Message
1131         MSG_MESSAGE_INFO_S msgInfo;
1132         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1133
1134         msgInfo.addressList = NULL;
1135         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
1136
1137         err = MsgStoGetQuickPanelData(*type, &msgInfo);
1138
1139         if (err == MSG_SUCCESS)
1140         {
1141                 MSG_DEBUG("Command Handle Success : MsgStoGetQuickPanelData()");
1142
1143                 // Encoding Message Info Data
1144                 dataSize = MsgEncodeMsgInfo(&msgInfo, &encodedData);
1145         }
1146         else
1147         {
1148                 MSG_DEBUG("Command Handle Fail : MsgStoGetQuickPanelData()");
1149         }
1150
1151         // Make Event Data
1152         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_QUICKPANEL_DATA, err, (void**)ppEvent);
1153
1154         return eventSize;
1155 }
1156
1157
1158 int MsgResetDatabaseHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1159 {
1160         msg_error_t err = MSG_SUCCESS;
1161
1162         int eventSize = 0;
1163
1164         // Reset DB
1165         err = MsgStoResetDatabase();
1166
1167         if (err == MSG_SUCCESS)
1168                 MSG_DEBUG("Command Handle Success : MsgStoResetDatabase()");
1169         else
1170                 MSG_DEBUG("Command Handle Fail : MsgStoResetDatabase()");
1171
1172         // Make Event Data
1173         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_RESET_DB, err, (void**)ppEvent);
1174
1175         return eventSize;
1176 }
1177
1178
1179 int MsgGetMemSizeHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1180 {
1181         msg_error_t err = MSG_SUCCESS;
1182
1183         char* encodedData = NULL;
1184         AutoPtr<char> buf(&encodedData);
1185
1186         int dataSize = 0, eventSize = 0;
1187
1188         // Get Memory size
1189         unsigned int memsize = 0;
1190
1191         memsize = MsgDu(MSG_DATA_ROOT_PATH);
1192
1193         dataSize = MsgEncodeMemSize(&memsize, &encodedData);
1194
1195         // Make Event Data
1196         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_MEMSIZE, err, (void**)ppEvent);
1197
1198         return eventSize;
1199 }
1200
1201
1202 int MsgBackupMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1203 {
1204         msg_error_t err = MSG_SUCCESS;
1205
1206         int eventSize = 0;
1207         char path[MSG_FILEPATH_LEN_MAX+1] = {0,};
1208         msg_message_backup_type_t type;
1209
1210         memcpy(&type, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_message_backup_type_t));
1211         memcpy(&path, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(msg_message_backup_type_t)), sizeof(path));
1212
1213         MSG_DEBUG("type = %d, path = %s", type, path);
1214
1215         err = MsgStoBackupMessage(type, path);
1216         if (err == MSG_SUCCESS)
1217                 MSG_DEBUG("Command Handle Success : MsgBackupMessageHandler()");
1218         else
1219                 MSG_DEBUG("Command Handle Fail : MsgBackupMessageHandler()");
1220
1221         // Make Event Data
1222         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_BACKUP_MESSAGE, err, (void**)ppEvent);
1223
1224         return eventSize;
1225 }
1226
1227
1228 int MsgRestoreMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1229 {
1230         msg_error_t err = MSG_SUCCESS;
1231
1232         int eventSize = 0;
1233         msg_id_list_s *msgIdList = NULL;
1234         char path[MSG_FILEPATH_LEN_MAX+1] = {0,};
1235         memcpy(&path, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(path));
1236         MSG_DEBUG("path = %s", path);
1237
1238         // Reset DB
1239         err = MsgStoRestoreMessage(path, &msgIdList);
1240
1241         if (err == MSG_SUCCESS) {
1242                 MSG_DEBUG("Command Handle Success : MsgStoRestoreMessage()");
1243
1244                 if (msgIdList) {
1245                         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_INSERT, msgIdList);
1246
1247                         if (msgIdList->msgIdList)
1248                                 free(msgIdList->msgIdList);
1249
1250                         free(msgIdList);
1251                         msgIdList = NULL;
1252                 } else {
1253                         MSG_DEBUG("Not Exist msg id list to restore");
1254                 }
1255
1256         } else
1257                 MSG_DEBUG("Command Handle Fail : MsgStoRestoreMessage()");
1258
1259         // Make Event Data
1260         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_RESTORE_MESSAGE, err, (void**)ppEvent);
1261
1262         return eventSize;
1263 }
1264
1265
1266 int MsgGetReportStatusHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1267 {
1268         msg_error_t err = MSG_SUCCESS;
1269
1270         // Get Message ID
1271         msg_message_id_t* msgId = (msg_message_id_t*)pCmd->cmdData;
1272         MSG_MESSAGE_TYPE_S msgType = {0,};
1273
1274         char* encodedData = NULL;
1275         AutoPtr<char> buf(&encodedData);
1276
1277         int dataSize = 0, eventSize = 0;
1278
1279         MSG_REPORT_STATUS_INFO_S *reportStatus = NULL;
1280         int report_count = 0;
1281
1282         if (MsgStoGetMsgType(*msgId, &msgType) != MSG_SUCCESS)
1283                 MSG_DEBUG("MsgStoGetMsgType is failed");
1284
1285         if (msgType.mainType == MSG_SMS_TYPE) {
1286                 err = MsgStoGetSmsReportStatus(*msgId, &report_count, &reportStatus);
1287         } else if (msgType.mainType == MSG_MMS_TYPE) {
1288                 err = MsgStoGetMmsReportStatus(*msgId, &report_count, &reportStatus);
1289         } else {
1290                 MSG_DEBUG("Message Type is unknown!");
1291                 err = MSG_ERR_INVALID_MSG_TYPE;
1292         }
1293
1294         if (err == MSG_SUCCESS)
1295         {
1296                 MSG_DEBUG("Command Handle Success : MsgGetReportStatusHandler()");
1297
1298                 // Encoding Report Status Data
1299                 dataSize = MsgEncodeReportStatus(reportStatus, report_count, &encodedData);
1300         }
1301         else
1302         {
1303                 MSG_DEBUG("Command Handle Fail : MsgGetReportStatusHandler()");
1304         }
1305
1306         // Make Event Data
1307         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_REPORT_STATUS, err, (void**)ppEvent);
1308
1309         return eventSize;
1310 }
1311
1312
1313 int MsgGetThreadIdByAddressHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1314 {
1315         msg_error_t err = MSG_SUCCESS;
1316
1317         MSG_MESSAGE_INFO_S msgInfo;
1318         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1319
1320         msgInfo.addressList = NULL;
1321         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
1322
1323         int *addrCnt = (int *)pCmd->cmdData;
1324         MSG_DEBUG("*addrCnt [%d]", *addrCnt);
1325
1326         msgInfo.nAddressCnt = *addrCnt;
1327
1328         msgInfo.addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * msgInfo.nAddressCnt];
1329
1330         for(int i=0; i<msgInfo.nAddressCnt; i++)
1331                 memcpy(&msgInfo.addressList[i], (MSG_ADDRESS_INFO_S *)(pCmd->cmdData+sizeof(int)+(sizeof(MSG_ADDRESS_INFO_S)*i)), sizeof(MSG_ADDRESS_INFO_S));
1332
1333         char* encodedData = NULL;
1334         AutoPtr<char> buf(&encodedData);
1335
1336         int dataSize = 0, eventSize = 0;
1337
1338         msg_thread_id_t threadId;
1339
1340         err = MsgStoGetThreadIdByAddress(&msgInfo, &threadId);
1341
1342         if (err == MSG_SUCCESS)
1343         {
1344                 MSG_DEBUG("threadId [%d]", threadId);
1345                 MSG_DEBUG("Command Handle Success : MsgGetThreadIdByAddressHandler()");
1346
1347                 // Encoding threadId Data
1348                 dataSize = MsgEncodeThreadId(&threadId, &encodedData);
1349         }
1350         else
1351         {
1352                 MSG_DEBUG("Command Handle Fail : MsgGetThreadIdByAddressHandler()");
1353         }
1354
1355         // Make Event Data
1356         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_THREAD_ID_BY_ADDRESS, err, (void**)ppEvent);
1357
1358         return eventSize;
1359 }
1360
1361
1362 int MsgGetThreadInfoHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1363 {
1364         msg_error_t err = MSG_SUCCESS;
1365
1366         char* encodedData = NULL;
1367         AutoPtr<char> buf(&encodedData);
1368
1369         int dataSize = 0;
1370         int eventSize = 0;
1371
1372         msg_thread_id_t threadId;
1373
1374         memcpy(&threadId, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(msg_thread_id_t));
1375
1376         MSG_THREAD_VIEW_S threadInfo;
1377         memset(&threadInfo, 0x00, sizeof(threadInfo));
1378
1379         err = MsgStoGetThreadInfo(threadId, &threadInfo);
1380
1381         if (err == MSG_SUCCESS) {
1382                 MSG_DEBUG("Command Handle Success : MsgStoGetThreadInfo()");
1383
1384                 // Encoding thread Info Data
1385                 dataSize = MsgEncodeThreadInfo(&threadInfo, &encodedData);
1386         } else {
1387                 MSG_DEBUG("Command Handle Fail : MsgStoGetThreadInfo()");
1388         }
1389
1390         // Make Event Data
1391         eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_GET_THREAD_INFO, err, (void**)ppEvent);
1392
1393         return eventSize;
1394 }
1395
1396
1397 #ifdef MMS_REPORT_OPERATIONS
1398 int MsgCheckReadReportRequestedHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1399 {
1400         MSG_DEBUG();
1401         msg_error_t err = MSG_SUCCESS;
1402
1403         char* encodedData = NULL;
1404         AutoPtr<char> buf(&encodedData);
1405
1406         int dataSize = 0, eventSize = 0;
1407
1408         // Get Message ID
1409         msg_message_id_t* MsgId = (msg_message_id_t*)pCmd->cmdData;
1410
1411         // Check ReadReport Is Requested
1412         bool    bReadReportRequested;
1413
1414         bReadReportRequested = MsgStoCheckReadReportRequested(*MsgId);
1415         if (err == MSG_SUCCESS)
1416         {
1417                 MSG_DEBUG("Command Handle Success : MsgStoCheckSendReadReport()");
1418
1419                 // Encoding ReadReportIsSent Data
1420                 dataSize = MsgEncodeReadReportRequested(bReadReportRequested, &encodedData);
1421         }
1422         else
1423         {
1424                 MSG_DEBUG("Command Handle Fail : MsgStoCheckReadReportIsSent()");
1425         }
1426
1427         // Make Event Data
1428         eventSize = MsgMakeStorageEvent(encodedData, dataSize, MSG_EVENT_PLG_CHECK_READ_REPORT_REQUESTED, err, (void**)ppEvent);
1429
1430         return eventSize;
1431 }
1432
1433
1434 int MsgCheckReadReportIsSentHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1435 {
1436         MSG_DEBUG();
1437         msg_error_t err = MSG_SUCCESS;
1438
1439         char* encodedData = NULL;
1440         AutoPtr<char> buf(&encodedData);
1441
1442         int dataSize = 0, eventSize = 0;
1443
1444         // Get Message ID
1445         msg_message_id_t* MsgId = (msg_message_id_t*)pCmd->cmdData;
1446
1447         // Check ReadReport Is Sent
1448         bool    bReadReportIsSent;
1449
1450         MSG_DEBUG("#### MSGID = %d ####", *MsgId);
1451
1452         bReadReportIsSent = MsgStoCheckReadReportIsSent(*MsgId);
1453         MSG_DEBUG("######## 1. bReadStatusIsSent = %d #######", bReadReportIsSent);
1454         if (err == MSG_SUCCESS)
1455         {
1456                 MSG_DEBUG("Command Handle Success : MsgStoCheckReadReportIsSent()");
1457
1458                 // Encoding ReadReportIsSent Data
1459                 dataSize = MsgEncodeReadReportIsSent(bReadReportIsSent, &encodedData);
1460         }
1461         else
1462         {
1463                 MSG_DEBUG("Command Handle Fail : MsgStoCheckReadReportIsSent()");
1464         }
1465
1466         // Make Event Data
1467         eventSize = MsgMakeStorageEvent(encodedData, dataSize, MSG_EVENT_PLG_CHECK_READ_REPORT_IS_SENT, err, (void**)ppEvent);
1468
1469         return eventSize;
1470 }
1471
1472
1473 int MsgSetReadReportSendStatusHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1474 {
1475         MSG_DEBUG();
1476         msg_error_t err = MSG_SUCCESS;
1477
1478         char* encodedData = NULL;
1479         AutoPtr<char> buf(&encodedData);
1480
1481         int dataSize = 0, eventSize = 0;
1482
1483         // Get Message ID
1484         msg_message_id_t msgId;
1485         int     readReportSendStatus;
1486
1487         memcpy(&msgId, (char*)pCmd + sizeof(MSG_CMD_TYPE_T), sizeof(msg_message_id_t));
1488         memcpy(&readReportSendStatus, (char*)pCmd + sizeof(MSG_CMD_TYPE_T) + sizeof(msg_message_id_t), sizeof(int));
1489
1490         // Set Read Report Send Status
1491         err = MsgStoSetReadReportSendStatus(msgId, readReportSendStatus);
1492
1493         if (err == MSG_SUCCESS)
1494         {
1495                 MSG_DEBUG("Command Handle Success : MsgStoUpdateReadStatus()");
1496         }
1497         else
1498         {
1499                 MSG_DEBUG("Command Handle Fail : MsgStoUpdateReadStatus()");
1500         }
1501
1502         // Make Event Data
1503         eventSize = MsgMakeStorageEvent(encodedData, dataSize, MSG_EVENT_PLG_SET_READ_REPORT_SEND_STATUS, err, (void**)ppEvent);
1504
1505         return eventSize;
1506 }
1507
1508
1509 int MsgGetMmsVersionHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1510 {
1511         MSG_DEBUG();
1512         msg_error_t err = MSG_SUCCESS;
1513
1514         char* encodedData = NULL;
1515         AutoPtr<char> buf(&encodedData);
1516
1517         int dataSize = 0, eventSize = 0;
1518
1519         // Get Message ID
1520         msg_message_id_t* MsgId = (msg_message_id_t*)pCmd->cmdData;
1521
1522         // Check ReadReport Is Sent
1523         int     version;
1524
1525         MSG_DEBUG("#### MSGID = %d ####", *MsgId);
1526
1527         version = MsgStoGetMmsVersion(*MsgId);
1528         MSG_DEBUG("######## 1. version = %x #######", version);
1529         if (err == MSG_SUCCESS)
1530         {
1531                 MSG_DEBUG("Command Handle Success : MsgStoCheckReadReportIsSent()");
1532
1533                 // Encoding ReadReportIsSent Data
1534                 dataSize = MsgEncodeMmsVersion(version, &encodedData);
1535         }
1536         else
1537         {
1538                 MSG_DEBUG("Command Handle Fail : MsgStoCheckReadReportIsSent()");
1539         }
1540
1541         // Make Event Data
1542         eventSize = MsgMakeStorageEvent(encodedData, dataSize, MSG_EVENT_PLG_GET_MMS_VERSION, err, (void**)ppEvent);
1543
1544         return eventSize;
1545 }
1546
1547
1548 int MsgGetMmsStatusInfoHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1549 {
1550         MSG_DEBUG();
1551         msg_error_t err = MSG_SUCCESS;
1552
1553         char* encodedData = NULL;
1554         AutoPtr<char> buf(&encodedData);
1555
1556         int dataSize = 0, eventSize = 0;
1557
1558         // Get Message ID
1559         msg_message_id_t* MsgId = (msg_message_id_t*)pCmd->cmdData;
1560
1561         MMS_STATUS_INFO_S mmsStatusInfo;
1562
1563         MSG_DEBUG("#### MSGID = %d ####", *MsgId);
1564
1565         err = MsgStoGetMmsStatusInfo(*MsgId,&mmsStatusInfo);
1566         if (err == MSG_SUCCESS)
1567         {
1568                 MSG_DEBUG("Command Handle Success : MsgGetMmsStatusInfoHandler()");
1569
1570                 // Encoding ReadReportIsSent Data
1571                 dataSize = MsgEncodeMmsStatusInfo(&mmsStatusInfo, &encodedData);
1572         }
1573         else
1574         {
1575                 MSG_DEBUG("Command Handle Fail : MsgGetMmsStatusInfoHandler()");
1576         }
1577
1578         // Make Event Data
1579         eventSize = MsgMakeStorageEvent(encodedData, dataSize, MSG_EVENT_PLG_GET_MMS_STATUS_INFO, err, (void**)ppEvent);
1580
1581         return eventSize;
1582 }
1583 #endif
1584
1585
1586 int MsgAddPushEventHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1587 {
1588         msg_error_t err = MSG_SUCCESS;
1589
1590         int eventSize = 0;
1591
1592         // Get Message Info
1593         MSG_PUSH_EVENT_INFO_S* pPushEvent = (MSG_PUSH_EVENT_INFO_S*)pCmd->cmdData;
1594
1595         // Add Message
1596         err = MsgStoAddPushEvent(pPushEvent);
1597
1598         if (err == MSG_SUCCESS) {
1599                 MSG_DEBUG("Command Handle Success : MsgStoAddPushEvent()");
1600         } else {
1601                 MSG_DEBUG("Command Handle Fail : MsgStoAddPushEvent()");
1602         }
1603
1604         // Make Event Data
1605         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_ADD_PUSH_EVENT, err, (void**)ppEvent);
1606
1607         return eventSize;
1608 }
1609
1610 int MsgDeletePushEventHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1611 {
1612         msg_error_t err = MSG_SUCCESS;
1613
1614         int eventSize = 0;
1615
1616         // Get Message Info
1617         MSG_PUSH_EVENT_INFO_S* pPushEvent = (MSG_PUSH_EVENT_INFO_S*)pCmd->cmdData;
1618
1619         // Add Message
1620         err = MsgStoDeletePushEvent(pPushEvent);
1621
1622         if (err == MSG_SUCCESS) {
1623                 MSG_DEBUG("Command Handle Success : MsgStoDeletePushEvent()");
1624         } else {
1625                 MSG_DEBUG("Command Handle Fail : MsgStoDeletePushEvent()");
1626         }
1627
1628         // Make Event Data
1629         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_DELETE_PUSH_EVENT, err, (void**)ppEvent);
1630
1631         return eventSize;
1632 }
1633
1634 int MsgUpdatePushEventHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1635 {
1636         msg_error_t err = MSG_SUCCESS;
1637
1638
1639         int eventSize = 0;
1640
1641         // Get Message Info
1642         MSG_PUSH_EVENT_INFO_S* pSrc = (MSG_PUSH_EVENT_INFO_S*)pCmd->cmdData;
1643         MSG_PUSH_EVENT_INFO_S* pDst = (MSG_PUSH_EVENT_INFO_S*)(pCmd->cmdData + sizeof(MSG_PUSH_EVENT_INFO_S));
1644
1645         // Add Message
1646         err = MsgStoUpdatePushEvent(pSrc, pDst);
1647
1648         if (err == MSG_SUCCESS) {
1649                 MSG_DEBUG("Command Handle Success : MsgStoUpdatePushEvent()");
1650         } else {
1651                 MSG_DEBUG("Command Handle Fail : MsgStoUpdatePushEvent()");
1652         }
1653
1654         // Make Event Data
1655         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_PUSH_EVENT, err, (void**)ppEvent);
1656
1657         return eventSize;
1658 }
1659
1660 int MsgAddSimMessageHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1661 {
1662         msg_error_t err = MSG_SUCCESS;
1663
1664         int eventSize = 0;
1665
1666         MSG_MESSAGE_INFO_S msgInfo;
1667         MSG_SENDINGOPT_INFO_S sendOptInfo;
1668         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1669         memset(&sendOptInfo, 0x00, sizeof(MSG_SENDINGOPT_INFO_S));
1670
1671         msgInfo.addressList = NULL;
1672         AutoPtr<MSG_ADDRESS_INFO_S> addressListBuf(&msgInfo.addressList);
1673
1674         MsgDecodeMsgInfo((char *)pCmd->cmdData, &msgInfo);
1675
1676         int *simIdList = NULL;
1677         int listSize = 0;
1678         char *offset = NULL;
1679         offset = ((char *)pCmd->cmdData) + sizeof(MSG_MESSAGE_INFO_S) + (sizeof(MSG_ADDRESS_INFO_S) * msgInfo.nAddressCnt);
1680
1681         listSize = *(int*)offset;
1682         offset += sizeof(int);
1683         MSG_DEBUG("listSize [%d]", listSize);
1684
1685         simIdList = (int *)offset;
1686
1687         MSG_DEBUG("simIdList[0] [%d]", simIdList[0]);
1688
1689         // Add Message
1690 #if 0 // Not to broadcast callback here.
1691         bool isReplaceMsg = false;
1692
1693         if (msgInfo.msgId <= 0)
1694                 msgInfo.msgId = 0;
1695         else
1696                 isReplaceMsg = true;
1697 #else
1698         if (msgInfo.msgId <= 0)
1699                 msgInfo.msgId = 0;
1700 #endif
1701
1702         msg_message_id_t msg_id = MsgStoAddSimMessage(&msgInfo, simIdList, listSize);
1703
1704         if (msg_id > 0) {
1705                 MSG_DEBUG("Command Handle Success : MsgStoAddSimMessage()");
1706
1707 #if 0 // Not to broadcast callback here.
1708                 //storage change CB
1709                 msg_id_list_s msgIdList;
1710                 msg_message_id_t msgIds[1];
1711                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
1712
1713                 msgIdList.nCount = 1;
1714                 msgIds[0] = msgInfo.msgId;
1715                 msgIdList.msgIdList = msgIds;
1716
1717                 if (isReplaceMsg)
1718                         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_UPDATE, &msgIdList);
1719                 else
1720                         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_INSERT, &msgIdList);
1721 #endif
1722         } else {
1723                         MSG_DEBUG("Command Handle Fail : MsgStoAddSimMessage()");
1724                         err = MSG_ERR_INVALID_MESSAGE;
1725         }
1726
1727         // Delete Temp File for Message Data
1728         if (msgInfo.bTextSms == false)
1729                 MsgDeleteFile(msgInfo.msgData); //ipc
1730
1731         // Make Event Data
1732         eventSize = MsgMakeEvent((const void *)&msg_id, sizeof(msg_message_id_t), MSG_EVENT_ADD_SIM_MSG, err, (void**)ppEvent);
1733
1734         return eventSize;
1735 }
1736
1737
1738 int MsgUpdateIMSIHandler(const MSG_CMD_S *pCmd, char **ppEvent)
1739 {
1740         msg_error_t err = MSG_SUCCESS;
1741
1742
1743         int eventSize = 0;
1744
1745         int sim_idx = 0;
1746         memcpy(&sim_idx, (void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), sizeof(int));
1747
1748         // Add Message
1749         err = MsgStoUpdateIMSI(sim_idx);
1750
1751         if (err == MSG_SUCCESS) {
1752                 MSG_DEBUG("Command Handle Success : MsgStoUpdatePushEvent()");
1753         } else {
1754                 MSG_DEBUG("Command Handle Fail : MsgStoUpdatePushEvent()");
1755         }
1756
1757         // Make Event Data
1758         eventSize = MsgMakeEvent(NULL, 0, MSG_EVENT_UPDATE_IMSI, err, (void**)ppEvent);
1759
1760         return eventSize;
1761 }