Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginMessage.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21 #include <errno.h>
22 #include <sys/stat.h>
23
24 #include "MsgCppTypes.h"
25 #include "MsgStorageTypes.h"
26 #include "MsgSettingTypes.h"
27 #include "MsgUtilFile.h"
28 #include "MsgGconfWrapper.h"
29 #include "MsgMmsMessage.h"
30 #include "MmsPluginTypes.h"
31 #include "MmsPluginDebug.h"
32 #include "MmsPluginMessage.h"
33 #include "MmsPluginMIME.h"
34 #include "MmsPluginAvCodec.h"
35 #include "MmsPluginStorage.h"
36 #include "MmsPluginSMILValidate.h"
37 #include "MmsPluginSmil.h"
38 #include "MmsPluginUtil.h"
39
40 #define PRINT_KEY_VAL_STR(key, val)\
41 if (val) {\
42 MSG_DEBUG("%-20s: %s", key, val);\
43 }\
44
45 #define PRINT_KEY_VAL_INT(key, val)\
46 if (val) {\
47 MSG_DEBUG("%-20s: %d", key, val);\
48 }\
49
50 static MsgMultipart *MmsGetNthMultipart(MmsMsg *pMsg, int index);
51 static bool MmsSetMsgAddressList(MmsAttrib *pAttrib, const MSG_MESSAGE_INFO_S *pMsgInfo);
52 static char *MmsComposeAddress(const MSG_MESSAGE_INFO_S *pMsgInfo, int recipientType);
53 static bool MmsGetSmilRawData(MMS_MESSAGE_DATA_S *pMsgBody, char *pRawData, int *nSize);
54 static bool MmsInsertPresentation(MmsMsg *pMsg, MimeType mimeType, const char *content_id, char *pData, int size);
55 static bool MmsInsertPartFromFile(MmsMsg *pMsg, char *szTitleName, char *szOrgFilePath, char *szContentID);
56 static bool MmsInsertPartFromMultipart(MmsMsg *pMsg, MMS_MULTIPART_DATA_S *pNewMultipart);
57 static bool MmsGetTypeByFileName(int *type, char *szFileName);
58 static bool MmsInsertPartToMmsData(MMS_MESSAGE_DATA_S *pMsgData, MMS_MULTIPART_DATA_S *pMultipart);
59
60 bool MmsSetMsgAddressList(MmsAttrib *pAttrib, const MSG_MESSAGE_INFO_S * pMsgInfo)
61 {
62         MSG_DEBUG("MmsSetMsgAddressList");
63         pAttrib->szTo = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_TO);
64         MSG_DEBUG("To address: %s", pAttrib->szTo);
65         pAttrib->szCc = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_CC);
66         MSG_DEBUG("Cc address: %s", pAttrib->szCc);
67         pAttrib->szBcc = MmsComposeAddress(pMsgInfo, MSG_RECIPIENTS_TYPE_BCC);
68         MSG_DEBUG("Bcc address: %s", pAttrib->szBcc);
69
70         return true;
71 }
72
73 void MmsSetMsgMultiStatus(MmsAttrib *pAttrib, const MSG_MESSAGE_INFO_S *pMsgInfo)
74 {
75         int     nAddressCnt = 0;
76
77         nAddressCnt = pMsgInfo->nAddressCnt;
78
79         for (int i = 0; i < nAddressCnt; ++i) {
80                 pAttrib->pMultiStatus = (MmsMsgMultiStatus *)malloc(sizeof(MmsMsgMultiStatus));
81
82                 memset(pAttrib->pMultiStatus->szTo, 0, MAX_ADDRESS_VAL_LEN + 1);
83                 strncpy(pAttrib->pMultiStatus->szTo, pMsgInfo->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
84
85                 MSG_DEBUG("### pMultistatus->szTo = %s ####", pAttrib->pMultiStatus->szTo);
86                 pAttrib->pMultiStatus->bDeliveryReportIsRead = false;
87                 pAttrib->pMultiStatus->bDeliveyrReportIsLast = false;
88                 pAttrib->pMultiStatus->msgStatus = MMS_MSGSTATUS_NONE;
89                 pAttrib->pMultiStatus->handledTime = 0;
90                 pAttrib->pMultiStatus->bReadReplyIsRead = false;
91                 pAttrib->pMultiStatus->bReadReplyIsLast = false;
92                 pAttrib->pMultiStatus->readStatus = MMS_READSTATUS_NONE;
93                 pAttrib->pMultiStatus->readTime = 0;
94
95                 pAttrib->pMultiStatus = pAttrib->pMultiStatus->pNext;
96         }
97 }
98
99 char *MmsComposeAddress(const MSG_MESSAGE_INFO_S *pMsgInfo, int recipientType)
100 {
101         MSG_DEBUG("MmsComposeAddress");
102         int     addrLen = 0;
103         int     nAddressCnt = 0;
104         int nRecpCnt = 0;
105         char pString[MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3] = {0, };
106         char *szCompose;
107
108         nAddressCnt = pMsgInfo->nAddressCnt;
109
110         // Calculate allocated buffer size
111         for (int i = 0; i < nAddressCnt; ++i) {
112                 MSG_DEBUG("recipientType: %d, address value: %s", pMsgInfo->addressList[i].recipientType, pMsgInfo->addressList[i].addressVal);
113                 if (pMsgInfo->addressList[i].recipientType == recipientType) {
114                         if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_PLMN) {
115                                 addrLen += strlen(MsgGetString(MSG_ADDR_TYPE, MSG_ADDR_TYPE_PHONE));
116                                 addrLen += strlen(pMsgInfo->addressList[i].addressVal);
117                         } else if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_EMAIL) {
118                                 addrLen += strlen(pMsgInfo->addressList[i].addressVal);
119                         } else
120                                 ; // Need to consider IPV4, IPV6, and Alias formatted address
121
122                         nRecpCnt++;
123                 }
124         }
125
126         if (nRecpCnt > 1)
127                 addrLen = addrLen + nRecpCnt - 1;
128         szCompose = (char *)calloc(addrLen + 1, 1);
129
130         // Address String copy
131         for (int i = 0; i < nAddressCnt; ++i) {
132                 if (pMsgInfo->addressList[i].recipientType == recipientType) {
133                         if (strlen(szCompose) > 0)
134                                 strcat(szCompose, MSG_STR_ADDR_DELIMETER);
135
136                         memset(pString, 0x00, (MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3) * sizeof(char));
137                         if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_PLMN) {
138                                 snprintf(pString, MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3, "%s%s", pMsgInfo->addressList[i].addressVal, MsgGetString(MSG_ADDR_TYPE, MSG_ADDR_TYPE_PHONE));
139                                 MSG_DEBUG("%s", pString);
140                         } else if (pMsgInfo->addressList[i].addressType == MSG_ADDRESS_TYPE_EMAIL) {
141                                 snprintf(pString, MSG_LOCALE_NAME_LEN + MSG_ADDR_LEN + 3, "%s", pMsgInfo->addressList[i].addressVal);
142                         } else
143                                 ; // Need to consider IPV4, IPV6, and Alias formatted address
144
145                         strcat(szCompose, pString);
146                 }
147         }
148
149         return szCompose;
150 }
151
152
153 bool MmsGetMsgBodyfromMsgInfo(const MSG_MESSAGE_INFO_S *pMsgInfo, MMS_MESSAGE_DATA_S *pMsgBody, char *pFileData)
154 {
155         MSG_DEBUG("MmsGetMsgBodyfromMsgInfo");
156         memset(pMsgBody, 0, sizeof(MMS_MESSAGE_DATA_S));
157
158         if (pMsgInfo->bTextSms == false) {       //if  the message body was stored in file.
159                 _MsgMmsDeserializeMessageData(pMsgBody, pFileData);
160         }
161
162         return true;
163 }
164
165 bool MmsGetSmilRawData(MMS_MESSAGE_DATA_S *pMsgBody, char **pRawdata)
166 {
167         MSG_BEGIN();
168
169         if (MsgReadSmilFile(pMsgBody->szSmilFilePath, pRawdata) < 0)
170                 return false;
171
172         MsgDeleteSmilFile(pMsgBody->szSmilFilePath);
173
174         MSG_END();
175
176         return true;
177 }
178
179 bool MmsInsertPresentation(MmsMsg *pMsg, MimeType mimeType, const char *content_id, char *pData, int size)
180 {
181         MSG_BEGIN();
182
183         if (pMsg == NULL) {
184                 MSG_DEBUG("pMsg is NULL");
185                 return false;
186         }
187
188         if (pMsg->msgBody.pPresentationBody != NULL)
189                 goto __CATCH;
190
191         memset(&pMsg->msgBody.presentationType, 0, sizeof(MsgType));
192         pMsg->msgBody.pPresentationBody = (MsgBody *)malloc(sizeof(MsgBody));
193         if (pMsg->msgBody.pPresentationBody == NULL)
194                 goto __CATCH;
195
196         MmsInitMsgBody(pMsg->msgBody.pPresentationBody);
197
198         pMsg->msgBody.pPresentationBody->body.pText = (char *)malloc(size + 1);
199         if (pMsg->msgBody.pPresentationBody->body.pText == NULL)
200                 goto __CATCH;
201
202         pMsg->msgBody.pPresentationBody->size = size;
203         pMsg->msgBody.presentationType.type = mimeType;
204         pMsg->msgBody.presentationType.param.charset = MSG_CHARSET_UTF8;
205
206         if (content_id && strlen(content_id) > 0) {
207                 snprintf(pMsg->msgBody.presentationType.szContentID, MSG_MSG_ID_LEN + 1, "%s", content_id);
208         } else {
209                 snprintf(pMsg->msgBody.presentationType.szContentID, MSG_MSG_ID_LEN + 1, "<_S_>");//default
210         }
211
212         snprintf(pMsg->msgType.param.szStart, MSG_MSG_ID_LEN + 1, "%s", pMsg->msgBody.presentationType.szContentID);
213
214         pMsg->msgType.param.type = mimeType;
215
216         memset(pMsg->msgBody.pPresentationBody->body.pText, 0, size + 1);
217         strncpy(pMsg->msgBody.pPresentationBody->body.pText, pData, size);
218
219         MSG_END();
220         return true;
221
222 __CATCH:
223
224         if (pMsg->msgBody.pPresentationBody != NULL) {
225                 if (pMsg->msgBody.pPresentationBody->body.pText != NULL) {
226                         free(pMsg->msgBody.pPresentationBody->body.pText);
227                         pMsg->msgBody.pPresentationBody->body.pText = NULL;
228                 }
229
230                 free(pMsg->msgBody.pPresentationBody);
231                 pMsg->msgBody.pPresentationBody = NULL;
232         }
233
234         return false;
235 }
236
237 bool MmsInsertPartFromFile(MmsMsg *pMsg, char *szTitleName, char *szOrgFilePath, char *szContentID)
238 {
239         MSG_DEBUG("MmsInsertPartFromFile");
240
241         MsgMultipart *pMultipart = NULL;
242         MsgMultipart *pLastPart = NULL;
243         int nFileSize;
244         MimeType mimeType = MIME_UNKNOWN;
245         char *pExt = NULL;
246
247         pExt = strrchr(szOrgFilePath, '.');
248
249         if (pExt == NULL || pExt[0] == '\0' || strrchr(pExt, '/'))
250                 mimeType = MIME_UNKNOWN;
251         else {
252                 if (strcasecmp(pExt, ".dcf") == 0)
253                         mimeType = MIME_APPLICATION_VND_OMA_DRM_CONTENT;
254                 else {
255                         if (MmsGetTypeByFileName((int *)&mimeType, szOrgFilePath) == false)
256                                 goto __CATCH;
257                 }
258         }
259
260         MSG_DEBUG("MmsInsertPartFromFile: type = %d, name = %s, filepath = %s, cid = %s", mimeType, szTitleName, szOrgFilePath, szContentID);
261
262         if (mimeType == MIME_UNKNOWN)
263                 mimeType = MIME_APPLICATION_OCTET_STREAM;
264
265         if (MmsIsMultipart(pMsg->msgType.type) == true) {
266                 /* Insert as a multipart */
267                 if (MsgGetFileSize(szOrgFilePath, &nFileSize) == false) {
268                         MSG_DEBUG("MsgGetFileSize: failed");
269                         goto __CATCH;
270                 }
271
272                 pMultipart = MmsMakeMultipart(mimeType, szTitleName, szOrgFilePath, szContentID, NULL);
273                 if (pMultipart == NULL)
274                         goto __CATCH;
275
276                 if (pMsg->mmsAttrib.contentType == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
277                         pMsg->mmsAttrib.contentType == MIME_MULTIPART_MIXED)
278                         pMultipart->type.disposition = MSG_DISPOSITION_ATTACHMENT;
279
280                 if (pMsg->msgBody.body.pMultipart == NULL) {
281                         pMsg->msgBody.body.pMultipart = pMultipart;
282                 } else {
283                         pLastPart = pMsg->msgBody.body.pMultipart;
284                         while (pLastPart->pNext) {
285                                 pLastPart = pLastPart->pNext;
286                         }
287
288                         pLastPart->pNext = pMultipart;
289                 }
290
291                 pMsg->msgBody.size += pMultipart->pBody->size;
292                 pMsg->msgType.contentSize += pMultipart->pBody->size;
293         } else {
294                 /* Single part - Insert as a message body */
295                 if (pMsg->mmsAttrib.contentType != mimeType || pMsg->msgType.type != mimeType)
296                         goto __CATCH;
297
298                 strncpy(pMsg->msgType.param.szName, szTitleName, MSG_LOCALE_FILENAME_LEN_MAX);
299
300                 if (MmsIsText(pMsg->msgType.type) == true) {
301                         pMsg->msgType.param.charset = MSG_CHARSET_UTF8;
302                 }
303
304                 strncpy(pMsg->msgBody.szOrgFilePath, szOrgFilePath, MSG_FILEPATH_LEN_MAX - 1);
305                 if (MsgGetFileSize(szOrgFilePath, &nFileSize) == false) {
306                         MSG_DEBUG("MsgGetFileSize: failed");
307                         goto __CATCH;
308                 }
309
310                 pMsg->msgBody.offset = 0;
311                 pMsg->msgBody.size = nFileSize;
312                 pMsg->msgType.contentSize = nFileSize;
313         }
314
315         pMsg->nPartCount++;
316
317         return true;
318
319 __CATCH:
320         return false;
321
322 }
323
324 bool MmsIsMultipart(int type)
325 {
326         MSG_DEBUG("MmsIsMultipart");
327         if (type == MIME_MULTIPART_RELATED ||
328                 type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
329                 type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED ||
330                 type == MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC ||
331                 type == MIME_MULTIPART_MIXED ||
332                 type == MIME_MULTIPART_REPORT) {
333                 return true;
334         } else {
335                 return false;
336         }
337 }
338
339 bool MmsIsText(int type)
340 {
341         if (type == MIME_TEXT_PLAIN ||
342                 type == MIME_TEXT_HTML ||
343                 type == MIME_TEXT_VND_WAP_WML ||
344                 type == MIME_TEXT_X_VCARD ||
345                 type == MIME_TEXT_X_VCALENDAR ||
346                 type == MIME_TEXT_X_VNOTE ||
347                 type == MIME_APPLICATION_SMIL ||
348                 type == MIME_TEXT_X_IMELODY) {
349                 MSG_DEBUG("MmsIsText true.");
350                 return true;
351         } else {
352                 MSG_DEBUG("MmsIsText false.");
353                 return false;
354         }
355 }
356
357 bool MmsIsVitemContent (int type, char *pszName)
358 {
359         switch (type) {
360
361 /*
362 *       To make Encoding information right.
363 *               case MIME_TEXT_X_VCARD :
364 *               case MIME_TEXT_X_VCALENDAR :
365 *               case MIME_TEXT_X_VNOTE :        // vnt
366 *               {
367 *                       MSG_DEBUG("MmsIsVitemContent true.");
368 *                       return true;
369 *               }
370 *
371 */
372         case MIME_TEXT_X_VCARD:
373         case MIME_TEXT_X_VCALENDAR:
374         case MIME_TEXT_X_VNOTE: // vnt
375         case MIME_TEXT_PLAIN:           // vbm - It SHOULD be distinguished from a normal text file.
376                 {
377                         char *pszExt = NULL;
378
379                         if (!pszName)
380                                 break;
381
382                         // search file extension.
383                         if ((pszExt = strrchr(pszName, '.')) == NULL)
384                                 break;
385
386                         if (!strcasecmp(pszExt, ".vbm")) {
387                                 MSG_DEBUG("MmsIsVitemContent true.");
388                                 return true;
389                         }
390                 }
391                 break;
392
393         default:
394                 break;
395         }
396
397         MSG_DEBUG("MmsIsVitemContent false.");
398         return false;
399 }
400
401
402
403 MsgMultipart *MmsAllocMultipart(void)
404 {
405         MsgMultipart *pMultipart = NULL;
406
407         pMultipart = (MsgMultipart *)malloc(sizeof(MsgMultipart));
408
409         if (pMultipart == NULL)
410                 goto __CATCH;
411
412         pMultipart->pBody = (MsgBody *)malloc(sizeof(MsgBody));
413
414         if (pMultipart->pBody == NULL)
415                 goto __CATCH;
416
417         MmsInitMsgType(&pMultipart->type);
418         MmsInitMsgBody(pMultipart->pBody);
419
420         pMultipart->pNext = NULL;
421
422         return pMultipart;
423
424 __CATCH:
425
426         if (pMultipart) {
427                 if (pMultipart->pBody) {
428                         free(pMultipart->pBody);
429                         pMultipart->pBody = NULL;
430                 }
431
432                 free(pMultipart);
433                 pMultipart = NULL;
434         }
435
436         return NULL;
437 }
438
439 MsgMultipart *MmsMakeMultipart(MimeType mimeType, char *szTitleName, char *szOrgFilePath, char *szContentID, char *szContentLocation)
440 {
441         MsgMultipart *pMultipart = NULL;
442
443         if ((pMultipart = MmsAllocMultipart()) == NULL)
444                 return NULL;
445
446         pMultipart->type.type = mimeType;
447
448         if (szTitleName && szTitleName[0]) {
449                 memset(pMultipart->type.param.szName, 0, MSG_LOCALE_FILENAME_LEN_MAX + 1);
450                 strncpy(pMultipart->type.param.szName, szTitleName, MSG_LOCALE_FILENAME_LEN_MAX);
451         }
452
453         if (szContentID && szContentID[0]) {
454                 memset(pMultipart->type.szContentID, 0, MSG_MSG_ID_LEN + 1);
455                 snprintf(pMultipart->type.szContentID, MSG_MSG_ID_LEN + 1, "<%s>", szContentID);
456         }
457
458         if (szContentLocation && szContentLocation[0]) {
459                 memset(pMultipart->type.szContentLocation, 0, MSG_MSG_ID_LEN + 1);
460                 snprintf(pMultipart->type.szContentLocation, MSG_MSG_ID_LEN + 1, "%s", szContentLocation);
461         }
462
463         if (MmsIsText(mimeType) == true) {
464                 if (!MmsIsVitemContent (mimeType, pMultipart->type.param.szName)) {
465                         pMultipart->type.param.charset = MSG_CHARSET_UTF8;
466                 }
467                 pMultipart->type.encoding = MSG_ENCODING_8BIT;
468         } else {
469                 pMultipart->type.encoding = MSG_ENCODING_BINARY;
470         }
471
472         if (szOrgFilePath) {
473                 strncpy(pMultipart->pBody->szOrgFilePath, szOrgFilePath, MSG_FILEPATH_LEN_MAX - 1);
474                 pMultipart->pBody->offset = 0;
475                 pMultipart->pBody->size = MsgGetFileSize(szOrgFilePath);
476         }
477         return pMultipart;
478 }
479
480 bool MmsGetTypeByFileName(int *type, char *szFileName)
481 {
482         char *pExt   = NULL;
483         AvCodecType AvType = AV_CODEC_NONE;
484
485         /* AVMS unknown or text/image file format identify type from file extention  */
486
487         pExt = strrchr(szFileName, '.');
488         if (pExt == NULL || pExt[0] == '\0')
489                 goto __CATCH;
490
491         pExt++;
492
493         if (strcasecmp(pExt, "mp4") == 0 || strcasecmp(pExt, "mpeg4") == 0 ||
494                 strcasecmp(pExt, "3gp") == 0 || strcasecmp(pExt, "3gpp") == 0) {
495                 /* Audio / Video format. If file exists already, AvGetFileCodecType() can identify the format  */
496                 if (szFileName[0] != '/')
497                         goto __CATCH;
498
499                 AvType = AvGetFileCodecType(szFileName);
500
501                 switch (AvType) {
502                 case AV_DEC_AUDIO_MPEG4:
503                         *type = MIME_AUDIO_MP4;//*type = MIME_AUDIO_3GPP;
504                         break;
505
506                 case AV_DEC_VIDEO_MPEG4:
507                         *type = MIME_VIDEO_MP4;
508                         break;
509
510                 default:
511                         *type = MIME_VIDEO_3GPP;
512                         break;
513                 }
514                 return true;
515         }
516
517         if (strcasecmp(pExt, "amr") == 0) {
518                 *type = MIME_AUDIO_AMR;
519                 return true;
520         } else if ((strcasecmp(pExt, "mid") == 0) || (strcasecmp(pExt, "midi") == 0)) {
521                 *type = MIME_AUDIO_MIDI;
522                 return true;
523         } else if (strcasecmp(pExt, "imy") == 0) {
524                 *type = MIME_TEXT_X_IMELODY;
525                 return true;
526         }
527
528         *type = MimeGetMimeFromExtInt((const char *)pExt);
529
530         return true;
531
532 __CATCH:
533
534         *type = MIME_UNKNOWN;
535         return false;
536
537 }
538
539 bool MmsComposeMessage(MmsMsg *pMmsMsg, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, MMS_MESSAGE_DATA_S *pMsgData, char *pFileData)
540 {
541         MSG_BEGIN();
542
543         char *pRawData = NULL;
544         AutoPtr<char> buf(&pRawData);
545
546         struct tm *timeInfo = NULL;
547         time_t RawTime = 0;
548         time_t nTimeInSecs = 0;
549
550         msg_error_t     err = MSG_SUCCESS;
551
552         // Initialize mmsMsg structure
553         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
554         MmsInitMsgType(&pMmsMsg->msgType);
555         MmsInitMsgBody(&pMmsMsg->msgBody);
556
557         // setting mmsMsg structure
558         pMmsMsg->mailbox = pMsgInfo->folderId;
559         pMmsMsg->msgID = pMsgInfo->msgId;
560
561         memset(pMmsMsg->szTrID, 0, MMS_TR_ID_LEN + 1);
562         memset(pMmsMsg->szContentLocation, 0, MMS_LOCATION_LEN + 1);
563         memset(pMmsMsg->szMsgID, 0, MMS_MSG_ID_LEN + 1);
564         memset(pMmsMsg->szForwardMsgID, 0, MMS_MSG_ID_LEN + 1);
565
566         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_DRAFT;
567
568         MSG_DEBUG("## delivery = %d ##", pSendOptInfo->bDeliverReq);
569         MSG_DEBUG("## read = %d ##", pSendOptInfo->option.mmsSendOptInfo.bReadReq);
570         MSG_DEBUG("## priority = %d ##", pSendOptInfo->option.mmsSendOptInfo.priority);
571         MSG_DEBUG("## expiryTime = %d ##", pSendOptInfo->option.mmsSendOptInfo.expiryTime.time);
572
573         if (pSendOptInfo->bSetting == false) {
574                 unsigned int expiryTime;
575                 MSG_MMS_DELIVERY_TIME_T deliveryTime;
576
577                 pMmsMsg->mmsAttrib.priority = (MmsPriority)MsgSettingGetInt(MMS_SEND_PRIORITY);
578
579                 MsgSettingGetBool(MMS_SEND_DELIVERY_REPORT, &pMmsMsg->mmsAttrib.bAskDeliveryReport);
580                 MsgSettingGetBool(MMS_SEND_READ_REPLY, &pMmsMsg->mmsAttrib.bAskReadReply);
581                 MsgSettingGetBool(MSG_KEEP_COPY, &pMmsMsg->mmsAttrib.bLeaveCopy);
582
583                 expiryTime = (unsigned int)MsgSettingGetInt(MMS_SEND_EXPIRY_TIME);
584
585                 if (expiryTime == 0)
586                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_NONE;
587                 else {
588                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_RELATIVE;
589                         pMmsMsg->mmsAttrib.expiryTime.time = expiryTime;
590                 }
591
592                 deliveryTime = (MSG_MMS_DELIVERY_TIME_T)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
593
594                 if (deliveryTime == MSG_DELIVERY_TIME_CUSTOM) {
595                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = true;
596
597                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
598                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_CUSTOM_DELIVERY);
599                 } else {
600                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = false;
601
602                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
603                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)deliveryTime;
604                 }
605         } else {
606                 pMmsMsg->mmsAttrib.priority = (MmsPriority)pSendOptInfo->option.mmsSendOptInfo.priority;
607                 pMmsMsg->mmsAttrib.bAskDeliveryReport = pSendOptInfo->bDeliverReq;
608                 pMmsMsg->mmsAttrib.bAskReadReply = pSendOptInfo->option.mmsSendOptInfo.bReadReq;
609                 pMmsMsg->mmsAttrib.expiryTime.type = pSendOptInfo->option.mmsSendOptInfo.expiryTime.type;
610                 pMmsMsg->mmsAttrib.bLeaveCopy = pSendOptInfo->bKeepCopy;
611
612                 if (pMmsMsg->mmsAttrib.expiryTime.type != MMS_TIMETYPE_NONE)
613                         pMmsMsg->mmsAttrib.expiryTime.time = pSendOptInfo->option.mmsSendOptInfo.expiryTime.time;
614
615                 pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime;
616                 pMmsMsg->mmsAttrib.deliveryTime.type = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.type;
617                 pMmsMsg->mmsAttrib.deliveryTime.time = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time;
618         }
619
620         MSG_DEBUG("pSendOptInfo->bSetting = %d", pSendOptInfo->bSetting);
621         MSG_DEBUG("pMmsMsg->mmsAttrib.bLeaveCopy = %d", pMmsMsg->mmsAttrib.bLeaveCopy);
622         MSG_DEBUG("pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = %d", pMmsMsg->mmsAttrib.bUseDeliveryCustomTime);
623         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.type = %d", pMmsMsg->mmsAttrib.deliveryTime.type);
624         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.time = %d", pMmsMsg->mmsAttrib.deliveryTime.time);
625
626         /* MMS-1.3-con-739 */
627         pMmsMsg->mmsAttrib.msgClass = (MmsMsgClass)MsgSettingGetInt(MMS_SEND_MSG_CLASS);
628         /* MMS-1.3-con-739 */
629 #ifdef MMS_13_CON_742_ENABLED
630         /* MMS-1.3-con-742 */
631         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
632         /* MMS-1.3-con-742 */
633 #endif
634
635         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.bAskDeliveryReport = %d @@@", pMmsMsg->mmsAttrib.bAskDeliveryReport);
636         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.bAskReadReply = %d @@@", pMmsMsg->mmsAttrib.bAskReadReply);
637         MSG_DEBUG("@@@ pMmsMsg->mmsAttrib.priority = %d @@@", pMmsMsg->mmsAttrib.priority);
638
639         // setting date
640         time(&RawTime);
641         timeInfo = localtime(&RawTime);
642         nTimeInSecs = mktime(timeInfo);
643         pMmsMsg->mmsAttrib.date = nTimeInSecs;  // todo: need to subtract timeline value to make GMT+0 time
644
645         //setting subject
646         strcpy(pMmsMsg->mmsAttrib.szSubject, pMsgInfo->subject);
647
648         //setting adddress
649         MmsSetMsgAddressList(&pMmsMsg->mmsAttrib, pMsgInfo);
650         MmsGetMsgBodyfromMsgInfo(pMsgInfo, pMsgData, pFileData);
651
652         int pageCnt = _MsgMmsGetPageCount(pMsgData);
653
654         if (pageCnt == 0) {     // Multipart mixed
655                 pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
656                 pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
657                 MmsMakePreviewInfo(pMsgInfo->msgId, pMsgData);
658         } else {        // Multipart related
659
660                 int RawDataSize = 0;
661
662                 time_t RawTime = 0;
663                 time(&RawTime);
664                 snprintf(pMsgData->szSmilFilePath, MSG_FILEPATH_LEN_MAX, "%lu", RawTime);
665
666                 MsgMMSCreateSMIL(pMsgData);
667
668                 RawDataSize = MmsGetSmilRawData(pMsgData, &pRawData);
669                 if (RawDataSize < 0) {
670                         MSG_DEBUG("Smil file size is less than 0");
671                         return false;
672                 }
673                 MSG_DEBUG("%s", pRawData);
674                 if (pRawData)
675                         MmsInsertPresentation(pMmsMsg, MIME_APPLICATION_SMIL, NULL, pRawData, strlen(pRawData));
676
677                 pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
678                 pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
679
680                 for (int i = 0; i < pageCnt; ++i) {
681                         MMS_PAGE_S *pPage = _MsgMmsGetPage(pMsgData, i);
682                         int mediaCnt = pPage->mediaCnt;
683                         MSG_DEBUG("PAGE %d's media Cnt: %d", i+1, mediaCnt);
684
685                         for (int j = 0; j < mediaCnt; ++j) {
686                                 MMS_MEDIA_S *pMedia = _MsgMmsGetMedia(pPage, j);
687
688                                 switch (pMedia->mediatype) {
689                                 case MMS_SMIL_MEDIA_IMG:
690                                 case MMS_SMIL_MEDIA_VIDEO:
691                                 case MMS_SMIL_MEDIA_AUDIO:
692                                 case MMS_SMIL_MEDIA_TEXT:
693                                         if (pMedia->szFilePath[0] != 0) {
694                                                 if (!MmsInsertPartFromFile(pMmsMsg, pMedia->szFileName, pMedia->szFilePath, pMedia->szContentID))
695                                                         return false;
696                                         }
697                                         break;
698
699                                 default:
700                                         break;
701                                 }
702                         }
703                 }
704
705                 char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };;
706                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%d.mms", pMsgInfo->msgId);
707
708                 MmsPluginStorage *pStorage = MmsPluginStorage::instance();
709                 err = pStorage->getMsgText(pMsgData, pMsgInfo->msgText);
710                 MmsMakePreviewInfo(pMsgInfo->msgId, pMsgData);
711         }
712
713 #ifdef FEATURE_JAVA_MMS
714         MSG_DEBUG("msgAppId: valid:%d appId:%s replyToAppId:%s", pMsgData->msgAppId.valid, pMsgData->msgAppId.appId, pMsgData->msgAppId.replyToAppId);
715         if (pMsgData->msgAppId.valid) { // check if msgAppId.valid is true, both appId and replytoappId must have a meaning data
716                 if (pMsgData->msgAppId.appId[0] != 0) {
717                         pMmsMsg->msgType.param.szApplicationID = (char *)malloc(strlen(pMsgData->msgAppId.appId) + 1);
718                         if (pMmsMsg->msgType.param.szApplicationID == NULL) {
719                                 MSG_DEBUG("Error: out of Memory");
720                                 return false;
721                         }
722                         memset(pMmsMsg->msgType.param.szApplicationID, 0, strlen(pMsgData->msgAppId.appId) + 1);
723
724                         strcpy(pMmsMsg->msgType.param.szApplicationID, pMsgData->msgAppId.appId);
725                 }
726
727                 if (pMsgData->msgAppId.replyToAppId[0] != 0) {
728                         pMmsMsg->msgType.param.szReplyToApplicationID = (char *)malloc(strlen(pMsgData->msgAppId.replyToAppId) + 1);
729                         if (pMmsMsg->msgType.param.szReplyToApplicationID == NULL) {
730                                 MSG_DEBUG("Error: out of Memory");
731                                 return false;
732                         }
733                         memset(pMmsMsg->msgType.param.szReplyToApplicationID, 0, strlen(pMsgData->msgAppId.replyToAppId) + 1);
734
735                         strcpy(pMmsMsg->msgType.param.szReplyToApplicationID, pMsgData->msgAppId.replyToAppId);
736                 }
737         }
738 #endif
739
740         //Processing Attachment List
741         for (int i = 0; i < _MsgMmsGetAttachCount(pMsgData); ++i) {
742                 MMS_ATTACH_S *pMedia = _MsgMmsGetAttachment(pMsgData, i);
743                 if (pMedia->szFilePath[0] != 0) {
744                         if (!MmsInsertPartFromFile(pMmsMsg, pMedia->szFileName, pMedia->szFilePath, NULL)) {
745                                 free(pMedia);
746                                 return false;
747                         }
748                 }
749         }
750
751         return true;
752 }
753
754 void MmsComposeNotiMessage(MmsMsg *pMmsMsg, msg_message_id_t msgID)
755 {
756         MSG_BEGIN();
757
758         struct tm *timeInfo = NULL;
759         time_t RawTime = 0;
760         time_t nTimeInSecs = 0;
761
762         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
763         MmsInitMsgType(&pMmsMsg->msgType);
764         MmsInitMsgBody(&pMmsMsg->msgBody);
765
766         pMmsMsg->msgID = msgID;
767
768         pMmsMsg->mmsAttrib.version = mmsHeader.version;
769
770         // setting date
771         time(&RawTime);
772         timeInfo = localtime(&RawTime);
773         nTimeInSecs = mktime(timeInfo);
774         pMmsMsg->mmsAttrib.date = nTimeInSecs;
775
776         pMmsMsg->mmsAttrib.bReportAllowed = mmsHeader.reportAllowed;
777         pMmsMsg->mmsAttrib.bAskDeliveryReport = mmsHeader.deliveryReport;
778
779         MSG_DEBUG("######## Version = %d ########", pMmsMsg->mmsAttrib.version);
780
781         strncpy(pMmsMsg->szTrID, mmsHeader.szTrID, MMS_TR_ID_LEN);
782         strncpy(pMmsMsg->szMsgID, mmsHeader.szMsgID, MMS_MSG_ID_LEN);
783         pMmsMsg->szForwardMsgID[0] = '\0';
784
785         if (mmsHeader.pFrom) {
786                 MmsAddrUtilRemovePlmnString(mmsHeader.pFrom->szAddr );
787                 strncpy(pMmsMsg->mmsAttrib.szFrom, mmsHeader.pFrom->szAddr, MSG_LOCALE_ADDR_LEN + 9);
788         }
789
790         strncpy(pMmsMsg->mmsAttrib.szSubject, mmsHeader.szSubject, MSG_LOCALE_SUBJ_LEN);
791         strncpy(pMmsMsg->szContentLocation, mmsHeader.szContentLocation, MMS_LOCATION_LEN);
792
793         pMmsMsg->mmsAttrib.msgClass = mmsHeader.msgClass;
794         pMmsMsg->mmsAttrib.msgSize = mmsHeader.msgSize;
795         pMmsMsg->mmsAttrib.expiryTime.type = mmsHeader.expiryTime.type;
796         pMmsMsg->mmsAttrib.expiryTime.time = mmsHeader.expiryTime.time;
797         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_NOTIFY;
798         pMmsMsg->mmsAttrib.bRead = false;
799         pMmsMsg->mailbox = MSG_INBOX_ID;
800
801         pMmsMsg->mmsAttrib.replyCharge.chargeType = mmsHeader.replyCharge.chargeType;
802         pMmsMsg->mmsAttrib.replyCharge.deadLine.type = mmsHeader.replyCharge.deadLine.type;
803         pMmsMsg->mmsAttrib.replyCharge.deadLine.time = mmsHeader.replyCharge.deadLine.time;
804         pMmsMsg->mmsAttrib.replyCharge.chargeSize = mmsHeader.replyCharge.chargeSize;
805
806         strncpy(pMmsMsg->mmsAttrib.replyCharge.szChargeID, mmsHeader.replyCharge.szChargeID, MMS_MSG_ID_LEN);
807
808         MSG_END();
809 }
810
811 void MmsComposeReadReportMessage(MmsMsg *pMmsMsg, const MSG_MESSAGE_INFO_S *pMsgInfo, msg_message_id_t selectedMsgId)
812 {
813         struct tm *timeInfo = NULL;
814         time_t RawTime = 0;
815         time_t nTimeInSecs = 0;
816
817         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
818         MmsInitMsgType(&pMmsMsg->msgType);
819         MmsInitMsgBody(&pMmsMsg->msgBody);
820
821         // setting mmsMsg structure
822         pMmsMsg->mailbox = pMsgInfo->folderId;
823         pMmsMsg->msgID = pMsgInfo->msgId;
824
825         memset(pMmsMsg->szTrID, 0, MMS_TR_ID_LEN + 1);
826         memset(pMmsMsg->szContentLocation, 0, MMS_LOCATION_LEN + 1);
827         memset(pMmsMsg->szForwardMsgID, 0, MMS_MSG_ID_LEN + 1);
828
829         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_DRAFT;
830
831         // setting date
832         time(&RawTime);
833         timeInfo = localtime(&RawTime);
834         nTimeInSecs = mktime(timeInfo);
835         pMmsMsg->mmsAttrib.date = nTimeInSecs;
836
837         // setting szMsgId
838         MmsPluginStorage::instance()->getMmsMessageId(selectedMsgId, pMmsMsg);
839
840         //setting subject
841         strcpy(pMmsMsg->mmsAttrib.szSubject, pMsgInfo->subject);
842
843         //setting adddress
844         MmsSetMsgAddressList(&pMmsMsg->mmsAttrib, pMsgInfo);
845
846         if (pMmsMsg->mmsAttrib.szTo)
847                 strncpy(pMmsMsg->mmsAttrib.szFrom, pMmsMsg->mmsAttrib.szTo, strlen(pMmsMsg->mmsAttrib.szTo));
848 }
849
850 bool MmsFindMatchedMedia(MMS_MESSAGE_DATA_S *pMsgData, char *pszFilePath)
851 {
852         if (pMsgData == NULL || pszFilePath == NULL)
853                 return false;
854
855         if (pMsgData->pagelist) {
856                 for (int pageIdx = 0; pageIdx < pMsgData->pageCnt; pageIdx++) {
857                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
858
859                         if (page && page->medialist) {
860                                 for (int mediaIdx = 0; mediaIdx < page->mediaCnt; mediaIdx++) {
861                                         MMS_MEDIA_S *media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, mediaIdx);
862                                         if (media) {
863                                                 if (strcmp(pszFilePath, media->szFilePath) == 0)
864                                                         return true;
865                                         }
866                                 }
867                         }
868                 }
869         }
870
871         return false;
872 }
873
874 bool MmsCheckAdditionalMedia(MMS_MESSAGE_DATA_S *pMsgData, MsgType *partHeader)
875 {
876         if (MmsFindMatchedMedia(pMsgData, partHeader->param.szFileName))
877                 return false;
878         else
879                 return true;
880 }
881
882 /*PIM objects SHALL be supported as attachments to an MM*/
883 bool MmsRemovePims(MMS_MESSAGE_DATA_S *pMsgData)
884 {
885         GList *cur_page = NULL;
886         GList *cur_media = NULL;
887
888         if (pMsgData == NULL)
889                 return false;
890
891         cur_page = pMsgData->pagelist;
892
893         while(cur_page) {
894                 MMS_PAGE_S *page = (MMS_PAGE_S *)cur_page->data;
895                 if (page) {
896                         cur_media = page->medialist;
897
898                         while(cur_media) {
899                                 MMS_MEDIA_S *pMedia = (MMS_MEDIA_S *)cur_media->data;
900                                 if (pMedia) {
901                                         int tempType;
902                                         MsgGetTypeByFileName(&tempType, pMedia->szFilePath);
903                                         if (tempType == MIME_TEXT_X_VCALENDAR || tempType == MIME_TEXT_X_VCARD) {
904                                                 page->medialist = g_list_remove_all(page->medialist, pMedia);
905                                                 page->mediaCnt = g_list_length(page->medialist);
906                                                 cur_media = page->medialist;
907                                                 free(pMedia);
908                                         } else {
909                                                 cur_media =  g_list_next(cur_media);
910                                         }
911                                 } else {
912                                         cur_media =  g_list_next(cur_media);
913                                 }
914                         } //cur_media while for remove pims file in list
915
916                         if (page->medialist == NULL) {//remove empty page
917                                 pMsgData->pagelist = g_list_remove_all(pMsgData->pagelist, page);
918                                 pMsgData->pageCnt = g_list_length(pMsgData->pagelist);
919                                 cur_page = pMsgData->pagelist;
920                                 free(page);
921                         } else {
922                                 cur_page =  g_list_next(cur_page);
923                         }
924                 }
925         }//cur_page while
926
927
928         return true;
929 }
930
931 msg_error_t MmsMakePreviewInfo(int msgId, MMS_MESSAGE_DATA_S *pMmsMsg)
932 {
933         MMS_PAGE_S *pPage = NULL;
934         MMS_MEDIA_S *pMedia = NULL;
935
936         if (pMmsMsg == NULL)
937                 return MSG_ERR_NULL_POINTER;
938
939         MmsPluginStorage::instance()->removePreviewInfo(msgId); //remove exist previnfo
940
941         if (pMmsMsg->pageCnt > 0) {
942
943                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_PAGE, (char *)"pagecount", pMmsMsg->pageCnt);
944
945                 pPage = _MsgMmsGetPage(pMmsMsg, 0);
946                 for (int j = 0; j < pPage->mediaCnt; j++) {
947
948                         pMedia = _MsgMmsGetMedia(pPage, j);
949                         MSG_DEBUG("pMedia's Name: %s", pMedia->szFilePath);
950
951                         if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG || pMedia->mediatype == MMS_SMIL_MEDIA_VIDEO) {
952                                 char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };
953                                 char thumbPath[MSG_FILEPATH_LEN_MAX+1] = {0, };
954                                 char *pszExt = NULL;
955
956                                 memset(szFileName, 0x00, MSG_FILENAME_LEN_MAX+1);
957                                 memset(thumbPath, 0x00, MSG_FILEPATH_LEN_MAX);
958
959                                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%d.mms",msgId);
960
961                                 if ((pszExt = strrchr(pMedia->szFilePath, '.')) != NULL && !strcasecmp(pszExt, ".png")) {
962                                         snprintf(thumbPath, MSG_FILEPATH_LEN_MAX, MSG_THUMBNAIL_PATH"/%s.png", szFileName);
963                                 } else {
964                                         snprintf(thumbPath, MSG_FILEPATH_LEN_MAX, MSG_THUMBNAIL_PATH"/%s.jpg", szFileName);
965                                 }
966
967                                 if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG) {
968                                         if (makeImageThumbnail(pMedia->szFilePath, thumbPath) == true) {
969                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_IMG, thumbPath);
970                                         } else {
971                                                 MSG_DEBUG("Fail of generating thumbnail: %s to %s", pMedia->szFilePath, thumbPath);
972                                         }
973                                 } else {
974                                         if (makeImageThumbnail(pMedia->szFilePath, thumbPath) == true) {
975                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_VIDEO, thumbPath);
976                                         } else {
977                                                 MSG_DEBUG("Fail of generating thumbnail: %s to %s", pMedia->szFilePath, thumbPath);
978                                         }
979                                 }
980
981                         } else if (pMedia->mediatype == MMS_SMIL_MEDIA_AUDIO) {
982                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_AUDIO, pMedia->szFileName);
983                         }
984                 }
985         } else {
986                 MSG_DEBUG("There is no page");
987         }
988
989         int attachCnt = _MsgMmsGetAttachCount(pMmsMsg);
990         if (attachCnt > 0) {
991                 MMS_ATTACH_S *pAttach = _MsgMmsGetAttachment(pMmsMsg, 0);
992                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_ATTACH, pAttach->szFileName, attachCnt);
993         } else {
994                 MSG_DEBUG("There is no attachment");
995         }
996
997         return MSG_SUCCESS;
998 }
999
1000 void MmsPrintFileInfoForVLD(MMS_MESSAGE_DATA_S *pMmsMsg)
1001 {
1002         MMS_PAGE_S *pPage = NULL;
1003         MMS_MEDIA_S *pMedia = NULL;
1004
1005         if (pMmsMsg == NULL)
1006                 return;
1007
1008         if (pMmsMsg->pageCnt > 0) {
1009                 for (int i = 0; i < pMmsMsg->pageCnt; i++) {
1010
1011                         pPage = _MsgMmsGetPage(pMmsMsg, i);
1012
1013                         if (pPage == NULL)
1014                                 continue;
1015
1016                         for (int j = 0; j < pPage->mediaCnt; j++) {
1017
1018                                 pMedia = _MsgMmsGetMedia(pPage, j);
1019                                 if (pMedia == NULL)
1020                                         continue;
1021
1022                                 MSG_MMS_VLD_FILE("[%s], %d", pMedia->szFilePath, MsgGetFileSize(pMedia->szFilePath));
1023                         }
1024                 }
1025         }
1026
1027         int attachCnt = _MsgMmsGetAttachCount(pMmsMsg);
1028         if (attachCnt > 0) {
1029                 for (int i = 0; i < pMmsMsg->attachCnt; i++) {
1030                 MMS_ATTACH_S *pAttach = _MsgMmsGetAttachment(pMmsMsg, i);
1031                 MSG_MMS_VLD_FILE("[%s], %d", pAttach->szFilePath, MsgGetFileSize(pAttach->szFilePath));
1032                 }
1033         }
1034 }
1035
1036 MMS_MEDIA_S *MmsFindMediaWithCID(MMS_MESSAGE_DATA_S *pMsgData, const char *szContentID)
1037 {
1038
1039         if (pMsgData == NULL || szContentID == NULL || strlen(szContentID) == 0) {
1040                 MSG_DEBUG("Invalid Parameter pMsgData = %p, szContentID = %p", pMsgData, szContentID);
1041                 return NULL;
1042         }
1043
1044         if (pMsgData->pagelist) {
1045                 for (int pageIdx = 0; pageIdx < pMsgData->pageCnt; pageIdx++) {
1046
1047                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
1048
1049                         if (page && page->medialist) {
1050
1051                                 for (int mediaIdx = 0; mediaIdx < page->mediaCnt; mediaIdx++) {
1052
1053                                         MMS_MEDIA_S *media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, mediaIdx);
1054
1055                                         if (media) {
1056                                                 if (strcmp(media->szContentID,  szContentID) == 0) {
1057                                                         MSG_DEBUG("Find media with Content ID [%s] from pMsgData", szContentID);
1058                                                         return media;
1059                                                 }
1060
1061                                         } else {
1062                                                 MSG_DEBUG("Error media NULL");
1063                                                 return NULL;
1064                                         }
1065                                 } //end for media list
1066                         }
1067                 } //end for page list
1068         }
1069
1070         MSG_DEBUG("Not exist Matched media with [%s]", szContentID);
1071         return NULL;
1072 }
1073
1074 MsgMultipart *MmsGetNthMultipart(MmsMsg *pMsg, int index)
1075 {
1076         MsgMultipart *pPart = NULL;
1077
1078         if (MsgIsMultipart(pMsg->msgType.type) == true) {
1079
1080                 pPart = pMsg->msgBody.body.pMultipart;
1081
1082                 while (pPart && index--) {
1083                         pPart = pPart->pNext;
1084                 }
1085
1086                 if (pPart == NULL) {
1087                         MSG_DEBUG("There is no such Multipart [index = %d].", index);
1088                         return NULL;
1089                 } else {
1090                         return pPart;
1091                 }
1092         } else {
1093                 MSG_DEBUG("This Msg is not Multipart");
1094         }
1095
1096         return NULL;
1097 }
1098
1099 bool MmsInsertPartToMmsData(MMS_MESSAGE_DATA_S *pMsgData, MMS_MULTIPART_DATA_S *pMultipart)
1100 {
1101         MMS_MEDIA_S * match_media = NULL;
1102         bool isPimsFile = false;
1103         int tempType;//MimeType
1104
1105         if (pMsgData == NULL || pMultipart == NULL) {
1106                 return false;
1107         }
1108
1109         //for pims file add to attach
1110         MsgGetTypeByFileName(&tempType, pMultipart->szFilePath);
1111         if (tempType == MIME_TEXT_X_VCALENDAR || tempType == MIME_TEXT_X_VCARD) {
1112                 MSG_DEBUG("Pims File");
1113                 isPimsFile = true;
1114         } else {
1115
1116                 if (strlen(pMultipart->szContentID) > 0) {
1117                         char szTempContentID[MSG_MSG_ID_LEN + 1] = {0,};
1118                         removeLessGreaterMark(pMultipart->szContentID, szTempContentID, sizeof(szTempContentID));
1119                         match_media = MmsFindMediaWithCID(pMsgData, szTempContentID);
1120
1121                         if (match_media == NULL && strlen(pMultipart->szContentLocation) > 0 ) {
1122                                 match_media = MmsFindMediaWithCID(pMsgData, pMultipart->szContentLocation);
1123                         }
1124
1125                 } else {
1126                         MSG_DEBUG("ContentID is NULL");
1127                         match_media = NULL;
1128                 }
1129
1130         }
1131
1132         if (match_media && isPimsFile == false) { // set file path
1133                 snprintf(match_media->szFilePath, sizeof(match_media->szFilePath), "%s", pMultipart->szFilePath);
1134                 snprintf(match_media->szFileName, sizeof(match_media->szFileName), "%s", pMultipart->szFileName);
1135                 snprintf(match_media->szContentID, sizeof(match_media->szContentID), "%s", pMultipart->szContentID);
1136                 snprintf(match_media->szContentLocation, sizeof(match_media->szContentLocation), "%s", pMultipart->szContentLocation);
1137                 snprintf(match_media->szContentType, sizeof(match_media->szContentType), "%s", pMultipart->szContentType);
1138         } else { // add attach
1139
1140                 MMS_ATTACH_S *attachment = NULL;
1141                 attachment = (MMS_ATTACH_S *)calloc(sizeof(MMS_ATTACH_S), 1);
1142
1143                 attachment->mediatype = (MimeType)tempType;
1144                 snprintf(attachment->szContentType, sizeof(attachment->szContentType), "%s", pMultipart->szContentType);
1145                 snprintf(attachment->szFilePath, sizeof(attachment->szFilePath), "%s", pMultipart->szFilePath);
1146                 snprintf(attachment->szFileName, sizeof(attachment->szFileName), "%s", pMultipart->szFileName);
1147                 attachment->fileSize = MsgGetFileSize(attachment->szFilePath);
1148
1149                 if (_MsgMmsAddAttachment(pMsgData, attachment) != MSG_SUCCESS) {
1150                         g_free(attachment);
1151                         return false;
1152                 }
1153         }
1154
1155         return true;
1156 }
1157
1158 bool MmsEncodeMmsMessage(MmsMsg *pMmsMsg, const char *raw_filepath)
1159 {
1160         bool encode_ret = false;
1161         mode_t file_mode = (S_IRUSR | S_IWUSR);
1162
1163         if (pMmsMsg == NULL || raw_filepath == NULL) {
1164                 MSG_DEBUG("Invalid Parameter pMmsMsg = %p , raw_filepath = %p", pMmsMsg, raw_filepath);
1165                 return false;
1166         }
1167
1168         FILE *pFile = MsgOpenFile(raw_filepath, "wb+");
1169
1170         if (pFile == NULL) {
1171                 MSG_FATAL("File Open Error: %s", strerror(errno));
1172                 goto __CATCH;
1173         }
1174
1175         if (MsgFseek(pFile, 0L, SEEK_CUR) < 0) {
1176                 MSG_DEBUG("File Fseek Error: %s", strerror(errno));
1177                 goto __CATCH;
1178         }
1179
1180         if (fchmod(fileno(pFile), file_mode) < 0) {
1181                 MSG_DEBUG("File chmod Error: %s", strerror(errno));
1182                 goto __CATCH;
1183         }
1184
1185         switch(pMmsMsg->mmsAttrib.msgType)
1186         {
1187                 case MMS_MSGTYPE_SEND_REQ:
1188                 case MMS_MSGTYPE_SEND_CONF:
1189                         encode_ret = MmsEncodeSendReq(pFile, pMmsMsg);
1190                         if (encode_ret == false) {
1191                                 MSG_DEBUG("Fail to MmsEncodeSendReq");
1192                                 goto __CATCH;
1193                         }
1194                 break;
1195                 default:
1196                         MSG_DEBUG("Not Support msg type : %d", pMmsMsg->mmsAttrib.msgType);
1197                         goto __CATCH;
1198                 break;
1199         }
1200
1201         MsgFsync(pFile);        //file is written to device immediately, it prevents missing file data from unexpected power off
1202         MsgCloseFile(pFile);
1203
1204         return true;
1205
1206 __CATCH:
1207         if (pFile) {
1208                 MsgCloseFile(pFile);
1209         }
1210
1211         return false;
1212 }
1213
1214 bool MmsInsertPartFromMultipart(MmsMsg *pMsg, MMS_MULTIPART_DATA_S *pNewMultipart)
1215 {
1216         MsgMultipart *pMultipart = NULL;
1217         MsgMultipart *pLastPart = NULL;
1218
1219         MimeType mimeType = MIME_UNKNOWN;
1220         char *pExt = NULL;
1221
1222         pExt = strrchr(pNewMultipart->szFilePath, '.');
1223
1224         if (pExt == NULL || pExt[0] == '\0' || strrchr(pExt, '/')) {
1225                 //mimeType = MIME_UNKNOWN;
1226                 mimeType = MimeGetMimeIntFromMimeString(pNewMultipart->szContentType);
1227         } else {
1228                 if (strcasecmp(pExt, ".dcf") == 0)
1229                         mimeType = MIME_APPLICATION_VND_OMA_DRM_CONTENT;
1230                 else {
1231                         if (MmsGetTypeByFileName((int *)&mimeType, pNewMultipart->szFilePath) == false)
1232                                 goto __CATCH;
1233                 }
1234         }
1235
1236         MSG_DEBUG("type = %d, name = %s, filepath = %s, cid = %s, cl = %s", mimeType, pNewMultipart->szFileName, pNewMultipart->szFilePath, pNewMultipart->szContentID, pNewMultipart->szContentLocation);
1237
1238         if (mimeType == MIME_UNKNOWN)
1239                 mimeType = MIME_APPLICATION_OCTET_STREAM;
1240
1241         if (MmsIsMultipart(pMsg->msgType.type) == true) {
1242                 /* Insert as a multipart */
1243                 pMultipart = MmsMakeMultipart(mimeType, pNewMultipart->szFileName, pNewMultipart->szFilePath, pNewMultipart->szContentID, pNewMultipart->szContentLocation);
1244
1245                 if (pMultipart == NULL)
1246                         goto __CATCH;
1247
1248                 if (pMsg->mmsAttrib.contentType == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
1249                         pMsg->mmsAttrib.contentType == MIME_MULTIPART_MIXED)
1250                         pMultipart->type.disposition = MSG_DISPOSITION_ATTACHMENT;
1251
1252                 if (pMsg->msgBody.body.pMultipart == NULL) {
1253                         pMsg->msgBody.body.pMultipart = pMultipart;
1254                 } else {
1255                         pLastPart = pMsg->msgBody.body.pMultipart;
1256                         while (pLastPart->pNext) {
1257                                 pLastPart = pLastPart->pNext;
1258                         }
1259
1260                         pLastPart->pNext = pMultipart;
1261                 }
1262
1263                 pMsg->msgBody.size += pMultipart->pBody->size;
1264                 pMsg->msgType.contentSize += pMultipart->pBody->size;
1265         } else {
1266                 /* Single part - Insert as a message body */
1267                 if (pMsg->mmsAttrib.contentType != mimeType || pMsg->msgType.type != mimeType)
1268                         goto __CATCH;
1269
1270                 strncpy(pMsg->msgType.param.szName, pNewMultipart->szFileName, MSG_LOCALE_FILENAME_LEN_MAX);
1271
1272                 if (MmsIsText(pMsg->msgType.type) == true) {
1273                         pMsg->msgType.param.charset = MSG_CHARSET_UTF8;
1274                 }
1275
1276                 strncpy(pMsg->msgBody.szOrgFilePath, pNewMultipart->szFilePath, MSG_FILEPATH_LEN_MAX - 1);
1277                 pMsg->msgBody.offset = 0;
1278                 pMsg->msgBody.size = MsgGetFileSize(pNewMultipart->szFilePath);
1279                 pMsg->msgType.contentSize = MsgGetFileSize(pNewMultipart->szFilePath);
1280         }
1281
1282         pMsg->nPartCount++;
1283
1284         return true;
1285
1286 __CATCH:
1287         return false;
1288
1289 }
1290
1291 bool MmsMakeMmsData(MmsMsg *pMsg, MMS_MESSAGE_DATA_S *pMmsMsg)
1292 {
1293         MSG_BEGIN();
1294
1295         bzero(pMmsMsg, sizeof(MMS_MESSAGE_DATA_S));
1296         pMmsMsg->regionCnt = 0;
1297         pMmsMsg->pageCnt = 0;
1298         pMmsMsg->attachCnt = 0;
1299         pMmsMsg->transitionCnt = 0;
1300         pMmsMsg->metaCnt = 0;
1301         memset(pMmsMsg->szSmilFilePath, 0, MSG_FILEPATH_LEN_MAX);
1302
1303         if (pMsg->mmsAttrib.contentType == MIME_MULTIPART_RELATED || pMsg->mmsAttrib.contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1304                 char *pSmilDoc = NULL;
1305
1306                 if (pMsg->msgBody.pPresentationBody) {
1307                         if (pMsg->msgBody.pPresentationBody->body.pText) {
1308                                 pSmilDoc = pMsg->msgBody.pPresentationBody->body.pText;
1309                                 if (pSmilDoc) {
1310                                         MmsSmilParseSmilDocOnlyLayout(pMmsMsg, pSmilDoc);
1311                                 }
1312
1313                                 pMmsMsg->smil.type = MIME_APPLICATION_SMIL;
1314                                 snprintf(pMmsMsg->smil.szContentType, MSG_MSG_ID_LEN, "%s", MimeGetMimeStringFromMimeInt(pMsg->msgBody.presentationType.type));
1315                                 snprintf(pMmsMsg->smil.szContentID, MSG_MSG_ID_LEN, "%s", pMsg->msgBody.presentationType.szContentID);
1316                                 snprintf(pMmsMsg->smil.szContentLocation, MSG_MSG_ID_LEN, "%s", pMsg->msgBody.presentationType.szContentLocation);
1317                                 snprintf(pMmsMsg->smil.szFileName, MSG_FILENAME_LEN_MAX, "%s", pMsg->msgBody.presentationType.param.szName);
1318                                 snprintf(pMmsMsg->smil.szFilePath, MSG_FILEPATH_LEN_MAX, MSG_DATA_PATH"%s", pMsg->msgBody.presentationType.param.szFileName);
1319                         }
1320                 } else {
1321                         MSG_DEBUG("Not Exist pPresentationBody");
1322                 }
1323         }
1324
1325         int partCnt = pMsg->nPartCount;
1326
1327         for (int i = 0; i < partCnt; ++i) {
1328
1329                 MsgMultipart *multipart = MmsGetNthMultipart(pMsg, i);
1330                 MMS_MULTIPART_DATA_S pMultipart;
1331
1332                 if (multipart == NULL) {
1333                         MSG_DEBUG("multipart is NULL [%d]", i);
1334                         goto FREE_CATCH;
1335                 }
1336
1337                 bzero(&pMultipart, sizeof(MMS_MULTIPART_DATA_S));
1338                 snprintf(pMultipart.szContentID, sizeof(pMultipart.szContentID), "%s", multipart->type.szContentID);
1339                 snprintf(pMultipart.szContentLocation, sizeof(pMultipart.szContentLocation), "%s", multipart->type.szContentLocation);
1340                 snprintf(pMultipart.szFileName, sizeof(pMultipart.szFileName), "%s", multipart->type.param.szName);
1341                 snprintf(pMultipart.szFilePath, sizeof(pMultipart.szFilePath), "%s", multipart->pBody->szOrgFilePath);
1342                 snprintf(pMultipart.szContentType, sizeof(pMultipart.szContentType), "%s",MimeGetMimeStringFromMimeInt(multipart->type.type));
1343
1344                 if (MmsInsertPartToMmsData(pMmsMsg, &pMultipart) == false) {
1345                         MSG_DEBUG("Fail to MmsSetMultipartToMmsData");
1346                         goto FREE_CATCH;
1347                 }
1348         }
1349         MSG_END();
1350         return true;
1351 FREE_CATCH:
1352         return false;
1353 }
1354
1355 void MmsPrintMmsMsg(const MmsMsg *pMmsMsg)
1356 {
1357         MmsMsgType msgType = pMmsMsg->mmsAttrib.msgType;
1358         if (msgType == MMS_MSGTYPE_SEND_REQ) {
1359                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_SEND_REQ");
1360         } else if (msgType == MMS_MSGTYPE_SEND_CONF) {
1361                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_SEND_CONF");
1362         } else if (msgType == MMS_MSGTYPE_NOTIFICATION_IND) {
1363                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_NOTIFICATION_IND");
1364         } else if (msgType == MMS_MSGTYPE_NOTIFYRESP_IND) {
1365                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_NOTIFYRESP_IND");
1366         } else if (msgType == MMS_MSGTYPE_RETRIEVE_CONF) {
1367                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_RETRIEVE_CONF");
1368         } else if (msgType == MMS_MSGTYPE_ACKNOWLEDGE_IND) {
1369                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_ACKNOWLEDGE_IND");
1370         } else if (msgType == MMS_MSGTYPE_DELIVERY_IND) {
1371                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_DELIVERY_IND");
1372         } else if (msgType == MMS_MSGTYPE_READREC_IND) {
1373                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_READREC_IND");
1374         } else if (msgType == MMS_MSGTYPE_READORG_IND) {
1375                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_READORG_IND");
1376         } else if (msgType == MMS_MSGTYPE_FORWARD_REQ) {
1377                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_FORWARD_REQ");
1378         } else if (msgType == MMS_MSGTYPE_FORWARD_CONF) {
1379                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_FORWARD_CONF");
1380         } else if (msgType == MMS_MSGTYPE_READ_REPLY) {
1381                 PRINT_KEY_VAL_STR("mms type", "MMS_MSGTYPE_READ_REPLY");
1382         } else {
1383                 PRINT_KEY_VAL_STR("mms type", "Unknown");
1384         }
1385
1386         PRINT_KEY_VAL_STR("mms ver", pMmsMsg->mmsAttrib.version);
1387
1388 }
1389
1390 bool MmsComposeSendReq(MmsMsg *pMmsMsg, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, MMS_MESSAGE_DATA_S *pMsgData)
1391 {
1392         MSG_BEGIN();
1393
1394         char *pRawData = NULL;
1395         AutoPtr<char> buf(&pRawData);
1396         struct tm *timeInfo = NULL;
1397         time_t RawTime = 0;
1398         time_t nTimeInSecs = 0;
1399
1400         // Initialize mmsMsg structure
1401         MmsInitMsgAttrib(&pMmsMsg->mmsAttrib);
1402         MmsInitMsgType(&pMmsMsg->msgType);
1403         MmsInitMsgBody(&pMmsMsg->msgBody);
1404
1405         // setting mmsMsg structure
1406         pMmsMsg->mailbox = pMsgInfo->folderId;
1407         pMmsMsg->msgID = pMsgInfo->msgId;
1408
1409         memset(pMmsMsg->szTrID, 0, MMS_TR_ID_LEN + 1);
1410         memset(pMmsMsg->szContentLocation, 0, MMS_LOCATION_LEN + 1);
1411         memset(pMmsMsg->szMsgID, 0, MMS_MSG_ID_LEN + 1);
1412         memset(pMmsMsg->szForwardMsgID, 0, MMS_MSG_ID_LEN + 1);
1413
1414         pMmsMsg->mmsAttrib.dataType = MMS_DATATYPE_DRAFT;
1415         pMmsMsg->mmsAttrib.msgType = MMS_MSGTYPE_SEND_REQ;
1416
1417         if (pSendOptInfo->bSetting == false) {
1418                 unsigned int expiryTime;
1419                 MSG_MMS_DELIVERY_TIME_T deliveryTime;
1420
1421                 pMmsMsg->mmsAttrib.priority = (MmsPriority)MsgSettingGetInt(MMS_SEND_PRIORITY);
1422
1423                 MsgSettingGetBool(MMS_SEND_DELIVERY_REPORT, &pMmsMsg->mmsAttrib.bAskDeliveryReport);
1424                 MsgSettingGetBool(MMS_SEND_READ_REPLY, &pMmsMsg->mmsAttrib.bAskReadReply);
1425                 MsgSettingGetBool(MSG_KEEP_COPY, &pMmsMsg->mmsAttrib.bLeaveCopy);
1426
1427                 expiryTime = (unsigned int)MsgSettingGetInt(MMS_SEND_EXPIRY_TIME);
1428
1429                 if (expiryTime == 0)
1430                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_NONE;
1431                 else {
1432                         pMmsMsg->mmsAttrib.expiryTime.type = MMS_TIMETYPE_RELATIVE;
1433                         pMmsMsg->mmsAttrib.expiryTime.time = expiryTime;
1434                 }
1435
1436                 deliveryTime = (MSG_MMS_DELIVERY_TIME_T)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
1437
1438                 if (deliveryTime == MSG_DELIVERY_TIME_CUSTOM) {
1439                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = true;
1440
1441                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
1442                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_CUSTOM_DELIVERY);
1443                 } else {
1444                         pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = false;
1445
1446                         pMmsMsg->mmsAttrib.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
1447                         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)deliveryTime;
1448                 }
1449         } else {
1450                 pMmsMsg->mmsAttrib.priority = (MmsPriority)pSendOptInfo->option.mmsSendOptInfo.priority;
1451                 pMmsMsg->mmsAttrib.bAskDeliveryReport = pSendOptInfo->bDeliverReq;
1452                 pMmsMsg->mmsAttrib.bAskReadReply = pSendOptInfo->option.mmsSendOptInfo.bReadReq;
1453                 pMmsMsg->mmsAttrib.expiryTime.type = pSendOptInfo->option.mmsSendOptInfo.expiryTime.type;
1454                 pMmsMsg->mmsAttrib.bLeaveCopy = pSendOptInfo->bKeepCopy;
1455
1456                 if (pMmsMsg->mmsAttrib.expiryTime.type != MMS_TIMETYPE_NONE)
1457                         pMmsMsg->mmsAttrib.expiryTime.time = pSendOptInfo->option.mmsSendOptInfo.expiryTime.time;
1458
1459                 pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime;
1460                 pMmsMsg->mmsAttrib.deliveryTime.type = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.type;
1461                 pMmsMsg->mmsAttrib.deliveryTime.time = pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time;
1462         }
1463
1464         MSG_DEBUG("pSendOptInfo->bSetting = %d", pSendOptInfo->bSetting);
1465         MSG_DEBUG("pMmsMsg->mmsAttrib.bLeaveCopy = %d", pMmsMsg->mmsAttrib.bLeaveCopy);
1466
1467         MSG_DEBUG("pMmsMsg->mmsAttrib.bUseDeliveryCustomTime = %d", pMmsMsg->mmsAttrib.bUseDeliveryCustomTime);
1468         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.type = %d", pMmsMsg->mmsAttrib.deliveryTime.type);
1469         MSG_DEBUG("pMmsMsg->mmsAttrib.deliveryTime.time = %d", pMmsMsg->mmsAttrib.deliveryTime.time);
1470
1471         MSG_DEBUG("pMmsMsg->mmsAttrib.priority = %d", pMmsMsg->mmsAttrib.priority);
1472
1473         MSG_DEBUG("pMmsMsg->mmsAttrib.bAskDeliveryReport = %d", pMmsMsg->mmsAttrib.bAskDeliveryReport);
1474         MSG_DEBUG("pMmsMsg->mmsAttrib.bAskReadReply = %d", pMmsMsg->mmsAttrib.bAskReadReply);
1475
1476         MSG_DEBUG("pMmsMsg->mmsAttrib.expiryTime.type = %d", pMmsMsg->mmsAttrib.expiryTime.type);
1477         MSG_DEBUG("pMmsMsg->mmsAttrib.expiryTime.time = %d", pMmsMsg->mmsAttrib.expiryTime.time);
1478
1479         /* MMS-1.3-con-739 */
1480         pMmsMsg->mmsAttrib.msgClass = (MmsMsgClass)MsgSettingGetInt(MMS_SEND_MSG_CLASS);
1481         /* MMS-1.3-con-739 */
1482 #ifdef MMS_13_CON_742_ENABLED
1483         /* MMS-1.3-con-742 */
1484         pMmsMsg->mmsAttrib.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
1485         /* MMS-1.3-con-742 */
1486 #endif
1487
1488         // setting date
1489         time(&RawTime);
1490         timeInfo = localtime(&RawTime);
1491         nTimeInSecs = mktime(timeInfo);
1492         pMmsMsg->mmsAttrib.date = nTimeInSecs;  // todo: need to subtract timeline value to make GMT+0 time
1493
1494         //setting subject
1495         strcpy(pMmsMsg->mmsAttrib.szSubject, pMsgInfo->subject);
1496
1497         //setting adddress
1498         MmsSetMsgAddressList(&pMmsMsg->mmsAttrib, pMsgInfo);
1499
1500         //default type mixed
1501         pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
1502         pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
1503
1504         int pageCnt = _MsgMmsGetPageCount(pMsgData);
1505
1506         if (pageCnt >  0) {     // Multipart related
1507
1508                 int RawDataSize = 0;
1509                 time_t RawTime = 0;
1510                 time(&RawTime);
1511
1512                 snprintf(pMsgData->szSmilFilePath, MSG_FILEPATH_LEN_MAX, "%lu", RawTime);
1513
1514                 MsgMMSCreateSMIL(pMsgData);
1515
1516                 RawDataSize = MmsGetSmilRawData(pMsgData, &pRawData);
1517                 if (RawDataSize < 0) {
1518                         MSG_DEBUG("Smil file size is less than 0");
1519                         return false;
1520                 }
1521                 MSG_DEBUG("%s", pRawData);
1522                 if (pRawData)
1523                         MmsInsertPresentation(pMmsMsg, MIME_APPLICATION_SMIL, NULL, pRawData, strlen(pRawData));
1524
1525                 pMmsMsg->mmsAttrib.contentType = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
1526                 pMmsMsg->msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_RELATED;
1527
1528         }
1529
1530         if (pMmsMsg->msgType.type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1531
1532                 int pageCnt = _MsgMmsGetPageCount(pMsgData);
1533
1534                 for (int i = 0; i < pageCnt; ++i) {
1535                         MMS_PAGE_S *pPage = _MsgMmsGetPage(pMsgData, i);
1536                         int mediaCnt = pPage->mediaCnt;
1537                         MSG_DEBUG("PAGE %d's media Cnt: %d", i+1, mediaCnt);
1538
1539                         for (int j = 0; j < mediaCnt; ++j) {
1540                                 MMS_MEDIA_S *pMedia = _MsgMmsGetMedia(pPage, j);
1541                                 if (pMedia->szFilePath[0] != 0) {
1542                                         MMS_MULTIPART_DATA_S pMultipart;
1543                                         bzero(&pMultipart, sizeof(MMS_MULTIPART_DATA_S));
1544                                         snprintf(pMultipart.szContentID, sizeof(pMultipart.szContentID), "%s", pMedia->szContentID);
1545                                         snprintf(pMultipart.szContentLocation, sizeof(pMultipart.szContentLocation), "%s", pMedia->szContentLocation);
1546                                         snprintf(pMultipart.szFileName, sizeof(pMultipart.szFileName), "%s", pMedia->szFileName);
1547                                         snprintf(pMultipart.szFilePath, sizeof(pMultipart.szFilePath), "%s", pMedia->szFilePath);
1548                                         snprintf(pMultipart.szContentType, sizeof(pMultipart.szContentType), "%s", pMedia->szContentType);
1549
1550                                         if (!MmsInsertPartFromMultipart(pMmsMsg, &pMultipart))
1551                                                 return false;
1552                                 }
1553                         }
1554                 }
1555         }
1556
1557         //Processing Attachment List
1558         for (int i = 0; i < _MsgMmsGetAttachCount(pMsgData); ++i) {
1559                 MMS_ATTACH_S *pMedia = _MsgMmsGetAttachment(pMsgData, i);
1560                 if (pMedia->szFilePath[0] != 0) {
1561                         MMS_MULTIPART_DATA_S pMultipart;
1562                         bzero(&pMultipart, sizeof(MMS_MULTIPART_DATA_S));
1563                         snprintf(pMultipart.szContentID, sizeof(pMultipart.szContentID), "%s", pMedia->szFileName);
1564                         snprintf(pMultipart.szContentLocation, sizeof(pMultipart.szContentLocation), "%s", pMedia->szFileName);
1565                         snprintf(pMultipart.szFileName, sizeof(pMultipart.szFileName), "%s", pMedia->szFileName);
1566                         snprintf(pMultipart.szFilePath, sizeof(pMultipart.szFilePath), "%s", pMedia->szFilePath);
1567                         snprintf(pMultipart.szContentType, sizeof(pMultipart.szContentType), "%s", pMedia->szContentType);
1568
1569                         if (!MmsInsertPartFromMultipart(pMmsMsg, &pMultipart))
1570                                 return false;
1571                 }
1572         }
1573
1574         return true;
1575 }
1576
1577 int MmsUpdatePreviewData(MSG_MESSAGE_INFO_S *pMsgInfo)
1578 {
1579         MSG_BEGIN();
1580         MmsMsg *pMmsMsg;
1581         MMS_MESSAGE_DATA_S msgData = {0,};
1582
1583         MmsPluginStorage::instance()->getMmsMessage(&pMmsMsg);
1584
1585         MmsReleaseMmsMsg(pMmsMsg);
1586
1587         if (MmsReadMsgBody(pMsgInfo->msgId, true, false, NULL) == false) {
1588                 MSG_DEBUG("Fail to MmsReadMsgBody");
1589                 goto __CATCH;
1590         }
1591
1592         if (MmsMakeMmsData(pMmsMsg, &msgData) == false) {
1593                 MSG_DEBUG("Fail to makeMmsMessageData");
1594                 goto __CATCH;
1595         }
1596
1597         MmsMakePreviewInfo(pMsgInfo->msgId, &msgData);
1598
1599         MmsPluginStorage::instance()->getMsgText(&msgData, pMsgInfo->msgText);
1600
1601         MmsReleaseMmsMsg(pMmsMsg);
1602         MsgMmsReleaseMmsLists(&msgData);
1603
1604         MSG_END();
1605         return 0;
1606
1607 __CATCH:
1608         MmsReleaseMmsMsg(pMmsMsg);
1609         MsgMmsReleaseMmsLists(&msgData);
1610         return -1;
1611 }