Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginStorage.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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 <stdlib.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20
21 #include "MsgException.h"
22 #include "MsgUtilFile.h"
23 #include "MsgMmsMessage.h"
24 #include "MsgStorageTypes.h"
25 #include "MmsPluginDebug.h"
26 #include "MmsPluginStorage.h"
27 #include "MmsPluginMessage.h"
28 #include "MmsPluginSmil.h"
29 #include "MmsPluginDrm.h"
30 #include "MmsPluginMIME.h"
31
32 /*==================================================================================================
33                                      IMPLEMENTATION OF SmsPluginStorage - Member Functions
34 ==================================================================================================*/
35 MmsPluginStorage *MmsPluginStorage::pInstance = NULL;
36
37
38 MmsPluginStorage::MmsPluginStorage()
39 {
40         memset(&mmsMsg, 0, sizeof(MmsMsg));
41 }
42
43
44 MmsPluginStorage::~MmsPluginStorage()
45 {
46         if (dbHandle.disconnect() != MSG_SUCCESS) {
47                 MSG_DEBUG("DB Disconnect Fail");
48         }
49 }
50
51
52 MmsPluginStorage *MmsPluginStorage::instance()
53 {
54         if (!pInstance)
55                 pInstance = new MmsPluginStorage();
56
57         return pInstance;
58 }
59
60
61 void MmsPluginStorage::getMmsMessage(MmsMsg **pMmsMsg)
62 {
63         *pMmsMsg = &mmsMsg;
64 }
65
66 #if 0 //Unused code
67 void MmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char *pFileData)
68 {
69         MSG_BEGIN();
70
71         msg_error_t     err;
72
73         MmsMsg mmsMsg;
74
75         bzero(&mmsMsg, sizeof(mmsMsg));
76
77         mode_t file_mode = (S_IRUSR | S_IWUSR);
78
79         if (pMsgInfo->msgType.subType == MSG_SENDREQ_MMS) {
80
81                 char szTemp[MAX_MSG_DATA_LEN + 1];
82
83                 MMS_MESSAGE_DATA_S mmsMsgData;
84                 bzero(&mmsMsgData,sizeof(MMS_MESSAGE_DATA_S));
85                 if (MmsComposeMessage(&mmsMsg, pMsgInfo, pSendOptInfo, &mmsMsgData, pFileData) != true) {
86                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
87
88                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
89                         __MmsReleaseMmsLists(&mmsMsgData);
90
91                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Compose Error");
92                 }
93
94                 MmsPrintFileInfoForVLD(&mmsMsgData);
95
96                 char fileName[MSG_FILENAME_LEN_MAX+1] = {0,};
97
98                 FILE *pFile = NULL;
99
100                 strcpy(szTemp,pMsgInfo->msgData);
101
102                 snprintf((char *)pMsgInfo->msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
103
104                 if (addMmsMsgToDB(&mmsMsg, pMsgInfo, _MsgMmsGetAttachCount(&mmsMsgData)) != MSG_SUCCESS) {
105                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
106                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
107                         __MmsReleaseMmsLists(&mmsMsgData);
108
109                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
110                 }
111
112                 strcpy((char *)pMsgInfo->msgData,szTemp);
113
114                 snprintf(fileName, MSG_FILENAME_LEN_MAX+1, MSG_DATA_PATH"%d", mmsMsg.msgID);
115
116                 pFile = MsgOpenMMSFile(fileName);
117                 if (!pFile) {
118                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
119                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
120                         __MmsReleaseMmsLists(&mmsMsgData);
121                         THROW(MsgException::MMS_PLG_ERROR, "MMS File open Error");
122                 }
123
124                 if (fchmod(fileno(pFile), file_mode) < 0) {
125                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
126                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
127                         __MmsReleaseMmsLists(&mmsMsgData);
128                         MsgCloseFile(pFile);
129
130                         THROW(MsgException::MMS_PLG_ERROR, "chmod() error: %s", strerror(errno));
131                 }
132
133                 if (MmsEncodeSendReq(pFile, &mmsMsg) != true) {
134                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
135                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
136                         __MmsReleaseMmsLists(&mmsMsgData);
137                         MsgCloseFile(pFile);
138
139                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Encode Send Req Error");
140                 }
141
142                 MsgFsync(pFile);        //file is written to device immediately, it prevents missing file data from unexpected power off
143                 MsgCloseFile(pFile);
144
145                 char filepath[MSG_FILEPATH_LEN_MAX+1] = {0,};
146                 int size = 0;
147
148                 snprintf((char *)filepath, MSG_FILEPATH_LEN_MAX+1, MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
149                 if (MsgGetFileSize(filepath, &size) == false) {
150                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message MsgGetFileSize Error");
151                 }
152
153                 pMsgInfo->dataSize = size;
154
155                 MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
156                 MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
157                 __MmsReleaseMmsLists(&mmsMsgData);
158
159         } else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
160                 MSG_DEBUG("######## MmsPlgAddMessage -> MSG_NOTIFICATIONIND_MMS ###########");
161
162                 MmsComposeNotiMessage(&mmsMsg, pMsgInfo->msgId);
163
164                 //Need to store mms specific data (contents location, TrID, ExpiryTime, Delivery Report, message ID)
165                 if (addMmsMsgToDB(&mmsMsg, pMsgInfo) != MSG_SUCCESS) {
166                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
167                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
168                 }
169         } else if (pMsgInfo->msgType.subType == MSG_SENDCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS) {
170                 MmsMsg *pMsg = NULL;
171                 char szTemp[MAX_MSG_DATA_LEN + 1]= {0, };
172
173                 if (!MmsReadMsgBody(pMsgInfo->msgId, true, true, pFileData))
174                         THROW(MsgException::MMS_PLG_ERROR, "_MmsReadMsgBody Error");
175
176                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
177
178                 if (pMsgInfo->msgType.subType == MSG_SENDCONF_MMS)
179                         pMsgInfo->networkStatus = MSG_NETWORK_SEND_SUCCESS;
180                 else
181                         pMsgInfo->networkStatus = MSG_NETWORK_RETRIEVE_SUCCESS;
182                 strcpy(szTemp,pMsgInfo->msgData);
183                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
184                 strncpy(pMsgInfo->msgData, pFileData, MAX_MSG_DATA_LEN);
185
186                 MmsPluginStorage *pStorage = MmsPluginStorage::instance();
187
188                 MMS_MESSAGE_DATA_S mmsMsgData;
189                 bzero(&mmsMsgData,sizeof(MMS_MESSAGE_DATA_S));
190                 if (mmsHeader.msgType.type == MIME_MULTIPART_RELATED || mmsHeader.msgType.type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
191                         char *pSmilDoc;
192                         char szFileName[MSG_FILENAME_LEN_MAX] = {0, };
193
194                         mmsMsgData.regionCnt = 0;
195                         mmsMsgData.pageCnt = 0;
196                         mmsMsgData.attachCnt = 0;
197                         mmsMsgData.transitionCnt = 0;
198                         mmsMsgData.metaCnt = 0;
199                         memset(mmsMsgData.szSmilFilePath, 0, MSG_FILEPATH_LEN_MAX);
200
201                         pSmilDoc = MmsSmilGetPresentationData(pMsgInfo->msgId);
202                         if (pSmilDoc) {
203                                 MmsSmilParseSmilDoc(&mmsMsgData, pSmilDoc);
204
205                                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
206                                 strcpy(szFileName, pMsg->szFileName);
207
208                                 err = pStorage->getMsgText(&mmsMsgData, pMsgInfo->msgText);
209                                 MmsMakePreviewInfo(pMsgInfo->msgId, &mmsMsgData);
210                         }
211
212                         MsgMmsReleaseMmsLists(&mmsMsgData);
213                 }
214
215                 if (addMmsMsgToDB(pMsg, pMsgInfo) != MSG_SUCCESS) {
216                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
217
218                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
219                 }
220                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
221                 strcpy((char *)pMsgInfo->msgData,szTemp);
222
223                 MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
224
225         } else if (pMsgInfo->msgType.subType == MSG_READREPLY_MMS || pMsgInfo->msgType.subType == MSG_READRECIND_MMS) {
226                 MSG_DEBUG("######## MmsPlgAddMessage -> MSG_READREPLY_MMS || MSG_READRECIND_MMS ###########");
227
228                 char filePath[MAX_FULL_PATH_SIZE+1] = {0, };
229                 FILE *pFile = NULL;
230
231                 msg_read_report_status_t readStatus;
232                 msg_message_id_t selectedMsgId;
233                 int     version;
234
235                 memcpy(&readStatus, pMsgInfo->msgData, sizeof(msg_read_report_status_t));
236                 memcpy(&selectedMsgId, pMsgInfo->msgData + sizeof(msg_read_report_status_t), sizeof(msg_message_id_t));
237
238                 version = MmsPluginStorage::instance()->getMmsVersion(selectedMsgId);
239
240                 snprintf((char *)pMsgInfo->msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
241
242                 MmsComposeReadReportMessage(&mmsMsg, pMsgInfo, selectedMsgId);
243
244                 if (addMmsMsgToDB(&mmsMsg, pMsgInfo) != MSG_SUCCESS) {
245                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
246                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
247
248                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
249                 }
250
251                 snprintf(filePath, MAX_FULL_PATH_SIZE+1, MSG_DATA_PATH"%d", mmsMsg.msgID);
252                 pFile = MsgOpenMMSFile(filePath);
253                 if (!pFile) {
254                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
255                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
256                         MsgCloseFile(pFile);
257                         pFile = NULL;
258
259                         THROW(MsgException::MMS_PLG_ERROR, "MsgOpenMMSFile error");
260                 }
261
262                 if (fchmod(fileno(pFile), file_mode) < 0) {
263                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
264                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
265                         MsgCloseFile(pFile);
266                         pFile = NULL;
267
268                         THROW(MsgException::MMS_PLG_ERROR, "chmod() error: %s", strerror(errno));
269                 }
270
271                 if (version == 0x90) {
272                         MSG_DEBUG("### version 1.0 ###");
273                         if (MmsEncodeReadReport10(pFile, &mmsMsg, readStatus) != true) {
274                                 MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
275                                 MsgCloseFile(pFile);
276                                 pFile = NULL;
277
278                                 THROW(MsgException::MMS_PLG_ERROR, "MMS Encode Read Report 1.0 Error");
279                         }
280                 } else {
281                         MSG_DEBUG("### version 1.1 ###");
282                         if (MmsEncodeReadReport11(pFile, &mmsMsg, readStatus) != true) {
283                                 MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
284                                 MsgCloseFile(pFile);
285                                 pFile = NULL;
286
287                                 THROW(MsgException::MMS_PLG_ERROR, "MMS Encode Read Report 1.1 Error");
288                         }
289                 }
290
291                 MsgFsync(pFile);
292                 MsgCloseFile(pFile);
293                 pFile = NULL;
294
295                 MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
296
297                 MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
298
299         } else if (pMsgInfo->msgType.subType == MSG_FORWARD_MMS) {
300                 MSG_DEBUG("######## MmsPlgAddMessage -> MSG_FORWARD_MMS ###########");
301
302                 char filePath[MAX_FULL_PATH_SIZE + 1] = {0, };
303                 char szTemp[MAX_MSG_DATA_LEN + 1] = {0, };
304                 FILE *pFile = NULL;
305                 MMS_MESSAGE_DATA_S mmsMsgData;
306
307                 if (MmsComposeMessage(&mmsMsg, pMsgInfo, pSendOptInfo, &mmsMsgData, pFileData) != true) {
308                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
309                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
310                         __MmsReleaseMmsLists(&mmsMsgData);
311
312                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Compose Error");
313                 }
314
315                 strcpy(szTemp,pMsgInfo->msgData);
316
317                 snprintf((char *)pMsgInfo->msgData, MAX_MSG_DATA_LEN + 1, MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
318
319                 if (addMmsMsgToDB(&mmsMsg, pMsgInfo, _MsgMmsGetAttachCount(&mmsMsgData)) != MSG_SUCCESS) {
320                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
321                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
322                         __MmsReleaseMmsLists(&mmsMsgData);
323
324                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
325                 }
326
327                 strcpy((char *)pMsgInfo->msgData,szTemp);
328
329                 snprintf(filePath, MAX_FULL_PATH_SIZE + 1 , MSG_DATA_PATH"%d", mmsMsg.msgID);
330
331                 pFile = MsgOpenMMSFile(filePath);
332                 if (!pFile) {
333                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
334                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
335                         __MmsReleaseMmsLists(&mmsMsgData);
336                         MsgCloseFile(pFile);
337                         pFile = NULL;
338
339                         THROW(MsgException::MMS_PLG_ERROR, "MsgOpenMMSFile error");
340                 }
341
342                 if (fchmod(fileno(pFile), file_mode) < 0) {
343                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
344                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
345                         __MmsReleaseMmsLists(&mmsMsgData);
346                         MsgCloseFile(pFile);
347                         pFile = NULL;
348
349                         THROW(MsgException::MMS_PLG_ERROR, "chmod() error: %s", strerror(errno));
350                 }
351
352                 if (MmsEncodeSendReq(pFile, &mmsMsg) != true) {
353                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
354                         MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
355                         __MmsReleaseMmsLists(&mmsMsgData);
356                         MsgCloseFile(pFile);
357                         pFile = NULL;
358
359                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Encode Send Req Error");
360                 }
361                 MsgFsync(pFile);
362                 MsgCloseFile(pFile);
363                 pFile = NULL;
364
365                 MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
366                 MmsReleaseMmsAttrib(&mmsMsg.mmsAttrib);
367                 __MmsReleaseMmsLists(&mmsMsgData);
368         }
369
370         MSG_END();
371 }
372 #endif
373
374 void MmsPluginStorage::composeReadReport(MSG_MESSAGE_INFO_S *pMsgInfo)
375 {
376         FILE *pFile = NULL;
377
378         msg_read_report_status_t readStatus;
379         int     version;
380
381         memcpy(&readStatus, pMsgInfo->msgData, sizeof(msg_read_report_status_t));
382
383         MSG_DEBUG("pMsgInfo->msgId = %d", pMsgInfo->msgId);
384
385         version = MmsPluginStorage::instance()->getMmsVersion(pMsgInfo->msgId);
386
387         snprintf((char *)pMsgInfo->msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d-Read-Rec.ind", pMsgInfo->msgId);
388
389         if (version == 0x90)
390                 pMsgInfo->msgType.subType = MSG_READREPLY_MMS;
391         else
392                 pMsgInfo->msgType.subType = MSG_READRECIND_MMS;
393
394         MmsComposeReadReportMessage(&mmsMsg, pMsgInfo, pMsgInfo->msgId);
395
396         pFile = MsgOpenFile(pMsgInfo->msgData, "wb+");
397         if (!pFile)
398                 THROW(MsgException::MMS_PLG_ERROR, "MsgOpenMMSFile Error");
399
400         if (version == 0x90) {
401                 MSG_DEBUG("### version 1.0 ###");
402                 if (MmsEncodeReadReport10(pFile, &mmsMsg, readStatus) != true) {
403                         MsgCloseFile(pFile);
404                         THROW(MsgException::MMS_PLG_ERROR, "MMS Encode Read Report 1.0 Error");
405                 }
406         } else {
407                 MSG_DEBUG("### version 1.1 ###");
408                 if (MmsEncodeReadReport11(pFile, &mmsMsg, readStatus) != true) {
409                         MsgCloseFile(pFile);
410                         THROW(MsgException::MMS_PLG_ERROR, "MMS Encode Read Report 1.1 Error");
411                 }
412         }
413
414         MsgCloseFile(pFile);
415 }
416
417
418 msg_error_t MmsPluginStorage::addMmsMsgToDB(MmsMsg *pMmsMsg, const MSG_MESSAGE_INFO_S *pMsgInfo, int attachCnt)
419 {
420         MSG_BEGIN();
421
422         char sqlQuery[MAX_QUERY_LEN + 1];
423
424         // Add Message
425         memset(sqlQuery, 0x00, sizeof(sqlQuery));
426         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, %d, %ld, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d);",
427                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMmsMsg->msgID, pMmsMsg->szTrID, pMmsMsg->szMsgID, pMmsMsg->szForwardMsgID, pMmsMsg->szContentLocation,
428                         pMsgInfo->msgData, pMmsMsg->mmsAttrib.version, pMmsMsg->mmsAttrib.dataType, pMmsMsg->mmsAttrib.date, pMmsMsg->mmsAttrib.bHideAddress,
429                         pMmsMsg->mmsAttrib.bAskDeliveryReport, pMmsMsg->mmsAttrib.bReportAllowed, pMmsMsg->mmsAttrib.readReportAllowedType,
430                         pMmsMsg->mmsAttrib.bAskReadReply, pMmsMsg->mmsAttrib.bRead, pMmsMsg->mmsAttrib.readReportSendStatus, pMmsMsg->mmsAttrib.bReadReportSent,
431                         pMmsMsg->mmsAttrib.priority, pMmsMsg->mmsAttrib.bLeaveCopy, pMmsMsg->mmsAttrib.msgSize, pMmsMsg->mmsAttrib.msgClass,
432                         pMmsMsg->mmsAttrib.expiryTime.time,     pMmsMsg->mmsAttrib.bUseDeliveryCustomTime, pMmsMsg->mmsAttrib.deliveryTime.time, pMmsMsg->mmsAttrib.msgStatus);
433
434         MSG_DEBUG("\n!!!!!!!!! QUERY : %s\n", sqlQuery);
435
436         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
437                 return MSG_ERR_DB_EXEC;
438
439         if (updateMmsAttachCount(pMmsMsg->msgID, attachCnt) != MSG_SUCCESS)
440                 return MSG_ERR_DB_EXEC;
441
442         MSG_END();
443
444         return MSG_SUCCESS;
445 }
446
447 msg_error_t MmsPluginStorage::addMmsMsgToDB(MmsMsg *pMmsMsg, const char *raw_filepath)
448 {
449         MSG_BEGIN();
450
451         char sqlQuery[MAX_QUERY_LEN + 1];
452
453         // Add Message
454         memset(sqlQuery, 0x00, sizeof(sqlQuery));
455         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, %d, %ld, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d);",
456                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMmsMsg->msgID, pMmsMsg->szTrID, pMmsMsg->szMsgID, pMmsMsg->szForwardMsgID, pMmsMsg->szContentLocation,
457                         raw_filepath, pMmsMsg->mmsAttrib.version, pMmsMsg->mmsAttrib.dataType, pMmsMsg->mmsAttrib.date, pMmsMsg->mmsAttrib.bHideAddress,
458                         pMmsMsg->mmsAttrib.bAskDeliveryReport, pMmsMsg->mmsAttrib.bReportAllowed, pMmsMsg->mmsAttrib.readReportAllowedType,
459                         pMmsMsg->mmsAttrib.bAskReadReply, pMmsMsg->mmsAttrib.bRead, pMmsMsg->mmsAttrib.readReportSendStatus, pMmsMsg->mmsAttrib.bReadReportSent,
460                         pMmsMsg->mmsAttrib.priority, pMmsMsg->mmsAttrib.bLeaveCopy, pMmsMsg->mmsAttrib.msgSize, pMmsMsg->mmsAttrib.msgClass,
461                         pMmsMsg->mmsAttrib.expiryTime.time,     pMmsMsg->mmsAttrib.bUseDeliveryCustomTime, pMmsMsg->mmsAttrib.deliveryTime.time, pMmsMsg->mmsAttrib.msgStatus);
462
463         MSG_DEBUG("\n!!!!!!!!! QUERY : %s\n", sqlQuery);
464
465         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
466                 return MSG_ERR_DB_EXEC;
467
468         MSG_END();
469
470         return MSG_SUCCESS;
471 }
472 #if 0
473 msg_error_t     MmsPluginStorage::plgGetMmsMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char **pDestMsg)
474 {
475         MSG_BEGIN();
476
477         msg_error_t     err = MSG_SUCCESS;
478         MMS_MESSAGE_DATA_S tempMmsMsg = {0,};
479         MMS_MESSAGE_DATA_S *pMmsMsg = &tempMmsMsg;
480         int partCnt = 0;
481         unsigned int nSize = 0;
482
483         MsgType partHeader;
484         MmsAttrib pMmsAttrib;
485
486         char szBuf[MSG_FILEPATH_LEN_MAX + 1] = {0, };
487         bool bMultipartRelated = false;
488
489         if (pSendOptInfo != NULL) {
490                 char sqlQuery[MAX_QUERY_LEN + 1];
491
492                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
493                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ASK_DELIVERY_REPORT, KEEP_COPY, ASK_READ_REPLY, PRIORITY, EXPIRY_TIME, CUSTOM_DELIVERY_TIME, DELIVERY_TIME \
494                                 FROM %s WHERE MSG_ID = %d;", MMS_PLUGIN_MESSAGE_TABLE_NAME, pMsg->msgId);
495
496                 MSG_DEBUG("### SQLQuery = %s ###", sqlQuery);
497
498                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
499                         MSG_DEBUG("MSG_ERR_DB_PREPARE");
500
501                 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
502                         pSendOptInfo->bDeliverReq = dbHandle.columnInt(0);
503                         MSG_DEBUG("## delivery = %d ##", pSendOptInfo->bDeliverReq);
504
505                         pSendOptInfo->bKeepCopy = dbHandle.columnInt(1);
506                         MSG_DEBUG("## bKeepCopy = %d ##", pSendOptInfo->bKeepCopy);
507
508                         pSendOptInfo->option.mmsSendOptInfo.bReadReq = dbHandle.columnInt(2);
509                         MSG_DEBUG("## bReadReq = %d ##", pSendOptInfo->option.mmsSendOptInfo.bReadReq);
510
511                         pSendOptInfo->option.mmsSendOptInfo.priority = dbHandle.columnInt(3);
512                         MSG_DEBUG("## priority = %d ##", pSendOptInfo->option.mmsSendOptInfo.priority);
513
514                         pSendOptInfo->option.mmsSendOptInfo.expiryTime.time = (unsigned int)dbHandle.columnInt(4);
515                         MSG_DEBUG("## expiryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.expiryTime.time);
516
517                         pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime = (unsigned int)dbHandle.columnInt(5);
518                         MSG_DEBUG("## bUseDeliveryCustomTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime);
519
520                         pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time = (unsigned int)dbHandle.columnInt(6);
521                         MSG_DEBUG("## deliveryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time);
522                 } else {
523                         dbHandle.finalizeQuery();
524                         return MSG_ERR_DB_STEP;
525                 }
526
527                 dbHandle.finalizeQuery();
528         }
529
530         if (MmsReadMsgBody(pMsg->msgId, true, false, NULL) == false) {
531                 MSG_DEBUG("The MMS Message might include drm contents!!!");
532
533 #ifdef __SUPPORT_DRM__
534                 if (MmsDrm2GetConvertState() == MMS_DRM2_CONVERT_REQUIRED) {
535                         bool bRetToConvert = true;
536
537                         bRetToConvert = MmsDrm2ConvertMsgBody(mmsHeader.msgType.szOrgFilePath);
538
539                         MmsDrm2SetConvertState(MMS_DRM2_CONVERT_FINISH);
540
541                         if (bRetToConvert) {
542                                 int ret;
543                                 ret = remove(mmsHeader.msgType.szOrgFilePath);
544                                 if (ret != 0) {
545                                         MSG_DEBUG("remove fail\n");
546                                 }
547
548                                 ret = rename(MMS_DECODE_DRM_CONVERTED_TEMP_FILE, mmsHeader.msgType.szOrgFilePath);
549                                 if (ret != 0) {
550                                         MSG_DEBUG("rename fail\n");
551                                 }
552
553                                 if (MmsDrm2ReadMsgConvertedBody(pMsg, true, false, NULL) == false) {
554                                         MSG_DEBUG("MmsLoadMsg:MmsDrm2ReadMsgConvertedBody() returns false\n");
555                                         goto L_CATCH;
556                                 }
557                         } else {
558                                 goto L_CATCH;
559                         }
560                 }
561 #endif
562         }
563
564         MmsGetMsgAttrib(pMsg->msgId, &pMmsAttrib);
565
566         pMmsMsg->regionCnt = 0;
567         pMmsMsg->pageCnt = 0;
568         pMmsMsg->attachCnt = 0;
569         pMmsMsg->transitionCnt = 0;
570         pMmsMsg->metaCnt = 0;
571         memset(pMmsMsg->szSmilFilePath, 0, MSG_FILEPATH_LEN_MAX);
572
573         if (pMmsAttrib.contentType == MIME_MULTIPART_RELATED || pMmsAttrib.contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
574                 char *pSmilDoc = NULL;
575
576                 pSmilDoc = MmsSmilGetPresentationData(pMsg->msgId);
577                 if (!pSmilDoc) {
578                         goto L_CATCH;
579                 }
580
581                 MmsSmilParseSmilDoc(pMmsMsg, pSmilDoc);
582                 MmsRemovePims(pMmsMsg);
583                 bMultipartRelated = true;
584         }
585
586         partCnt = MmsGetMediaPartCount(pMsg->msgId);
587         MSG_DEBUG("MmsUiGetMediaAttachInfo: partCnt=%d\n", partCnt);
588
589         if (partCnt < 0) {
590                 MSG_DEBUG("MmsUiGetMediaAttachInfo: partCnt=%d\n", partCnt);
591                 goto FREE_CATCH;
592         }
593
594         for (int i = 0; i < partCnt; ++i) {
595                 if (!MmsGetMediaPartHeader(i, &partHeader)) {
596                         MSG_DEBUG("MmsUiGetMediaAttachInfo: MmsGetMediaPartHeader failed\n");
597                         goto FREE_CATCH;
598                 }
599
600                 if (partHeader.contentSize > 0) {
601                         if (!strcasecmp(partHeader.param.szFileName, "cid:")) {
602                                 strncpy((char *)szBuf, &partHeader.param.szFileName[4], MSG_FILEPATH_LEN_MAX);
603                         } else {
604                                 strncpy((char *)szBuf, partHeader.param.szFileName, MSG_FILEPATH_LEN_MAX);
605                         }
606                         sprintf(partHeader.param.szFileName, MSG_DATA_PATH"%s", szBuf);
607
608                         if (!bMultipartRelated || MmsCheckAdditionalMedia(pMmsMsg, &partHeader)) {
609
610                                 MMS_ATTACH_S *attachment = NULL;
611                                 int tempType;
612
613                                 attachment = (MMS_ATTACH_S *)calloc(sizeof(MMS_ATTACH_S), 1);
614
615                                 MsgGetTypeByFileName(&tempType, partHeader.param.szFileName);
616                                 attachment->mediatype = (MimeType)tempType;
617
618                                 strcpy(attachment->szFilePath, partHeader.param.szFileName);
619
620                                 strncpy(attachment->szFileName, partHeader.param.szName, MSG_FILENAME_LEN_MAX -1);
621
622                                 attachment->fileSize = partHeader.contentSize;
623
624                                 _MsgMmsAddAttachment(pMmsMsg, attachment);
625                         }
626                 }
627         }
628
629         *pDestMsg = _MsgMmsSerializeMessageData(pMmsMsg, &nSize);
630
631         MsgMmsReleaseMmsLists(pMmsMsg);
632
633
634         MmsMsg *pStoMmsMsg;
635         MmsPluginStorage::instance()->getMmsMessage(&pStoMmsMsg);
636         MmsInitHeader();
637         MmsUnregisterDecodeBuffer();
638 #ifdef __SUPPORT_DRM__
639         MmsReleaseMsgDRMInfo(&pStoMmsMsg->msgType.drmInfo);
640 #endif
641         MmsReleaseMsgBody(&pStoMmsMsg->msgBody, pStoMmsMsg->msgType.type);
642
643         pMsg->dataSize = nSize;
644         MSG_END();
645
646         return err;
647
648 FREE_CATCH:
649         if (bMultipartRelated) {
650                 MsgMmsReleaseMmsLists(pMmsMsg);
651         }
652
653 L_CATCH:
654         MSG_DEBUG("MmsPlgUpdateMessage : Update MMS Message Failed");
655         MSG_END();
656         {
657                 MmsMsg *pMsg;
658                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
659                 MmsInitHeader();
660
661                 MmsUnregisterDecodeBuffer();
662 #ifdef __SUPPORT_DRM__
663                 MmsReleaseMsgDRMInfo(&pMsg->msgType.drmInfo);
664 #endif
665                 MmsReleaseMsgBody(&pMsg->msgBody, pMsg->msgType.type);
666
667                 return MSG_ERR_STORAGE_ERROR;
668         }
669 }
670 #endif
671
672 msg_error_t MmsPluginStorage::updateMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char *pFileData)
673 {
674         MSG_BEGIN();
675
676         msg_error_t     err = MSG_SUCCESS;
677         char sqlQuery[MAX_QUERY_LEN + 1];
678         MmsMsg mmsMsg;
679         MMS_MESSAGE_DATA_S mmsMsgData = {0,};
680         char raw_filepath[MSG_FILENAME_LEN_MAX+1] = {0,};
681
682         bzero(&mmsMsg, sizeof(mmsMsg));
683
684         memset(sqlQuery, 0x00, sizeof(sqlQuery));
685         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ASK_DELIVERY_REPORT = %d, KEEP_COPY = %d, ASK_READ_REPLY = %d, EXPIRY_TIME = %d, CUSTOM_DELIVERY_TIME = %d, DELIVERY_TIME= %d, PRIORITY = %d \
686                         WHERE MSG_ID = %d;", MMS_PLUGIN_MESSAGE_TABLE_NAME, pSendOptInfo->bDeliverReq, pSendOptInfo->bKeepCopy, pSendOptInfo->option.mmsSendOptInfo.bReadReq,
687                         pSendOptInfo->option.mmsSendOptInfo.expiryTime.time, pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime, pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time,
688                         pSendOptInfo->option.mmsSendOptInfo.priority, pMsgInfo->msgId);
689
690         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
691                 return MSG_ERR_DB_EXEC;
692
693         if (pMsgInfo->msgType.subType == MSG_SENDREQ_MMS) {
694
695                 if (_MsgMmsDeserializeMessageData(&mmsMsgData, pFileData) == false) {
696                         MSG_DEBUG("Fail to Deserialize Message Data");
697                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Compose Error");
698                 }
699
700                 if (MmsComposeSendReq(&mmsMsg, pMsgInfo, pSendOptInfo, &mmsMsgData) != true) {
701                         MmsReleaseMmsMsg(&mmsMsg);
702                         MsgMmsReleaseMmsLists(&mmsMsgData);
703                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message MmsComposeSendReq Error");
704                 }
705
706                 //mms file
707                 snprintf(raw_filepath, sizeof(raw_filepath), MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
708
709                 //encode mms
710                 if (MmsEncodeMmsMessage(&mmsMsg, raw_filepath) == false) {
711                         MSG_DEBUG("Fail to Encode Message");
712                         MmsReleaseMmsMsg(&mmsMsg);
713                         MsgMmsReleaseMmsLists(&mmsMsgData);
714                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message Encode Error");
715                 }
716
717                 //preview data
718                 err = MmsMakePreviewInfo(pMsgInfo->msgId, &mmsMsgData);
719
720                 err = getMsgText(&mmsMsgData, pMsgInfo->msgText);
721
722                 if (mmsMsgData.attachCnt > 0) {
723                         if (updateMmsAttachCount(mmsMsg.msgID, mmsMsgData.attachCnt) != MSG_SUCCESS) {
724                                 MSG_DEBUG("Fail to updateMmsAttachCount");
725                         }
726                 }
727
728                 int size = 0;
729
730                 if (MsgGetFileSize(raw_filepath, &size) == false) {
731                         MmsReleaseMmsMsg(&mmsMsg);
732                         MsgMmsReleaseMmsLists(&mmsMsgData);
733                         THROW(MsgException::MMS_PLG_ERROR, "MMS Message MsgGetFileSize Error");
734                 }
735
736                 pMsgInfo->dataSize = size;
737         }
738
739         MmsReleaseMmsMsg(&mmsMsg);
740         MsgMmsReleaseMmsLists(&mmsMsgData);
741
742         MSG_END();
743
744         return err;
745 }
746
747
748 msg_error_t MmsPluginStorage::updateConfMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
749 {
750         MSG_BEGIN();
751
752         char sqlQuery[MAX_QUERY_LEN + 1];
753
754         MMS_RECV_DATA_S *pMmsRecvData = (MMS_RECV_DATA_S *)pMsgInfo->msgData;
755
756         MSG_DEBUG("###### pMsgInfo->msgData = %s #######", pMmsRecvData->retrievedFilePath);
757
758         memset(sqlQuery, 0x00, sizeof(sqlQuery));
759         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MESSAGE_ID = '%s', FILE_PATH = '%s' WHERE MSG_ID = %d;",
760                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMmsRecvData->szMsgID, pMmsRecvData->retrievedFilePath, pMsgInfo->msgId);
761
762         MSG_DEBUG("SQLQuery = %s", sqlQuery);
763
764         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
765                 return MSG_ERR_DB_EXEC;
766
767         MSG_END();
768
769         return MSG_SUCCESS;
770 }
771
772
773 msg_error_t MmsPluginStorage::updateMsgServerID(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
774 {
775         MSG_BEGIN();
776
777         char sqlQuery[MAX_QUERY_LEN + 1];
778         memset(sqlQuery, 0x00, sizeof(sqlQuery));
779
780         MMS_RECV_DATA_S *pMmsRecvData = (MMS_RECV_DATA_S *)pMsgInfo->msgData;
781
782         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MESSAGE_ID = '%s' WHERE MSG_ID = %d;",
783                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMmsRecvData->szMsgID, pMsgInfo->msgId);
784
785         MSG_DEBUG("SQLQuery = %s", sqlQuery);
786
787         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
788                 return MSG_ERR_DB_EXEC;
789
790         if (pSendOptInfo != NULL) {
791                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
792                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ASK_DELIVERY_REPORT = %d, ASK_READ_REPLY = %d, PRIORITY = %d, EXPIRY_TIME = %d \
793                                 WHERE MSG_ID = %d;", MMS_PLUGIN_MESSAGE_TABLE_NAME, pSendOptInfo->bDeliverReq, pSendOptInfo->option.mmsSendOptInfo.bReadReq,
794                                 pSendOptInfo->option.mmsSendOptInfo.priority, pSendOptInfo->option.mmsSendOptInfo.expiryTime.time, pMsgInfo->msgId);
795
796                 MSG_DEBUG("SQLQuery = %s", sqlQuery);
797
798                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
799                         return MSG_ERR_DB_EXEC;
800                 }
801         }
802
803         dbHandle.finalizeQuery();
804
805         MSG_END();
806
807         return MSG_SUCCESS;
808 }
809
810
811 msg_error_t MmsPluginStorage::updateNetStatus(msg_message_id_t msgId, msg_network_status_t netStatus)
812 {
813         char sqlQuery[MAX_QUERY_LEN + 1];
814
815         memset(sqlQuery, 0x00, sizeof(sqlQuery));
816
817         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
818                         MSGFW_MESSAGE_TABLE_NAME, netStatus, msgId);
819
820         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
821                 return MSG_ERR_DB_EXEC;
822
823         return MSG_SUCCESS;
824 }
825
826 msg_error_t MmsPluginStorage::insertDeliveryReport(msg_message_id_t msgId, char *address, MmsMsgMultiStatus *pStatus)
827 {
828         char sqlQuery[MAX_QUERY_LEN + 1];
829         memset(sqlQuery, 0x00, sizeof(sqlQuery));
830
831         //( MSG_ID INTEGER , ADDRESS_VAL TEXT , STATUS_TYPE INTEGER , STATUS INTEGER DEFAULT 0 , TIME DATETIME);
832         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s "
833                         "(MSG_ID, ADDRESS_VAL, STATUS_TYPE, STATUS, TIME) "
834                         "VALUES (%d, '%s', %d, %d, %d);",
835                         MSGFW_REPORT_TABLE_NAME, msgId, address, MSG_REPORT_TYPE_DELIVERY, pStatus->msgStatus, (int)pStatus->handledTime);
836
837         MSG_DEBUG("QUERY : [%s]", sqlQuery);
838
839         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
840                 return MSG_ERR_DB_EXEC;
841
842         return MSG_SUCCESS;
843 }
844
845 msg_error_t MmsPluginStorage::insertReadReport(msg_message_id_t msgId, char *address, MmsMsgMultiStatus *pStatus)
846 {
847         char sqlQuery[MAX_QUERY_LEN + 1];
848         memset(sqlQuery, 0x00, sizeof(sqlQuery));
849
850         //( MSG_ID INTEGER , ADDRESS_VAL TEXT , STATUS_TYPE INTEGER , STATUS INTEGER DEFAULT 0 , TIME DATETIME);
851         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s "
852                         "(MSG_ID, ADDRESS_VAL, STATUS_TYPE, STATUS, TIME) "
853                         "VALUES (%d, '%s', %d, %d, %d);",
854                         MSGFW_REPORT_TABLE_NAME, msgId, address, MSG_REPORT_TYPE_READ, pStatus->readStatus, (int)pStatus->readTime);
855
856         MSG_DEBUG("QUERY : [%s]", sqlQuery);
857
858         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
859                 return MSG_ERR_DB_EXEC;
860
861         return MSG_SUCCESS;
862 }
863
864 msg_error_t MmsPluginStorage::updateMmsAttrib(msg_message_id_t msgId, MmsAttrib *attrib, MSG_SUB_TYPE_T msgSubType)
865 {
866         MSG_BEGIN();
867
868         char sqlQuery[MAX_QUERY_LEN + 1];
869
870         memset(sqlQuery, 0x00, sizeof(sqlQuery));
871
872         if (msgSubType == MSG_NOTIFICATIONIND_MMS) {
873                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET EXPIRY_TIME = %d WHERE MSG_ID = %d;",
874                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, attrib->expiryTime.time, msgId);
875         } else if (msgSubType == MSG_RETRIEVE_AUTOCONF_MMS || msgSubType == MSG_RETRIEVE_MANUALCONF_MMS) {
876                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ASK_DELIVERY_REPORT = %d, ASK_READ_REPLY = %d, PRIORITY = %d, VERSION = %d WHERE MSG_ID = %d;",
877                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, attrib->bAskDeliveryReport, attrib->bAskReadReply, attrib->priority, attrib->version, msgId);
878         }
879
880         MSG_DEBUG("QUERY : [%s]", sqlQuery);
881
882         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
883                 return MSG_ERR_DB_EXEC;
884
885         MSG_END();
886
887         return MSG_SUCCESS;
888 }
889
890
891 msg_error_t MmsPluginStorage::updateMmsAttachCount(msg_message_id_t msgId, int count)
892 {
893         MSG_BEGIN();
894
895         char sqlQuery[MAX_QUERY_LEN + 1];
896
897         memset(sqlQuery, 0x00, sizeof(sqlQuery));
898         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ATTACHMENT_COUNT = %d WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, count, msgId);
899
900         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
901                 MSG_DEBUG("Fail to execute query [%s]", sqlQuery);
902                 return MSG_ERR_DB_EXEC;
903         }
904
905         MSG_END();
906
907         return MSG_SUCCESS;
908 }
909
910 void MmsPluginStorage::getMmsFromDB(msg_message_id_t msgId, MmsMsg *pMmsMsg)
911 {
912         char sqlQuery[MAX_QUERY_LEN + 1];
913
914         memset(sqlQuery, 0x00, sizeof(sqlQuery));
915
916         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT VERSION, DATA_TYPE,  DATE, HIDE_ADDRESS, ASK_DELIVERY_REPORT, REPORT_ALLOWED, \
917                         READ_REPORT_ALLOWED_TYPE, ASK_READ_REPLY, READ, READ_REPORT_SEND_STATUS, READ_REPORT_SENT, PRIORITY, \
918                         MSG_SIZE, MSG_CLASS, EXPIRY_TIME, CUSTOM_DELIVERY_TIME, DELIVERY_TIME, MSG_STATUS, \
919                         MESSAGE_ID, TRANSACTION_ID, CONTENTS_LOCATION, FILE_PATH \
920                         FROM %s WHERE MSG_ID = %d;",
921                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
922
923         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
924                 MSG_DEBUG("MSG_ERR_DB_PREPARE");
925
926         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
927                 int i = 0;
928                 pMmsMsg->mmsAttrib.version = dbHandle.columnInt(i++);
929                 pMmsMsg->mmsAttrib.dataType = (MmsDataType)dbHandle.columnInt(i++);
930                 pMmsMsg->mmsAttrib.date = dbHandle.columnInt(i++);
931                 pMmsMsg->mmsAttrib.bHideAddress = dbHandle.columnInt(i++);
932                 pMmsMsg->mmsAttrib.bAskDeliveryReport = dbHandle.columnInt(i++);
933                 pMmsMsg->mmsAttrib.bReportAllowed = dbHandle.columnInt(i++);
934                 pMmsMsg->mmsAttrib.readReportAllowedType = (MmsRecvReadReportType)dbHandle.columnInt(i++);
935                 pMmsMsg->mmsAttrib.bAskReadReply = dbHandle.columnInt(i++);
936                 pMmsMsg->mmsAttrib.bRead = dbHandle.columnInt(i++);
937                 pMmsMsg->mmsAttrib.readReportSendStatus = (MmsRecvReadReportSendStatus)dbHandle.columnInt(i++);
938                 pMmsMsg->mmsAttrib.bReadReportSent = dbHandle.columnInt(i++);
939                 pMmsMsg->mmsAttrib.priority = (MmsPriority)dbHandle.columnInt(i++);
940                 pMmsMsg->mmsAttrib.msgSize = dbHandle.columnInt(i++);
941                 pMmsMsg->mmsAttrib.msgClass = (MmsMsgClass)dbHandle.columnInt(i++);
942                 pMmsMsg->mmsAttrib.expiryTime.time = dbHandle.columnInt(i++);
943                 i++;//CUSTOM_DELIVERY_TIME
944                 pMmsMsg->mmsAttrib.deliveryTime.time = dbHandle.columnInt(i++);
945                 pMmsMsg->mmsAttrib.msgStatus = (msg_delivery_report_status_t)dbHandle.columnInt(i++);
946                 snprintf(pMmsMsg->szMsgID, sizeof(pMmsMsg->szMsgID), "%s", dbHandle.columnText(i++));
947                 snprintf(pMmsMsg->szTrID, sizeof(pMmsMsg->szTrID), "%s", dbHandle.columnText(i++));
948                 snprintf(pMmsMsg->szContentLocation, sizeof(pMmsMsg->szContentLocation), "%s", dbHandle.columnText(i++));
949                 snprintf(pMmsMsg->szFileName, sizeof(pMmsMsg->szFileName), "%s", dbHandle.columnText(i++));
950         }
951
952
953         dbHandle.finalizeQuery();
954 }
955
956 void MmsPluginStorage::getMmsAttrib(msg_message_id_t msgId, MmsMsg *pMmsMsg)
957 {
958         char sqlQuery[MAX_QUERY_LEN + 1];
959
960         memset(sqlQuery, 0x00, sizeof(sqlQuery));
961
962         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT VERSION, DATA_TYPE,  DATE, HIDE_ADDRESS, ASK_DELIVERY_REPORT, REPORT_ALLOWED, \
963                         READ_REPORT_ALLOWED_TYPE, ASK_READ_REPLY, READ, READ_REPORT_SEND_STATUS, READ_REPORT_SENT, PRIORITY, \
964                         MSG_SIZE, MSG_CLASS, EXPIRY_TIME, CUSTOM_DELIVERY_TIME, DELIVERY_TIME, MSG_STATUS FROM %s WHERE MSG_ID = %d;",
965                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
966
967         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
968                 MSG_DEBUG("MSG_ERR_DB_PREPARE");
969
970         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
971                 pMmsMsg->mmsAttrib.version = dbHandle.columnInt(1);
972                 pMmsMsg->mmsAttrib.dataType = (MmsDataType)dbHandle.columnInt(2);
973                 pMmsMsg->mmsAttrib.date = dbHandle.columnInt(3);
974                 pMmsMsg->mmsAttrib.bHideAddress = dbHandle.columnInt(4);
975                 pMmsMsg->mmsAttrib.bAskDeliveryReport = dbHandle.columnInt(5);
976                 pMmsMsg->mmsAttrib.bReportAllowed = dbHandle.columnInt(6);
977                 pMmsMsg->mmsAttrib.readReportAllowedType = (MmsRecvReadReportType)dbHandle.columnInt(7);
978                 pMmsMsg->mmsAttrib.bAskReadReply = dbHandle.columnInt(8);
979                 pMmsMsg->mmsAttrib.bRead = dbHandle.columnInt(9);
980                 pMmsMsg->mmsAttrib.readReportSendStatus = (MmsRecvReadReportSendStatus)dbHandle.columnInt(10);
981                 pMmsMsg->mmsAttrib.bReadReportSent = dbHandle.columnInt(11);
982                 pMmsMsg->mmsAttrib.priority = (MmsPriority)dbHandle.columnInt(12);
983                 pMmsMsg->mmsAttrib.msgSize = dbHandle.columnInt(13);
984                 pMmsMsg->mmsAttrib.msgClass = (MmsMsgClass)dbHandle.columnInt(14);
985                 pMmsMsg->mmsAttrib.expiryTime.time = dbHandle.columnInt(15);
986                 pMmsMsg->mmsAttrib.deliveryTime.time = dbHandle.columnInt(17);
987                 pMmsMsg->mmsAttrib.msgStatus = (msg_delivery_report_status_t)dbHandle.columnInt(18);
988         }
989
990         dbHandle.finalizeQuery();
991 }
992
993
994 msg_error_t MmsPluginStorage::getMmsMessageId(msg_message_id_t selectedMsgId, MmsMsg *pMmsMsg)
995 {
996         msg_error_t err = MSG_SUCCESS;
997
998         int rowCnt = 0;
999
1000         char sqlQuery[MAX_QUERY_LEN + 1];
1001
1002         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1003
1004         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MESSAGE_ID FROM %s WHERE MSG_ID = %d;",
1005                         MMS_PLUGIN_MESSAGE_TABLE_NAME, selectedMsgId);
1006
1007         err = dbHandle.getTable(sqlQuery, &rowCnt);
1008
1009         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1010                 dbHandle.freeTable();
1011                 MSG_DEBUG("[Error]Failed to Get Table");
1012                 return MSG_ERR_DB_NORECORD;
1013         }
1014
1015         if (rowCnt != 1) {
1016                 dbHandle.freeTable();
1017                 MSG_DEBUG("[Error] MSG_ERR_DB_NORECORD");
1018                 return MSG_ERR_DB_NORECORD;
1019         }
1020
1021         dbHandle.getColumnToString(1, MMS_MSG_ID_LEN + 1, pMmsMsg->szMsgID);
1022
1023         dbHandle.freeTable();
1024
1025         return MSG_SUCCESS;
1026 }
1027
1028
1029 int MmsPluginStorage::getMmsVersion(msg_message_id_t selectedMsgId)
1030 {
1031         msg_error_t err = MSG_SUCCESS;
1032         int rowCnt = 0;
1033
1034         int     version = 0;
1035
1036         char sqlQuery[MAX_QUERY_LEN + 1];
1037
1038         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1039
1040         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT VERSION FROM %s WHERE MSG_ID = %d;",
1041                         MMS_PLUGIN_MESSAGE_TABLE_NAME, selectedMsgId);
1042
1043         MSG_DEBUG("SqlQuery = %s", sqlQuery);
1044
1045         err = dbHandle.getTable(sqlQuery, &rowCnt);
1046
1047         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1048                 dbHandle.freeTable();
1049                 MSG_DEBUG("[Error]Failed to Get Table");
1050                 return version;
1051         }
1052
1053         if (rowCnt != 1) {
1054                 dbHandle.freeTable();
1055                 MSG_DEBUG("[Error]MSG_ERR_DB_NORECORD");
1056                 return version;
1057         }
1058
1059         version = dbHandle.getColumnToInt(1);
1060
1061         dbHandle.freeTable();
1062
1063         return version;
1064 }
1065
1066
1067 msg_error_t MmsPluginStorage::getContentLocation(MSG_MESSAGE_INFO_S *pMsgInfo)
1068 {
1069         char sqlQuery[MAX_QUERY_LEN + 1];
1070
1071         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1072
1073         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENTS_LOCATION FROM %s WHERE MSG_ID = %d;",
1074                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
1075
1076         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1077                 return MSG_ERR_DB_PREPARE;
1078
1079         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1080                 if (dbHandle.columnText(0) != NULL) {
1081                         strncpy(pMsgInfo->msgData, (char *)dbHandle.columnText(0), (strlen((char *)dbHandle.columnText(0)) > MAX_MSG_DATA_LEN ? MAX_MSG_DATA_LEN : strlen((char *)dbHandle.columnText(0))));
1082                         pMsgInfo->dataSize = strlen(pMsgInfo->msgData);
1083                 }
1084         } else {
1085                 dbHandle.finalizeQuery();
1086
1087                 return MSG_ERR_DB_STEP;
1088         }
1089
1090         dbHandle.finalizeQuery();
1091
1092         return MSG_SUCCESS;
1093 }
1094
1095
1096 /* reject_msg_support */
1097 msg_error_t MmsPluginStorage::getTrID(MSG_MESSAGE_INFO_S *pMsgInfo,char *pszTrID,int nBufferLen)
1098 {
1099         char sqlQuery[MAX_QUERY_LEN + 1];
1100
1101         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1102
1103         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT TRANSACTION_ID FROM %s WHERE MSG_ID = %d;",
1104                         MMS_PLUGIN_MESSAGE_TABLE_NAME, pMsgInfo->msgId);
1105
1106         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1107                 return MSG_ERR_DB_PREPARE;
1108
1109         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1110                 if (dbHandle.columnText(0) != NULL) {
1111                         strncpy(pszTrID, (char *)dbHandle.columnText(0), nBufferLen - 1);
1112                         pszTrID[nBufferLen-1] = '\0';
1113                 }
1114         } else {
1115                 dbHandle.finalizeQuery();
1116
1117                 return MSG_ERR_DB_STEP;
1118         }
1119
1120         dbHandle.finalizeQuery();
1121
1122         return MSG_SUCCESS;
1123 }
1124 /* reject_msg_support */
1125
1126 msg_error_t MmsPluginStorage::getAddressInfo(msg_message_id_t msgId, MSG_ADDRESS_INFO_S *pAddrInfo)
1127 {
1128         char sqlQuery[MAX_QUERY_LEN+1];
1129
1130         // Add Address
1131         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1132
1133         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ADDRESS_TYPE, A.RECIPIENT_TYPE, A.CONTACT_ID, A.ADDRESS_VAL \
1134                         FROM %s A, %s B WHERE A.CONV_ID = B.CONV_ID AND B.MSG_ID = %d;",
1135                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
1136
1137         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1138                 return MSG_ERR_DB_PREPARE;
1139
1140         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1141                 pAddrInfo->addressType = dbHandle.columnInt(0);
1142                 pAddrInfo->recipientType = dbHandle.columnInt(1);
1143                 pAddrInfo->contactId = dbHandle.columnInt(2);
1144
1145                 strncpy(pAddrInfo->addressVal, (char*)dbHandle.columnText(3), MAX_ADDRESS_VAL_LEN);
1146         } else {
1147                 dbHandle.finalizeQuery();
1148                 return MSG_ERR_DB_STEP;
1149         }
1150
1151         dbHandle.finalizeQuery();
1152
1153         return MSG_SUCCESS;
1154 }
1155
1156
1157 msg_error_t MmsPluginStorage::getMmsRawFilePath(msg_message_id_t msgId, char *pFilepath)
1158 {
1159         char sqlQuery[MAX_QUERY_LEN + 1];
1160
1161         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1162
1163         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE MSG_ID = %d;",
1164                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
1165
1166         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1167                 return MSG_ERR_DB_PREPARE;
1168
1169         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1170                 if (dbHandle.columnText(0) != NULL) {
1171                         strcpy(pFilepath, (char *)dbHandle.columnText(0));
1172                 }
1173         } else {
1174                 dbHandle.finalizeQuery();
1175
1176                 return MSG_ERR_DB_STEP;
1177         }
1178
1179         dbHandle.finalizeQuery();
1180
1181         return MSG_SUCCESS;
1182 }
1183
1184
1185 int MmsPluginStorage::searchMsgId(char *toNumber, char *szMsgID)
1186 {
1187         int msgId = -1;
1188
1189         msg_folder_id_t folderId = MSG_SENTBOX_ID;
1190
1191         char sqlQuery[MAX_QUERY_LEN + 1];
1192
1193         MmsAddrUtilRemovePlmnString(toNumber);
1194
1195         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1196
1197         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID FROM %s A, %s B \
1198                         WHERE A.MSG_ID = B.MSG_ID AND A.FOLDER_ID = %d AND B.MESSAGE_ID LIKE '%%%s%%'",
1199                         MSGFW_MESSAGE_TABLE_NAME, MMS_PLUGIN_MESSAGE_TABLE_NAME, folderId, szMsgID);
1200
1201         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1202
1203         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1204                 return MSG_ERR_DB_PREPARE;
1205
1206         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1207                 msgId = dbHandle.columnInt(0);
1208         }
1209
1210         dbHandle.finalizeQuery();
1211
1212         return msgId;
1213 }
1214
1215
1216 msg_error_t MmsPluginStorage::setReadReportSendStatus(msg_message_id_t msgId, int readReportSendStatus)
1217 {
1218         bool bReadReportSent = false;
1219
1220         if ((MmsRecvReadReportSendStatus)readReportSendStatus == MMS_RECEIVE_READ_REPORT_SENT)
1221                 bReadReportSent = true;
1222         else
1223                 bReadReportSent = false;
1224
1225         char sqlQuery[MAX_QUERY_LEN + 1];
1226
1227         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1228
1229         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_REPORT_SEND_STATUS = %d, READ_REPORT_SENT = %d WHERE MSG_ID = %d;",
1230                         MMS_PLUGIN_MESSAGE_TABLE_NAME, (MmsRecvReadReportSendStatus)readReportSendStatus, (int)bReadReportSent, msgId);
1231
1232         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
1233                 return MSG_ERR_DB_EXEC;
1234
1235         return MSG_SUCCESS;
1236 }
1237
1238
1239 msg_error_t MmsPluginStorage::getMsgText(MMS_MESSAGE_DATA_S *pMmsMsg, char *pMsgText)
1240 {
1241         MMS_PAGE_S *pPage = NULL;
1242         MMS_MEDIA_S *pMedia = NULL;
1243         char *pMmsMsgText = NULL;
1244         int textLen = 0;
1245         bool bText = false;
1246
1247         if (pMmsMsg == NULL)
1248                 return MSG_ERR_NULL_POINTER;
1249
1250         // Get the text data from the 1st slide.
1251         for (int i = 0; i< pMmsMsg->pageCnt; ++i) {
1252                 pPage = _MsgMmsGetPage(pMmsMsg, i);
1253
1254                 for (int j = 0; j < pPage->mediaCnt; ++j) {
1255                         pMedia = _MsgMmsGetMedia(pPage, j);
1256
1257                         if (pMedia->mediatype == MMS_SMIL_MEDIA_TEXT) {
1258                                 pMmsMsgText = MsgOpenAndReadMmsFile(pMedia->szFilePath, 0, -1, &textLen);
1259                                 if (pMmsMsgText)
1260                                         strncpy(pMsgText, pMmsMsgText, MAX_MSG_TEXT_LEN);
1261
1262                                 // for avoiding break character end of the string.
1263                                 if ((textLen >= MAX_MSG_TEXT_LEN) && pMsgText[MAX_MSG_TEXT_LEN - 1] >= 0x80) { // if it is multibyte chars by UTF8, it would be presendted by 1xxx xxxx
1264                                         for (int k = 1; k < 5; k++) {
1265                                                 // the first byte of multi-byte chars of UTF8, should be larger than 1100 0000
1266                                                 // (two byte chars start with 110x xxxx, and three byte chars start with 1110 xxxx,
1267                                                 // four byte chars start with 1111 0xxx)
1268                                                 if ((pMsgText[MAX_MSG_TEXT_LEN - k] >= 0xC0)) {
1269                                                         pMsgText[MAX_MSG_TEXT_LEN - k] = '\0';
1270                                                         break;
1271                                                 }
1272                                         }
1273                                 }
1274
1275                                 if (pMmsMsgText) {
1276                                         free(pMmsMsgText);
1277                                         pMmsMsgText = NULL;
1278                                 }
1279                                 bText = true;
1280                                 break;
1281                         }
1282                 }
1283
1284                 if (bText)
1285                         break;
1286         }
1287
1288         return MSG_SUCCESS;
1289 }
1290
1291 msg_error_t MmsPluginStorage::insertPreviewInfo(int msgId, int type, char *value, int count)
1292 {
1293         char sqlQuery[MAX_QUERY_LEN + 1];
1294         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1295
1296         //(MSG_ID INTEGER, TYPE INTEGER, INFO TEXT)
1297         snprintf(sqlQuery, sizeof(sqlQuery),
1298                         "INSERT INTO %s "
1299                         "(MSG_ID, TYPE, VALUE, COUNT)"
1300                         "VALUES (%d, %d, '%s', %d);",
1301                         MSGFW_MMS_PREVIEW_TABLE_NAME, msgId, type, value, count);
1302
1303         MSG_DEBUG("QUERY : [%s]", sqlQuery);
1304
1305         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
1306                 return MSG_ERR_DB_EXEC;
1307
1308         return MSG_SUCCESS;
1309 }
1310
1311 msg_error_t MmsPluginStorage::removePreviewInfo(int msgId)
1312 {
1313         char sqlQuery[MAX_QUERY_LEN + 1];
1314         char filePath[MSG_FILEPATH_LEN_MAX + 1] = {0,};
1315
1316         // remove thumbnail file
1317         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1318         snprintf(sqlQuery, sizeof(sqlQuery),
1319                         "SELECT VALUE FROM %s "
1320                         "WHERE MSG_ID = %d AND (TYPE=%d OR TYPE=%d);",
1321                         MSGFW_MMS_PREVIEW_TABLE_NAME, msgId, MSG_MMS_ITEM_TYPE_IMG, MSG_MMS_ITEM_TYPE_VIDEO);
1322
1323         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1324                 return MSG_ERR_DB_PREPARE;
1325         }
1326
1327         while (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1328
1329                 memset(filePath, 0x00, sizeof(filePath));
1330                 strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
1331                 if (remove(filePath) == -1)
1332                         MSG_DEBUG("Fail to delete file [%s]", filePath);
1333                 else
1334                         MSG_DEBUG("Success to delete file [%s]", filePath);
1335         }
1336
1337         dbHandle.finalizeQuery();
1338
1339         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1340         //(MSG_ID INTEGER, TYPE INTEGER, INFO TEXT)
1341         snprintf(sqlQuery, sizeof(sqlQuery),
1342                         "DELETE FROM %s WHERE MSG_ID= %d;",
1343                         MSGFW_MMS_PREVIEW_TABLE_NAME, msgId);
1344
1345         MSG_DEBUG("QUERY : [%s]", sqlQuery);
1346
1347         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
1348                 return MSG_ERR_DB_EXEC;
1349
1350         return MSG_SUCCESS;
1351 }
1352
1353 msg_error_t MmsPluginStorage::deleteMmsMessage(int msgId)
1354 {
1355         char sqlQuery[MAX_QUERY_LEN + 1];
1356         char filePath[MSG_FILEPATH_LEN_MAX + 1] = {0,};
1357         char dirPath[MSG_FILEPATH_LEN_MAX + 1]= {0,};
1358
1359         //remove DB Preview
1360         removePreviewInfo(msgId);
1361
1362         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1363         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE MSG_ID = %d;",
1364                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
1365
1366         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1367                 dbHandle.endTrans(false);
1368                 return MSG_ERR_DB_PREPARE;
1369         }
1370
1371         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1372                 strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
1373
1374                 snprintf(dirPath, MSG_FILEPATH_LEN_MAX, "%s.dir", filePath);
1375
1376                 //delete pdu file
1377                 if (remove(filePath) == -1)
1378                         MSG_DEBUG("Fail to delete file [%s]", filePath);
1379                 else
1380                         MSG_DEBUG("Success to delete file [%s]", filePath);
1381
1382                 //delete multipart files
1383                 MsgRmRf(dirPath);
1384
1385                 //delete multipart dir
1386                 rmdir(dirPath);
1387
1388         } else {
1389                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
1390                 dbHandle.finalizeQuery();
1391                 dbHandle.endTrans(false);
1392                 return MSG_ERR_DB_STEP;
1393         }
1394
1395         dbHandle.finalizeQuery();
1396
1397
1398         // Delete Data from MMS table
1399         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1400         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
1401                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
1402
1403         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1404                 dbHandle.endTrans(false);
1405                 return MSG_ERR_DB_EXEC;
1406         }
1407
1408         // Delete Data from MMS STATUS table
1409         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1410         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
1411                         MSGFW_REPORT_TABLE_NAME, msgId);
1412
1413         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1414                 dbHandle.endTrans(false);
1415                 return MSG_ERR_DB_EXEC;
1416         }
1417
1418         return MSG_SUCCESS;
1419 }
1420
1421 msg_error_t MmsPluginStorage::plgGetMmsMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char **pDestMsg)
1422 {
1423         MSG_BEGIN();
1424
1425         msg_error_t     err = MSG_SUCCESS;
1426         MMS_MESSAGE_DATA_S tempMmsMsgData = {0,};
1427         MMS_MESSAGE_DATA_S *pMmsMsg = &tempMmsMsgData;
1428
1429         unsigned int nSize = 0;
1430         bool bMultipartRelated = false;
1431
1432         bzero(pMmsMsg, sizeof(MMS_MESSAGE_DATA_S));
1433         pMmsMsg->regionCnt = 0;
1434         pMmsMsg->pageCnt = 0;
1435         pMmsMsg->attachCnt = 0;
1436         pMmsMsg->transitionCnt = 0;
1437         pMmsMsg->metaCnt = 0;
1438         memset(pMmsMsg->szSmilFilePath, 0, MSG_FILEPATH_LEN_MAX);
1439
1440         MmsMsg tempMmsMsg;
1441         memset(&tempMmsMsg, 0x00, sizeof(MmsMsg));
1442
1443         getMmsFromDB(pMsg->msgId, &tempMmsMsg);
1444
1445         snprintf(pMmsMsg->header.contentLocation, MSG_MSG_ID_LEN, "%s", tempMmsMsg.szContentLocation);
1446         pMmsMsg->header.mmsVersion = tempMmsMsg.mmsAttrib.version;
1447         pMmsMsg->header.messageClass = tempMmsMsg.mmsAttrib.msgClass;
1448         pMmsMsg->header.mmsPriority = tempMmsMsg.mmsAttrib.priority;
1449         snprintf(pMmsMsg->header.messageID, MSG_MSG_ID_LEN, "%s", tempMmsMsg.szMsgID);
1450         snprintf(pMmsMsg->header.trID, MSG_MSG_ID_LEN, "%s", tempMmsMsg.szTrID);
1451
1452         if (pSendOptInfo != NULL) {
1453
1454                 pSendOptInfo->bDeliverReq = tempMmsMsg.mmsAttrib.bAskDeliveryReport;
1455                 MSG_DEBUG("## delivery = %d ##", pSendOptInfo->bDeliverReq);
1456
1457                 pSendOptInfo->bKeepCopy = tempMmsMsg.mmsAttrib.bLeaveCopy;
1458                 MSG_DEBUG("## bKeepCopy = %d ##", pSendOptInfo->bKeepCopy);
1459
1460                 pSendOptInfo->option.mmsSendOptInfo.bReadReq = tempMmsMsg.mmsAttrib.bAskReadReply;
1461                 MSG_DEBUG("## bReadReq = %d ##", pSendOptInfo->option.mmsSendOptInfo.bReadReq);
1462
1463                 pSendOptInfo->option.mmsSendOptInfo.priority = tempMmsMsg.mmsAttrib.priority;
1464                 MSG_DEBUG("## priority = %d ##", pSendOptInfo->option.mmsSendOptInfo.priority);
1465
1466                 pSendOptInfo->option.mmsSendOptInfo.expiryTime.time = tempMmsMsg.mmsAttrib.expiryTime.time;
1467                 MSG_DEBUG("## expiryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.expiryTime.time);
1468
1469                 pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time = tempMmsMsg.mmsAttrib.deliveryTime.time;
1470                 MSG_DEBUG("## deliveryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time);
1471         }
1472
1473         switch(pMsg->msgType.subType) {
1474                 case MSG_SENDREQ_MMS:
1475                 case MSG_SENDCONF_MMS:
1476                 case MSG_RETRIEVE_MMS:
1477                 case MSG_RETRIEVE_AUTOCONF_MMS:
1478                 case MSG_RETRIEVE_MANUALCONF_MMS:
1479                 {
1480                         if (MmsReadMsgBody(pMsg->msgId, true, false, NULL) == false) {
1481                                 MSG_DEBUG("Fail to MmsReadMsgBody");
1482                                 goto FREE_CATCH;
1483                         }
1484
1485                         //header value
1486                         snprintf(pMmsMsg->header.szContentType, MSG_MSG_ID_LEN, "%s", MimeGetMimeStringFromMimeInt(mmsMsg.msgType.type));
1487                         pMmsMsg->header.contentType = mmsMsg.msgType.type;
1488                         pMmsMsg->header.messageType = mmsMsg.mmsAttrib.msgType;
1489
1490                         //body value
1491                         if (MmsMakeMmsData(&mmsMsg, pMmsMsg) == false) {
1492                                 MSG_DEBUG("Fail to makeMmsMessageData");
1493                                 goto FREE_CATCH;
1494                         }
1495                 }
1496                 break;
1497
1498                 case MSG_NOTIFICATIONIND_MMS:
1499                         pMmsMsg->header.messageType = MMS_MSGTYPE_NOTIFICATION_IND;
1500
1501                 break;
1502                 default:
1503
1504                 break;
1505         }
1506
1507
1508
1509         *pDestMsg = _MsgMmsSerializeMessageData(pMmsMsg, &nSize);
1510
1511         MsgMmsReleaseMmsLists(pMmsMsg);
1512
1513
1514         MmsMsg *pStoMmsMsg;
1515         MmsPluginStorage::instance()->getMmsMessage(&pStoMmsMsg);
1516         MmsInitHeader();
1517         MmsUnregisterDecodeBuffer();
1518 #ifdef __SUPPORT_DRM__
1519         MmsReleaseMsgDRMInfo(&pStoMmsMsg->msgType.drmInfo);
1520 #endif
1521         MmsReleaseMsgBody(&pStoMmsMsg->msgBody, pStoMmsMsg->msgType.type);
1522
1523         pMsg->dataSize = nSize;
1524         MSG_END();
1525
1526         return err;
1527
1528 FREE_CATCH:
1529         /*
1530         if (bMultipartRelated) {
1531                 MsgMmsReleaseMmsLists(pMmsMsg);
1532         }
1533         */
1534
1535 L_CATCH:
1536         MSG_DEBUG("MmsPlgUpdateMessage : Update MMS Message Failed");
1537         MSG_END();
1538         {
1539                 MmsMsg *pMsg;
1540                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
1541                 MmsInitHeader();
1542
1543                 MmsUnregisterDecodeBuffer();
1544 #ifdef __SUPPORT_DRM__
1545                 MmsReleaseMsgDRMInfo(&pMsg->msgType.drmInfo);
1546 #endif
1547                 MmsReleaseMsgBody(&pMsg->msgBody, pMsg->msgType.type);
1548
1549                 return MSG_ERR_STORAGE_ERROR;
1550         }
1551 }
1552
1553 void MmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char *pFileData)
1554 {
1555         MSG_BEGIN();
1556
1557         msg_error_t     err;
1558         char raw_filepath[MSG_FILENAME_LEN_MAX+1] = {0,};
1559         MmsMsg mmsMsg;
1560         MMS_MESSAGE_DATA_S mmsMsgData;
1561
1562         bzero(&mmsMsgData, sizeof(MMS_MESSAGE_DATA_S));
1563         bzero(&mmsMsg, sizeof(mmsMsg));
1564
1565         if (pMsgInfo->msgType.subType == MSG_SENDREQ_MMS) {
1566
1567                 if (_MsgMmsDeserializeMessageData(&mmsMsgData, pFileData) == false) {
1568                         MSG_DEBUG("Fail to Deserialize Message Data");
1569                         goto __CATCH;
1570                 }
1571
1572                 if (MmsComposeMessage(&mmsMsg, pMsgInfo, pSendOptInfo, &mmsMsgData, pFileData) != true) {
1573                         MSG_DEBUG("Fail to Compose Message");
1574                         goto __CATCH;
1575                 }
1576
1577                 //mms file
1578                 snprintf(raw_filepath, sizeof(raw_filepath), MSG_DATA_PATH"%d.mms", pMsgInfo->msgId);
1579
1580                 //encode mms
1581                 if (MmsEncodeMmsMessage(&mmsMsg, raw_filepath) == false) {
1582                         MSG_DEBUG("Fail to Encode Message");
1583                         goto __CATCH;
1584                 }
1585
1586                 //add to db
1587                 if (addMmsMsgToDB(&mmsMsg, raw_filepath) != MSG_SUCCESS) {
1588                         MSG_DEBUG("Fail to add db message");
1589                         goto __CATCH;
1590                 }
1591
1592                 //preview data
1593                 err = MmsMakePreviewInfo(pMsgInfo->msgId, &mmsMsgData);
1594                 err = getMsgText(&mmsMsgData, pMsgInfo->msgText);
1595
1596                 if (mmsMsgData.attachCnt > 0) {
1597                         if (updateMmsAttachCount(mmsMsg.msgID, mmsMsgData.attachCnt) != MSG_SUCCESS) {
1598                                 MSG_DEBUG("Fail to updateMmsAttachCount");
1599                                 goto __CATCH;
1600                         }
1601                 }
1602
1603                 if (MsgGetFileSize(raw_filepath, (int *)&pMsgInfo->dataSize) == false) {
1604                         MSG_DEBUG("Fail to get mms file size [%s]", raw_filepath);
1605                         goto __CATCH;
1606                 }
1607
1608                 MmsReleaseMmsMsg(&mmsMsg);
1609                 MsgMmsReleaseMmsLists(&mmsMsgData);
1610         } else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
1611
1612                 MmsComposeNotiMessage(&mmsMsg, pMsgInfo->msgId);
1613
1614                 //add to db
1615                 if (addMmsMsgToDB(&mmsMsg, "") != MSG_SUCCESS) {
1616                         MSG_DEBUG("Fail to add db message");
1617                         goto __CATCH;
1618                 }
1619
1620                 MmsReleaseMmsMsg(&mmsMsg);
1621         } else if (pMsgInfo->msgType.subType == MSG_SENDCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS) {
1622                 MmsMsg *pMsg = NULL;
1623                 char szTemp[MAX_MSG_DATA_LEN + 1]= {0, };
1624
1625                 if (!MmsReadMsgBody(pMsgInfo->msgId, true, true, pFileData))
1626                         THROW(MsgException::MMS_PLG_ERROR, "_MmsReadMsgBody Error");
1627
1628                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
1629
1630                 if (pMsgInfo->msgType.subType == MSG_SENDCONF_MMS)
1631                         pMsgInfo->networkStatus = MSG_NETWORK_SEND_SUCCESS;
1632                 else
1633                         pMsgInfo->networkStatus = MSG_NETWORK_RETRIEVE_SUCCESS;
1634                 strcpy(szTemp,pMsgInfo->msgData);
1635                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
1636                 strncpy(pMsgInfo->msgData, pFileData, MAX_MSG_DATA_LEN);
1637
1638                 MmsPluginStorage *pStorage = MmsPluginStorage::instance();
1639
1640                 MMS_MESSAGE_DATA_S mmsMsgData;
1641                 bzero(&mmsMsgData,sizeof(MMS_MESSAGE_DATA_S));
1642                 if (mmsHeader.msgType.type == MIME_MULTIPART_RELATED || mmsHeader.msgType.type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1643                         char *pSmilDoc;
1644                         char szFileName[MSG_FILENAME_LEN_MAX] = {0, };
1645
1646                         mmsMsgData.regionCnt = 0;
1647                         mmsMsgData.pageCnt = 0;
1648                         mmsMsgData.attachCnt = 0;
1649                         mmsMsgData.transitionCnt = 0;
1650                         mmsMsgData.metaCnt = 0;
1651                         memset(mmsMsgData.szSmilFilePath, 0, MSG_FILEPATH_LEN_MAX);
1652
1653                         pSmilDoc = MmsSmilGetPresentationData(pMsgInfo->msgId);
1654                         if (pSmilDoc) {
1655                                 MmsSmilParseSmilDoc(&mmsMsgData, pSmilDoc);
1656
1657                                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
1658                                 strcpy(szFileName, pMsg->szFileName);
1659
1660                                 err = pStorage->getMsgText(&mmsMsgData, pMsgInfo->msgText);
1661                                 MmsMakePreviewInfo(pMsgInfo->msgId, &mmsMsgData);
1662                         }
1663
1664                         MsgMmsReleaseMmsLists(&mmsMsgData);
1665                 }
1666
1667                 if (addMmsMsgToDB(pMsg, pMsgInfo) != MSG_SUCCESS) {
1668                         MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
1669
1670                         THROW(MsgException::MMS_PLG_ERROR, "MMS Stroage Error");
1671                 }
1672                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
1673                 strcpy((char *)pMsgInfo->msgData,szTemp);
1674
1675                 MmsReleaseMsgBody(&mmsMsg.msgBody, mmsMsg.msgType.type);
1676
1677         } else {
1678                 MSG_DEBUG("Not support msg sub type [%d]", pMsgInfo->msgType.subType);
1679                 goto __CATCH;
1680         }
1681
1682         MSG_END();
1683         return;
1684
1685 __CATCH:
1686
1687         removePreviewInfo(pMsgInfo->msgId);
1688         MmsReleaseMmsMsg(&mmsMsg);
1689         MsgMmsReleaseMmsLists(&mmsMsgData);
1690
1691         THROW(MsgException::MMS_PLG_ERROR, "MMS add Error");
1692 }