2 * Copyright (c) 2011 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.
17 * MessageStorageReader.cpp
19 * Created on: 2011. 11. 16.
23 #include "MessageStorageReader.h"
24 #include "MsgSqliteWrapper.h"
26 #include <dpl/log/log.h>
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;
35 MSG_MAIN_TYPE_T mainType; /**< Message main type. See enum _MSG_MAIN_TYPE_E */
36 MSG_SUB_TYPE_T subType; /**< Message sub type. See enum _MSG_SUB_TYPE_E */
37 MSG_CLASS_TYPE_T classType; /**< Message class type. See enum _MSG_CLASS_TYPE_E */
41 MSG_MESSAGE_ID_T msgId; /**< Indicates the message ID of this message. */
42 MSG_FOLDER_ID_T folderId; /**< Indicates the folder ID. see enum _MSG_FOLDER_TYPE_E */
43 MSG_REFERENCE_ID_T referenceId; /**< Indicates group message of messages that is sent together. */
44 MSG_MESSAGE_TYPE_S msgType; /**< Indicates the message type either of SMS or MMS */
45 MSG_STORAGE_ID_T storageId; /**< Indicates where the message is saved. see enum _MSG_FOLDER_TYPE_E*/
46 int nAddressCnt; /**< Indicates the count of addresses. */
47 MSG_ADDRESS_INFO_S addressList[MAX_TO_ADDRESS_CNT]; /**< Indicates the address information list. */
48 char replyAddress[MAX_PHONE_NUMBER_LEN+1]; /**< Indicates the reply address. */
49 char subject[MAX_SUBJECT_LEN+1]; /**< Indicates the message subject. */
50 time_t displayTime; /**< Indicates the display time related to the specific operation. */
51 time_t scheduledTime; /**< Indicates the time to send scheduled message. */
52 MSG_NETWORK_STATUS_T networkStatus; /**< Indicates the network status of the message. */
53 MSG_ENCODE_TYPE_T encodeType; /**< Indicates the string encoding type. */
54 bool bRead; /**< Indicates whether the message is read or not. */
55 bool bProtected; /**< Indicates whether the message is protected or not. */
56 bool bBackup; /**< Indicates whether the message was restored from PC. */
57 MSG_PRIORITY_TYPE_T priority; /**< Indicates the priority of the message. */
58 MSG_DIRECTION_TYPE_T direction; /**< Indicates whether the message is MO or MT, affecting address. */
59 MSG_PORT_INFO_S msgPort; /**< Indicates the port number information. */
60 int attachCount; /**< Indicates the count of attached files in mms. */
61 char thumbPath[MSG_FILEPATH_LEN_MAX];
62 size_t dataSize; /**< Indicates the data size. The unit is byte. */
63 void *pData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
64 void *pMmsData; /**< Indicates the message payload information as a body. default character encoding is UTF-8*/
69 MSG_THREAD_ID_T threadId; /**< Indicates the thread ID of this peer. */
70 char threadAddr[MAX_THREAD_ADDR_LEN+1]; /**< Indicates the address of this peer. > */
71 char threadName[MAX_THREAD_NAME_LEN+1]; /**< Indicates the name of this peer. > */
72 char threadImagePath[MAX_IMAGE_PATH_LEN+1]; /**< Indicates the image path of this peer */
73 MSG_MESSAGE_TYPE_S threadType; /**< Indicates the latest msg type. */
74 char threadData[MAX_THREAD_DATA_LEN+1]; /**< Indicates the latest msg data. */
75 time_t threadTime; /**< Indicates the latest msg time. */
76 MSG_DIRECTION_TYPE_T direction; /**< Indicates whether the message is MO or MT (affecting address). */
78 MSG_CONTACT_ID_T contactId; /**< Indicates the unique contact ID. */
80 int unreadCnt; /**< Indicates the unread messages from the Peer. */
81 int smsCnt; /**< Indicates the SMS messages from the Peer. */
82 int mmsCnt; /**< Indicates the MMS messages from the Peer. */
87 MSG_UNKNOWN_TYPE = 0, /**< Unknown main type */
88 MSG_SMS_TYPE, /**< SMS */
89 MSG_MMS_TYPE, /**< MMS */
95 /* SMS Specific Message Type */
96 MSG_NORMAL_SMS = 0, /**< Text SMS message */
97 MSG_CB_SMS, /**< Cell Broadcasting message */
98 MSG_JAVACB_SMS, /**< JAVA Cell Broadcasting message */
99 MSG_TYPE0_SMS, /**< Short Message Type 0 */
100 MSG_REPLACE_TYPE1_SMS, /**< Replace Short Message Type 1 */
101 MSG_REPLACE_TYPE2_SMS, /**< Replace Short Message Type 2 */
102 MSG_REPLACE_TYPE3_SMS, /**< Replace Short Message Type 3 */
103 MSG_REPLACE_TYPE4_SMS, /**< Replace Short Message Type 4 */
104 MSG_REPLACE_TYPE5_SMS, /**< Replace Short Message Type 5 */
105 MSG_REPLACE_TYPE6_SMS, /**< Replace Short Message Type 6 */
106 MSG_REPLACE_TYPE7_SMS, /**< Replace Short Message Type 7 */
107 MSG_WAP_SI_SMS, /**< WAP Push Message SI */
108 MSG_WAP_SL_SMS, /**< WAP Push Message SL */
109 MSG_WAP_CO_SMS, /**< WAP Push Message CO */
110 MSG_MWI_VOICE_SMS, /**< MWI Message Voice */
111 MSG_MWI_FAX_SMS, /**< MWI Message Fax */
112 MSG_MWI_EMAIL_SMS, /**< MWI Message Email */
113 MSG_MWI_OTHER_SMS, /**< MWI Message Other */
114 MSG_STATUS_REPORT_SMS, /**< SMS-STATUS-REPORT */
115 MSG_SYNCML_CP, /**< SyncML Message CP */
116 MSG_LBS_SMS, /**< LBS Message */
117 MSG_SOS_SMS, /**< SOS Message */
118 MSG_REJECT_SMS, /**< Reject Message */
119 MSG_CONCAT_SIM_SMS, /**< Concatenated Message in SIM */
121 /* MMS Specific Message Type */
122 MSG_SENDREQ_MMS = 24, /**< MMS Send Request message */
123 MSG_SENDCONF_MMS, /**< MMS Send Confirm message */
124 MSG_NOTIFICATIONIND_MMS, /**< MMS Notification Indication message */
125 MSG_GET_MMS, /**< MMS GET MMS message */
126 MSG_NOTIFYRESPIND_MMS, /**< MMS Notify Response Indication message */
127 MSG_RETRIEVE_MMS, /**< MMS Retrive MMS message */
128 MSG_RETRIEVE_AUTOCONF_MMS, /**< MMS Retrieve Confirm message by auto retrieving*/
129 MSG_RETRIEVE_MANUALCONF_MMS, /**< MMS Retrieve Confirm message by manual retrieving*/
130 MSG_ACKNOWLEGEIND_MMS, /**< MMS Acknowledge Indication message */
131 MSG_DELIVERYIND_MMS, /**< MMS Delivery Indication message */
132 MSG_READRECIND_MMS, /**< MMS Read Receive Indication message */
133 MSG_READORGIND_MMS, /**< MMS Read Origin Indication message */
134 MSG_FORWARD_MMS, /**< MMS Forward message */
135 MSG_FORWARDREQ_MMS, /**< MMS Forward Request message */
136 MSG_FORWARDCONF_MMS, /**< MMS Forward Confirm message */
137 MSG_READREPLY_MMS, /**< MMS Read Reply message */
138 MSG_SENDREQ_JAVA_MMS, /**< MMS Send Request message for JAVA MMS */
142 MessageStorageReader::MessageStorageReader() {
143 // TODO Auto-generated constructor stub
147 MessageStorageReader::~MessageStorageReader() {
148 // TODO Auto-generated destructor stub
151 MSG_ERROR_T MessageStorageReader::MsgStoConnectDB(){
155 MSG_ERROR_T MessageStorageReader::MsgStoDisconnectDB(){
156 if (dbHandle.disconnect() != MSG_SUCCESS){
157 LogError(">>>[ERROR] DB Disconnect Fail");
158 return MSG_ERR_DB_DISCONNECT;
166 int MessageStorageReader::queryMessage(const std::string& whereClause, const std::string& orderLimit, /*out*/MSG_LIST_S *pMsgFolderViewList){
167 LogDebug("<<< whereClause:[" << whereClause << "]");
169 if (pMsgFolderViewList == NULL){
170 LogDebug("pMsgFolderViewList is NULL");
171 return MSG_ERR_NULL_POINTER;
175 int index = 27; // numbers of index
177 unsigned int addrId = 0;
179 std::ostringstream oStream;
180 std::string stringSqlQuery;
181 char firstName[MAX_DISPLAY_NAME_LEN];
182 char lastName[MAX_DISPLAY_NAME_LEN];
183 char displayName[MAX_DISPLAY_NAME_LEN];
185 std::string tmpWhereClause;
186 if(whereClause.length()==0){
187 tmpWhereClause.append("WHERE ");
189 tmpWhereClause.append(whereClause);
190 tmpWhereClause.append(" ");
191 tmpWhereClause.append("AND ");
194 tmpWhereClause.append("(A.ADDRESS_ID = B.ADDRESS_ID)");
196 if(orderLimit.length() == 0)
198 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
202 tmpWhereClause.append(orderLimit);
203 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
205 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
207 oStream << "SELECT A.MSG_ID, A.ADDRESS_ID, A.FOLDER_ID, A.REFERENCE_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
208 A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
209 A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
210 B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
211 FROM " << MSGFW_MESSAGE_TABLE_NAME << " A, " << MSGFW_ADDRESS_TABLE_NAME << " B ";
213 oStream << tmpWhereClause;
214 stringSqlQuery = oStream.str();
216 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
217 MSG_ERROR_T err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
218 if (err == MSG_ERR_DB_NORECORD){
219 LogDebug("MSG_ERR_DB_NORECORD");
220 pMsgFolderViewList->nCount = 0;
221 pMsgFolderViewList->msgInfo = NULL;
222 dbHandle.freeTable();
223 LogDebug(">>> MSG_ERR_DB_NORECORD");
225 }else if ( err != MSG_SUCCESS ){
226 dbHandle.freeTable();
230 pMsgFolderViewList->nCount = rowCnt;
231 LogDebug("pMsgCommInfoList->nCount [" << pMsgFolderViewList->nCount << "]");
233 pMsgFolderViewList->msgInfo = (msg_message_t*)new char[sizeof(MSG_MESSAGE_S*)*rowCnt];
235 MSG_MESSAGE_S* pTmp = NULL;
236 for (int i = 0; i < rowCnt; i++){
237 pMsgFolderViewList->msgInfo[i] = (msg_message_t)new char[sizeof(MSG_MESSAGE_S)];
238 pTmp = (MSG_MESSAGE_S*)pMsgFolderViewList->msgInfo[i];
239 memset(pTmp, 0x00, sizeof(pTmp));
242 pTmp->pMmsData = NULL;
244 pTmp->msgId = dbHandle.getColumnToInt(index++);
245 addrId = dbHandle.getColumnToInt(index++);
246 pTmp->folderId = dbHandle.getColumnToInt(index++);
247 pTmp->referenceId = dbHandle.getColumnToInt(index++);
248 pTmp->storageId = dbHandle.getColumnToInt(index++);
249 pTmp->msgType.mainType = dbHandle.getColumnToInt(index++);
250 pTmp->msgType.subType = dbHandle.getColumnToInt(index++);
251 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
252 pTmp->dataSize = dbHandle.getColumnToInt(index++);
253 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
254 pTmp->bRead = dbHandle.getColumnToInt(index++);
255 pTmp->bProtected = dbHandle.getColumnToInt(index++);
256 pTmp->bBackup = dbHandle.getColumnToInt(index++);
257 pTmp->priority = dbHandle.getColumnToInt(index++);
258 pTmp->direction= dbHandle.getColumnToInt(index++);
259 pTmp->scheduledTime = (time_t)dbHandle.getColumnToInt(index++);
261 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
263 if (pTmp->msgType.mainType == MSG_MMS_TYPE &&
264 (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL ||
265 pTmp->msgType.subType == MSG_NOTIFICATIONIND_MMS)){
269 pTmp->pData = (void*)new char[pTmp->dataSize + 2];
270 memset(pTmp->pData, 0x00, sizeof(pTmp->pData));
272 dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char*)pTmp->pData);
274 LogDebug("pTmp->pData:[" << (char*)pTmp->pData << "]");
278 pTmp->nAddressCnt = 1;
280 pTmp->nAddressCnt = 0;
283 pTmp->addressList[0].threadId = addrId;
284 pTmp->addressList[0].addressType = dbHandle.getColumnToInt(index++);
285 pTmp->addressList[0].recipientType = dbHandle.getColumnToInt(index++);
286 pTmp->addressList[0].contactId = dbHandle.getColumnToInt(index++);
288 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressList[0].addressVal);
290 memset(displayName, 0x00, sizeof(displayName));
291 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
293 memset(firstName, 0x00, sizeof(firstName));
294 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
296 memset(lastName, 0x00, sizeof(lastName));
297 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
299 if (strlen(displayName) <= 0){
300 if (strlen(firstName) > 0){
301 strncpy(displayName, firstName, strlen(firstName));
304 if (strlen(lastName) > 0){
305 strcat(displayName, " ");
306 strncat(displayName, lastName, strlen(lastName));
310 strncpy(pTmp->addressList[0].displayName, displayName, MAX_DISPLAY_NAME_LEN);
312 pTmp->attachCount = dbHandle.getColumnToInt(index++);
314 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
317 dbHandle.freeTable();
323 int MessageStorageReader::queryConversation(const std::string& whereClause, const std::string& orderLimit,
324 /*out*/MSG_THREAD_VIEW_LIST_S *pThreadViewList){
326 pThreadViewList->nCount = 0;
327 pThreadViewList->msgThreadInfo = NULL;
330 int index = 15; // numbers of index
332 std::ostringstream oStream;
333 std::string stringSqlQuery;
335 std::string tmpWhereClause;
336 if(whereClause.length()==0){
337 tmpWhereClause.append(" WHERE ADDRESS_ID <> 0 ");
339 tmpWhereClause.append(whereClause);
342 if(orderLimit.length() == 0)
344 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
348 tmpWhereClause.append(orderLimit);
349 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
352 oStream << "SELECT ADDRESS_ID, CONTACT_ID, ADDRESS_VAL, DISPLAY_NAME, FIRST_NAME, \
353 LAST_NAME, IMAGE_PATH, UNREAD_CNT, SMS_CNT, MMS_CNT, MAIN_TYPE, SUB_TYPE, \
354 MSG_DIRECTION, MSG_TIME, MSG_TEXT FROM " << MSGFW_ADDRESS_TABLE_NAME << " ";
356 oStream << tmpWhereClause;
357 stringSqlQuery = oStream.str();
358 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
360 MSG_ERROR_T err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
362 if (err == MSG_ERR_DB_NORECORD) {
363 dbHandle.freeTable();
366 } else if (err != MSG_SUCCESS) {
367 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
369 dbHandle.freeTable();
374 char firstName[MAX_THREAD_NAME_LEN], lastName[MAX_THREAD_NAME_LEN];
375 char displayName[MAX_DISPLAY_NAME_LEN];
379 pThreadViewList->nCount = rowCnt;
381 LogDebug("pThreadViewList->nCount [" << pThreadViewList->nCount << "]");
383 pThreadViewList->msgThreadInfo = (msg_thread_view_t*) new char[sizeof(MSG_THREAD_VIEW_S*) * rowCnt];
385 MSG_THREAD_VIEW_S* pTmp = NULL;
387 for (int i = 0; i < rowCnt; i++) {
388 pThreadViewList->msgThreadInfo[i] = (msg_thread_view_t) new char[sizeof(MSG_THREAD_VIEW_S)];
390 pTmp = (MSG_THREAD_VIEW_S*) pThreadViewList->msgThreadInfo[i];
392 pTmp->threadId = dbHandle.getColumnToInt(index++);
393 pTmp->contactId = dbHandle.getColumnToInt(index++);
395 memset(pTmp->threadAddr, 0x00, sizeof(pTmp->threadAddr));
396 dbHandle.getColumnToString(index++, MAX_THREAD_ADDR_LEN, pTmp->threadAddr);
398 memset(displayName, 0x00, sizeof(displayName));
399 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
401 memset(firstName, 0x00, sizeof(firstName));
402 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
404 memset(lastName, 0x00, sizeof(lastName));
405 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
407 if (strlen(displayName) <= 0) {
409 if (strlen(firstName) > 0) {
410 strncpy(displayName, firstName, strlen(firstName));
413 if (strlen(lastName) > 0) {
414 strcat(displayName, " ");
415 strncat(displayName, lastName, strlen(lastName));
417 } else if (order == 1) {
418 if (strlen(lastName) > 0) {
419 strncpy(displayName, lastName, strlen(lastName));
420 strcat(displayName, " ");
423 if (strlen(firstName) > 0) {
424 strncat(displayName, firstName, strlen(firstName));
429 memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
430 strncpy(pTmp->threadName, displayName, MAX_THREAD_NAME_LEN);
432 memset(pTmp->threadImagePath, 0x00, sizeof(pTmp->threadImagePath));
433 dbHandle.getColumnToString(index++, MAX_IMAGE_PATH_LEN, pTmp->threadImagePath);
435 pTmp->unreadCnt = dbHandle.getColumnToInt(index++);
436 pTmp->smsCnt = dbHandle.getColumnToInt(index++);
437 pTmp->mmsCnt = dbHandle.getColumnToInt(index++);
439 pTmp->threadType.mainType = dbHandle.getColumnToInt(index++);
440 pTmp->threadType.subType = dbHandle.getColumnToInt(index++);
442 pTmp->direction = dbHandle.getColumnToInt(index++);
443 pTmp->threadTime = (time_t) dbHandle.getColumnToInt(index++);
445 memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
446 dbHandle.getColumnToString(index++, MAX_THREAD_DATA_LEN, pTmp->threadData);
449 dbHandle.freeTable();