3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
5 * This file is part of msg-service.
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 * Sangkoo Kim <sangkoo.kim@samsung.com>
9 * Seunghwan Lee <sh.cat.lee@samsung.com>
10 * SoonMin Jung <sm0415.jung@samsung.com>
11 * Jae-Young Lee <jy4710.lee@samsung.com>
12 * KeeBum Kim <keebum.kim@samsung.com>
14 * PROPRIETARY/CONFIDENTIAL
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
32 #include "MsgCppTypes.h"
33 #include "MsgUtilFile.h"
34 #include "MsgUtilStorage.h"
35 #include "MsgSqliteWrapper.h"
36 #include "MsgStorageHandler.h"
37 #include "MsgContact.h"
40 /*==================================================================================================
42 ==================================================================================================*/
43 extern MsgDbHandler dbHandle;
46 /*==================================================================================================
47 FUNCTION IMPLEMENTATION
48 ==================================================================================================*/
49 MSG_ERROR_T MsgStoGetText(MSG_MESSAGE_ID_T MsgId, char *pSubject, char *pMsgText)
51 char sqlQuery[MAX_QUERY_LEN+1];
53 memset(sqlQuery, 0x00, sizeof(sqlQuery));
55 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT, MSG_TEXT FROM %s WHERE MSG_ID = %d;",
56 MSGFW_MESSAGE_TABLE_NAME, MsgId);
58 if(dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
59 return MSG_ERR_DB_PREPARE;
61 if(dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
63 char *subject = (char*)dbHandle.columnText(0);
64 char *text = (char*)dbHandle.columnText(1);
67 strncpy(pSubject, subject, MAX_SUBJECT_LEN);
69 strncpy(pMsgText, text, MAX_MSG_TEXT_LEN);
71 dbHandle.finalizeQuery();
72 return MSG_ERR_DB_STEP;
75 dbHandle.finalizeQuery();
83 MSG_ERROR_T MsgStoUpdateMMSMessage(MSG_MESSAGE_INFO_S *pMsg)
87 MSG_ERROR_T err = MSG_SUCCESS;
89 char sqlQuery[MAX_QUERY_LEN+1];
91 memset(sqlQuery, 0x00, sizeof(sqlQuery));
93 dbHandle.beginTrans();
95 if(pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS)
97 if( pMsg->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS )
99 snprintf(sqlQuery, sizeof(sqlQuery),
100 "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 REFERENCE_ID = %d;",
101 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->networkStatus, pMsg->thumbPath, pMsg->dataSize, pMsg->msgId);
103 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
104 return MSG_ERR_DB_PREPARE;
106 dbHandle.bindText(pMsg->subject, 1);
107 dbHandle.bindText(pMsg->msgText, 2);
109 else if( pMsg->networkStatus == MSG_NETWORK_RETRIEVE_FAIL)
111 snprintf(sqlQuery, sizeof(sqlQuery),
112 "UPDATE %s SET MAIN_TYPE = %d, SUB_TYPE = %d, SUBJECT = ?, NETWORK_STATUS = %d, MSG_TEXT = ?, THUMB_PATH = '%s' WHERE REFERENCE_ID = %d;",
113 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->networkStatus, pMsg->thumbPath, pMsg->msgId);
115 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
116 return MSG_ERR_DB_PREPARE;
118 dbHandle.bindText(pMsg->subject, 1);
119 dbHandle.bindText(pMsg->msgText, 2);
122 else if (pMsg->msgType.subType == MSG_SENDREQ_MMS)
124 snprintf(sqlQuery, sizeof(sqlQuery),
125 "UPDATE %s SET MSG_DATA = '%s', MSG_TEXT = ?, THUMB_PATH = '%s' WHERE REFERENCE_ID = %d;",
126 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgData, pMsg->thumbPath, pMsg->referenceId);
128 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
129 return MSG_ERR_DB_PREPARE;
131 dbHandle.bindText(pMsg->msgText, 1);
135 snprintf(sqlQuery, sizeof(sqlQuery),
136 "UPDATE %s SET MAIN_TYPE = %d, SUB_TYPE = %d, FOLDER_ID = %d, NETWORK_STATUS = %d WHERE REFERENCE_ID = %d;",
137 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->folderId, pMsg->networkStatus, pMsg->msgId);
139 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
140 return MSG_ERR_DB_PREPARE;
143 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE)
145 dbHandle.finalizeQuery();
146 dbHandle.endTrans(false);
147 MSG_DEBUG("Update MMS Message. Fail [%s]", sqlQuery);
148 return MSG_ERR_DB_STEP;
151 dbHandle.finalizeQuery();
153 unsigned int addrId = 0;
155 memset(sqlQuery, 0x00, sizeof(sqlQuery));
157 // Get SUB_TYPE, STORAGE_ID
158 memset(sqlQuery, 0x00, sizeof(sqlQuery));
159 snprintf(sqlQuery, sizeof(sqlQuery),
163 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
165 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
167 dbHandle.endTrans(false);
168 return MSG_ERR_DB_PREPARE;
171 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
173 addrId = dbHandle.columnInt(0);
175 MSG_DEBUG("AddressId:[%d]", addrId);
179 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
180 dbHandle.finalizeQuery();
181 dbHandle.endTrans(false);
182 return MSG_ERR_DB_STEP;
185 dbHandle.finalizeQuery();
187 memset(sqlQuery, 0x00, sizeof(sqlQuery));
188 snprintf(sqlQuery, sizeof(sqlQuery),
191 WHERE REFERENCE_ID = %d;",
192 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
194 err = dbHandle.getTable(sqlQuery, &rowCnt);
196 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD)
198 MSG_DEBUG("Failed to Get Table");
200 dbHandle.freeTable();
201 dbHandle.endTrans(false);
203 return MSG_ERR_STORAGE_ERROR;
206 for (int i = 1; i <= rowCnt; i++)
208 addrId = dbHandle.getColumnToInt(i);
210 if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS)
212 MSG_DEBUG("MsgStoUpdateAddress() Error");
213 dbHandle.freeTable();
214 dbHandle.endTrans(false);
216 return MSG_ERR_STORAGE_ERROR;
220 dbHandle.freeTable();
222 dbHandle.endTrans(true);
230 MSG_ERROR_T MsgStoGetContentLocation(MSG_MESSAGE_INFO_S* pMsgInfo)
232 char sqlQuery[MAX_QUERY_LEN+1];
234 memset(sqlQuery, 0x00, sizeof(sqlQuery));
236 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENTS_LOCATION FROM %s WHERE REFERENCE_ID IN \
237 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
238 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
240 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
241 return MSG_ERR_DB_PREPARE;
243 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
245 if (dbHandle.columnText(0) != NULL)
247 strncpy(pMsgInfo->msgData, (char*)dbHandle.columnText(0), MAX_MSG_DATA_LEN);
248 pMsgInfo->dataSize = strlen(pMsgInfo->msgData);
252 dbHandle.finalizeQuery();
253 return MSG_ERR_DB_NORECORD;
258 dbHandle.finalizeQuery();
259 return MSG_ERR_DB_STEP;
262 dbHandle.finalizeQuery();
268 MSG_ERROR_T MsgStoSetReadReportSendStatus(MSG_MESSAGE_ID_T msgId, int readReportSendStatus)
270 bool bReadReportSent = false;
272 if((MmsRecvReadReportSendStatus)readReportSendStatus == MMS_RECEIVE_READ_REPORT_SENT)
273 bReadReportSent = true;
275 bReadReportSent = false;
277 char sqlQuery[MAX_QUERY_LEN+1];
279 memset(sqlQuery, 0x00, sizeof(sqlQuery));
281 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_REPORT_SEND_STATUS = %d, READ_REPORT_SENT = %d WHERE REFERENCE_ID IN \
282 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
283 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, (MmsRecvReadReportSendStatus)readReportSendStatus, (int)bReadReportSent,
284 MSGFW_MESSAGE_TABLE_NAME, msgId);
286 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
287 return MSG_ERR_DB_EXEC;
293 MSG_ERROR_T MsgStoGetOrgAddressList(MSG_MESSAGE_INFO_S *pMsg)
295 MSG_ERROR_T err = MSG_SUCCESS;
296 char sqlQuery[MAX_QUERY_LEN+1];
301 memset(sqlQuery, 0x00, sizeof(sqlQuery));
302 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
304 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
305 return MSG_ERR_DB_PREPARE;
307 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
308 referenceId = dbHandle.columnInt(0);
310 dbHandle.finalizeQuery();
312 memset(sqlQuery, 0x00, sizeof(sqlQuery));
313 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_TYPE, RECIPIENT_TYPE, ADDRESS_VAL FROM %s WHERE ADDRESS_ID IN \
314 (SELECT ADDRESS_ID FROM %s WHERE REFERENCE_ID = %d);",
315 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, referenceId);
317 err = dbHandle.getTable(sqlQuery, &rowCnt);
319 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD)
321 dbHandle.freeTable();
325 for(int i = 0; i < rowCnt; i++)
327 pMsg->addressList[i].addressType = dbHandle.getColumnToInt(index++);
328 pMsg->addressList[i].recipientType = dbHandle.getColumnToInt(index++);
329 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pMsg->addressList[i].addressVal);
332 pMsg->nAddressCnt = rowCnt;
334 dbHandle.freeTable();
340 MSG_ERROR_T MsgStoGetSubject(MSG_MESSAGE_ID_T MsgId, char *pSubject)
342 char sqlQuery[MAX_QUERY_LEN+1];
344 memset(sqlQuery, 0x00, sizeof(sqlQuery));
346 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SUBJECT FROM %s WHERE MSG_ID = %d;",
347 MSGFW_MESSAGE_TABLE_NAME, MsgId);
349 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
350 return MSG_ERR_DB_PREPARE;
352 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
354 strncpy(pSubject, (char*)dbHandle.columnText(0), MAX_SUBJECT_LEN);
358 dbHandle.finalizeQuery();
359 return MSG_ERR_DB_STEP;
362 dbHandle.finalizeQuery();
368 MSG_ERROR_T MsgStoUpdateNetworkStatus(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_NETWORK_STATUS_T Status)
372 MSG_ERROR_T err = MSG_SUCCESS;
374 char sqlQuery[MAX_QUERY_LEN+1];
376 memset(sqlQuery, 0x00, sizeof(sqlQuery));
378 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE REFERENCE_ID IN \
379 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
380 MSGFW_MESSAGE_TABLE_NAME, Status,
381 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
383 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
384 return MSG_ERR_DB_EXEC;
392 MSG_ERROR_T MsgStoGetRecipientList(MSG_MESSAGE_ID_T msgId, MSG_RECIPIENTS_LIST_S *pRecipientList)
394 if (pRecipientList == NULL)
396 MSG_DEBUG("pRecipientList is NULL");
397 return MSG_ERR_NULL_POINTER;
400 int rowCnt = 0, index = 7;
402 char sqlQuery[MAX_QUERY_LEN+1];
404 memset(sqlQuery, 0x00, sizeof(sqlQuery));
406 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT B.ADDRESS_TYPE, B.RECIPIENT_TYPE, B.ADDRESS_VAL, B.CONTACT_ID, \
407 B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME \
409 WHERE A.MSG_ID = %d \
410 AND A.ADDRESS_ID = B.ADDRESS_ID;",
411 MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, msgId);
413 if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS)
415 dbHandle.freeTable();
416 return MSG_ERR_DB_GETTABLE;
419 pRecipientList->recipientCnt= rowCnt;
421 MSG_DEBUG("pRecipientList->recipientCnt [%d]", pRecipientList->recipientCnt);
423 pRecipientList->recipientAddr= (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*rowCnt];
425 MSG_ADDRESS_INFO_S* pTmp = pRecipientList->recipientAddr;
427 char firstName[MAX_THREAD_NAME_LEN+1], lastName[MAX_THREAD_NAME_LEN+1];
428 char displayName[MAX_DISPLAY_NAME_LEN+1];
430 int order = MsgGetContactNameOrder();
432 for (int i = 0; i < rowCnt; i++)
436 pTmp->addressType = dbHandle.getColumnToInt(index++);
437 pTmp->recipientType= dbHandle.getColumnToInt(index++);
439 memset(pTmp->addressVal, 0x00, sizeof(pTmp->addressVal));
440 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressVal);
442 pTmp->contactId= dbHandle.getColumnToInt(index++);
444 memset(displayName, 0x00, sizeof(displayName));
445 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
447 memset(firstName, 0x00, sizeof(firstName));
448 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
450 memset(lastName, 0x00, sizeof(lastName));
451 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
453 if (strlen(displayName) <= 0)
457 if (firstName[0] != '\0')
459 strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
462 if (lastName[0] != '\0')
464 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
465 strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
470 if (lastName[0] != '\0')
472 strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
473 strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
476 if (firstName[0] != '\0')
478 strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
483 memset(pTmp->displayName, 0x00, sizeof(pTmp->displayName));
484 strncpy(pTmp->displayName, displayName, MAX_DISPLAY_NAME_LEN);
489 dbHandle.freeTable();
495 MSG_ERROR_T MsgStoGetReadStatus(MSG_MESSAGE_ID_T MsgId, bool *pReadStatus)
497 char sqlQuery[MAX_QUERY_LEN+1];
499 memset(sqlQuery, 0x00, sizeof(sqlQuery));
501 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT READ_STATUS FROM %s WHERE MSG_ID = %d;",
502 MSGFW_MESSAGE_TABLE_NAME, MsgId);
504 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
505 return MSG_ERR_DB_PREPARE;
507 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
509 *pReadStatus = (bool)dbHandle.columnInt(0);
513 dbHandle.finalizeQuery();
514 return MSG_ERR_DB_STEP;
517 dbHandle.finalizeQuery();
523 MSG_ERROR_T MsgStoGetAddrInfo(MSG_MESSAGE_ID_T MsgId, MSG_ADDRESS_INFO_S *pAddrInfo)
525 char sqlQuery[MAX_QUERY_LEN+1];
528 memset(sqlQuery, 0x00, sizeof(sqlQuery));
530 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ADDRESS_ID, A.ADDRESS_TYPE, A.RECIPIENT_TYPE, A.CONTACT_ID, A.ADDRESS_VAL \
531 FROM %s A, %s B WHERE A.ADDRESS_ID = B.ADDRESS_ID AND B.MSG_ID = %d;",
532 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MsgId);
534 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
535 return MSG_ERR_DB_PREPARE;
537 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
538 pAddrInfo->threadId = dbHandle.columnInt(0);
539 pAddrInfo->addressType = dbHandle.columnInt(1);
540 pAddrInfo->recipientType = dbHandle.columnInt(2);
541 pAddrInfo->contactId = dbHandle.columnInt(3);
543 strncpy(pAddrInfo->addressVal, (char*)dbHandle.columnText(4), MAX_ADDRESS_VAL_LEN);
545 dbHandle.finalizeQuery();
546 return MSG_ERR_DB_STEP;
549 dbHandle.finalizeQuery();