2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "MsgCppTypes.h"
24 #include "MsgUtilFile.h"
25 #include "MsgContact.h"
26 #include "MsgSoundPlayer.h"
27 #include "MsgGconfWrapper.h"
28 #include "MsgSqliteWrapper.h"
29 #include "MsgPluginManager.h"
30 #include "MsgUtilStorage.h"
31 #include "MsgStorageHandler.h"
35 /*==================================================================================================
37 ==================================================================================================*/
40 /*==================================================================================================
41 FUNCTION IMPLEMENTATION
42 ==================================================================================================*/
43 msg_error_t MsgStoGetSmsSendOpt(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S* pSendOpt)
45 MsgDbHandler *dbHandle = getDbHandle();
46 char sqlQuery[MAX_QUERY_LEN+1];
48 memset(sqlQuery, 0x00, sizeof(sqlQuery));
50 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DELREP_REQ, KEEP_COPY, REPLY_PATH, ENCODE_TYPE FROM %s WHERE MSG_ID = %d;",
51 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId);
53 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
54 return MSG_ERR_DB_PREPARE;
56 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
57 pSendOpt->bSetting = true;
58 pSendOpt->bDeliverReq = (bool)dbHandle->columnInt(0);
59 pSendOpt->bKeepCopy = (bool)dbHandle->columnInt(1);
60 pSendOpt->option.smsSendOptInfo.bReplyPath = (bool)dbHandle->columnInt(2);
61 pMsg->encodeType = dbHandle->columnInt(3);
63 dbHandle->finalizeQuery();
64 return MSG_ERR_DB_STEP;
67 dbHandle->finalizeQuery();
73 msg_error_t MsgStoGetMmsSendOpt(msg_message_id_t msgId, MSG_SENDINGOPT_INFO_S* pSendOpt)
75 MsgDbHandler *dbHandle = getDbHandle();
76 char sqlQuery[MAX_QUERY_LEN+1];
78 memset(sqlQuery, 0x00, sizeof(sqlQuery));
80 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ASK_DELIVERY_REPORT, KEEP_COPY, ASK_READ_REPLY, EXPIRY_TIME, PRIORITY FROM %s WHERE MSG_ID = %d;",
81 MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
83 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
84 return MSG_ERR_DB_PREPARE;
86 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
87 pSendOpt->bSetting = true;
88 pSendOpt->bDeliverReq = (bool)dbHandle->columnInt(0);
89 pSendOpt->bKeepCopy = (bool)dbHandle->columnInt(1);
90 pSendOpt->option.mmsSendOptInfo.bReadReq = (bool)dbHandle->columnInt(2);
91 pSendOpt->option.mmsSendOptInfo.expiryTime.time = (unsigned int)dbHandle->columnInt(3);
92 pSendOpt->option.mmsSendOptInfo.priority = (msg_priority_type_t)dbHandle->columnInt(4);
94 dbHandle->finalizeQuery();
95 return MSG_ERR_DB_STEP;
98 dbHandle->finalizeQuery();
104 bool MsgStoCheckSyncMLMsgInThread(msg_thread_id_t threadId)
107 bool isSyncMLMsg = false;
108 MsgDbHandler *dbHandle = getDbHandle();
109 char sqlQuery[MAX_QUERY_LEN+1];
111 memset(sqlQuery, 0x00, sizeof(sqlQuery));
113 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d;",
114 MSGFW_MESSAGE_TABLE_NAME, MSG_SYNCML_CP, threadId);
116 if (dbHandle->getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
117 MSG_DEBUG("getTable is failed!!!");
120 if (rowCnt > 0) isSyncMLMsg = true;
122 MSG_DEBUG("rowCnt [%d]", rowCnt);
124 dbHandle->freeTable();
130 msg_error_t MsgStoResetNetworkStatus()
133 MsgDbHandler *dbHandle = getDbHandle();
134 char sqlQuery[MAX_QUERY_LEN+1];
136 memset(sqlQuery, 0x00, sizeof(sqlQuery));
138 snprintf(sqlQuery, sizeof(sqlQuery),
139 "UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d;",
140 MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_SEND_FAIL, MSG_NETWORK_SENDING);
142 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
143 //continue reset process for MSG_NETWORK_RETRIEVE_FAIL case
144 MSG_INFO("execQuery is failed");
147 memset(sqlQuery, 0x00, sizeof(sqlQuery));
149 snprintf(sqlQuery, sizeof(sqlQuery),
150 "UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d;",
151 MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_RETRIEVE_FAIL, MSG_NETWORK_RETRIEVING);
153 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
154 return MSG_ERR_DB_EXEC;
162 msg_error_t MsgStoResetCBMessage()
165 char sqlQuery[MAX_QUERY_LEN+1];
167 memset(sqlQuery, 0x00, sizeof(sqlQuery));
169 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s",
170 MSGFW_RECEIVED_CB_MSG_TABLE_NAME);
172 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
173 MSG_DEBUG("Delete Received CB Msg Fail!!");
174 return MSG_ERR_DB_EXEC;
183 msg_error_t MsgStoCleanAbnormalMmsData()
187 int rowCnt = 0, index = 2; // numbers of index
188 MsgDbHandler *dbHandle = getDbHandle();
189 msg_message_id_t msgId;
191 char sqlQuery[MAX_QUERY_LEN+1];
192 char filePath[MSG_FILEPATH_LEN_MAX];
194 memset(sqlQuery, 0x00, sizeof(sqlQuery));
196 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.FILE_PATH FROM %s A, %s B WHERE A.MSG_ID = B.MSG_ID AND (B.SUB_TYPE = %d OR B.SUB_TYPE = %d OR B.SUB_TYPE = %d);",
197 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS);
199 msg_error_t err = dbHandle->getTable(sqlQuery, &rowCnt);
201 if (err == MSG_ERR_DB_NORECORD) {
202 dbHandle->freeTable();
204 } else if (err != MSG_SUCCESS) {
205 MSG_DEBUG("Fail to getTable().");
206 dbHandle->freeTable();
211 for (int i = 0; i < rowCnt; i++)
213 memset(filePath, 0x00, sizeof(filePath));
215 msgId = dbHandle->getColumnToInt(index++);
217 dbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, filePath);
219 if(strlen(filePath) > 1) {
220 MSG_DEBUG("strlen(filePath) [%d]", strlen(filePath));
221 MSG_SEC_DEBUG("filePath [%s]", filePath);
223 if(MsgGetFileSize(filePath) < 0) {
224 // abnormal mms message
225 MSG_DEBUG("abnormal mms message [%d]", msgId);
227 // delete mms message
228 MsgStoDeleteMessage(msgId, false);
233 dbHandle->freeTable();
241 msg_error_t MsgStoCheckReadReportStatus(msg_message_id_t msgId)
245 bool bReadReportRequested;
246 bool bReadReportIsSent;
247 MsgDbHandler *dbHandle = getDbHandle();
248 bReadReportRequested = MsgStoCheckReadReportRequested(dbHandle, msgId);
249 if(bReadReportRequested == false)
250 return MSG_ERR_READREPORT_NOT_REQUESTED;
252 bReadReportIsSent = MsgStoCheckReadReportIsSent(dbHandle, msgId);
253 if(bReadReportIsSent == true)
254 return MSG_ERR_READREPORT_ALEADY_SENT;
262 msg_error_t MsgStoAutoDeleteConversation(msg_thread_id_t threadId, msg_id_list_s *msgIdList)
265 MsgDbHandler *dbHandle = getDbHandle();
266 msg_error_t err = MSG_SUCCESS;
268 bool bAutoErase = false;
269 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
273 MSG_DEBUG("threadId [%d]", threadId);
275 //msg_id_list_s msgIdList;
276 int currentSmsCnt = 0;
277 int currentMmsCnt = 0;
281 char sqlQuery[MAX_QUERY_LEN+1];
284 unsigned int index = 1;
286 //memset(msgIdList, 0x00, sizeof(msg_id_list_s));
288 // Get current count of messages
289 memset(sqlQuery, 0x00, sizeof(sqlQuery));
291 #ifdef MSG_NOTI_INTEGRATION
292 snprintf(sqlQuery, sizeof(sqlQuery),
293 "SELECT COUNT(MSG_ID) FROM %s "
294 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d "
296 "SELECT COUNT(MSG_ID) FROM %s "
297 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d);",
298 MSGFW_MESSAGE_TABLE_NAME,
299 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE, MSG_SMS_TYPE,
300 MSGFW_MESSAGE_TABLE_NAME,
301 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS);
303 snprintf(sqlQuery, sizeof(sqlQuery),
304 "SELECT COUNT(MSG_ID) FROM %s "
305 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d "
307 "SELECT COUNT(MSG_ID) FROM %s "
308 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d);",
309 MSGFW_MESSAGE_TABLE_NAME,
310 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE, MSG_SMS_TYPE,
311 MSGFW_MESSAGE_TABLE_NAME,
312 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS);
315 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
316 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
317 return MSG_ERR_DB_PREPARE;
320 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
321 currentSmsCnt = dbHandle->columnInt(0);
323 dbHandle->finalizeQuery();
324 return MSG_ERR_DB_STEP;
327 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
328 currentMmsCnt = dbHandle->columnInt(0);
330 dbHandle->finalizeQuery();
331 return MSG_ERR_DB_STEP;
334 dbHandle->finalizeQuery();
336 MSG_DEBUG("currentSmsCnt [%d], currentMmsCnt [%d]", currentSmsCnt, currentMmsCnt);
338 if (currentSmsCnt > 0 || currentMmsCnt > 0) {
339 limitSmsCnt = MsgSettingGetInt(MSG_SMS_LIMIT);
340 limitMmsCnt = MsgSettingGetInt(MSG_MMS_LIMIT);
342 if (limitSmsCnt < 0 || limitMmsCnt < 0) {
343 MSG_DEBUG("limitSmsCnt [%d], limitMmsCnt [%d]", limitSmsCnt, limitMmsCnt);
346 } else { // !(currentSmsCnt > 0 || currentMmsCnt > 0)
350 memset(sqlQuery, 0x00, sizeof(sqlQuery));
352 #ifdef MSG_NOTI_INTEGRATION
353 snprintf(sqlQuery, sizeof(sqlQuery),
354 "SELECT DISTINCT(MSG_ID) FROM %s "
356 "(SELECT MSG_ID FROM %s "
357 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
358 "AND MAIN_TYPE = %d ORDER BY MSG_ID ASC LIMIT %d) "
360 "(SELECT MSG_ID FROM %s "
361 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
362 "AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d) ORDER BY MSG_ID ASC LIMIT %d);",
363 MSGFW_MESSAGE_TABLE_NAME,
364 MSGFW_MESSAGE_TABLE_NAME,
365 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
366 MSG_SMS_TYPE, ((currentSmsCnt-limitSmsCnt)>0)?(currentSmsCnt-limitSmsCnt):0,
367 MSGFW_MESSAGE_TABLE_NAME,
368 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
369 MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS, ((currentMmsCnt-limitMmsCnt)>0)?(currentMmsCnt-limitMmsCnt):0);
371 snprintf(sqlQuery, sizeof(sqlQuery),
372 "SELECT DISTINCT(MSG_ID) FROM %s "
374 "(SELECT MSG_ID FROM %s "
375 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
376 "AND MAIN_TYPE = %d ORDER BY MSG_ID ASC LIMIT %d) "
378 "(SELECT MSG_ID FROM %s "
379 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
380 "AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d) ORDER BY MSG_ID ASC LIMIT %d);",
381 MSGFW_MESSAGE_TABLE_NAME,
382 MSGFW_MESSAGE_TABLE_NAME,
383 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE,
384 MSG_SMS_TYPE, ((currentSmsCnt-limitSmsCnt)>0)?(currentSmsCnt-limitSmsCnt):0,
385 MSGFW_MESSAGE_TABLE_NAME,
386 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE,
387 MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS, ((currentMmsCnt-limitMmsCnt)>0)?(currentMmsCnt-limitMmsCnt):0);
389 err = dbHandle->getTable(sqlQuery, &rowCnt);
391 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
392 MSG_DEBUG("Fail to getTable().");
393 dbHandle->freeTable();
398 MSG_DEBUG("rowCnt <= 0");
399 dbHandle->freeTable();
403 msgIdList->nCount = rowCnt;
405 MSG_DEBUG("msgIdList.nCount [%d]", msgIdList->nCount);
407 msgIdList->msgIdList = (msg_message_id_t *)new char[sizeof(msg_message_id_t) * rowCnt];
409 for (int i = 0; i < rowCnt; i++) {
410 msgIdList->msgIdList[i] = dbHandle->getColumnToInt(index++);
413 dbHandle->freeTable();
416 err = MsgStoDeleteMessageByList(msgIdList);
418 //delete [] (char*)msgIdList.msgIdList;
421 MSG_DEBUG("bAutoErase [%d]", bAutoErase);
430 msg_error_t MsgStoGetReplaceMsgId(MSG_MESSAGE_INFO_S *pMsgInfo)
432 msg_error_t err = MSG_SUCCESS;
433 MsgDbHandler *dbHandle = getDbHandle();
434 char sqlQuery[MAX_QUERY_LEN+1];
436 msg_thread_id_t convId = 0;
438 /** Check if new address or not */
439 if (MsgExistAddress(dbHandle, pMsgInfo, &convId) == true) {
440 MSG_DEBUG("Address Info. exists [%d]", convId);
442 /** Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
443 memset(sqlQuery, 0x00, sizeof(sqlQuery));
444 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d AND STORAGE_ID = %d;",
445 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId, pMsgInfo->storageId);
447 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
448 return MSG_ERR_DB_PREPARE;
450 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
451 pMsgInfo->msgId = dbHandle->columnInt(0);
453 dbHandle->finalizeQuery();
454 return MSG_ERR_DB_STEP;
457 dbHandle->finalizeQuery();
464 msg_error_t MsgStoAddWAPMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
466 msg_error_t err = MSG_SUCCESS;
467 MsgDbHandler *dbHandle = getDbHandle();
468 MSG_PUSH_MESSAGE_S pushMsg = {};
470 char sqlQuery[MAX_QUERY_LEN+1];
471 memset(sqlQuery, 0x00, sizeof(sqlQuery));
475 char* pFileData = NULL;
476 AutoPtr<char> buf(&pFileData);
478 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
479 return MSG_ERR_STORAGE_ERROR;
481 MSG_DEBUG("fileSize : %d", fileSize);
483 memcpy(&pushMsg, pFileData, fileSize);
485 /** Delete temporary file */
486 MsgDeleteFile(pMsgInfo->msgData);
488 /** check pPushMsg data */
490 MSG_DEBUG("check pushMsg data");
491 MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
492 MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
493 MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
494 MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
495 MSG_SEC_DEBUG("pushMsg.id : [%s]", pushMsg.id);
496 MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
497 MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
499 bool bProceed = true;
501 /** check validation of contents */
502 if (MsgStoCheckPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
503 MSG_DEBUG("Fail to check Push Message validation.");
506 /** if validation check value is false */
507 /** return and drop message. */
508 if (bProceed == false)
509 return MSG_ERR_INVALID_MESSAGE;
512 /** Update Msg Text - remove */
513 strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
515 if (pushMsg.contents[0] != '\0') {
516 strncat(pMsgInfo->msgText, " ", MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
517 strncat(pMsgInfo->msgText, pushMsg.contents, MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
520 pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
522 pMsgInfo->bTextSms = true;
523 pMsgInfo->folderId = MSG_INBOX_ID;
524 pMsgInfo->storageId = MSG_STORAGE_PHONE;
526 msg_thread_id_t convId = 0;
528 dbHandle->beginTrans();
530 if (pMsgInfo->nAddressCnt > 0) {
532 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
534 if (err != MSG_SUCCESS) {
535 dbHandle->endTrans(false);
539 pMsgInfo->threadId = convId;
542 /** get last row count for Message id */
543 unsigned int rowId = 0;
545 /** Add Message Table */
546 rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
549 dbHandle->endTrans(false);
550 return MSG_ERR_DB_ROW;
553 pMsgInfo->msgId = (msg_message_id_t)rowId;
555 /** add msg_push_table */
556 snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
557 MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
559 if ((err = dbHandle->prepareQuery(sqlQuery)) != MSG_SUCCESS) {
560 dbHandle->endTrans(false);
564 dbHandle->bindText(pushMsg.id, 1);
566 dbHandle->bindText(pushMsg.href, 2);
568 dbHandle->bindText(pushMsg.contents, 3);
570 if ((err = dbHandle->stepQuery()) != MSG_ERR_DB_DONE) {
571 dbHandle->finalizeQuery();
572 dbHandle->endTrans(false);
576 dbHandle->finalizeQuery();
578 /** Update conversation table. */
579 if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
580 dbHandle->endTrans(false);
581 return MSG_ERR_STORAGE_ERROR;
584 dbHandle->endTrans(true);
590 msg_error_t MsgStoAddCOWAPMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
592 msg_error_t err = MSG_SUCCESS;
593 MsgDbHandler *dbHandle = getDbHandle();
594 char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
595 char sqlQuery[MAX_QUERY_LEN+1];
599 char* pFileData = NULL;
600 AutoPtr<char> buf(&pFileData);
602 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
603 return MSG_ERR_STORAGE_ERROR;
605 MSG_PUSH_CACHEOP_S *pPushMsg;
607 pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
609 for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
613 memset(href, 0x00, sizeof(href));
614 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
616 memset(sqlQuery, 0x00, sizeof(sqlQuery));
617 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
619 dbHandle->beginTrans();
621 err = dbHandle->prepareQuery(sqlQuery);
623 if ((dbHandle->stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
625 msgid = dbHandle->getColumnToInt(1);
627 memset(sqlQuery, 0x00, sizeof(sqlQuery));
628 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
630 /** Delete Message from Push table */
631 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
632 dbHandle->finalizeQuery();
633 dbHandle->endTrans(false);
637 memset(sqlQuery, 0x00, sizeof(sqlQuery));
638 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
640 /** Delete Message from msg table */
641 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
642 dbHandle->finalizeQuery();
643 dbHandle->endTrans(false);
647 /** Update all Address */
648 if (MsgStoUpdateAllAddress() != MSG_SUCCESS) {
649 dbHandle->finalizeQuery();
650 dbHandle->endTrans(false);
654 /** Clear Conversation table */
655 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
656 dbHandle->finalizeQuery();
657 dbHandle->endTrans(false);
662 dbHandle->finalizeQuery();
664 dbHandle->endTrans(true);
667 for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
671 memset(href, 0x00, sizeof(href));
672 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
674 memset(sqlQuery, 0x00, sizeof(sqlQuery));
675 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
677 dbHandle->beginTrans();
679 err = dbHandle->prepareQuery(sqlQuery);
681 if ((dbHandle->stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
683 msgid = dbHandle->getColumnToInt(1);
685 memset(sqlQuery, 0x00, sizeof(sqlQuery));
686 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
688 /** Delete Message from Push table */
689 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
690 dbHandle->finalizeQuery();
691 dbHandle->endTrans(false);
695 memset(sqlQuery, 0x00, sizeof(sqlQuery));
696 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
698 /** Delete Message from msg table */
699 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
700 dbHandle->finalizeQuery();
701 dbHandle->endTrans(false);
705 /** Update all Address */
706 if (MsgStoUpdateAllAddress() != MSG_SUCCESS) {
707 dbHandle->finalizeQuery();
708 dbHandle->endTrans(false);
712 /** Clear Address table */
713 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
714 dbHandle->finalizeQuery();
715 dbHandle->endTrans(false);
720 dbHandle->finalizeQuery();
722 dbHandle->endTrans(true);
725 /** delete temporary file */
726 MsgDeleteFile(pMsgInfo->msgData);
732 msg_error_t MsgStoAddCBMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
734 msg_error_t err = MSG_SUCCESS;
735 MsgDbHandler *dbHandle = getDbHandle();
736 unsigned int rowId = 0;
737 msg_thread_id_t convId = 0;
739 char sqlQuery[MAX_QUERY_LEN+1];
741 dbHandle->beginTrans();
743 if (pMsgInfo->nAddressCnt > 0) {
744 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
746 if (err != MSG_SUCCESS) {
747 dbHandle->endTrans(false);
751 pMsgInfo->threadId = convId;
754 /** Add Message Table */
755 rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
758 dbHandle->endTrans(false);
759 return MSG_ERR_DB_ROW;
763 unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
765 /** Add CB Msg in MSG_CBMSG_TABLE */
766 memset(sqlQuery, 0x00, sizeof(sqlQuery));
768 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
769 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
771 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
772 dbHandle->endTrans(false);
773 return MSG_ERR_DB_EXEC;
776 /** Update conversation table. */
777 if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
778 dbHandle->endTrans(false);
779 return MSG_ERR_STORAGE_ERROR;
782 dbHandle->endTrans(true);
785 pMsgInfo->msgId = (msg_message_id_t)rowId;
791 msg_error_t MsgStoCheckPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
793 msg_error_t err = MSG_SUCCESS;
794 MsgDbHandler *dbHandle = getDbHandle();
795 unsigned long oldExpireTime = 0;
798 char sqlQuery[MAX_QUERY_LEN+1];
800 /** is push message is expired?? */
801 if (pPushMsg->received > pPushMsg->expires) {
802 MSG_DEBUG("Push Message is expired.");
807 if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
808 memset(sqlQuery, 0x00, sizeof(sqlQuery));
809 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
810 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
812 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
813 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
816 err = dbHandle->getTable(sqlQuery, &rowCnt);
819 dbHandle->freeTable();
823 oldExpireTime = dbHandle->getColumnToInt(1);
825 dbHandle->freeTable();
827 if (pPushMsg->created < oldExpireTime) {
828 MSG_DEBUG("Push Message is expired.");
837 msg_error_t MsgStoUpdateAllAddress()
839 msg_error_t err = MSG_SUCCESS;
840 MsgDbHandler *dbHandle = getDbHandle();
841 int rowCnt = 0, index = 1;
842 char sqlQuery[MAX_QUERY_LEN+1];
844 memset(sqlQuery, 0x00, sizeof(sqlQuery));
846 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
848 err = dbHandle->getTable(sqlQuery, &rowCnt);
850 if (err == MSG_ERR_DB_NORECORD) {
851 dbHandle->freeTable();
853 } else if ( err != MSG_SUCCESS) {
854 dbHandle->freeTable();
858 for (int i = 0; i < rowCnt; i++) {
860 err = MsgStoUpdateConversation(dbHandle, index++);
862 if (err != MSG_SUCCESS)
866 dbHandle->freeTable();