2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
18 #include "MsgCppTypes.h"
19 #include "MsgUtilFile.h"
20 #include "MsgUtilStorage.h"
21 #include "MsgSqliteWrapper.h"
22 #include "MsgStorageHandler.h"
23 #include "MsgContact.h"
26 /*==================================================================================================
28 ==================================================================================================*/
29 extern MsgDbHandler dbHandle;
32 /*==================================================================================================
33 FUNCTION IMPLEMENTATION
34 ==================================================================================================*/
35 msg_error_t MsgStoGetText(msg_message_id_t msgId, char *pSubject, char *pMsgText)
37 char sqlQuery[MAX_QUERY_LEN+1];
39 memset(sqlQuery, 0x00, sizeof(sqlQuery));
41 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT, MSG_TEXT FROM %s WHERE MSG_ID = %d;",
42 MSGFW_MESSAGE_TABLE_NAME, msgId);
44 if(dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
45 return MSG_ERR_DB_PREPARE;
47 if(dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
49 char *subject = (char*)dbHandle.columnText(0);
50 char *text = (char*)dbHandle.columnText(1);
53 strncpy(pSubject, subject, MAX_SUBJECT_LEN);
55 strncpy(pMsgText, text, MAX_MSG_TEXT_LEN);
57 dbHandle.finalizeQuery();
58 return MSG_ERR_DB_STEP;
61 dbHandle.finalizeQuery();
69 msg_error_t MsgStoUpdateMMSMessage(MSG_MESSAGE_INFO_S *pMsg)
73 char sqlQuery[MAX_QUERY_LEN+1];
75 memset(sqlQuery, 0x00, sizeof(sqlQuery));
77 dbHandle.beginTrans();
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);
85 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
86 dbHandle.endTrans(false);
87 return MSG_ERR_DB_PREPARE;
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);
97 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
98 dbHandle.endTrans(false);
99 return MSG_ERR_DB_PREPARE;
102 dbHandle.bindText(pMsg->subject, 1);
103 dbHandle.bindText(pMsg->msgText, 2);
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);
110 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
111 dbHandle.endTrans(false);
112 return MSG_ERR_DB_PREPARE;
115 dbHandle.bindText(pMsg->msgText, 1);
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);
121 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
122 dbHandle.endTrans(false);
123 return MSG_ERR_DB_PREPARE;
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;
134 dbHandle.finalizeQuery();
136 msg_thread_id_t convId = 0;
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);
144 if (dbHandle.getTable(sqlQuery, &row) != MSG_SUCCESS) {
145 dbHandle.freeTable();
146 dbHandle.endTrans(false);
147 return MSG_ERR_DB_PREPARE;
151 convId = dbHandle.getColumnToInt(1);
153 MSG_DEBUG("Conversation id:[%d]", convId);
155 if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
156 MSG_DEBUG("MsgStoUpdateConversation() Error");
157 dbHandle.freeTable();
158 dbHandle.endTrans(false);
160 return MSG_ERR_STORAGE_ERROR;
164 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
165 dbHandle.freeTable();
166 dbHandle.endTrans(false);
167 return MSG_ERR_DB_STEP;
170 dbHandle.freeTable();
171 dbHandle.endTrans(true);
179 msg_error_t MsgStoGetContentLocation(MSG_MESSAGE_INFO_S* pMsgInfo)
181 char sqlQuery[MAX_QUERY_LEN+1];
183 memset(sqlQuery, 0x00, sizeof(sqlQuery));
185 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENTS_LOCATION FROM %s WHERE MSG_ID = %d;",
186 MMS_PLUGIN_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
188 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
189 return MSG_ERR_DB_PREPARE;
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);
196 dbHandle.finalizeQuery();
197 return MSG_ERR_DB_NORECORD;
200 dbHandle.finalizeQuery();
201 return MSG_ERR_DB_STEP;
204 dbHandle.finalizeQuery();
210 msg_error_t MsgStoSetReadReportSendStatus(msg_message_id_t msgId, int readReportSendStatus)
212 bool bReadReportSent = false;
214 if((MmsRecvReadReportSendStatus)readReportSendStatus == MMS_RECEIVE_READ_REPORT_SENT)
215 bReadReportSent = true;
217 bReadReportSent = false;
219 char sqlQuery[MAX_QUERY_LEN+1];
221 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
226 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
227 return MSG_ERR_DB_EXEC;
233 msg_error_t MsgStoGetOrgAddressList(MSG_MESSAGE_INFO_S *pMsg)
235 msg_error_t err = MSG_SUCCESS;
236 char sqlQuery[MAX_QUERY_LEN+1];
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);
245 err = dbHandle.getTable(sqlQuery, &rowCnt);
247 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
248 dbHandle.freeTable();
252 for(int i = 0; i < rowCnt; i++)
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);
259 pMsg->nAddressCnt = rowCnt;
261 dbHandle.freeTable();
267 msg_error_t MsgStoGetSubject(msg_message_id_t msgId, char *pSubject)
269 char sqlQuery[MAX_QUERY_LEN+1];
271 memset(sqlQuery, 0x00, sizeof(sqlQuery));
273 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT FROM %s WHERE MSG_ID = %d;",
274 MSGFW_MESSAGE_TABLE_NAME, msgId);
276 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
277 return MSG_ERR_DB_PREPARE;
279 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
280 strncpy(pSubject, (char*)dbHandle.columnText(0), MAX_SUBJECT_LEN);
282 dbHandle.finalizeQuery();
283 return MSG_ERR_DB_STEP;
286 dbHandle.finalizeQuery();
291 msg_error_t MsgStoGetRecipientList(msg_message_id_t msgId, MSG_RECIPIENTS_LIST_S *pRecipientList)
293 if (pRecipientList == NULL) {
294 MSG_DEBUG("pRecipientList is NULL");
295 return MSG_ERR_NULL_POINTER;
298 int rowCnt = 0, index = 7;
300 char sqlQuery[MAX_QUERY_LEN+1];
302 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
309 if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
310 dbHandle.freeTable();
311 return MSG_ERR_DB_GETTABLE;
314 pRecipientList->recipientCnt= rowCnt;
316 MSG_DEBUG("pRecipientList->recipientCnt [%d]", pRecipientList->recipientCnt);
318 pRecipientList->recipientAddr= (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*rowCnt];
320 MSG_ADDRESS_INFO_S* pTmp = pRecipientList->recipientAddr;
322 char firstName[MAX_THREAD_NAME_LEN+1], lastName[MAX_THREAD_NAME_LEN+1];
323 char displayName[MAX_DISPLAY_NAME_LEN+1];
325 int order = MsgGetContactNameOrder();
327 for (int i = 0; i < rowCnt; i++)
329 pTmp->addressType = dbHandle.getColumnToInt(index++);
330 pTmp->recipientType= dbHandle.getColumnToInt(index++);
332 memset(pTmp->addressVal, 0x00, sizeof(pTmp->addressVal));
333 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressVal);
335 pTmp->contactId= dbHandle.getColumnToInt(index++);
337 memset(displayName, 0x00, sizeof(displayName));
338 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
340 memset(firstName, 0x00, sizeof(firstName));
341 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
343 memset(lastName, 0x00, sizeof(lastName));
344 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
346 if (strlen(displayName) <= 0) {
348 if (firstName[0] != '\0')
349 strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
351 if (lastName[0] != '\0') {
352 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
353 strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
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));
361 if (firstName[0] != '\0')
362 strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
366 memset(pTmp->displayName, 0x00, sizeof(pTmp->displayName));
367 strncpy(pTmp->displayName, displayName, MAX_DISPLAY_NAME_LEN);
372 dbHandle.freeTable();
378 msg_error_t MsgStoGetReadStatus(msg_message_id_t msgId, bool *pReadStatus)
380 char sqlQuery[MAX_QUERY_LEN+1];
382 memset(sqlQuery, 0x00, sizeof(sqlQuery));
384 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT READ_STATUS FROM %s WHERE MSG_ID = %d;",
385 MSGFW_MESSAGE_TABLE_NAME, msgId);
387 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
388 return MSG_ERR_DB_PREPARE;
390 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
391 *pReadStatus = (bool)dbHandle.columnInt(0);
393 dbHandle.finalizeQuery();
394 return MSG_ERR_DB_STEP;
397 dbHandle.finalizeQuery();
403 msg_error_t MsgStoGetAddrInfo(msg_message_id_t msgId, MSG_ADDRESS_INFO_S *pAddrInfo)
405 char sqlQuery[MAX_QUERY_LEN+1];
408 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
414 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
415 return MSG_ERR_DB_PREPARE;
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);
422 strncpy(pAddrInfo->addressVal, (char*)dbHandle.columnText(3), MAX_ADDRESS_VAL_LEN);
424 dbHandle.finalizeQuery();
425 return MSG_ERR_DB_STEP;
428 dbHandle.finalizeQuery();