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 /*** No need to connect DB anymore.
42 if (dbHandle->connect() != MSG_SUCCESS) {
43 MSG_DEBUG("DB Connect Fail");
49 SmsPluginStorage::~SmsPluginStorage()
55 SmsPluginStorage* SmsPluginStorage::instance()
58 MSG_DEBUG("pInstance is NULL. Now creating instance.");
59 pInstance = new SmsPluginStorage();
66 msg_error_t SmsPluginStorage::insertMsgRef(MSG_MESSAGE_INFO_S *pMsg, unsigned char msgRef, int index)
70 time_t curTime = time(NULL);
72 MsgDbHandler *dbHandle = getDbHandle();
74 char sqlQuery[MAX_QUERY_LEN+1];
75 char *normalNum = NULL;
77 memset(sqlQuery, 0x00, sizeof(sqlQuery));
79 normalNum = msg_normalize_number(pMsg->addressList[index].addressVal);
81 MSG_SEC_DEBUG("Insert MsgID=[%d], Address=[%s], MsgRef=[%d], Time=[%d]", \
82 pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
84 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %s, %d, 0, -1, %d);",
85 MSGFW_SMS_REPORT_TABLE_NAME, pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
87 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
88 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
89 return MSG_ERR_DB_EXEC;
99 msg_error_t SmsPluginStorage::updateMsgDeliverStatus(MSG_MESSAGE_INFO_S *pMsgInfo, unsigned char msgRef)
103 MsgDbHandler *dbHandle = getDbHandle();
105 char sqlQuery[MAX_QUERY_LEN+1];
107 msg_message_id_t msgId = 0;
109 char *normalNum = NULL;
111 normalNum = msg_normalize_number(pMsgInfo->addressList[0].addressVal);
113 memset(sqlQuery, 0x00, sizeof(sqlQuery));
114 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_VAL = %s AND MSG_REF > 0 ORDER BY TIME ASC;",
115 MSGFW_SMS_REPORT_TABLE_NAME, normalNum);
116 MSG_DEBUG("[SQL Query] %s", sqlQuery);
118 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
119 return MSG_ERR_DB_PREPARE;
121 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
122 msgId = dbHandle->columnInt(0);
124 dbHandle->finalizeQuery();
126 pMsgInfo->msgId = msgId;
128 /** Update Status - MSG_MESSAGE_TABLE */
129 memset(sqlQuery, 0x00, sizeof(sqlQuery));
130 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE MSG_ID = %d AND MSG_REF > 0;",
131 MSGFW_SMS_REPORT_TABLE_NAME, msgId);
133 if (dbHandle->getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
134 dbHandle->freeTable();
135 return MSG_ERR_DB_GETTABLE;
138 dbHandle->freeTable();
140 MSG_DEBUG("Selected row count = [%d]", rowCnt);
142 if (rowCnt == 1 && pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
143 memset(sqlQuery, 0x00, sizeof(sqlQuery));
144 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
145 MSGFW_MESSAGE_TABLE_NAME, (int)pMsgInfo->networkStatus, msgId);
146 MSG_DEBUG("[SQL Query] %s", sqlQuery);
148 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
149 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
150 return MSG_ERR_DB_EXEC;
154 /** Update Status - MSG_REPORT_TABLE */
155 if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
156 memset(sqlQuery, 0x00, sizeof(sqlQuery));
157 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
158 MSGFW_SMS_REPORT_TABLE_NAME, 1, (int)pMsgInfo->displayTime, msgId, normalNum);
159 MSG_DEBUG("[SQL Query] %s", sqlQuery);
161 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
162 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
163 return MSG_ERR_DB_EXEC;
165 } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_EXPIRED) {
166 memset(sqlQuery, 0x00, sizeof(sqlQuery));
167 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
168 MSGFW_SMS_REPORT_TABLE_NAME, 0, (int)pMsgInfo->displayTime, msgId, normalNum);
169 MSG_DEBUG("[SQL Query] %s", sqlQuery);
171 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
172 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
173 return MSG_ERR_DB_EXEC;
175 } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_PENDING) {
176 memset(sqlQuery, 0x00, sizeof(sqlQuery));
177 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
178 MSGFW_SMS_REPORT_TABLE_NAME, 3, (int)pMsgInfo->displayTime, msgId, normalNum);
179 MSG_DEBUG("[SQL Query] %s", sqlQuery);
181 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
182 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
183 return MSG_ERR_DB_EXEC;
185 } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL) {
186 memset(sqlQuery, 0x00, sizeof(sqlQuery));
187 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
188 MSGFW_SMS_REPORT_TABLE_NAME, 8, (int)pMsgInfo->displayTime, msgId, normalNum);
189 MSG_DEBUG("[SQL Query] %s", sqlQuery);
191 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
192 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
193 return MSG_ERR_DB_EXEC;
203 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
207 if (!pMsgInfo || (pMsgInfo && pMsgInfo->msgId <= 0)) {
208 MSG_DEBUG("Invalid message id");
209 return MSG_ERR_INVALID_MESSAGE_ID;
212 /*** Comment below line to not save the time value after sent status (it could be used later.)
213 time_t curTime = time(NULL);
215 MsgDbHandler *dbHandle = getDbHandle();
217 char sqlQuery[MAX_QUERY_LEN+1];
219 memset(sqlQuery, 0x00, sizeof(sqlQuery));
221 MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
223 /** Move Msg to SENTBOX */
224 if (status == MSG_NETWORK_SEND_SUCCESS) {
225 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
226 MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
228 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, READ_STATUS = 0 WHERE MSG_ID = %d;",
229 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
232 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
233 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
234 return MSG_ERR_DB_EXEC;
236 //contacts-service is not used for gear
237 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
238 if (status == MSG_NETWORK_SEND_SUCCESS) {
239 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
240 MsgAddPhoneLog(pMsgInfo);
242 #endif //MSG_CONTACTS_SERVICE_NOT_SUPPORTED
248 #ifdef SMS_REPORT_OPERATION
249 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
253 MsgDbHandler *dbHandle = getDbHandle();
255 char sqlQuery[MAX_QUERY_LEN+1];
257 memset(sqlQuery, 0x00, sizeof(sqlQuery));
259 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
260 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
262 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
263 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
264 return MSG_ERR_DB_EXEC;
267 /** Set Message Reference for updating report table */
270 MSG_DEBUG("MsgRef : %d", MsgRef);
278 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
282 MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
284 MsgDbHandler *dbHandle = getDbHandle();
286 char sqlQuery[MAX_QUERY_LEN+1];
288 /** Get Msg Id for Quickpanel Noti */
289 msg_message_id_t msgId = 0;
291 memset(sqlQuery, 0x00, sizeof(sqlQuery));
292 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
293 MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
295 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
296 return MSG_ERR_DB_PREPARE;
298 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
299 msgId = dbHandle->columnInt(0);
301 dbHandle->finalizeQuery();
304 memset(sqlQuery, 0x00, sizeof(sqlQuery));
305 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
306 MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
308 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
309 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
310 return MSG_ERR_DB_EXEC;
313 /** Insert Quickpanel Noti */
314 msg_error_t ret = MSG_SUCCESS;
316 ret = MsgInsertSmsNotiToQuickpanel(dbHandle, msgId, Status);
318 if (ret != MSG_SUCCESS) {
319 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
329 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo, int *simIdList)
331 msg_error_t err = MSG_SUCCESS;
333 unsigned int simId = 0;
334 MsgDbHandler *dbHandle = getDbHandle();
336 char sqlQuery[MAX_QUERY_LEN+1];
338 // dbHandle->beginTrans();
341 msg_message_id_t msgId = 0;
342 msg_thread_id_t convId = 0;
344 err = MsgStoAddAddress(dbHandle, pSimMsgInfo, &convId);
346 if (err != MSG_SUCCESS) {
347 dbHandle->endTrans(false);
351 pSimMsgInfo->threadId = convId;
353 err = dbHandle->getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
355 if (err != MSG_SUCCESS) {
356 dbHandle->endTrans(false);
360 //simId = pSimMsgInfo->msgId;
361 pSimMsgInfo->msgId = msgId;
364 memset(sqlQuery, 0x00, sizeof(sqlQuery));
366 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, 0, 0, 0);",
367 MSGFW_MESSAGE_TABLE_NAME, msgId, convId, pSimMsgInfo->folderId, pSimMsgInfo->storageId,
368 pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
369 pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
370 pSimMsgInfo->direction, 0, pSimMsgInfo->bBackup);
372 MSG_DEBUG("QUERY : %s", sqlQuery);
374 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
375 dbHandle->endTrans(false);
376 return MSG_ERR_DB_PREPARE;
379 dbHandle->bindText(pSimMsgInfo->subject, 1);
381 dbHandle->bindText(pSimMsgInfo->msgText, 2);
383 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
384 dbHandle->endTrans(false);
385 return MSG_ERR_DB_STEP;
388 dbHandle->finalizeQuery();
391 MSG_DEBUG("simIdList exist.");
392 for(int i=0; i < MAX_SIM_SMS_NUM; ++i)
395 simId = simIdList[i] - 1;
396 memset(sqlQuery, 0x00, sizeof(sqlQuery));
397 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
398 MSGFW_SIM_MSG_TABLE_NAME, simId, pSimMsgInfo->msgId);
400 MSG_DEBUG("QUERY : %s", sqlQuery);
402 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
403 // dbHandle->endTrans(false);
404 return MSG_ERR_DB_EXEC;
411 // dbHandle->endTrans(true);
418 msg_error_t SmsPluginStorage::insertSimMessage(int simId, int msgId)
422 MsgDbHandler *dbHandle = getDbHandle();
424 char sqlQuery[MAX_QUERY_LEN+1];
426 dbHandle->beginTrans();
428 /** Insert Message into msg_sim table */
429 memset(sqlQuery, 0x00, sizeof(sqlQuery));
430 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);", MSGFW_SIM_MSG_TABLE_NAME, simId, msgId);
431 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
432 dbHandle->endTrans(false);
433 return MSG_ERR_DB_EXEC;
436 dbHandle->endTrans(true);
442 msg_error_t SmsPluginStorage::deleteSimMessage(int sim_idx, int simId)
446 MsgDbHandler *dbHandle = getDbHandle();
448 char sqlQuery[MAX_QUERY_LEN+1];
450 dbHandle->beginTrans();
452 /** Delete Message from msg_sim table */
453 memset(sqlQuery, 0x00, sizeof(sqlQuery));
454 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d AND SIM_SLOT_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simId, sim_idx);
455 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
456 dbHandle->endTrans(false);
457 return MSG_ERR_DB_EXEC;
460 dbHandle->endTrans(true);
466 msg_error_t SmsPluginStorage::checkMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
470 msg_error_t err = MSG_SUCCESS;
472 /** Check whether storage is full or not */
473 err = checkStorageStatus(pMsgInfo);
475 if (err != MSG_SUCCESS) {
476 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
477 pMsgInfo->folderId = 0;
480 else if(pMsgInfo->msgType.classType == MSG_CLASS_2 &&
481 (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS)) {
482 err = addClass2Message(pMsgInfo);
484 else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS){
490 /** Amend message information for type **/
491 if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
493 MSG_DEBUG("Normal SMS");
495 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
496 err = addClass2Message(pMsgInfo);
497 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
498 /** Class 0 Msg should be saved in hidden folder */
499 pMsgInfo->folderId = 0;
502 } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
503 MSG_DEBUG("Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
505 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
506 err = addClass2Message(pMsgInfo);
507 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
508 /** Class 0 Msg should be saved in hidden folder */
509 pMsgInfo->folderId = 0;
510 pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
513 } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
514 if (pMsgInfo->bStore == true) {
515 MSG_DEBUG("MWI Message");
517 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
518 err = addClass2Message(pMsgInfo);
522 MSG_DEBUG("No matching type [%d]", pMsgInfo->msgType.subType);
525 if (err == MSG_SUCCESS) {
526 MSG_DEBUG("Success to check message !!");
528 MSG_DEBUG("fail to check message !! : [%d]", err);
535 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
537 msg_error_t err = MSG_SUCCESS;
539 unsigned int rowId = 0;
540 msg_thread_id_t convId = 0;
542 MsgDbHandler *dbHandle = getDbHandle();
544 dbHandle->beginTrans();
546 if (pMsgInfo->nAddressCnt > 0) {
548 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
550 if (err != MSG_SUCCESS) {
551 dbHandle->endTrans(false);
555 pMsgInfo->threadId = convId;
558 /** Add Message Table */
559 rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
562 dbHandle->endTrans(false);
563 return MSG_ERR_DB_ROW;
566 /** Update conversation table */
567 err = MsgStoUpdateConversation(dbHandle, convId);
569 if (err != MSG_SUCCESS) {
570 dbHandle->endTrans(false);
574 err = dbHandle->endTrans(true);
575 if (err != MSG_SUCCESS) {
579 pMsgInfo->msgId = (msg_message_id_t)rowId;
587 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
591 MsgDbHandler *dbHandle = getDbHandle();
593 char sqlQuery[MAX_QUERY_LEN+1];
595 /** Get SUB_TYPE, STORAGE_ID */
596 memset(sqlQuery, 0x00, sizeof(sqlQuery));
597 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, CONV_ID, SIM_INDEX \
598 FROM %s WHERE MSG_ID = %d;",
599 MSGFW_MESSAGE_TABLE_NAME, msgId);
601 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
602 return MSG_ERR_DB_PREPARE;
604 MSG_MESSAGE_TYPE_S msgType;
605 msg_folder_id_t folderId;
607 msg_thread_id_t convId;
608 msg_sim_slot_id_t simIndex;
610 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
611 msgType.mainType = dbHandle->columnInt(0);
612 msgType.subType = dbHandle->columnInt(1);
613 folderId = dbHandle->columnInt(2);
614 convId = dbHandle->columnInt(3);
615 simIndex = dbHandle->columnInt(4);
617 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
619 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
620 dbHandle->finalizeQuery();
621 return MSG_ERR_DB_STEP;
624 dbHandle->finalizeQuery();
626 dbHandle->beginTrans();
628 memset(sqlQuery, 0x00, sizeof(sqlQuery));
629 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
631 /** Delete SMS Send Option */
632 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
633 dbHandle->endTrans(false);
634 return MSG_ERR_DB_EXEC;
637 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
638 memset(sqlQuery, 0x00, sizeof(sqlQuery));
639 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
641 /** Delete Push Message from push table */
642 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
643 dbHandle->endTrans(false);
644 return MSG_ERR_DB_EXEC;
646 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
647 memset(sqlQuery, 0x00, sizeof(sqlQuery));
648 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
650 /** Delete Push Message from push table */
651 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
652 dbHandle->endTrans(false);
653 return MSG_ERR_DB_EXEC;
655 } else if (msgType.subType == MSG_SYNCML_CP) {
656 memset(sqlQuery, 0x00, sizeof(sqlQuery));
657 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
659 /** Delete SyncML Message from syncML table */
660 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
661 dbHandle->endTrans(false);
662 return MSG_ERR_DB_EXEC;
666 /** Delete Message from msg table */
667 memset(sqlQuery, 0x00, sizeof(sqlQuery));
668 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
669 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
670 dbHandle->endTrans(false);
671 return MSG_ERR_DB_EXEC;
674 /** Delete Message from msg_report table */
675 memset(sqlQuery, 0x00, sizeof(sqlQuery));
676 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_REPORT_TABLE_NAME, msgId);
677 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
678 dbHandle->endTrans(false);
679 return MSG_ERR_DB_EXEC;
682 /** Delete Message from msg_sim table */
683 memset(sqlQuery, 0x00, sizeof(sqlQuery));
684 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, msgId);
685 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
686 dbHandle->endTrans(false);
687 return MSG_ERR_DB_EXEC;
690 /** Clear Conversation table */
691 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
692 dbHandle->endTrans(false);
693 return MSG_ERR_DB_EXEC;
696 /** Update conversation table.*/
697 if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
698 dbHandle->endTrans(false);
699 return MSG_ERR_STORAGE_ERROR;
702 dbHandle->endTrans(true);
704 if (folderId == MSG_INBOX_ID) {
705 msgType.classType = MSG_CLASS_NONE;
707 /** Set memory status in SIM */
708 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
709 MSG_DEBUG("Set Memory Status");
710 SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
714 MsgRefreshAllNotification(true, false, false);
720 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
724 msg_error_t err = MSG_SUCCESS;
726 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
727 memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
728 memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
729 memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
730 msgInfo.addressList = &addrInfo;
732 if(pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
734 MSG_DEBUG("pthread_create() error");
737 //pthread_join(thd, (void **)&err);
741 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
744 if (err == MSG_SUCCESS) {
745 MSG_DEBUG("Success to saveClass2Message.");
747 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
756 void* SmsPluginStorage::class2_thread(void *data)
760 msg_error_t err = MSG_SUCCESS;
761 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
763 err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
764 if (err == MSG_SUCCESS) {
765 MSG_DEBUG("Success to saveClass2Message.");
767 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
775 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
779 MsgDbHandler *dbHandle = getDbHandle();
781 char sqlQuery[MAX_QUERY_LEN+1];
782 msg_thread_id_t convId = 0;
783 msg_message_id_t msgId = 0;
785 dbHandle->beginTrans();
787 if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
788 MSG_DEBUG("Address Info. exists [%d]", convId);
790 /** Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
791 memset(sqlQuery, 0x00, sizeof(sqlQuery));
792 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
793 WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
794 ORDER BY DISPLAY_TIME ASC;",
795 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
797 MSG_DEBUG("Query=[%s]", sqlQuery);
798 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
799 dbHandle->endTrans(false);
800 return MSG_ERR_DB_PREPARE;
803 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
804 *pMsgId = dbHandle->columnInt(0);
806 dbHandle->finalizeQuery();
807 dbHandle->endTrans(false);
808 return MSG_ERR_DB_STEP;
811 dbHandle->finalizeQuery();
814 dbHandle->endTrans(false);
815 return MSG_ERR_DB_NORECORD;
818 memset(sqlQuery, 0x00, sizeof(sqlQuery));
820 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
822 MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
824 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
825 dbHandle->endTrans(false);
826 return MSG_ERR_DB_PREPARE;
829 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
830 *pSimId = dbHandle->columnInt(0);
832 dbHandle->finalizeQuery();
833 dbHandle->endTrans(false);
834 return MSG_ERR_DB_STEP;
837 MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
839 dbHandle->finalizeQuery();
841 dbHandle->endTrans(true);
848 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
852 msg_error_t err = MSG_SUCCESS;
854 if (pSendOptInfo->bSetting == false) {
855 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
856 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
858 // if (pSendOptInfo->bDeliverReq || pSendOptInfo->option.smsSendOptInfo.bReplyPath) {
859 // pSendOptInfo->bSetting = true;
860 MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
864 // if (pSendOptInfo->bSetting == true) {
865 MsgDbHandler *dbHandle = getDbHandle();
867 char sqlQuery[MAX_QUERY_LEN+1];
869 memset(sqlQuery, 0x00, sizeof(sqlQuery));
870 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
871 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
872 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
874 MSG_DEBUG("Query = [%s]", sqlQuery);
876 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
877 err = MSG_ERR_DB_EXEC;
887 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
889 msg_error_t err = MSG_SUCCESS;
891 MsgDbHandler *dbHandle = getDbHandle();
893 err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
895 if (err != MSG_SUCCESS) {
897 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
898 bool bAutoErase = false;
900 MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
902 MSG_DEBUG("bAutoErase: %d", bAutoErase);
904 if (bAutoErase == true) {
905 msg_message_id_t msgId;
907 /** Find the oldest message's msgId */
908 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
910 if (err != MSG_SUCCESS)
913 /** Delete the corresponding message. */
914 err = deleteSmsMessage(msgId);
927 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
929 msg_error_t err = MSG_SUCCESS;
933 MsgDbHandler *dbHandle = getDbHandle();
935 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
937 memset(sqlQuery, 0x00, sizeof(sqlQuery));
939 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
940 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
941 CbPage.pageHeader.serialNum.msgCode,CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
943 err = dbHandle->getTable(sqlQuery, &rowCnt);
944 MSG_DEBUG("rowCnt: %d", rowCnt);
946 dbHandle->freeTable();
950 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
953 msg_error_t err = MSG_SUCCESS;
955 unsigned int rowId = 0;
957 MsgDbHandler *dbHandle = getDbHandle();
959 char sqlQuery[MAX_QUERY_LEN+1];
961 err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
963 if (err != MSG_SUCCESS)
967 memset(sqlQuery, 0x00, sizeof(sqlQuery));
968 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
969 MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
970 CbPage.pageHeader.serialNum.msgCode,CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
972 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
973 return MSG_ERR_DB_EXEC;
981 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
983 msg_error_t err = MSG_SUCCESS;
985 int rowCnt = 0, index = 3;
987 MsgDbHandler *dbHandle = getDbHandle();
989 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
991 memset(sqlQuery, 0x00, sizeof(sqlQuery));
993 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
995 err = dbHandle->getTable(sqlQuery, &rowCnt);
996 MSG_DEBUG("rowCnt: %d", rowCnt);
998 if (err == MSG_ERR_DB_NORECORD) {
999 dbHandle->freeTable();
1002 else if ( err != MSG_SUCCESS) {
1003 dbHandle->freeTable();
1007 char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0,};
1008 char appId[MAX_WAPPUSH_ID_LEN + 1] = {0,};
1009 int appcode = 0, default_appcode = 0;
1011 char *_content_type = NULL, *_app_id = NULL;
1015 for (int i = 0; i < rowCnt; i++) {
1016 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
1017 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
1019 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
1020 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
1021 appcode = dbHandle->getColumnToInt(index++);
1023 //MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id);
1024 _content_type = strcasestr(pPushHeader, contentType);
1026 _app_id = strcasestr(pPushHeader, appId);
1028 default_appcode = appcode;
1031 PUSH_APPLICATION_INFO_S pInfo = {0, };
1032 pInfo.appcode = appcode;
1033 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
1034 snprintf(application_id, app_id_len, "%s", appId);
1035 snprintf(content_type, content_type_len, "%s", contentType);
1036 pushAppInfoList.push_back(pInfo);
1043 if(!found && default_appcode != SMS_WAP_APPLICATION_LBS)
1045 // perform default action.
1046 PUSH_APPLICATION_INFO_S pInfo = {0, };
1047 pInfo.appcode = default_appcode;
1048 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
1049 snprintf(application_id, app_id_len, "%s", appId);
1050 snprintf(content_type, content_type_len, "%s", contentType);
1051 pushAppInfoList.push_back(pInfo);
1054 dbHandle->freeTable();
1060 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1062 msg_error_t err = MSG_SUCCESS;
1063 if((unsigned int)index > pushAppInfoList.size() - 1)
1064 return MSG_ERR_INVALID_PARAMETER;
1066 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1068 for (; it != pushAppInfoList.end(); it++)
1071 *appcode = it->appcode;
1081 msg_error_t SmsPluginStorage::releasePushEvent()
1083 msg_error_t err = MSG_SUCCESS;
1084 std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1086 for (; it != pushAppInfoList.end(); it++)
1087 it = pushAppInfoList.erase(it);