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()
44 SmsPluginStorage::~SmsPluginStorage()
46 if (dbHandle.disconnect() != MSG_SUCCESS) {
47 MSG_DEBUG("DB Disconnect Fail");
52 SmsPluginStorage* SmsPluginStorage::instance()
55 MSG_DEBUG("pInstance is NULL. Now creating instance.");
56 pInstance = new SmsPluginStorage();
63 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
67 char sqlQuery[MAX_QUERY_LEN+1];
69 memset(sqlQuery, 0x00, sizeof(sqlQuery));
71 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
73 /** Move Msg to SENTBOX */
74 if (status == MSG_NETWORK_SEND_SUCCESS) {
75 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
76 MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
78 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
79 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
82 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
83 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
84 return MSG_ERR_DB_EXEC;
87 if (status == MSG_NETWORK_SEND_SUCCESS) {
88 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
89 MsgAddPhoneLog(pMsgInfo);
98 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo)
100 msg_error_t err = MSG_SUCCESS;
102 msg_message_id_t msgId = 0;
103 msg_thread_id_t convId = 0;
104 unsigned int simId = 0;
106 char sqlQuery[MAX_QUERY_LEN+1];
108 dbHandle.beginTrans();
110 err = MsgStoAddAddress(&dbHandle, pSimMsgInfo, &convId);
112 if (err != MSG_SUCCESS) {
113 dbHandle.endTrans(false);
117 err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
119 if (err != MSG_SUCCESS) {
120 dbHandle.endTrans(false);
124 simId = pSimMsgInfo->msgId;
125 pSimMsgInfo->msgId = msgId;
127 SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
129 /** Get Data from Concat SIM Msg */
130 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
134 char* pFileData = NULL;
135 AutoPtr<char> buf(&pFileData);
137 if (MsgOpenAndReadFile(pSimMsgInfo->msgData, &pFileData, &fileSize) == false) {
138 dbHandle.endTrans(false);
139 return MSG_ERR_STORAGE_ERROR;
143 memcpy(&concatSimMsg, (SMS_CONCAT_SIM_MSG_S*)pFileData, fileSize);
145 /** Delete temporary file */
146 MsgDeleteFile(pSimMsgInfo->msgData); /** ipc */
148 MSG_DEBUG("SIM ID [%d], MSG DATA [%s]", concatSimMsg.simIdCnt, concatSimMsg.msgData);
152 memset(sqlQuery, 0x00, sizeof(sqlQuery));
154 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, %d, 0, %d, 0, 0);",
155 MSGFW_MESSAGE_TABLE_NAME, msgId, convId, pSimMsgInfo->folderId, pSimMsgInfo->storageId,
156 pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
157 pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
158 pSimMsgInfo->direction, 0, pSimMsgInfo->bBackup, MSG_DELIVERY_REPORT_NONE, MSG_READ_REPORT_NONE);
160 MSG_DEBUG("QUERY : %s", sqlQuery);
162 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
163 dbHandle.endTrans(false);
164 return MSG_ERR_DB_PREPARE;
167 dbHandle.bindText(pSimMsgInfo->subject, 1);
169 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false)
170 dbHandle.bindText(concatSimMsg.msgData, 2);
172 dbHandle.bindText(pSimMsgInfo->msgText, 2);
174 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
175 dbHandle.endTrans(false);
176 return MSG_ERR_DB_STEP;
179 dbHandle.finalizeQuery();
181 /** Insert to Sim table */
182 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
184 MSG_DEBUG("sim count : %d", concatSimMsg.simIdCnt);
186 for (unsigned int i = 0; i < concatSimMsg.simIdCnt; i++) {
187 memset(sqlQuery, 0x00, sizeof(sqlQuery));
188 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
189 MSGFW_SIM_MSG_TABLE_NAME, msgId, concatSimMsg.simIdList[i]);
191 MSG_DEBUG("QUERY : %s", sqlQuery);
193 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
194 dbHandle.endTrans(false);
195 return MSG_ERR_DB_EXEC;
199 memset(sqlQuery, 0x00, sizeof(sqlQuery));
200 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
201 MSGFW_SIM_MSG_TABLE_NAME, msgId, simId);
203 MSG_DEBUG("QUERY : %s", sqlQuery);
205 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
206 dbHandle.endTrans(false);
207 return MSG_ERR_DB_EXEC;
211 /** Update conversation table. */
212 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
213 dbHandle.endTrans(false);
214 return MSG_ERR_STORAGE_ERROR;
217 dbHandle.endTrans(true);
223 msg_error_t SmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
227 msg_error_t err = MSG_SUCCESS;
229 /** Check whether storage is full or not */
230 err = checkStorageStatus(pMsgInfo);
232 if (err != MSG_SUCCESS) {
236 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
238 MSG_DEBUG("Add Normal SMS");
240 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
241 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
243 if (err == MSG_SUCCESS) {
244 MSG_DEBUG("Success to saveSimMessage.");
246 MSG_DEBUG("Fail to saveSimMessage : [%d]", err);
249 /** Class 0 Msg should be saved in hidden folder */
250 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
251 pMsgInfo->folderId = 0;
255 err = addSmsMessage(pMsgInfo);
258 } else if ((pMsgInfo->msgType.subType == MSG_CB_SMS) || (pMsgInfo->msgType.subType == MSG_JAVACB_SMS)) {
259 MSG_DEBUG("Add CB Message");
260 err = addCbMessage(pMsgInfo);
261 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
262 MSG_DEBUG("Add Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
263 err = addReplaceTypeMsg(pMsgInfo);
264 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
265 MSG_DEBUG("Add MWI Message");
266 err = addSmsMessage(pMsgInfo);
267 } else if ((pMsgInfo->msgType.subType == MSG_WAP_SI_SMS) || (pMsgInfo->msgType.subType == MSG_WAP_CO_SMS)) {
268 MSG_DEBUG("Add WAP Push Message");
269 switch (pMsgInfo->msgType.subType)
273 // save push message information
274 err = addWAPMessage(pMsgInfo);
280 err = handleCOWAPMessage(pMsgInfo);
284 } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
285 MSG_DEBUG("Add Status Report");
286 err = addSmsMessage(pMsgInfo);
289 if (err == MSG_SUCCESS) {
290 MSG_DEBUG("Success to add message !!");
292 MSG_DEBUG("fail to add message !! : [%d]", err);
299 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
301 msg_error_t err = MSG_SUCCESS;
303 unsigned int rowId = 0;
304 msg_thread_id_t convId = 0;
306 char sqlQuery[MAX_QUERY_LEN+1];
308 dbHandle.beginTrans();
310 if (pMsgInfo->nAddressCnt > 0) {
312 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
314 if (err != MSG_SUCCESS) {
315 dbHandle.endTrans(false);
319 pMsgInfo->threadId = convId;
322 /** Add Message Table */
323 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
326 dbHandle.endTrans(false);
327 return MSG_ERR_DB_ROW;
330 /** Update conversation table */
331 err = MsgStoUpdateConversation(&dbHandle, convId);
333 if (err != MSG_SUCCESS) {
334 dbHandle.endTrans(false);
338 dbHandle.endTrans(true);
340 pMsgInfo->msgId = (msg_message_id_t)rowId;
348 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
350 msg_error_t err = MSG_SUCCESS;
352 char sqlQuery[MAX_QUERY_LEN+1];
354 msg_thread_id_t convId = 0;
356 dbHandle.beginTrans();
358 if (pMsg->nAddressCnt > 0) {
360 err = MsgStoAddAddress(&dbHandle, pMsg, &convId);
362 if (err != MSG_SUCCESS) {
363 dbHandle.endTrans(false);
370 char* pFileData = NULL;
371 AutoPtr<char> buf(&pFileData);
374 if (pMsg->bTextSms == false) {
375 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
376 dbHandle.endTrans(false);
377 return MSG_ERR_STORAGE_ERROR;
381 /** Update Message */
382 memset(sqlQuery, 0x00, sizeof(sqlQuery));
384 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ADDRESS_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
385 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
386 BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
387 MSGFW_MESSAGE_TABLE_NAME, convId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
388 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->bBackup, pMsg->msgId);
390 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
391 dbHandle.endTrans(false);
392 return MSG_ERR_DB_EXEC;
395 dbHandle.bindText(pMsg->subject, 1);
397 dbHandle.bindText(pMsg->msgData, 2);
399 dbHandle.bindText(pMsg->thumbPath, 3);
401 if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
402 dbHandle.bindText(pFileData, 4);
404 dbHandle.bindText(pMsg->msgText, 4);
406 MSG_DEBUG("%s", sqlQuery);
408 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
409 dbHandle.endTrans(false);
410 return MSG_ERR_DB_EXEC;
413 dbHandle.finalizeQuery();
415 err = MsgStoUpdateConversation(&dbHandle, convId);
417 if (err != MSG_SUCCESS) {
418 dbHandle.endTrans(false);
419 return MSG_ERR_STORAGE_ERROR;
422 err = MsgStoClearConversationTable(&dbHandle);
424 if (err != MSG_SUCCESS) {
425 dbHandle.endTrans(false);
426 return MSG_ERR_STORAGE_ERROR;
429 dbHandle.endTrans(true);
435 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
439 msg_error_t err = MSG_SUCCESS;
441 char sqlQuery[MAX_QUERY_LEN+1];
443 /** Get SUB_TYPE, STORAGE_ID */
444 memset(sqlQuery, 0x00, sizeof(sqlQuery));
445 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
446 FROM %s WHERE MSG_ID = %d;",
447 MSGFW_MESSAGE_TABLE_NAME, msgId);
449 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
450 return MSG_ERR_DB_PREPARE;
452 MSG_MESSAGE_TYPE_S msgType;
453 msg_folder_id_t folderId;
455 msg_thread_id_t convId;
457 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
458 msgType.mainType = dbHandle.columnInt(0);
459 msgType.subType = dbHandle.columnInt(1);
460 folderId = dbHandle.columnInt(2);
461 convId = dbHandle.columnInt(3);
463 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
465 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
466 dbHandle.finalizeQuery();
467 return MSG_ERR_DB_STEP;
470 dbHandle.finalizeQuery();
472 dbHandle.beginTrans();
474 memset(sqlQuery, 0x00, sizeof(sqlQuery));
475 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
477 /** Delete SMS Send Option */
478 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
479 dbHandle.endTrans(false);
480 return MSG_ERR_DB_EXEC;
483 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
484 memset(sqlQuery, 0x00, sizeof(sqlQuery));
485 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
487 /** Delete Push Message from push table */
488 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
489 dbHandle.endTrans(false);
490 return MSG_ERR_DB_EXEC;
492 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
493 memset(sqlQuery, 0x00, sizeof(sqlQuery));
494 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
496 /** Delete Push Message from push table */
497 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
498 dbHandle.endTrans(false);
499 return MSG_ERR_DB_EXEC;
501 } else if (msgType.subType == MSG_SYNCML_CP) {
502 memset(sqlQuery, 0x00, sizeof(sqlQuery));
503 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
505 /** Delete SyncML Message from syncML table */
506 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
507 dbHandle.endTrans(false);
508 return MSG_ERR_DB_EXEC;
512 memset(sqlQuery, 0x00, sizeof(sqlQuery));
513 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
515 /** Delete Message from msg table */
516 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
517 dbHandle.endTrans(false);
518 return MSG_ERR_DB_EXEC;
521 /** Clear Conversation table */
522 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
523 dbHandle.endTrans(false);
524 return MSG_ERR_DB_EXEC;
527 /** Update conversation table.*/
528 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
529 dbHandle.endTrans(false);
530 return MSG_ERR_STORAGE_ERROR;
533 dbHandle.endTrans(true);
535 if (folderId == MSG_INBOX_ID) {
536 msgType.classType = MSG_CLASS_NONE;
538 /** Set memory status in SIM */
539 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
540 MSG_DEBUG("Set Memory Status");
541 SmsPlgSetMemoryStatus(MSG_SUCCESS);
545 int smsCnt = 0, mmsCnt = 0;
547 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
548 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
550 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
551 MsgDeleteNotiByMsgId(msgId);
557 msg_error_t SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
559 msg_error_t err = MSG_SUCCESS;
561 unsigned int rowId = 0;
562 msg_thread_id_t convId = 0;
564 char sqlQuery[MAX_QUERY_LEN+1];
566 dbHandle.beginTrans();
568 if (pMsgInfo->nAddressCnt > 0) {
569 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
571 if (err != MSG_SUCCESS) {
572 dbHandle.endTrans(false);
576 pMsgInfo->threadId = convId;
579 /** Add Message Table */
580 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
583 dbHandle.endTrans(false);
584 return MSG_ERR_DB_ROW;
588 unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
590 /** Add CB Msg in MSG_CBMSG_TABLE */
591 memset(sqlQuery, 0x00, sizeof(sqlQuery));
593 sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
594 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
596 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
597 dbHandle.endTrans(false);
598 return MSG_ERR_DB_EXEC;
601 /** Update conversation table. */
602 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
603 dbHandle.endTrans(false);
604 return MSG_ERR_STORAGE_ERROR;
607 dbHandle.endTrans(true);
610 pMsgInfo->msgId = (msg_message_id_t)rowId;
616 msg_error_t SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
618 msg_error_t err = MSG_SUCCESS;
620 char sqlQuery[MAX_QUERY_LEN+1];
622 unsigned int retCnt = 0;
623 msg_thread_id_t convId = 0;
625 /** Check if new address or not */
626 if (MsgExistAddress(&dbHandle, pMsgInfo, &convId) == true) {
627 MSG_DEBUG("Address Info. exists [%d]", convId);
629 /** Find Replace Type Msg : Same Replace Type, Same Origin Address */
630 memset(sqlQuery, 0x00, sizeof(sqlQuery));
631 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), MSG_ID FROM %s WHERE SUB_TYPE = %d AND B.CONV_ID = %d;",
632 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId);
634 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
635 return MSG_ERR_DB_PREPARE;
637 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
638 retCnt = dbHandle.columnInt(0);
639 pMsgInfo->msgId = dbHandle.columnInt(1);
641 dbHandle.finalizeQuery();
642 return MSG_ERR_DB_STEP;
645 dbHandle.finalizeQuery();
648 /** Update New Replace Type Msg */
650 MSG_DEBUG("Update Replace Type Msg");
651 err = updateSmsMessage(pMsgInfo);
652 } else if (retCnt == 0) { /** Insert New Replace Type Msg */
653 MSG_DEBUG("Insert Replace Type Msg");
654 err = addSmsMessage(pMsgInfo);
661 msg_error_t SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
663 msg_error_t err = MSG_SUCCESS;
665 MSG_PUSH_MESSAGE_S pushMsg = {};
667 char sqlQuery[MAX_QUERY_LEN+1];
668 memset(sqlQuery, 0x00, sizeof(sqlQuery));
672 char* pFileData = NULL;
673 AutoPtr<char> buf(&pFileData);
675 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
676 return MSG_ERR_STORAGE_ERROR;
678 MSG_DEBUG("fileSize : %d", fileSize);
680 memcpy(&pushMsg, pFileData, fileSize);
682 /** Delete temporary file */
683 MsgDeleteFile(pMsgInfo->msgData);
685 /** check pPushMsg data */
687 MSG_DEBUG("check pushMsg data");
688 MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
689 MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
690 MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
691 MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
692 MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
693 MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
694 MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
696 bool bProceed = true;
698 /** check validation of contents */
699 if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
700 MSG_DEBUG("Fail to check Push Message validation.");
703 /** if validation check value is false */
704 /** return and drop message. */
705 if (bProceed == false)
706 return MSG_ERR_INVALID_MESSAGE;
708 /** update subject */
709 int len = strlen(pushMsg.contents);
711 if (len > MAX_SUBJECT_LEN) {
712 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
713 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
715 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
718 /** Update Msg Text - remove */
719 strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
720 pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
722 pMsgInfo->bTextSms = true;
723 pMsgInfo->folderId = MSG_INBOX_ID;
724 pMsgInfo->storageId = MSG_STORAGE_PHONE;
726 msg_thread_id_t convId = 0;
728 dbHandle.beginTrans();
730 if (pMsgInfo->nAddressCnt > 0) {
732 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
734 if (err != MSG_SUCCESS) {
735 dbHandle.endTrans(false);
740 /** get last row count for Message id */
741 unsigned int rowId = 0;
743 /** Add Message Table */
744 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
747 dbHandle.endTrans(false);
748 return MSG_ERR_DB_ROW;
751 /** add msg_push_table */
752 snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
753 MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
755 if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
756 dbHandle.endTrans(false);
760 dbHandle.bindText(pushMsg.id, 1);
762 dbHandle.bindText(pushMsg.href, 2);
764 dbHandle.bindText(pushMsg.contents, 3);
766 if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
767 dbHandle.endTrans(false);
771 /** Update conversation table. */
772 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
773 dbHandle.endTrans(false);
774 return MSG_ERR_STORAGE_ERROR;
777 dbHandle.endTrans(true);
779 pMsgInfo->msgId = (msg_message_id_t)rowId;
785 msg_error_t SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
787 msg_error_t err = MSG_SUCCESS;
789 char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
790 char sqlQuery[MAX_QUERY_LEN+1];
794 char* pFileData = NULL;
795 AutoPtr<char> buf(&pFileData);
797 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
798 return MSG_ERR_STORAGE_ERROR;
800 MSG_PUSH_CACHEOP_S *pPushMsg;
802 pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
804 for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
808 memset(href, 0x00, sizeof(href));
809 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
811 memset(sqlQuery, 0x00, sizeof(sqlQuery));
812 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
814 dbHandle.beginTrans();
816 err = dbHandle.prepareQuery(sqlQuery);
818 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
820 msgid = dbHandle.getColumnToInt(1);
822 memset(sqlQuery, 0x00, sizeof(sqlQuery));
823 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
825 /** Delete Message from Push table */
826 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
827 dbHandle.finalizeQuery();
828 dbHandle.endTrans(false);
832 memset(sqlQuery, 0x00, sizeof(sqlQuery));
833 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
835 /** Delete Message from msg table */
836 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
837 dbHandle.finalizeQuery();
838 dbHandle.endTrans(false);
842 /** Update all Address */
843 if (updateAllAddress() != MSG_SUCCESS) {
844 dbHandle.finalizeQuery();
845 dbHandle.endTrans(false);
849 /** Clear Conversation table */
850 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
851 dbHandle.finalizeQuery();
852 dbHandle.endTrans(false);
857 dbHandle.finalizeQuery();
859 dbHandle.endTrans(true);
862 for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
866 memset(href, 0x00, sizeof(href));
867 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
869 memset(sqlQuery, 0x00, sizeof(sqlQuery));
870 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
872 dbHandle.beginTrans();
874 err = dbHandle.prepareQuery(sqlQuery);
876 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
878 msgid = dbHandle.getColumnToInt(1);
880 memset(sqlQuery, 0x00, sizeof(sqlQuery));
881 sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
883 /** Delete Message from Push table */
884 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
885 dbHandle.finalizeQuery();
886 dbHandle.endTrans(false);
890 memset(sqlQuery, 0x00, sizeof(sqlQuery));
891 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
893 /** Delete Message from msg table */
894 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
895 dbHandle.finalizeQuery();
896 dbHandle.endTrans(false);
900 /** Update all Address */
901 if (updateAllAddress() != MSG_SUCCESS) {
902 dbHandle.finalizeQuery();
903 dbHandle.endTrans(false);
907 /** Clear Address table */
908 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
909 dbHandle.finalizeQuery();
910 dbHandle.endTrans(false);
915 dbHandle.finalizeQuery();
917 dbHandle.endTrans(true);
920 /** delete temporary file */
921 MsgDeleteFile(pMsgInfo->msgData);
927 msg_error_t SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
929 msg_error_t err = MSG_SUCCESS;
931 unsigned long oldExpireTime = 0;
934 char sqlQuery[MAX_QUERY_LEN+1];
936 /** is push message is expired?? */
937 if (pPushMsg->received > pPushMsg->expires) {
938 MSG_DEBUG("Push Message is expired.");
944 if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
945 memset(sqlQuery, 0x00, sizeof(sqlQuery));
946 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
947 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
949 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
950 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
953 err = dbHandle.getTable(sqlQuery, &rowCnt);
956 dbHandle.freeTable();
960 oldExpireTime = dbHandle.getColumnToInt(1);
962 dbHandle.freeTable();
964 if (pPushMsg->created < oldExpireTime) {
965 MSG_DEBUG("Push Message is expired.");
974 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
976 msg_error_t err = MSG_SUCCESS;
978 err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
980 if (err != MSG_SUCCESS) {
982 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
983 bool bAutoErase = false;
985 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
987 MSG_DEBUG("bAutoErase: %d", bAutoErase);
989 if (bAutoErase == true) {
990 msg_message_id_t msgId;
992 /** Find the oldest message's msgId */
993 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
995 if (err != MSG_SUCCESS)
998 /** Delete the corresponding message. */
999 err = deleteSmsMessage(msgId);
1010 msg_error_t SmsPluginStorage::updateAllAddress()
1012 msg_error_t err = MSG_SUCCESS;
1014 int rowCnt = 0, index = 1;
1015 char sqlQuery[MAX_QUERY_LEN+1];
1017 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1019 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
1021 err = dbHandle.getTable(sqlQuery, &rowCnt);
1023 if (err == MSG_ERR_DB_NORECORD) {
1024 dbHandle.freeTable();
1026 } else if ( err != MSG_SUCCESS) {
1027 dbHandle.freeTable();
1032 for (int i = 0; i < rowCnt; i++) {
1034 err = MsgStoUpdateConversation(&dbHandle, index++);
1036 if (err != MSG_SUCCESS)
1040 dbHandle.freeTable();