2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgUtilFile.h"
24 #include "MsgUtilStorage.h"
25 #include "MsgGconfWrapper.h"
26 #include "MsgNotificationWrapper.h"
27 #include "SmsPluginMain.h"
28 #include "SmsPluginSimMsg.h"
29 #include "SmsPluginStorage.h"
32 /*==================================================================================================
33 IMPLEMENTATION OF SmsPluginStorage - Member Functions
34 ==================================================================================================*/
35 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
38 SmsPluginStorage::SmsPluginStorage()
40 /*** No need to connect DB anymore.
41 if (dbHandle.connect() != MSG_SUCCESS) {
42 MSG_DEBUG("DB Connect Fail");
48 SmsPluginStorage::~SmsPluginStorage()
50 if (dbHandle.disconnect() != MSG_SUCCESS) {
51 MSG_DEBUG("DB Disconnect Fail");
56 SmsPluginStorage* SmsPluginStorage::instance()
59 MSG_DEBUG("pInstance is NULL. Now creating instance.");
60 pInstance = new SmsPluginStorage();
67 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
71 /*** Comment below line to not save the time value after sent status (it could be used later.)
72 time_t curTime = time(NULL);
74 char sqlQuery[MAX_QUERY_LEN+1];
76 memset(sqlQuery, 0x00, sizeof(sqlQuery));
78 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
80 /** Move Msg to SENTBOX */
81 if (status == MSG_NETWORK_SEND_SUCCESS) {
82 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
83 MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
85 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
86 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
89 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
90 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
91 return MSG_ERR_DB_EXEC;
94 if (status == MSG_NETWORK_SEND_SUCCESS) {
95 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
96 MsgAddPhoneLog(pMsgInfo);
105 #ifdef SMS_REPORT_OPERATION
106 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
110 char sqlQuery[MAX_QUERY_LEN+1];
112 memset(sqlQuery, 0x00, sizeof(sqlQuery));
114 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
115 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
117 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
118 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
119 return MSG_ERR_DB_EXEC;
122 /** Set Message Reference for updating report table */
125 MSG_DEBUG("MsgRef : %d", MsgRef);
133 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
137 MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
139 char sqlQuery[MAX_QUERY_LEN+1];
141 /** Get Msg Id for Quickpanel Noti */
142 msg_message_id_t msgId = 0;
144 memset(sqlQuery, 0x00, sizeof(sqlQuery));
145 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
146 MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
148 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
149 return MSG_ERR_DB_PREPARE;
151 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
152 msgId = dbHandle.columnInt(0);
154 dbHandle.finalizeQuery();
157 memset(sqlQuery, 0x00, sizeof(sqlQuery));
158 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
159 MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
161 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
162 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
163 return MSG_ERR_DB_EXEC;
166 /** Insert Quickpanel Noti */
167 msg_error_t ret = MSG_SUCCESS;
169 ret = MsgInsertSmsNotiToQuickpanel(&dbHandle, msgId, Status);
171 if (ret != MSG_SUCCESS) {
172 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
182 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo)
184 msg_error_t err = MSG_SUCCESS;
186 msg_message_id_t msgId = 0;
187 msg_thread_id_t convId = 0;
188 unsigned int simId = 0;
190 char sqlQuery[MAX_QUERY_LEN+1];
192 dbHandle.beginTrans();
194 err = MsgStoAddAddress(&dbHandle, pSimMsgInfo, &convId);
196 if (err != MSG_SUCCESS) {
197 dbHandle.endTrans(false);
201 pSimMsgInfo->threadId = convId;
203 err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
205 if (err != MSG_SUCCESS) {
206 dbHandle.endTrans(false);
210 simId = pSimMsgInfo->msgId;
211 pSimMsgInfo->msgId = msgId;
213 SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
215 /** Get Data from Concat SIM Msg */
216 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
220 char* pFileData = NULL;
221 AutoPtr<char> buf(&pFileData);
223 if (MsgOpenAndReadFile(pSimMsgInfo->msgData, &pFileData, &fileSize) == false) {
224 dbHandle.endTrans(false);
225 return MSG_ERR_STORAGE_ERROR;
229 memcpy(&concatSimMsg, (SMS_CONCAT_SIM_MSG_S*)pFileData, fileSize);
231 /** Delete temporary file */
232 MsgDeleteFile(pSimMsgInfo->msgData); /** ipc */
234 MSG_DEBUG("SIM ID [%d], MSG DATA [%s]", concatSimMsg.simIdCnt, concatSimMsg.msgData);
238 memset(sqlQuery, 0x00, sizeof(sqlQuery));
240 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, 0, 0, 0);",
241 MSGFW_MESSAGE_TABLE_NAME, msgId, convId, pSimMsgInfo->folderId, pSimMsgInfo->storageId,
242 pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
243 pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
244 pSimMsgInfo->direction, 0, pSimMsgInfo->bBackup);
246 MSG_DEBUG("QUERY : %s", sqlQuery);
248 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
249 dbHandle.endTrans(false);
250 return MSG_ERR_DB_PREPARE;
253 dbHandle.bindText(pSimMsgInfo->subject, 1);
255 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false)
256 dbHandle.bindText(concatSimMsg.msgData, 2);
258 dbHandle.bindText(pSimMsgInfo->msgText, 2);
260 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
261 dbHandle.endTrans(false);
262 return MSG_ERR_DB_STEP;
265 dbHandle.finalizeQuery();
267 /** Insert to Sim table */
268 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
270 MSG_DEBUG("sim count : %d", concatSimMsg.simIdCnt);
272 for (unsigned int i = 0; i < concatSimMsg.simIdCnt; i++) {
273 memset(sqlQuery, 0x00, sizeof(sqlQuery));
274 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
275 MSGFW_SIM_MSG_TABLE_NAME, msgId, concatSimMsg.simIdList[i]);
277 MSG_DEBUG("QUERY : %s", sqlQuery);
279 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
280 dbHandle.endTrans(false);
281 return MSG_ERR_DB_EXEC;
285 memset(sqlQuery, 0x00, sizeof(sqlQuery));
286 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
287 MSGFW_SIM_MSG_TABLE_NAME, msgId, simId);
289 MSG_DEBUG("QUERY : %s", sqlQuery);
291 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
292 dbHandle.endTrans(false);
293 return MSG_ERR_DB_EXEC;
297 /** Update conversation table. */
298 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
299 dbHandle.endTrans(false);
300 return MSG_ERR_STORAGE_ERROR;
303 dbHandle.endTrans(true);
309 msg_error_t SmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
313 msg_error_t err = MSG_SUCCESS;
315 /** Check whether storage is full or not */
316 err = checkStorageStatus(pMsgInfo);
318 if (err != MSG_SUCCESS) {
319 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
320 pMsgInfo->folderId = 0;
321 if (addSmsMessage(pMsgInfo) != MSG_SUCCESS) {
322 MSG_DEBUG("addSmsMessage is failed!");
328 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
330 MSG_DEBUG("Add Normal SMS");
332 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
333 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
335 if (err == MSG_SUCCESS) {
336 MSG_DEBUG("Success to saveSimMessage.");
338 MSG_DEBUG("Fail to saveSimMessage : [%d]", err);
341 /** Class 0 Msg should be saved in hidden folder */
342 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
343 pMsgInfo->folderId = 0;
347 err = addSmsMessage(pMsgInfo);
350 } else if ((pMsgInfo->msgType.subType == MSG_CB_SMS) || (pMsgInfo->msgType.subType == MSG_JAVACB_SMS)) {
351 /** check add message option */
353 MsgSettingGetBool(CB_SAVE, &bSave);
355 MSG_DEBUG("Add CB Message");
356 err = addCbMessage(pMsgInfo);
358 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
359 MSG_DEBUG("Add Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
360 err = addReplaceTypeMsg(pMsgInfo);
361 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
362 if (pMsgInfo->bStore == true) {
363 MSG_DEBUG("Add MWI Message");
364 err = addSmsMessage(pMsgInfo);
366 } else if ((pMsgInfo->msgType.subType == MSG_WAP_SI_SMS) || (pMsgInfo->msgType.subType == MSG_WAP_CO_SMS)) {
367 MSG_DEBUG("Add WAP Push Message");
368 switch (pMsgInfo->msgType.subType)
372 // save push message information
373 err = addWAPMessage(pMsgInfo);
379 err = handleCOWAPMessage(pMsgInfo);
385 if (err == MSG_SUCCESS) {
386 MSG_DEBUG("Success to add message !!");
388 MSG_DEBUG("fail to add message !! : [%d]", err);
395 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
397 msg_error_t err = MSG_SUCCESS;
399 unsigned int rowId = 0;
400 msg_thread_id_t convId = 0;
402 dbHandle.beginTrans();
404 if (pMsgInfo->nAddressCnt > 0) {
406 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
408 if (err != MSG_SUCCESS) {
409 dbHandle.endTrans(false);
413 pMsgInfo->threadId = convId;
416 /** Add Message Table */
417 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
420 dbHandle.endTrans(false);
421 return MSG_ERR_DB_ROW;
424 /** Update conversation table */
425 err = MsgStoUpdateConversation(&dbHandle, convId);
427 if (err != MSG_SUCCESS) {
428 dbHandle.endTrans(false);
432 dbHandle.endTrans(true);
434 pMsgInfo->msgId = (msg_message_id_t)rowId;
441 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
445 msg_error_t err = MSG_SUCCESS;
447 if (pSendOptInfo->bSetting == false) {
448 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
449 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
451 if (pSendOptInfo->bDeliverReq || pSendOptInfo->option.smsSendOptInfo.bReplyPath) {
452 pSendOptInfo->bSetting = true;
453 MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
457 if (pSendOptInfo->bSetting == true) {
458 char sqlQuery[MAX_QUERY_LEN+1];
460 dbHandle.beginTrans();
462 memset(sqlQuery, 0x00, sizeof(sqlQuery));
463 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d);",
464 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
465 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath);
467 MSG_DEBUG("Query = [%s]", sqlQuery);
469 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
470 dbHandle.endTrans(false);
471 err = MSG_ERR_DB_EXEC;
473 dbHandle.endTrans(true);
483 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
485 msg_error_t err = MSG_SUCCESS;
487 char sqlQuery[MAX_QUERY_LEN+1];
489 msg_thread_id_t convId = 0;
491 dbHandle.beginTrans();
493 if (pMsg->nAddressCnt > 0) {
495 err = MsgStoAddAddress(&dbHandle, pMsg, &convId);
497 if (err != MSG_SUCCESS) {
498 dbHandle.endTrans(false);
505 char* pFileData = NULL;
506 AutoPtr<char> buf(&pFileData);
509 if (pMsg->bTextSms == false) {
510 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
511 dbHandle.endTrans(false);
512 return MSG_ERR_STORAGE_ERROR;
516 /** Update Message */
517 memset(sqlQuery, 0x00, sizeof(sqlQuery));
519 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
520 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
521 BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
522 MSGFW_MESSAGE_TABLE_NAME, convId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
523 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->bBackup, pMsg->msgId);
525 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
526 dbHandle.endTrans(false);
527 return MSG_ERR_DB_EXEC;
530 dbHandle.bindText(pMsg->subject, 1);
532 dbHandle.bindText(pMsg->msgData, 2);
534 dbHandle.bindText(pMsg->thumbPath, 3);
536 if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
537 dbHandle.bindText(pFileData, 4);
539 dbHandle.bindText(pMsg->msgText, 4);
541 MSG_DEBUG("%s", sqlQuery);
543 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
544 dbHandle.endTrans(false);
545 return MSG_ERR_DB_EXEC;
548 dbHandle.finalizeQuery();
550 err = MsgStoUpdateConversation(&dbHandle, convId);
552 if (err != MSG_SUCCESS) {
553 dbHandle.endTrans(false);
554 return MSG_ERR_STORAGE_ERROR;
557 err = MsgStoClearConversationTable(&dbHandle);
559 if (err != MSG_SUCCESS) {
560 dbHandle.endTrans(false);
561 return MSG_ERR_STORAGE_ERROR;
564 dbHandle.endTrans(true);
570 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
574 char sqlQuery[MAX_QUERY_LEN+1];
576 /** Get SUB_TYPE, STORAGE_ID */
577 memset(sqlQuery, 0x00, sizeof(sqlQuery));
578 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
579 FROM %s WHERE MSG_ID = %d;",
580 MSGFW_MESSAGE_TABLE_NAME, msgId);
582 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
583 return MSG_ERR_DB_PREPARE;
585 MSG_MESSAGE_TYPE_S msgType;
586 msg_folder_id_t folderId;
588 msg_thread_id_t convId;
590 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
591 msgType.mainType = dbHandle.columnInt(0);
592 msgType.subType = dbHandle.columnInt(1);
593 folderId = dbHandle.columnInt(2);
594 convId = dbHandle.columnInt(3);
596 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
598 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
599 dbHandle.finalizeQuery();
600 return MSG_ERR_DB_STEP;
603 dbHandle.finalizeQuery();
605 dbHandle.beginTrans();
607 memset(sqlQuery, 0x00, sizeof(sqlQuery));
608 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
610 /** Delete SMS Send Option */
611 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
612 dbHandle.endTrans(false);
613 return MSG_ERR_DB_EXEC;
616 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
617 memset(sqlQuery, 0x00, sizeof(sqlQuery));
618 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
620 /** Delete Push Message from push table */
621 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
622 dbHandle.endTrans(false);
623 return MSG_ERR_DB_EXEC;
625 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
626 memset(sqlQuery, 0x00, sizeof(sqlQuery));
627 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
629 /** Delete Push Message from push table */
630 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
631 dbHandle.endTrans(false);
632 return MSG_ERR_DB_EXEC;
634 } else if (msgType.subType == MSG_SYNCML_CP) {
635 memset(sqlQuery, 0x00, sizeof(sqlQuery));
636 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
638 /** Delete SyncML Message from syncML table */
639 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
640 dbHandle.endTrans(false);
641 return MSG_ERR_DB_EXEC;
645 memset(sqlQuery, 0x00, sizeof(sqlQuery));
646 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
648 /** Delete Message from msg table */
649 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
650 dbHandle.endTrans(false);
651 return MSG_ERR_DB_EXEC;
654 /** Clear Conversation table */
655 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
656 dbHandle.endTrans(false);
657 return MSG_ERR_DB_EXEC;
660 /** Update conversation table.*/
661 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
662 dbHandle.endTrans(false);
663 return MSG_ERR_STORAGE_ERROR;
666 dbHandle.endTrans(true);
668 if (folderId == MSG_INBOX_ID) {
669 msgType.classType = MSG_CLASS_NONE;
671 /** Set memory status in SIM */
672 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
673 MSG_DEBUG("Set Memory Status");
674 SmsPlgSetMemoryStatus(MSG_SUCCESS);
678 int smsCnt = 0, mmsCnt = 0;
680 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
681 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
683 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
684 MsgRefreshNoti(false);
690 msg_error_t SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
692 msg_error_t err = MSG_SUCCESS;
694 unsigned int rowId = 0;
695 msg_thread_id_t convId = 0;
697 char sqlQuery[MAX_QUERY_LEN+1];
699 dbHandle.beginTrans();
701 if (pMsgInfo->nAddressCnt > 0) {
702 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
704 if (err != MSG_SUCCESS) {
705 dbHandle.endTrans(false);
709 pMsgInfo->threadId = convId;
712 /** Add Message Table */
713 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
716 dbHandle.endTrans(false);
717 return MSG_ERR_DB_ROW;
721 unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
723 /** Add CB Msg in MSG_CBMSG_TABLE */
724 memset(sqlQuery, 0x00, sizeof(sqlQuery));
726 sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
727 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
729 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
730 dbHandle.endTrans(false);
731 return MSG_ERR_DB_EXEC;
734 /** Update conversation table. */
735 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
736 dbHandle.endTrans(false);
737 return MSG_ERR_STORAGE_ERROR;
740 dbHandle.endTrans(true);
743 pMsgInfo->msgId = (msg_message_id_t)rowId;
749 msg_error_t SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
751 msg_error_t err = MSG_SUCCESS;
753 char sqlQuery[MAX_QUERY_LEN+1];
755 unsigned int retCnt = 0;
756 msg_thread_id_t convId = 0;
758 /** Check if new address or not */
759 if (MsgExistAddress(&dbHandle, pMsgInfo, &convId) == true) {
760 MSG_DEBUG("Address Info. exists [%d]", convId);
762 /** Find Replace Type Msg : Same Replace Type, Same Origin Address */
763 memset(sqlQuery, 0x00, sizeof(sqlQuery));
764 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d;",
765 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId);
767 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
768 return MSG_ERR_DB_PREPARE;
770 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
771 retCnt = dbHandle.columnInt(0);
772 pMsgInfo->msgId = dbHandle.columnInt(1);
774 dbHandle.finalizeQuery();
775 return MSG_ERR_DB_STEP;
778 dbHandle.finalizeQuery();
781 /** Update New Replace Type Msg */
782 if (retCnt == 0) { /** Insert New Replace Type Msg */
783 MSG_DEBUG("Insert Replace Type Msg");
784 err = addSmsMessage(pMsgInfo);
786 MSG_DEBUG("Update Replace Type Msg");
787 err = updateSmsMessage(pMsgInfo);
794 msg_error_t SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
796 msg_error_t err = MSG_SUCCESS;
798 MSG_PUSH_MESSAGE_S pushMsg = {};
800 char sqlQuery[MAX_QUERY_LEN+1];
801 memset(sqlQuery, 0x00, sizeof(sqlQuery));
805 char* pFileData = NULL;
806 AutoPtr<char> buf(&pFileData);
808 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
809 return MSG_ERR_STORAGE_ERROR;
811 MSG_DEBUG("fileSize : %d", fileSize);
813 memcpy(&pushMsg, pFileData, fileSize);
815 /** Delete temporary file */
816 MsgDeleteFile(pMsgInfo->msgData);
818 /** check pPushMsg data */
820 MSG_DEBUG("check pushMsg data");
821 MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
822 MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
823 MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
824 MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
825 MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
826 MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
827 MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
829 bool bProceed = true;
831 /** check validation of contents */
832 if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
833 MSG_DEBUG("Fail to check Push Message validation.");
836 /** if validation check value is false */
837 /** return and drop message. */
838 if (bProceed == false)
839 return MSG_ERR_INVALID_MESSAGE;
842 /** update subject */
843 int len = strlen(pushMsg.contents);
845 if (len > MAX_SUBJECT_LEN) {
846 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
847 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
849 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
853 /** Update Msg Text - remove */
854 strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
856 if (pushMsg.contents[0] != '\0') {
857 strncat(pMsgInfo->msgText, " ", MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
858 strncat(pMsgInfo->msgText, pushMsg.contents, MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
861 pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
863 pMsgInfo->bTextSms = true;
864 pMsgInfo->folderId = MSG_INBOX_ID;
865 pMsgInfo->storageId = MSG_STORAGE_PHONE;
867 msg_thread_id_t convId = 0;
869 dbHandle.beginTrans();
871 if (pMsgInfo->nAddressCnt > 0) {
873 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
875 if (err != MSG_SUCCESS) {
876 dbHandle.endTrans(false);
880 pMsgInfo->threadId = convId;
883 /** get last row count for Message id */
884 unsigned int rowId = 0;
886 /** Add Message Table */
887 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
890 dbHandle.endTrans(false);
891 return MSG_ERR_DB_ROW;
894 /** add msg_push_table */
895 snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
896 MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
898 if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
899 dbHandle.endTrans(false);
903 dbHandle.bindText(pushMsg.id, 1);
905 dbHandle.bindText(pushMsg.href, 2);
907 dbHandle.bindText(pushMsg.contents, 3);
909 if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
910 dbHandle.endTrans(false);
914 /** Update conversation table. */
915 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
916 dbHandle.endTrans(false);
917 return MSG_ERR_STORAGE_ERROR;
920 dbHandle.endTrans(true);
922 pMsgInfo->msgId = (msg_message_id_t)rowId;
928 msg_error_t SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
930 msg_error_t err = MSG_SUCCESS;
932 char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
933 char sqlQuery[MAX_QUERY_LEN+1];
937 char* pFileData = NULL;
938 AutoPtr<char> buf(&pFileData);
940 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
941 return MSG_ERR_STORAGE_ERROR;
943 MSG_PUSH_CACHEOP_S *pPushMsg;
945 pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
947 for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
951 memset(href, 0x00, sizeof(href));
952 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
954 memset(sqlQuery, 0x00, sizeof(sqlQuery));
955 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
957 dbHandle.beginTrans();
959 err = dbHandle.prepareQuery(sqlQuery);
961 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
963 msgid = dbHandle.getColumnToInt(1);
965 memset(sqlQuery, 0x00, sizeof(sqlQuery));
966 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
968 /** Delete Message from Push table */
969 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
970 dbHandle.finalizeQuery();
971 dbHandle.endTrans(false);
975 memset(sqlQuery, 0x00, sizeof(sqlQuery));
976 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
978 /** Delete Message from msg table */
979 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
980 dbHandle.finalizeQuery();
981 dbHandle.endTrans(false);
985 /** Update all Address */
986 if (updateAllAddress() != MSG_SUCCESS) {
987 dbHandle.finalizeQuery();
988 dbHandle.endTrans(false);
992 /** Clear Conversation table */
993 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
994 dbHandle.finalizeQuery();
995 dbHandle.endTrans(false);
1000 dbHandle.finalizeQuery();
1002 dbHandle.endTrans(true);
1005 for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
1009 memset(href, 0x00, sizeof(href));
1010 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
1012 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1013 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
1015 dbHandle.beginTrans();
1017 err = dbHandle.prepareQuery(sqlQuery);
1019 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
1021 msgid = dbHandle.getColumnToInt(1);
1023 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1024 sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
1026 /** Delete Message from Push table */
1027 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1028 dbHandle.finalizeQuery();
1029 dbHandle.endTrans(false);
1033 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1034 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
1036 /** Delete Message from msg table */
1037 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1038 dbHandle.finalizeQuery();
1039 dbHandle.endTrans(false);
1043 /** Update all Address */
1044 if (updateAllAddress() != MSG_SUCCESS) {
1045 dbHandle.finalizeQuery();
1046 dbHandle.endTrans(false);
1050 /** Clear Address table */
1051 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
1052 dbHandle.finalizeQuery();
1053 dbHandle.endTrans(false);
1058 dbHandle.finalizeQuery();
1060 dbHandle.endTrans(true);
1063 /** delete temporary file */
1064 MsgDeleteFile(pMsgInfo->msgData);
1070 msg_error_t SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
1072 msg_error_t err = MSG_SUCCESS;
1074 unsigned long oldExpireTime = 0;
1077 char sqlQuery[MAX_QUERY_LEN+1];
1079 /** is push message is expired?? */
1080 if (pPushMsg->received > pPushMsg->expires) {
1081 MSG_DEBUG("Push Message is expired.");
1087 if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
1088 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1089 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
1090 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
1092 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
1093 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
1096 err = dbHandle.getTable(sqlQuery, &rowCnt);
1099 dbHandle.freeTable();
1103 oldExpireTime = dbHandle.getColumnToInt(1);
1105 dbHandle.freeTable();
1107 if (pPushMsg->created < oldExpireTime) {
1108 MSG_DEBUG("Push Message is expired.");
1117 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
1119 msg_error_t err = MSG_SUCCESS;
1121 err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
1123 if (err != MSG_SUCCESS) {
1125 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
1126 bool bAutoErase = false;
1128 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
1130 MSG_DEBUG("bAutoErase: %d", bAutoErase);
1132 if (bAutoErase == true) {
1133 msg_message_id_t msgId;
1135 /** Find the oldest message's msgId */
1136 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
1138 if (err != MSG_SUCCESS)
1141 /** Delete the corresponding message. */
1142 err = deleteSmsMessage(msgId);
1153 msg_error_t SmsPluginStorage::updateAllAddress()
1155 msg_error_t err = MSG_SUCCESS;
1157 int rowCnt = 0, index = 1;
1158 char sqlQuery[MAX_QUERY_LEN+1];
1160 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1162 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
1164 err = dbHandle.getTable(sqlQuery, &rowCnt);
1166 if (err == MSG_ERR_DB_NORECORD) {
1167 dbHandle.freeTable();
1169 } else if ( err != MSG_SUCCESS) {
1170 dbHandle.freeTable();
1175 for (int i = 0; i < rowCnt; i++) {
1177 err = MsgStoUpdateConversation(&dbHandle, index++);
1179 if (err != MSG_SUCCESS)
1183 dbHandle.freeTable();
1189 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, char *contentType)
1191 msg_error_t err = MSG_SUCCESS;
1193 int rowCnt = 0, index = 3;
1195 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1197 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1199 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
1201 err = dbHandle.getTable(sqlQuery, &rowCnt);
1202 MSG_DEBUG("rowCnt: %d", rowCnt);
1204 if (err == MSG_ERR_DB_NORECORD) {
1205 dbHandle.freeTable();
1208 else if ( err != MSG_SUCCESS) {
1209 dbHandle.freeTable();
1213 char content_type[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1];
1214 char app_id[MAX_WAPPUSH_ID_LEN + 1];
1215 int appcode = 0, default_appcode = 0;
1217 char *_content_type = NULL, *_app_id = NULL;
1221 for (int i = 0; i < rowCnt; i++) {
1222 memset(content_type, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
1223 memset(app_id, 0, MAX_WAPPUSH_ID_LEN);
1225 dbHandle.getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, content_type);
1226 dbHandle.getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, app_id);
1227 appcode = dbHandle.getColumnToInt(index++);
1229 //MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id);
1230 _content_type = strcasestr(pPushHeader, content_type);
1232 _app_id = strcasestr(pPushHeader, app_id);
1234 default_appcode = appcode;
1237 PUSH_APPLICATION_INFO_S pInfo = {0, };
1238 pInfo.appcode = appcode;
1239 MSG_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, app_id);
1240 strcpy(application_id, app_id);
1241 strcpy(contentType, content_type);
1242 pushAppInfoList.push_back(pInfo);
1251 // perform default action.
1252 PUSH_APPLICATION_INFO_S pInfo = {0, };
1253 pInfo.appcode = default_appcode;
1254 strcpy(application_id, app_id);
1255 strcpy(contentType, content_type);
1256 pushAppInfoList.push_back(pInfo);
1259 dbHandle.freeTable();
1265 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1267 msg_error_t err = MSG_SUCCESS;
1268 if((unsigned int)index > pushAppInfoList.size() - 1)
1269 return MSG_ERR_INVALID_PARAMETER;
1271 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1273 for (; it != pushAppInfoList.end(); it++)
1276 *appcode = it->appcode;
1286 msg_error_t SmsPluginStorage::releasePushEvent()
1288 msg_error_t err = MSG_SUCCESS;
1289 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1291 for (; it != pushAppInfoList.end(); it++)
1292 it = pushAppInfoList.erase(it);