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 dbHandle->finalizeQuery();
650 signed char folder_id = (signed char)folderId;
651 if (folder_id == MSG_INBOX_ID) {
652 msgType.classType = MSG_CLASS_NONE;
654 /** Set memory status in SIM */
655 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
656 MSG_DEBUG("Set Memory Status");
657 SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
661 MsgRefreshAllNotification(true, false, MSG_ACTIVE_NOTI_TYPE_NONE);
667 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
671 msg_error_t err = MSG_SUCCESS;
674 char keyName[MAX_VCONFKEY_NAME_LEN];
675 memset(keyName, 0x00, sizeof(keyName));
676 snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
678 if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS) {
679 MSG_ERR("MsgSettingGetBool [%s] failed", keyName);
682 if (bSimSst == false) {
683 return MSG_ERR_SIM_STORAGE_FULL;
686 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
687 memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
688 memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
689 memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
690 msgInfo.addressList = &addrInfo;
692 if (pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
693 MSG_DEBUG("pthread_create() error");
698 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
701 if (err == MSG_SUCCESS) {
702 MSG_DEBUG("Success to saveClass2Message.");
704 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
713 void* SmsPluginStorage::class2_thread(void *data)
717 msg_error_t err = MSG_SUCCESS;
718 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
720 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
721 if (err == MSG_SUCCESS) {
722 MSG_DEBUG("Success to saveClass2Message.");
724 MSG_ERR("Fail to saveClass2Message : [%d]", err);
732 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
736 MsgDbHandler *dbHandle = getDbHandle();
738 char sqlQuery[MAX_QUERY_LEN+1];
739 msg_thread_id_t convId = 0;
740 msg_message_id_t msgId = 0;
742 dbHandle->beginTrans();
744 if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
745 MSG_DEBUG("Address Info. exists [%d]", convId);
747 /** Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
748 memset(sqlQuery, 0x00, sizeof(sqlQuery));
749 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
750 WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
751 ORDER BY DISPLAY_TIME ASC;",
752 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
754 MSG_DEBUG("Query=[%s]", sqlQuery);
755 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
756 dbHandle->endTrans(false);
757 return MSG_ERR_DB_PREPARE;
760 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
761 *pMsgId = dbHandle->columnInt(0);
763 dbHandle->finalizeQuery();
764 dbHandle->endTrans(false);
765 return MSG_ERR_DB_STEP;
768 dbHandle->finalizeQuery();
771 dbHandle->endTrans(false);
772 return MSG_ERR_DB_NORECORD;
775 memset(sqlQuery, 0x00, sizeof(sqlQuery));
777 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
779 MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
781 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
782 dbHandle->endTrans(false);
783 return MSG_ERR_DB_PREPARE;
786 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
787 *pSimId = dbHandle->columnInt(0);
789 dbHandle->finalizeQuery();
790 dbHandle->endTrans(false);
791 return MSG_ERR_DB_STEP;
794 MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
796 dbHandle->finalizeQuery();
798 dbHandle->endTrans(true);
805 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
809 msg_error_t err = MSG_SUCCESS;
811 if (pSendOptInfo->bSetting == false) {
812 if (MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq) != MSG_SUCCESS)
813 MSG_INFO("MsgSettingGetBool() is failed");
815 if (MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath) != MSG_SUCCESS)
816 MSG_INFO("MsgSettingGetBool() is failed");
818 if (MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy) != MSG_SUCCESS)
819 MSG_INFO("MsgSettingGetBool() is failed");
822 MsgDbHandler *dbHandle = getDbHandle();
824 char sqlQuery[MAX_QUERY_LEN+1];
826 dbHandle->beginTrans();
828 memset(sqlQuery, 0x00, sizeof(sqlQuery));
829 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
830 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
831 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
833 MSG_DEBUG("Query = [%s]", sqlQuery);
835 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
836 MSG_ERR("execQuery() is failed");
837 dbHandle->endTrans(false);
838 return MSG_ERR_DB_EXEC;
841 dbHandle->endTrans(true);
849 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
851 msg_error_t err = MSG_SUCCESS;
853 MsgDbHandler *dbHandle = getDbHandle();
855 err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
857 if (err != MSG_SUCCESS) {
858 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
859 bool bAutoErase = false;
861 if (MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase) != MSG_SUCCESS)
862 MSG_INFO("MsgSettingGetBool() is failed");
864 MSG_DEBUG("bAutoErase: %d", bAutoErase);
866 if (bAutoErase == true) {
867 msg_message_id_t msgId;
869 /** Find the oldest message's msgId */
870 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
872 if (err != MSG_SUCCESS)
875 /** Delete the corresponding message. */
876 err = deleteSmsMessage(msgId);
889 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
891 msg_error_t err = MSG_SUCCESS;
895 MsgDbHandler *dbHandle = getDbHandle();
897 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
899 memset(sqlQuery, 0x00, sizeof(sqlQuery));
901 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
902 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
903 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
905 err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
906 MSG_DEBUG("rowCnt: %d", rowCnt);
908 dbHandle->freeTable();
912 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
914 msg_error_t err = MSG_SUCCESS;
916 unsigned int rowId = 0;
918 MsgDbHandler *dbHandle = getDbHandle();
920 char sqlQuery[MAX_QUERY_LEN+1];
922 err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
924 if (err != MSG_SUCCESS)
928 memset(sqlQuery, 0x00, sizeof(sqlQuery));
929 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
930 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
931 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
933 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
934 return MSG_ERR_DB_EXEC;
941 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
943 msg_error_t err = MSG_SUCCESS;
945 int rowCnt = 0, index = 0;
947 MsgDbHandler *dbHandle = getDbHandle();
949 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
951 memset(sqlQuery, 0x00, sizeof(sqlQuery));
953 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
955 err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
956 MSG_DEBUG("rowCnt: %d", rowCnt);
958 if (err != MSG_SUCCESS) {
959 dbHandle->freeTable();
961 if (err == MSG_ERR_DB_NORECORD)
967 char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0, };
968 char appId[MAX_WAPPUSH_ID_LEN + 1] = {0, };
969 int appcode = 0, default_appcode = 0;
971 char *_content_type = NULL, *_app_id = NULL;
975 for (int i = 0; i < rowCnt; i++) {
976 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
977 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
979 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
980 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
981 appcode = dbHandle->getColumnToInt(index++);
983 /* MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id); */
984 _content_type = strcasestr(pPushHeader, contentType);
986 _app_id = strcasestr(pPushHeader, appId);
988 default_appcode = appcode;
991 PUSH_APPLICATION_INFO_S pInfo = {0, };
992 pInfo.appcode = appcode;
993 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
994 snprintf(application_id, app_id_len, "%s", appId);
995 snprintf(content_type, content_type_len, "%s", contentType);
996 pushAppInfoList.push_back(pInfo);
1003 if (!found && default_appcode != SMS_WAP_APPLICATION_LBS) {
1004 /* perform default action. */
1005 PUSH_APPLICATION_INFO_S pInfo = {0, };
1006 pInfo.appcode = default_appcode;
1007 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
1008 snprintf(application_id, app_id_len, "%s", appId);
1009 snprintf(content_type, content_type_len, "%s", contentType);
1010 pushAppInfoList.push_back(pInfo);
1013 dbHandle->freeTable();
1019 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1021 msg_error_t err = MSG_SUCCESS;
1022 if ((unsigned int)index > pushAppInfoList.size() - 1)
1023 return MSG_ERR_INVALID_PARAMETER;
1025 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1027 for (; it != pushAppInfoList.end(); it++) {
1028 if (index == count) {
1029 *appcode = it->appcode;
1039 msg_error_t SmsPluginStorage::releasePushEvent()
1041 msg_error_t err = MSG_SUCCESS;
1042 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1044 for (; it != pushAppInfoList.end(); it++)
1045 it = pushAppInfoList.erase(it);
1051 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
1054 MsgDbHandler *dbHandle = getDbHandle();
1055 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1056 msg_thread_id_t convId = 0;
1058 dbHandle->beginTrans();
1060 if (pMsgInfo->nAddressCnt > 0) {
1061 pMsgInfo->threadId = 0;
1062 msg_error_t err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
1064 if (err != MSG_SUCCESS) {
1065 dbHandle->endTrans(false);
1072 char *pFileData = NULL;
1073 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
1076 if (pMsgInfo->bTextSms == false) {
1077 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false) {
1078 dbHandle->endTrans(false);
1079 return MSG_ERR_STORAGE_ERROR;
1083 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
1084 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
1085 BACKUP = %d, SUBJECT = ?, MSG_TEXT = ?, SIM_INDEX = %d \
1086 WHERE MSG_ID = %d;",
1087 MSGFW_MESSAGE_TABLE_NAME, convId, pMsgInfo->folderId, pMsgInfo->storageId, pMsgInfo->msgType.mainType, pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize,
1088 pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected, pMsgInfo->priority, pMsgInfo->direction, pMsgInfo->bBackup, pMsgInfo->sim_idx, pMsgInfo->msgId);
1090 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1091 dbHandle->endTrans(false);
1092 return MSG_ERR_DB_EXEC;
1095 dbHandle->bindText(pMsgInfo->subject, 1);
1097 if (pMsgInfo->bTextSms == false)
1098 dbHandle->bindText(pFileData, 2);
1100 dbHandle->bindText(pMsgInfo->msgText, 2);
1102 MSG_DEBUG("%s", sqlQuery);
1104 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1105 dbHandle->finalizeQuery();
1106 dbHandle->endTrans(false);
1107 return MSG_ERR_DB_EXEC;
1110 dbHandle->finalizeQuery();
1111 dbHandle->endTrans(true);