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 <tr1/unordered_set>
24 #include <boost/filesystem.hpp>
25 #include <boost/chrono/detail/system.hpp>
28 #include "MsgUtilFile.h"
29 #include "MsgContact.h"
30 #include "MsgCppTypes.h"
31 #include "MsgGconfWrapper.h"
32 #include "MsgUtilFunction.h"
33 #include "MsgUtilStorage.h"
34 #include "MsgException.h"
37 static int msgCntLimit[MSG_COUNT_LIMIT_MAILBOX_TYPE_MAX][MSG_COUNT_LIMIT_MSG_TYPE_MAX] = {{10, 10, 0, 10, 10}, {5, 10, 0, 0, 0}, {10, 10, 0, 0, 0}, {10, 10, 0, 0, 0}, {0, 0, 10, 0, 0}};
41 #define CHECK_SIZE 1 * 1024 * 1024 * 1024
42 #define RESERVE 100 * 1024 * 1024
43 #define RESERVE_LITE 5 * 1024 * 1024
45 /*==================================================================================================
46 FUNCTION IMPLEMENTATION
47 ==================================================================================================*/
49 unsigned int MsgStoAddMessageTable(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsgInfo)
53 msg_error_t err = MSG_SUCCESS;
55 msg_message_id_t msgId = 0;
57 err = pDbHandle->getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
59 if (err != MSG_SUCCESS)
64 char* pFileData = NULL;
65 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
68 if (pMsgInfo->bTextSms == false) {
69 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
72 MSG_DEBUG("file size [%d]", fileSize);
75 char keyName[MAX_VCONFKEY_NAME_LEN];
76 memset(keyName, 0x00, sizeof(keyName));
77 snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SUBS_ID, pMsgInfo->sim_idx);
80 if (MsgSettingGetString(keyName, &imsi) != MSG_SUCCESS) {
81 MSG_INFO("MsgSettingGetString() is failed");
85 char sqlQuery[MAX_QUERY_LEN+1];
87 memset(sqlQuery, 0x00, sizeof(sqlQuery));
88 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %lu, %zu, %d, %d, %d, %d, %d, %d, %d, ?, '', '', ?, 0, %d, '%s', %d);",
89 MSGFW_MESSAGE_TABLE_NAME, msgId, pMsgInfo->threadId, pMsgInfo->folderId, pMsgInfo->storageId, pMsgInfo->msgType.mainType,
90 pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize, pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected,
91 pMsgInfo->priority, pMsgInfo->direction, 0, pMsgInfo->bBackup, pMsgInfo->sim_idx, imsi, pMsgInfo->bRestricted);
93 MSG_DEBUG("QUERY : %s", sqlQuery);
98 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
101 pDbHandle->bindText(pMsgInfo->subject, 1);
103 if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
104 pDbHandle->bindText("", 2);
106 if (pMsgInfo->bTextSms == false)
107 pDbHandle->bindText(pFileData, 2);
109 pDbHandle->bindText(pMsgInfo->msgText, 2);
112 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
113 pDbHandle->finalizeQuery();
117 pDbHandle->finalizeQuery();
123 msg_error_t MsgStoSetReadStatus(MsgDbHandler *pDbHandle, msg_message_id_t msgId, bool bRead)
125 char sqlQuery[MAX_QUERY_LEN+1];
127 memset(sqlQuery, 0x00, sizeof(sqlQuery));
128 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_STATUS = %d WHERE MSG_ID = %d;",
129 MSGFW_MESSAGE_TABLE_NAME, (int)bRead, msgId);
131 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
132 return MSG_ERR_DB_EXEC;
134 /* Get MAIN_TYPE, SUB_TYPE, STORAGE_ID */
135 memset(sqlQuery, 0x00, sizeof(sqlQuery));
136 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MAIN_TYPE, A.SUB_TYPE, B.CONV_ID \
137 FROM %s A, %s B WHERE A.MSG_ID = %d AND A.CONV_ID = B.CONV_ID;",
138 MSGFW_MESSAGE_TABLE_NAME, MSGFW_CONVERSATION_TABLE_NAME, msgId);
140 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
141 return MSG_ERR_DB_PREPARE;
143 MSG_MESSAGE_TYPE_S msgType;
144 msg_thread_id_t convId;
146 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
147 msgType.mainType = pDbHandle->columnInt(0);
148 msgType.subType = pDbHandle->columnInt(1);
149 convId = pDbHandle->columnInt(2);
151 pDbHandle->finalizeQuery();
152 return MSG_ERR_DB_STEP;
155 pDbHandle->finalizeQuery();
157 MSG_DEBUG("Main Type:[%d] SubType:[%d] ConvId:[%d]", msgType.mainType, msgType.subType, convId);
159 if (MsgStoUpdateConversation(pDbHandle, convId) != MSG_SUCCESS) {
160 MSG_DEBUG("MsgStoUpdateConversation() Error");
161 return MSG_ERR_STORAGE_ERROR;
168 msg_error_t MsgStoGetOldestMessage(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsgInfo, msg_message_id_t *pMsgId)
170 char sqlQuery[MAX_QUERY_LEN+1];
172 memset(sqlQuery, 0x00, sizeof(sqlQuery));
174 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
175 WHERE SUB_TYPE = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND PROTECTED = 0 \
176 ORDER BY DISPLAY_TIME ASC",
177 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, pMsgInfo->folderId, MSG_STORAGE_PHONE);
179 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
180 return MSG_ERR_DB_PREPARE;
182 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
183 *pMsgId = pDbHandle->columnInt(0);
185 pDbHandle->finalizeQuery();
186 return MSG_ERR_DB_STEP;
189 pDbHandle->finalizeQuery();
195 msg_error_t MsgStoCheckMsgCntFull(MsgDbHandler *pDbHandle, const MSG_MESSAGE_TYPE_S* pMsgType, msg_folder_id_t folderId)
199 msg_error_t err = MSG_SUCCESS;
201 boost::system::error_code ec;
202 boost::filesystem::space_info si = boost::filesystem::space(TZ_SYS_HOME_PATH, ec);
203 long long int available = 0;
206 MSG_ERR("Failed to get space info [%s]", ec.message().c_str());
207 return MSG_ERR_STORAGE_ERROR;
209 if (si.capacity < CHECK_SIZE)
210 available = si.available - RESERVE_LITE;
212 available = si.available - RESERVE;
217 MSG_DEBUG("Free space of storage is [%llu] Bytes.", available);
219 if (available < SMS_MINIMUM_SPACE && pMsgType->mainType == MSG_SMS_TYPE)
220 err = MSG_ERR_MESSAGE_COUNT_FULL;
221 else if (available < MMS_MINIMUM_SPACE && pMsgType->mainType == MSG_MMS_TYPE)
222 err = MSG_ERR_MESSAGE_COUNT_FULL;
231 msg_error_t MsgStoCountMsgByLimitCategory(MsgDbHandler *pDbHandle, const MSG_MESSAGE_TYPE_S *pMsgType, int *pMsgCount, msg_folder_id_t folderId)
233 if (pMsgType == NULL) {
234 MSG_DEBUG("pMsgType is NULL");
235 return MSG_ERR_NULL_POINTER;
240 char sqlQuery[MAX_QUERY_LEN+1];
241 memset(sqlQuery, 0x00, sizeof(sqlQuery));
243 if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_WAP_SI_SMS ||pMsgType->subType == MSG_WAP_SL_SMS)) { /* PUSH */
244 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d) AND FOLDER_ID = %d;",
245 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_INBOX_ID);
246 } else if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_CB_SMS)) { /* CB */
247 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d) AND FOLDER_ID = %d;",
248 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_CB_SMS, MSG_CBMSGBOX_ID);
249 } else if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_SYNCML_CP)) { /* Provision */
250 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d) AND FOLDER_ID = %d;",
251 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_SYNCML_CP, MSG_INBOX_ID);
252 } else if ((pMsgType->mainType == MSG_SMS_TYPE)) { /* SMS */
253 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d, %d) AND FOLDER_ID = %d;",
254 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_CB_SMS, MSG_SYNCML_CP, MSG_INBOX_ID); /* etc SMS */
255 } else if ((pMsgType->mainType == MSG_MMS_TYPE) &&
256 (pMsgType->subType == MSG_SENDREQ_MMS || pMsgType->subType == MSG_SENDCONF_MMS || pMsgType->subType == MSG_RETRIEVE_AUTOCONF_MMS ||
257 pMsgType->subType == MSG_RETRIEVE_MANUALCONF_MMS || pMsgType->subType == MSG_NOTIFICATIONIND_MMS)) { /* MMS */
258 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d, %d, %d) AND FOLDER_ID = %d;",
259 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_SENDREQ_MMS, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS, MSG_NOTIFICATIONIND_MMS, folderId);
261 return MSG_ERR_INVALID_PARAMETER;
264 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
265 return MSG_ERR_DB_PREPARE;
267 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
268 *pMsgCount = pDbHandle->columnInt(0);
270 pDbHandle->finalizeQuery();
271 return MSG_ERR_DB_STEP;
274 pDbHandle->finalizeQuery();
280 int MsgStoCheckMsgCntLimit(const MSG_MESSAGE_TYPE_S* pMsgType, msg_folder_id_t folderId)
287 msgboxType = MSG_COUNT_LIMIT_INBOX_TYPE;
291 msgboxType = MSG_COUNT_LIMIT_OUTBOX_TYPE;
294 case MSG_SENTBOX_ID :
295 msgboxType = MSG_COUNT_LIMIT_SENTBOX_TYPE;
299 msgboxType = MSG_COUNT_LIMIT_DRAFTBOX_TYPE;
302 case MSG_CBMSGBOX_ID :
303 msgboxType = MSG_COUNT_LIMIT_CBMSGBOX_TYPE;
307 MSG_DEBUG("Unknown mailbox Type [%d]", folderId);
311 switch (pMsgType->subType) {
313 case MSG_REPLACE_TYPE1_SMS:
314 case MSG_REPLACE_TYPE2_SMS:
315 case MSG_REPLACE_TYPE3_SMS:
316 case MSG_REPLACE_TYPE4_SMS:
317 case MSG_REPLACE_TYPE5_SMS:
318 case MSG_REPLACE_TYPE6_SMS:
319 case MSG_REPLACE_TYPE7_SMS:
320 case MSG_MWI_VOICE_SMS:
321 case MSG_MWI_FAX_SMS:
322 case MSG_MWI_EMAIL_SMS:
323 case MSG_MWI_OTHER_SMS:
324 case MSG_STATUS_REPORT_SMS:
325 msgType = MSG_COUNT_LIMIT_SMS_TYPE;
329 msgType = MSG_COUNT_LIMIT_CB_TYPE;
334 msgType = MSG_COUNT_LIMIT_WAPPUSH_TYPE;
338 msgType = MSG_COUNT_LIMIT_PROVISION_TYPE;
341 case MSG_SENDREQ_MMS:
342 case MSG_SENDCONF_MMS:
343 case MSG_NOTIFICATIONIND_MMS:
344 case MSG_RETRIEVE_AUTOCONF_MMS:
345 case MSG_RETRIEVE_MANUALCONF_MMS:
346 msgType = MSG_COUNT_LIMIT_MMS_TYPE;
350 MSG_DEBUG("Unknown Message Type [%d]", pMsgType->subType);
354 return msgCntLimit[msgboxType][msgType];
358 msg_error_t MsgStocheckMemoryStatus()
360 msg_error_t err = MSG_SUCCESS;
361 boost::system::error_code ec;
362 boost::filesystem::space_info si = boost::filesystem::space(TZ_SYS_HOME_PATH, ec);
363 long long int available = 0;
366 MSG_ERR("Failed to get space info [%s]", ec.message().c_str());
367 return MSG_ERR_STORAGE_ERROR;
370 if (si.capacity < CHECK_SIZE)
371 available = si.available - RESERVE_LITE;
373 available = si.available - RESERVE;
378 MSG_DEBUG("Free space of storage is [%llu] Bytes.", available);
380 if (available < SMS_MINIMUM_SPACE)
381 err = MSG_ERR_MESSAGE_COUNT_FULL;
383 MSG_DEBUG("Memory status =[%d]", err);
389 msg_error_t MsgStoAddAddress(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsg, msg_thread_id_t *pConvId)
391 msg_error_t err = MSG_SUCCESS;
393 char sqlQuery[MAX_QUERY_LEN+1];
395 /* Check if new address or not */
396 if (MsgExistAddress(pDbHandle, pMsg, pConvId) == true) {
397 MSG_DEBUG("The address already exists. Conversation ID : [%d]", *pConvId);
398 MsgStoUpdateAddress(pDbHandle, pMsg, *pConvId);
403 *pConvId = pMsg->threadId;
405 /* conversation insert */
406 err = MsgStoAddConversation(pDbHandle, pConvId);
407 if (err != MSG_SUCCESS) {
408 MSG_DEBUG("MsgStoAddConversation() fail [%d]", err);
412 /* insert address in loop */
413 for (int i = 0; i < pMsg->nAddressCnt; i++) {
415 MSG_CONTACT_INFO_S contactInfo;
416 memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
418 /* Get Contact Info */
420 if (MsgGetContactInfo(&(pMsg->addressList[i]), &contactInfo) != MSG_SUCCESS) {
421 MSG_DEBUG("MsgGetContactInfo() fail.");
425 err = pDbHandle->getRowId(MSGFW_ADDRESS_TABLE_NAME, &addrId);
426 if (err != MSG_SUCCESS) {
427 MSG_DEBUG("pDbHandle->getRowId fail. [%d]", err);
432 memset(sqlQuery, 0x00, sizeof(sqlQuery));
433 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, '%s', %d, %d, ?, ?, ?, ?, ?, '%s', 0);",
434 MSGFW_ADDRESS_TABLE_NAME, addrId, *pConvId, pMsg->addressList[i].addressType, pMsg->addressList[i].recipientType, pMsg->addressList[i].addressVal,
435 contactInfo.contactId, contactInfo.addrbookId, contactInfo.imagePath);
437 MSG_SEC_DEBUG("Add Address Info. [%s]", sqlQuery);
439 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
440 return MSG_ERR_DB_PREPARE;
442 pDbHandle->bindText(contactInfo.firstName, 1);
443 pDbHandle->bindText(contactInfo.lastName, 2);
444 pDbHandle->bindText(contactInfo.middleName, 3);
445 pDbHandle->bindText(contactInfo.prefix, 4);
446 pDbHandle->bindText(contactInfo.suffix, 5);
448 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
449 pDbHandle->finalizeQuery();
450 return MSG_ERR_DB_STEP;
453 pDbHandle->finalizeQuery();
457 /* set conversation display name by conv id */
458 MsgStoSetConversationDisplayName(pDbHandle, *pConvId);
463 msg_error_t MsgStoGetAddressByMsgId(MsgDbHandler *pDbHandle, msg_message_id_t msgId, int *nAddressCnt, MSG_ADDRESS_INFO_S **pAddress)
465 char sqlQuery[MAX_QUERY_LEN+1];
466 int rowCnt = 0, index = 0;
470 memset(sqlQuery, 0x00, sizeof(sqlQuery));
471 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT "
476 "WHERE A.CONV_ID = B.CONV_ID "
477 "AND B.MSG_ID = %d;",
478 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
481 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
483 if (err == MSG_ERR_DB_NORECORD) {
484 pDbHandle->freeTable();
486 } else if (err != MSG_SUCCESS) {
487 MSG_DEBUG("Fail to getTable().");
488 pDbHandle->freeTable();
492 *nAddressCnt = rowCnt;
494 MSG_DEBUG("*nAddressCnt [%d]", *nAddressCnt);
496 MSG_ADDRESS_INFO_S *tmpAddressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * rowCnt];
497 memset(tmpAddressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * rowCnt);
498 *pAddress = tmpAddressList;
500 for (int i = 0; i < rowCnt; i++) {
501 tmpAddressList[i].addressType = pDbHandle->getColumnToInt(index++);
502 tmpAddressList[i].recipientType = pDbHandle->getColumnToInt(index++);
503 pDbHandle->getColumnToString(index++, MAX_ADDRESS_VAL_LEN, tmpAddressList[i].addressVal);
505 strncpy(tmpAddressList[i].displayName, tmpAddressList[i].addressVal, MAX_DISPLAY_NAME_LEN);
507 pDbHandle->freeTable();
512 msg_error_t MsgStoGetAddressByMsgId(MsgDbHandler *pDbHandle, msg_message_id_t msgId, msg_struct_list_s *pAddress)
514 char sqlQuery[MAX_QUERY_LEN+1];
516 int rowCnt = 0, index = 0;
518 pAddress->nCount = 0;
519 pAddress->msg_struct_info = NULL;
521 msg_struct_s *pTmp = NULL;
522 MSG_ADDRESS_INFO_S *pAddr = NULL;
524 pAddress->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(msg_struct_t));
525 if (pAddress->msg_struct_info == NULL)
526 return MSG_ERR_MEMORY_ERROR;
528 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
529 pAddress->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
530 pTmp = (msg_struct_s *)pAddress->msg_struct_info[i];
531 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
532 pTmp->data = new MSG_ADDRESS_INFO_S;
533 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
536 memset(sqlQuery, 0x00, sizeof(sqlQuery));
537 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT "
542 "WHERE A.CONV_ID = B.CONV_ID "
543 "AND B.MSG_ID = %d;",
544 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
547 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
549 if (err == MSG_ERR_DB_NORECORD) {
550 pDbHandle->freeTable();
552 } else if (err != MSG_SUCCESS) {
553 MSG_DEBUG("Fail to getTable().");
554 pDbHandle->freeTable();
558 rowCnt = (rowCnt > 10)? MAX_TO_ADDRESS_CNT: rowCnt;
559 pAddress->nCount = rowCnt;
561 for (int i = 0; i < rowCnt; i++) {
562 pTmp = (msg_struct_s *)pAddress->msg_struct_info[i];
563 pAddr = (MSG_ADDRESS_INFO_S *)pTmp->data;
565 pAddr->addressType = pDbHandle->getColumnToInt(index++);
566 pAddr->recipientType = pDbHandle->getColumnToInt(index++);
568 pDbHandle->getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pAddr->addressVal);
570 strncpy(pAddr->displayName, pAddr->addressVal, MAX_DISPLAY_NAME_LEN);
573 pDbHandle->freeTable();
579 msg_error_t MsgStoGetAddressByConvId(MsgDbHandler *pDbHandle, msg_thread_id_t convId, msg_struct_list_s *pAddrlist)
581 char sqlQuery[MAX_QUERY_LEN+1];
583 int rowCnt = 0, index = 0;
585 pAddrlist->nCount = 0;
586 pAddrlist->msg_struct_info = NULL;
588 memset(sqlQuery, 0x00, sizeof(sqlQuery));
589 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT "
593 "FROM %s WHERE CONV_ID = %d;",
594 MSGFW_ADDRESS_TABLE_NAME, convId);
596 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
598 if (err == MSG_ERR_DB_NORECORD) {
599 pDbHandle->freeTable();
601 } else if (err != MSG_SUCCESS) {
602 MSG_DEBUG("Fail to getTable().");
603 pDbHandle->freeTable();
607 pAddrlist->nCount = rowCnt;
609 MSG_DEBUG("pAddrlist->nCount [%d]", pAddrlist->nCount);
611 msg_struct_s *pTmp = NULL;
612 MSG_ADDRESS_INFO_S *pAddr = NULL;
614 pAddrlist->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(msg_struct_t));
616 for (int i = 0; i < rowCnt; i++) {
617 pAddrlist->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
618 pTmp = (msg_struct_s *)pAddrlist->msg_struct_info[i];
619 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
620 pTmp->data = new MSG_ADDRESS_INFO_S;
621 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
624 /* rowCnt = (rowCnt > 10)? MAX_TO_ADDRESS_CNT: rowCnt; */
626 for (int i = 0; i < rowCnt; i++) {
627 pTmp = (msg_struct_s *)pAddrlist->msg_struct_info[i];
628 pAddr = (MSG_ADDRESS_INFO_S *)pTmp->data;
630 pAddr->addressType = pDbHandle->getColumnToInt(index++);
631 pAddr->recipientType = pDbHandle->getColumnToInt(index++);
632 pDbHandle->getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pAddr->addressVal);
634 strncpy(pAddr->displayName, pAddr->addressVal, MAX_DISPLAY_NAME_LEN);
636 pDbHandle->freeTable();
641 /* Have to use trigger for this function. */
642 msg_error_t MsgStoUpdateConversation(MsgDbHandler *pDbHandle, msg_thread_id_t convId)
646 char sqlQuery[MAX_QUERY_LEN];
647 unsigned int tmpSize = 0;
649 #ifdef MSG_NOTI_INTEGRATION
650 memset(sqlQuery, 0x00, MAX_QUERY_LEN);
651 snprintf(sqlQuery, sizeof(sqlQuery),
654 "WHERE CONV_ID = %d "
655 "AND FOLDER_ID > %d AND FOLDER_ID < %d "
656 "AND STORAGE_ID = %d;",
657 MSGFW_MESSAGE_TABLE_NAME,
659 MSG_ALLBOX_ID, MSG_SPAMBOX_ID,
662 memset(sqlQuery, 0x00, MAX_QUERY_LEN);
663 snprintf(sqlQuery, sizeof(sqlQuery),
666 "WHERE CONV_ID = %d "
667 "AND FOLDER_ID > %d AND FOLDER_ID < %d "
668 "AND STORAGE_ID = %d;",
669 MSGFW_MESSAGE_TABLE_NAME,
671 MSG_ALLBOX_ID, MSG_CBMSGBOX_ID,
675 msg_error_t err = pDbHandle->prepareQuery(sqlQuery);
676 if (err != MSG_SUCCESS) {
677 MSG_DEBUG("Fail to prepareQuery().");
678 pDbHandle->finalizeQuery();
682 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
683 pDbHandle->finalizeQuery();
685 memset(sqlQuery, 0x00, MAX_QUERY_LEN);
686 snprintf(sqlQuery, sizeof(sqlQuery),
687 "SELECT MAIN_TYPE, SUB_TYPE, MSG_DIRECTION, MSG_ID, DISPLAY_TIME, LENGTH(SUBJECT), SUBJECT, MSG_TEXT, DPM_RESTRICTED "
689 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC;",
690 MSGFW_MESSAGE_TABLE_NAME,
691 convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
693 err = pDbHandle->prepareQuery(sqlQuery);
694 if (err != MSG_SUCCESS) {
695 MSG_DEBUG("Fail to prepareQuery().");
699 err = pDbHandle->stepQuery();
700 if (err != MSG_ERR_DB_ROW) {
701 MSG_DEBUG("Fail to stepQuery().");
702 pDbHandle->finalizeQuery();
706 int main_type = pDbHandle->columnInt(0);
707 int sub_type = pDbHandle->columnInt(1);
708 int msg_direction = pDbHandle->columnInt(2);
709 int last_msg_id = pDbHandle->columnInt(3);
710 time_t disp_time = (time_t)pDbHandle->columnInt(4);
711 int subject_length = pDbHandle->columnInt(5);
712 bool dpm_restricted = pDbHandle->columnInt(8);
713 char subject[MAX_SUBJECT_LEN+1] = {0, };
714 char msg_text[MAX_MSG_TEXT_LEN+1] = {0, };
715 if (!dpm_restricted) {
716 snprintf(subject, sizeof(subject), "%s", pDbHandle->columnText(6));
717 snprintf(msg_text, sizeof(msg_text), "%s", pDbHandle->columnText(7));
719 snprintf(subject, sizeof(subject), "restricted message");
720 snprintf(msg_text, sizeof(msg_text), "restricted message");
723 pDbHandle->finalizeQuery();
724 memset(sqlQuery, 0x00, MAX_QUERY_LEN);
725 snprintf(sqlQuery, sizeof(sqlQuery),
727 MSGFW_CONVERSATION_TABLE_NAME);
729 tmpSize = strlen(sqlQuery);
730 #ifdef MSG_NOTI_INTEGRATION
731 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
732 "UNREAD_CNT = (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND (FOLDER_ID = %d OR FOLDER_ID = %d) AND STORAGE_ID = %d AND READ_STATUS = 0), ",
733 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_INBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE);
735 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
736 "UNREAD_CNT = (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND READ_STATUS = 0), ",
737 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_INBOX_ID, MSG_STORAGE_PHONE);
740 tmpSize = strlen(sqlQuery);
741 #ifdef MSG_NOTI_INTEGRATION
742 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
743 "SMS_CNT = (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND MAIN_TYPE = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d), ",
744 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_SMS_TYPE, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
746 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
747 "SMS_CNT = (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND MAIN_TYPE = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d), ",
748 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_SMS_TYPE, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE);
751 tmpSize = strlen(sqlQuery);
752 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
753 "MMS_CNT = (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d) AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d), ",
754 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE);
757 tmpSize = strlen(sqlQuery);
758 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
759 "MAIN_TYPE = (SELECT MAIN_TYPE FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME DESC), ",
760 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
762 tmpSize = strlen(sqlQuery);
763 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
764 "SUB_TYPE = (SELECT SUB_TYPE FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME DESC), ",
765 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
767 tmpSize = strlen(sqlQuery);
768 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
769 "MSG_DIRECTION = (SELECT MSG_DIRECTION FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME DESC), ",
770 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
772 tmpSize = strlen(sqlQuery);
774 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
775 "DISPLAY_TIME = CASE "
776 "WHEN (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) > 0 "
777 "THEN (SELECT DISPLAY_TIME FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) "
780 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
781 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
783 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
784 "DISPLAY_TIME = CASE "
785 "WHEN (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND READ_STATUS = 0) > 0 "
786 "THEN (SELECT DISPLAY_TIME FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC) "
787 "WHEN (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d) > 0 "
788 "THEN (SELECT DISPLAY_TIME FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME DESC) "
789 "WHEN (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND NETWORK_STATUS = %d) > 0 "
790 "THEN (SELECT DISPLAY_TIME FROM %s WHERE CONV_ID = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND NETWORK_STATUS = %d ORDER BY DISPLAY_TIME DESC) "
791 "ELSE (SELECT DISPLAY_TIME FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME DESC) "
793 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_INBOX_ID, MSG_STORAGE_PHONE,
794 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_INBOX_ID, MSG_STORAGE_PHONE,
795 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_DRAFT_ID, MSG_STORAGE_PHONE,
796 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_DRAFT_ID, MSG_STORAGE_PHONE,
797 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_OUTBOX_ID, MSG_STORAGE_PHONE, MSG_NETWORK_SEND_FAIL,
798 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_OUTBOX_ID, MSG_STORAGE_PHONE, MSG_NETWORK_SEND_FAIL,
799 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
803 tmpSize = strlen(sqlQuery);
804 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
806 "WHEN %d > 0 THEN ? ELSE ? "
810 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
812 "WHEN (SELECT COUNT(MSG_ID) FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) > 0 "
814 "WHEN (SELECT LENGTH(SUBJECT) FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) > 0 "
815 "THEN (SELECT SUBJECT FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) "
816 "ELSE (SELECT MSG_TEXT FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND SCHEDULED_TIME = 0 ORDER BY DISPLAY_TIME DESC) "
819 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
820 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
821 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
822 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
824 tmpSize = strlen(sqlQuery);
825 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
826 "MAIN_TYPE = %d, SUB_TYPE = %d, MSG_DIRECTION = %d, DISPLAY_TIME = %lu, LAST_MSG_ID = %d ",
827 main_type, sub_type, msg_direction, disp_time, last_msg_id);
829 tmpSize = strlen(sqlQuery);
830 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
831 "WHERE CONV_ID = %d;",
833 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
834 MSG_DEBUG("Query Failed [%s]", sqlQuery);
835 return MSG_ERR_DB_PREPARE;
838 pDbHandle->bindText(subject, 1);
839 pDbHandle->bindText(msg_text, 2);
841 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
842 MSG_DEBUG("stepQuery() Failed");
843 pDbHandle->finalizeQuery();
844 return MSG_ERR_DB_STEP;
847 pDbHandle->finalizeQuery();
849 pDbHandle->finalizeQuery();
851 memset(sqlQuery, 0x00, MAX_QUERY_LEN);
852 snprintf(sqlQuery, sizeof(sqlQuery),
853 "UPDATE %s SET UNREAD_CNT = 0, SMS_CNT = 0, MMS_CNT = 0, MAIN_TYPE = 0, SUB_TYPE = 0, MSG_DIRECTION = 0, DISPLAY_TIME = 0, MSG_TEXT = '' "
854 "WHERE CONV_ID = %d;",
855 MSGFW_CONVERSATION_TABLE_NAME, convId);
857 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
858 MSG_DEBUG("Query Failed [%s]", sqlQuery);
859 return MSG_ERR_DB_EXEC;
869 /* consider to replcae this function to trigger. */
870 msg_error_t MsgStoClearConversationTable(MsgDbHandler *pDbHandle)
872 msg_error_t err = MSG_SUCCESS;
874 char sqlQuery[MAX_QUERY_LEN+1];
876 memset(sqlQuery, 0x00, sizeof(sqlQuery));
878 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s "
879 "WHERE CONV_ID NOT IN (SELECT CONV_ID FROM %s) AND CONV_ID <> 0;",
880 MSGFW_CONVERSATION_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
882 err = pDbHandle->execQuery(sqlQuery);
884 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE CONV_ID NOT IN (SELECT CONV_ID FROM %s);",
885 MSGFW_ADDRESS_TABLE_NAME, MSGFW_CONVERSATION_TABLE_NAME);
887 err = pDbHandle->execQuery(sqlQuery);
893 msg_thread_id_t MsgGetThreadId(MsgDbHandler *pDbHandle, msg_message_id_t msgId)
895 msg_thread_id_t conv_id = 0;
897 char sqlQuery[MAX_QUERY_LEN+1];
898 memset(sqlQuery, 0x00, sizeof(sqlQuery));
899 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONV_ID FROM %s WHERE MSG_ID = %d;",
900 MSGFW_MESSAGE_TABLE_NAME, msgId);
902 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
905 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
906 conv_id = pDbHandle->columnInt(0);
909 pDbHandle->finalizeQuery();
914 /* Change the function name to conversation related. */
915 bool MsgExistAddress(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsg, msg_thread_id_t *pConvId)
917 msg_error_t err = MSG_SUCCESS;
919 char sqlQuery[MAX_QUERY_LEN+1];
920 unsigned int tmpSize = 0;
924 memset(sqlQuery, 0x00, sizeof(sqlQuery));
925 snprintf(sqlQuery, sizeof(sqlQuery),
926 "SELECT CONV_ID FROM ( SELECT CONV_ID FROM %s WHERE ( ",
927 MSGFW_ADDRESS_TABLE_NAME);
929 for (int i = 0; i < pMsg->nAddressCnt; i++) {
930 if (strlen(pMsg->addressList[i].addressVal) >= (unsigned int)MsgContactGetMinMatchDigit()
931 && pMsg->addressList[i].addressType != MSG_ADDRESS_TYPE_EMAIL
932 && MsgIsNumber(pMsg->addressList[i].addressVal)) {
933 int addrSize = strlen(pMsg->addressList[i].addressVal);
934 char newPhoneNum[addrSize+1];
935 memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
936 MsgConvertNumber(pMsg->addressList[i].addressVal, newPhoneNum, addrSize);
938 tmpSize = strlen(sqlQuery);
939 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
940 "ADDRESS_VAL LIKE '%%%%%s' ",
943 if ((pMsg->nAddressCnt-1) == i) break;
945 tmpSize = strlen(sqlQuery);
946 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize, " OR ");
949 tmpSize = strlen(sqlQuery);
950 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
951 "ADDRESS_VAL LIKE '%s' ",
952 pMsg->addressList[i].addressVal);
954 if ((pMsg->nAddressCnt-1) == i) break;
956 tmpSize = strlen(sqlQuery);
957 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize, " OR ");
961 tmpSize = strlen(sqlQuery);
962 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
963 ") AND CONV_ID IN (SELECT CONV_ID FROM %s GROUP BY CONV_ID HAVING COUNT(CONV_ID)=%d) ",
964 MSGFW_ADDRESS_TABLE_NAME, pMsg->nAddressCnt);
967 tmpSize = strlen(sqlQuery);
968 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
969 ") GROUP BY CONV_ID HAVING COUNT(CONV_ID)=%d;",
975 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
977 /* No record or other error */
978 if (err != MSG_SUCCESS) {
979 MSG_DEBUG("Fail to getTable().");
980 pDbHandle->freeTable();
984 convId = pDbHandle->getColumnToInt(1);
987 MSG_DEBUG("Success to get convId [%d]", convId);
989 pDbHandle->freeTable();
993 pDbHandle->freeTable();
999 int MsgStoGetUnreadCnt(MsgDbHandler *pDbHandle, MSG_MAIN_TYPE_T msgType)
1003 char sqlQuery[MAX_QUERY_LEN+1];
1005 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1007 if (msgType == MSG_SMS_TYPE) {
1008 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s "
1009 "WHERE MAIN_TYPE = %d "
1010 "AND (SUB_TYPE IN (%d, %d, %d, %d, %d, %d, %d) OR (SUB_TYPE >= %d AND SUB_TYPE <= %d)) "
1011 "AND FOLDER_ID = %d AND READ_STATUS = 0 AND STORAGE_ID = %d;",
1012 MSGFW_MESSAGE_TABLE_NAME,
1014 MSG_NORMAL_SMS, MSG_STATUS_REPORT_SMS, MSG_CONCAT_SIM_SMS, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_MWI_VOICE_SMS, MSG_SYNCML_CP,
1015 MSG_REPLACE_TYPE1_SMS, MSG_REPLACE_TYPE7_SMS,
1016 MSG_INBOX_ID, MSG_STORAGE_PHONE);
1017 } else if (msgType == MSG_MMS_TYPE) {
1018 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s "
1019 "WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d) "
1020 "AND FOLDER_ID = %d AND READ_STATUS = 0 AND STORAGE_ID = %d;",
1021 MSGFW_MESSAGE_TABLE_NAME,
1023 MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS, MSG_NOTIFICATIONIND_MMS,
1024 MSG_INBOX_ID, MSG_STORAGE_PHONE);
1027 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
1030 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
1031 msgCnt = pDbHandle->columnInt(0);
1033 pDbHandle->finalizeQuery();
1037 pDbHandle->finalizeQuery();
1043 msg_error_t MsgStoGetMmsRawFilePath(MsgDbHandler *pDbHandle, msg_message_id_t msgId, char *pFilePath)
1045 char sqlQuery[MAX_QUERY_LEN+1];
1047 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1049 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.FILE_PATH FROM %s A, %s B \
1050 WHERE A.MSG_ID = B.MSG_ID AND B.MSG_ID = %d;",
1051 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
1053 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
1054 return MSG_ERR_DB_PREPARE;
1056 if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
1057 if (pDbHandle->columnText(0) != NULL)
1058 strncpy(pFilePath, (char*)pDbHandle->columnText(0), MSG_FILEPATH_LEN_MAX);
1060 pDbHandle->finalizeQuery();
1061 return MSG_ERR_DB_STEP;
1064 pDbHandle->finalizeQuery();
1070 bool MsgStoCheckReadReportRequested(MsgDbHandler *pDbHandle, msg_message_id_t msgId)
1072 msg_error_t err = MSG_SUCCESS;
1074 char sqlQuery[MAX_QUERY_LEN+1];
1076 bool bReadReportRequested = false;
1078 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1080 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ASK_READ_REPLY FROM %s A, %s B \
1081 WHERE A.MSG_ID = B.MSG_ID AND B.MSG_ID = %d;",
1082 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
1084 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1086 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1087 pDbHandle->freeTable();
1088 MSG_DEBUG("[Error]Failed to Get Table");
1089 return bReadReportRequested;
1093 pDbHandle->freeTable();
1094 MSG_DEBUG("[Error]MSG_ERR_DB_NORECORD");
1095 return bReadReportRequested;
1098 bReadReportRequested = pDbHandle->getColumnToInt(1);
1100 pDbHandle->freeTable();
1102 return bReadReportRequested;
1106 bool MsgStoCheckReadReportIsSent(MsgDbHandler *pDbHandle, msg_message_id_t msgId)
1108 msg_error_t err = MSG_SUCCESS;
1110 char sqlQuery[MAX_QUERY_LEN+1];
1113 bool bReadReportIsSent = true;
1115 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1117 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.READ_REPORT_SENT FROM %s A, %s B \
1118 WHERE A.MSG_ID = B.MSG_ID AND B.MSG_ID = %d;",
1119 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
1121 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1123 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1124 pDbHandle->freeTable();
1125 MSG_DEBUG("[Error]Failed to Get Table");
1126 return bReadReportIsSent;
1130 pDbHandle->freeTable();
1131 MSG_DEBUG("[Error]MSG_ERR_DB_NORECORD");
1132 return bReadReportIsSent;
1135 bReadReportIsSent = (bool)pDbHandle->getColumnToInt(1);
1137 pDbHandle->freeTable();
1139 return bReadReportIsSent;
1143 msg_error_t MsgStoAddConversation(MsgDbHandler *pDbHandle, msg_thread_id_t *pConvId)
1145 char sqlQuery[MAX_QUERY_LEN+1];
1148 if (pDbHandle->getRowId(MSGFW_CONVERSATION_TABLE_NAME, pConvId) != MSG_SUCCESS) {
1149 return MSG_ERR_DB_EXEC;
1152 /* Add Conversation */
1153 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1154 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 0, 0, 0, 0, 0, 0, 0, '', '', 0);",
1155 MSGFW_CONVERSATION_TABLE_NAME, *pConvId);
1157 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
1158 MSG_DEBUG("Query Failed. [%s]", sqlQuery);
1159 return MSG_ERR_DB_EXEC;
1166 msg_error_t MsgStoSetConversationDisplayName(MsgDbHandler *pDbHandle, int contactId)
1168 msg_error_t err = MSG_SUCCESS;
1170 char displayName[MAX_DISPLAY_NAME_LEN+1];
1171 char sqlQuery[MAX_QUERY_LEN+1];
1173 MSG_DEBUG("contactId [%d]", contactId);
1175 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1176 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(CONV_ID) FROM %s WHERE CONTACT_ID = %d;",
1177 MSGFW_ADDRESS_TABLE_NAME, contactId);
1179 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1181 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1182 pDbHandle->freeTable();
1183 MSG_DEBUG("Fail to getTable().");
1187 msg_struct_s *pAddrInfo = NULL;
1188 MSG_ADDRESS_INFO_S *address = NULL;
1190 for (int i = 1; i <= rowCnt; i++) {
1191 memset(displayName, 0x00, sizeof(displayName));
1192 MsgDbHandler tmpDbHandle;
1193 msg_struct_list_s addressList = {0, };
1194 MsgStoGetAddressByConvId(&tmpDbHandle, (msg_thread_id_t)pDbHandle->getColumnToInt(i), &addressList);
1196 for (int j = 0; j < addressList.nCount; j++) {
1198 strncat(displayName, ", ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1200 pAddrInfo = (msg_struct_s *)addressList.msg_struct_info[j];
1201 address = (MSG_ADDRESS_INFO_S *)pAddrInfo->data;
1203 if (address->displayName[0] == '\0')
1204 strncat(displayName, address->addressVal, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1206 strncat(displayName, address->displayName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1209 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1210 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET DISPLAY_NAME = ? WHERE CONV_ID = %d;",
1211 MSGFW_CONVERSATION_TABLE_NAME, pDbHandle->getColumnToInt(i));
1213 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1214 pDbHandle->freeTable();
1215 MSG_DEBUG("Query Failed [%s]", sqlQuery);
1216 return MSG_ERR_DB_PREPARE;
1219 pDbHandle->bindText(displayName, 1);
1221 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1222 pDbHandle->freeTable();
1223 pDbHandle->finalizeQuery();
1224 MSG_SEC_DEBUG("Update Conversation disply name. Fail [%s]", sqlQuery);
1225 return MSG_ERR_DB_STEP;
1228 pDbHandle->finalizeQuery();
1230 /* free address list */
1231 for (int j = 0; j < addressList.nCount; j++) {
1232 msg_struct_s *pStruct = (msg_struct_s *)addressList.msg_struct_info[j];
1233 delete (MSG_ADDRESS_INFO_S *)pStruct->data;
1234 delete (msg_struct_s *)pStruct;
1237 if (addressList.msg_struct_info != NULL) {
1238 g_free((msg_struct_t *)addressList.msg_struct_info);
1242 pDbHandle->freeTable();
1248 msg_error_t MsgStoSetConversationDisplayName(MsgDbHandler *pDbHandle, msg_thread_id_t convId)
1250 msg_error_t err = MSG_SUCCESS;
1252 char displayName[MAX_DISPLAY_NAME_LEN+1];
1253 char sqlQuery[MAX_QUERY_LEN+1];
1255 msg_struct_list_s addressList = {0, };
1257 msg_struct_s *pAddrInfo = NULL;
1258 MSG_ADDRESS_INFO_S *address = NULL;
1260 memset(displayName, 0x00, sizeof(displayName));
1262 MsgStoGetAddressByConvId(pDbHandle, convId, &addressList);
1264 for (int j = 0; j < addressList.nCount; j++) {
1266 strncat(displayName, ", ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1268 pAddrInfo = (msg_struct_s *)addressList.msg_struct_info[j];
1269 address = (MSG_ADDRESS_INFO_S *)pAddrInfo->data;
1271 if (address->displayName[0] == '\0')
1272 strncat(displayName, address->addressVal, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1274 strncat(displayName, address->displayName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1277 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1278 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET DISPLAY_NAME = ? WHERE CONV_ID = %d;",
1279 MSGFW_CONVERSATION_TABLE_NAME, convId);
1281 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1282 MSG_DEBUG("Query Failed [%s]", sqlQuery);
1283 return MSG_ERR_DB_PREPARE;
1286 pDbHandle->bindText(displayName, 1);
1288 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1289 pDbHandle->finalizeQuery();
1290 MSG_SEC_DEBUG("Update Conversation disply name. Fail [%s]", sqlQuery);
1291 return MSG_ERR_DB_STEP;
1294 pDbHandle->finalizeQuery();
1296 for (int j = 0; j < addressList.nCount; j++) {
1297 msg_struct_s *pStruct = (msg_struct_s *)addressList.msg_struct_info[j];
1298 delete (MSG_ADDRESS_INFO_S *)pStruct->data;
1299 delete (msg_struct_s *)pStruct;
1302 if (addressList.msg_struct_info != NULL) {
1303 g_free((msg_struct_t *)addressList.msg_struct_info);
1309 msg_error_t MsgStoUpdateNetworkStatus(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
1311 msg_error_t err = MSG_SUCCESS;
1313 char sqlQuery[MAX_QUERY_LEN+1];
1315 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1317 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
1318 MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
1320 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
1321 err = MSG_ERR_DB_EXEC;
1323 pDbHandle->finalizeQuery();
1328 bool MsgExistConversation(MsgDbHandler *pDbHandle, msg_thread_id_t convId)
1330 msg_error_t err = MSG_SUCCESS;
1332 char sqlQuery[MAX_QUERY_LEN+1];
1336 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1338 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONV_ID FROM %s WHERE CONV_ID = %d;",
1339 MSGFW_CONVERSATION_TABLE_NAME, convId);
1341 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1343 if (err == MSG_ERR_DB_NORECORD) {
1344 pDbHandle->freeTable();
1346 } else if (err != MSG_SUCCESS) {
1347 pDbHandle->freeTable();
1350 pDbHandle->freeTable();
1355 bool MsgExistInThreadViewList(MsgDbHandler *pDbHandle, msg_thread_id_t convId)
1357 msg_error_t err = MSG_SUCCESS;
1359 char sqlQuery[MAX_QUERY_LEN+1];
1363 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1365 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d;",
1366 MSGFW_MESSAGE_TABLE_NAME, convId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
1368 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1370 if (err == MSG_ERR_DB_NORECORD) {
1371 pDbHandle->freeTable();
1373 } else if (err != MSG_SUCCESS) {
1374 pDbHandle->freeTable();
1377 pDbHandle->freeTable();
1382 bool MsgExistMessage(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsg)
1384 msg_error_t err = MSG_SUCCESS;
1386 char sqlQuery[MAX_QUERY_LEN+1];
1391 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1393 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE CONV_ID = %ud AND DISPLAY_TIME = %ud;",
1394 MSGFW_MESSAGE_TABLE_NAME, pMsg->threadId, (int)pMsg->displayTime);
1396 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1399 if (err != MSG_SUCCESS) {
1400 pDbHandle->freeTable();
1405 pMsg->msgId = pDbHandle->getColumnToInt(1);
1407 pDbHandle->freeTable();
1413 bool MsgExistAddress(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsg, msg_thread_id_t convId, int index)
1415 msg_error_t err = MSG_SUCCESS;
1417 char sqlQuery[MAX_QUERY_LEN+1];
1422 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1424 if (strlen(pMsg->addressList[index].addressVal) > (unsigned int)MsgContactGetMinMatchDigit()) {
1425 int addrSize = strlen(pMsg->addressList[index].addressVal);
1426 char newPhoneNum[addrSize+1];
1427 memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
1428 MsgConvertNumber(pMsg->addressList[index].addressVal, newPhoneNum, addrSize);
1430 snprintf(sqlQuery, sizeof(sqlQuery),
1431 "SELECT ADDRESS_ID FROM %s WHERE ADDRESS_VAL LIKE '%%%%%s' AND CONV_ID= %d;",
1432 MSGFW_ADDRESS_TABLE_NAME, newPhoneNum, convId);
1434 snprintf(sqlQuery, sizeof(sqlQuery),
1435 "SELECT ADDRESS_ID FROM %s WHERE ADDRESS_VAL = '%s' AND CONV_ID= %d;",
1436 MSGFW_ADDRESS_TABLE_NAME, pMsg->addressList[index].addressVal, convId);
1439 err = pDbHandle->getTable(sqlQuery, &rowCnt, NULL);
1441 if (err == MSG_ERR_DB_NORECORD) {
1442 pDbHandle->freeTable();
1444 } else if (err != MSG_SUCCESS) {
1445 pDbHandle->freeTable();
1448 pDbHandle->freeTable();
1454 void MsgStoUpdateAddress(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsg, msg_thread_id_t convId)
1457 msg_error_t err = MSG_SUCCESS;
1459 char sqlQuery[MAX_QUERY_LEN+1];
1461 for (int i = 0; i < pMsg->nAddressCnt; i++) {
1462 if (strlen(pMsg->addressList[i].addressVal) > (unsigned int)MsgContactGetMinMatchDigit() && pMsg->addressList[i].addressType == MSG_ADDRESS_TYPE_PLMN) {
1463 int addrSize = strlen(pMsg->addressList[i].addressVal);
1464 char newPhoneNum[addrSize+1];
1465 memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
1466 MsgConvertNumber(pMsg->addressList[i].addressVal, newPhoneNum, addrSize);
1468 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1469 snprintf(sqlQuery, sizeof(sqlQuery),
1471 "ADDRESS_VAL = '%s', "
1472 "ADDRESS_TYPE = %d, "
1473 "RECIPIENT_TYPE = %d "
1474 "WHERE CONV_ID = %d "
1475 "AND ADDRESS_VAL LIKE '%%%%%s';",
1476 MSGFW_ADDRESS_TABLE_NAME, pMsg->addressList[i].addressVal,
1477 pMsg->addressList[i].addressType, pMsg->addressList[i].recipientType, convId, newPhoneNum);
1479 err = pDbHandle->execQuery(sqlQuery);
1480 if (err != MSG_SUCCESS) MSG_DEBUG("Fail to execQuery(). [%s]", sqlQuery);
1482 pDbHandle->finalizeQuery();
1489 msg_error_t MsgStoAddCBChannelInfo(MsgDbHandler *pDbHandle, MSG_CB_CHANNEL_S *pCBChannel, msg_sim_slot_id_t simIndex)
1491 #ifndef FEATURE_SMS_CDMA
1494 char sqlQuery[MAX_QUERY_LEN] = {0, };
1496 pDbHandle->beginTrans();
1498 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1499 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_INDEX = %d;", MSGFW_CB_CHANNEL_INFO_TABLE_NAME, simIndex);
1501 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
1502 pDbHandle->endTrans(false);
1503 return MSG_ERR_DB_EXEC;
1506 for (int i = 0; i < pCBChannel->channelCnt; i++) {
1508 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1509 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s(CHANNEL_ACTIVATION, CHANNEL_FROM, CHANNEL_TO, CHANNEL_NAME, SIM_INDEX) VALUES (?, ?, ?, ?, ?);",
1510 MSGFW_CB_CHANNEL_INFO_TABLE_NAME);
1512 if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1513 pDbHandle->endTrans(false);
1514 return MSG_ERR_DB_PREPARE;
1516 pDbHandle->bindInt(pCBChannel->channelInfo[i].bActivate, index++);
1517 pDbHandle->bindInt(pCBChannel->channelInfo[i].from, index++);
1518 pDbHandle->bindInt(pCBChannel->channelInfo[i].to, index++);
1519 pDbHandle->bindText(pCBChannel->channelInfo[i].name, index++);
1520 pDbHandle->bindInt(simIndex, index++);
1522 if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1523 pDbHandle->finalizeQuery();
1524 pDbHandle->endTrans(false);
1525 return MSG_ERR_DB_STEP;
1528 pDbHandle->finalizeQuery();
1531 pDbHandle->endTrans(true);
1536 #else /* TODO: Add multisim for CDMA */
1539 char sqlQuery[MAX_QUERY_LEN] = {0, };
1541 pDbHandle->beginTrans();
1543 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1544 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s;", MSGFW_CDMA_CB_CHANNEL_INFO_TABLE_NAME);
1546 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
1547 pDbHandle->endTrans(false);
1548 return MSG_ERR_DB_EXEC;
1551 for (int i = 0; i < pCBChannel->channelCnt; i++) {
1552 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1553 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, '%s');", MSGFW_CDMA_CB_CHANNEL_INFO_TABLE_NAME,
1554 i, pCBChannel->channelInfo[i].bActivate, pCBChannel->channelInfo[i].ctg,
1555 pCBChannel->channelInfo[i].lang, pCBChannel->channelInfo[i].name);
1557 if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
1558 pDbHandle->endTrans(false);
1559 return MSG_ERR_DB_EXEC;
1563 pDbHandle->endTrans(true);
1572 msg_error_t MsgStoGetCBChannelInfo(MsgDbHandler *pDbHandle, MSG_CB_CHANNEL_S *pCBChannel, msg_sim_slot_id_t simIndex)
1574 #ifndef FEATURE_SMS_CDMA
1577 int rowCnt = 0, index = 0;
1579 char sqlQuery[MAX_QUERY_LEN] = {0, };
1581 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1582 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CHANNEL_ACTIVATION, CHANNEL_FROM, CHANNEL_TO, CHANNEL_NAME FROM %s WHERE SIM_INDEX = %d;", MSGFW_CB_CHANNEL_INFO_TABLE_NAME, simIndex);
1584 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
1586 pCBChannel->channelCnt = rowCnt;
1588 if (err == MSG_ERR_DB_NORECORD) {
1589 pDbHandle->freeTable();
1590 return MSG_ERR_DB_NORECORD;
1591 } else if (err != MSG_SUCCESS) {
1592 MSG_DEBUG("Fail to getTable().");
1593 pDbHandle->freeTable();
1594 return MSG_ERR_DB_GETTABLE;
1597 for (int i = 0; i < rowCnt; i++) {
1598 pCBChannel->channelInfo[i].bActivate = pDbHandle->getColumnToInt(index++);
1599 pCBChannel->channelInfo[i].from = pDbHandle->getColumnToInt(index++);
1600 pCBChannel->channelInfo[i].to = pDbHandle->getColumnToInt(index++);
1601 pDbHandle->getColumnToString(index++, CB_CHANNEL_NAME_MAX, pCBChannel->channelInfo[i].name);
1603 MSG_DEBUG("CH_ACT = %d", pCBChannel->channelInfo[i].bActivate);
1604 MSG_DEBUG("CH_FROM = %d", pCBChannel->channelInfo[i].from);
1605 MSG_DEBUG("CH_TO = %d", pCBChannel->channelInfo[i].to);
1606 MSG_DEBUG("CH_NAME = %s", pCBChannel->channelInfo[i].name);
1609 pDbHandle->freeTable();
1614 #else /* TODO: Add multisim for CDMA */
1617 int rowCnt = 0, index = 0;
1619 char sqlQuery[MAX_QUERY_LEN] = {0, };
1621 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1622 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CHANNEL_ACTIVATION, CHANNEL_CATEGORY, CHANNEL_LANGUAGE, CHANNEL_NAME FROM %s;", MSGFW_CDMA_CB_CHANNEL_INFO_TABLE_NAME);
1624 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
1626 pCBChannel->channelCnt = rowCnt;
1628 if (err == MSG_ERR_DB_NORECORD) {
1629 pDbHandle->freeTable();
1630 return MSG_ERR_DB_NORECORD;
1631 } else if (err != MSG_SUCCESS) {
1632 MSG_DEBUG("Fail to getTable().");
1633 pDbHandle->freeTable();
1634 return MSG_ERR_DB_GETTABLE;
1637 for (int i = 0; i < rowCnt; i++) {
1638 pCBChannel->channelInfo[i].bActivate = pDbHandle->getColumnToInt(index++);
1639 pCBChannel->channelInfo[i].ctg = pDbHandle->getColumnToInt(index++);
1640 pCBChannel->channelInfo[i].lang = pDbHandle->getColumnToInt(index++);
1641 pDbHandle->getColumnToString(index++, CB_CHANNEL_NAME_MAX, pCBChannel->channelInfo[i].name);
1643 MSG_DEBUG("CH_ACT = %d", pCBChannel->channelInfo[i].bActivate);
1644 MSG_DEBUG("CH_CTG = %d", pCBChannel->channelInfo[i].ctg);
1645 MSG_DEBUG("CH_LANG = %d", pCBChannel->channelInfo[i].lang);
1646 MSG_DEBUG("CH_NAME = %s", pCBChannel->channelInfo[i].name);
1649 pDbHandle->freeTable();
1657 msg_error_t MsgStoGetThreadViewList(const MSG_SORT_RULE_S *pSortRule, msg_struct_list_s *pThreadViewList)
1659 MsgDbHandler *dbHandle = getDbHandle();
1660 dbHandle->connectReadOnly();
1662 pThreadViewList->nCount = 0;
1663 pThreadViewList->msg_struct_info = NULL;
1665 int rowCnt = 0, index = 0;
1667 char sqlQuery[MAX_QUERY_LEN+1];
1668 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1670 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.CONV_ID, A.UNREAD_CNT, A.SMS_CNT, A.MMS_CNT, A.MAIN_TYPE, A.SUB_TYPE, "
1671 "A.MSG_DIRECTION, A.DISPLAY_TIME, A.DISPLAY_NAME, A.MSG_TEXT, "
1672 "(COUNT(CASE WHEN M.PROTECTED = 1 THEN 1 END)) AS PROTECTED, "
1673 "(CASE WHEN B.FOLDER_ID = %d THEN 1 END) AS DRAFT, "
1674 "(CASE WHEN B.NETWORK_STATUS = %d THEN 1 END) AS FAILED, "
1675 "(CASE WHEN B.NETWORK_STATUS = %d THEN 1 END) AS SENDING "
1677 "LEFT OUTER JOIN %s B ON A.LAST_MSG_ID = B.MSG_ID "
1678 "LEFT OUTER JOIN %s M ON A.CONV_ID = M.CONV_ID "
1679 "WHERE A.SMS_CNT + A.MMS_CNT > 0 "
1680 "GROUP BY A.CONV_ID ORDER BY A.DISPLAY_TIME DESC;",
1682 MSG_NETWORK_SEND_FAIL,
1683 MSG_NETWORK_SENDING,
1684 MSGFW_CONVERSATION_TABLE_NAME,
1685 MSGFW_MESSAGE_TABLE_NAME,
1686 MSGFW_MESSAGE_TABLE_NAME);
1688 msg_error_t err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
1690 if (err == MSG_ERR_DB_NORECORD) {
1691 dbHandle->freeTable();
1693 } else if (err != MSG_SUCCESS) {
1694 MSG_DEBUG("Fail to getTable().");
1695 dbHandle->freeTable();
1700 MSG_DEBUG("rowCnt is %d", rowCnt);
1701 dbHandle->freeTable();
1705 pThreadViewList->nCount = rowCnt;
1707 MSG_DEBUG("pThreadViewList->nCount [%d]", pThreadViewList->nCount);
1709 pThreadViewList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(msg_struct_t));
1711 MSG_THREAD_VIEW_S *pTmp = NULL;
1712 msg_struct_s *thread_t = NULL;
1714 for (int i = 0; i < rowCnt; i++) {
1715 thread_t = (msg_struct_s *)new msg_struct_s;
1716 pThreadViewList->msg_struct_info[i] = (msg_struct_t)thread_t;
1718 thread_t->type = MSG_STRUCT_THREAD_INFO;
1719 thread_t->data = new MSG_THREAD_VIEW_S;
1721 pTmp = (MSG_THREAD_VIEW_S *)thread_t->data;
1722 memset(pTmp, 0x00, sizeof(MSG_THREAD_VIEW_S));
1724 pTmp->threadId = dbHandle->getColumnToInt(index++);
1726 pTmp->unreadCnt = dbHandle->getColumnToInt(index++);
1727 pTmp->smsCnt = dbHandle->getColumnToInt(index++);
1728 pTmp->mmsCnt = dbHandle->getColumnToInt(index++);
1730 pTmp->mainType = dbHandle->getColumnToInt(index++);
1731 pTmp->subType = dbHandle->getColumnToInt(index++);
1733 pTmp->direction = dbHandle->getColumnToInt(index++);
1734 pTmp->threadTime = (time_t)dbHandle->getColumnToInt(index++);
1736 memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
1737 dbHandle->getColumnToString(index++, MAX_THREAD_NAME_LEN, pTmp->threadName);
1739 char tmpData[MAX_THREAD_DATA_LEN + 1] = {0};
1740 dbHandle->getColumnToString(index++, MAX_THREAD_DATA_LEN, tmpData);
1741 memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
1743 if (g_utf8_validate(tmpData, -1, (const gchar **)&end) == true) {
1744 g_utf8_strncpy(pTmp->threadData, tmpData, g_utf8_strlen(tmpData, -1));
1746 g_utf8_strncpy(pTmp->threadData, tmpData, g_utf8_strlen(tmpData, end - tmpData));
1749 pTmp->bProtected = (dbHandle->getColumnToInt(index++) > 0) ? true : false;
1750 pTmp->bDraft = dbHandle->getColumnToInt(index++);
1751 pTmp->bSendFailed = dbHandle->getColumnToInt(index++);
1752 pTmp->bSending = dbHandle->getColumnToInt(index++);
1755 dbHandle->freeTable();
1761 msg_error_t MsgStoGetConversationPreview(MsgDbHandler *pDbHandle, MSG_CONVERSATION_VIEW_S *pConv)
1763 char sqlQuery[MAX_QUERY_LEN + 1];
1764 int rowCnt = 0, index = 0;
1765 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1768 return MSG_ERR_NULL_POINTER;
1770 pConv->tcs_bc_level = -1; /* init */
1772 /*(MSG_ID INTEGER, TYPE INTEGER, VALUE TEXT, COUNT INTEGER) */
1773 snprintf(sqlQuery, sizeof(sqlQuery),
1774 "SELECT TYPE, VALUE, COUNT "
1775 "FROM %s WHERE MSG_ID=%d;",
1776 MSGFW_MMS_PREVIEW_TABLE_NAME, pConv->msgId);
1778 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
1779 if (err == MSG_SUCCESS) {
1780 for (int i = 0; i < rowCnt; i++) {
1781 int type = pDbHandle->getColumnToInt(index++);
1782 if (type == MSG_MMS_ITEM_TYPE_IMG) {
1783 pDbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pConv->imageThumbPath);
1784 pDbHandle->getColumnToInt(index++);
1785 } else if (type == MSG_MMS_ITEM_TYPE_VIDEO) {
1786 pDbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pConv->videoThumbPath);
1787 pDbHandle->getColumnToInt(index++);
1788 } else if (type == MSG_MMS_ITEM_TYPE_AUDIO) {
1789 pDbHandle->getColumnToString(index++, MSG_FILENAME_LEN_MAX, pConv->audioFileName);
1790 pDbHandle->getColumnToInt(index++);
1791 } else if (type == MSG_MMS_ITEM_TYPE_ATTACH) {
1792 pDbHandle->getColumnToString(index++, MSG_FILENAME_LEN_MAX, pConv->attachFileName);
1793 pConv->attachCount = pDbHandle->getColumnToInt(index++);
1794 } else if (type == MSG_MMS_ITEM_TYPE_PAGE) {
1796 pConv->pageCount = pDbHandle->getColumnToInt(index++);
1797 } else if (type == MSG_MMS_ITEM_TYPE_MALWARE) {
1799 pConv->tcs_bc_level = pDbHandle->getColumnToInt(index++);
1800 } else if (type == MSG_MMS_ITEM_TYPE_1ST_MEDIA) {
1801 pDbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pConv->firstMediaPath);
1802 pDbHandle->getColumnToInt(index++);
1804 MSG_DEBUG("Unknown item type [%d]", type);
1810 pDbHandle->freeTable();
1814 msg_error_t MsgStoGetConversationMultipart(MsgDbHandler *pDbHandle, MSG_CONVERSATION_VIEW_S *pConv)
1816 char sqlQuery[MAX_QUERY_LEN + 1];
1817 int rowCnt = 0, index = 0;
1818 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1821 return MSG_ERR_NULL_POINTER;
1823 snprintf(sqlQuery, sizeof(sqlQuery),
1824 "SELECT CONTENT_TYPE, NAME, FILE_PATH, CONTENT_ID, CONTENT_LOCATION, TCS_LEVEL, MALWARE_ALLOW, THUMB_FILE_PATH "
1825 "FROM %s WHERE MSG_ID=%d;",
1826 MSGFW_MMS_MULTIPART_TABLE_NAME, pConv->msgId);
1828 msg_error_t err = pDbHandle->getTable(sqlQuery, &rowCnt, &index);
1829 if (err == MSG_SUCCESS) {
1830 GList *multipart_list = NULL;
1831 for (int i = 0; i < rowCnt; i++) {
1832 msg_struct_s *multipart_struct_s = new msg_struct_s;
1833 multipart_struct_s->type = MSG_STRUCT_MULTIPART_INFO;
1834 multipart_struct_s->data = new MMS_MULTIPART_DATA_S;
1835 memset(multipart_struct_s->data, 0x00, sizeof(MMS_MULTIPART_DATA_S));
1837 MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)multipart_struct_s->data;
1839 pDbHandle->getColumnToString(index++, sizeof(multipart->szContentType), multipart->szContentType);
1840 pDbHandle->getColumnToString(index++, sizeof(multipart->szFileName), multipart->szFileName);
1841 pDbHandle->getColumnToString(index++, sizeof(multipart->szFilePath), multipart->szFilePath);
1842 pDbHandle->getColumnToString(index++, sizeof(multipart->szContentID), multipart->szContentID);
1843 pDbHandle->getColumnToString(index++, sizeof(multipart->szContentLocation), multipart->szContentLocation);
1845 multipart->tcs_bc_level = pDbHandle->getColumnToInt(index++);
1846 multipart->malware_allow = pDbHandle->getColumnToInt(index++);
1847 pDbHandle->getColumnToString(index++, sizeof(multipart->szThumbFilePath), multipart->szThumbFilePath);
1849 multipart_list = g_list_append(multipart_list, multipart_struct_s);
1851 pConv->multipart_list = (msg_list_handle_t)multipart_list;
1854 pDbHandle->freeTable();
1858 msg_error_t MsgStoGetConversationViewItem(msg_message_id_t msgId, MSG_CONVERSATION_VIEW_S *pConv)
1860 MsgDbHandler *dbHandle = getDbHandle();
1861 dbHandle->connectReadOnly();
1863 int rowCnt = 0, index = 0;
1865 char sqlQuery[MAX_QUERY_LEN+1];
1867 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1869 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, CONV_ID, FOLDER_ID, STORAGE_ID, MAIN_TYPE, SUB_TYPE, \
1870 DISPLAY_TIME, DATA_SIZE, NETWORK_STATUS, READ_STATUS, PROTECTED, \
1871 MSG_DIRECTION, DPM_RESTRICTED, SCHEDULED_TIME, SUBJECT, MSG_TEXT, ATTACHMENT_COUNT, SIM_INDEX\
1872 FROM %s WHERE MSG_ID = %d;",
1873 MSGFW_MESSAGE_TABLE_NAME, msgId);
1875 msg_error_t err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
1877 if (err == MSG_ERR_DB_NORECORD) {
1878 dbHandle->freeTable();
1880 } else if (err != MSG_SUCCESS) {
1881 MSG_DEBUG("Fail to getTable().");
1882 dbHandle->freeTable();
1886 memset(pConv, 0x00, sizeof(MSG_CONVERSATION_VIEW_S));
1887 pConv->pText = NULL;
1889 pConv->msgId = dbHandle->getColumnToInt(index++);
1890 pConv->threadId = dbHandle->getColumnToInt(index++);
1891 pConv->folderId = dbHandle->getColumnToInt(index++);
1892 pConv->storageId = dbHandle->getColumnToInt(index++);
1893 pConv->mainType = dbHandle->getColumnToInt(index++);
1894 pConv->subType = dbHandle->getColumnToInt(index++);
1895 pConv->displayTime = (time_t)dbHandle->getColumnToInt(index++);
1896 pConv->textSize = dbHandle->getColumnToInt(index++);
1897 pConv->networkStatus = dbHandle->getColumnToInt(index++);
1898 pConv->bRead = dbHandle->getColumnToInt(index++);
1899 pConv->bProtected = dbHandle->getColumnToInt(index++);
1900 pConv->direction = dbHandle->getColumnToInt(index++);
1901 pConv->bRestricted = dbHandle->getColumnToInt(index++);
1902 pConv->scheduledTime = (time_t)dbHandle->getColumnToInt(index++);
1904 dbHandle->getColumnToString(index++, MAX_SUBJECT_LEN, pConv->subject);
1905 char *tmpText = g_strdup(dbHandle->getColumnToString(index++));
1907 /*It does Not need to Get attach count in MSG_MESSAGE_TABLE. see MsgStoGetConversationPreview */
1908 /*pConv->attachCount = dbHandle->getColumnToInt(index++); */
1910 if (pConv->bRestricted == true) {
1911 pConv->textSize = 0;
1912 memset(pConv->subject, 0x00, sizeof(pConv->subject));
1916 pConv->simIndex = dbHandle->getColumnToInt(index++);
1918 dbHandle->freeTable();
1920 if (pConv->mainType == MSG_MMS_TYPE &&
1921 (pConv->networkStatus == MSG_NETWORK_RETRIEVING || pConv->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pConv->subType == MSG_NOTIFICATIONIND_MMS)) {
1922 pConv->pText = NULL;
1923 pConv->textSize = 0;
1925 if (pConv->mainType == MSG_SMS_TYPE) {
1926 pConv->pText = new char[pConv->textSize+2];
1927 memset(pConv->pText, 0x00, pConv->textSize+2);
1928 snprintf(pConv->pText, pConv->textSize+1, "%s", tmpText);
1929 } else if (pConv->mainType == MSG_MMS_TYPE) {
1931 pConv->textSize = strlen(tmpText);
1933 pConv->pText = new char[pConv->textSize+1];
1934 memset(pConv->pText, 0x00, pConv->textSize+1);
1936 strncpy(pConv->pText, tmpText, pConv->textSize);
1939 MsgStoGetConversationPreview(dbHandle, pConv);
1940 MsgStoGetConversationMultipart(dbHandle, pConv);
1955 msg_error_t MsgStoGetConversationViewList(msg_thread_id_t threadId, msg_struct_list_s *pConvViewList)
1959 MsgDbHandler *dbHandle = getDbHandle();
1960 dbHandle->connectReadOnly();
1962 pConvViewList->nCount = 0;
1963 pConvViewList->msg_struct_info = NULL;
1965 int rowCnt = 0, index = 0;
1967 char sqlQuery[MAX_QUERY_LEN+1];
1969 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1971 #ifdef MSG_NOTI_INTEGRATION
1972 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, CONV_ID, FOLDER_ID, STORAGE_ID, MAIN_TYPE, SUB_TYPE, \
1973 DISPLAY_TIME, DATA_SIZE, NETWORK_STATUS, READ_STATUS, PROTECTED, \
1974 MSG_DIRECTION, DPM_RESTRICTED, SCHEDULED_TIME, SUBJECT, MSG_TEXT, ATTACHMENT_COUNT, SIM_INDEX \
1975 FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME, MSG_ID ASC;",
1976 MSGFW_MESSAGE_TABLE_NAME, threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE);
1978 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, CONV_ID, FOLDER_ID, STORAGE_ID, MAIN_TYPE, SUB_TYPE, \
1979 DISPLAY_TIME, DATA_SIZE, NETWORK_STATUS, READ_STATUS, PROTECTED, \
1980 MSG_DIRECTION, DPM_RESTRICTED, SCHEDULED_TIME, SUBJECT, MSG_TEXT, ATTACHMENT_COUNT \
1981 FROM %s WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d ORDER BY DISPLAY_TIME, MSG_ID ASC;",
1982 MSGFW_MESSAGE_TABLE_NAME, threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE);
1985 msg_error_t err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
1987 if (err == MSG_ERR_DB_NORECORD) {
1988 dbHandle->freeTable();
1990 } else if (err != MSG_SUCCESS) {
1991 MSG_DEBUG("Fail to getTable().");
1992 dbHandle->freeTable();
1996 pConvViewList->nCount = rowCnt;
1997 char *tmpText[rowCnt] = {NULL};
1999 MSG_DEBUG("pConvViewList->nCount [%d]", pConvViewList->nCount);
2001 pConvViewList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(msg_struct_t));
2002 memset(pConvViewList->msg_struct_info, 0x00, sizeof(msg_struct_t) * rowCnt);
2004 msg_struct_s *conv = NULL;
2005 MSG_CONVERSATION_VIEW_S *pTmp = NULL;
2007 for (int i = 0; i < rowCnt; i++) {
2008 pConvViewList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
2009 memset(pConvViewList->msg_struct_info[i], 0x00, sizeof(msg_struct_s));
2011 conv = (msg_struct_s *)pConvViewList->msg_struct_info[i];
2013 conv->type = MSG_STRUCT_CONV_INFO;
2014 conv->data = new MSG_CONVERSATION_VIEW_S;
2015 memset(conv->data, 0x00, sizeof(MSG_CONVERSATION_VIEW_S));
2017 pTmp = (MSG_CONVERSATION_VIEW_S *)conv->data;
2021 pTmp->msgId = dbHandle->getColumnToInt(index++);
2022 pTmp->threadId = dbHandle->getColumnToInt(index++);
2023 pTmp->folderId = dbHandle->getColumnToInt(index++);
2024 pTmp->storageId = dbHandle->getColumnToInt(index++);
2025 pTmp->mainType = dbHandle->getColumnToInt(index++);
2026 pTmp->subType = dbHandle->getColumnToInt(index++);
2027 pTmp->displayTime = (time_t)dbHandle->getColumnToInt(index++);
2028 pTmp->textSize = dbHandle->getColumnToInt(index++);
2029 pTmp->networkStatus = dbHandle->getColumnToInt(index++);
2030 pTmp->bRead = dbHandle->getColumnToInt(index++);
2031 pTmp->bProtected = dbHandle->getColumnToInt(index++);
2032 pTmp->direction = dbHandle->getColumnToInt(index++);
2033 pTmp->bRestricted = dbHandle->getColumnToInt(index++);
2034 index++; /* This field is reserved. */
2036 dbHandle->getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
2037 tmpText[i] = g_strdup(dbHandle->getColumnToString(index++));
2039 /*It does Not need to Get attach count in MSG_MESSAGE_TABLE. see MsgStoGetConversationPreview */
2040 /*pTmp->attachCount = dbHandle->getColumnToInt(index++); */
2043 if (pTmp->bRestricted == true) {
2045 memset(pTmp->subject, 0x00, sizeof(pTmp->subject));
2046 tmpText[i][0] = '\0';
2048 pTmp->simIndex = dbHandle->getColumnToInt(index++);
2050 dbHandle->freeTable();
2052 for (int i = 0; i < pConvViewList->nCount; i++) {
2053 conv = (msg_struct_s *)pConvViewList->msg_struct_info[i];
2054 pTmp = (MSG_CONVERSATION_VIEW_S *)conv->data;
2056 if (pTmp->mainType == MSG_MMS_TYPE &&
2057 (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->subType == MSG_NOTIFICATIONIND_MMS)) {
2061 if (pTmp->mainType == MSG_SMS_TYPE) {
2062 pTmp->pText = new char[pTmp->textSize+2];
2063 memset(pTmp->pText, 0x00, pTmp->textSize+2);
2064 snprintf(pTmp->pText, pTmp->textSize+1, "%s", tmpText[i]);
2065 } else if (pTmp->mainType == MSG_MMS_TYPE) {
2067 pTmp->textSize = strlen(tmpText[i]);
2069 pTmp->pText = new char[pTmp->textSize+1];
2070 memset(pTmp->pText, 0x00, pTmp->textSize+1);
2072 strncpy(pTmp->pText, tmpText[i], pTmp->textSize);
2075 MsgStoGetConversationPreview(dbHandle, pTmp);
2076 MsgStoGetConversationMultipart(dbHandle, pTmp);
2091 msg_error_t MsgStoSearchMessage(const char *pSearchString, msg_struct_list_s *pThreadViewList, int contactCount)
2094 return MSG_ERR_NULL_POINTER;
2096 MsgDbHandler *dbHandle = getDbHandle();
2097 dbHandle->connectReadOnly();
2098 char *escapeAddressStr = NULL;
2100 /* Clear Out Parameter */
2101 pThreadViewList->nCount = 0;
2102 pThreadViewList->msg_struct_info = NULL;
2104 tr1::unordered_set<msg_thread_id_t> IdList;
2105 queue<MSG_THREAD_VIEW_S> searchList;
2107 MSG_THREAD_VIEW_S threadView;
2109 char sqlQuery[MAX_QUERY_LEN+1];
2111 /* Search - Address, Name */
2112 memset(sqlQuery, 0x00, MAX_QUERY_LEN+1);
2113 snprintf(sqlQuery, MAX_QUERY_LEN, "SELECT A.CONV_ID, A.UNREAD_CNT, A.SMS_CNT, A.MMS_CNT, A.DISPLAY_NAME, "
2114 "A.MAIN_TYPE, A.SUB_TYPE, A.MSG_DIRECTION, A.DISPLAY_TIME, A.MSG_TEXT, "
2115 "(SELECT COUNT(*) FROM %s B WHERE B.CONV_ID = A.CONV_ID AND B.PROTECTED = 1) AS PROTECTED, "
2116 "(SELECT COUNT(*) FROM %s B WHERE B.MSG_ID = A.LAST_MSG_ID AND B.FOLDER_ID = %d) AS DRAFT, "
2117 "(SELECT COUNT(*) FROM %s B WHERE B.MSG_ID = A.LAST_MSG_ID AND B.NETWORK_STATUS = %d) AS FAILED, "
2118 "(SELECT COUNT(*) FROM %s B WHERE B.MSG_ID = A.LAST_MSG_ID AND B.NETWORK_STATUS = %d) AS SENDING "
2119 "FROM %s A WHERE (A.SMS_CNT > 0 OR A.MMS_CNT > 0) "
2121 "(SELECT DISTINCT(CONV_ID) FROM %s WHERE "
2122 "ADDRESS_VAL LIKE ? ESCAPE '%c' ",
2123 MSGFW_MESSAGE_TABLE_NAME,
2124 MSGFW_MESSAGE_TABLE_NAME, MSG_DRAFT_ID,
2125 MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_SEND_FAIL,
2126 MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_SENDING,
2127 MSGFW_CONVERSATION_TABLE_NAME,
2128 MSGFW_ADDRESS_TABLE_NAME,
2129 MSGFW_DB_ESCAPE_CHAR);
2131 unsigned int tmpSize = 0;
2132 if (contactCount > 0) {
2133 tmpSize = strlen(sqlQuery);
2134 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
2135 "OR ADDRESS_VAL IN (SELECT C.ADDRESS_VAL FROM %s C JOIN %s D ON (C.ADDRESS_VAL LIKE D.ADDRESS_VAL))"
2136 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_ADDRESS_TEMP_TABLE_NAME);
2139 tmpSize = strlen(sqlQuery);
2140 snprintf(sqlQuery+tmpSize, MAX_QUERY_LEN-tmpSize,
2141 ") ORDER BY A.DISPLAY_TIME DESC;");
2143 MSG_DEBUG("sqlQuery=[%s]", sqlQuery);
2145 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
2146 MSG_DEBUG("Prepare query fail.");
2147 return MSG_ERR_DB_PREPARE;
2150 MsgConvertStrWithEscape(pSearchString, &escapeAddressStr);
2151 MSG_DEBUG("escapeAddressStr [%s]", escapeAddressStr);
2152 dbHandle->bindText(escapeAddressStr, 1);
2153 /*dbHandle->bindText(escapeAddressStr, 2); */
2154 /*dbHandle->bindText(escapeAddressStr, 3); */
2155 /*dbHandle->bindText(escapeAddressStr, 4); */
2157 while (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
2158 memset(&threadView, 0x00, sizeof(threadView));
2160 threadView.threadId = dbHandle->columnInt(0);
2161 threadView.unreadCnt = dbHandle->columnInt(1);
2162 threadView.smsCnt = dbHandle->columnInt(2);
2163 threadView.mmsCnt = dbHandle->columnInt(3);
2165 if (dbHandle->columnText(4))
2166 strncpy(threadView.threadName, (char *)dbHandle->columnText(4), MAX_THREAD_NAME_LEN);
2168 threadView.mainType = dbHandle->columnInt(5);
2169 threadView.subType = dbHandle->columnInt(6);
2171 threadView.direction = dbHandle->columnInt(7);
2172 threadView.threadTime = (time_t)dbHandle->columnInt(8);
2174 if (dbHandle->columnText(9))
2175 strncpy(threadView.threadData, (char *)dbHandle->columnText(9), MAX_THREAD_DATA_LEN);
2177 int protectedCnt = dbHandle->columnInt(10);
2178 if (protectedCnt > 0)
2179 threadView.bProtected = true;
2181 int draftCnt = dbHandle->columnInt(11);
2183 threadView.bDraft = true;
2185 int failedCnt = dbHandle->columnInt(12);
2187 threadView.bSendFailed = true;
2189 int sendingCnt = dbHandle->columnInt(13);
2191 threadView.bSending = true;
2193 tr1::unordered_set<msg_thread_id_t>::iterator it;
2195 it = IdList.find(threadView.threadId);
2197 if (it == IdList.end()) {
2198 IdList.insert(threadView.threadId);
2199 searchList.push(threadView);
2203 dbHandle->finalizeQuery();
2205 if (escapeAddressStr) {
2206 free(escapeAddressStr);
2207 escapeAddressStr = NULL;
2211 /* Add data to Out Parameter */
2212 pThreadViewList->nCount = searchList.size();
2213 pThreadViewList->msg_struct_info = (msg_struct_t *)calloc(searchList.size(), sizeof(msg_struct_t));
2214 if (pThreadViewList->msg_struct_info == NULL)
2215 return MSG_ERR_MEMORY_ERROR;
2217 MSG_THREAD_VIEW_S *pTmp = NULL;
2218 msg_struct_s *thread_t = NULL;
2222 while (!searchList.empty()) {
2223 thread_t = (msg_struct_s *)new msg_struct_s;
2224 pThreadViewList->msg_struct_info[index] = (msg_struct_t)thread_t;
2226 thread_t->type = MSG_STRUCT_THREAD_INFO;
2227 thread_t->data = new MSG_THREAD_VIEW_S;
2229 pTmp = (MSG_THREAD_VIEW_S *)thread_t->data;
2230 memset(pTmp, 0x00, sizeof(MSG_THREAD_VIEW_S));
2232 memcpy(pTmp, &(searchList.front()), sizeof(MSG_THREAD_VIEW_S));
2243 msg_error_t MsgStoGetRejectMsgList(const char *pNumber, msg_struct_list_s *pRejectMsgList)
2245 MsgDbHandler *dbHandle = getDbHandle();
2246 dbHandle->connectReadOnly();
2248 /* Clear Out Parameter */
2249 pRejectMsgList->nCount = 0;
2250 pRejectMsgList->msg_struct_info = NULL;
2252 int rowCnt = 0, index = 0;
2254 char sqlQuery[MAX_QUERY_LEN+1];
2256 /* Search Reject Msg */
2257 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2259 if (pNumber != NULL) {
2260 int addrSize = strlen(pNumber);
2261 char phoneNumber[addrSize+1];
2262 memset(phoneNumber, 0x00, sizeof(phoneNumber));
2264 if (addrSize > MsgContactGetMinMatchDigit())
2265 MsgConvertNumber(pNumber, phoneNumber, addrSize);
2267 strncpy(phoneNumber, pNumber, addrSize);
2269 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT "
2274 "WHERE A.CONV_ID = B.CONV_ID "
2275 "AND B.MAIN_TYPE = %d "
2276 "AND B.SUB_TYPE = %d "
2277 "AND A.ADDRESS_VAL LIKE '%%%s' "
2278 "ORDER BY B.DISPLAY_TIME DESC;",
2279 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
2284 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT "
2289 "WHERE A.CONV_ID = B.CONV_ID "
2290 "AND B.MAIN_TYPE = %d "
2291 "AND B.SUB_TYPE = %d "
2292 "ORDER BY B.DISPLAY_TIME DESC;",
2293 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
2298 msg_error_t err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
2300 if (err != MSG_SUCCESS) {
2301 MSG_DEBUG("Fail to getTable().");
2302 dbHandle->freeTable();
2306 pRejectMsgList->nCount = rowCnt;
2308 MSG_DEBUG("pRejectMsgList->nCount [%d]", pRejectMsgList->nCount);
2310 pRejectMsgList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_REJECT_MSG_INFO_S *));
2312 msg_struct_s* pTmp = NULL;
2314 for (int i = 0; i < rowCnt; i++) {
2315 pRejectMsgList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
2317 pTmp = (msg_struct_s *)pRejectMsgList->msg_struct_info[i];
2318 pTmp->type = MSG_STRUCT_REJECT_MSG_INFO;
2319 pTmp->data = new MSG_REJECT_MSG_INFO_S;
2320 MSG_REJECT_MSG_INFO_S * pMsg = (MSG_REJECT_MSG_INFO_S *)pTmp->data;
2321 memset(pMsg, 0x00, sizeof(MSG_REJECT_MSG_INFO_S));
2323 pMsg->msgId = dbHandle->getColumnToInt(index++);
2324 memset(pMsg->msgText, 0x00, sizeof(pMsg->msgText));
2325 dbHandle->getColumnToString(index++, MAX_MSG_TEXT_LEN, pMsg->msgText);
2327 pMsg->displayTime = (time_t)dbHandle->getColumnToInt(index++);
2330 dbHandle->freeTable();
2336 msg_error_t MsgStoGetAddressList(const msg_thread_id_t threadId, msg_struct_list_s *pAddrList)
2338 MsgDbHandler *dbHandle = getDbHandle();
2339 dbHandle->connectReadOnly();
2341 msg_error_t err = MSG_SUCCESS;
2343 err = MsgStoGetAddressByConvId(dbHandle, threadId, pAddrList);
2349 msg_error_t MsgStoGetMessageList(const MSG_LIST_CONDITION_S *pListCond, msg_struct_list_s *pMsgList, int contactCount)
2351 MsgDbHandler *dbHandle = getDbHandle();
2352 dbHandle->connectReadOnly();
2354 /* Clear Out Parameter */
2355 pMsgList->nCount = 0;
2356 pMsgList->msg_struct_info = NULL;
2359 int multipartCnt = 0;
2361 char sqlQuery[MAX_QUERY_LEN+1];
2362 char sqlQuerySubset[(MAX_QUERY_LEN/5)+1];
2364 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2365 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s;", MSGFW_MMS_MULTIPART_TABLE_NAME);
2367 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
2368 return MSG_ERR_DB_PREPARE;
2370 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
2371 multipartCnt = dbHandle->columnInt(0);
2373 dbHandle->finalizeQuery();
2374 return MSG_ERR_DB_STEP;
2377 dbHandle->finalizeQuery();
2379 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2380 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT "
2389 "A.NETWORK_STATUS, "
2395 "A.SCHEDULED_TIME, "
2398 "A.ATTACHMENT_COUNT, "
2402 "B.RECIPIENT_TYPE, "
2405 if (pListCond->pTextVal != NULL && multipartCnt > 0) {
2406 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2407 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "FROM %s C, %s B, %s A WHERE A.CONV_ID > 0 AND A.CONV_ID = B.CONV_ID ",
2408 MSGFW_MMS_MULTIPART_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
2410 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2411 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "FROM %s B, %s A WHERE A.CONV_ID > 0 AND A.CONV_ID = B.CONV_ID ",
2412 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
2415 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2417 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2418 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.DPM_RESTRICTED = 0 ");
2419 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2422 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2424 if (pListCond->folderId == MSG_ALLBOX_ID)
2425 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_SPAMBOX_ID);
2426 else if (pListCond->folderId == MSG_IOSBOX_ID)
2427 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_DRAFT_ID);
2429 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID = %d ", pListCond->folderId);
2431 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2435 if (pListCond->threadId > 0) {
2436 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2437 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.CONV_ID = %d ", pListCond->threadId);
2438 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2443 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2445 switch (pListCond->msgType) {
2447 if (pListCond->pAddressVal == NULL)
2448 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d ", MSG_SMS_TYPE);
2450 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_NORMAL_SMS);
2454 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d ", MSG_MMS_TYPE);
2457 case MSG_TYPE_MMS_JAVA:
2458 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_MMS_TYPE, MSG_SENDREQ_JAVA_MMS);
2461 case MSG_TYPE_SMS_SYNCML:
2462 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_SYNCML_CP);
2464 case MSG_TYPE_SMS_REJECT:
2465 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_REJECT_SMS);
2469 MSG_DEBUG("msg type is not set.");
2473 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2477 if (pListCond->storageId > MSG_STORAGE_UNKNOWN) {
2478 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2479 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.STORAGE_ID = %d ", pListCond->storageId);
2480 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2485 if (pListCond->bProtected) {
2486 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2487 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.PROTECTED = %d ", pListCond->bProtected);
2488 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2493 if (pListCond->bScheduled) {
2494 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2495 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.SCHEDULED_TIME > 0 ");
2496 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2501 if (pListCond->simIndex > 0) {
2502 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2503 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.SIM_INDEX = %d ", pListCond->simIndex);
2504 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2509 if (pListCond->fromTime > 0) {
2510 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2511 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.DISPLAY_TIME >= %u ", (unsigned int)pListCond->fromTime);
2512 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2515 if (pListCond->toTime > 0) {
2516 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2517 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.DISPLAY_TIME <= %u ", (unsigned int)pListCond->toTime);
2518 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2521 if (pListCond->pAddressVal == NULL) {
2523 if (pListCond->pTextVal != NULL) {
2524 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2525 if (multipartCnt > 0) {
2526 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2527 "AND ((A.MSG_TEXT LIKE ? ESCAPE '%c' OR A.SUBJECT LIKE ? ESCAPE '%c' OR (C.TEXT LIKE ? ESCAPE '%c' AND A.MSG_ID = C.MSG_ID AND C.CONTENT_TYPE = 'text/plain'))) ",
2528 MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR);
2530 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2531 "AND ((A.MSG_TEXT LIKE ? ESCAPE '%c' OR A.SUBJECT LIKE ? ESCAPE '%c')) ",
2532 MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR);
2534 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2538 if (pListCond->pTextVal != NULL) {
2539 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2540 if (multipartCnt > 0) {
2541 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2542 "AND ((A.MSG_TEXT LIKE ? ESCAPE '%c' OR A.SUBJECT LIKE ? ESCAPE '%c' OR (C.TEXT LIKE ? ESCAPE '%c' AND A.MSG_ID = C.MSG_ID AND C.CONTENT_TYPE = 'text/plain')) ",
2543 MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR);
2545 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2546 "AND ((A.MSG_TEXT LIKE ? ESCAPE '%c' OR A.SUBJECT LIKE ? ESCAPE '%c') ",
2547 MSGFW_DB_ESCAPE_CHAR, MSGFW_DB_ESCAPE_CHAR);
2549 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2551 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2552 if (pListCond->bAnd) {
2553 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND ");
2555 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "OR ");
2557 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2560 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2561 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2562 "(B.ADDRESS_VAL LIKE ? ESCAPE '%c' ", MSGFW_DB_ESCAPE_CHAR);
2563 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2565 if (contactCount > 0) {
2566 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2567 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2568 "OR B.ADDRESS_VAL IN (SELECT D.ADDRESS_VAL FROM %s D JOIN %s E ON (D.ADDRESS_VAL LIKE E.ADDRESS_VAL)) "
2569 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_ADDRESS_TEMP_TABLE_NAME);
2570 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2573 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2574 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), ")) ");
2575 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2578 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2579 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2580 "AND (B.ADDRESS_VAL LIKE ? ESCAPE '%c' ", MSGFW_DB_ESCAPE_CHAR);
2581 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2583 if (contactCount > 0) {
2584 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2585 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
2586 "OR B.ADDRESS_VAL IN (SELECT D.ADDRESS_VAL FROM %s D JOIN %s E ON (D.ADDRESS_VAL LIKE E.ADDRESS_VAL)) "
2587 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_ADDRESS_TEMP_TABLE_NAME);
2588 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2591 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2592 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), ") ");
2593 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2597 msg_struct_s *pSortRule = (msg_struct_s *)pListCond->sortRule;
2599 if (pSortRule->type != MSG_STRUCT_SORT_RULE) {
2601 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2602 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.DISPLAY_TIME ");
2604 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2607 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2608 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "DESC ");
2610 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2612 MSG_SORT_RULE_S *pTmp = (MSG_SORT_RULE_S *)pSortRule->data;
2613 /* order : TODO: have to finish this */
2614 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2615 switch (pTmp->sortType) {
2616 case MSG_SORT_BY_MSG_TYPE:
2617 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.MAIN_TYPE ");
2619 case MSG_SORT_BY_READ_STATUS:
2620 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.READ_STATUS ");
2622 case MSG_SORT_BY_STORAGE_TYPE:
2623 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.STORAGE_ID ");
2626 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.DISPLAY_TIME ");
2629 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2632 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2633 if (pTmp->bAscending)
2634 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ASC ");
2636 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "DESC ");
2638 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2641 /* offset & limit */
2642 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2643 if (pListCond->offset >= 0 && pListCond->limit > 0)
2644 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "LIMIT %d OFFSET %d;", pListCond->limit, pListCond->offset);
2646 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), ";");
2648 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2651 /* 'til here sqlQuery is complete. */
2653 queue<MSG_MESSAGE_HIDDEN_S*> searchList;
2655 MSG_DEBUG("[%s]", sqlQuery);
2657 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
2658 MSG_DEBUG("Prepare query fail.");
2659 return MSG_ERR_DB_PREPARE;
2662 char *escapeTextStr = NULL;
2663 char *escapeAddressStr = NULL;
2665 if (pListCond->pAddressVal == NULL) {
2667 if (pListCond->pTextVal != NULL) {
2668 MsgConvertStrWithEscape(pListCond->pTextVal, &escapeTextStr);
2669 MSG_DEBUG("escapeTextStr [%s]", escapeTextStr);
2670 dbHandle->bindText(escapeTextStr, 1);
2671 dbHandle->bindText(escapeTextStr, 2);
2672 if (multipartCnt > 0) dbHandle->bindText(escapeTextStr, 3);
2676 if (pListCond->pTextVal != NULL) {
2677 MsgConvertStrWithEscape(pListCond->pTextVal, &escapeTextStr);
2678 MSG_DEBUG("escapeTestStr [%s]", escapeTextStr);
2681 MsgConvertStrWithEscape(pListCond->pAddressVal, &escapeAddressStr);
2682 MSG_DEBUG("escapeAddressStr [%s]", escapeAddressStr);
2684 dbHandle->bindText(escapeTextStr, 1);
2685 dbHandle->bindText(escapeTextStr, 2);
2686 if (multipartCnt > 0) {
2687 dbHandle->bindText(escapeTextStr, 3);
2688 dbHandle->bindText(escapeAddressStr, 4);
2690 dbHandle->bindText(escapeAddressStr, 3);
2695 MsgConvertStrWithEscape(pListCond->pAddressVal, &escapeAddressStr);
2696 MSG_DEBUG("escapeAddressStr [%s]", escapeAddressStr);
2697 dbHandle->bindText(escapeAddressStr, 1);
2702 MSG_MESSAGE_HIDDEN_S *pTmp = NULL;
2703 int lastMsgId = 0; /* for comparing same msg id. */
2705 while (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
2708 int msgid = dbHandle->columnInt(index++);
2709 MSG_DEBUG("msgid [%d]", msgid);
2711 if (lastMsgId != msgid) {
2712 MSG_DEBUG("lastMsgId != msgid");
2716 pTmp = new MSG_MESSAGE_HIDDEN_S;
2719 memset(pTmp, 0x00, sizeof(MSG_MESSAGE_HIDDEN_S));
2722 pTmp->pMmsData = NULL;
2723 pTmp->addressList = NULL;
2725 pTmp->msgId = msgid;
2727 pTmp->threadId = dbHandle->columnInt(index++);
2728 pTmp->folderId = dbHandle->columnInt(index++);
2729 pTmp->storageId = dbHandle->columnInt(index++);
2730 pTmp->mainType = dbHandle->columnInt(index++);
2731 pTmp->subType = dbHandle->columnInt(index++);
2732 pTmp->displayTime = (time_t)dbHandle->columnInt(index++);
2733 pTmp->dataSize = dbHandle->columnInt(index++);
2734 pTmp->networkStatus = dbHandle->columnInt(index++);
2735 pTmp->bRead = dbHandle->columnInt(index++);
2736 pTmp->bProtected = dbHandle->columnInt(index++);
2737 pTmp->bBackup = dbHandle->columnInt(index++);
2738 pTmp->priority = dbHandle->columnInt(index++);
2739 pTmp->direction = dbHandle->columnInt(index++);
2740 index++; /* This field is reserved. */
2742 strncpy(pTmp->subject, (char *)dbHandle->columnText(index++), MAX_SUBJECT_LEN);
2744 if (pTmp->mainType == MSG_MMS_TYPE &&
2745 (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->subType == MSG_NOTIFICATIONIND_MMS)) {
2749 pTmp->pData = (void *)new char[pTmp->dataSize+2];
2750 memset(pTmp->pData, 0x00, pTmp->dataSize+2);
2752 strncpy((char *)pTmp->pData, (char *)dbHandle->columnText(index++), pTmp->dataSize+1);
2755 pTmp->attachCount = dbHandle->columnInt(index++);
2757 strncpy(pTmp->thumbPath, (char *)dbHandle->columnText(index++), MSG_FILEPATH_LEN_MAX);
2759 pTmp->simIndex = dbHandle->columnInt(index++);
2761 pTmp->addr_list = (msg_struct_list_s *)new msg_struct_list_s;
2762 pTmp->addr_list->nCount = 0;
2763 pTmp->addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(msg_struct_t));
2764 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
2765 pTmp->addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
2766 memset(pTmp->addr_list->msg_struct_info[i], 0x00, sizeof(msg_struct_s));
2769 searchList.push(pTmp);
2773 MSG_DEBUG("lastMsgId == msgid");
2778 MSG_ADDRESS_INFO_S *pAddr = new MSG_ADDRESS_INFO_S;
2779 memset(pAddr, 0x00, sizeof(MSG_ADDRESS_INFO_S));
2781 pAddr->addressType = dbHandle->columnInt(index++);
2782 pAddr->recipientType = dbHandle->columnInt(index++);
2784 strncpy(pAddr->addressVal, (char *)dbHandle->columnText(index++), MAX_ADDRESS_VAL_LEN);
2786 strncpy(pAddr->displayName, pAddr->addressVal, MAX_DISPLAY_NAME_LEN);
2788 /* For GList *addressList */
2789 msg_struct_s *addr_info_s = new msg_struct_s;
2790 memset(addr_info_s, 0x00, sizeof(msg_struct_s));
2791 addr_info_s->type = MSG_STRUCT_ADDRESS_INFO;
2792 addr_info_s->data = new MSG_ADDRESS_INFO_S;
2793 memset(addr_info_s->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
2794 MSG_ADDRESS_INFO_S *addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
2795 addr_info->addressType = pAddr->addressType;
2796 addr_info->recipientType = pAddr->recipientType;
2797 addr_info->contactId = pAddr->contactId;
2798 strncpy(addr_info->addressVal, pAddr->addressVal, MAX_ADDRESS_VAL_LEN);
2799 strncpy(addr_info->displayName, pAddr->displayName, MAX_DISPLAY_NAME_LEN);
2800 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
2802 pTmp->addressList = g_list_append(pTmp->addressList, addr_info_s);
2804 if (pTmp->addr_list->nCount >= MAX_TO_ADDRESS_CNT) {
2808 msg_struct_s *pStruct = (msg_struct_s *)pTmp->addr_list->msg_struct_info[pTmp->addr_list->nCount];
2809 pTmp->addr_list->nCount++;
2810 pStruct->type = MSG_STRUCT_ADDRESS_INFO;
2811 pStruct->data = pAddr;
2816 dbHandle->finalizeQuery();
2818 pMsgList->nCount = searchList.size();
2819 MSG_DEBUG("pMsgList->nCount [%d]", pMsgList->nCount);
2821 pMsgList->msg_struct_info = (msg_struct_t *)calloc(pMsgList->nCount, sizeof(msg_struct_t));
2822 if (pMsgList->msg_struct_info == NULL)
2823 return MSG_ERR_MEMORY_ERROR;
2826 while (!searchList.empty()) {
2827 msg_struct_s *msg = new msg_struct_s;
2829 pMsgList->msg_struct_info[offset++] = (msg_struct_t)msg;
2831 msg->type = MSG_STRUCT_MESSAGE_INFO;
2832 msg->data = searchList.front();
2839 free(escapeTextStr);
2841 if (escapeAddressStr)
2842 free(escapeAddressStr);
2848 msg_error_t MsgStoGetMediaList(const msg_thread_id_t threadId, msg_list_handle_t *pMediaList)
2851 msg_error_t err = MSG_SUCCESS;
2852 MsgDbHandler *dbHandle = getDbHandle();
2853 dbHandle->connectReadOnly();
2854 char sqlQuery[MAX_QUERY_LEN+1];
2857 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2858 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MAIN_TYPE = %d AND DPM_RESTRICTED = 0 AND CONV_ID = %d;",
2859 MSGFW_MESSAGE_TABLE_NAME, MSG_MMS_TYPE, threadId);
2861 MSG_DEBUG("sqlQuery = [%s]", sqlQuery);
2863 err = dbHandle->getTable(sqlQuery, &msgIdCnt, NULL);
2864 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
2865 dbHandle->freeTable();
2867 } else if (err == MSG_ERR_DB_NORECORD) {
2868 dbHandle->freeTable();
2872 msg_message_id_t msgIds[msgIdCnt];
2874 for (int i = 1; i <= msgIdCnt; i++) {
2875 msgIds[i-1] = dbHandle->getColumnToInt(i);
2878 dbHandle->freeTable();
2880 GList *media_list = NULL;
2882 for (int i = 0; i < msgIdCnt; i++) {
2883 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2884 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, CONTENT_TYPE, FILE_PATH, THUMB_FILE_PATH "
2885 "FROM %s WHERE MSG_ID = %d AND SEQ <> -1 AND (TCS_LEVEL = -1 OR MALWARE_ALLOW = 1);",
2886 MSGFW_MMS_MULTIPART_TABLE_NAME, msgIds[i]);
2888 int rowCnt = 0, msg_id = 0, index = 0;
2890 err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
2892 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
2893 dbHandle->freeTable();
2897 MSG_MEDIA_INFO_S *pMedia = NULL;
2898 char mime_type[MAX_MIME_TYPE_LEN+1], media_item[MSG_FILEPATH_LEN_MAX+1], thumb_path[MSG_FILEPATH_LEN_MAX+1];
2900 for (int j = 0; j < rowCnt; j++) {
2901 msg_id = dbHandle->getColumnToInt(index++);
2902 memset(mime_type, 0x00, sizeof(mime_type));
2903 dbHandle->getColumnToString(index++, MAX_MIME_TYPE_LEN, mime_type);
2904 memset(media_item, 0x00, sizeof(media_item));
2905 dbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, media_item);
2906 memset(thumb_path, 0x00, sizeof(thumb_path));
2907 dbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, thumb_path);
2909 if (strstr(mime_type, "image") || strstr(mime_type, "video")) {
2910 msg_struct_s *media_struct_s = new msg_struct_s;
2911 media_struct_s->type = MSG_STRUCT_MEDIA_INFO;
2912 media_struct_s->data = new MSG_MEDIA_INFO_S;
2913 memset(media_struct_s->data, 0x00, sizeof(MSG_MEDIA_INFO_S));
2915 pMedia = (MSG_MEDIA_INFO_S *)media_struct_s->data;
2917 pMedia->msg_id = msg_id;
2918 snprintf(pMedia->mime_type, MAX_MIME_TYPE_LEN, "%s", mime_type);
2919 snprintf(pMedia->media_item, MSG_FILEPATH_LEN_MAX, "%s", media_item);
2920 snprintf(pMedia->thumb_path, MSG_FILEPATH_LEN_MAX, "%s", thumb_path);
2922 media_list = g_list_append(media_list, media_struct_s);
2926 dbHandle->freeTable();
2928 *pMediaList = (msg_list_handle_t)media_list;
2936 msg_error_t MsgStoDbSelectWithQuery(const char *szQuery, char ***db_res, int *row_count, int *col_count)
2940 msg_error_t err = MSG_SUCCESS;
2942 MsgDbHandler *dbHandle = getDbHandle();
2943 err = dbHandle->connectReadOnly();
2944 if (err != MSG_SUCCESS) {
2945 MSG_ERR("db connect (read only) is failed [%d]", err);
2949 char *zSQL = sqlite3_mprintf("SELECT %q;", szQuery);
2952 err = dbHandle->getTableWithResult((const char *)zSQL, db_res, row_count, col_count);
2956 THROW(MsgException::INVALID_RESULT, "sqlite3_mprintf() is failed");
2959 MSG_DEBUG("getTableWithResult :: row_count=[%d], col_count=[%d]", *row_count, *col_count);
2961 if (err == MSG_ERR_DB_NORECORD) {
2962 dbHandle->freeTable(*db_res);
2965 } else if (err != MSG_SUCCESS) {
2966 MSG_DEBUG("Fail to getTable().");
2967 dbHandle->freeTable(*db_res);
2975 void MsgStoDbFree(char **db_res)
2977 MsgDbHandler *dbHandle = getDbHandle();
2978 dbHandle->freeTable(db_res);
2982 #ifdef FEATURE_SMS_CDMA
2983 msg_error_t MsgStoClearUniquenessTable()
2987 msg_error_t err = MSG_SUCCESS;
2989 MsgDbHandler *dbHandle = getDbHandle();
2991 char sqlQuery[MAX_QUERY_LEN+1] = {0, };
2992 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = 0", MSGFW_UNIQUENESS_INFO_TABLE_NAME);
2994 err = dbHandle->execQuery(sqlQuery);