upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / messageDB / MessageStorageReader.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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. 
15 */
16 /*
17  * MessageStorageReader.cpp
18  *
19  *  Created on: 2011. 11. 16.
20  *      Author: sangtai
21  */
22
23 #include "MessageStorageReader.h"
24 #include "MsgSqliteWrapper.h"
25
26 #include <dpl/log/log.h>
27
28 namespace{
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;
33
34         typedef struct{
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 */
38         } MSG_MESSAGE_TYPE_S;
39
40         typedef struct{
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*/
65         } MSG_MESSAGE_S;
66
67         typedef struct
68         {
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). */
77
78                 MSG_CONTACT_ID_T      contactId;                                                        /**< Indicates the unique contact ID. */
79
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. */
83         } MSG_THREAD_VIEW_S;
84
85         enum _MSG_MAIN_TYPE_E
86         {
87                 MSG_UNKNOWN_TYPE = 0,           /**< Unknown main type */
88                 MSG_SMS_TYPE,                           /**< SMS */
89                 MSG_MMS_TYPE,                           /**< MMS */
90         };
91 }//namespace
92
93 enum _MSG_SUB_TYPE_E
94 {
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 */
120
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 */
139 };
140
141
142 MessageStorageReader::MessageStorageReader() {
143         // TODO Auto-generated constructor stub
144
145 }
146
147 MessageStorageReader::~MessageStorageReader() {
148         // TODO Auto-generated destructor stub
149 }
150
151 MSG_ERROR_T MessageStorageReader::MsgStoConnectDB(){
152         return MSG_SUCCESS;
153 }
154
155 MSG_ERROR_T MessageStorageReader::MsgStoDisconnectDB(){
156         if (dbHandle.disconnect() != MSG_SUCCESS){
157                 LogError(">>>[ERROR] DB Disconnect Fail");
158                 return MSG_ERR_DB_DISCONNECT;
159         }
160
161         LogDebug(">>>");
162
163         return MSG_SUCCESS;
164 }
165
166 int MessageStorageReader::queryMessage(const std::string& whereClause, const std::string& orderLimit, /*out*/MSG_LIST_S *pMsgFolderViewList){
167         LogDebug("<<< whereClause:[" << whereClause << "]");
168
169         if (pMsgFolderViewList == NULL){
170                 LogDebug("pMsgFolderViewList is NULL");
171                 return MSG_ERR_NULL_POINTER;
172         }
173
174         int rowCnt = 0;
175         int index = 27; // numbers of index
176
177         unsigned int addrId = 0;
178
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];
184
185         std::string tmpWhereClause;
186         if(whereClause.length()==0){
187                 tmpWhereClause.append("WHERE ");
188         }else{
189                 tmpWhereClause.append(whereClause);
190                 tmpWhereClause.append(" ");
191                 tmpWhereClause.append("AND ");
192         }
193
194         tmpWhereClause.append("(A.ADDRESS_ID = B.ADDRESS_ID)");
195
196         if(orderLimit.length() == 0)
197         {
198                 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
199         }
200         else
201         {
202                 tmpWhereClause.append(orderLimit);
203                 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
204         }
205         LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
206
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 ";
212
213         oStream << tmpWhereClause;
214         stringSqlQuery = oStream.str();
215
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");
224                 return MSG_SUCCESS;
225         }else if ( err != MSG_SUCCESS ){
226                 dbHandle.freeTable();
227                 return err;
228         }
229
230         pMsgFolderViewList->nCount = rowCnt;
231         LogDebug("pMsgCommInfoList->nCount [" << pMsgFolderViewList->nCount << "]");
232
233         pMsgFolderViewList->msgInfo = (msg_message_t*)new char[sizeof(MSG_MESSAGE_S*)*rowCnt];
234
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));
240
241                 pTmp->pData = NULL;
242                 pTmp->pMmsData = NULL;
243
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++);
260
261                 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
262
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)){
266                         pTmp->pData = NULL;
267                         index++;
268                 }else{
269                         pTmp->pData = (void*)new char[pTmp->dataSize + 2];
270                         memset(pTmp->pData, 0x00, sizeof(pTmp->pData));
271
272                         dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char*)pTmp->pData);
273
274                         LogDebug("pTmp->pData:[" << (char*)pTmp->pData << "]");
275                 }
276
277                 if (addrId > 0){
278                         pTmp->nAddressCnt = 1;
279                 }else{
280                         pTmp->nAddressCnt = 0;
281                 }
282
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++);
287
288                 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressList[0].addressVal);
289
290                 memset(displayName, 0x00, sizeof(displayName));
291                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
292
293                 memset(firstName, 0x00, sizeof(firstName));
294                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
295
296                 memset(lastName, 0x00, sizeof(lastName));
297                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
298
299                 if (strlen(displayName) <= 0){
300                         if (strlen(firstName) > 0){
301                                 strncpy(displayName, firstName, strlen(firstName));
302                         }
303
304                         if (strlen(lastName) > 0){
305                                 strcat(displayName, " ");
306                                 strncat(displayName, lastName, strlen(lastName));
307                         }
308                 }//if
309
310                 strncpy(pTmp->addressList[0].displayName, displayName, MAX_DISPLAY_NAME_LEN);
311
312                 pTmp->attachCount = dbHandle.getColumnToInt(index++);
313
314                 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
315         }//for
316
317         dbHandle.freeTable();
318
319         LogDebug(">>>");
320         return MSG_SUCCESS;
321 }
322
323 int MessageStorageReader::queryConversation(const std::string& whereClause, const std::string& orderLimit,
324                 /*out*/MSG_THREAD_VIEW_LIST_S *pThreadViewList){
325
326         pThreadViewList->nCount = 0;
327         pThreadViewList->msgThreadInfo = NULL;
328
329         int rowCnt = 0;
330         int index = 15; // numbers of index
331
332         std::ostringstream oStream;
333         std::string stringSqlQuery;
334
335         std::string tmpWhereClause;
336         if(whereClause.length()==0){
337                 tmpWhereClause.append(" WHERE ADDRESS_ID <> 0 ");
338         }else{
339                 tmpWhereClause.append(whereClause);
340         }
341
342         if(orderLimit.length() == 0)
343         {
344                 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
345         }
346         else
347         {
348                 tmpWhereClause.append(orderLimit);
349                 LogDebug("<<< tmpWhereClause:[" << tmpWhereClause << "]");
350         }
351
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 << " ";
355
356         oStream << tmpWhereClause;
357         stringSqlQuery = oStream.str();
358         LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
359
360         MSG_ERROR_T err = dbHandle.getTable(stringSqlQuery.c_str(), &rowCnt);
361
362         if (err == MSG_ERR_DB_NORECORD) {
363                 dbHandle.freeTable();
364
365                 return MSG_SUCCESS;
366         } else if (err != MSG_SUCCESS) {
367                 LogDebug("stringSqlQuery:[" << stringSqlQuery << "]");
368
369                 dbHandle.freeTable();
370
371                 return err;
372         }
373
374         char firstName[MAX_THREAD_NAME_LEN], lastName[MAX_THREAD_NAME_LEN];
375         char displayName[MAX_DISPLAY_NAME_LEN];
376
377         int order = 0;
378
379         pThreadViewList->nCount = rowCnt;
380
381         LogDebug("pThreadViewList->nCount [" << pThreadViewList->nCount << "]");
382
383         pThreadViewList->msgThreadInfo = (msg_thread_view_t*) new char[sizeof(MSG_THREAD_VIEW_S*) * rowCnt];
384
385         MSG_THREAD_VIEW_S* pTmp = NULL;
386
387         for (int i = 0; i < rowCnt; i++) {
388                         pThreadViewList->msgThreadInfo[i] = (msg_thread_view_t) new char[sizeof(MSG_THREAD_VIEW_S)];
389
390                         pTmp = (MSG_THREAD_VIEW_S*) pThreadViewList->msgThreadInfo[i];
391
392                         pTmp->threadId = dbHandle.getColumnToInt(index++);
393                         pTmp->contactId = dbHandle.getColumnToInt(index++);
394
395                         memset(pTmp->threadAddr, 0x00, sizeof(pTmp->threadAddr));
396                         dbHandle.getColumnToString(index++, MAX_THREAD_ADDR_LEN, pTmp->threadAddr);
397
398                         memset(displayName, 0x00, sizeof(displayName));
399                         dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
400
401                         memset(firstName, 0x00, sizeof(firstName));
402                         dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
403
404                         memset(lastName, 0x00, sizeof(lastName));
405                         dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
406
407                         if (strlen(displayName) <= 0) {
408                                 if (order == 0) {
409                                         if (strlen(firstName) > 0) {
410                                                 strncpy(displayName, firstName, strlen(firstName));
411                                         }
412
413                                         if (strlen(lastName) > 0) {
414                                                 strcat(displayName, " ");
415                                                 strncat(displayName, lastName, strlen(lastName));
416                                         }
417                                 } else if (order == 1) {
418                                         if (strlen(lastName) > 0) {
419                                                 strncpy(displayName, lastName, strlen(lastName));
420                                                 strcat(displayName, " ");
421                                         }
422
423                                         if (strlen(firstName) > 0) {
424                                                 strncat(displayName, firstName, strlen(firstName));
425                                         }
426                                 }
427                         }
428
429                         memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
430                         strncpy(pTmp->threadName, displayName, MAX_THREAD_NAME_LEN);
431
432                         memset(pTmp->threadImagePath, 0x00, sizeof(pTmp->threadImagePath));
433                         dbHandle.getColumnToString(index++, MAX_IMAGE_PATH_LEN, pTmp->threadImagePath);
434
435                         pTmp->unreadCnt = dbHandle.getColumnToInt(index++);
436                         pTmp->smsCnt = dbHandle.getColumnToInt(index++);
437                         pTmp->mmsCnt = dbHandle.getColumnToInt(index++);
438
439                         pTmp->threadType.mainType = dbHandle.getColumnToInt(index++);
440                         pTmp->threadType.subType = dbHandle.getColumnToInt(index++);
441
442                         pTmp->direction = dbHandle.getColumnToInt(index++);
443                         pTmp->threadTime = (time_t) dbHandle.getColumnToInt(index++);
444
445                         memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
446                         dbHandle.getColumnToString(index++, MAX_THREAD_DATA_LEN, pTmp->threadData);
447                 }
448
449         dbHandle.freeTable();
450
451         return MSG_SUCCESS;
452 }
453