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;
58 char replyAddress[MAX_PHONE_NUMBER_LEN+1]; /**< Indicates the reply address. */
59 char subject[MAX_SUBJECT_LEN+1]; /**< Indicates the message subject. */
60 time_t displayTime; /**< Indicates the display time related to the specific operation. */
61 msg_network_status_t networkStatus; /**< Indicates the network status of the message. */
62 msg_encode_type_t encodeType; /**< Indicates the string encoding type. */
63 bool bRead; /**< Indicates whether the message is read or not. */
64 bool bProtected; /**< Indicates whether the message is protected or not. */
65 bool bBackup; /**< Indicates whether the message was restored from PC. */
66 msg_priority_type_t priority; /**< Indicates the priority of the message. */
67 msg_direction_type_t direction; /**< Indicates whether the message is MO or MT, affecting address. */
68 bool bPortValid; /**< Indicates whether port information is used or not. */
69 unsigned short dstPort; /**< Recipient port number, not greater than 16 bit */
70 unsigned short srcPort; /**< Sender port number, not greater than 16 bit */
71 int attachCount; /**< Indicates the count of attached files in mms. */
72 char thumbPath[MSG_FILEPATH_LEN_MAX+1];
73 size_t dataSize; /**< Indicates the data size. The unit is byte. */
74 void *pData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
75 void *pMmsData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
76 } MSG_MESSAGE_HIDDEN_S;
80 * @brief Represents message information for thread view.
84 msg_thread_id_t threadId; /**< Indicates the thread ID of this peer. */
85 char threadName[MAX_THREAD_NAME_LEN+1]; /**< Indicates the name of this peer. > */
86 MSG_MAIN_TYPE_T mainType; /**< Indicates the latest msg main type. */
87 MSG_SUB_TYPE_T subType; /**< Indicates the latest msg sub type. */
88 char threadData[MAX_THREAD_DATA_LEN+1]; /**< Indicates the latest msg data. */
89 time_t threadTime; /**< Indicates the latest msg time. */
90 msg_direction_type_t direction; /**< Indicates whether the message is MO or MT (affecting address). */
91 int unreadCnt; /**< Indicates the unread messages from the Peer. */
92 int smsCnt; /**< Indicates the SMS messages from the Peer. */
93 int mmsCnt; /**< Indicates the MMS messages from the Peer. */
94 bool bProtected; /**< Indicates whether the thread includes protected messages. */
99 MSG_UNKNOWN_TYPE = 0, /**< Unknown main type */
100 MSG_SMS_TYPE, /**< SMS */
101 MSG_MMS_TYPE, /**< MMS */
108 /* SMS Specific Message Type */
109 MSG_NORMAL_SMS = 0, /**< Text SMS message */
110 MSG_CB_SMS, /**< Cell Broadcasting message */
111 MSG_JAVACB_SMS, /**< JAVA Cell Broadcasting message */
112 MSG_TYPE0_SMS, /**< Short Message Type 0 */
113 MSG_REPLACE_TYPE1_SMS, /**< Replace Short Message Type 1 */
114 MSG_REPLACE_TYPE2_SMS, /**< Replace Short Message Type 2 */
115 MSG_REPLACE_TYPE3_SMS, /**< Replace Short Message Type 3 */
116 MSG_REPLACE_TYPE4_SMS, /**< Replace Short Message Type 4 */
117 MSG_REPLACE_TYPE5_SMS, /**< Replace Short Message Type 5 */
118 MSG_REPLACE_TYPE6_SMS, /**< Replace Short Message Type 6 */
119 MSG_REPLACE_TYPE7_SMS, /**< Replace Short Message Type 7 */
120 MSG_WAP_SI_SMS, /**< WAP Push Message SI */
121 MSG_WAP_SL_SMS, /**< WAP Push Message SL */
122 MSG_WAP_CO_SMS, /**< WAP Push Message CO */
123 MSG_MWI_VOICE_SMS, /**< MWI Message Voice */
124 MSG_MWI_FAX_SMS, /**< MWI Message Fax */
125 MSG_MWI_EMAIL_SMS, /**< MWI Message Email */
126 MSG_MWI_OTHER_SMS, /**< MWI Message Other */
127 MSG_STATUS_REPORT_SMS, /**< SMS-STATUS-REPORT */
128 MSG_SYNCML_CP, /**< SyncML Message CP */
129 MSG_LBS_SMS, /**< LBS Message */
130 MSG_REJECT_SMS, /**< Reject Message */
131 MSG_CONCAT_SIM_SMS, /**< Concatenated Message in SIM */
133 /* MMS Specific Message Type */
134 MSG_SENDREQ_MMS = 24, /**< MMS Send Request message */
135 MSG_SENDCONF_MMS, /**< MMS Send Confirm message */
136 MSG_NOTIFICATIONIND_MMS, /**< MMS Notification Indication message */
137 MSG_GET_MMS, /**< MMS GET MMS message */
138 MSG_NOTIFYRESPIND_MMS, /**< MMS Notify Response Indication message */
139 MSG_RETRIEVE_MMS, /**< MMS Retrive MMS message */
140 MSG_RETRIEVE_AUTOCONF_MMS, /**< MMS Retrieve Confirm message by auto retrieving*/
141 MSG_RETRIEVE_MANUALCONF_MMS, /**< MMS Retrieve Confirm message by manual retrieving*/
142 MSG_ACKNOWLEGEIND_MMS, /**< MMS Acknowledge Indication message */
143 MSG_DELIVERYIND_MMS, /**< MMS Delivery Indication message */
144 MSG_READRECIND_MMS, /**< MMS Read Receive Indication message */
145 MSG_READORGIND_MMS, /**< MMS Read Origin Indication message */
146 MSG_FORWARD_MMS, /**< MMS Forward message */
147 MSG_FORWARDREQ_MMS, /**< MMS Forward Request message */
148 MSG_FORWARDCONF_MMS, /**< MMS Forward Confirm message */
149 MSG_READREPLY_MMS, /**< MMS Read Reply message */
150 MSG_SENDREQ_JAVA_MMS, /**< MMS Send Request message for JAVA MMS */
157 MessageStorageReader::MessageStorageReader() {
158 // TODO Auto-generated constructor stub
162 MessageStorageReader::~MessageStorageReader() {
163 // TODO Auto-generated destructor stub
166 msg_error_t MessageStorageReader::MsgStoConnectDB(){
170 msg_error_t MessageStorageReader::MsgStoDisconnectDB(){
171 if (dbHandle.disconnect() != MSG_SUCCESS){
172 LogError(">>>[ERROR] DB Disconnect Fail");
173 return MSG_ERR_DB_DISCONNECT;
182 // MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int offset, int limit, msg_struct_list_s *pMsgList)
183 int MessageStorageReader::queryMessage(const std::string& whereClause, const std::string& orderLimit, /*out*/msg_struct_list_s *pMsgList){
184 LogDebug("<<< whereClause:[" << whereClause << "]");
186 // Clear Out Parameter
187 pMsgList->nCount = 0;
188 pMsgList->msg_struct_info = NULL;
191 int index = 26; // numbers of index
193 char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
194 char displayName[MAX_DISPLAY_NAME_LEN+1];
196 std::ostringstream oStream;
197 std::string stringSqlQuery;
198 std::string tmpWhereClause;
201 // int order = MsgGetContactNameOrder();
204 oStream << "SELECT A.MSG_ID, A.CONV_ID, A.FOLDER_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
205 A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
206 A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
207 B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
208 FROM " << MSGFW_MESSAGE_TABLE_NAME << " A, " << MSGFW_ADDRESS_TABLE_NAME << " B ";
211 if(whereClause.length()==0){
212 tmpWhereClause.append("WHERE ");
214 tmpWhereClause.append(whereClause);
215 tmpWhereClause.append(" ");
216 tmpWhereClause.append("AND ");
219 tmpWhereClause.append("(A.CONV_ID = B.CONV_ID AND B.ADDRESS_ID <> 0) ");
221 if(orderLimit.length() == 0)
223 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
227 tmpWhereClause.append(orderLimit);
228 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
230 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
233 oStream << tmpWhereClause;
234 stringSqlQuery = oStream.str();
236 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
237 msg_error_t err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
239 if (err == MSG_ERR_DB_NORECORD) {
240 LogDebug("MSG_ERR_DB_NORECORD");
241 dbHandle.freeTable();
244 } else if (err != MSG_SUCCESS) {
245 LogDebug("Get table fail. "<< stringSqlQuery);
247 dbHandle.freeTable();
252 pMsgList->nCount = rowCnt;
254 LogDebug("pMsgList->nCount ="<< pMsgList->nCount);
256 pMsgList->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t) * rowCnt];
258 MSG_MESSAGE_HIDDEN_S *pTmp = NULL;
259 msg_struct_s *msg = NULL;
261 for (int i = 0; i < rowCnt; i++) {
262 pMsgList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
264 msg = (msg_struct_s *)pMsgList->msg_struct_info[i];
266 msg->type = MSG_STRUCT_MESSAGE_INFO;
267 msg->data = (int *)new char[sizeof(MSG_MESSAGE_HIDDEN_S)];
269 pTmp = (MSG_MESSAGE_HIDDEN_S *)msg->data;
271 memset(pTmp, 0x00, sizeof(MSG_MESSAGE_HIDDEN_S));
274 pTmp->pMmsData = NULL;
276 pTmp->msgId = dbHandle.getColumnToInt(index++);
277 pTmp->threadId = dbHandle.getColumnToInt(index++);
278 pTmp->folderId = dbHandle.getColumnToInt(index++);
279 pTmp->storageId = dbHandle.getColumnToInt(index++);
280 pTmp->mainType = dbHandle.getColumnToInt(index++);
281 pTmp->subType = dbHandle.getColumnToInt(index++);
282 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
283 pTmp->dataSize = dbHandle.getColumnToInt(index++);
284 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
285 pTmp->bRead = dbHandle.getColumnToInt(index++);
286 pTmp->bProtected = dbHandle.getColumnToInt(index++);
287 pTmp->bBackup = dbHandle.getColumnToInt(index++);
288 pTmp->priority = dbHandle.getColumnToInt(index++);
289 pTmp->direction= dbHandle.getColumnToInt(index++);
290 index++; // This field is reserved.
292 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
294 if (pTmp->mainType == MSG_MMS_TYPE &&
295 (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->subType == MSG_NOTIFICATIONIND_MMS)) {
299 LogDebug("pTmp->dataSize = "<< pTmp->dataSize);
300 pTmp->pData = (void *)new char[pTmp->dataSize + 2];
301 memset(pTmp->pData, 0x00, pTmp->dataSize + 2);
303 dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char *)pTmp->pData);
306 msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
307 msg_struct_s *addr_info = NULL;
308 MSG_ADDRESS_INFO_S *address = NULL;
310 addr_list->nCount = 1;
311 addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t *)*MAX_TO_ADDRESS_CNT];
313 msg_struct_s *pTmpAddr = NULL;
315 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
316 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
317 pTmpAddr = (msg_struct_s *)addr_list->msg_struct_info[i];
318 pTmpAddr->type = MSG_STRUCT_ADDRESS_INFO;
319 pTmpAddr->data = new MSG_ADDRESS_INFO_S;
320 memset(pTmpAddr->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
322 addr_list->msg_struct_info[i] = (msg_struct_t)pTmpAddr;
325 addr_info = (msg_struct_s *)addr_list->msg_struct_info[0];
326 address = (MSG_ADDRESS_INFO_S *)addr_info->data;
327 address->addressType = dbHandle.getColumnToInt(index++);
328 address->recipientType = dbHandle.getColumnToInt(index++);
329 address->contactId = dbHandle.getColumnToInt(index++);
331 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, address->addressVal);
333 memset(displayName, 0x00, sizeof(displayName));
334 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
336 memset(firstName, 0x00, sizeof(firstName));
337 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
339 memset(lastName, 0x00, sizeof(lastName));
340 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
342 if (strlen(displayName) <= 0) {
344 if (firstName[0] != '\0') {
345 strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
348 if (lastName[0] != '\0') {
349 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
350 strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
354 else if (order == 1) {
355 if (lastName[0] != '\0') {
356 strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
357 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
360 if (firstName[0] != '\0') {
361 strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
367 strncpy(address->displayName, displayName, MAX_DISPLAY_NAME_LEN);
369 pTmp->addr_list = addr_list;
371 pTmp->attachCount = dbHandle.getColumnToInt(index++);
373 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
376 dbHandle.freeTable();
382 //msg_error_t MsgStoGetThreadViewList(const MSG_SORT_RULE_S *pSortRule, msg_struct_list_s *pThreadViewList)
383 int MessageStorageReader::queryConversation(const std::string& whereClause, const std::string& orderLimit,
384 msg_struct_list_s *pThreadViewList){
386 pThreadViewList->nCount = 0;
387 pThreadViewList->msg_struct_info = NULL;
390 int index = 10; // numbers of index
392 std::ostringstream oStream;
393 std::string stringSqlQuery;
395 std::string tmpWhereClause;
396 LogDebug("<<< whereClause:[" << whereClause << "]");
397 if(whereClause.length()==0){
398 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
399 tmpWhereClause.append("WHERE (B.SMS_CNT > 0 OR B.MMS_CNT > 0) AND A.CONV_ID = B.CONV_ID ");
400 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
402 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
403 tmpWhereClause.append(whereClause);
404 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
407 if(orderLimit.length() == 0)
409 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
413 tmpWhereClause.append(orderLimit);
414 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
417 oStream << "SELECT B.CONV_ID, B.UNREAD_CNT, B.SMS_CNT, B.MMS_CNT, \
418 B.MAIN_TYPE, B.SUB_TYPE, B.MSG_DIRECTION, B.DISPLAY_TIME, B.DISPLAY_NAME, B.MSG_TEXT \
419 FROM " << MSGFW_ADDRESS_TABLE_NAME << " A, " << MSGFW_CONVERSATION_TABLE_NAME << " B ";
421 oStream << tmpWhereClause;
422 stringSqlQuery = oStream.str();
423 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
425 msg_error_t err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
427 if (err == MSG_ERR_DB_NORECORD) {
428 dbHandle.freeTable();
430 } else if (err != MSG_SUCCESS) {
431 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
432 dbHandle.freeTable();
437 LogDebug("rowCnt is %d"<< rowCnt);
438 dbHandle.freeTable();
442 pThreadViewList->nCount = rowCnt;
444 LogDebug("pThreadViewList->nCount [" << pThreadViewList->nCount << "]");
446 pThreadViewList->msg_struct_info = (msg_struct_t *)new char[sizeof(msg_struct_t)*rowCnt];
448 MSG_THREAD_VIEW_S *pTmp = NULL;
449 msg_struct_s *thread_t = NULL;
451 for (int i = 0; i < rowCnt; i++) {
452 thread_t = (msg_struct_s *)new msg_struct_s;
453 pThreadViewList->msg_struct_info[i] = (msg_struct_t)thread_t;
455 thread_t->type = MSG_STRUCT_THREAD_INFO;
456 thread_t->data = new MSG_THREAD_VIEW_S;
458 pTmp = (MSG_THREAD_VIEW_S *)thread_t->data;
459 memset(pTmp, 0x00, sizeof(MSG_THREAD_VIEW_S));
461 pTmp->threadId = dbHandle.getColumnToInt(index++);
463 pTmp->unreadCnt = dbHandle.getColumnToInt(index++);
464 pTmp->smsCnt = dbHandle.getColumnToInt(index++);
465 pTmp->mmsCnt = dbHandle.getColumnToInt(index++);
467 pTmp->mainType = dbHandle.getColumnToInt(index++);
468 pTmp->subType = dbHandle.getColumnToInt(index++);
470 pTmp->direction = dbHandle.getColumnToInt(index++);
471 pTmp->threadTime = (time_t)dbHandle.getColumnToInt(index++);
473 memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
474 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, pTmp->threadName);
476 memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
477 dbHandle.getColumnToString(index++, MAX_THREAD_DATA_LEN, pTmp->threadData);
480 dbHandle.freeTable();