3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
5 * This file is part of msg-service.
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 * Sangkoo Kim <sangkoo.kim@samsung.com>
9 * Seunghwan Lee <sh.cat.lee@samsung.com>
10 * SoonMin Jung <sm0415.jung@samsung.com>
11 * Jae-Young Lee <jy4710.lee@samsung.com>
12 * KeeBum Kim <keebum.kim@samsung.com>
14 * PROPRIETARY/CONFIDENTIAL
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
34 #include "MsgCppTypes.h"
35 #include "MsgException.h"
36 #include "MsgContact.h"
37 #include "MsgUtilFile.h"
38 #include "MsgUtilStorage.h"
39 #include "MsgGconfWrapper.h"
40 #include "MsgNotificationWrapper.h"
41 #include "SmsPluginMain.h"
42 #include "SmsPluginSimMsg.h"
43 #include "SmsPluginStorage.h"
46 /*==================================================================================================
47 IMPLEMENTATION OF SmsPluginStorage - Member Functions
48 ==================================================================================================*/
49 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
52 SmsPluginStorage::SmsPluginStorage()
57 SmsPluginStorage::~SmsPluginStorage()
59 if (dbHandle.disconnect() != MSG_SUCCESS) {
60 MSG_DEBUG("DB Disconnect Fail");
65 SmsPluginStorage* SmsPluginStorage::instance()
68 MSG_DEBUG("pInstance is NULL. Now creating instance.");
69 pInstance = new SmsPluginStorage();
76 MSG_ERROR_T SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_NETWORK_STATUS_T Status)
80 char sqlQuery[MAX_QUERY_LEN+1];
82 memset(sqlQuery, 0x00, sizeof(sqlQuery));
84 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, Status);
86 /** Move Msg to SENTBOX */
87 if (Status == MSG_NETWORK_SEND_SUCCESS) {
88 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
89 MSGFW_MESSAGE_TABLE_NAME, Status, MSG_SENTBOX_ID, pMsgInfo->msgId);
91 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
92 MSGFW_MESSAGE_TABLE_NAME, Status, pMsgInfo->msgId);
95 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
96 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
97 return MSG_ERR_DB_EXEC;
106 MSG_ERROR_T SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo)
108 MSG_ERROR_T err = MSG_SUCCESS;
110 unsigned int msgId = 0;
111 unsigned int addrId = 0;
112 unsigned int simId = 0;
114 char sqlQuery[MAX_QUERY_LEN+1];
116 dbHandle.beginTrans();
118 err = MsgStoAddAddress(&dbHandle, &(pSimMsgInfo->addressList[0]), &addrId);
120 if (err != MSG_SUCCESS) {
121 dbHandle.endTrans(false);
125 err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
127 if (err != MSG_SUCCESS) {
128 dbHandle.endTrans(false);
132 simId = pSimMsgInfo->msgId;
133 pSimMsgInfo->msgId = (MSG_MESSAGE_ID_T)msgId;
135 SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
137 /** Get Data from Concat SIM Msg */
138 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
142 char* pFileData = NULL;
143 AutoPtr<char> buf(&pFileData);
145 if (MsgOpenAndReadFile(pSimMsgInfo->msgData, &pFileData, &fileSize) == false) {
146 dbHandle.endTrans(false);
147 return MSG_ERR_STORAGE_ERROR;
151 memcpy(&concatSimMsg, (SMS_CONCAT_SIM_MSG_S*)pFileData, fileSize);
153 /** Delete temporary file */
154 MsgDeleteFile(pSimMsgInfo->msgData); /** ipc */
156 MSG_DEBUG("SIM ID [%d], MSG DATA [%s]", concatSimMsg.simIdCnt, concatSimMsg.msgData);
160 memset(sqlQuery, 0x00, sizeof(sqlQuery));
162 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, %d, 0, %d, 0, 0);",
163 MSGFW_MESSAGE_TABLE_NAME, msgId, addrId, pSimMsgInfo->folderId, 0, pSimMsgInfo->storageId,
164 pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
165 pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
166 pSimMsgInfo->direction, pSimMsgInfo->scheduledTime, pSimMsgInfo->bBackup, MSG_DELIVERY_REPORT_NONE, MSG_READ_REPORT_NONE);
168 MSG_DEBUG("QUERY : %s", sqlQuery);
170 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
171 dbHandle.endTrans(false);
172 return MSG_ERR_DB_PREPARE;
175 dbHandle.bindText(pSimMsgInfo->subject, 1);
177 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false)
178 dbHandle.bindText(concatSimMsg.msgData, 2);
180 dbHandle.bindText(pSimMsgInfo->msgText, 2);
182 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
183 dbHandle.endTrans(false);
184 return MSG_ERR_DB_STEP;
187 dbHandle.finalizeQuery();
189 /** Insert to Sim table */
190 if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
192 MSG_DEBUG("sim count : %d", concatSimMsg.simIdCnt);
194 for (unsigned int i = 0; i < concatSimMsg.simIdCnt; i++) {
195 memset(sqlQuery, 0x00, sizeof(sqlQuery));
196 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
197 MSGFW_SIM_MSG_TABLE_NAME, msgId, concatSimMsg.simIdList[i]);
199 MSG_DEBUG("QUERY : %s", sqlQuery);
201 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
202 dbHandle.endTrans(false);
203 return MSG_ERR_DB_EXEC;
207 memset(sqlQuery, 0x00, sizeof(sqlQuery));
208 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
209 MSGFW_SIM_MSG_TABLE_NAME, msgId, simId);
211 MSG_DEBUG("QUERY : %s", sqlQuery);
213 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
214 dbHandle.endTrans(false);
215 return MSG_ERR_DB_EXEC;
219 /** Update Address Info. */
220 if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
221 dbHandle.endTrans(false);
222 return MSG_ERR_STORAGE_ERROR;
225 dbHandle.endTrans(true);
231 MSG_ERROR_T SmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
235 MSG_ERROR_T err = MSG_SUCCESS;
237 /** Check whether storage is full or not */
238 err = checkStorageStatus(pMsgInfo);
240 if (err != MSG_SUCCESS) {
244 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
246 MSG_DEBUG("Add Normal SMS");
248 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
249 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
251 if (err == MSG_SUCCESS) {
252 MSG_DEBUG("Success to saveSimMessage.");
254 MSG_DEBUG("Fail to saveSimMessage : [%d]", err);
257 /** Class 0 Msg should be saved in hidden folder */
258 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
259 pMsgInfo->folderId = 0;
263 err = addSmsMessage(pMsgInfo);
266 } else if ((pMsgInfo->msgType.subType == MSG_CB_SMS) || (pMsgInfo->msgType.subType == MSG_JAVACB_SMS)) {
267 MSG_DEBUG("Add CB Message");
268 err = addCbMessage(pMsgInfo);
269 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
270 MSG_DEBUG("Add Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
271 err = addReplaceTypeMsg(pMsgInfo);
272 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
273 MSG_DEBUG("Add MWI Message");
274 err = addSmsMessage(pMsgInfo);
275 } else if ((pMsgInfo->msgType.subType == MSG_WAP_SI_SMS) || (pMsgInfo->msgType.subType == MSG_WAP_CO_SMS)) {
276 MSG_DEBUG("Add WAP Push Message");
277 switch (pMsgInfo->msgType.subType)
281 // save push message information
282 err = addWAPMessage(pMsgInfo);
288 err = handleCOWAPMessage(pMsgInfo);
292 } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
293 MSG_DEBUG("Add Status Report");
294 err = addSmsMessage(pMsgInfo);
297 if (err == MSG_SUCCESS) {
298 MSG_DEBUG("Success to add message !!");
300 MSG_DEBUG("fail to add message !! : [%d]", err);
307 MSG_ERROR_T SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
309 MSG_ERROR_T err = MSG_SUCCESS;
311 unsigned int rowId = 0;
312 unsigned int addrId = 0;
314 char sqlQuery[MAX_QUERY_LEN+1];
316 dbHandle.beginTrans();
318 if (pMsgInfo->nAddressCnt > 0) {
320 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
322 if (err != MSG_SUCCESS) {
323 dbHandle.endTrans(false);
327 pMsgInfo->addressList[0].threadId = (MSG_THREAD_ID_T)addrId;
330 /** Add Message Table */
331 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
334 dbHandle.endTrans(false);
335 return MSG_ERR_DB_ROW;
338 /** Update Address table */
339 err = MsgStoUpdateAddress(&dbHandle, addrId);
341 if (err != MSG_SUCCESS) {
342 dbHandle.endTrans(false);
346 dbHandle.endTrans(true);
348 pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
349 pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
357 MSG_ERROR_T SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
359 MSG_ERROR_T err = MSG_SUCCESS;
361 char sqlQuery[MAX_QUERY_LEN+1];
363 unsigned int addrId = 0;
365 dbHandle.beginTrans();
367 if (pMsg->nAddressCnt > 0) {
369 err = MsgStoAddAddress(&dbHandle, &(pMsg->addressList[0]), &addrId);
371 if (err != MSG_SUCCESS) {
372 dbHandle.endTrans(false);
379 char* pFileData = NULL;
380 AutoPtr<char> buf(&pFileData);
383 if (pMsg->bTextSms == false) {
384 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
385 dbHandle.endTrans(false);
386 return MSG_ERR_STORAGE_ERROR;
390 /** Update Message */
391 memset(sqlQuery, 0x00, sizeof(sqlQuery));
393 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ADDRESS_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
394 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, \
395 PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, SCHEDULED_TIME = %lu, BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? \
397 MSGFW_MESSAGE_TABLE_NAME, addrId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
398 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->scheduledTime, pMsg->bBackup, pMsg->msgId);
400 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
401 dbHandle.endTrans(false);
402 return MSG_ERR_DB_EXEC;
405 dbHandle.bindText(pMsg->subject, 1);
407 dbHandle.bindText(pMsg->msgData, 2);
409 dbHandle.bindText(pMsg->thumbPath, 3);
411 if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
412 dbHandle.bindText(pFileData, 4);
414 dbHandle.bindText(pMsg->msgText, 4);
416 MSG_DEBUG("%s", sqlQuery);
418 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
419 dbHandle.endTrans(false);
420 return MSG_ERR_DB_EXEC;
423 dbHandle.finalizeQuery();
425 err = MsgStoUpdateAddress(&dbHandle, addrId);
427 if (err != MSG_SUCCESS) {
428 dbHandle.endTrans(false);
429 return MSG_ERR_STORAGE_ERROR;
432 err = MsgStoClearAddressTable(&dbHandle);
434 if (err != MSG_SUCCESS) {
435 dbHandle.endTrans(false);
436 return MSG_ERR_STORAGE_ERROR;
439 dbHandle.endTrans(true);
445 MSG_ERROR_T SmsPluginStorage::deleteSmsMessage(MSG_MESSAGE_ID_T MsgId)
449 MSG_ERROR_T err = MSG_SUCCESS;
451 char sqlQuery[MAX_QUERY_LEN+1];
453 /** Get SUB_TYPE, STORAGE_ID */
454 memset(sqlQuery, 0x00, sizeof(sqlQuery));
455 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
456 FROM %s WHERE MSG_ID = %d;",
457 MSGFW_MESSAGE_TABLE_NAME, MsgId);
459 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
460 return MSG_ERR_DB_PREPARE;
462 MSG_MESSAGE_TYPE_S msgType;
463 MSG_FOLDER_ID_T folderId;
467 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
468 msgType.mainType = dbHandle.columnInt(0);
469 msgType.subType = dbHandle.columnInt(1);
470 folderId = dbHandle.columnInt(2);
471 addrId = dbHandle.columnInt(3);
473 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] AddressId:[%d]", msgType.mainType, msgType.subType, folderId, addrId);
475 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
476 dbHandle.finalizeQuery();
477 return MSG_ERR_DB_STEP;
480 dbHandle.finalizeQuery();
482 dbHandle.beginTrans();
484 memset(sqlQuery, 0x00, sizeof(sqlQuery));
485 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SCHEDULED_MSG_TABLE_NAME, MsgId);
487 /** Delete Message from scheduled msg table */
488 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
489 dbHandle.endTrans(false);
490 return MSG_ERR_DB_EXEC;
493 memset(sqlQuery, 0x00, sizeof(sqlQuery));
494 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, MsgId);
496 /** Delete SMS Send Option */
497 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
498 dbHandle.endTrans(false);
499 return MSG_ERR_DB_EXEC;
502 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
503 memset(sqlQuery, 0x00, sizeof(sqlQuery));
504 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, MsgId);
506 /** Delete Push Message from push table */
507 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
508 dbHandle.endTrans(false);
509 return MSG_ERR_DB_EXEC;
511 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
512 memset(sqlQuery, 0x00, sizeof(sqlQuery));
513 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, MsgId);
515 /** Delete Push Message from push table */
516 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
517 dbHandle.endTrans(false);
518 return MSG_ERR_DB_EXEC;
520 } else if (msgType.subType == MSG_SYNCML_CP) {
521 memset(sqlQuery, 0x00, sizeof(sqlQuery));
522 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, MsgId);
524 /** Delete SyncML Message from syncML table */
525 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
526 dbHandle.endTrans(false);
527 return MSG_ERR_DB_EXEC;
531 memset(sqlQuery, 0x00, sizeof(sqlQuery));
532 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, MsgId);
534 /** Delete Message from msg table */
535 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
536 dbHandle.endTrans(false);
537 return MSG_ERR_DB_EXEC;
540 /** Clear Address table */
541 if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
542 dbHandle.endTrans(false);
543 return MSG_ERR_DB_EXEC;
546 /** Update Address Info.*/
547 if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
548 dbHandle.endTrans(false);
549 return MSG_ERR_STORAGE_ERROR;
552 dbHandle.endTrans(true);
554 if (folderId == MSG_INBOX_ID) {
555 msgType.classType = MSG_CLASS_NONE;
557 /** Set memory status in SIM */
558 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
559 MSG_DEBUG("Set Memory Status");
560 SmsPlgSetMemoryStatus(MSG_SUCCESS);
564 int smsCnt = 0, mmsCnt = 0;
566 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
567 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
569 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
570 MsgDeleteNotiByMsgId(MsgId);
576 MSG_ERROR_T SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
578 MSG_ERROR_T err = MSG_SUCCESS;
580 unsigned int rowId = 0, addrId = 0;
582 char sqlQuery[MAX_QUERY_LEN+1];
584 dbHandle.beginTrans();
586 if (pMsgInfo->nAddressCnt > 0) {
587 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
589 if (err != MSG_SUCCESS) {
590 dbHandle.endTrans(false);
594 pMsgInfo->addressList[0].threadId = (MSG_THREAD_ID_T)addrId;
597 /** Add Message Table */
598 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
601 dbHandle.endTrans(false);
602 return MSG_ERR_DB_ROW;
606 unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
608 /** Add CB Msg in MSG_CBMSG_TABLE */
609 memset(sqlQuery, 0x00, sizeof(sqlQuery));
611 sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
612 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
614 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
615 dbHandle.endTrans(false);
616 return MSG_ERR_DB_EXEC;
619 /** Update Address Info. */
620 if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
621 dbHandle.endTrans(false);
622 return MSG_ERR_STORAGE_ERROR;
625 dbHandle.endTrans(true);
628 pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
629 pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
635 MSG_ERROR_T SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
637 MSG_ERROR_T err = MSG_SUCCESS;
639 char sqlQuery[MAX_QUERY_LEN+1];
641 unsigned int addrId = 0, retCnt = 0;
643 /** Check if new address or not */
644 if (MsgExistAddress(&dbHandle, pMsgInfo->addressList[0].addressVal, &addrId) == true) {
645 MSG_DEBUG("Address Info. exists [%d] [%s]", addrId, pMsgInfo->addressList[0].addressVal);
647 /** Find Replace Type Msg : Same Replace Type, Same Origin Address */
648 memset(sqlQuery, 0x00, sizeof(sqlQuery));
649 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), A.MSG_ID FROM %s A, %s B \
650 WHERE A.ADDRESS_ID = B.ADDRESS_ID AND A.SUB_TYPE = %d AND B.ADDRESS_ID = %d;",
651 MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, pMsgInfo->msgType.subType, addrId);
653 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
654 return MSG_ERR_DB_PREPARE;
656 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
657 retCnt = dbHandle.columnInt(0);
658 pMsgInfo->msgId = dbHandle.columnInt(1);
660 dbHandle.finalizeQuery();
661 return MSG_ERR_DB_STEP;
664 dbHandle.finalizeQuery();
667 /** Update New Replace Type Msg */
669 MSG_DEBUG("Update Replace Type Msg");
670 err = updateSmsMessage(pMsgInfo);
671 } else if (retCnt == 0) { /** Insert New Replace Type Msg */
672 MSG_DEBUG("Insert Replace Type Msg");
673 err = addSmsMessage(pMsgInfo);
680 MSG_ERROR_T SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
682 MSG_ERROR_T err = MSG_SUCCESS;
684 MSG_PUSH_MESSAGE_S pushMsg = {};
686 char sqlQuery[MAX_QUERY_LEN+1];
687 memset(sqlQuery, 0x00, sizeof(sqlQuery));
691 char* pFileData = NULL;
692 AutoPtr<char> buf(&pFileData);
694 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
695 return MSG_ERR_STORAGE_ERROR;
697 MSG_DEBUG("fileSize : %d", fileSize);
699 memcpy(&pushMsg, pFileData, fileSize);
701 /** Delete temporary file */
702 MsgDeleteFile(pMsgInfo->msgData);
704 /** check pPushMsg data */
706 MSG_DEBUG("check pushMsg data");
707 MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
708 MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
709 MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
710 MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
711 MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
712 MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
713 MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
715 bool bProceed = true;
717 /** check validation of contents */
718 if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
719 MSG_DEBUG("Fail to check Push Message validation.");
722 /** if validation check value is false */
723 /** return and drop message. */
724 if (bProceed == false)
725 return MSG_ERR_INVALID_MESSAGE;
727 /** update subject */
728 int len = strlen(pushMsg.contents);
730 if (len > MAX_SUBJECT_LEN) {
731 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
732 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
734 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
737 /** Update Msg Text - remove */
738 strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
739 pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
741 pMsgInfo->bTextSms = true;
742 pMsgInfo->folderId = MSG_INBOX_ID;
743 pMsgInfo->storageId = MSG_STORAGE_PHONE;
745 unsigned int addrId = 0;
747 dbHandle.beginTrans();
749 if (pMsgInfo->nAddressCnt > 0) {
751 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
753 if (err != MSG_SUCCESS) {
754 dbHandle.endTrans(false);
759 /** get last row count for Message id */
760 unsigned int rowId = 0;
762 /** Add Message Table */
763 rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
766 dbHandle.endTrans(false);
767 return MSG_ERR_DB_ROW;
770 /** add msg_push_table */
771 snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
772 MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
774 if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
775 dbHandle.endTrans(false);
779 dbHandle.bindText(pushMsg.id, 1);
781 dbHandle.bindText(pushMsg.href, 2);
783 dbHandle.bindText(pushMsg.contents, 3);
785 if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
786 dbHandle.endTrans(false);
790 /** Update Address Info. */
791 if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
792 dbHandle.endTrans(false);
793 return MSG_ERR_STORAGE_ERROR;
796 dbHandle.endTrans(true);
798 pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
799 pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
805 MSG_ERROR_T SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
807 MSG_ERROR_T err = MSG_SUCCESS;
809 char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
810 char sqlQuery[MAX_QUERY_LEN+1];
814 char* pFileData = NULL;
815 AutoPtr<char> buf(&pFileData);
817 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
818 return MSG_ERR_STORAGE_ERROR;
820 MSG_PUSH_CACHEOP_S *pPushMsg;
822 pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
824 for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
828 memset(href, 0x00, sizeof(href));
829 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
831 memset(sqlQuery, 0x00, sizeof(sqlQuery));
832 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';",
833 MSGFW_PUSH_MSG_TABLE_NAME, href);
835 dbHandle.beginTrans();
837 err = dbHandle.prepareQuery(sqlQuery);
839 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
841 msgid = dbHandle.getColumnToInt(1);
843 memset(sqlQuery, 0x00, sizeof(sqlQuery));
844 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;",
845 MSGFW_PUSH_MSG_TABLE_NAME, msgid);
847 /** Delete Message from Push table */
848 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
849 dbHandle.finalizeQuery();
850 dbHandle.endTrans(false);
854 memset(sqlQuery, 0x00, sizeof(sqlQuery));
855 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
856 MSGFW_MESSAGE_TABLE_NAME, msgid);
858 /** Delete Message from msg table */
859 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
860 dbHandle.finalizeQuery();
861 dbHandle.endTrans(false);
865 /** Update all Address */
866 if (updateAllAddress() != MSG_SUCCESS) {
867 dbHandle.finalizeQuery();
868 dbHandle.endTrans(false);
872 /** Clear Address table */
873 if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
874 dbHandle.finalizeQuery();
875 dbHandle.endTrans(false);
880 dbHandle.finalizeQuery();
882 dbHandle.endTrans(true);
885 for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
889 memset(href, 0x00, sizeof(href));
890 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
892 memset(sqlQuery, 0x00, sizeof(sqlQuery));
893 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'",
894 MSGFW_PUSH_MSG_TABLE_NAME, href);
896 dbHandle.beginTrans();
898 err = dbHandle.prepareQuery(sqlQuery);
900 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
902 msgid = dbHandle.getColumnToInt(1);
904 memset(sqlQuery, 0x00, sizeof(sqlQuery));
905 sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'",
906 MSGFW_PUSH_MSG_TABLE_NAME, msgid);
908 /** Delete Message from Push table */
909 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
910 dbHandle.finalizeQuery();
911 dbHandle.endTrans(false);
915 memset(sqlQuery, 0x00, sizeof(sqlQuery));
916 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
917 MSGFW_MESSAGE_TABLE_NAME, msgid);
919 /** Delete Message from msg table */
920 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
921 dbHandle.finalizeQuery();
922 dbHandle.endTrans(false);
926 /** Update all Address */
927 if (updateAllAddress() != MSG_SUCCESS) {
928 dbHandle.finalizeQuery();
929 dbHandle.endTrans(false);
933 /** Clear Address table */
934 if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
935 dbHandle.finalizeQuery();
936 dbHandle.endTrans(false);
941 dbHandle.finalizeQuery();
943 dbHandle.endTrans(true);
946 /** delete temporary file */
947 MsgDeleteFile(pMsgInfo->msgData);
953 MSG_ERROR_T SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
955 MSG_ERROR_T err = MSG_SUCCESS;
957 unsigned long oldExpireTime = 0;
960 char sqlQuery[MAX_QUERY_LEN+1];
962 /** is push message is expired?? */
963 if (pPushMsg->received > pPushMsg->expires) {
964 MSG_DEBUG("Push Message is expired.");
970 if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
971 memset(sqlQuery, 0x00, sizeof(sqlQuery));
972 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
973 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
975 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
976 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
979 err = dbHandle.getTable(sqlQuery, &rowCnt);
982 dbHandle.freeTable();
986 oldExpireTime = dbHandle.getColumnToInt(1);
988 dbHandle.freeTable();
990 if (pPushMsg->created < oldExpireTime) {
991 MSG_DEBUG("Push Message is expired.");
1000 MSG_ERROR_T SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
1002 MSG_ERROR_T err = MSG_SUCCESS;
1004 err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
1006 if (err != MSG_SUCCESS) {
1008 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
1009 bool bAutoErase = false;
1011 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
1013 MSG_DEBUG("bAutoErase: %d", bAutoErase);
1015 if (bAutoErase == true) {
1016 MSG_MESSAGE_ID_T msgId;
1018 /** Find the oldest message's msgId */
1019 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
1021 if (err != MSG_SUCCESS)
1024 /** Delete the corresponding message. */
1025 err = deleteSmsMessage(msgId);
1036 MSG_ERROR_T SmsPluginStorage::updateAllAddress()
1038 MSG_ERROR_T err = MSG_SUCCESS;
1040 int rowCnt = 0, index = 1;
1041 char sqlQuery[MAX_QUERY_LEN+1];
1043 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1045 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s",
1046 MSGFW_ADDRESS_TABLE_NAME);
1048 err = dbHandle.getTable(sqlQuery, &rowCnt);
1050 if (err == MSG_ERR_DB_NORECORD) {
1051 dbHandle.freeTable();
1053 } else if ( err != MSG_SUCCESS) {
1054 dbHandle.freeTable();
1059 for (int i = 0; i < rowCnt; i++) {
1061 err = MsgStoUpdateAddress(&dbHandle, index++);
1063 if (err != MSG_SUCCESS)
1067 dbHandle.freeTable();