2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.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);
383 } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
384 MSG_DEBUG("Add Status Report");
385 err = addSmsMessage(pMsgInfo);
388 if (err == MSG_SUCCESS) {
389 MSG_DEBUG("Success to add message !!");
391 MSG_DEBUG("fail to add message !! : [%d]", err);
398 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
400 msg_error_t err = MSG_SUCCESS;
402 unsigned int rowId = 0;
403 msg_thread_id_t convId = 0;
405 dbHandle.beginTrans();
407 if (pMsgInfo->nAddressCnt > 0) {
409 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
411 if (err != MSG_SUCCESS) {
412 dbHandle.endTrans(false);
416 pMsgInfo->threadId = convId;
419 /** Add Message Table */
420 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
423 dbHandle.endTrans(false);
424 return MSG_ERR_DB_ROW;
427 /** Update conversation table */
428 err = MsgStoUpdateConversation(&dbHandle, convId);
430 if (err != MSG_SUCCESS) {
431 dbHandle.endTrans(false);
435 dbHandle.endTrans(true);
437 pMsgInfo->msgId = (msg_message_id_t)rowId;
445 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
447 msg_error_t err = MSG_SUCCESS;
449 char sqlQuery[MAX_QUERY_LEN+1];
451 msg_thread_id_t convId = 0;
453 dbHandle.beginTrans();
455 if (pMsg->nAddressCnt > 0) {
457 err = MsgStoAddAddress(&dbHandle, pMsg, &convId);
459 if (err != MSG_SUCCESS) {
460 dbHandle.endTrans(false);
467 char* pFileData = NULL;
468 AutoPtr<char> buf(&pFileData);
471 if (pMsg->bTextSms == false) {
472 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
473 dbHandle.endTrans(false);
474 return MSG_ERR_STORAGE_ERROR;
478 /** Update Message */
479 memset(sqlQuery, 0x00, sizeof(sqlQuery));
481 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
482 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
483 BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
484 MSGFW_MESSAGE_TABLE_NAME, convId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
485 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->bBackup, pMsg->msgId);
487 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
488 dbHandle.endTrans(false);
489 return MSG_ERR_DB_EXEC;
492 dbHandle.bindText(pMsg->subject, 1);
494 dbHandle.bindText(pMsg->msgData, 2);
496 dbHandle.bindText(pMsg->thumbPath, 3);
498 if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
499 dbHandle.bindText(pFileData, 4);
501 dbHandle.bindText(pMsg->msgText, 4);
503 MSG_DEBUG("%s", sqlQuery);
505 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
506 dbHandle.endTrans(false);
507 return MSG_ERR_DB_EXEC;
510 dbHandle.finalizeQuery();
512 err = MsgStoUpdateConversation(&dbHandle, convId);
514 if (err != MSG_SUCCESS) {
515 dbHandle.endTrans(false);
516 return MSG_ERR_STORAGE_ERROR;
519 err = MsgStoClearConversationTable(&dbHandle);
521 if (err != MSG_SUCCESS) {
522 dbHandle.endTrans(false);
523 return MSG_ERR_STORAGE_ERROR;
526 dbHandle.endTrans(true);
532 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
536 char sqlQuery[MAX_QUERY_LEN+1];
538 /** Get SUB_TYPE, STORAGE_ID */
539 memset(sqlQuery, 0x00, sizeof(sqlQuery));
540 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
541 FROM %s WHERE MSG_ID = %d;",
542 MSGFW_MESSAGE_TABLE_NAME, msgId);
544 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
545 return MSG_ERR_DB_PREPARE;
547 MSG_MESSAGE_TYPE_S msgType;
548 msg_folder_id_t folderId;
550 msg_thread_id_t convId;
552 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
553 msgType.mainType = dbHandle.columnInt(0);
554 msgType.subType = dbHandle.columnInt(1);
555 folderId = dbHandle.columnInt(2);
556 convId = dbHandle.columnInt(3);
558 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
560 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
561 dbHandle.finalizeQuery();
562 return MSG_ERR_DB_STEP;
565 dbHandle.finalizeQuery();
567 dbHandle.beginTrans();
569 memset(sqlQuery, 0x00, sizeof(sqlQuery));
570 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
572 /** Delete SMS Send Option */
573 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
574 dbHandle.endTrans(false);
575 return MSG_ERR_DB_EXEC;
578 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
579 memset(sqlQuery, 0x00, sizeof(sqlQuery));
580 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
582 /** Delete Push Message from push table */
583 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
584 dbHandle.endTrans(false);
585 return MSG_ERR_DB_EXEC;
587 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
588 memset(sqlQuery, 0x00, sizeof(sqlQuery));
589 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
591 /** Delete Push Message from push table */
592 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
593 dbHandle.endTrans(false);
594 return MSG_ERR_DB_EXEC;
596 } else if (msgType.subType == MSG_SYNCML_CP) {
597 memset(sqlQuery, 0x00, sizeof(sqlQuery));
598 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
600 /** Delete SyncML Message from syncML table */
601 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
602 dbHandle.endTrans(false);
603 return MSG_ERR_DB_EXEC;
607 memset(sqlQuery, 0x00, sizeof(sqlQuery));
608 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
610 /** Delete Message from msg table */
611 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
612 dbHandle.endTrans(false);
613 return MSG_ERR_DB_EXEC;
616 /** Clear Conversation table */
617 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
618 dbHandle.endTrans(false);
619 return MSG_ERR_DB_EXEC;
622 /** Update conversation table.*/
623 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
624 dbHandle.endTrans(false);
625 return MSG_ERR_STORAGE_ERROR;
628 dbHandle.endTrans(true);
630 if (folderId == MSG_INBOX_ID) {
631 msgType.classType = MSG_CLASS_NONE;
633 /** Set memory status in SIM */
634 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
635 MSG_DEBUG("Set Memory Status");
636 SmsPlgSetMemoryStatus(MSG_SUCCESS);
640 int smsCnt = 0, mmsCnt = 0;
642 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
643 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
645 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
646 // MsgDeleteNotiByMsgId(msgId);
653 msg_error_t SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
655 msg_error_t err = MSG_SUCCESS;
657 unsigned int rowId = 0;
658 msg_thread_id_t convId = 0;
660 char sqlQuery[MAX_QUERY_LEN+1];
662 dbHandle.beginTrans();
664 if (pMsgInfo->nAddressCnt > 0) {
665 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
667 if (err != MSG_SUCCESS) {
668 dbHandle.endTrans(false);
672 pMsgInfo->threadId = convId;
675 /** Add Message Table */
676 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
679 dbHandle.endTrans(false);
680 return MSG_ERR_DB_ROW;
684 unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
686 /** Add CB Msg in MSG_CBMSG_TABLE */
687 memset(sqlQuery, 0x00, sizeof(sqlQuery));
689 sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
690 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
692 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
693 dbHandle.endTrans(false);
694 return MSG_ERR_DB_EXEC;
697 /** Update conversation table. */
698 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
699 dbHandle.endTrans(false);
700 return MSG_ERR_STORAGE_ERROR;
703 dbHandle.endTrans(true);
706 pMsgInfo->msgId = (msg_message_id_t)rowId;
712 msg_error_t SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
714 msg_error_t err = MSG_SUCCESS;
716 char sqlQuery[MAX_QUERY_LEN+1];
718 unsigned int retCnt = 0;
719 msg_thread_id_t convId = 0;
721 /** Check if new address or not */
722 if (MsgExistAddress(&dbHandle, pMsgInfo, &convId) == true) {
723 MSG_DEBUG("Address Info. exists [%d]", convId);
725 /** Find Replace Type Msg : Same Replace Type, Same Origin Address */
726 memset(sqlQuery, 0x00, sizeof(sqlQuery));
727 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d;",
728 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId);
730 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
731 return MSG_ERR_DB_PREPARE;
733 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
734 retCnt = dbHandle.columnInt(0);
735 pMsgInfo->msgId = dbHandle.columnInt(1);
737 dbHandle.finalizeQuery();
738 return MSG_ERR_DB_STEP;
741 dbHandle.finalizeQuery();
744 /** Update New Replace Type Msg */
745 if (retCnt == 0) { /** Insert New Replace Type Msg */
746 MSG_DEBUG("Insert Replace Type Msg");
747 err = addSmsMessage(pMsgInfo);
749 MSG_DEBUG("Update Replace Type Msg");
750 err = updateSmsMessage(pMsgInfo);
757 msg_error_t SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
759 msg_error_t err = MSG_SUCCESS;
761 MSG_PUSH_MESSAGE_S pushMsg = {};
763 char sqlQuery[MAX_QUERY_LEN+1];
764 memset(sqlQuery, 0x00, sizeof(sqlQuery));
768 char* pFileData = NULL;
769 AutoPtr<char> buf(&pFileData);
771 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
772 return MSG_ERR_STORAGE_ERROR;
774 MSG_DEBUG("fileSize : %d", fileSize);
776 memcpy(&pushMsg, pFileData, fileSize);
778 /** Delete temporary file */
779 MsgDeleteFile(pMsgInfo->msgData);
781 /** check pPushMsg data */
783 MSG_DEBUG("check pushMsg data");
784 MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
785 MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
786 MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
787 MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
788 MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
789 MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
790 MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
792 bool bProceed = true;
794 /** check validation of contents */
795 if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
796 MSG_DEBUG("Fail to check Push Message validation.");
799 /** if validation check value is false */
800 /** return and drop message. */
801 if (bProceed == false)
802 return MSG_ERR_INVALID_MESSAGE;
805 /** update subject */
806 int len = strlen(pushMsg.contents);
808 if (len > MAX_SUBJECT_LEN) {
809 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
810 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
812 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
816 /** Update Msg Text - remove */
817 strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
819 if (pushMsg.contents[0] != '\0') {
820 strncat(pMsgInfo->msgText, " ", MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
821 strncat(pMsgInfo->msgText, pushMsg.contents, MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
824 pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
826 pMsgInfo->bTextSms = true;
827 pMsgInfo->folderId = MSG_INBOX_ID;
828 pMsgInfo->storageId = MSG_STORAGE_PHONE;
830 msg_thread_id_t convId = 0;
832 dbHandle.beginTrans();
834 if (pMsgInfo->nAddressCnt > 0) {
836 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
838 if (err != MSG_SUCCESS) {
839 dbHandle.endTrans(false);
843 pMsgInfo->threadId = convId;
846 /** get last row count for Message id */
847 unsigned int rowId = 0;
849 /** Add Message Table */
850 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
853 dbHandle.endTrans(false);
854 return MSG_ERR_DB_ROW;
857 /** add msg_push_table */
858 snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
859 MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
861 if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
862 dbHandle.endTrans(false);
866 dbHandle.bindText(pushMsg.id, 1);
868 dbHandle.bindText(pushMsg.href, 2);
870 dbHandle.bindText(pushMsg.contents, 3);
872 if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
873 dbHandle.endTrans(false);
877 /** Update conversation table. */
878 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
879 dbHandle.endTrans(false);
880 return MSG_ERR_STORAGE_ERROR;
883 dbHandle.endTrans(true);
885 pMsgInfo->msgId = (msg_message_id_t)rowId;
891 msg_error_t SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
893 msg_error_t err = MSG_SUCCESS;
895 char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
896 char sqlQuery[MAX_QUERY_LEN+1];
900 char* pFileData = NULL;
901 AutoPtr<char> buf(&pFileData);
903 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
904 return MSG_ERR_STORAGE_ERROR;
906 MSG_PUSH_CACHEOP_S *pPushMsg;
908 pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
910 for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
914 memset(href, 0x00, sizeof(href));
915 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
917 memset(sqlQuery, 0x00, sizeof(sqlQuery));
918 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
920 dbHandle.beginTrans();
922 err = dbHandle.prepareQuery(sqlQuery);
924 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
926 msgid = dbHandle.getColumnToInt(1);
928 memset(sqlQuery, 0x00, sizeof(sqlQuery));
929 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
931 /** Delete Message from Push table */
932 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
933 dbHandle.finalizeQuery();
934 dbHandle.endTrans(false);
938 memset(sqlQuery, 0x00, sizeof(sqlQuery));
939 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
941 /** Delete Message from msg table */
942 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
943 dbHandle.finalizeQuery();
944 dbHandle.endTrans(false);
948 /** Update all Address */
949 if (updateAllAddress() != MSG_SUCCESS) {
950 dbHandle.finalizeQuery();
951 dbHandle.endTrans(false);
955 /** Clear Conversation table */
956 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
957 dbHandle.finalizeQuery();
958 dbHandle.endTrans(false);
963 dbHandle.finalizeQuery();
965 dbHandle.endTrans(true);
968 for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
972 memset(href, 0x00, sizeof(href));
973 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
975 memset(sqlQuery, 0x00, sizeof(sqlQuery));
976 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
978 dbHandle.beginTrans();
980 err = dbHandle.prepareQuery(sqlQuery);
982 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
984 msgid = dbHandle.getColumnToInt(1);
986 memset(sqlQuery, 0x00, sizeof(sqlQuery));
987 sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
989 /** Delete Message from Push table */
990 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
991 dbHandle.finalizeQuery();
992 dbHandle.endTrans(false);
996 memset(sqlQuery, 0x00, sizeof(sqlQuery));
997 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
999 /** Delete Message from msg table */
1000 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1001 dbHandle.finalizeQuery();
1002 dbHandle.endTrans(false);
1006 /** Update all Address */
1007 if (updateAllAddress() != MSG_SUCCESS) {
1008 dbHandle.finalizeQuery();
1009 dbHandle.endTrans(false);
1013 /** Clear Address table */
1014 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
1015 dbHandle.finalizeQuery();
1016 dbHandle.endTrans(false);
1021 dbHandle.finalizeQuery();
1023 dbHandle.endTrans(true);
1026 /** delete temporary file */
1027 MsgDeleteFile(pMsgInfo->msgData);
1033 msg_error_t SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
1035 msg_error_t err = MSG_SUCCESS;
1037 unsigned long oldExpireTime = 0;
1040 char sqlQuery[MAX_QUERY_LEN+1];
1042 /** is push message is expired?? */
1043 if (pPushMsg->received > pPushMsg->expires) {
1044 MSG_DEBUG("Push Message is expired.");
1050 if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
1051 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1052 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
1053 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
1055 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
1056 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
1059 err = dbHandle.getTable(sqlQuery, &rowCnt);
1062 dbHandle.freeTable();
1066 oldExpireTime = dbHandle.getColumnToInt(1);
1068 dbHandle.freeTable();
1070 if (pPushMsg->created < oldExpireTime) {
1071 MSG_DEBUG("Push Message is expired.");
1080 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
1082 msg_error_t err = MSG_SUCCESS;
1084 err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
1086 if (err != MSG_SUCCESS) {
1088 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
1089 bool bAutoErase = false;
1091 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
1093 MSG_DEBUG("bAutoErase: %d", bAutoErase);
1095 if (bAutoErase == true) {
1096 msg_message_id_t msgId;
1098 /** Find the oldest message's msgId */
1099 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
1101 if (err != MSG_SUCCESS)
1104 /** Delete the corresponding message. */
1105 err = deleteSmsMessage(msgId);
1116 msg_error_t SmsPluginStorage::updateAllAddress()
1118 msg_error_t err = MSG_SUCCESS;
1120 int rowCnt = 0, index = 1;
1121 char sqlQuery[MAX_QUERY_LEN+1];
1123 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1125 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
1127 err = dbHandle.getTable(sqlQuery, &rowCnt);
1129 if (err == MSG_ERR_DB_NORECORD) {
1130 dbHandle.freeTable();
1132 } else if ( err != MSG_SUCCESS) {
1133 dbHandle.freeTable();
1138 for (int i = 0; i < rowCnt; i++) {
1140 err = MsgStoUpdateConversation(&dbHandle, index++);
1142 if (err != MSG_SUCCESS)
1146 dbHandle.freeTable();
1152 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id)
1154 msg_error_t err = MSG_SUCCESS;
1156 int rowCnt = 0, index = 3;
1158 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1160 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1162 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
1164 err = dbHandle.getTable(sqlQuery, &rowCnt);
1165 MSG_DEBUG("rowCnt: %d", rowCnt);
1167 if (err == MSG_ERR_DB_NORECORD) {
1168 dbHandle.freeTable();
1171 else if ( err != MSG_SUCCESS) {
1172 dbHandle.freeTable();
1176 char content_type[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1];
1177 char app_id[MAX_WAPPUSH_ID_LEN + 1];
1178 int appcode = 0, default_appcode = 0;
1180 char *_content_type = NULL, *_app_id = NULL;
1184 for (int i = 0; i < rowCnt; i++) {
1185 memset(content_type, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
1186 memset(app_id, 0, MAX_WAPPUSH_ID_LEN);
1188 dbHandle.getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, content_type);
1189 dbHandle.getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, app_id);
1190 appcode = dbHandle.getColumnToInt(index++);
1192 //MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id);
1193 _content_type = strcasestr(pPushHeader, content_type);
1195 _app_id = strcasestr(pPushHeader, app_id);
1197 default_appcode = appcode;
1200 PUSH_APPLICATION_INFO_S pInfo = {0, };
1201 pInfo.appcode = appcode;
1202 MSG_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, app_id);
1203 strcpy(application_id, app_id);
1204 pushAppInfoList.push_back(pInfo);
1213 // perform default action.
1214 PUSH_APPLICATION_INFO_S pInfo = {0, };
1215 pInfo.appcode = default_appcode;
1216 strcpy(application_id, app_id);
1217 pushAppInfoList.push_back(pInfo);
1220 dbHandle.freeTable();
1226 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1228 msg_error_t err = MSG_SUCCESS;
1229 if((unsigned int)index > pushAppInfoList.size() - 1)
1230 return MSG_ERR_INVALID_PARAMETER;
1232 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1234 for (; it != pushAppInfoList.end(); it++)
1237 *appcode = it->appcode;
1247 msg_error_t SmsPluginStorage::releasePushEvent()
1249 msg_error_t err = MSG_SUCCESS;
1250 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1252 for (; it != pushAppInfoList.end(); it++)
1253 it = pushAppInfoList.erase(it);