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.
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgUtilFile.h"
24 #include "MsgUtilStorage.h"
25 #include "MsgUtilFunction.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgNotificationWrapper.h"
28 #include "SmsPluginMain.h"
29 #include "SmsPluginSimMsg.h"
30 #include "SmsPluginEventHandler.h"
31 #include "SmsPluginStorage.h"
34 /*==================================================================================================
35 IMPLEMENTATION OF SmsPluginStorage - Member Functions
36 ==================================================================================================*/
37 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
40 SmsPluginStorage::SmsPluginStorage()
42 memset(&msgInfo, 0x00, sizeof(msgInfo));
43 memset(&addrInfo, 0x00, sizeof(addrInfo));
47 SmsPluginStorage::~SmsPluginStorage()
52 SmsPluginStorage* SmsPluginStorage::instance()
55 MSG_DEBUG("pInstance is NULL. Now creating instance.");
56 pInstance = new SmsPluginStorage();
63 msg_error_t SmsPluginStorage::insertMsgRef(MSG_MESSAGE_INFO_S *pMsg, unsigned char msgRef, int index)
67 time_t curTime = time(NULL);
69 MsgDbHandler *dbHandle = getDbHandle();
71 char sqlQuery[MAX_QUERY_LEN+1];
72 char *normalNum = NULL;
74 memset(sqlQuery, 0x00, sizeof(sqlQuery));
76 normalNum = msg_normalize_number(pMsg->addressList[index].addressVal);
78 MSG_SEC_DEBUG("Insert MsgID=[%d], Address=[%s], MsgRef=[%d], Time=[%d]", \
79 pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
81 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %s, %d, 0, -1, %d);",
82 MSGFW_SMS_REPORT_TABLE_NAME, pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
84 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
85 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
86 return MSG_ERR_DB_EXEC;
95 msg_error_t SmsPluginStorage::updateMsgDeliverStatus(MSG_MESSAGE_INFO_S *pMsgInfo, unsigned char msgRef)
99 MsgDbHandler *dbHandle = getDbHandle();
101 char sqlQuery[MAX_QUERY_LEN+1];
103 msg_message_id_t msgId = 0;
105 char *normalNum = NULL;
107 normalNum = msg_normalize_number(pMsgInfo->addressList[0].addressVal);
109 memset(sqlQuery, 0x00, sizeof(sqlQuery));
110 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_VAL = %s AND MSG_REF > 0 ORDER BY TIME ASC;",
111 MSGFW_SMS_REPORT_TABLE_NAME, normalNum);
112 MSG_DEBUG("[SQL Query] %s", sqlQuery);
114 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
115 return MSG_ERR_DB_PREPARE;
117 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
118 msgId = dbHandle->columnInt(0);
120 dbHandle->finalizeQuery();
122 pMsgInfo->msgId = msgId;
124 /** Update Status - MSG_MESSAGE_TABLE */
125 memset(sqlQuery, 0x00, sizeof(sqlQuery));
126 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE MSG_ID = %d AND MSG_REF > 0;",
127 MSGFW_SMS_REPORT_TABLE_NAME, msgId);
129 if (dbHandle->getTable(sqlQuery, &rowCnt, NULL) != MSG_SUCCESS) {
130 dbHandle->freeTable();
131 return MSG_ERR_DB_GETTABLE;
134 dbHandle->freeTable();
136 MSG_DEBUG("Selected row count = [%d]", rowCnt);
138 if (rowCnt == 1 && pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
139 memset(sqlQuery, 0x00, sizeof(sqlQuery));
140 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
141 MSGFW_MESSAGE_TABLE_NAME, (int)pMsgInfo->networkStatus, msgId);
142 MSG_DEBUG("[SQL Query] %s", sqlQuery);
144 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
145 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
146 return MSG_ERR_DB_EXEC;
150 /** Update Status - MSG_REPORT_TABLE */
151 if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
152 memset(sqlQuery, 0x00, sizeof(sqlQuery));
153 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
154 MSGFW_SMS_REPORT_TABLE_NAME, 1, (int)pMsgInfo->displayTime, msgId, normalNum);
155 MSG_DEBUG("[SQL Query] %s", sqlQuery);
157 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
158 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
159 return MSG_ERR_DB_EXEC;
161 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_EXPIRED) {
162 memset(sqlQuery, 0x00, sizeof(sqlQuery));
163 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
164 MSGFW_SMS_REPORT_TABLE_NAME, 0, (int)pMsgInfo->displayTime, msgId, normalNum);
165 MSG_DEBUG("[SQL Query] %s", sqlQuery);
167 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
168 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
169 return MSG_ERR_DB_EXEC;
171 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_PENDING) {
172 memset(sqlQuery, 0x00, sizeof(sqlQuery));
173 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
174 MSGFW_SMS_REPORT_TABLE_NAME, 3, (int)pMsgInfo->displayTime, msgId, normalNum);
175 MSG_DEBUG("[SQL Query] %s", sqlQuery);
177 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
178 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
179 return MSG_ERR_DB_EXEC;
181 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL) {
182 memset(sqlQuery, 0x00, sizeof(sqlQuery));
183 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
184 MSGFW_SMS_REPORT_TABLE_NAME, 8, (int)pMsgInfo->displayTime, msgId, normalNum);
185 MSG_DEBUG("[SQL Query] %s", sqlQuery);
187 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
188 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
189 return MSG_ERR_DB_EXEC;
199 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
203 if (!pMsgInfo || (pMsgInfo && pMsgInfo->msgId <= 0)) {
204 MSG_DEBUG("Invalid message id");
205 return MSG_ERR_INVALID_MESSAGE_ID;
208 /*** Comment below line to not save the time value after sent status (it could be used later.)
209 time_t curTime = time(NULL);
211 MsgDbHandler *dbHandle = getDbHandle();
213 char sqlQuery[MAX_QUERY_LEN+1];
215 memset(sqlQuery, 0x00, sizeof(sqlQuery));
217 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
219 /** Move Msg to SENTBOX */
220 if (status == MSG_NETWORK_SEND_SUCCESS) {
221 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
222 MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
224 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, READ_STATUS = 0 WHERE MSG_ID = %d;",
225 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
228 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
229 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
230 return MSG_ERR_DB_EXEC;
233 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
234 if (status == MSG_NETWORK_SEND_SUCCESS) {
235 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
236 MsgAddPhoneLog(pMsgInfo);
238 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
244 #ifdef SMS_REPORT_OPERATION
245 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
249 MsgDbHandler *dbHandle = getDbHandle();
251 char sqlQuery[MAX_QUERY_LEN+1];
253 memset(sqlQuery, 0x00, sizeof(sqlQuery));
255 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
256 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
258 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
259 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
260 return MSG_ERR_DB_EXEC;
263 /** Set Message Reference for updating report table */
266 MSG_DEBUG("MsgRef : %d", MsgRef);
274 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
278 MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
280 MsgDbHandler *dbHandle = getDbHandle();
282 char sqlQuery[MAX_QUERY_LEN+1];
284 /** Get Msg Id for Quickpanel Noti */
285 msg_message_id_t msgId = 0;
287 memset(sqlQuery, 0x00, sizeof(sqlQuery));
288 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
289 MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
291 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
292 return MSG_ERR_DB_PREPARE;
294 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
295 msgId = dbHandle->columnInt(0);
297 dbHandle->finalizeQuery();
300 memset(sqlQuery, 0x00, sizeof(sqlQuery));
301 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
302 MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
304 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
305 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
306 return MSG_ERR_DB_EXEC;
309 /** Insert Quickpanel Noti */
310 msg_error_t ret = MSG_SUCCESS;
312 ret = MsgInsertSmsNotiToQuickpanel(dbHandle, msgId, Status);
314 if (ret != MSG_SUCCESS) {
315 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
325 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo, int *simIdList)
327 msg_error_t err = MSG_SUCCESS;
329 unsigned int simId = 0;
330 MsgDbHandler *dbHandle = getDbHandle();
332 char sqlQuery[MAX_QUERY_LEN+1];
335 MSG_DEBUG("simIdList exist.");
336 for (int i = 0; i < MAX_SIM_SMS_NUM; ++i) {
338 simId = simIdList[i] - 1;
339 memset(sqlQuery, 0x00, sizeof(sqlQuery));
340 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
341 MSGFW_SIM_MSG_TABLE_NAME, simId, pSimMsgInfo->msgId);
343 MSG_DEBUG("QUERY : %s", sqlQuery);
345 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
346 return MSG_ERR_DB_EXEC;
358 msg_error_t SmsPluginStorage::insertSimMessage(int simId, int msgId)
362 MsgDbHandler *dbHandle = getDbHandle();
364 char sqlQuery[MAX_QUERY_LEN+1];
366 dbHandle->beginTrans();
368 /** Insert Message into msg_sim table */
369 memset(sqlQuery, 0x00, sizeof(sqlQuery));
370 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);", MSGFW_SIM_MSG_TABLE_NAME, simId, msgId);
371 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
372 dbHandle->endTrans(false);
373 return MSG_ERR_DB_EXEC;
376 dbHandle->endTrans(true);
382 msg_error_t SmsPluginStorage::deleteSimMessage(int sim_idx, int simId)
386 MsgDbHandler *dbHandle = getDbHandle();
388 char sqlQuery[MAX_QUERY_LEN+1];
390 dbHandle->beginTrans();
392 /** Delete Message from msg_sim table */
393 memset(sqlQuery, 0x00, sizeof(sqlQuery));
394 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d AND SIM_SLOT_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simId, sim_idx);
395 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
396 dbHandle->endTrans(false);
397 return MSG_ERR_DB_EXEC;
400 dbHandle->endTrans(true);
406 msg_error_t SmsPluginStorage::checkMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
410 msg_error_t err = MSG_SUCCESS;
412 /** Check whether storage is full or not */
413 err = checkStorageStatus(pMsgInfo);
415 if (err != MSG_SUCCESS) {
416 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
417 pMsgInfo->folderId = 0;
419 } else if (pMsgInfo->msgType.classType == MSG_CLASS_2 &&
420 (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS)) {
421 err = addClass2Message(pMsgInfo);
422 } else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
428 /** Amend message information for type **/
429 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
430 MSG_DEBUG("Normal SMS");
432 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
433 err = addClass2Message(pMsgInfo);
434 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
435 /** Class 0 Msg should be saved in hidden folder */
436 pMsgInfo->folderId = 0;
439 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
440 MSG_DEBUG("Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
442 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
443 err = addClass2Message(pMsgInfo);
444 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
445 /** Class 0 Msg should be saved in hidden folder */
446 pMsgInfo->folderId = 0;
447 pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
450 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
451 if (pMsgInfo->bStore == true) {
452 MSG_DEBUG("MWI Message");
454 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
455 err = addClass2Message(pMsgInfo);
459 MSG_DEBUG("No matching type [%d]", pMsgInfo->msgType.subType);
462 if (err == MSG_SUCCESS) {
463 MSG_DEBUG("Success to check message !!");
465 MSG_DEBUG("fail to check message !! : [%d]", err);
472 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
474 msg_error_t err = MSG_SUCCESS;
476 unsigned int rowId = 0;
477 msg_thread_id_t convId = 0;
479 MsgDbHandler *dbHandle = getDbHandle();
481 dbHandle->beginTrans();
483 if (pMsgInfo->nAddressCnt > 0) {
484 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
486 if (err != MSG_SUCCESS) {
487 dbHandle->endTrans(false);
491 pMsgInfo->threadId = convId;
494 /** Add Message Table */
495 rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
498 dbHandle->endTrans(false);
499 return MSG_ERR_DB_ROW;
502 /** Update conversation table */
503 err = MsgStoUpdateConversation(dbHandle, convId);
505 if (err != MSG_SUCCESS) {
506 dbHandle->endTrans(false);
510 err = dbHandle->endTrans(true);
511 if (err != MSG_SUCCESS) {
515 pMsgInfo->msgId = (msg_message_id_t)rowId;
523 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
527 MsgDbHandler *dbHandle = getDbHandle();
529 char sqlQuery[MAX_QUERY_LEN+1];
531 /** Get SUB_TYPE, STORAGE_ID */
532 memset(sqlQuery, 0x00, sizeof(sqlQuery));
533 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, CONV_ID, SIM_INDEX \
534 FROM %s WHERE MSG_ID = %d;",
535 MSGFW_MESSAGE_TABLE_NAME, msgId);
537 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
538 return MSG_ERR_DB_PREPARE;
540 MSG_MESSAGE_TYPE_S msgType;
541 msg_folder_id_t folderId;
543 msg_thread_id_t convId;
544 msg_sim_slot_id_t simIndex;
546 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
547 msgType.mainType = dbHandle->columnInt(0);
548 msgType.subType = dbHandle->columnInt(1);
549 folderId = dbHandle->columnInt(2);
550 convId = dbHandle->columnInt(3);
551 simIndex = dbHandle->columnInt(4);
553 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
555 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
556 dbHandle->finalizeQuery();
557 return MSG_ERR_DB_STEP;
560 dbHandle->finalizeQuery();
562 dbHandle->beginTrans();
564 memset(sqlQuery, 0x00, sizeof(sqlQuery));
565 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
567 /** Delete SMS Send Option */
568 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
569 dbHandle->endTrans(false);
570 return MSG_ERR_DB_EXEC;
573 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
574 memset(sqlQuery, 0x00, sizeof(sqlQuery));
575 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
577 /** Delete Push Message from push table */
578 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
579 dbHandle->endTrans(false);
580 return MSG_ERR_DB_EXEC;
582 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
583 memset(sqlQuery, 0x00, sizeof(sqlQuery));
584 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
586 /** Delete Push Message from push table */
587 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
588 dbHandle->endTrans(false);
589 return MSG_ERR_DB_EXEC;
591 } else if (msgType.subType == MSG_SYNCML_CP) {
592 memset(sqlQuery, 0x00, sizeof(sqlQuery));
593 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
595 /** Delete SyncML Message from syncML table */
596 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
597 dbHandle->endTrans(false);
598 return MSG_ERR_DB_EXEC;
602 /** Delete Message from msg table */
603 memset(sqlQuery, 0x00, sizeof(sqlQuery));
604 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
605 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
606 dbHandle->endTrans(false);
607 return MSG_ERR_DB_EXEC;
610 /** Delete Message from msg_report table */
611 memset(sqlQuery, 0x00, sizeof(sqlQuery));
612 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_REPORT_TABLE_NAME, msgId);
613 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
614 dbHandle->endTrans(false);
615 return MSG_ERR_DB_EXEC;
618 /** Delete Message from msg_sim table */
619 memset(sqlQuery, 0x00, sizeof(sqlQuery));
620 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, msgId);
621 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
622 dbHandle->endTrans(false);
623 return MSG_ERR_DB_EXEC;
626 /** Clear Conversation table */
627 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
628 dbHandle->endTrans(false);
629 return MSG_ERR_DB_EXEC;
632 /** Update conversation table.*/
633 if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
634 dbHandle->endTrans(false);
635 return MSG_ERR_STORAGE_ERROR;
638 dbHandle->endTrans(true);
640 memset(sqlQuery, 0x00, sizeof(sqlQuery));
641 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONV_ID FROM %s WHERE CONV_ID = %d;", MSGFW_CONVERSATION_TABLE_NAME, convId);
642 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
643 return MSG_ERR_DB_PREPARE;
645 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
646 SmsPluginEventHandler::instance()->callbackThreadChange(MSG_STORAGE_CHANGE_UPDATE, convId);
648 SmsPluginEventHandler::instance()->callbackThreadChange(MSG_STORAGE_CHANGE_DELETE, convId);
650 dbHandle->finalizeQuery();
652 signed char folder_id = (signed char)folderId;
653 if (folder_id == MSG_INBOX_ID) {
654 msgType.classType = MSG_CLASS_NONE;
656 /** Set memory status in SIM */
657 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
658 MSG_DEBUG("Set Memory Status");
659 SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
663 MsgRefreshAllNotification(true, false, MSG_ACTIVE_NOTI_TYPE_NONE);
669 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
673 msg_error_t err = MSG_SUCCESS;
676 char keyName[MAX_VCONFKEY_NAME_LEN];
677 memset(keyName, 0x00, sizeof(keyName));
678 snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
680 if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS) {
681 MSG_ERR("MsgSettingGetBool [%s] failed", keyName);
684 if (bSimSst == false) {
685 return MSG_ERR_SIM_STORAGE_FULL;
688 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
689 memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
690 memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
691 memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
692 msgInfo.addressList = &addrInfo;
694 if (pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
695 MSG_DEBUG("pthread_create() error");
700 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
703 if (err == MSG_SUCCESS) {
704 MSG_DEBUG("Success to saveClass2Message.");
706 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
715 void* SmsPluginStorage::class2_thread(void *data)
719 msg_error_t err = MSG_SUCCESS;
720 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
722 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
723 if (err == MSG_SUCCESS) {
724 MSG_DEBUG("Success to saveClass2Message.");
726 MSG_ERR("Fail to saveClass2Message : [%d]", err);
734 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
738 MsgDbHandler *dbHandle = getDbHandle();
740 char sqlQuery[MAX_QUERY_LEN+1];
741 msg_thread_id_t convId = 0;
742 msg_message_id_t msgId = 0;
744 dbHandle->beginTrans();
746 if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
747 MSG_DEBUG("Address Info. exists [%d]", convId);
749 /** Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
750 memset(sqlQuery, 0x00, sizeof(sqlQuery));
751 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
752 WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
753 ORDER BY DISPLAY_TIME ASC;",
754 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
756 MSG_DEBUG("Query=[%s]", sqlQuery);
757 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
758 dbHandle->endTrans(false);
759 return MSG_ERR_DB_PREPARE;
762 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
763 *pMsgId = dbHandle->columnInt(0);
765 dbHandle->finalizeQuery();
766 dbHandle->endTrans(false);
767 return MSG_ERR_DB_STEP;
770 dbHandle->finalizeQuery();
773 dbHandle->endTrans(false);
774 return MSG_ERR_DB_NORECORD;
777 memset(sqlQuery, 0x00, sizeof(sqlQuery));
779 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
781 MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
783 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
784 dbHandle->endTrans(false);
785 return MSG_ERR_DB_PREPARE;
788 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
789 *pSimId = dbHandle->columnInt(0);
791 dbHandle->finalizeQuery();
792 dbHandle->endTrans(false);
793 return MSG_ERR_DB_STEP;
796 MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
798 dbHandle->finalizeQuery();
800 dbHandle->endTrans(true);
807 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
811 msg_error_t err = MSG_SUCCESS;
813 if (pSendOptInfo->bSetting == false) {
814 if (MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq) != MSG_SUCCESS)
815 MSG_INFO("MsgSettingGetBool() is failed");
817 if (MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath) != MSG_SUCCESS)
818 MSG_INFO("MsgSettingGetBool() is failed");
820 if (MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy) != MSG_SUCCESS)
821 MSG_INFO("MsgSettingGetBool() is failed");
824 MsgDbHandler *dbHandle = getDbHandle();
826 char sqlQuery[MAX_QUERY_LEN+1];
828 dbHandle->beginTrans();
830 memset(sqlQuery, 0x00, sizeof(sqlQuery));
831 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
832 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
833 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
835 MSG_DEBUG("Query = [%s]", sqlQuery);
837 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
838 MSG_ERR("execQuery() is failed");
839 dbHandle->endTrans(false);
840 return MSG_ERR_DB_EXEC;
843 dbHandle->endTrans(true);
851 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
853 msg_error_t err = MSG_SUCCESS;
855 MsgDbHandler *dbHandle = getDbHandle();
857 err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
859 if (err != MSG_SUCCESS) {
860 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
861 bool bAutoErase = false;
863 if (MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase) != MSG_SUCCESS)
864 MSG_INFO("MsgSettingGetBool() is failed");
866 MSG_DEBUG("bAutoErase: %d", bAutoErase);
868 if (bAutoErase == true) {
869 msg_message_id_t msgId;
871 /** Find the oldest message's msgId */
872 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
874 if (err != MSG_SUCCESS)
877 /** Delete the corresponding message. */
878 err = deleteSmsMessage(msgId);
891 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
893 msg_error_t err = MSG_SUCCESS;
897 MsgDbHandler *dbHandle = getDbHandle();
899 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
901 memset(sqlQuery, 0x00, sizeof(sqlQuery));
903 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
904 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
905 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
907 err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
908 MSG_DEBUG("rowCnt: %d", rowCnt);
910 dbHandle->freeTable();
914 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
916 msg_error_t err = MSG_SUCCESS;
918 unsigned int rowId = 0;
920 MsgDbHandler *dbHandle = getDbHandle();
922 char sqlQuery[MAX_QUERY_LEN+1];
924 err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
926 if (err != MSG_SUCCESS)
930 memset(sqlQuery, 0x00, sizeof(sqlQuery));
931 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
932 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
933 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
935 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
936 return MSG_ERR_DB_EXEC;
943 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
945 msg_error_t err = MSG_SUCCESS;
947 int rowCnt = 0, index = 0;
949 MsgDbHandler *dbHandle = getDbHandle();
951 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
953 memset(sqlQuery, 0x00, sizeof(sqlQuery));
955 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
957 err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
958 MSG_DEBUG("rowCnt: %d", rowCnt);
960 if (err != MSG_SUCCESS) {
961 dbHandle->freeTable();
963 if (err == MSG_ERR_DB_NORECORD)
969 char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0, };
970 char appId[MAX_WAPPUSH_ID_LEN + 1] = {0, };
971 int appcode = 0, default_appcode = 0;
973 char *_content_type = NULL, *_app_id = NULL;
977 for (int i = 0; i < rowCnt; i++) {
978 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
979 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
981 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
982 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
983 appcode = dbHandle->getColumnToInt(index++);
985 /* MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id); */
986 _content_type = strcasestr(pPushHeader, contentType);
988 _app_id = strcasestr(pPushHeader, appId);
990 default_appcode = appcode;
993 PUSH_APPLICATION_INFO_S pInfo = {0, };
994 pInfo.appcode = appcode;
995 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
996 snprintf(application_id, app_id_len, "%s", appId);
997 snprintf(content_type, content_type_len, "%s", contentType);
998 pushAppInfoList.push_back(pInfo);
1005 if (!found && default_appcode != SMS_WAP_APPLICATION_LBS) {
1006 /* perform default action. */
1007 PUSH_APPLICATION_INFO_S pInfo = {0, };
1008 pInfo.appcode = default_appcode;
1009 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
1010 snprintf(application_id, app_id_len, "%s", appId);
1011 snprintf(content_type, content_type_len, "%s", contentType);
1012 pushAppInfoList.push_back(pInfo);
1015 dbHandle->freeTable();
1021 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1023 msg_error_t err = MSG_SUCCESS;
1024 if ((unsigned int)index > pushAppInfoList.size() - 1)
1025 return MSG_ERR_INVALID_PARAMETER;
1027 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1029 for (; it != pushAppInfoList.end(); it++) {
1030 if (index == count) {
1031 *appcode = it->appcode;
1041 msg_error_t SmsPluginStorage::releasePushEvent()
1043 msg_error_t err = MSG_SUCCESS;
1044 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1046 for (; it != pushAppInfoList.end(); it++)
1047 it = pushAppInfoList.erase(it);
1053 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
1056 MsgDbHandler *dbHandle = getDbHandle();
1057 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1058 msg_thread_id_t convId = 0;
1060 dbHandle->beginTrans();
1062 if (pMsgInfo->nAddressCnt > 0) {
1063 pMsgInfo->threadId = 0;
1064 msg_error_t err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
1066 if (err != MSG_SUCCESS) {
1067 dbHandle->endTrans(false);
1074 char *pFileData = NULL;
1075 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
1078 if (pMsgInfo->bTextSms == false) {
1079 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false) {
1080 dbHandle->endTrans(false);
1081 return MSG_ERR_STORAGE_ERROR;
1085 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
1086 DISPLAY_TIME = %lu, DATA_SIZE = %zu, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
1087 BACKUP = %d, SUBJECT = ?, MSG_TEXT = ?, SIM_INDEX = %d \
1088 WHERE MSG_ID = %d;",
1089 MSGFW_MESSAGE_TABLE_NAME, convId, pMsgInfo->folderId, pMsgInfo->storageId, pMsgInfo->msgType.mainType, pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize,
1090 pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected, pMsgInfo->priority, pMsgInfo->direction, pMsgInfo->bBackup, pMsgInfo->sim_idx, pMsgInfo->msgId);
1092 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1093 dbHandle->endTrans(false);
1094 return MSG_ERR_DB_EXEC;
1097 dbHandle->bindText(pMsgInfo->subject, 1);
1099 if (pMsgInfo->bTextSms == false)
1100 dbHandle->bindText(pFileData, 2);
1102 dbHandle->bindText(pMsgInfo->msgText, 2);
1104 MSG_DEBUG("%s", sqlQuery);
1106 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1107 dbHandle->finalizeQuery();
1108 dbHandle->endTrans(false);
1109 return MSG_ERR_DB_EXEC;
1112 dbHandle->finalizeQuery();
1113 dbHandle->endTrans(true);
1120 bool SmsPluginStorage::isDuplicatedCBMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
1122 msg_error_t err = MSG_SUCCESS;
1123 MsgDbHandler *dbHandle = getDbHandle();
1124 char sqlQuery[MAX_QUERY_LEN+1] = {0};
1127 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SERIAL_NUM = %d",
1128 MSGFW_CB_MSG_TABLE_NAME, pMsgInfo->serialNum);
1130 err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
1131 if (err == MSG_SUCCESS) {
1132 dbHandle->freeTable();
1136 dbHandle->freeTable();