2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
21 #include <Commons/Exception.h>
22 #include "ReqReceiverMessage.h"
23 #include "Messaging.h"
24 #include "CallbackMgr.h"
25 #include "MsgServiceHandleMgr.h"
30 #include <msg_storage.h>
35 const char* TEXT_AREA = "Text";
36 const char* TEXT_FILE_EXTENSION = ".txt";
37 const char* TEXT_DIR_PREFIX = "/tmp/";
38 const int WHITE_COLOR = 0xffffff;
39 const int BLACK_COLOR = 0x000000;
40 const int ROOT_LAYOUT_WIDTH = 100;
41 const int ROOT_LAYOUT_HEIGHT = 100;
42 const char* EMPTY_ID = "0";
50 Mms::Mms(const string& id) :
52 // m_bodyFilePath(string(tmpnam(NULL)) + TEXT_FILE_EXTENSION),
55 LoggerD("m_id=" << getIdRef());
56 LoggerD("m_msgType=" << getMessageType());
58 char buf[] = "XXXXXX";
59 mode_t mask = umask(S_IWGRP | S_IWOTH);
61 LoggerD("buf : " << buf << " i : " << i);
64 m_bodyFilePath = TEXT_DIR_PREFIX+m_bodyFilePath+TEXT_FILE_EXTENSION;
66 LoggerD("m_bodyFilePath : " << m_bodyFilePath);
68 if (getIdRef().empty()) {
69 LoggerD("create new message");
72 LoggerD("read existing message");
73 readExistingMessage();
77 Mms::Mms(const string& id, const msg_struct_t msg_data) :
79 // m_bodyFilePath(string(tmpnam(NULL)) + TEXT_FILE_EXTENSION),
80 m_messageData(msg_data)
82 LoggerD("m_id=" << getIdRef());
83 LoggerD("m_msgType=" << getMessageType());
85 char buf[] = "XXXXXX";
86 mode_t mask = umask(S_IWGRP | S_IWOTH);
88 LoggerD("buf : " << buf << " i : " << i);
91 m_bodyFilePath = TEXT_DIR_PREFIX+m_bodyFilePath+TEXT_FILE_EXTENSION;
93 LoggerD("m_bodyFilePath : " << m_bodyFilePath);
95 readConversationId(m_messageData);
96 readRecipientList(m_messageData);
97 readPriority(m_messageData);
98 readSubject(m_messageData);
99 readBodyAndAttachments(m_messageData);
100 readFolder(m_messageData);
101 readDateTime(m_messageData);
102 readReadStatus(m_messageData);
103 readMessageStatus(m_messageData);
104 // setMessageStatus(MESSAGE_STATUS_LOADED);
113 // release platform message structure
114 // msg_release_message(&m_messageData);
115 msg_release_struct(&m_messageData);
119 //trying to remove file, return value is skipped
120 if (!m_bodyFilePath.empty()) {
121 LoggerD("remove tmp file=" << m_bodyFilePath);
122 (void) ::remove(m_bodyFilePath.c_str());
126 void Mms::createNewMessage()
129 msg_release_struct(&m_messageData);
130 m_messageData = NULL;
133 char strSubject[MAX_SUBJECT_LEN] = {0,};
135 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
136 msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
137 // msg_struct_t option = NULL;
139 // initialize platform message structure
140 msg_set_int_value(m_messageData, MSG_MESSAGE_TYPE_INT, MSG_TYPE_MMS);
141 msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
142 msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, strSubject, MAX_SUBJECT_LEN);
144 setMmsType(MULTIPART_MIXED);
145 setReadStatus(false);
146 setMessageStatus(MESSAGE_STATUS_CREATED);
148 msg_release_struct(&sendOpt);
149 LoggerD("Message created successfully, Id: " << getIdRef());
152 void Mms::readExistingMessage()
156 if (getIdRef().empty() || (EMPTY_ID == getIdRef()))
158 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
161 msg_release_struct(&m_messageData);
162 m_messageData = NULL;
163 msg_struct_t sendOpt = NULL;
167 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
168 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
170 // trying to get message from platform
171 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt))
173 LoggerE("get message error");
174 Throw(WrtDeviceApis::Commons::PlatformException);
176 LoggerD("message found with msgId=" << getIdRef());
178 // read all mms dat to abstraction layer
179 readConversationId(m_messageData);
180 readRecipientList(m_messageData);
181 readPriority(m_messageData);
182 readSubject(m_messageData);
183 readBodyAndAttachments(m_messageData);
184 readFolder(m_messageData);
185 readDateTime(m_messageData);
186 readReadStatus(m_messageData);
187 readMessageStatus(m_messageData);
189 msg_release_struct(&sendOpt);
191 Catch(WrtDeviceApis::Commons::PlatformException) {
192 LoggerE("exception");
194 msg_release_struct(&sendOpt);
198 void Mms::readDateTime(msg_struct_t& messageData)
200 // tm* time = localtime(msg_get_time(messageData));
201 // tm dateT = getDateTime();
203 msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
205 LoggerE("readDateTime : " <<tempInt);
207 tm* time = localtime((time_t*)&tempInt);
209 LoggerE("localtime failed");
210 Throw(WrtDeviceApis::Commons::PlatformException);
215 void Mms::readReadStatus(msg_struct_t& messageData)
218 msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
219 setReadStatus(tempBool);
222 void Mms::readFolder(msg_struct_t& messageData)
225 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
229 setFolderType(INBOX);
232 setFolderType(OUTBOX);
235 setFolderType(SENTBOX);
238 setFolderType(DRAFTBOX);
241 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
245 void Mms::readMessageStatus(msg_struct_t& messageData)
248 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
250 LoggerD("readMessageStatus folder : " << tempValue);
254 setMessageStatus(MESSAGE_STATUS_LOADED);
257 setMessageStatus(MESSAGE_STATUS_SENDING);
260 setMessageStatus(MESSAGE_STATUS_SENT);
263 setMessageStatus(MESSAGE_STATUS_DRAFT);
266 setMessageStatus(MESSAGE_STATUS_LOADED);
271 void Mms::readPriority(msg_struct_t& messageData)
273 // switch (msg_get_priority_info(messageData)) {
275 msg_get_int_value(messageData, MSG_MESSAGE_PRIORITY_INT, &tempInt);
277 case MSG_MESSAGE_PRIORITY_LOW:
278 setPriority(MessagePriority::LOW);
280 case MSG_MESSAGE_PRIORITY_NORMAL:
281 setPriority(MessagePriority::NORMAL);
283 case MSG_MESSAGE_PRIORITY_HIGH:
284 setPriority(MessagePriority::HIGH);
287 LoggerE("Wrong priority type");
288 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong priority type");
292 void Mms::readConversationId(msg_struct_t& messageData)
295 msg_get_int_value(messageData, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
296 LoggerD("conversationID : " <<tempInt);
300 void Mms::readRecipientList(msg_struct_t& messageData)
302 std::string phoneNumber;
303 // int recipientCount = msg_get_address_count(messageData);
304 // LoggerD("Recipient count " << recipientCount);
306 msg_struct_list_s *addr_list;
307 msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
310 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
312 int recipientCount = addr_list->nCount;
313 LoggerD("Recipient count " << recipientCount);
315 for (int i = 0; i < recipientCount; ++i)
317 // int type = msg_get_direction_info(messageData);
318 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
320 LoggerD("Direction: " << type);
322 if (MSG_DIRECTION_TYPE_MT == type)
324 msg_struct_t addr_info = addr_list->msg_struct_info[i];
325 memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
326 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
327 phoneNumber = strNumber;
329 if (validatePhoneNumber(phoneNumber))
331 setSourceAddress(phoneNumber);
332 setFrom(phoneNumber);
334 LoggerD("MT number: " << phoneNumber);
336 else if (MSG_DIRECTION_TYPE_MO == type)
338 msg_struct_t addr_info = addr_list->msg_struct_info[i];
339 memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
340 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
341 msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
342 LoggerD("MO number: " << strNumber);
343 LoggerD("Type : " << tempInt);
346 case MSG_RECIPIENTS_TYPE_TO:
347 appendToRecipients(strNumber);
349 case MSG_RECIPIENTS_TYPE_CC:
350 appendCcRecipients(strNumber);
352 case MSG_RECIPIENTS_TYPE_BCC:
353 appendBccRecipients(strNumber);
356 LoggerE("Wrong type of recipient");
361 LoggerE("Wrong type of recipient");
362 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
367 void Mms::readSubject(msg_struct_t& messageData)
369 char strTemp[MAX_SUBJECT_LEN] = {0};
370 msg_get_str_value(messageData, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
371 // setSubject(msg_get_subject(messageData));
372 LoggerE("readSubject : " << strTemp);
376 void Mms::readBodyAndAttachments(msg_struct_t& messageData)
379 int pageLen, mediaLen, attachLen, tempInt, ret;
381 msg_struct_t mms_struct = NULL;
383 msg_list_handle_t page_list = NULL;
384 msg_list_handle_t media_list = NULL;
385 msg_list_handle_t attach_list = NULL;
387 msg_struct_t page = NULL;
388 msg_struct_t media = NULL;
389 msg_struct_t attach = NULL;
391 mms_struct = msg_create_struct(MSG_STRUCT_MMS);
392 ret = msg_get_mms_struct(messageData, mms_struct);
394 if (ret != MSG_SUCCESS)
396 LoggerE("cannot get mms struct");
397 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "cannot get mms struct");
400 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
401 char szFileName[MSG_FILENAME_LEN_MAX] = {0,};
403 msg_get_list_handle(mms_struct, MSG_MMS_PAGE_LIST_HND, (void **)&page_list);
404 pageLen = msg_list_length(page_list);
407 setMmsType(MULTIPART_RELATED);
409 setMmsType(MULTIPART_MIXED);
412 LoggerD("page count: " << pageLen);
414 for (int p = 0; p < pageLen; ++p)
416 LoggerD("page " << p);
417 page = (msg_struct_t)msg_list_nth_data(page_list, p);
420 LoggerE("returned page is null, continue");
424 msg_get_list_handle(page, MSG_MMS_PAGE_MEDIA_LIST_HND, (void **)&media_list);
425 mediaLen = msg_list_length(media_list);
427 for (int m = 0; m < mediaLen; ++m)
429 LoggerD("media file " << m);
430 media = (msg_struct_t)msg_list_nth_data(media_list, m);
433 LoggerE("returned media is null, continue");
437 msg_get_int_value(media, MSG_MMS_MEDIA_TYPE_INT, &tempInt);
439 memset(szFilePath, 0, sizeof(szFilePath));
440 msg_get_str_value(media, MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
442 if ((0 == p) && (MMS_SMIL_MEDIA_TEXT == tempInt))
444 //text value on first page goes to body attribute
445 LoggerD("setting body from " << szFilePath);
448 f = fopen(szFilePath, "r");
451 LoggerE("cannot read file with body" << szFilePath);
452 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cannot read file with body");
454 fseek(f, 0, SEEK_END);
455 long int size = ftell(f);
456 fseek(f, 0, SEEK_SET);
457 char* data = new (nothrow) char[size + 1];
460 memset(data, 0, size + 1);
461 error = fread(data, 1, size, f);
465 LoggerE("body is not set");
467 LoggerD("message body: '" << getBody() << "'");
472 LoggerD("adding attachment " << szFilePath);
474 IAttachmentPtr attachment = appendAttachment(szFilePath, false);
475 //attachment->setMessage(this); //set IMessagePtr
477 memset(szFileName, 0, sizeof(szFileName));
478 msg_get_str_value(media, MSG_MMS_MEDIA_FILENAME_STR, szFileName, sizeof(szFileName));
479 if ((szFileName[0] =! '\0' )&& (strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0))
481 LoggerD("renaming to " << szFileName);
482 attachment->rename(szFileName);
484 else if (MMS_SMIL_MEDIA_TEXT == tempInt)
486 std::stringstream newName;
487 newName << "body_page_" << p + 1 << ".txt";
488 LoggerD("renaming to " << newName.str());
489 attachment->rename(newName.str());
495 msg_get_list_handle(mms_struct, MSG_MMS_ATTACH_LIST_HND, (void **)&attach_list);
497 attachLen= msg_list_length(attach_list);
499 LoggerD("attachment count: " << attachLen);
500 for (int a = 0; a < attachLen; ++a)
502 attach = (msg_struct_t)msg_list_nth_data(attach_list, a);
505 LoggerE("attachment is null, continue");
509 memset(szFilePath, 0, sizeof(szFilePath));
510 memset(szFileName, 0, sizeof(szFileName));
512 msg_get_str_value(attach, MSG_MMS_ATTACH_FILEPATH_STR, szFilePath, sizeof(szFilePath));
513 msg_get_str_value(attach, MSG_MMS_ATTACH_FILENAME_STR, szFileName, sizeof(szFileName));
515 IAttachmentPtr att = appendAttachment(szFilePath, false);
516 if ((szFileName[0] =! '\0') &&
517 strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) {
518 LoggerD("renaming to " << szFileName);
519 att->rename(szFileName);
522 msg_release_struct(&mms_struct);
525 bool Mms::hasAttachment()
527 std::size_t attachmentSize = getAttachmentsCount();
528 if ( attachmentSize > 0)
534 void Mms::update(bool draftsOnly)
536 LoggerD("updating m_id=" << getIdRef());
538 if (!m_messageData) {
539 //error if platform message not exists
540 LoggerE("message can not be updated");
541 Throw(WrtDeviceApis::Commons::PlatformException);
544 //update mms data from abstraction
545 if (!draftsOnly || getCurrentFolder() == DRAFTBOX) {
547 updateBodyAndAttachments();
548 updateRecipientList();
554 void Mms::updatePriority()
556 if (isPriorityValid()) {
560 LoggerI("MMS updating priority");
562 //set priority in platform
563 switch (getPriority()) {
564 case MessagePriority::LOW:
565 priority = MSG_MESSAGE_PRIORITY_LOW;
567 case MessagePriority::NORMAL:
568 priority = MSG_MESSAGE_PRIORITY_NORMAL;
570 case MessagePriority::HIGH:
571 priority = MSG_MESSAGE_PRIORITY_HIGH;
574 LoggerE("Wrong priority");
575 Throw(WrtDeviceApis::Commons::PlatformException);
577 LoggerI("priority : " << priority);
580 msg_set_int_value(m_messageData, MSG_MESSAGE_PRIORITY_INT, priority)){
582 LoggerE("Error during setting priority");
583 Throw(WrtDeviceApis::Commons::PlatformException);
587 void Mms::updateRecipientList()
589 // check if abstraction recipient value has been changed
590 if (getToRecipients().isValid() &&
591 getBccRecipients().isValid() &&
592 getCcRecipients().isValid()) {
596 LoggerI("MMS updating platform recipients");
598 // reset addresses in platform structure
599 // msg_reset_address(m_messageData);
600 msg_struct_list_s *addr_list = NULL;
601 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
603 nToCnt = addr_list->nCount;
605 LoggerI("addr_list->nCount" << addr_list->nCount);
609 for (int i = 0; i < nToCnt; i++) {
610 msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, (char *)"", 0);
611 msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, (char *)"", 0);
612 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
613 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
614 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
617 addr_list->nCount = nToCnt;
620 LoggerI("addr_list->nCount" << addr_list->nCount);
622 vector<string> to = getToRecipients().getRecipients();
623 vector<string> cc = getCcRecipients().getRecipients();
624 vector<string> bcc = getBccRecipients().getRecipients();
626 // update addresses in platform structure
629 LoggerI("updating to");
630 for (size_t i = 0; i < to.size(); i++)
632 if (i >= MAX_TO_ADDRESS_CNT)
634 LoggerE("max number of recipient exceeded");
637 LoggerD("adding to[" << i << "]=" << to[i]);
639 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
641 if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
642 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS)
644 LoggerE("problem with adding to address");
645 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
648 if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
649 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS)
651 LoggerE("problem with adding to address");
652 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
656 addr_list->nCount = nToCnt;
657 LoggerE("nToCnt : " << nToCnt);
658 LoggerE("addr_list->nCount: " <<addr_list->nCount);
662 LoggerI("addr_list->nCount" << addr_list->nCount);
666 LoggerI("updating cc");
667 for (size_t i = 0; i < cc.size(); i++)
669 if (nToCnt >= MAX_TO_ADDRESS_CNT)
671 LoggerE("max number of recipient exceeded");
674 LoggerD("adding cc[" << i << "]=" << cc[i]);
676 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
678 if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
679 MSG_RECIPIENTS_TYPE_CC) != MSG_SUCCESS)
681 LoggerE("problem with adding cc address");
682 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
685 if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
686 const_cast<char*>(cc[i].c_str()), cc[i].size()) != MSG_SUCCESS)
688 LoggerE("problem with adding cc address");
689 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
693 addr_list->nCount = nToCnt;
694 LoggerE("nToCnt : " << nToCnt);
695 LoggerE("addr_list->nCount: " <<addr_list->nCount);
698 LoggerI("addr_list->nCount" << addr_list->nCount);
702 LoggerI("updating bcc");
703 for (size_t i = 0; i < bcc.size(); i++)
705 if (nToCnt >= MAX_TO_ADDRESS_CNT)
707 LoggerE("max number of recipient exceeded");
710 LoggerD("adding bcc[" << i << "]=" << bcc[i]);
712 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
714 if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
715 MSG_RECIPIENTS_TYPE_BCC) != MSG_SUCCESS)
717 LoggerE("problem with adding bcc address");
718 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
721 if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
722 const_cast<char*>(bcc[i].c_str()), bcc[i].size()) != MSG_SUCCESS)
724 LoggerE("problem with adding bcc address");
725 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
729 addr_list->nCount = nToCnt;
730 LoggerE("nToCnt : " << nToCnt);
731 LoggerE("addr_list->nCount: " <<addr_list->nCount);
734 LoggerI("addr_list->nCount" << addr_list->nCount);
737 setBccValidity(true);
740 void Mms::updateSubject()
742 // check if abstraction subject value has been changed
743 if (isSubjectValid()) {
746 LoggerI("updating platform subject: " << getSubjectRef());
748 // msg_set_subject(m_messageData, getSubjectRef().c_str())) {
749 msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, const_cast<char*>(getSubjectRef().c_str()), MAX_SUBJECT_LEN)) {
751 LoggerE("problem with setting subject");
752 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
754 setSubjectValidity(true);
757 void Mms::updateBodyAndAttachments()
759 // check if attachment or body source address value has been changed
760 if (isAttachmentsValid() && isBodyValid()) {
764 // MMS_MESSAGE_DATA_S *mmsData = NULL;
765 msg_struct_t mms_struct = NULL;
766 // msg_struct_t old_mms_struct = NULL;// THIS
769 LoggerI("updating platform body and attachment");
773 msg_struct_t region[1];
774 msg_struct_t page[1];
775 msg_struct_t media[1];
776 msg_struct_t smil_text[1];
778 if (getMessageType() != MMS) {
779 LoggerE("Wrong msg type");
780 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong msg type");
782 // mmsData = msg_mms_create_message();
783 mms_struct = msg_create_struct(MSG_STRUCT_MMS);
784 // old_mms_struct = msg_create_struct(MSG_STRUCT_MMS);// THIS
786 // ret = msg_get_mms_struct(m_messageData, old_mms_struct);// THIS
787 // LoggerE("ret " << ret);// THIS
788 // ret = msg_release_struct(&old_mms_struct);// THIS
789 // LoggerE("ret " << ret);// THIS
792 LoggerE("create message body failed");
793 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "create message body failed");
797 if (!getBodyRef().empty()) {
799 f = fopen(m_bodyFilePath.c_str(), "w");
801 LoggerE("cannot create file with body" << m_bodyFilePath);
802 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
803 "cannot create file with body");
805 LoggerD("body file: " << m_bodyFilePath);
806 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
810 LoggerD("getBodyRef(): " << getBodyRef());
812 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_INT, ROOT_LAYOUT_WIDTH);
813 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
814 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_BGCOLOR_INT, WHITE_COLOR);
815 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_PERCENT_BOOL, true);
816 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_PERCENT_BOOL, true);
818 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_REGION, ®ion[0]);
819 msg_set_str_value(region[0], MSG_MMS_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
820 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_INT, 0);
821 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_TOP_INT, 0);
822 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_INT, ROOT_LAYOUT_WIDTH);
823 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
824 msg_set_int_value(region[0], MSG_MMS_REGION_BGCOLOR_INT, WHITE_COLOR);
826 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_PERCENT_BOOL, true);
827 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_TOP_PERCENT_BOOL, true);
828 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_PERCENT_BOOL, true);
829 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_PERCENT_BOOL, true);
831 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_PAGE, &page[0]);
832 msg_set_int_value(page[0], MSG_MMS_PAGE_PAGE_DURATION_INT, 0);
834 msg_mms_add_item(page[0], MSG_STRUCT_MMS_MEDIA, &media[0]);
835 msg_set_int_value(media[0], MSG_MMS_MEDIA_TYPE_INT, MMS_SMIL_MEDIA_TEXT);
836 msg_set_str_value(media[0], MSG_MMS_MEDIA_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
838 error = msg_set_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, const_cast<char*>(m_bodyFilePath.c_str()),MSG_FILEPATH_LEN_MAX);
839 LoggerD("bodyFile Path= " << m_bodyFilePath);
840 LoggerD("error= " << error);
841 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
842 memset(szFilePath, 0, sizeof(szFilePath));
843 msg_get_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
844 LoggerD("bodyFile Path= " << m_bodyFilePath);
846 msg_get_struct_handle(media[0], MSG_MMS_MEDIA_SMIL_TEXT_HND, &smil_text[0]);
847 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_COLOR_INT, BLACK_COLOR);
848 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_SIZE_INT, MMS_SMIL_FONT_SIZE_NORMAL);
849 msg_set_bool_value(smil_text[0], MSG_MMS_SMIL_TEXT_BOLD_BOOL, true);
857 msg_struct_t attachment[20];
859 for (size_t i = 0; i < getAttachmentsRef().size(); ++i) {
860 IAttachmentPtr att = getAttachment(i);
865 LoggerD("Add attachment=" << att->getFullPath());
867 errnum = stat(att->getFullPath().c_str(), &buffer);
868 LoggerD("errnum=" << errnum);
871 LoggerE("Opening file: " << att->getFullPath().c_str());
872 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
873 "cannot open attachment file");
876 //this function should return valid pointer
878 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_ATTACH, &attachment[i]);
879 msg_set_str_value(attachment[i], MSG_MMS_ATTACH_FILEPATH_STR, const_cast<char*>(att->getFullPath().c_str()), MSG_FILEPATH_LEN_MAX);
881 LoggerD("Attachment added");
887 if (MSG_SUCCESS !=msg_set_mms_struct(m_messageData, mms_struct))
889 LoggerE("set message body error");
890 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "set message body error");
893 msg_release_struct(&mms_struct);
895 setAttachmentsValidity(true);
896 setBodyValidity(true);
899 Catch(WrtDeviceApis::Commons::PlatformException) {
900 LoggerE("Platform error");
901 msg_release_struct(&mms_struct);
907 void Mms::updateReadStatus()
909 if (isReadStatusValid()) {
913 LoggerI("updating platform read status: " << isRead());
914 // if (MSG_SUCCESS != msg_set_read_status(m_messageData, isRead())) {
915 if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
916 LoggerE("problem with setting subject");
917 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
920 setReadStatusValidity(true);
923 void Mms::updateIsRead()
925 LoggerD("updating m_id=" << getIdRef());
927 if (!m_messageData) {
928 //error if platform message not exists
929 LoggerE("message can not be updated");
930 Throw(WrtDeviceApis::Commons::PlatformException);
933 // check if abstraction from m_isReadChange value has been changed
934 if (isReadChangeStatusValid()) {
935 // do not update if not changed
941 if (this->getIdRef().empty()) {
942 LoggerE("existing msgId is zero, remove msg not done");
943 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
944 "existing msgId is zero, remove msg not done");
948 msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
950 LoggerE("Failed to update isRead");
951 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
954 Catch(WrtDeviceApis::Commons::PlatformException) {
955 LoggerE("platform error occurs");
957 setisReadChangeStatusValidity(true);
961 void Mms::updateMessage()
963 LoggerD("updating m_id=" << getIdRef());
964 msg_error_t err = MSG_SUCCESS;
965 msg_struct_t msg = NULL;
966 msg_struct_t sendOpt = NULL;
968 if (!m_messageData) {
969 //error if platform message not exists
970 LoggerE("message can not be updated");
971 Throw(WrtDeviceApis::Commons::PlatformException);
977 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
978 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
980 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
982 if (err != MSG_SUCCESS)
984 LoggerE("Get Message Failed!");
985 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
988 if (this->getIdRef().empty()) {
989 LoggerE("existing msgId is zero, update msg not done");
990 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
991 "existing msgId is zero, update msg not done");
996 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
998 LoggerE("Failed to update message");
999 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
1001 msg_release_struct(&msg);
1002 msg_release_struct(&sendOpt);
1004 Catch(WrtDeviceApis::Commons::PlatformException) {
1005 msg_release_struct(&msg);
1006 msg_release_struct(&sendOpt);
1007 LoggerE("platform error occurs");
1012 void Mms::createSendMessage()
1014 LoggerD("convert m_id= " << convertId(getIdRef()));
1016 //prepare for add sms to draft folder
1017 if (!m_messageData) {
1018 //error if platform message not exists
1019 LoggerE("message can not be updated");
1020 Throw(WrtDeviceApis::Commons::PlatformException);
1023 //update all sms data
1024 if (getCurrentFolder() == DRAFTBOX) {
1025 LoggerD("update all mms data");
1027 updateBodyAndAttachments();
1028 updateRecipientList();
1035 // MSG_SENDINGOPT_S option = { false, false, false };
1036 // option.option.smsSendOpt.bReplyPath = true;
1037 // trying to get message from platform
1039 // msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1040 // msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
1043 // const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
1045 // msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1046 // msg_set_int_value(m_messageData, MSG_MESSAGE_FOLDER_ID_INT, 4);
1047 // msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_NOT_SEND);
1049 setId(convertId(msgId));
1050 setFolderType(DRAFTBOX);
1051 setMessageStatus(MESSAGE_STATUS_DRAFT);
1053 Catch(WrtDeviceApis::Commons::PlatformException) {
1054 LoggerE("remove message error");
1055 if (m_messageData) {
1056 //releasing platform message structure
1057 // msg_release_message(&m_messageData);
1058 msg_release_struct(&m_messageData);
1065 void Mms::addMessageToDraft()
1067 LoggerD("convert m_id= " << convertId(getIdRef()));
1069 msg_struct_t sendOpt = NULL;
1071 //prepare for add sms to draft folder
1072 if (!m_messageData) {
1073 //error if platform message not exists
1074 LoggerE("message can not be updated");
1075 Throw(WrtDeviceApis::Commons::PlatformException);
1078 //update all mms data
1079 if (getCurrentFolder() == DRAFTBOX) {
1081 updateBodyAndAttachments();
1082 updateRecipientList();
1089 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1091 // trying to add message
1092 LoggerD("add message");
1093 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1094 if (ret < MSG_SUCCESS) {
1095 LoggerE("msg_add_message failed, error code=" << ret);
1096 Throw(WrtDeviceApis::Commons::PlatformException);
1103 LoggerD("Message ID : " << msgId);
1106 LoggerD("Message ID is invailded ");
1107 Throw(WrtDeviceApis::Commons::PlatformException);
1110 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1112 setId(convertId(msgId));
1113 readExistingMessage();
1115 msg_release_struct(&sendOpt);
1118 Catch(WrtDeviceApis::Commons::PlatformException) {
1119 LoggerE("add draft message error");
1120 msg_release_struct(&sendOpt);
1126 void Mms::readAllData()
1128 readExistingMessage();
1131 void Mms::moveToFolder(const FolderType newFolder)
1138 msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1141 if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1142 platfromFolderId) != MSG_SUCCESS) {
1143 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error during movinf message");
1147 Catch(WrtDeviceApis::Commons::PlatformException) {
1148 LoggerE("remove message error");
1154 void Mms::moveToFolder(const string& newFolder)
1161 msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1164 if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1165 platfromFolderId) != MSG_SUCCESS) {
1166 LoggerE("msg_move_msg_to_folder error");
1167 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_move_msg_to_folder error");
1171 Catch(WrtDeviceApis::Commons::PlatformException) {
1172 LoggerE("remove message error");
1178 void Mms::copyToFolder(const FolderType newFolder)
1183 msg_message_t msg = msg_new_message();
1187 MSG_SENDINGOPT_S option = { false, false, false };
1188 option.option.smsSendOpt.bReplyPath = true;
1190 msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1192 const msg_folder_id_t platfromFolderId =
1193 Messaging::convertFolderToPlatform(newFolder);
1194 msg_set_message_id(msg, 0);
1195 msg_set_folder_id(msg, platfromFolderId);
1197 int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1198 if (error != MSG_SUCCESS) {
1199 LoggerE("msg_add_message failed, error code=" << error);
1200 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1204 msg_release_message(&msg);
1208 Catch(WrtDeviceApis::Commons::PlatformException) {
1209 LoggerE("remove message error");
1211 msg_release_message(&msg);
1218 void Mms::copyToFolder(const string& newFolder)
1223 msg_message_t msg = msg_new_message();
1227 MSG_SENDINGOPT_S option = { false, false, false };
1228 option.option.smsSendOpt.bReplyPath = true;
1230 msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1232 const msg_folder_id_t platfromFolderId =
1233 Messaging::convertFolderToPlatform(newFolder);
1234 msg_set_message_id(msg, 0);
1235 msg_set_folder_id(msg, platfromFolderId);
1237 int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1238 if (error != MSG_SUCCESS) {
1239 LoggerE("msg_add_message failed, error code=" << error);
1240 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1244 msg_release_message(&msg);
1248 Catch(WrtDeviceApis::Commons::PlatformException) {
1249 LoggerE("remove message error");
1251 msg_release_message(&msg);
1260 LoggerD("delete m_id=" << getIdRef());
1264 if (this->getIdRef().empty()) {
1265 LoggerE("existing msgId is zero, remove msg not done");
1266 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1267 "existing msgId is zero, remove msg not done");
1271 msg_delete_message(MsgGetCommonHandle(),
1272 convertId(getIdRef()))) {
1273 LoggerE("Failed to delete Message");
1274 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to delete Message");
1278 Catch(WrtDeviceApis::Commons::PlatformException) {
1279 LoggerE("platform error occurs");
1285 LoggerD("sending message, id=" << getIdRef());
1288 //prepare for sending sms/mms
1291 LoggerD("Start Sending Message...");
1293 LoggerD("trying to send mms");
1294 msg_error_t err = CallbackMgrSingleton::Instance().registerAndSend(msg_mms_send_message, m_messageData, this);
1296 if (err != MSG_SUCCESS)
1298 LoggerE("Sending Message (submit request) failed!!! err=" << err);
1299 setMessageStatus(MESSAGE_STATUS_FAILED);
1303 setMessageStatus(MESSAGE_STATUS_SENDING);
1305 LoggerD("Sending Message request is submitted!");
1307 Catch(WrtDeviceApis::Commons::PlatformException) {
1308 LoggerE("message is not send, manually run callback");
1315 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1316 if (requestReceiver) {
1317 LoggerE("send Error");
1318 EventMessagingServicePtr event = getMessagingServiceEvent();
1319 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1320 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService>::ManualAnswer(event);
1325 void Mms::sendingCallback(msg_struct_t sent_status)
1327 // LoggerI("sendingCallback callback received. Req id = " <<
1328 // sent_status->reqId << " Status = " << (int)sent_status->status);
1329 // msg_struct_t request_data = NULL;
1330 // msg_get_struct_handle(sent_status, MSG_STRUCT_REQUEST_INFO, (void **)request_data);
1331 // msg_get_int_value(request_data, MSG_REQUEST_REQUESTID_INT, &tempInt);
1332 int status = MSG_NETWORK_SEND_FAIL;
1333 msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
1335 LoggerD("status : " << status);
1337 if (MSG_NETWORK_SEND_SUCCESS == status) {
1338 LoggerD("sending ok");
1339 setSendingStatusOk();
1340 setMessageStatus(MESSAGE_STATUS_SENT);
1342 LoggerE("sending failed: " << static_cast<unsigned int>(status));
1343 setSendingStatusFailed();
1344 setMessageStatus(MESSAGE_STATUS_FAILED);
1348 void Mms::sendCancel(int handle)
1350 LoggerD("sending message, id=" << getIdRef());
1352 LoggerD("handle =" << handle);
1355 void Mms::setSendingStatusOk()
1357 //success callback should be executed here
1358 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1359 msg_struct_list_s *addr_list = NULL;
1360 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1363 if (requestReceiver) {
1364 LoggerI("calling JS success callback");
1365 EventMessagingServicePtr event = getMessagingServiceEvent();
1366 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1368 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1370 nToCnt = addr_list->nCount;
1372 for ( int index=0; index < nToCnt; index++)
1374 msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1375 event->m_successRecipients.push_back(strNumber);
1378 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1382 void Mms::setSendingStatusFailed()
1384 //error callback should be executed here
1385 EventOnSendingFailedEmitterPtr emitter = getEmitter();
1386 msg_struct_list_s *addr_list = NULL;
1387 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1392 EventOnSendingFailedPtr event(new EventOnSendingFailed);
1393 event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
1394 emitter->emit(event);
1398 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1399 EventMessagingServicePtr event = getMessagingServiceEvent();
1400 if (requestReceiver)
1402 LoggerE("calling JS error callback");
1403 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1404 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1405 nToCnt = addr_list->nCount;
1407 LoggerD("nToCnt : " << nToCnt);
1409 for ( int index=0; index < nToCnt; index++)
1411 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1412 event->m_failRecipients.push_back(strNumber);
1415 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1420 FolderType Mms::toFolder(const std::string &folder)
1422 if (folder == "INBOX") {
1424 } else if (folder == "OUTBOX") {
1426 } else if (folder == "SENTBOX") {
1428 } else if (folder == "DRAFTBOX") {
1431 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");