2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "MessageStorageReader.h"
19 #include "MsgSqliteWrapper.h"
21 #include <dpl/log/log.h>
24 #define MAX_THREAD_ADDR_LEN 40
25 #define MAX_THREAD_NAME_LEN 195
26 #define MAX_THREAD_DATA_LEN 128
29 //TODO copied from MsgInternalTypes.h
30 typedef unsigned char MSG_MAIN_TYPE_T;
31 typedef unsigned char MSG_SUB_TYPE_T;
32 typedef unsigned char MSG_CLASS_TYPE_T;
36 msg_address_type_t addressType; /**< The type of an address in case of an Email or a mobile phone */
37 msg_recipient_type_t recipientType; /**< The type of recipient address in case of To, Cc, and Bcc */
38 msg_contact_id_t contactId; /**< The contact ID of address */
39 char addressVal[MAX_ADDRESS_VAL_LEN+1]; /**< The actual value of an address */
40 char displayName[MAX_DISPLAY_NAME_LEN+1]; /**< The display name of an address */
43 typedef struct _msg_struct{
50 msg_message_id_t msgId; /**< Indicates the message ID of this message. */
51 msg_thread_id_t threadId; /**< Indicates the thread ID. */
52 msg_folder_id_t folderId; /**< Indicates the folder ID. see enum _MSG_FOLDER_TYPE_E */
53 MSG_MAIN_TYPE_T mainType; /**< Message main type. See enum _MSG_MAIN_TYPE_E */
54 MSG_SUB_TYPE_T subType; /**< Message sub type. See enum _MSG_SUB_TYPE_E */
55 MSG_CLASS_TYPE_T classType; /**< Message class type. See enum _MSG_CLASS_TYPE_E */
56 msg_storage_id_t storageId; /**< Indicates where the message is saved. see enum _MSG_FOLDER_TYPE_E*/
57 msg_struct_list_s *addr_list;
59 char replyAddress[MAX_PHONE_NUMBER_LEN+1]; /**< Indicates the reply address. */
60 char subject[MAX_SUBJECT_LEN+1]; /**< Indicates the message subject. */
61 time_t displayTime; /**< Indicates the display time related to the specific operation. */
62 time_t scheduledTime; /**< Indicates the time to send scheduled message. */
63 msg_network_status_t networkStatus; /**< Indicates the network status of the message. */
64 msg_encode_type_t encodeType; /**< Indicates the string encoding type. */
65 bool bRead; /**< Indicates whether the message is read or not. */
66 bool bProtected; /**< Indicates whether the message is protected or not. */
67 bool bBackup; /**< Indicates whether the message was restored from PC. */
68 msg_priority_type_t priority; /**< Indicates the priority of the message. */
69 msg_direction_type_t direction; /**< Indicates whether the message is MO or MT, affecting address. */
70 bool bPortValid; /**< Indicates whether port information is used or not. */
71 unsigned short dstPort; /**< Recipient port number, not greater than 16 bit */
72 unsigned short srcPort; /**< Sender port number, not greater than 16 bit */
73 int attachCount; /**< Indicates the count of attached files in mms. */
74 char thumbPath[MSG_FILEPATH_LEN_MAX+1];
75 size_t dataSize; /**< Indicates the data size. The unit is byte. */
76 void *pData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
77 void *pMmsData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
79 } MSG_MESSAGE_HIDDEN_S;
83 * @brief Represents message information for thread view.
87 msg_thread_id_t threadId; /**< Indicates the thread ID of this peer. */
88 char threadName[MAX_THREAD_NAME_LEN+1]; /**< Indicates the name of this peer. > */
89 MSG_MAIN_TYPE_T mainType; /**< Indicates the latest msg main type. */
90 MSG_SUB_TYPE_T subType; /**< Indicates the latest msg sub type. */
91 char threadData[MAX_THREAD_DATA_LEN+1]; /**< Indicates the latest msg data. */
92 time_t threadTime; /**< Indicates the latest msg time. */
93 msg_direction_type_t direction; /**< Indicates whether the message is MO or MT (affecting address). */
94 int unreadCnt; /**< Indicates the unread messages from the Peer. */
95 int smsCnt; /**< Indicates the SMS messages from the Peer. */
96 int mmsCnt; /**< Indicates the MMS messages from the Peer. */
97 bool bProtected; /**< Indicates whether the thread includes protected messages. */
100 enum _MSG_MAIN_TYPE_E
102 MSG_UNKNOWN_TYPE = 0, /**< Unknown main type */
103 MSG_SMS_TYPE, /**< SMS */
104 MSG_MMS_TYPE, /**< MMS */
111 /* SMS Specific Message Type */
112 MSG_NORMAL_SMS = 0, /**< Text SMS message */
113 MSG_CB_SMS, /**< Cell Broadcasting message */
114 MSG_JAVACB_SMS, /**< JAVA Cell Broadcasting message */
115 MSG_TYPE0_SMS, /**< Short Message Type 0 */
116 MSG_REPLACE_TYPE1_SMS, /**< Replace Short Message Type 1 */
117 MSG_REPLACE_TYPE2_SMS, /**< Replace Short Message Type 2 */
118 MSG_REPLACE_TYPE3_SMS, /**< Replace Short Message Type 3 */
119 MSG_REPLACE_TYPE4_SMS, /**< Replace Short Message Type 4 */
120 MSG_REPLACE_TYPE5_SMS, /**< Replace Short Message Type 5 */
121 MSG_REPLACE_TYPE6_SMS, /**< Replace Short Message Type 6 */
122 MSG_REPLACE_TYPE7_SMS, /**< Replace Short Message Type 7 */
123 MSG_WAP_SI_SMS, /**< WAP Push Message SI */
124 MSG_WAP_SL_SMS, /**< WAP Push Message SL */
125 MSG_WAP_CO_SMS, /**< WAP Push Message CO */
126 MSG_MWI_VOICE_SMS, /**< MWI Message Voice */
127 MSG_MWI_FAX_SMS, /**< MWI Message Fax */
128 MSG_MWI_EMAIL_SMS, /**< MWI Message Email */
129 MSG_MWI_OTHER_SMS, /**< MWI Message Other */
130 MSG_STATUS_REPORT_SMS, /**< SMS-STATUS-REPORT */
131 MSG_SYNCML_CP, /**< SyncML Message CP */
132 MSG_LBS_SMS, /**< LBS Message */
133 MSG_SOS_SMS, /**< SOS Message */
134 MSG_REJECT_SMS, /**< Reject Message */
135 MSG_CONCAT_SIM_SMS, /**< Concatenated Message in SIM */
137 /* MMS Specific Message Type */
138 MSG_SENDREQ_MMS = 24, /**< MMS Send Request message */
139 MSG_SENDCONF_MMS, /**< MMS Send Confirm message */
140 MSG_NOTIFICATIONIND_MMS, /**< MMS Notification Indication message */
141 MSG_GET_MMS, /**< MMS GET MMS message */
142 MSG_NOTIFYRESPIND_MMS, /**< MMS Notify Response Indication message */
143 MSG_RETRIEVE_MMS, /**< MMS Retrive MMS message */
144 MSG_RETRIEVE_AUTOCONF_MMS, /**< MMS Retrieve Confirm message by auto retrieving*/
145 MSG_RETRIEVE_MANUALCONF_MMS, /**< MMS Retrieve Confirm message by manual retrieving*/
146 MSG_ACKNOWLEGEIND_MMS, /**< MMS Acknowledge Indication message */
147 MSG_DELIVERYIND_MMS, /**< MMS Delivery Indication message */
148 MSG_READRECIND_MMS, /**< MMS Read Receive Indication message */
149 MSG_READORGIND_MMS, /**< MMS Read Origin Indication message */
150 MSG_FORWARD_MMS, /**< MMS Forward message */
151 MSG_FORWARDREQ_MMS, /**< MMS Forward Request message */
152 MSG_FORWARDCONF_MMS, /**< MMS Forward Confirm message */
153 MSG_READREPLY_MMS, /**< MMS Read Reply message */
154 MSG_SENDREQ_JAVA_MMS, /**< MMS Send Request message for JAVA MMS */
161 MessageStorageReader::MessageStorageReader() {
162 // TODO Auto-generated constructor stub
166 MessageStorageReader::~MessageStorageReader() {
167 // TODO Auto-generated destructor stub
170 msg_error_t MessageStorageReader::MsgStoConnectDB(){
174 msg_error_t MessageStorageReader::MsgStoDisconnectDB(){
175 if (dbHandle.disconnect() != MSG_SUCCESS){
176 LogError(">>>[ERROR] DB Disconnect Fail");
177 return MSG_ERR_DB_DISCONNECT;
186 // MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int offset, int limit, msg_struct_list_s *pMsgList)
187 int MessageStorageReader::queryMessage(const std::string& whereClause, const std::string& orderLimit, /*out*/msg_struct_list_s *pMsgList){
188 LogDebug("<<< whereClause:[" << whereClause << "]");
190 // Clear Out Parameter
191 pMsgList->nCount = 0;
192 pMsgList->msg_struct_info = NULL;
195 int index = 26; // numbers of index
197 char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
198 char displayName[MAX_DISPLAY_NAME_LEN+1];
200 std::ostringstream oStream;
201 std::string stringSqlQuery;
202 std::string tmpWhereClause;
205 // int order = MsgGetContactNameOrder();
208 oStream << "SELECT A.MSG_ID, A.CONV_ID, A.FOLDER_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
209 A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
210 A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
211 B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
212 FROM " << MSGFW_MESSAGE_TABLE_NAME << " A, " << MSGFW_ADDRESS_TABLE_NAME << " B ";
215 if(whereClause.length()==0){
216 tmpWhereClause.append("WHERE ");
218 tmpWhereClause.append(whereClause);
219 tmpWhereClause.append(" ");
220 tmpWhereClause.append("AND ");
223 tmpWhereClause.append("(A.CONV_ID = B.CONV_ID AND B.ADDRESS_ID <> 0) ");
225 if(orderLimit.length() == 0)
227 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
231 tmpWhereClause.append(orderLimit);
232 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
234 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
237 oStream << tmpWhereClause;
238 stringSqlQuery = oStream.str();
240 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
241 msg_error_t err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
243 if (err == MSG_ERR_DB_NORECORD) {
244 LogDebug("MSG_ERR_DB_NORECORD");
245 dbHandle.freeTable();
248 } else if (err != MSG_SUCCESS) {
249 LogDebug("Get table fail. "<< stringSqlQuery);
251 dbHandle.freeTable();
256 pMsgList->nCount = rowCnt;
258 LogDebug("pMsgList->nCount ="<< pMsgList->nCount);
260 pMsgList->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t) * rowCnt];
262 MSG_MESSAGE_HIDDEN_S *pTmp = NULL;
263 msg_struct_s *msg = NULL;
265 for (int i = 0; i < rowCnt; i++) {
266 pMsgList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
268 msg = (msg_struct_s *)pMsgList->msg_struct_info[i];
270 msg->type = MSG_STRUCT_MESSAGE_INFO;
271 msg->data = (int *)new char[sizeof(MSG_MESSAGE_HIDDEN_S)];
273 pTmp = (MSG_MESSAGE_HIDDEN_S *)msg->data;
275 memset(pTmp, 0x00, sizeof(MSG_MESSAGE_HIDDEN_S));
278 pTmp->pMmsData = NULL;
280 pTmp->msgId = dbHandle.getColumnToInt(index++);
281 pTmp->threadId = dbHandle.getColumnToInt(index++);
282 pTmp->folderId = dbHandle.getColumnToInt(index++);
283 pTmp->storageId = dbHandle.getColumnToInt(index++);
284 pTmp->mainType = dbHandle.getColumnToInt(index++);
285 pTmp->subType = dbHandle.getColumnToInt(index++);
286 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
287 pTmp->dataSize = dbHandle.getColumnToInt(index++);
288 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
289 pTmp->bRead = dbHandle.getColumnToInt(index++);
290 pTmp->bProtected = dbHandle.getColumnToInt(index++);
291 pTmp->bBackup = dbHandle.getColumnToInt(index++);
292 pTmp->priority = dbHandle.getColumnToInt(index++);
293 pTmp->direction= dbHandle.getColumnToInt(index++);
294 pTmp->addressList = NULL;
296 pTmp->scheduledTime = (time_t)dbHandle.getColumnToInt(index++);
298 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
300 if (pTmp->mainType == MSG_MMS_TYPE &&
301 (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->subType == MSG_NOTIFICATIONIND_MMS)) {
305 LogDebug("pTmp->dataSize = "<< pTmp->dataSize);
306 pTmp->pData = (void *)new char[pTmp->dataSize + 2];
307 memset(pTmp->pData, 0x00, pTmp->dataSize + 2);
309 dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char *)pTmp->pData);
312 msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
313 msg_struct_s *addr_info = NULL;
314 MSG_ADDRESS_INFO_S *address = NULL;
316 addr_list->nCount = 1;
317 addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t *)*MAX_TO_ADDRESS_CNT];
319 msg_struct_s *pTmpAddr = NULL;
321 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
322 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
323 pTmpAddr = (msg_struct_s *)addr_list->msg_struct_info[i];
324 pTmpAddr->type = MSG_STRUCT_ADDRESS_INFO;
325 pTmpAddr->data = new MSG_ADDRESS_INFO_S;
326 memset(pTmpAddr->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
328 addr_list->msg_struct_info[i] = (msg_struct_t)pTmpAddr;
331 addr_info = (msg_struct_s *)addr_list->msg_struct_info[0];
332 address = (MSG_ADDRESS_INFO_S *)addr_info->data;
333 address->addressType = dbHandle.getColumnToInt(index++);
334 address->recipientType = dbHandle.getColumnToInt(index++);
335 address->contactId = dbHandle.getColumnToInt(index++);
337 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, address->addressVal);
339 memset(displayName, 0x00, sizeof(displayName));
340 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
342 memset(firstName, 0x00, sizeof(firstName));
343 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
345 memset(lastName, 0x00, sizeof(lastName));
346 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
348 if (strlen(displayName) <= 0) {
350 if (firstName[0] != '\0') {
351 strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
354 if (lastName[0] != '\0') {
355 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
356 strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
360 else if (order == 1) {
361 if (lastName[0] != '\0') {
362 strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
363 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
366 if (firstName[0] != '\0') {
367 strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
373 strncpy(address->displayName, displayName, MAX_DISPLAY_NAME_LEN);
375 pTmp->addr_list = addr_list;
377 // For GList *addressList
378 msg_struct_s *addrStruct = NULL;
379 MSG_ADDRESS_INFO_S *addrInfo = NULL;
381 addrStruct = new msg_struct_s;
382 memset(addrStruct, 0x00, sizeof(msg_struct_s));
384 addrStruct->type = MSG_STRUCT_ADDRESS_INFO;
385 addrStruct->data = new MSG_ADDRESS_INFO_S;
386 memset(addrStruct->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
388 addrInfo = (MSG_ADDRESS_INFO_S *)addrStruct->data;
390 addrInfo->addressType = address->addressType;
391 addrInfo->recipientType = address->recipientType;
392 addrInfo->contactId = address->contactId;
393 strncpy(addrInfo->addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
394 strncpy(addrInfo->displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
395 addrInfo->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
397 pTmp->addressList = g_list_append(pTmp->addressList, addrStruct);
399 pTmp->attachCount = dbHandle.getColumnToInt(index++);
401 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
404 dbHandle.freeTable();
410 //msg_error_t MsgStoGetThreadViewList(const MSG_SORT_RULE_S *pSortRule, msg_struct_list_s *pThreadViewList)
411 int MessageStorageReader::queryConversation(const std::string& whereClause, const std::string& orderLimit,
412 msg_struct_list_s *pThreadViewList){
414 pThreadViewList->nCount = 0;
415 pThreadViewList->msg_struct_info = NULL;
418 int index = 10; // numbers of index
420 std::ostringstream oStream;
421 std::string stringSqlQuery;
423 std::string tmpWhereClause;
424 LogDebug("<<< whereClause:[" << whereClause << "]");
425 if(whereClause.length()==0){
426 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
427 tmpWhereClause.append("WHERE (B.SMS_CNT > 0 OR B.MMS_CNT > 0) AND A.CONV_ID = B.CONV_ID ");
428 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
430 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
431 tmpWhereClause.append(whereClause);
432 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
435 if(orderLimit.length() == 0)
437 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
441 tmpWhereClause.append(orderLimit);
442 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
445 oStream << "SELECT B.CONV_ID, B.UNREAD_CNT, B.SMS_CNT, B.MMS_CNT, \
446 B.MAIN_TYPE, B.SUB_TYPE, B.MSG_DIRECTION, B.DISPLAY_TIME, B.DISPLAY_NAME, B.MSG_TEXT \
447 FROM " << MSGFW_ADDRESS_TABLE_NAME << " A, " << MSGFW_CONVERSATION_TABLE_NAME << " B ";
449 oStream << tmpWhereClause;
450 stringSqlQuery = oStream.str();
451 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
453 msg_error_t err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
455 if (err == MSG_ERR_DB_NORECORD) {
456 dbHandle.freeTable();
458 } else if (err != MSG_SUCCESS) {
459 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
460 dbHandle.freeTable();
465 LogDebug("rowCnt is %d"<< rowCnt);
466 dbHandle.freeTable();
470 pThreadViewList->nCount = rowCnt;
472 LogDebug("pThreadViewList->nCount [" << pThreadViewList->nCount << "]");
474 pThreadViewList->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t)*rowCnt];
476 MSG_THREAD_VIEW_S *pTmp = NULL;
477 msg_struct_s *thread_t = NULL;
479 for (int i = 0; i < rowCnt; i++) {
480 thread_t = (msg_struct_s *)new msg_struct_s;
481 pThreadViewList->msg_struct_info[i] = (msg_struct_t)thread_t;
483 thread_t->type = MSG_STRUCT_THREAD_INFO;
484 thread_t->data = new MSG_THREAD_VIEW_S;
486 pTmp = (MSG_THREAD_VIEW_S *)thread_t->data;
487 memset(pTmp, 0x00, sizeof(MSG_THREAD_VIEW_S));
489 pTmp->threadId = dbHandle.getColumnToInt(index++);
491 pTmp->unreadCnt = dbHandle.getColumnToInt(index++);
492 pTmp->smsCnt = dbHandle.getColumnToInt(index++);
493 pTmp->mmsCnt = dbHandle.getColumnToInt(index++);
495 pTmp->mainType = dbHandle.getColumnToInt(index++);
496 pTmp->subType = dbHandle.getColumnToInt(index++);
498 pTmp->direction = dbHandle.getColumnToInt(index++);
499 pTmp->threadTime = (time_t)dbHandle.getColumnToInt(index++);
501 memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
502 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, pTmp->threadName);
504 memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
505 dbHandle.getColumnToString(index++, MAX_THREAD_DATA_LEN, pTmp->threadData);
508 dbHandle.freeTable();