Merge branch 'master' into systemd
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginMessage.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21 #include <ctype.h>
22
23 #include "MsgTypes.h"
24 #include "MsgCppTypes.h"
25 #include "MsgException.h"
26 #include "MsgStorageTypes.h"
27 #include "MsgSettingTypes.h"
28 #include "MmsPluginMessage.h"
29 #include "MmsPluginMIME.h"
30 #include "MmsPluginAvCodec.h"
31 #include "MsgUtilFile.h"
32 #include "MsgDebug.h"
33 #include "MmsPluginCodec.h"
34 #include "MmsPluginStorage.h"
35 #include "MsgMmsMessage.h"
36 #include "MsgGconfWrapper.h"
37 #include "MmsPluginUtil.h"
38 #include "MmsPluginSMILValidate.h"
39
40
41 bool MmsInitMsgAttrib(MmsAttrib *pAttrib)
42 {
43         MSG_DEBUG("MmsInitMsgAttrib");
44
45         pAttrib->bAskDeliveryReport = false;
46
47         pAttrib->bAskReadReply = false;
48         pAttrib->bRead = false;
49         pAttrib->bReportAllowed = false;
50         pAttrib->readReportAllowedType = MMS_RECEIVE_READ_REPORT_ALLOWED;
51         pAttrib->readReportSendStatus = MMS_RECEIVE_READ_REPORT_NO_SEND;
52         pAttrib->bReadReportSent = false;
53
54         pAttrib->bHideAddress = false;
55         pAttrib->date = 0;
56         pAttrib->bUseDeliveryCustomTime = false;
57         pAttrib->deliveryTime.type = MMS_TIMETYPE_RELATIVE;
58         pAttrib->deliveryTime.time = 0;
59         pAttrib->bUseExpiryCustomTime = false;
60         pAttrib->expiryTime.type = MMS_TIMETYPE_RELATIVE;
61         pAttrib->expiryTime.time = 0;
62         memset(&pAttrib->expiryTime, 0, sizeof(MmsTimeStruct));
63         pAttrib->msgClass = MMS_MSGCLASS_PERSONAL;
64         pAttrib->msgStatus = MSG_DELIVERY_REPORT_NONE;
65         pAttrib->priority = MMS_PRIORITY_NORMAL;
66         pAttrib->responseStatus = MMS_RESPSTATUS_ERROR;
67         pAttrib->retrieveStatus = MMS_RETRSTATUS_ERROR;
68         pAttrib->contentType = MIME_UNKNOWN;
69         pAttrib->msgSize = 0;
70         pAttrib->bLeaveCopy = true;
71
72         pAttrib->specialMsgType = MMS_SPECIAL_MSG_TYPE_NONE;
73 #ifdef __SUPPORT_DRM__
74         pAttrib->drmType = MSG_DRM_TYPE_NONE;
75         pAttrib->roWaitingTimerMax = 0;
76         pAttrib->pszDrmData = NULL;
77 #endif
78         pAttrib->version = MMS_VERSION;
79
80         memset(pAttrib->szFrom, 0, MSG_LOCALE_ADDR_LEN + 10);
81         memset(pAttrib->szResponseText, 0, MMS_LOCALE_RESP_TEXT_LEN + 1);
82         memset(pAttrib->szRetrieveText, 0, MMS_LOCALE_RESP_TEXT_LEN + 1);
83         memset(pAttrib->szSubject, 0, MSG_LOCALE_SUBJ_LEN + 1);
84         pAttrib->szTo = NULL;
85         pAttrib->szCc = NULL;
86         pAttrib->szBcc = NULL;
87
88         pAttrib->pMultiStatus = NULL;
89
90         pAttrib->replyCharge.chargeType = MMS_REPLY_NONE;
91         memset(&pAttrib->replyCharge.deadLine , 0, sizeof(MmsTimeStruct));
92         pAttrib->replyCharge.chargeSize = 0;
93         memset(pAttrib->replyCharge.szChargeID, 0, MMS_MSG_ID_LEN);
94
95         return true;
96 }
97
98 bool MmsInitMsgType(MsgType *pMsgType)
99 {
100         MSG_DEBUG("MmsInitMsgType");
101         pMsgType->offset = 0;
102         pMsgType->size = 0;
103         pMsgType->contentSize = 0;
104         pMsgType->disposition = -1;
105         pMsgType->encoding = -1;
106         pMsgType->type = MIME_UNKNOWN;
107         pMsgType->section = -1;
108
109         pMsgType->szOrgFilePath[0] = '\0';
110         pMsgType->szContentID[0] = '\0';
111         pMsgType->szContentLocation[0] = '\0';
112
113         pMsgType->szContentRepPos[0] = '\0';
114         pMsgType->szContentRepSize[0] = '\0';
115         pMsgType->szContentRepIndex[0] = '\0';
116
117         MmsInitMsgContentParam(&pMsgType->param);
118 #ifdef __SUPPORT_DRM__
119         __MsgInitMsgDRMInfo(&pMsgType->drmInfo);
120 #endif
121
122         return true;
123 }
124
125 bool MmsInitMsgBody(MsgBody *pMsgBody)
126 {
127         MSG_DEBUG("MmsInitMsgBody");
128         pMsgBody->offset = 0;
129         pMsgBody->size = 0;
130         pMsgBody->body.pText = NULL;
131
132         MmsInitMsgType(&pMsgBody->presentationType);
133         pMsgBody->pPresentationBody = NULL;
134
135         memset(pMsgBody->szOrgFilePath, 0, MSG_FILEPATH_LEN_MAX);
136
137         return true;
138 }
139
140 bool MmsInitMsgContentParam(MsgContentParam *pMsgContentParam)
141 {
142         MSG_DEBUG("MmsInitMsgContentParam");
143         pMsgContentParam->charset = MSG_CHARSET_UNKNOWN;
144         pMsgContentParam->type = MIME_UNKNOWN;
145         pMsgContentParam->szBoundary[0] = '\0';
146         pMsgContentParam->szFileName[0] = '\0';
147         pMsgContentParam->szName[0] = '\0';
148         pMsgContentParam->szStart[0] = '\0';
149         pMsgContentParam->szStartInfo[0] = '\0';
150         pMsgContentParam->pPresentation = NULL;
151         pMsgContentParam->reportType = MSG_PARAM_REPORT_TYPE_UNKNOWN; // only used as parameter of Content-Type: multipart/report; report-type
152 #ifdef FEATURE_JAVA_MMS
153         pMsgContentParam->szApplicationID = NULL;
154         pMsgContentParam->szReplyToApplicationID = NULL;
155 #endif
156         return true;
157 }
158
159 bool MmsSetMsgAddressList(MmsAttrib *pAttrib, const MSG_MESSAGE_INFO_S * pMsgInfo)
160 {
161         MSG_DEBUG("MmsSetMsgAddressList");
162         pAttrib->szTo = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_TO);
163         MSG_DEBUG("To address: %s", pAttrib->szTo);
164         pAttrib->szCc = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_CC);
165         MSG_DEBUG("Cc address: %s", pAttrib->szCc);
166         pAttrib->szBcc = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_BCC);
167         MSG_DEBUG("Bcc address: %s", pAttrib->szBcc);
168
169         return true;
170 }
171
172 void MmsSetMsgMultiStatus(MmsAttrib *pAttrib, const MSG_MESSAGE_INFO_S *pMsgInfo)
173 {
174         int     nAddressCnt = 0;
175
176         nAddressCnt = pMsgInfo->nAddressCnt;
177
178         for (int i = 0; i < nAddressCnt; ++i) {
179                 pAttrib->pMultiStatus = (MmsMsgMultiStatus *)malloc(sizeof(MmsMsgMultiStatus));
180
181                 memset(pAttrib->pMultiStatus->szTo, 0, MAX_ADDRESS_VAL_LEN + 1);
182                 strncpy(pAttrib->pMultiStatus->szTo, pMsgInfo->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
183
184                 MSG_DEBUG("### pMultistatus->szTo = %s ####", pAttrib->pMultiStatus->szTo);
185                 pAttrib->pMultiStatus->bDeliveryReportIsRead = false;
186                 pAttrib->pMultiStatus->bDeliveyrReportIsLast = false;
187                 pAttrib->pMultiStatus->msgStatus = MMS_MSGSTATUS_NONE;
188                 pAttrib->pMultiStatus->handledTime = 0;
189                 pAttrib->pMultiStatus->bReadReplyIsRead = false;
190                 pAttrib->pMultiStatus->bReadReplyIsLast = false;
191                 pAttrib->pMultiStatus->readStatus = MMS_READSTATUS_NONE;
192                 pAttrib->pMultiStatus->readTime = 0;
193
194                 pAttrib->pMultiStatus = pAttrib->pMultiStatus->pNext;
195         }
196 }
197
198 char *MmsComposeAddress(const MSG_MESSAGE_INFO_S *pMsgInfo, int recipientType)
199 {
200         MSG_DEBUG("MmsComposeAddress");
201         int     addrLen = 0;
202         int     nAddressCnt = 0;
203         int nRecpCnt = 0;
204         char pString[MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3] = {0, };
205         char *szCompose;
206
207         nAddressCnt = pMsgInfo->nAddressCnt;
208
209         // Calculate allocated buffer size
210         for (int i = 0; i < nAddressCnt; ++i) {
211                 MSG_DEBUG("recipientType: %d, address value: %s", pMsgInfo->addressList[i].recipientType, pMsgInfo->addressList[i].addressVal);
212                 if (pMsgInfo->addressList[i].recipientType == recipientType) {
213                         if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_PLMN) {
214                                 addrLen += strlen(MsgGetString(MSG_ADDR_TYPE, MSG_ADDR_TYPE_PHONE));
215                                 addrLen += strlen(pMsgInfo->addressList[i].addressVal);
216                         } else if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_EMAIL) {
217                                 addrLen += strlen(pMsgInfo->addressList[i].addressVal);
218                         } else
219                                 ; // Need to consider IPV4, IPV6, and Alias formatted address
220
221                         nRecpCnt++;
222                 }
223         }
224
225         if (nRecpCnt > 1)
226                 addrLen = addrLen + nRecpCnt - 1;
227         szCompose = (char *)calloc(addrLen + 1, 1);
228
229         // Address String copy
230         for (int i = 0; i < nAddressCnt; ++i) {
231                 if (pMsgInfo->addressList[i].recipientType == recipientType) {
232                         if (strlen(szCompose) > 0)
233                                 strcat(szCompose, MSG_STR_ADDR_DELIMETER);
234
235                         memset(pString, 0x00, (MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3) * sizeof(char));
236                         if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_PLMN) {
237                                 snprintf(pString, MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3, "%s%s", pMsgInfo->addressList[i].addressVal, MsgGetString(MSG_ADDR_TYPE, MSG_ADDR_TYPE_PHONE));
238                                 MSG_DEBUG("%s", pString);
239                         } else if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_EMAIL) {
240                                 snprintf(pString, MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3, "%s", pMsgInfo->addressList[i].addressVal);
241                         } else
242                                 ; // Need to consider IPV4, IPV6, and Alias formatted address
243
244                         strcat(szCompose, pString);
245                 }
246         }
247
248         return szCompose;
249 }
250
251
252 bool MmsGetMsgBodyfromMsgInfo(const MSG_MESSAGE_INFO_S *pMsgInfo, MMS_MESSAGE_DATA_S *pMsgBody, char *pFileData)
253 {
254         MSG_DEBUG("MmsGetMsgBodyfromMsgInfo");
255         memset(pMsgBody, 0, sizeof(MMS_MESSAGE_DATA_S));
256
257         if (pMsgInfo->bTextSms == false) {       //if  the message body was stored in file.
258                 _MsgMmsDeserializeMessageData(pMsgBody, pFileData);
259         }
260
261         return true;
262 }
263
264 int MmsGetSmilRawData(MMS_MESSAGE_DATA_S *pMsgBody, char **pRawdata)
265 {
266         MSG_BEGIN();
267
268         if (MsgReadSmilFile(pMsgBody->szSmilFilePath, pRawdata) < 0)
269                 return false;
270
271         MsgDeleteSmilFile(pMsgBody->szSmilFilePath);
272
273         MSG_END();
274
275         return true;
276 }
277
278
279 bool MmsInsertPresentation(MmsMsg *pMsg, MsgContentType mimeType, char *pData, int size)
280 {
281         MSG_DEBUG("MmsInsertPresentation");
282
283         if (pMsg == NULL) {
284                 MSG_DEBUG("pMsg is NULL");
285                 return false;
286         }
287
288         if (pMsg->msgBody.pPresentationBody != NULL)
289                 goto __CATCH;
290
291         memset(&pMsg->msgBody.presentationType, 0, sizeof(MsgType));
292         pMsg->msgBody.pPresentationBody = (MsgBody *)malloc(sizeof(MsgBody));
293         if (pMsg->msgBody.pPresentationBody == NULL)
294                 goto __CATCH;
295
296         MmsInitMsgBody(pMsg->msgBody.pPresentationBody);
297
298         pMsg->msgBody.pPresentationBody->body.pText = (char *)malloc(size + 1);
299         if (pMsg->msgBody.pPresentationBody->body.pText == NULL)
300                 goto __CATCH;
301
302         pMsg->msgBody.pPresentationBody->size = size;
303         pMsg->msgBody.presentationType.type = mimeType;
304         pMsg->msgBody.presentationType.param.charset = MSG_CHARSET_UTF8;
305         snprintf(pMsg->msgBody.presentationType.szContentID, MSG_MSG_ID_LEN + 1, "<_S_>");
306
307         snprintf(pMsg->msgType.param.szStart, MSG_MSG_ID_LEN + 1, "%s", pMsg->msgBody.presentationType.szContentID);
308         pMsg->msgType.param.type = mimeType;
309
310         memset(pMsg->msgBody.pPresentationBody->body.pText, 0, size + 1);
311         strncpy(pMsg->msgBody.pPresentationBody->body.pText, pData, size);
312
313         return true;
314
315 __CATCH:
316
317         if (pMsg->msgBody.pPresentationBody != NULL) {
318                 if (pMsg->msgBody.pPresentationBody->body.pText != NULL) {
319                         free(pMsg->msgBody.pPresentationBody->body.pText);
320                         pMsg->msgBody.pPresentationBody->body.pText = NULL;
321                 }
322
323                 free(pMsg->msgBody.pPresentationBody);
324                 pMsg->msgBody.pPresentationBody = NULL;
325         }
326
327         return false;
328 }
329
330
331 bool MmsInsertPartFromFile(MmsMsg *pMsg, char *szTitleName, char *szOrgFilePath, char *szContentID)
332 {
333         MSG_DEBUG("MmsInsertPartFromFile");
334
335         MsgMultipart *pMultipart = NULL;
336         MsgMultipart *pLastPart = NULL;
337         int nFileSize;
338         MsgContentType mimeType = MIME_UNKNOWN;
339         char *pExt = NULL;
340
341         pExt = strrchr(szOrgFilePath, '.');
342
343         if (pExt == NULL || pExt[0] == '\0' || strrchr(pExt, '/'))
344                 mimeType = MIME_UNKNOWN;
345         else {
346                 if (strcasecmp(pExt, ".dcf") == 0)
347                         mimeType = MIME_APPLICATION_VND_OMA_DRM_CONTENT;
348                 else {
349                         if (MmsGetTypeByFileName((int *)&mimeType, szOrgFilePath) == false)
350                                 goto __CATCH;
351                 }
352         }
353
354         if (mimeType == MIME_UNKNOWN)
355                 mimeType = MIME_APPLICATION_OCTET_STREAM;
356
357         if (MmsIsMultipart(pMsg->msgType.type) == true) {
358                 /* Insert as a multipart */
359                 if (MsgGetFileSize(szOrgFilePath, &nFileSize) == false) {
360                         MSG_DEBUG("MsgGetFileSize: failed");
361                         goto __CATCH;
362                 }
363
364                 pMultipart = MmsMakeMultipart(mimeType, szTitleName, szOrgFilePath, NULL, 0, nFileSize, szContentID);
365                 if (pMultipart == NULL)
366                         goto __CATCH;
367
368                 if (pMsg->mmsAttrib.contentType == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
369                         pMsg->mmsAttrib.contentType == MIME_MULTIPART_MIXED)
370                         pMultipart->type.disposition = MSG_DISPOSITION_ATTACHMENT;
371
372                 if (pMsg->msgBody.body.pMultipart == NULL) {
373                         pMsg->msgBody.body.pMultipart = pMultipart;
374                 } else {
375                         pLastPart = pMsg->msgBody.body.pMultipart;
376                         while (pLastPart->pNext) {
377                                 pLastPart = pLastPart->pNext;
378                         }
379
380                         pLastPart->pNext = pMultipart;
381                 }
382
383                 pMsg->msgBody.size += pMultipart->pBody->size;
384                 pMsg->msgType.contentSize += pMultipart->pBody->size;
385         } else {
386                 /* Single part - Insert as a message body */
387                 if (pMsg->mmsAttrib.contentType != mimeType || pMsg->msgType.type != mimeType)
388                         goto __CATCH;
389
390                 strncpy(pMsg->msgType.param.szName, szTitleName, MSG_LOCALE_FILENAME_LEN_MAX);
391
392                 if (MmsIsText(pMsg->msgType.type) == true) {
393                         pMsg->msgType.param.charset = MSG_CHARSET_UTF8;
394                         if (pMultipart)
395                                 pMultipart->type.encoding       = MSG_ENCODING_8BIT;
396                 } else {
397                         if (pMultipart)
398                                 pMultipart->type.encoding       = MSG_ENCODING_BINARY;
399                 }
400
401                 strncpy(pMsg->msgBody.szOrgFilePath, szOrgFilePath, MSG_FILEPATH_LEN_MAX - 1);
402                 if (MsgGetFileSize(szOrgFilePath, &nFileSize) == false) {
403                         MSG_DEBUG("MsgGetFileSize: failed");
404                         goto __CATCH;
405                 }
406
407                 pMsg->msgBody.offset = 0;
408                 pMsg->msgBody.size = nFileSize;
409                 pMsg->msgType.contentSize = nFileSize;
410         }
411
412         pMsg->nPartCount++;
413
414         return true;
415
416 __CATCH:
417         return false;
418
419 }
420
421 bool MmsIsMultipart(int type)
422 {
423         MSG_DEBUG("MmsIsMultipart");
424         if (type == MIME_MULTIPART_RELATED ||
425                 type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
426                 type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED ||
427                 type == MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC ||
428                 type == MIME_MULTIPART_MIXED ||
429                 type == MIME_MULTIPART_REPORT) {
430                 return true;
431         } else {
432                 return false;
433         }
434 }
435
436 bool MmsIsText(int type)
437 {
438         if (type == MIME_TEXT_PLAIN ||
439                 type == MIME_TEXT_HTML ||
440                 type == MIME_TEXT_VND_WAP_WML ||
441                 type == MIME_TEXT_X_VCARD ||
442                 type == MIME_TEXT_X_VCALENDAR ||
443                 type == MIME_TEXT_X_VNOTE ||
444                 type == MIME_APPLICATION_SMIL ||
445                 type == MIME_TEXT_X_IMELODY) {
446                 MSG_DEBUG("MmsIsText true.");
447                 return true;
448         } else {
449                 MSG_DEBUG("MmsIsText false.");
450                 return false;
451         }
452 }
453
454 bool MmsIsVitemContent (int type, char *pszName)
455 {
456         switch (type) {
457
458 /*
459 *       To make Encoding information right.
460 *               case MIME_TEXT_X_VCARD :
461 *               case MIME_TEXT_X_VCALENDAR :
462 *               case MIME_TEXT_X_VNOTE :        // vnt
463 *               {
464 *                       MSG_DEBUG("MmsIsVitemContent true.");
465 *                       return true;
466 *               }
467 *
468 */
469         case MIME_TEXT_X_VCARD:
470         case MIME_TEXT_X_VCALENDAR:
471         case MIME_TEXT_X_VNOTE: // vnt
472         case MIME_TEXT_PLAIN:           // vbm - It SHOULD be distinguished from a normal text file.
473                 {
474                         char *pszExt = NULL;
475
476                         if (!pszName)
477                                 break;
478
479                         // search file extension.
480                         if ((pszExt = strrchr(pszName, '.')) == NULL)
481                                 break;
482
483                         if (!strcasecmp(pszExt, ".vbm")) {
484                                 MSG_DEBUG("MmsIsVitemContent true.");
485                                 return true;
486                         }
487                 }
488                 break;
489
490         default:
491                 break;
492         }
493
494         MSG_DEBUG("MmsIsVitemContent false.");
495         return false;
496 }
497
498
499
500 MsgMultipart *MmsAllocMultipart(void)
501 {
502         MsgMultipart *pMultipart = NULL;
503
504         pMultipart = (MsgMultipart *)malloc(sizeof(MsgMultipart));
505
506         if (pMultipart == NULL)
507                 goto __CATCH;
508
509         pMultipart->pBody = (MsgBody *)malloc(sizeof(MsgBody));
510
511         if (pMultipart == NULL)
512                 goto __CATCH;
513
514         MmsInitMsgType(&pMultipart->type);
515         MmsInitMsgBody(pMultipart->pBody);
516
517         pMultipart->pNext = NULL;
518
519         return pMultipart;
520
521 __CATCH:
522
523         if (pMultipart) {
524                 if (pMultipart->pBody) {
525                         free(pMultipart->pBody);
526                         pMultipart->pBody = NULL;
527                 }
528
529                 free(pMultipart);
530                 pMultipart = NULL;
531         }
532
533         return NULL;
534 }
535
536
537
538 MsgMultipart *MmsMakeMultipart(MsgContentType mimeType, char *szTitleName, char *szOrgFilePath, void *pData, int offset, int size, char *szContentID)
539 {
540         MsgMultipart *pMultipart = NULL;
541
542         if ((pMultipart = MmsAllocMultipart()) == NULL)
543                 goto __CATCH;
544
545         pMultipart->type.type = mimeType;
546
547         if (szTitleName && szTitleName[0]) {
548                 memset(pMultipart->type.param.szName, 0, MSG_LOCALE_FILENAME_LEN_MAX + 1);
549                 strncpy(pMultipart->type.param.szName, szTitleName, MSG_LOCALE_FILENAME_LEN_MAX);
550         }
551
552         if (szContentID && szContentID[0]) {
553                 memset(pMultipart->type.szContentID, 0, MSG_MSG_ID_LEN + 1);
554                 snprintf(pMultipart->type.szContentID, MSG_MSG_ID_LEN + 1, "<%s>", szContentID);
555
556                 memset(pMultipart->type.szContentLocation, 0, MSG_MSG_ID_LEN + 1);
557                 snprintf(pMultipart->type.szContentLocation, MSG_MSG_ID_LEN + 1, "%s", szContentID);
558         }
559
560         if (MmsIsText(mimeType) == true) {
561                 if (!MmsIsVitemContent (mimeType, pMultipart->type.param.szName)) {
562                         pMultipart->type.param.charset = MSG_CHARSET_UTF8;
563                 }
564                 pMultipart->type.encoding = MSG_ENCODING_8BIT;
565         } else {
566                 pMultipart->type.encoding = MSG_ENCODING_BINARY;
567         }
568
569         if (szOrgFilePath == NULL) {
570                 if (pData != NULL) {
571                         if (MmsIsText(mimeType) == true) {
572                                 pMultipart->pBody->body.pText = (char *)malloc(size + 1);
573                                 if (pMultipart->pBody->body.pText == NULL)
574                                         goto __CATCH;
575
576                                 memset(pMultipart->pBody->body.pText, 0, size + 1);
577                         } else {
578                                 pMultipart->pBody->body.pBinary = malloc(size);
579                                 if (pMultipart->pBody->body.pBinary == NULL)
580                                         goto __CATCH;
581
582                                 memset(pMultipart->pBody->body.pBinary, 0, size);
583                         }
584
585                         memcpy(pMultipart->pBody->body.pBinary, pData, size);
586                         pMultipart->pBody->size = size;
587                 }
588         } else {
589                 if (szOrgFilePath)
590                         strncpy(pMultipart->pBody->szOrgFilePath, szOrgFilePath, MSG_FILEPATH_LEN_MAX - 1);
591
592                 pMultipart->pBody->offset = offset;
593                 pMultipart->pBody->size = size;
594         }
595
596         return pMultipart;
597
598 __CATCH:
599
600         if (pMultipart) {
601                 if (pMultipart->pBody) {
602                         if (pMultipart->pBody->body.pText) {
603                                 free(pMultipart->pBody->body.pText);
604                                 pMultipart->pBody->body.pText = NULL;
605                         }
606                         free(pMultipart->pBody);
607                         pMultipart->pBody = NULL;
608                 }
609                 free(pMultipart);
610                 pMultipart = NULL;
611         }
612
613         return NULL;
614 }
615
616
617 bool MmsGetTypeByFileName(int *type, char *szFileName)
618 {
619         char *pExt   = NULL;
620         AvCodecType AvType = AV_CODEC_NONE;
621
622         /* AVMS unknown or text/image file format identify type from file extention  */
623
624         pExt = strrchr(szFileName, '.');
625         if (pExt == NULL || pExt[0] == '\0')
626                 goto __CATCH;
627
628         pExt++;
629
630         if (strcasecmp(pExt, "mp4") == 0 || strcasecmp(pExt, "mpeg4") == 0 ||
631                 strcasecmp(pExt, "3gp") == 0 || strcasecmp(pExt, "3gpp") == 0) {
632                 /* Audio / Video format. If file exists already, AvGetFileCodecType() can identify the format  */
633                 if (szFileName[0] != '/')
634                         goto __CATCH;
635
636                 AvType = AvGetFileCodecType(szFileName);
637
638                 switch (AvType) {
639                 case AV_DEC_AUDIO_MPEG4:
640                         *type = MIME_AUDIO_MP4;//*type = MIME_AUDIO_3GPP;
641                         break;
642
643                 case AV_DEC_VIDEO_MPEG4:
644                         *type = MIME_VIDEO_MP4;
645                         break;
646
647                 default:
648                         *type = MIME_VIDEO_3GPP;
649                         break;
650                 }
651                 return true;
652         }
653
654         if (strcasecmp(pExt, "amr") == 0) {
655                 *type = MIME_AUDIO_AMR;
656                 return true;
657         } else if ((strcasecmp(pExt, "mid") == 0) || (strcasecmp(pExt, "midi") == 0)) {
658                 *type = MIME_AUDIO_MIDI;
659                 return true;
660         } else if (strcasecmp(pExt, "imy") == 0) {
661                 *type = MIME_TEXT_X_IMELODY;
662                 return true;
663         }
664
665         *type = MimeGetMimeFromExtInt((const char *)pExt);
666
667         return true;
668
669 __CATCH:
670
671         *type = MIME_UNKNOWN;
672         return false;
673
674 }
675
676 bool MmsComposeMessage(MmsMsg *pMmsMsg, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, MMS_MESSAGE_DATA_S *pMsgData, char *pFileData)
677 {
678         MSG_BEGIN();
679
680         char *pRawData = NULL;
681         AutoPtr<char> buf(&pRawData);
682
683         struct tm *timeInfo = NULL;
684         time_t RawTime = 0;
685         time_t nTimeInSecs = 0;
686
687         msg_error_t     err = MSG_SUCCESS;
688
689         // Initialize mmsMsg structure
690         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
691         MmsInitMsgType(&pMmsMsg->msgType);
692         MmsInitMsgBody(&pMmsMsg->msgBody);
693
694         // setting mmsMsg structure
695         pMmsMsg->mailbox = pMsgInfo->folderId;
696         pMmsMsg->msgID = pMsgInfo->msgId;
697
698         memset(pMmsMsg->szTrID, 0, MMS_TR_ID_LEN + 1);
699         memset(pMmsMsg->szContentLocation, 0, MMS_LOCATION_LEN + 1);
700         memset(pMmsMsg->szMsgID, 0, MMS_MSG_ID_LEN + 1);
701         memset(pMmsMsg->szForwardMsgID, 0, MMS_MSG_ID_LEN + 1);
702
703         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_DRAFT;
704
705         MSG_DEBUG("## delivery = %d ##", pSendOptInfo->bDeliverReq);
706         MSG_DEBUG("## read = %d ##", pSendOptInfo->option.mmsSendOptInfo.bReadReq);
707         MSG_DEBUG("## priority = %d ##", pSendOptInfo->option.mmsSendOptInfo.priority);
708         MSG_DEBUG("## expiryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.expiryTime.time);
709
710         if (pSendOptInfo->bSetting == false) {
711                 unsigned int expiryTime;
712                 MSG_MMS_DELIVERY_TIME_T deliveryTime;
713
714                 pMmsMsg->mmsAttrib.priority = (MmsPriority)MsgSettingGetInt(MMS_SEND_PRIORITY);
715
716                 MsgSettingGetBool(MMS_SEND_DELIVERY_REPORT, &pMmsMsg->mmsAttrib.bAskDeliveryReport);
717                 MsgSettingGetBool(MMS_SEND_READ_REPLY, &pMmsMsg->mmsAttrib.bAskReadReply);
718                 MsgSettingGetBool(MSG_KEEP_COPY, &pMmsMsg->mmsAttrib.bLeaveCopy);
719
720                 expiryTime = (unsigned int)MsgSettingGetInt(MMS_SEND_EXPIRY_TIME);
721
722                 if (expiryTime == 0)
723                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_NONE;
724                 else {
725                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_RELATIVE;
726                         pMmsMsg->mmsAttrib.expiryTime.time = expiryTime;
727                 }
728
729                 deliveryTime = (MSG_MMS_DELIVERY_TIME_T)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
730
731                 if (deliveryTime == MSG_DELIVERY_TIME_CUSTOM) {
732                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = true;
733
734                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
735                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_CUSTOM_DELIVERY);
736                 } else {
737                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = false;
738
739                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
740                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)deliveryTime;
741                 }
742         } else {
743                 pMmsMsg->mmsAttrib.priority = (MmsPriority)pSendOptInfo->option.mmsSendOptInfo.priority;
744                 pMmsMsg->mmsAttrib.bAskDeliveryReport = pSendOptInfo->bDeliverReq;
745                 pMmsMsg->mmsAttrib.bAskReadReply = pSendOptInfo->option.mmsSendOptInfo.bReadReq;
746                 pMmsMsg->mmsAttrib.expiryTime.type = pSendOptInfo->option.mmsSendOptInfo.expiryTime.type;
747                 pMmsMsg->mmsAttrib.bLeaveCopy = pSendOptInfo->bKeepCopy;
748
749                 if (pMmsMsg->mmsAttrib.expiryTime.type != MMS_TIMETYPE_NONE)
750                         pMmsMsg->mmsAttrib.expiryTime.time = pSendOptInfo->option.mmsSendOptInfo.expiryTime.time;
751
752                 pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime;
753                 pMmsMsg->mmsAttrib.deliveryTime.type = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.type;
754                 pMmsMsg->mmsAttrib.deliveryTime.time = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time;
755         }
756
757         MSG_DEBUG("pSendOptInfo->bSetting = %d", pSendOptInfo->bSetting);
758         MSG_DEBUG("pMmsMsg->mmsAttrib.bLeaveCopy = %d", pMmsMsg->mmsAttrib.bLeaveCopy);
759         MSG_DEBUG("pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = %d", pMmsMsg->mmsAttrib.bUseDeliveryCustomTime);
760         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.type = %d", pMmsMsg->mmsAttrib.deliveryTime.type);
761         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.time = %d", pMmsMsg->mmsAttrib.deliveryTime.time);
762
763         /* MMS-1.3-con-739 */
764         pMmsMsg->mmsAttrib.msgClass = (MmsMsgClass)MsgSettingGetInt(MMS_SEND_MSG_CLASS);
765         /* MMS-1.3-con-739 */
766 #ifdef MMS_13_CON_742_ENABLED
767         /* MMS-1.3-con-742 */
768         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
769         /* MMS-1.3-con-742 */
770 #endif
771
772         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.bAskDeliveryReport = %d @@@", pMmsMsg->mmsAttrib.bAskDeliveryReport);
773         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.bAskReadReply = %d @@@", pMmsMsg->mmsAttrib.bAskReadReply);
774         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.priority = %d @@@", pMmsMsg->mmsAttrib.priority);
775
776         // setting date
777         time(&RawTime);
778         timeInfo = localtime(&RawTime);
779         nTimeInSecs = mktime(timeInfo);
780         pMmsMsg->mmsAttrib.date = nTimeInSecs;
781
782         //setting subject
783         strcpy(pMmsMsg->mmsAttrib.szSubject, pMsgInfo->subject);
784
785         //setting adddress
786         MmsSetMsgAddressList(&pMmsMsg->mmsAttrib, pMsgInfo);
787         MmsGetMsgBodyfromMsgInfo(pMsgInfo, pMsgData, pFileData);
788
789         int pageCnt = _MsgMmsGetPageCount(pMsgData);
790
791         if (pageCnt == 0) {     // Multipart mixed
792                 pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
793                 pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
794                 MmsMakePreviewInfo(pMsgInfo->msgId, pMsgData);
795         } else {        // Multipart related
796
797                 int RawDataSize = 0;
798
799                 time_t RawTime = 0;
800                 time(&RawTime);
801                 snprintf(pMsgData->szSmilFilePath, MSG_FILEPATH_LEN_MAX, "%lu", RawTime);
802
803                 MsgMMSCreateSMIL(pMsgData);
804
805                 RawDataSize = MmsGetSmilRawData(pMsgData, &pRawData);
806                 if (RawDataSize < 0) {
807                         MSG_DEBUG("Smil file size is less than 0");
808                         return false;
809                 }
810                 MSG_DEBUG("%s", pRawData);
811                 if (pRawData)
812                         MmsInsertPresentation(pMmsMsg, MIME_APPLICATION_SMIL, pRawData, strlen(pRawData));
813
814                 pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
815                 pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
816
817                 for (int i = 0; i < pageCnt; ++i) {
818                         MMS_PAGE_S *pPage = _MsgMmsGetPage(pMsgData, i);
819                         int mediaCnt = pPage->mediaCnt;
820                         MSG_DEBUG("PAGE %d's media Cnt: %d", i+1, mediaCnt);
821
822                         for (int j = 0; j < mediaCnt; ++j) {
823                                 MMS_MEDIA_S *pMedia = _MsgMmsGetMedia(pPage, j);
824
825                                 switch (pMedia->mediatype) {
826                                 case MMS_SMIL_MEDIA_IMG:
827                                 case MMS_SMIL_MEDIA_VIDEO:
828                                 case MMS_SMIL_MEDIA_AUDIO:
829                                 case MMS_SMIL_MEDIA_TEXT:
830                                         if (pMedia->szFilePath[0] != 0) {
831                                                 if (!MmsInsertPartFromFile(pMmsMsg, pMedia->szFileName, pMedia->szFilePath, pMedia->szContentID))
832                                                         return false;
833                                         }
834                                         break;
835
836                                 default:
837                                         break;
838                                 }
839                         }
840                 }
841
842                 char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };;
843                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%d.mms", pMsgInfo->msgId);
844
845                 MmsPluginStorage *pStorage = MmsPluginStorage::instance();
846                 err = pStorage->getMsgText(pMsgData, pMsgInfo->msgText);
847                 MmsMakePreviewInfo(pMsgInfo->msgId, pMsgData);
848         }
849
850 #ifdef FEATURE_JAVA_MMS
851         MSG_DEBUG("msgAppId: valid:%d appId:%s replyToAppId:%s", pMsgData->msgAppId.valid, pMsgData->msgAppId.appId, pMsgData->msgAppId.replyToAppId);
852         if (pMsgData->msgAppId.valid) { // check if msgAppId.valid is true, both appId and replytoappId must have a meaning data
853                 if (pMsgData->msgAppId.appId[0] != 0) {
854                         pMmsMsg->msgType.param.szApplicationID = (char *)malloc(strlen(pMsgData->msgAppId.appId) + 1);
855                         if (pMmsMsg->msgType.param.szApplicationID == NULL) {
856                                 MSG_DEBUG("Error: out of Memory");
857                                 return false;
858                         }
859                         memset(pMmsMsg->msgType.param.szApplicationID, 0, strlen(pMsgData->msgAppId.appId) + 1);
860
861                         strcpy(pMmsMsg->msgType.param.szApplicationID, pMsgData->msgAppId.appId);
862                 }
863
864                 if (pMsgData->msgAppId.replyToAppId[0] != 0) {
865                         pMmsMsg->msgType.param.szReplyToApplicationID = (char *)malloc(strlen(pMsgData->msgAppId.replyToAppId) + 1);
866                         if (pMmsMsg->msgType.param.szReplyToApplicationID == NULL) {
867                                 MSG_DEBUG("Error: out of Memory");
868                                 return false;
869                         }
870                         memset(pMmsMsg->msgType.param.szReplyToApplicationID, 0, strlen(pMsgData->msgAppId.replyToAppId) + 1);
871
872                         strcpy(pMmsMsg->msgType.param.szReplyToApplicationID, pMsgData->msgAppId.replyToAppId);
873                 }
874         }
875 #endif
876
877         //Processing Attachment List
878         for (int i = 0; i < _MsgMmsGetAttachCount(pMsgData); ++i) {
879                 MMS_ATTACH_S *pMedia = _MsgMmsGetAttachment(pMsgData, i);
880                 if (pMedia->szFilePath[0] != 0) {
881                         if (!MmsInsertPartFromFile(pMmsMsg, pMedia->szFileName, pMedia->szFilePath, NULL))
882                                 return false;
883                 }
884         }
885
886         return true;
887 }
888
889 void MmsComposeNotiMessage(MmsMsg *pMmsMsg, msg_message_id_t msgID)
890 {
891         MSG_BEGIN();
892
893         struct tm *timeInfo = NULL;
894         time_t RawTime = 0;
895         time_t nTimeInSecs = 0;
896
897         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
898         MmsInitMsgType(&pMmsMsg->msgType);
899         MmsInitMsgBody(&pMmsMsg->msgBody);
900
901         pMmsMsg->msgID = msgID;
902
903         pMmsMsg->mmsAttrib.version = mmsHeader.version;
904
905         // setting date
906         time(&RawTime);
907         timeInfo = localtime(&RawTime);
908         nTimeInSecs = mktime(timeInfo);
909         pMmsMsg->mmsAttrib.date = nTimeInSecs;
910
911         pMmsMsg->mmsAttrib.bReportAllowed = mmsHeader.reportAllowed;
912         pMmsMsg->mmsAttrib.bAskDeliveryReport = mmsHeader.deliveryReport;
913
914         MSG_DEBUG("######## Version = %d ########", pMmsMsg->mmsAttrib.version);
915
916         strncpy(pMmsMsg->szTrID, mmsHeader.szTrID, MMS_TR_ID_LEN);
917         strncpy(pMmsMsg->szMsgID, mmsHeader.szMsgID, MMS_MSG_ID_LEN);
918         pMmsMsg->szForwardMsgID[0] = '\0';
919
920         if (mmsHeader.pFrom) {
921                 MmsAddrUtilRemovePlmnString(mmsHeader.pFrom->szAddr );
922                 strncpy(pMmsMsg->mmsAttrib.szFrom, mmsHeader.pFrom->szAddr, MSG_LOCALE_ADDR_LEN + 9);
923         }
924
925         strncpy(pMmsMsg->mmsAttrib.szSubject, mmsHeader.szSubject, MSG_LOCALE_SUBJ_LEN);
926         strncpy(pMmsMsg->szContentLocation, mmsHeader.szContentLocation, MMS_LOCATION_LEN);
927
928         pMmsMsg->mmsAttrib.msgClass = mmsHeader.msgClass;
929         pMmsMsg->mmsAttrib.msgSize = mmsHeader.msgSize;
930         pMmsMsg->mmsAttrib.expiryTime.type = mmsHeader.expiryTime.type;
931         pMmsMsg->mmsAttrib.expiryTime.time = mmsHeader.expiryTime.time;
932         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_NOTIFY;
933         pMmsMsg->mmsAttrib.bRead = false;
934         pMmsMsg->mailbox = MSG_INBOX_ID;
935
936         pMmsMsg->mmsAttrib.replyCharge.chargeType = mmsHeader.replyCharge.chargeType;
937         pMmsMsg->mmsAttrib.replyCharge.deadLine.type = mmsHeader.replyCharge.deadLine.type;
938         pMmsMsg->mmsAttrib.replyCharge.deadLine.time = mmsHeader.replyCharge.deadLine.time;
939         pMmsMsg->mmsAttrib.replyCharge.chargeSize = mmsHeader.replyCharge.chargeSize;
940
941         strncpy(pMmsMsg->mmsAttrib.replyCharge.szChargeID, mmsHeader.replyCharge.szChargeID, MMS_MSG_ID_LEN);
942
943         MSG_END();
944 }
945
946 bool MmsGetMmsMessageBody(MmsMsg *pMmsMsg, char *retrievedFilePath)
947 {
948         FILE *pFile     = NULL;
949         MsgMultipart *pMultipart = NULL;
950         int attachmax = MSG_ATTACH_MAX;
951         int nSize = 0;
952
953         /*      read from MMS raw file  */
954         if (retrievedFilePath)
955                 strncpy(pMmsMsg->szFileName, retrievedFilePath + strlen(MSG_DATA_PATH), strlen(retrievedFilePath + strlen(MSG_DATA_PATH)));
956         else
957                 goto __CATCH;
958
959         pFile = MsgOpenFile(retrievedFilePath, "rb");
960
961         if (pFile == NULL) {
962                 MSG_DEBUG( "_MmsReadMsgBody: invalid mailbox\n");
963                 goto __CATCH;
964         }
965
966         if (MsgGetFileSize(retrievedFilePath, &nSize) == false) {
967                 MSG_DEBUG("MsgGetFileSize: failed");
968                 goto __CATCH;
969         }
970
971         _MmsRegisterDecodeBuffer(gszMmsLoadBuf1,
972                                                           gszMmsLoadBuf2,
973                                                           MSG_MMS_DECODE_BUFFER_MAX);
974
975         if (MmsBinaryDecodeMsgHeader(pFile, nSize) == false) {
976                 MSG_DEBUG( "_MmsReadMsgBody: MmsBinaryDecodeMsgHeader fail...\n");
977                 goto __CATCH;
978         }
979
980         if (MmsBinaryDecodeMsgBody(pFile, retrievedFilePath, nSize) == false) {
981                 MSG_DEBUG( "_MmsReadMsgBody: MmsBinaryDecodeMsgBody fail\n");
982                 goto __CATCH;
983         }
984
985         /* Set mmsHeader.msgType & msgBody to pMsg ----------- */
986
987         pMmsMsg->mmsAttrib.contentType = (MsgContentType)mmsHeader.msgType.type;
988
989         memcpy(&(pMmsMsg->msgType), &(mmsHeader.msgType), sizeof(MsgType));
990         memcpy(&(pMmsMsg->msgBody), &(mmsHeader.msgBody), sizeof(MsgBody));
991
992         MSG_DEBUG("#############################");
993         MSG_DEBUG("## pMmsMsg->msgType.type = %d ##", pMmsMsg->msgType.type);
994         MSG_DEBUG("## pMmsMsg->msgType.szContentID = %s ##", pMmsMsg->msgType.szContentID);
995         MSG_DEBUG("## pMmsMsg->msgType.szContentLocation = %s ##", pMmsMsg->msgType.szContentLocation);
996         MSG_DEBUG("#############################");
997
998         if (pMmsMsg->msgBody.pPresentationBody) {
999                 if(MsgFseek(pFile, pMmsMsg->msgBody.pPresentationBody->offset, SEEK_SET) < 0)
1000                         goto __CATCH;
1001
1002                 pMmsMsg->msgBody.pPresentationBody->body.pText = (char *)malloc(pMmsMsg->msgBody.pPresentationBody->size + 1);
1003                 if (pMmsMsg->msgBody.pPresentationBody->body.pText == NULL)
1004                         goto __CATCH;
1005
1006                 memset(pMmsMsg->msgBody.pPresentationBody->body.pText, 0, pMmsMsg->msgBody.pPresentationBody->size + 1);
1007                 ULONG nRead = 0;
1008                 nRead = MsgReadFile(pMmsMsg->msgBody.pPresentationBody->body.pText, sizeof(char), pMmsMsg->msgBody.pPresentationBody->size, pFile);
1009
1010                 if (nRead == 0)
1011                         goto __CATCH;
1012         }
1013
1014         MsgCloseFile(pFile);
1015         pFile = NULL;
1016
1017         /* nPartCount */
1018         pMmsMsg->nPartCount = 0;
1019
1020         if (MsgIsMultipart(mmsHeader.msgType.type) == true) {
1021                 pMultipart = pMmsMsg->msgBody.body.pMultipart;
1022                 while (pMultipart) {
1023                         pMmsMsg->nPartCount++;
1024
1025                         if (pMultipart->type.type == MIME_TEXT_PLAIN)
1026                                 attachmax++;
1027
1028                         if ((mmsHeader.msgType.type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED) ||
1029                                 (mmsHeader.msgType.type == MIME_MULTIPART_MIXED)) {
1030                                 if ((pMmsMsg->nPartCount >= attachmax ) && (pMultipart->pNext != NULL)) {
1031                                         _MsgFreeBody(pMultipart->pNext->pBody, pMultipart->pNext->type.type);
1032
1033                                         free(pMultipart->pNext->pBody);
1034                                         pMultipart->pNext->pBody = NULL;
1035
1036                                         free(pMultipart->pNext);
1037
1038                                         pMultipart->pNext = NULL;
1039                                         break;
1040                                 }
1041                         }
1042                         pMultipart = pMultipart->pNext;
1043                 }
1044         } else {
1045                 if (pMmsMsg->msgBody.size > 0)
1046                         pMmsMsg->nPartCount++;
1047         }
1048
1049         //call before processing urgent event.
1050         _MmsInitHeader();
1051         _MmsUnregisterDecodeBuffer();
1052
1053         return true;
1054
1055 __CATCH:
1056
1057         _MmsInitHeader();
1058         _MmsUnregisterDecodeBuffer();
1059
1060         if (pFile != NULL) {
1061                 MsgCloseFile(pFile);
1062                 pFile = NULL;
1063         }
1064
1065         _MsgFreeBody(&pMmsMsg->msgBody, pMmsMsg->msgType.type);
1066         MSG_DEBUG("_MmsReadMsgBody:    E  N  D    ( fail )    ******************** \n");
1067
1068         return false;
1069 }
1070
1071 #ifdef MMS_DELIEVERY_IND_ENABLED
1072 MmsMsgMultiStatus *MmsComposeDeliveryIndMessage(MmsMsg *pMmsMsg, msg_message_id_t msgId)
1073 {
1074         MmsMsgMultiStatus *pStatus = NULL;
1075         MmsMsgMultiStatus *pLastStatus = NULL;
1076         bool bFound = false;
1077
1078         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
1079         MmsInitMsgType(&pMmsMsg->msgType);
1080         MmsInitMsgBody(&pMmsMsg->msgBody);
1081
1082         pMmsMsg->mmsAttrib.version = mmsHeader.version;
1083
1084         pMmsMsg->mmsAttrib.pMultiStatus = MmsGetMultiStatus(msgId);
1085
1086         pStatus = pMmsMsg->mmsAttrib.pMultiStatus;
1087
1088         MSG_DEBUG("### pStatus->szTo = %s ###", pStatus->szTo);
1089
1090         while (pStatus && !bFound) {
1091                 MSG_DEBUG("### MmsAddrUtilCompareAddr ###");
1092                 MSG_DEBUG("### mmsHeader.pTo->szAddr = %s ###", mmsHeader.pTo->szAddr);
1093                 if (MmsAddrUtilCompareAddr(pStatus->szTo, mmsHeader.pTo->szAddr)) {
1094                         bFound = true;
1095                         break;
1096                 }
1097
1098                 pStatus = pStatus->pNext;
1099         }
1100
1101         if (bFound == false) {
1102                 MSG_DEBUG("### bFound == false ###");
1103                 /* Queue the delivery report  --------------------------- */
1104
1105                 pStatus = (MmsMsgMultiStatus *)malloc(sizeof(MmsMsgMultiStatus));
1106                 memset(pStatus, 0, sizeof(MmsMsgMultiStatus));
1107
1108                 pStatus->readStatus = MMS_READSTATUS_NONE;
1109
1110                 memset(pStatus->szTo, 0, MSG_ADDR_LEN + 1);
1111                 strncpy(pStatus->szTo, mmsHeader.pTo->szAddr, MSG_ADDR_LEN);
1112
1113                 if (pMmsMsg->mmsAttrib.pMultiStatus == NULL) {
1114                         /* first delivery report */
1115                         pMmsMsg->mmsAttrib.pMultiStatus = pStatus;
1116                 } else {
1117                         pLastStatus = pMmsMsg->mmsAttrib.pMultiStatus;
1118                         while (pLastStatus->pNext) {
1119                                 pLastStatus = pLastStatus->pNext;
1120                         }
1121
1122                         pLastStatus->pNext = pStatus;
1123                         pLastStatus = pStatus;
1124                 }
1125         }
1126
1127         pStatus->handledTime = mmsHeader.date;
1128         pStatus->msgStatus = mmsHeader.msgStatus;
1129         pStatus->bDeliveryReportIsRead = false;
1130
1131         _MmsDataUpdateLastStatus(pMmsMsg);
1132
1133         pStatus->bDeliveyrReportIsLast = true;
1134
1135         return pStatus;
1136 }
1137 #endif
1138
1139 void MmsComposeReadReportMessage(MmsMsg *pMmsMsg, const MSG_MESSAGE_INFO_S *pMsgInfo, msg_message_id_t selectedMsgId)
1140 {
1141         struct tm *timeInfo = NULL;
1142         time_t RawTime = 0;
1143         time_t nTimeInSecs = 0;
1144
1145         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
1146         MmsInitMsgType(&pMmsMsg->msgType);
1147         MmsInitMsgBody(&pMmsMsg->msgBody);
1148
1149         // setting mmsMsg structure
1150         pMmsMsg->mailbox = pMsgInfo->folderId;
1151         pMmsMsg->msgID = pMsgInfo->msgId;
1152
1153         memset(pMmsMsg->szTrID, 0, MMS_TR_ID_LEN + 1);
1154         memset(pMmsMsg->szContentLocation, 0, MMS_LOCATION_LEN + 1);
1155         memset(pMmsMsg->szForwardMsgID, 0, MMS_MSG_ID_LEN + 1);
1156
1157         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_DRAFT;
1158
1159         // setting date
1160         time(&RawTime);
1161         timeInfo = localtime(&RawTime);
1162         nTimeInSecs = mktime(timeInfo);
1163         pMmsMsg->mmsAttrib.date = nTimeInSecs;
1164
1165         // setting szMsgId
1166         MmsPluginStorage::instance()->getMmsMessageId(selectedMsgId, pMmsMsg);
1167
1168         //setting subject
1169         strcpy(pMmsMsg->mmsAttrib.szSubject, pMsgInfo->subject);
1170
1171         //setting adddress
1172         MmsSetMsgAddressList(&pMmsMsg->mmsAttrib, pMsgInfo);
1173
1174         if (pMmsMsg->mmsAttrib.szTo)
1175                 strncpy(pMmsMsg->mmsAttrib.szFrom, pMmsMsg->mmsAttrib.szTo, strlen(pMmsMsg->mmsAttrib.szTo));
1176 }
1177
1178 int MmsSearchMsgId(char *toNumber, char *szMsgID)
1179 {
1180         MSG_BEGIN();
1181
1182         int msgId;
1183
1184         msgId = MmsPluginStorage::instance()->searchMsgId(toNumber, szMsgID);
1185
1186         if (msgId < 0)
1187                 return 0;
1188
1189         MSG_END();
1190
1191         return msgId;
1192 }
1193
1194 MmsMsgMultiStatus *MmsGetMultiStatus(msg_message_id_t msgId)
1195 {
1196         MmsMsgMultiStatus *pMultiStatus;
1197
1198         pMultiStatus = MmsPluginStorage::instance()->getMultiStatus(msgId);
1199
1200         MSG_DEBUG("### szTo = %s ###", pMultiStatus->szTo);
1201
1202         MSG_END();
1203
1204         return pMultiStatus;
1205 }
1206
1207 bool MmsCheckAdditionalMedia(MMS_MESSAGE_DATA_S *pMsgData, MsgType *partHeader)
1208 {
1209         if (_MsgMmsFindMatchedMedia(pMsgData, partHeader->param.szFileName))
1210                 return false;
1211         else
1212                 return true;
1213 }
1214
1215 #ifdef __SUPPORT_DRM__
1216 bool __MsgInitMsgDRMInfo(MsgDRMInfo *pMsgDrmInfo)
1217 {
1218         pMsgDrmInfo->contentType = MIME_UNKNOWN;
1219         pMsgDrmInfo->drmType = MSG_DRM_TYPE_NONE;
1220
1221         pMsgDrmInfo->szContentName = NULL;
1222         pMsgDrmInfo->szContentURI = NULL;
1223         pMsgDrmInfo->szContentDescription = NULL;
1224         pMsgDrmInfo->szContentVendor = NULL;
1225         pMsgDrmInfo->szRightIssuer = NULL;
1226         pMsgDrmInfo->szDrm2FullPath = NULL;
1227         pMsgDrmInfo->roWaitingTimerMax = 0;
1228         pMsgDrmInfo->bFwdLock = false;
1229         pMsgDrmInfo->bNoScreen = false;
1230         pMsgDrmInfo->bNoRingTone = false;
1231         pMsgDrmInfo->pszContentType = NULL;
1232
1233         return true;
1234 }
1235 #endif
1236
1237 msg_error_t MmsMakePreviewInfo(int msgId, MMS_MESSAGE_DATA_S *pMmsMsg)
1238 {
1239         MMS_PAGE_S *pPage = NULL;
1240         MMS_MEDIA_S *pMedia = NULL;
1241
1242         if (pMmsMsg == NULL)
1243                 return MSG_ERR_NULL_POINTER;
1244
1245         if (pMmsMsg->pageCnt > 0) {
1246
1247                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_PAGE, (char *)"pagecount", pMmsMsg->pageCnt);
1248
1249                 pPage = _MsgMmsGetPage(pMmsMsg, 0);
1250                 for (int j = 0; j < pPage->mediaCnt; j++) {
1251
1252                         pMedia = _MsgMmsGetMedia(pPage, j);
1253                         MSG_DEBUG("pMedia's Name: %s", pMedia->szFilePath);
1254
1255                         if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG || pMedia->mediatype == MMS_SMIL_MEDIA_VIDEO) {
1256                                 char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };
1257                                 char thumbPath[MSG_FILEPATH_LEN_MAX+1] = {0, };
1258
1259                                 memset(szFileName, 0x00, MSG_FILENAME_LEN_MAX+1);
1260                                 memset(thumbPath, 0x00, MSG_FILEPATH_LEN_MAX);
1261
1262                                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%d.mms",msgId);
1263                                 snprintf(thumbPath, MSG_FILEPATH_LEN_MAX, MSG_THUMBNAIL_PATH"/%s.jpg", szFileName);
1264
1265                                 if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG) {
1266                                         if (makeImageThumbnail(pMedia->szFilePath, thumbPath) == true) {
1267                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_IMG, thumbPath);
1268                                         } else {
1269                                                 MSG_DEBUG("Fail of generating thumbnail: %s to %s", pMedia->szFilePath, thumbPath);
1270                                         }
1271                                 } else {
1272                                         if (makeVideoThumbnail(pMedia->szFilePath, thumbPath) == true) {;
1273                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_VIDEO, thumbPath);
1274                                         } else {
1275                                                 MSG_DEBUG("Fail of generating thumbnail: %s to %s", pMedia->szFilePath, thumbPath);
1276                                         }
1277                                 }
1278
1279                         } else if (pMedia->mediatype == MMS_SMIL_MEDIA_AUDIO) {
1280                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_AUDIO, pMedia->szFileName);
1281                         }
1282                 }
1283         } else {
1284                 MSG_DEBUG("There is no page");
1285         }
1286
1287         int attachCnt = _MsgMmsGetAttachCount(pMmsMsg);
1288         if (attachCnt > 0) {
1289                 MMS_ATTACH_S *pAttach = _MsgMmsGetAttachment(pMmsMsg, 0);
1290                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_ATTACH, pAttach->szFileName, attachCnt);
1291         } else {
1292                 MSG_DEBUG("There is no attachment");
1293         }
1294
1295         return MSG_SUCCESS;
1296 }