RSA sync with private
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageMms.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include "MsgDebug.h"
18 #include "MsgCppTypes.h"
19 #include "MsgUtilFile.h"
20 #include "MsgUtilStorage.h"
21 #include "MsgSqliteWrapper.h"
22 #include "MsgStorageHandler.h"
23 #include "MsgContact.h"
24
25
26 /*==================================================================================================
27                                      VARIABLES
28 ==================================================================================================*/
29 extern MsgDbHandler dbHandle;
30
31
32 /*==================================================================================================
33                                      FUNCTION IMPLEMENTATION
34 ==================================================================================================*/
35 msg_error_t MsgStoGetText(msg_message_id_t msgId, char *pSubject, char *pMsgText)
36 {
37         char sqlQuery[MAX_QUERY_LEN+1];
38
39         memset(sqlQuery, 0x00, sizeof(sqlQuery));
40
41         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT, MSG_TEXT FROM %s WHERE MSG_ID = %d;",
42                         MSGFW_MESSAGE_TABLE_NAME, msgId);
43
44         if(dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
45                 return MSG_ERR_DB_PREPARE;
46
47         if(dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
48
49                 char *subject = (char*)dbHandle.columnText(0);
50                 char *text = (char*)dbHandle.columnText(1);
51
52                 if(subject)
53                         strncpy(pSubject, subject, MAX_SUBJECT_LEN);
54                 if(text)
55                         strncpy(pMsgText, text, MAX_MSG_TEXT_LEN);
56         } else {
57                 dbHandle.finalizeQuery();
58                 return MSG_ERR_DB_STEP;
59         }
60
61         dbHandle.finalizeQuery();
62
63         return MSG_SUCCESS;
64
65 }
66
67
68
69 msg_error_t MsgStoUpdateMMSMessage(MSG_MESSAGE_INFO_S *pMsg)
70 {
71         MSG_BEGIN();
72
73         char sqlQuery[MAX_QUERY_LEN+1];
74
75         memset(sqlQuery, 0x00, sizeof(sqlQuery));
76
77         dbHandle.beginTrans();
78
79         if(pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS) {
80                 if( pMsg->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS ) {
81                         snprintf(sqlQuery, sizeof(sqlQuery),
82                                         "UPDATE %s SET MAIN_TYPE = %d, SUB_TYPE = %d, DISPLAY_TIME = %lu, SUBJECT = ?, NETWORK_STATUS = %d, MSG_TEXT = ?, THUMB_PATH = '%s', DATA_SIZE = %d WHERE MSG_ID = %d;",
83                                         MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->networkStatus, pMsg->thumbPath,  pMsg->dataSize, pMsg->msgId);
84
85                         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
86                                 dbHandle.endTrans(false);
87                                 return MSG_ERR_DB_PREPARE;
88                         }
89
90                         dbHandle.bindText(pMsg->subject, 1);
91                         dbHandle.bindText(pMsg->msgText, 2);
92                 } else if( pMsg->networkStatus == MSG_NETWORK_RETRIEVE_FAIL) {
93                         snprintf(sqlQuery, sizeof(sqlQuery),
94                                         "UPDATE %s SET MAIN_TYPE = %d, SUB_TYPE = %d, SUBJECT = ?, NETWORK_STATUS = %d, MSG_TEXT = ?, THUMB_PATH = '%s' WHERE MSG_ID = %d;",
95                                         MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->networkStatus, pMsg->thumbPath,  pMsg->msgId);
96
97                         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
98                                 dbHandle.endTrans(false);
99                                 return MSG_ERR_DB_PREPARE;
100                         }
101
102                         dbHandle.bindText(pMsg->subject, 1);
103                         dbHandle.bindText(pMsg->msgText, 2);
104                 }
105         } else if (pMsg->msgType.subType == MSG_SENDREQ_MMS) {
106                 snprintf(sqlQuery, sizeof(sqlQuery),
107                                 "UPDATE %s SET MSG_DATA = '%s', MSG_TEXT = ?, THUMB_PATH = '%s', DATA_SIZE = %d WHERE MSG_ID = %d;",
108                                 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgData, pMsg->thumbPath, pMsg->dataSize, pMsg->msgId);
109
110                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
111                         dbHandle.endTrans(false);
112                         return MSG_ERR_DB_PREPARE;
113                 }
114
115                 dbHandle.bindText(pMsg->msgText, 1);
116         } else {
117                 snprintf(sqlQuery, sizeof(sqlQuery),
118                                 "UPDATE %s SET MAIN_TYPE = %d, SUB_TYPE = %d, FOLDER_ID = %d, NETWORK_STATUS = %d WHERE MSG_ID = %d;",
119                                 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->folderId, pMsg->networkStatus, pMsg->msgId);
120
121                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
122                         dbHandle.endTrans(false);
123                         return MSG_ERR_DB_PREPARE;
124                 }
125         }
126
127         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
128                 dbHandle.finalizeQuery();
129                 dbHandle.endTrans(false);
130                 MSG_DEBUG("Update MMS Message. Fail [%s]", sqlQuery);
131                 return MSG_ERR_DB_STEP;
132         }
133
134         dbHandle.finalizeQuery();
135
136         msg_thread_id_t convId = 0;
137         int row = 0;
138
139         // Get SUB_TYPE, STORAGE_ID
140         memset(sqlQuery, 0x00, sizeof(sqlQuery));
141         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONV_ID FROM %s WHERE MSG_ID = %d;",
142                         MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
143
144         if (dbHandle.getTable(sqlQuery, &row) != MSG_SUCCESS) {
145                 dbHandle.freeTable();
146                 dbHandle.endTrans(false);
147                 return MSG_ERR_DB_PREPARE;
148         }
149
150         if (row > 0) {
151                 convId = dbHandle.getColumnToInt(1);
152
153                 MSG_DEBUG("Conversation id:[%d]", convId);
154
155                 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
156                         MSG_DEBUG("MsgStoUpdateConversation() Error");
157                         dbHandle.freeTable();
158                         dbHandle.endTrans(false);
159
160                         return MSG_ERR_STORAGE_ERROR;
161                 }
162
163         } else {
164                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
165                 dbHandle.freeTable();
166                 dbHandle.endTrans(false);
167                 return MSG_ERR_DB_STEP;
168         }
169
170         dbHandle.freeTable();
171         dbHandle.endTrans(true);
172
173         MSG_END();
174
175         return MSG_SUCCESS;
176 }
177
178
179 msg_error_t MsgStoGetContentLocation(MSG_MESSAGE_INFO_S* pMsgInfo)
180 {
181         char sqlQuery[MAX_QUERY_LEN+1];
182
183         memset(sqlQuery, 0x00, sizeof(sqlQuery));
184
185         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENTS_LOCATION FROM %s WHERE MSG_ID = %d;",
186                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
187
188         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
189                 return MSG_ERR_DB_PREPARE;
190
191         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
192                 if (dbHandle.columnText(0) != NULL) {
193                         strncpy(pMsgInfo->msgData, (char*)dbHandle.columnText(0), MAX_MSG_DATA_LEN);
194                         pMsgInfo->dataSize = strlen(pMsgInfo->msgData);
195                 } else {
196                         dbHandle.finalizeQuery();
197                         return MSG_ERR_DB_NORECORD;
198                 }
199         } else {
200                 dbHandle.finalizeQuery();
201                 return MSG_ERR_DB_STEP;
202         }
203
204         dbHandle.finalizeQuery();
205
206         return MSG_SUCCESS;
207 }
208
209
210 msg_error_t MsgStoSetReadReportSendStatus(msg_message_id_t msgId, int readReportSendStatus)
211 {
212         bool bReadReportSent = false;
213
214         if((MmsRecvReadReportSendStatus)readReportSendStatus == MMS_RECEIVE_READ_REPORT_SENT)
215                 bReadReportSent = true;
216         else
217                 bReadReportSent = false;
218
219         char sqlQuery[MAX_QUERY_LEN+1];
220
221         memset(sqlQuery, 0x00, sizeof(sqlQuery));
222
223         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_REPORT_SEND_STATUS = %d, READ_REPORT_SENT = %d WHERE MSG_ID = %d;",
224                         MMS_PLUGIN_MESSAGE_TABLE_NAME, (MmsRecvReadReportSendStatus)readReportSendStatus, (int)bReadReportSent, msgId);
225
226         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
227                 return MSG_ERR_DB_EXEC;
228
229         return MSG_SUCCESS;
230 }
231
232
233 msg_error_t MsgStoGetOrgAddressList(MSG_MESSAGE_INFO_S *pMsg)
234 {
235         msg_error_t err = MSG_SUCCESS;
236         char sqlQuery[MAX_QUERY_LEN+1];
237         int rowCnt = 0;
238         int index = 3;
239
240         memset(sqlQuery, 0x00, sizeof(sqlQuery));
241         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ADDRESS_TYPE, A.RECIPIENT_TYPE, A.ADDRESS_VAL FROM %s A, %s B \
242                         WHERE A.CONV_ID = B.CONV_ID AND B.MSG_ID = %d;",
243                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
244
245         err = dbHandle.getTable(sqlQuery, &rowCnt);
246
247         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
248                 dbHandle.freeTable();
249                 return err;
250         }
251
252         for(int i = 0; i < rowCnt; i++)
253         {
254                 pMsg->addressList[i].addressType = dbHandle.getColumnToInt(index++);
255                 pMsg->addressList[i].recipientType = dbHandle.getColumnToInt(index++);
256                 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pMsg->addressList[i].addressVal);
257         }
258
259         pMsg->nAddressCnt = rowCnt;
260
261         dbHandle.freeTable();
262
263         return MSG_SUCCESS;
264 }
265
266
267 msg_error_t MsgStoGetSubject(msg_message_id_t msgId, char *pSubject)
268 {
269         char sqlQuery[MAX_QUERY_LEN+1];
270
271         memset(sqlQuery, 0x00, sizeof(sqlQuery));
272
273         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT FROM %s WHERE MSG_ID = %d;",
274                         MSGFW_MESSAGE_TABLE_NAME, msgId);
275
276         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
277                 return MSG_ERR_DB_PREPARE;
278
279         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
280                 strncpy(pSubject, (char*)dbHandle.columnText(0), MAX_SUBJECT_LEN);
281         } else {
282                 dbHandle.finalizeQuery();
283                 return MSG_ERR_DB_STEP;
284         }
285
286         dbHandle.finalizeQuery();
287
288         return MSG_SUCCESS;
289 }
290
291 msg_error_t MsgStoGetRecipientList(msg_message_id_t msgId, MSG_RECIPIENTS_LIST_S *pRecipientList)
292 {
293         if (pRecipientList == NULL) {
294                 MSG_DEBUG("pRecipientList is NULL");
295                 return MSG_ERR_NULL_POINTER;
296         }
297
298         int rowCnt = 0, index = 7;
299
300         char sqlQuery[MAX_QUERY_LEN+1];
301
302         memset(sqlQuery, 0x00, sizeof(sqlQuery));
303
304         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
305                         B.ADDRESS_VAL, B.CONTACT_ID, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME \
306                         FROM %s A, %s B WHERE A.MSG_ID = %d AND A.CONV_ID = B.CONV_ID;",
307                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, msgId);
308
309         if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
310                 dbHandle.freeTable();
311                 return MSG_ERR_DB_GETTABLE;
312         }
313
314         pRecipientList->recipientCnt= rowCnt;
315
316         MSG_DEBUG("pRecipientList->recipientCnt [%d]", pRecipientList->recipientCnt);
317
318         pRecipientList->recipientAddr= (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*rowCnt];
319
320         MSG_ADDRESS_INFO_S* pTmp = pRecipientList->recipientAddr;
321
322         char firstName[MAX_THREAD_NAME_LEN+1], lastName[MAX_THREAD_NAME_LEN+1];
323         char displayName[MAX_DISPLAY_NAME_LEN+1];
324
325         int order = MsgGetContactNameOrder();
326
327         for (int i = 0; i < rowCnt; i++)
328         {
329                 pTmp->addressType = dbHandle.getColumnToInt(index++);
330                 pTmp->recipientType= dbHandle.getColumnToInt(index++);
331
332                 memset(pTmp->addressVal, 0x00, sizeof(pTmp->addressVal));
333                 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressVal);
334
335                 pTmp->contactId= dbHandle.getColumnToInt(index++);
336
337                 memset(displayName, 0x00, sizeof(displayName));
338                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
339
340                 memset(firstName, 0x00, sizeof(firstName));
341                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
342
343                 memset(lastName, 0x00, sizeof(lastName));
344                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
345
346                 if (strlen(displayName) <= 0) {
347                         if (order == 0) {
348                                 if (firstName[0] != '\0')
349                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
350
351                                 if (lastName[0] != '\0') {
352                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
353                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
354                                 }
355                         } else if (order == 1) {
356                                 if (lastName[0] != '\0') {
357                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
358                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
359                                 }
360
361                                 if (firstName[0] != '\0')
362                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
363                         }
364                 }
365
366                 memset(pTmp->displayName, 0x00, sizeof(pTmp->displayName));
367                 strncpy(pTmp->displayName, displayName, MAX_DISPLAY_NAME_LEN);
368
369                 pTmp++;
370         }
371
372         dbHandle.freeTable();
373
374         return MSG_SUCCESS;
375 }
376
377
378 msg_error_t MsgStoGetReadStatus(msg_message_id_t msgId, bool *pReadStatus)
379 {
380         char sqlQuery[MAX_QUERY_LEN+1];
381
382         memset(sqlQuery, 0x00, sizeof(sqlQuery));
383
384         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT READ_STATUS FROM %s WHERE MSG_ID = %d;",
385                         MSGFW_MESSAGE_TABLE_NAME, msgId);
386
387         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
388                 return MSG_ERR_DB_PREPARE;
389
390         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
391                 *pReadStatus = (bool)dbHandle.columnInt(0);
392         } else {
393                 dbHandle.finalizeQuery();
394                 return MSG_ERR_DB_STEP;
395         }
396
397         dbHandle.finalizeQuery();
398
399         return MSG_SUCCESS;
400 }
401
402
403 msg_error_t MsgStoGetAddrInfo(msg_message_id_t msgId, MSG_ADDRESS_INFO_S *pAddrInfo)
404 {
405         char sqlQuery[MAX_QUERY_LEN+1];
406
407         // Add Address
408         memset(sqlQuery, 0x00, sizeof(sqlQuery));
409
410         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ADDRESS_TYPE, A.RECIPIENT_TYPE, A.CONTACT_ID, A.ADDRESS_VAL \
411                         FROM %s A, %s B WHERE A.CONV_ID = B.CONV_ID AND B.MSG_ID = %d;",
412                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
413
414         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
415                 return MSG_ERR_DB_PREPARE;
416
417         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
418                 pAddrInfo->addressType = dbHandle.columnInt(0);
419                 pAddrInfo->recipientType = dbHandle.columnInt(1);
420                 pAddrInfo->contactId = dbHandle.columnInt(2);
421
422                 strncpy(pAddrInfo->addressVal, (char*)dbHandle.columnText(3), MAX_ADDRESS_VAL_LEN);
423         } else {
424                 dbHandle.finalizeQuery();
425                 return MSG_ERR_DB_STEP;
426         }
427
428         dbHandle.finalizeQuery();
429
430         return MSG_SUCCESS;
431 }