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 "SmsPluginStorage.h"
33 /*==================================================================================================
34 IMPLEMENTATION OF SmsPluginStorage - Member Functions
35 ==================================================================================================*/
36 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
39 SmsPluginStorage::SmsPluginStorage()
41 memset(&msgInfo, 0x00, sizeof(msgInfo));
42 memset(&addrInfo, 0x00, sizeof(addrInfo));
46 SmsPluginStorage::~SmsPluginStorage()
51 SmsPluginStorage* SmsPluginStorage::instance()
54 MSG_DEBUG("pInstance is NULL. Now creating instance.");
55 pInstance = new SmsPluginStorage();
62 msg_error_t SmsPluginStorage::insertMsgRef(MSG_MESSAGE_INFO_S *pMsg, unsigned char msgRef, int index)
66 time_t curTime = time(NULL);
68 MsgDbHandler *dbHandle = getDbHandle();
70 char sqlQuery[MAX_QUERY_LEN+1];
71 char *normalNum = NULL;
73 memset(sqlQuery, 0x00, sizeof(sqlQuery));
75 normalNum = msg_normalize_number(pMsg->addressList[index].addressVal);
77 MSG_SEC_DEBUG("Insert MsgID=[%d], Address=[%s], MsgRef=[%d], Time=[%d]", \
78 pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
80 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %s, %d, 0, -1, %d);",
81 MSGFW_SMS_REPORT_TABLE_NAME, pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
83 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
84 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
85 return MSG_ERR_DB_EXEC;
94 msg_error_t SmsPluginStorage::updateMsgDeliverStatus(MSG_MESSAGE_INFO_S *pMsgInfo, unsigned char msgRef)
98 MsgDbHandler *dbHandle = getDbHandle();
100 char sqlQuery[MAX_QUERY_LEN+1];
102 msg_message_id_t msgId = 0;
104 char *normalNum = NULL;
106 normalNum = msg_normalize_number(pMsgInfo->addressList[0].addressVal);
108 memset(sqlQuery, 0x00, sizeof(sqlQuery));
109 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_VAL = %s AND MSG_REF > 0 ORDER BY TIME ASC;",
110 MSGFW_SMS_REPORT_TABLE_NAME, normalNum);
111 MSG_DEBUG("[SQL Query] %s", sqlQuery);
113 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
114 return MSG_ERR_DB_PREPARE;
116 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
117 msgId = dbHandle->columnInt(0);
119 dbHandle->finalizeQuery();
121 pMsgInfo->msgId = msgId;
123 /** Update Status - MSG_MESSAGE_TABLE */
124 memset(sqlQuery, 0x00, sizeof(sqlQuery));
125 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE MSG_ID = %d AND MSG_REF > 0;",
126 MSGFW_SMS_REPORT_TABLE_NAME, msgId);
128 if (dbHandle->getTable(sqlQuery, &rowCnt, NULL) != MSG_SUCCESS) {
129 dbHandle->freeTable();
130 return MSG_ERR_DB_GETTABLE;
133 dbHandle->freeTable();
135 MSG_DEBUG("Selected row count = [%d]", rowCnt);
137 if (rowCnt == 1 && pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
138 memset(sqlQuery, 0x00, sizeof(sqlQuery));
139 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
140 MSGFW_MESSAGE_TABLE_NAME, (int)pMsgInfo->networkStatus, msgId);
141 MSG_DEBUG("[SQL Query] %s", sqlQuery);
143 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
144 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
145 return MSG_ERR_DB_EXEC;
149 /** Update Status - MSG_REPORT_TABLE */
150 if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
151 memset(sqlQuery, 0x00, sizeof(sqlQuery));
152 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
153 MSGFW_SMS_REPORT_TABLE_NAME, 1, (int)pMsgInfo->displayTime, msgId, normalNum);
154 MSG_DEBUG("[SQL Query] %s", sqlQuery);
156 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
157 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
158 return MSG_ERR_DB_EXEC;
160 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_EXPIRED) {
161 memset(sqlQuery, 0x00, sizeof(sqlQuery));
162 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
163 MSGFW_SMS_REPORT_TABLE_NAME, 0, (int)pMsgInfo->displayTime, msgId, normalNum);
164 MSG_DEBUG("[SQL Query] %s", sqlQuery);
166 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
167 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
168 return MSG_ERR_DB_EXEC;
170 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_PENDING) {
171 memset(sqlQuery, 0x00, sizeof(sqlQuery));
172 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
173 MSGFW_SMS_REPORT_TABLE_NAME, 3, (int)pMsgInfo->displayTime, msgId, normalNum);
174 MSG_DEBUG("[SQL Query] %s", sqlQuery);
176 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
177 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
178 return MSG_ERR_DB_EXEC;
180 } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL) {
181 memset(sqlQuery, 0x00, sizeof(sqlQuery));
182 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
183 MSGFW_SMS_REPORT_TABLE_NAME, 8, (int)pMsgInfo->displayTime, msgId, normalNum);
184 MSG_DEBUG("[SQL Query] %s", sqlQuery);
186 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
187 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
188 return MSG_ERR_DB_EXEC;
198 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
202 if (!pMsgInfo || (pMsgInfo && pMsgInfo->msgId <= 0)) {
203 MSG_DEBUG("Invalid message id");
204 return MSG_ERR_INVALID_MESSAGE_ID;
207 /*** Comment below line to not save the time value after sent status (it could be used later.)
208 time_t curTime = time(NULL);
210 MsgDbHandler *dbHandle = getDbHandle();
212 char sqlQuery[MAX_QUERY_LEN+1];
214 memset(sqlQuery, 0x00, sizeof(sqlQuery));
216 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
218 /** Move Msg to SENTBOX */
219 if (status == MSG_NETWORK_SEND_SUCCESS) {
220 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
221 MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
223 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, READ_STATUS = 0 WHERE MSG_ID = %d;",
224 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
227 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
228 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
229 return MSG_ERR_DB_EXEC;
232 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
233 if (status == MSG_NETWORK_SEND_SUCCESS) {
234 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
235 MsgAddPhoneLog(pMsgInfo);
237 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
243 #ifdef SMS_REPORT_OPERATION
244 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
248 MsgDbHandler *dbHandle = getDbHandle();
250 char sqlQuery[MAX_QUERY_LEN+1];
252 memset(sqlQuery, 0x00, sizeof(sqlQuery));
254 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
255 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
257 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
258 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
259 return MSG_ERR_DB_EXEC;
262 /** Set Message Reference for updating report table */
265 MSG_DEBUG("MsgRef : %d", MsgRef);
273 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
277 MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
279 MsgDbHandler *dbHandle = getDbHandle();
281 char sqlQuery[MAX_QUERY_LEN+1];
283 /** Get Msg Id for Quickpanel Noti */
284 msg_message_id_t msgId = 0;
286 memset(sqlQuery, 0x00, sizeof(sqlQuery));
287 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
288 MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
290 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
291 return MSG_ERR_DB_PREPARE;
293 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
294 msgId = dbHandle->columnInt(0);
296 dbHandle->finalizeQuery();
299 memset(sqlQuery, 0x00, sizeof(sqlQuery));
300 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
301 MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
303 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
304 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
305 return MSG_ERR_DB_EXEC;
308 /** Insert Quickpanel Noti */
309 msg_error_t ret = MSG_SUCCESS;
311 ret = MsgInsertSmsNotiToQuickpanel(dbHandle, msgId, Status);
313 if (ret != MSG_SUCCESS) {
314 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
324 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo, int *simIdList)
326 msg_error_t err = MSG_SUCCESS;
328 unsigned int simId = 0;
329 MsgDbHandler *dbHandle = getDbHandle();
331 char sqlQuery[MAX_QUERY_LEN+1];
334 MSG_DEBUG("simIdList exist.");
335 for (int i = 0; i < MAX_SIM_SMS_NUM; ++i) {
337 simId = simIdList[i] - 1;
338 memset(sqlQuery, 0x00, sizeof(sqlQuery));
339 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
340 MSGFW_SIM_MSG_TABLE_NAME, simId, pSimMsgInfo->msgId);
342 MSG_DEBUG("QUERY : %s", sqlQuery);
344 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
345 return MSG_ERR_DB_EXEC;
357 msg_error_t SmsPluginStorage::insertSimMessage(int simId, int msgId)
361 MsgDbHandler *dbHandle = getDbHandle();
363 char sqlQuery[MAX_QUERY_LEN+1];
365 dbHandle->beginTrans();
367 /** Insert Message into msg_sim table */
368 memset(sqlQuery, 0x00, sizeof(sqlQuery));
369 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);", MSGFW_SIM_MSG_TABLE_NAME, simId, msgId);
370 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
371 dbHandle->endTrans(false);
372 return MSG_ERR_DB_EXEC;
375 dbHandle->endTrans(true);
381 msg_error_t SmsPluginStorage::deleteSimMessage(int sim_idx, int simId)
385 MsgDbHandler *dbHandle = getDbHandle();
387 char sqlQuery[MAX_QUERY_LEN+1];
389 dbHandle->beginTrans();
391 /** Delete Message from msg_sim table */
392 memset(sqlQuery, 0x00, sizeof(sqlQuery));
393 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d AND SIM_SLOT_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simId, sim_idx);
394 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
395 dbHandle->endTrans(false);
396 return MSG_ERR_DB_EXEC;
399 dbHandle->endTrans(true);
405 msg_error_t SmsPluginStorage::checkMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
409 msg_error_t err = MSG_SUCCESS;
411 /** Check whether storage is full or not */
412 err = checkStorageStatus(pMsgInfo);
414 if (err != MSG_SUCCESS) {
415 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
416 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);
423 else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
429 /** Amend message information for type **/
430 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
431 MSG_DEBUG("Normal SMS");
433 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
434 err = addClass2Message(pMsgInfo);
435 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
436 /** Class 0 Msg should be saved in hidden folder */
437 pMsgInfo->folderId = 0;
440 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
441 MSG_DEBUG("Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
443 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
444 err = addClass2Message(pMsgInfo);
445 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
446 /** Class 0 Msg should be saved in hidden folder */
447 pMsgInfo->folderId = 0;
448 pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
451 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
452 if (pMsgInfo->bStore == true) {
453 MSG_DEBUG("MWI Message");
455 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
456 err = addClass2Message(pMsgInfo);
460 MSG_DEBUG("No matching type [%d]", pMsgInfo->msgType.subType);
463 if (err == MSG_SUCCESS) {
464 MSG_DEBUG("Success to check message !!");
466 MSG_DEBUG("fail to check message !! : [%d]", err);
473 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
475 msg_error_t err = MSG_SUCCESS;
477 unsigned int rowId = 0;
478 msg_thread_id_t convId = 0;
480 MsgDbHandler *dbHandle = getDbHandle();
482 dbHandle->beginTrans();
484 if (pMsgInfo->nAddressCnt > 0) {
485 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
487 if (err != MSG_SUCCESS) {
488 dbHandle->endTrans(false);
492 pMsgInfo->threadId = convId;
495 /** Add Message Table */
496 rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
499 dbHandle->endTrans(false);
500 return MSG_ERR_DB_ROW;
503 /** Update conversation table */
504 err = MsgStoUpdateConversation(dbHandle, convId);
506 if (err != MSG_SUCCESS) {
507 dbHandle->endTrans(false);
511 err = dbHandle->endTrans(true);
512 if (err != MSG_SUCCESS) {
516 pMsgInfo->msgId = (msg_message_id_t)rowId;
524 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
528 MsgDbHandler *dbHandle = getDbHandle();
530 char sqlQuery[MAX_QUERY_LEN+1];
532 /** Get SUB_TYPE, STORAGE_ID */
533 memset(sqlQuery, 0x00, sizeof(sqlQuery));
534 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, CONV_ID, SIM_INDEX \
535 FROM %s WHERE MSG_ID = %d;",
536 MSGFW_MESSAGE_TABLE_NAME, msgId);
538 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
539 return MSG_ERR_DB_PREPARE;
541 MSG_MESSAGE_TYPE_S msgType;
542 msg_folder_id_t folderId;
544 msg_thread_id_t convId;
545 msg_sim_slot_id_t simIndex;
547 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
548 msgType.mainType = dbHandle->columnInt(0);
549 msgType.subType = dbHandle->columnInt(1);
550 folderId = dbHandle->columnInt(2);
551 convId = dbHandle->columnInt(3);
552 simIndex = dbHandle->columnInt(4);
554 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
556 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
557 dbHandle->finalizeQuery();
558 return MSG_ERR_DB_STEP;
561 dbHandle->finalizeQuery();
563 dbHandle->beginTrans();
565 memset(sqlQuery, 0x00, sizeof(sqlQuery));
566 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
568 /** Delete SMS Send Option */
569 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
570 dbHandle->endTrans(false);
571 return MSG_ERR_DB_EXEC;
574 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
575 memset(sqlQuery, 0x00, sizeof(sqlQuery));
576 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
578 /** Delete Push Message from push table */
579 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
580 dbHandle->endTrans(false);
581 return MSG_ERR_DB_EXEC;
583 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
584 memset(sqlQuery, 0x00, sizeof(sqlQuery));
585 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
587 /** Delete Push Message from push table */
588 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
589 dbHandle->endTrans(false);
590 return MSG_ERR_DB_EXEC;
592 } else if (msgType.subType == MSG_SYNCML_CP) {
593 memset(sqlQuery, 0x00, sizeof(sqlQuery));
594 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
596 /** Delete SyncML Message from syncML table */
597 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
598 dbHandle->endTrans(false);
599 return MSG_ERR_DB_EXEC;
603 /** Delete Message from msg table */
604 memset(sqlQuery, 0x00, sizeof(sqlQuery));
605 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
606 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
607 dbHandle->endTrans(false);
608 return MSG_ERR_DB_EXEC;
611 /** Delete Message from msg_report table */
612 memset(sqlQuery, 0x00, sizeof(sqlQuery));
613 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_REPORT_TABLE_NAME, msgId);
614 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
615 dbHandle->endTrans(false);
616 return MSG_ERR_DB_EXEC;
619 /** Delete Message from msg_sim table */
620 memset(sqlQuery, 0x00, sizeof(sqlQuery));
621 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, msgId);
622 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
623 dbHandle->endTrans(false);
624 return MSG_ERR_DB_EXEC;
627 /** Clear Conversation table */
628 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
629 dbHandle->endTrans(false);
630 return MSG_ERR_DB_EXEC;
633 /** Update conversation table.*/
634 if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
635 dbHandle->endTrans(false);
636 return MSG_ERR_STORAGE_ERROR;
639 dbHandle->endTrans(true);
641 signed char folder_id = (signed char)folderId;
642 if (folder_id == MSG_INBOX_ID) {
643 msgType.classType = MSG_CLASS_NONE;
645 /** Set memory status in SIM */
646 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
647 MSG_DEBUG("Set Memory Status");
648 SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
652 MsgRefreshAllNotification(true, false, MSG_ACTIVE_NOTI_TYPE_NONE);
658 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
662 msg_error_t err = MSG_SUCCESS;
665 char keyName[MAX_VCONFKEY_NAME_LEN];
666 memset(keyName, 0x00, sizeof(keyName));
667 snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
669 if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS) {
670 MSG_ERR("MsgSettingGetBool [%s] failed", keyName);
673 if (bSimSst == false) {
674 return MSG_ERR_SIM_STORAGE_FULL;
677 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
678 memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
679 memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
680 memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
681 msgInfo.addressList = &addrInfo;
683 if (pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
684 MSG_DEBUG("pthread_create() error");
689 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
692 if (err == MSG_SUCCESS) {
693 MSG_DEBUG("Success to saveClass2Message.");
695 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
704 void* SmsPluginStorage::class2_thread(void *data)
708 msg_error_t err = MSG_SUCCESS;
709 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
711 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
712 if (err == MSG_SUCCESS) {
713 MSG_DEBUG("Success to saveClass2Message.");
715 MSG_ERR("Fail to saveClass2Message : [%d]", err);
723 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
727 MsgDbHandler *dbHandle = getDbHandle();
729 char sqlQuery[MAX_QUERY_LEN+1];
730 msg_thread_id_t convId = 0;
731 msg_message_id_t msgId = 0;
733 dbHandle->beginTrans();
735 if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
736 MSG_DEBUG("Address Info. exists [%d]", convId);
738 /** Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
739 memset(sqlQuery, 0x00, sizeof(sqlQuery));
740 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
741 WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
742 ORDER BY DISPLAY_TIME ASC;",
743 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
745 MSG_DEBUG("Query=[%s]", sqlQuery);
746 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
747 dbHandle->endTrans(false);
748 return MSG_ERR_DB_PREPARE;
751 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
752 *pMsgId = dbHandle->columnInt(0);
754 dbHandle->finalizeQuery();
755 dbHandle->endTrans(false);
756 return MSG_ERR_DB_STEP;
759 dbHandle->finalizeQuery();
762 dbHandle->endTrans(false);
763 return MSG_ERR_DB_NORECORD;
766 memset(sqlQuery, 0x00, sizeof(sqlQuery));
768 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
770 MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
772 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
773 dbHandle->endTrans(false);
774 return MSG_ERR_DB_PREPARE;
777 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
778 *pSimId = dbHandle->columnInt(0);
780 dbHandle->finalizeQuery();
781 dbHandle->endTrans(false);
782 return MSG_ERR_DB_STEP;
785 MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
787 dbHandle->finalizeQuery();
789 dbHandle->endTrans(true);
796 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
800 msg_error_t err = MSG_SUCCESS;
802 if (pSendOptInfo->bSetting == false) {
803 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
804 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
806 MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
809 MsgDbHandler *dbHandle = getDbHandle();
811 char sqlQuery[MAX_QUERY_LEN+1];
813 dbHandle->beginTrans();
815 memset(sqlQuery, 0x00, sizeof(sqlQuery));
816 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
817 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
818 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
820 MSG_DEBUG("Query = [%s]", sqlQuery);
822 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
823 MSG_ERR("execQuery() is failed");
824 dbHandle->endTrans(false);
825 return MSG_ERR_DB_EXEC;
828 dbHandle->endTrans(true);
836 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
838 msg_error_t err = MSG_SUCCESS;
840 MsgDbHandler *dbHandle = getDbHandle();
842 err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
844 if (err != MSG_SUCCESS) {
845 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
846 bool bAutoErase = false;
848 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
850 MSG_DEBUG("bAutoErase: %d", bAutoErase);
852 if (bAutoErase == true) {
853 msg_message_id_t msgId;
855 /** Find the oldest message's msgId */
856 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
858 if (err != MSG_SUCCESS)
861 /** Delete the corresponding message. */
862 err = deleteSmsMessage(msgId);
875 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
877 msg_error_t err = MSG_SUCCESS;
881 MsgDbHandler *dbHandle = getDbHandle();
883 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
885 memset(sqlQuery, 0x00, sizeof(sqlQuery));
887 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
888 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
889 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
891 err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
892 MSG_DEBUG("rowCnt: %d", rowCnt);
894 dbHandle->freeTable();
898 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
900 msg_error_t err = MSG_SUCCESS;
902 unsigned int rowId = 0;
904 MsgDbHandler *dbHandle = getDbHandle();
906 char sqlQuery[MAX_QUERY_LEN+1];
908 err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
910 if (err != MSG_SUCCESS)
914 memset(sqlQuery, 0x00, sizeof(sqlQuery));
915 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
916 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
917 CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
919 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
920 return MSG_ERR_DB_EXEC;
927 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
929 msg_error_t err = MSG_SUCCESS;
931 int rowCnt = 0, index = 0;
933 MsgDbHandler *dbHandle = getDbHandle();
935 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
937 memset(sqlQuery, 0x00, sizeof(sqlQuery));
939 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
941 err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
942 MSG_DEBUG("rowCnt: %d", rowCnt);
944 if (err != MSG_SUCCESS) {
945 dbHandle->freeTable();
947 if (err == MSG_ERR_DB_NORECORD)
953 char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0, };
954 char appId[MAX_WAPPUSH_ID_LEN + 1] = {0, };
955 int appcode = 0, default_appcode = 0;
957 char *_content_type = NULL, *_app_id = NULL;
961 for (int i = 0; i < rowCnt; i++) {
962 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
963 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
965 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
966 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
967 appcode = dbHandle->getColumnToInt(index++);
969 /* MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id); */
970 _content_type = strcasestr(pPushHeader, contentType);
972 _app_id = strcasestr(pPushHeader, appId);
974 default_appcode = appcode;
977 PUSH_APPLICATION_INFO_S pInfo = {0, };
978 pInfo.appcode = appcode;
979 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
980 snprintf(application_id, app_id_len, "%s", appId);
981 snprintf(content_type, content_type_len, "%s", contentType);
982 pushAppInfoList.push_back(pInfo);
989 if (!found && default_appcode != SMS_WAP_APPLICATION_LBS) {
990 /* perform default action. */
991 PUSH_APPLICATION_INFO_S pInfo = {0, };
992 pInfo.appcode = default_appcode;
993 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
994 snprintf(application_id, app_id_len, "%s", appId);
995 snprintf(content_type, content_type_len, "%s", contentType);
996 pushAppInfoList.push_back(pInfo);
999 dbHandle->freeTable();
1005 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1007 msg_error_t err = MSG_SUCCESS;
1008 if ((unsigned int)index > pushAppInfoList.size() - 1)
1009 return MSG_ERR_INVALID_PARAMETER;
1011 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1013 for (; it != pushAppInfoList.end(); it++) {
1014 if (index == count) {
1015 *appcode = it->appcode;
1025 msg_error_t SmsPluginStorage::releasePushEvent()
1027 msg_error_t err = MSG_SUCCESS;
1028 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1030 for (; it != pushAppInfoList.end(); it++)
1031 it = pushAppInfoList.erase(it);
1037 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
1040 MsgDbHandler *dbHandle = getDbHandle();
1041 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1042 msg_thread_id_t convId = 0;
1044 dbHandle->beginTrans();
1046 if (pMsgInfo->nAddressCnt > 0) {
1047 pMsgInfo->threadId = 0;
1048 msg_error_t err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
1050 if (err != MSG_SUCCESS) {
1051 dbHandle->endTrans(false);
1058 char *pFileData = NULL;
1059 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
1062 if (pMsgInfo->bTextSms == false) {
1063 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false) {
1064 dbHandle->endTrans(false);
1065 return MSG_ERR_STORAGE_ERROR;
1069 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
1070 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
1071 BACKUP = %d, SUBJECT = ?, MSG_TEXT = ?, SIM_INDEX = %d \
1072 WHERE MSG_ID = %d;",
1073 MSGFW_MESSAGE_TABLE_NAME, convId, pMsgInfo->folderId, pMsgInfo->storageId, pMsgInfo->msgType.mainType, pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize,
1074 pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected, pMsgInfo->priority, pMsgInfo->direction, pMsgInfo->bBackup, pMsgInfo->sim_idx, pMsgInfo->msgId);
1076 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1077 dbHandle->endTrans(false);
1078 return MSG_ERR_DB_EXEC;
1081 dbHandle->bindText(pMsgInfo->subject, 1);
1083 if (pMsgInfo->bTextSms == false)
1084 dbHandle->bindText(pFileData, 2);
1086 dbHandle->bindText(pMsgInfo->msgText, 2);
1088 MSG_DEBUG("%s", sqlQuery);
1090 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1091 dbHandle->finalizeQuery();
1092 dbHandle->endTrans(false);
1093 return MSG_ERR_DB_EXEC;
1096 dbHandle->finalizeQuery();
1097 dbHandle->endTrans(true);