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