2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
20 #include "MsgMmsTypes.h"
23 #include "MsgUtilFile.h"
24 #include "MsgCppTypes.h"
25 #include "MsgVMessage.h"
28 /*==================================================================================================
30 ==================================================================================================*/
31 #define INSERT_VMSG_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
34 vmsg_free_vtree_memory( pMessage );\
37 if (pMessage->pTop == NULL)\
39 pMessage->pTop = pObject;\
43 pMessage->pCur->pSibling = pObject;\
45 pMessage->pCur = pObject;
48 #define INSERT_VBODY_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
51 vmsg_free_vtree_memory( pMessage );\
54 if (pBody->pTop == NULL)\
56 pBody->pTop = pObject;\
60 pBody->pCur->pSibling = pObject;\
62 pBody->pCur = pObject;
65 #define INSERT_VCARD_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
68 vmsg_free_vtree_memory( pMessage );\
71 if (pCard->pTop == NULL)\
73 pCard->pTop = pObject;\
77 pCard->pCur->pSibling = pObject;\
79 pCard->pCur = pObject;
82 #define INSERT_PARAM param = (VParam*)calloc(1, sizeof(VParam));\
85 vmsg_free_vtree_memory( pMessage );\
95 /*==================================================================================================
96 FUNCTION IMPLEMENTATION
97 ==================================================================================================*/
98 char* MsgVMessageAddRecord(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S* pMsg)
102 VObject* pObject = NULL;
103 VParam* param = NULL;
104 VTree * pBody = NULL;
105 VTree * pCard = NULL;
106 VTree * pCurrent= NULL;
107 struct tm display_time;
108 char* encoded_data = NULL;
109 int err = MSG_SUCCESS;
111 VTree* pMessage = NULL;
112 if (pMessage == NULL) {
113 pMessage = (VTree *)malloc(sizeof(VTree));
117 pMessage->treeType = VMESSAGE;
118 pMessage->pTop = NULL;
119 pMessage->pCur = NULL;
120 pMessage->pNext = NULL;
125 //Insert VObject (X-MESSAGE-TYPE) to VMessage tree
128 pObject->property = VMSG_TYPE_MSGTYPE;
130 if(pMsg->msgType.subType == MSG_NORMAL_SMS)
131 pObject->pszValue[0] = strdup("SMS");
133 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
134 pObject->pszValue[0] = strdup("MMS NOTIFICATION");
137 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_SENDREQ_MMS || pMsg->msgType.subType == MSG_SENDCONF_MMS))
138 pObject->pszValue[0] = strdup("MMS SEND");
140 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS))
141 pObject->pszValue[0] = strdup("MMS RETRIEVED");
146 pObject->valueCount = 1;
149 //Insert VObject (X-IRMC-BOX) to VMessate tree
152 pObject->property = VMSG_TYPE_MSGBOX;
154 switch(pMsg->folderId)
157 pObject->pszValue[0] = strdup("INBOX");
160 pObject->pszValue[0] = strdup("OUTBOX");
163 pObject->pszValue[0] = strdup("SENTBOX");
166 pObject->pszValue[0] = strdup("DRAFTBOX");
169 // Discard User Defined or Spam folder's messages.
172 pObject->valueCount = 1;
175 //Insert VObject (X-SS-DT) to VMessage tree
178 pObject->property = VMSG_TYPE_DATE;
179 localtime_r(&(pMsg->displayTime), &display_time);
180 pObject->pszValue[0] = _convert_tm_to_vdata_str(&display_time);
181 pObject->valueCount = 1;
184 //Insert Vobject read status to VMessage tree
187 pObject->property = VMSG_TYPE_STATUS;
190 pObject->pszValue[0] = strdup("READ");
192 pObject->pszValue[0] = strdup("UNREAD");
194 pObject->valueCount = 1;
197 //Insert VBody tree for message body;
198 pBody = (VTree*)calloc(1, sizeof(VTree));
201 pBody->treeType = VBODY;
205 pCurrent->pNext = pBody;
208 if(strlen(pMsg->subject) > 0)
210 //Insert Subject object
212 pObject->property = VMSG_TYPE_SUBJECT;
213 pObject->pszValue[0] = strdup(pMsg->subject);
214 pObject->valueCount = 1;
216 //Insert VBody object
218 pObject->property = VMSG_TYPE_BODY;
220 if(pMsg->msgType.mainType == MSG_SMS_TYPE)
222 if(pMsg->msgType.subType == MSG_NORMAL_SMS)
224 if (pMsg->bTextSms == false)
226 char* pFileData = NULL;
227 AutoPtr<char> buf(&pFileData);
230 char* msgText = NULL;
232 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false)
235 msgText = (char *)calloc(1, fileSize);
236 memcpy(msgText, pFileData, fileSize);
237 pObject->numOfBiData = fileSize;
238 pObject->pszValue[0] = msgText;
242 pObject->numOfBiData = pMsg->dataSize;
243 pObject->pszValue[0] = strdup(pMsg->msgText);
245 pObject->valueCount = 1;
251 else if(pMsg->msgType.mainType == MSG_MMS_TYPE)
253 //Insert VBody for mms raw data;
254 char* pFileData = NULL;
257 char* msgText = NULL;
258 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
260 if(pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
261 pFileData = MsgOpenAndReadMmsFile(pMsg->msgData, 0, -1, &fileSize);
265 err = MsgStoGetMmsRawFilePath(pDbHandle, pMsg->msgId, filePath);
267 if (err != MSG_SUCCESS)
270 pFileData = MsgOpenAndReadMmsFile(filePath, 0, -1, &fileSize);
272 MSG_DEBUG("FILE SIZE IS %d", fileSize);
273 msgText = (char *)calloc(1, fileSize);
275 memcpy(msgText, pFileData, fileSize);
276 pObject->numOfBiData = fileSize;
277 pObject->pszValue[0] = msgText;
278 pObject->valueCount = 1;
286 // Insert parameter for base64 encoding
287 MSG_DEBUG("before to start INSERT_PARAM");
289 pObject->pParam = param;
290 param->parameter = VMSG_PARAM_ENCODING;
291 param->paramValue = VMSG_ENC_PARAM_BASE64;
293 // Add VCard tree for recipient address information.
294 for(int i = 0; i < pMsg->nAddressCnt; ++i)
296 pCard = (VTree*)calloc(1, sizeof(VTree));
300 pCard->treeType = VCARD;
304 pCurrent->pNext = pCard;
308 pObject->property = VCARD_TYPE_TEL;
309 pObject->pszValue [0] = strdup(pMsg->addressList[i].addressVal);
310 pObject->valueCount = 1;
312 MSG_DEBUG("before to start vmsg_encode");
313 encoded_data = vmsg_encode(pMessage);
315 vmsg_free_vtree_memory(pMessage);
320 vmsg_free_vtree_memory( pMessage );
326 char* _convert_tm_to_vdata_str(const struct tm * tm)
328 char str[17] = {0, };
330 snprintf(str, 17, "%04d%02d%02dT%02d%02d%02dZ",
342 bool _convert_vdata_str_to_tm(const char* szText, struct tm * tm)
345 if (szText == NULL) return false;
346 if (strlen(szText) < 15) return false;
347 if (szText[8] != 'T') return false;
350 memset(tm, 0, sizeof(struct tm));
353 memcpy(szBuff, &(szText[0]), 4);
355 tm->tm_year = atol(szBuff) - 1900;
356 if ((tm->tm_year > 137) || (tm->tm_year < 0))
359 memcpy(szBuff, &(szText[4]), 2);
361 tm->tm_mon = atol(szBuff)-1;
362 if ((tm->tm_mon > 11) || (tm->tm_mon < 0))
365 memcpy(szBuff, &(szText[6]), 2);
367 tm->tm_mday = atol(szBuff);
368 if ((tm->tm_mday > 31) || (tm->tm_mday < 1))
371 // hour, minute, second
372 memcpy(szBuff, &(szText[9]), 2);
374 tm->tm_hour = atol(szBuff);
375 if ((tm->tm_hour > 23) || (tm->tm_hour < 0))
378 memcpy(szBuff, &(szText[11]), 2);
380 tm->tm_min = atol(szBuff);
381 if ((tm->tm_min > 59) || (tm->tm_min < 0))
384 memcpy(szBuff, &(szText[13]), 2);
386 tm->tm_sec = atol(szBuff);
387 if ((tm->tm_sec > 59) || (tm->tm_sec < 0))
393 char *MsgVMessageEncode(MSG_MESSAGE_INFO_S *pMsg)
397 MsgDbHandler dbHandle;
398 VObject *pObject = NULL;
399 VParam *param = NULL;
402 VTree *pCurrent= NULL;
403 VTree *pMessage = NULL;
405 struct tm display_time;
406 char *encoded_data = NULL;
407 int err = MSG_SUCCESS;
409 if (pMessage == NULL) {
410 pMessage = (VTree *)malloc(sizeof(VTree));
414 pMessage->treeType = VMESSAGE;
415 pMessage->pTop = NULL;
416 pMessage->pCur = NULL;
417 pMessage->pNext = NULL;
422 //Insert VObject (X-MESSAGE-TYPE) to VMessage tree
425 pObject->property = VMSG_TYPE_MSGTYPE;
427 if(pMsg->msgType.subType == MSG_NORMAL_SMS)
428 pObject->pszValue[0] = strdup("SMS");
430 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
431 pObject->pszValue[0] = strdup("MMS NOTIFICATION");
434 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_SENDREQ_MMS || pMsg->msgType.subType == MSG_SENDCONF_MMS))
435 pObject->pszValue[0] = strdup("MMS SEND");
437 else if(pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS))
438 pObject->pszValue[0] = strdup("MMS RETRIEVED");
443 pObject->valueCount = 1;
446 //Insert VObject (X-IRMC-BOX) to VMessate tree
449 pObject->property = VMSG_TYPE_MSGBOX;
451 switch(pMsg->folderId)
454 pObject->pszValue[0] = strdup("INBOX");
457 pObject->pszValue[0] = strdup("OUTBOX");
460 pObject->pszValue[0] = strdup("SENTBOX");
463 pObject->pszValue[0] = strdup("DRAFTBOX");
466 // Discard User Defined or Spam folder's messages.
469 pObject->valueCount = 1;
472 //Insert VObject (X-SS-DT) to VMessage tree
475 pObject->property = VMSG_TYPE_DATE;
476 localtime_r(&(pMsg->displayTime), &display_time);
477 pObject->pszValue[0] = _convert_tm_to_vdata_str(&display_time);
478 pObject->valueCount = 1;
481 //Insert Vobject read status to VMessage tree
484 pObject->property = VMSG_TYPE_STATUS;
487 pObject->pszValue[0] = strdup("READ");
489 pObject->pszValue[0] = strdup("UNREAD");
491 pObject->valueCount = 1;
494 //Insert VBody tree for message body;
495 pBody = (VTree*)calloc(1, sizeof(VTree));
498 pBody->treeType = VBODY;
502 pCurrent->pNext = pBody;
505 if(strlen(pMsg->subject) > 0)
507 //Insert Subject object
509 pObject->property = VMSG_TYPE_SUBJECT;
510 pObject->pszValue[0] = strdup(pMsg->subject);
511 pObject->valueCount = 1;
513 //Insert VBody object
515 pObject->property = VMSG_TYPE_BODY;
517 if(pMsg->msgType.mainType == MSG_SMS_TYPE)
519 if(pMsg->msgType.subType == MSG_NORMAL_SMS)
521 if (pMsg->bTextSms == false)
523 char* pFileData = NULL;
524 AutoPtr<char> buf(&pFileData);
527 char* msgText = NULL;
529 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false)
532 msgText = (char *)calloc(1, fileSize);
533 memcpy(msgText, pFileData, fileSize);
534 pObject->numOfBiData = fileSize;
535 pObject->pszValue[0] = msgText;
539 pObject->numOfBiData = pMsg->dataSize;
540 pObject->pszValue[0] = strdup(pMsg->msgText);
542 pObject->valueCount = 1;
548 else if(pMsg->msgType.mainType == MSG_MMS_TYPE)
550 //Insert VBody for mms raw data;
551 char* pFileData = NULL;
554 char* msgText = NULL;
555 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
557 if(pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
558 pFileData = MsgOpenAndReadMmsFile(pMsg->msgData, 0, -1, &fileSize);
562 err = MsgStoGetMmsRawFilePath(&dbHandle, pMsg->msgId, filePath);
564 if (err != MSG_SUCCESS)
567 pFileData = MsgOpenAndReadMmsFile(filePath, 0, -1, &fileSize);
569 MSG_DEBUG("FILE SIZE IS %d", fileSize);
570 msgText = (char *)calloc(1, fileSize);
572 memcpy(msgText, pFileData, fileSize);
573 pObject->numOfBiData = fileSize;
574 pObject->pszValue[0] = msgText;
575 pObject->valueCount = 1;
583 // Insert parameter for base64 encoding
584 MSG_DEBUG("before to start INSERT_PARAM");
586 pObject->pParam = param;
587 param->parameter = VMSG_PARAM_ENCODING;
588 param->paramValue = VMSG_ENC_PARAM_BASE64;
590 // Add VCard tree for recipient address information.
591 for(int i = 0; i < pMsg->nAddressCnt; ++i)
593 pCard = (VTree*)calloc(1, sizeof(VTree));
597 pCard->treeType = VCARD;
601 pCurrent->pNext = pCard;
605 pObject->property = VCARD_TYPE_TEL;
606 pObject->pszValue [0] = strdup(pMsg->addressList[i].addressVal);
607 pObject->valueCount = 1;
609 MSG_DEBUG("before to start vmsg_encode");
610 encoded_data = vmsg_encode(pMessage);
612 vmsg_free_vtree_memory(pMessage);
617 vmsg_free_vtree_memory( pMessage );