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.
23 #include <email-types.h>
24 #include <email-api.h>
25 #include <dpl/scoped_fclose.h>
26 #include <dpl/scoped_ptr.h>
27 #include <Commons/Exception.h>
28 #include <Commons/StringUtils.h>
29 #include "MessageFactory.h"
30 #include "Messaging.h"
32 #include "EmailService.h"
33 #include "EmailUtils.h"
34 #include "EmailConverter.h"
35 #include "MailSender.h"
37 #include "MessagingService.h"
38 #include "MessagingServiceManager.h"
39 #include "Attachment.h"
42 #define LOG_ENTER LoggerD("---> ENTER");
43 #define LOG_EXIT LoggerD("---> EXIT");
46 using namespace WrtDeviceApis::Commons;
52 const char* COMMAND_NAME = "/bin/cp";
53 const char* COMMAND_SWITCH_RECURSIVE = "-r";
54 //const char* COMMAND_SWITCH_FORCE = "-f";
57 Email::Email(const string& id) :
62 if (getIdRef().empty()) {
64 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
68 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
69 LoggerE("Exception: " << ex.DumpToString());
75 Email::Email(const std::string& id, int accountId) :
81 if (getIdRef().empty()) {
83 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
86 LoggerD("Account ID = " << accountId);
87 m_accountId = accountId;
90 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
91 LoggerE("Exception: " << ex.DumpToString());
98 Email::Email(EmailAccountInfo& account) : IMessage(EMAIL)
103 if (getIdRef().empty()) {
109 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
110 LoggerE("Exception: " << ex.DumpToString());
116 Email::Email(const FolderType folder) : IMessage(EMAIL)
121 if (folder == TEMP_FOLDER) {
125 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Folder Type is miss-matched");
128 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
129 LoggerE("Exception: " << ex.DumpToString());
146 int handle = MailSender::getInstance().send(
147 MessageFactory::convertToEmail(SharedFromThis())
154 void Email::sendCancel(int handle)
158 MailSender::getInstance().cancel(handle);
163 int Email::downloadBody()
166 return MailSync::getInstance().downloadBody( MessageFactory::convertToEmail(SharedFromThis()) );
170 void Email::downloadBodyCancel( int handle)
172 return MailSync::getInstance().cancelDownloadBody(handle);
175 int Email::downloadAttachment( const IAttachmentPtr& attachment)
178 return MailSync::getInstance().downloadAttachment( MessageFactory::convertToEmail(SharedFromThis()), attachment );
182 void Email::downloadAttachmentCancel( int handle)
185 return MailSync::getInstance().cancelDownloadAttachment(handle);
189 void Email::update(bool draftsOnly)
193 DPL::Mutex::ScopedLock mx(&m_updateMutex);
196 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
205 email_attachment_data_t* attachment=NULL;
206 int attachmentCount = 0;
207 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
208 if (EMAIL_ERROR_NONE != error) {
209 LoggerW("Nothing to update or error. [" << error << "]");
212 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, NULL, 0);
213 if (EMAIL_ERROR_NONE != error) {
214 LoggerW("Nothing to update or error. [" << error << "]");
219 error = email_free_attachment_data(&attachment, attachmentCount);
220 if (EMAIL_ERROR_NONE != error) {
221 LoggerW("Nothing to update or error. [" << error << "]");
228 int Email::getAccountID()
233 return m_mail->account_id;
245 return m_mail->mail_id;
252 int Email::isBodyDownloaded()
256 if ( m_mail->body_download_status == 1 ) // 1 is fully downloaded body.
265 bool Email::hasAttachment()
267 std::size_t attachmentSize = getAttachmentsCount();
268 if ( attachmentSize > 0)
274 void Email::readAllData()
279 void Email::moveToFolder(const FolderType newFolder)
284 moveToFolder(EmailConverter::toMailboxType(newFolder));
289 void Email::moveToFolder(const string& newFolder)
292 Assert(m_mail && "mail is NULL.");
296 int accountId = m_mail->account_id;
297 int mailId = getIntId();
299 emf_mailbox_t* mailbox = EmailService::alloc<emf_mailbox_t>();
300 mailbox->account_id = accountId;
301 mailbox->name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
303 int error = email_move_mail_to_mailbox(&mailId, 1, mailbox);
304 if (EMF_ERROR_NONE != error) {
306 WrtDeviceApis::Commons::PlatformException,
307 "Couldn't move mail to folder: " << newFolder << ". [" <<
311 error = email_free_mailbox(&mailbox, 1); //free
312 if (EMF_ERROR_NONE != error) {
313 LoggerE("Failed to destroy mailbox: " << error);
318 void Email::copyToFolder(const FolderType newFolder)
322 copyToFolder(EmailConverter::toMailboxName(newFolder));
327 void Email::copyToFolder(const string& newFolder)
331 Assert(m_mail && "mail is NULL.");
335 int accountId = m_mail->account_id;
336 ScopedMail mail(EmailService::cloneMail(m_mail.Get())); //clone
337 mail->mailbox_name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
338 mail->account_id = accountId;
341 email_attachment_data_t* attachment = NULL;
342 int attachmentCount = 0;
343 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
344 if (EMAIL_ERROR_NONE != error) {
345 ThrowMsg( WrtDeviceApis::Commons::PlatformException,
346 "Couldn't get attachment list: " << newFolder << ". [" << error << "]");
349 int mailId = EmailService::addMail( mail.Get(), attachment );
351 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cloned mail didn't get new id.");
359 EmailService::deleteMail(m_accountId, getIntId());
367 EmailAccountInfo account = IMessaging::getInstance().getCurrentEmailAccount();
368 m_accountId = account.getIntId();
369 ScopedMail mail(EmailService::createMail(account));
370 ScopedMailbox mailbox(
371 EmailService::getMailboxByType(account.getIntId(),
372 EMAIL_MAILBOX_TYPE_DRAFT)
374 setId(convertId(EmailService::addMailToMailbox(mail.Get(), mailbox.Get())));
375 setFolderType(DRAFTBOX);
376 setMessageStatus(MESSAGE_STATUS_DRAFT);
385 setFolderType(TEMP_FOLDER);
386 setMessageStatus(MESSAGE_STATUS_CREATED);
390 void Email::create( EmailAccountInfo& account )
394 m_accountId = account.getIntId();
395 //MailSender::getInstance();
397 LoggerD("account ID : " << m_accountId);
398 ScopedMail mail(EmailService::createMailData(account));
400 setEmailAccount(account); //save account
402 mail->mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
404 setId(convertId(EmailService::addMail(mail.Get(), NULL)));
405 setFolderType(DRAFTBOX);
406 setMessageStatus(MESSAGE_STATUS_DRAFT);
415 LoggerD("mail ID :" << getIntId());
416 m_mail.Reset(EmailService::readMail(getIntId())); //reset Mail Data
422 //if (m_mail->head) { readHeader(); }
423 //if (m_mail->body) { readBody(); }
424 //if (m_mail->info) { readInfo(); }
426 //m_mailbox.Reset(EmailService::getMailboxByMailId(m_accountId, getIntId()) );
427 //setFolderType(EmailConverter::toFolderType(m_mailbox->mailbox_type));
429 setFolderType(EmailConverter::toFolderType(m_mail->mailbox_type));
430 setMessageStatus(EmailConverter::toMessageStatus(m_mail->mailbox_type));
435 void Email::readHeader()
440 LoggerE("Header is NULL");
441 Throw(WrtDeviceApis::Commons::PlatformException);
444 if (m_mail->subject) {
445 setSubject(m_mail->subject);
448 if (m_mail->full_address_to) {
449 appendToRecipients(EmailUtils::stripAddressLine(m_mail->full_address_to));
452 if (m_mail->full_address_cc) {
453 appendCcRecipients(EmailUtils::stripAddressLine(m_mail->full_address_cc));
456 if (m_mail->full_address_bcc) {
457 appendBccRecipients(EmailUtils::stripAddressLine(m_mail->full_address_bcc));
461 LoggerD(" " << m_mail->date_time);
462 tm* time = localtime(&m_mail->date_time);
464 LoggerE("localtime failed");
465 Throw(WrtDeviceApis::Commons::PlatformException);
470 if (m_mail->datetime[0] != '\0')
472 char buf[MAX_DATETIME_STRING_LENGTH];
473 char *targetBuf = m_mail->datetime;
476 memset(buf, 0x00, sizeof(buf));
477 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
478 timeinfo.tm_year = atoi(buf) - 1900;
480 memset(buf, 0x00, sizeof(buf));
481 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
482 timeinfo.tm_mon = atoi(buf) - 1;
484 memset(buf, 0x00, sizeof(buf));
485 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
486 timeinfo.tm_mday = atoi(buf);
488 memset(buf, 0x00, sizeof(buf));
489 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
490 timeinfo.tm_hour = atoi(buf);
492 memset(buf, 0x00, sizeof(buf));
493 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
494 timeinfo.tm_min = atoi(buf);
496 memset(buf, 0x00, sizeof(buf));
497 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
498 timeinfo.tm_sec = atoi(buf);
500 setDateTime(timeinfo);
508 struct tm tmpTime = *(localtime(&rawtime));
510 tmpTime.tm_year = m_mail->head->datetime.year;
511 tmpTime.tm_mon = m_mail->head->datetime.month;
512 tmpTime.tm_mday = m_mail->head->datetime.day;
513 tmpTime.tm_hour = m_mail->head->datetime.hour;
514 tmpTime.tm_min = m_mail->head->datetime.minute;
515 tmpTime.tm_sec = m_mail->head->datetime.second;
517 setDateTime(tmpTime);
520 if (m_mail->full_address_from) {
522 from.setRecipients(EmailUtils::stripAddress(m_mail->full_address_from));
523 setSourceAddress(from);
524 setSourceAddressValidity(true); //not needed to update in platform
525 setFrom(EmailUtils::stripAddress(m_mail->full_address_from));
526 setFromValidity(true);
532 void Email::readBody() {
536 LoggerE("Body is NULL");
537 Throw(WrtDeviceApis::Commons::PlatformException);
540 if (m_mail->file_path_plain) {
541 LoggerD("m_mail->file_path_plain : " << m_mail->file_path_plain);
542 DPL::ScopedFClose file(::fopen(m_mail->file_path_plain, "r"));
544 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
545 "Cannot read body file: " << m_mail->file_path_plain);
547 fseek(file.Get(), 0, SEEK_END);
548 long int size = ftell(file.Get());
551 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
552 "Cannot read plain body size");
555 fseek(file.Get(), 0, SEEK_SET);
556 DPL::ScopedPtr<char> data(new char[size + 1]);
557 memset(data.Get(), 0, size + 1);
558 TEMP_FAILURE_RETRY(fread(data.Get(), 1, size, file.Get()) &&
559 ferror(file.Get()) == 0 ? -1 : 0);
564 if (m_mail->file_path_html) {
565 LoggerD("m_mail->file_path_html : " << m_mail->file_path_html);
566 DPL::ScopedFClose file(::fopen(m_mail->file_path_html, "r")); //auto close, scopedFClose
568 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
569 "Cannot read html body file: " << m_mail->file_path_html);
572 fseek(file.Get(), 0, SEEK_END);
573 long int size = ftell(file.Get());
576 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
577 "Cannot read html body size");
580 fseek(file.Get(), 0, SEEK_SET);
581 DPL::ScopedPtr<char> data(new char[size + 1]);
582 memset(data.Get(), 0, size + 1);
583 TEMP_FAILURE_RETRY(fread(data.Get(), 1, size, file.Get()) &&
584 ferror(file.Get()) == 0 ? -1 : 0);
585 setHtmlBody(data.Get()); //setHtmlBody declarate in Email Calss.
589 //email_attachment_info_t* attachment = NULL;
590 email_attachment_data_t* attachment = NULL;
591 int attachmentCount = 0;
593 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
594 if (EMAIL_ERROR_NONE != error) {
595 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
598 if ( attachment && attachmentCount > 0)
600 LoggerD("reading attachments , attahcment count = " << attachmentCount);
603 for ( i = 0; i < attachmentCount; i++)
606 LoggerD("attachment ID : [" << attachment[i].attachment_id
607 << "] name : [" << attachment[i].attachment_name
608 << "] download : [" << attachment[i].save_status
609 << "] savefile : [" << attachment[i].attachment_path
610 << "] mimetype : [" << attachment[i].attachment_mime_type
612 LoggerD("attachment inline status : ["
613 << attachment[i].inline_content_status << "]");
614 IAttachmentPtr tmpAtt(new Attachment());
615 if (attachment[i].attachment_path)
616 tmpAtt->init(attachment[i].attachment_path, false);
620 LoggerD("attachment[i].inline_content_status : ["
621 << attachment[i].inline_content_status << "]");
622 tmpAtt->rename(std::string(attachment[i].attachment_name));
623 tmpAtt->setAttachmentID(attachment[i].attachment_id);
624 tmpAtt->setDownloaded(attachment[i].save_status);
625 tmpAtt->setIsInlineAttachment((bool)attachment[i].inline_content_status);
626 if(attachment[i].attachment_mime_type)
628 tmpAtt->setMimeType(attachment[i].attachment_mime_type);
631 LoggerD(" setNth(i+1) : " << i+1);
632 if(attachment[i].inline_content_status == 0)
634 appendAttachment(tmpAtt);
638 appendInlineAttachment(tmpAtt);
641 LoggerD(" append Attachment complete");
645 catch ( const WrtDeviceApis::Commons::Exception& ex )
647 LoggerE("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
656 error = email_free_attachment_data(&attachment, attachmentCount);
657 if (EMAIL_ERROR_NONE != error) {
658 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
665 void Email::readInfo()
671 LoggerD("m_mail is NULL");
672 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Info is NULL.");
675 setReadStatus(m_mail->flags_seen_field == 1);
677 m_accountId = m_mail->account_id;
678 setConvId(m_mail->thread_id);
679 setPriority( EmailConverter::toMessagePriority( m_mail->priority ) );
680 setSize(m_mail->mail_size);
681 setCurrentFolderId(m_mail->mailbox_id);
686 void Email::updateBody()
691 bool locationChecker = false;
693 if (isBodyValid()) { return; }
696 LoggerD("m_mail is NULL");
697 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
699 if (!m_mail->file_path_plain) {
700 LoggerD("file_path_plain is NULL");
701 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
705 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_plain);
706 if(locationChecker == true) // already exist palin body file on email frame work, we can not update that file so make new temp file
708 LoggerD("create new plain body");
709 char buf[] = "XXXXXX";
710 mode_t mask = umask(S_IWGRP | S_IWOTH);
711 error = mkstemp(buf);
713 std::string bodyFile = buf;
714 bodyFile = EmailUtils::TMP_DIR_PREFIX+bodyFile;
715 LoggerD("bodyFile : " << bodyFile);
717 FILE* f = fopen(bodyFile.c_str(), "w");
722 m_mail->file_path_plain = strdup(bodyFile.c_str());
725 FILE* f = fopen(m_mail->file_path_plain, "w");
727 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
732 LoggerD("Plain Body file open fail : " << m_mail->file_path_plain);
733 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
737 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_html);
738 if((locationChecker == true) || (!m_mail->file_path_html)) // html file is not exist or exist on email frame work then create tmp html file
740 LoggerD("create new html body");
741 char buf[] = "XXXXXX";
742 mode_t mask = umask(S_IWGRP | S_IWOTH);
743 error = mkstemp(buf);
745 std::string htmlFile = buf;
746 htmlFile = EmailUtils::TMP_DIR_PREFIX+htmlFile;
747 LoggerD("htmlFile : " << htmlFile);
749 FILE* f = fopen(htmlFile.c_str(), "w");
753 m_mail->file_path_html = strdup(htmlFile.c_str());
755 if(!m_mail->file_path_html)
757 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
761 f = fopen(m_mail->file_path_html, "w");
763 if(strlen(getHtmlBody().c_str()) > 0) // check html data is exist if not exist copy plain body to html body
765 fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
767 else if(strlen(getBodyRef().c_str()) > 0)
769 // htmlbody is empty and plainBody exist. copy plainBody to htmlBody
770 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
774 // htmlBody is empty.
780 LoggerD("html Body file open fail : " << m_mail->file_path_html);
781 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "html Body File Open Error");
784 m_mail->body_download_status = 1;
786 setBodyValidity(true);
792 void Email::updateSubject()
796 if (isSubjectValid()) { return; }
798 LoggerD("update subject, msgId=" << getIdRef());
801 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
804 m_mail->subject = String::strdup(getSubjectRef());
805 setSubjectValidity(true);
810 void Email::updateReadStatus()
814 if (isReadStatusValid()) { return; }
816 LoggerD("update read, msgId=" << getIdRef());
819 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
822 m_mail->flags_seen_field = isRead();
823 setReadStatusValidity(true);
828 void Email::updateIsRead()
831 if (isReadChangeStatusValid()) {
832 // do not update if not changed
836 EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
837 setisReadChangeStatusValidity(true);
841 void Email::updateMessage()
844 email_meeting_request_t *meeting_req = NULL;
846 DPL::Mutex::ScopedLock mx(&m_updateMutex);
849 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
852 LoggerD("getCurrentFolder() = " << getCurrentFolder() );
853 LoggerD("m_mail->mail_id = " << m_mail->mail_id );
854 LoggerD("m_mail->file_path_plain = " << m_mail->file_path_plain );
855 LoggerD("m_mail->file_path_html = " << m_mail->file_path_html );
857 if(getCurrentFolder() == DRAFTBOX)
861 // updateRecipients();
863 // updateAttachments();
869 email_attachment_data_t* attachment=NULL;
870 int attachmentCount = 0;
871 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
872 if (EMAIL_ERROR_NONE != error) {
873 LoggerW("email_get_attachment_data_list [" << error << "]");
876 LoggerW("meeting_request_status " << m_mail->meeting_request_status);
878 if( m_mail->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
879 || m_mail->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
880 || m_mail->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST)
882 error = email_get_meeting_request(m_mail->mail_id, &meeting_req);
883 if (EMAIL_ERROR_NONE != error) {
884 LoggerW("email_get_meeting_request() failed [" << error <<"]");
887 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, meeting_req, 0);
888 if (EMAIL_ERROR_NONE != error) {
889 LoggerW("Nothing to update or error. [" << error << "]");
892 if(getCurrentFolder() == DRAFTBOX)
895 // updateAttachments();
898 email_mail_data_t* result = NULL;
900 error = email_get_mail_data(m_mail->mail_id, &result);
901 if (EMAIL_ERROR_NONE != error) {
902 LoggerW("email_get_mail_data error. [" << error << "]");
906 if(result->body_download_status != 1)
908 LoggerD("result->body_download_status " << result->body_download_status);
909 int mail_id_array[1];
910 email_mail_attribute_type attribute_type;
911 email_mail_attribute_value_t attribute_value;
913 mail_id_array[0] = result->mail_id;
914 LoggerD("result->mail_id " << result->mail_id);
915 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
916 attribute_value.integer_type_value = 1;
918 error = email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
919 if (EMAIL_ERROR_NONE != error) {
920 LoggerW("email_update_mail_attribute error. [" << error << "]");
923 tm* time = localtime(&result->date_time);
925 LoggerE("localtime failed");
926 Throw(WrtDeviceApis::Commons::PlatformException);
932 error = email_free_mail_data(&result, 1);
933 if (EMAIL_ERROR_NONE != error) {
934 LoggerW("email_free_mail_data error. [" << error << "]");
941 email_free_meeting_request(&meeting_req, 1);
946 error = email_free_attachment_data(&attachment, attachmentCount);
947 if (EMAIL_ERROR_NONE != error) {
948 LoggerW("email_free_attachment_data [" << error << "]");
955 void Email::createSendMessage()
959 MailSender::getInstance(); //start email service
960 LoggerD("getUID: " << getUID());
961 if ( getUID() > 0 && m_accountId > 0)
967 DPL::Mutex::ScopedLock mx(&m_updateMutex);
969 EmailAccountInfo account = getEmailAccount();
970 m_accountId = account.getIntId(); //set account ID
971 LoggerD("account ID : " << m_accountId);
972 m_mail.Reset(EmailService::createMailData(account));
980 email_mailbox_t* mailbox;
981 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox );
982 if (EMAIL_ERROR_NONE != error) {
983 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
984 "Couldn't add message to mailbox. [" << error << "]");
986 if ( mailbox->mailbox_id )
987 m_mail->mailbox_id = mailbox->mailbox_id;
988 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
990 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
991 if (EMAIL_ERROR_NONE != error) {
992 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
993 "Couldn't add message to mailbox. [" << error << "]");
996 LoggerD("m_mail->mail_id =" << m_mail->mail_id);
998 setId(convertId(m_mail->mail_id));
999 setFolderType(OUTBOX);
1000 setMessageStatus(MESSAGE_STATUS_SENDING);
1002 updateAttachments(); //update Attachment.
1004 error = email_free_mailbox(&mailbox, 1);
1005 if (EMAIL_ERROR_NONE != error) {
1006 LoggerE("Failed to destroy mailbox: " << error);
1009 LoggerD("m_mail->from" << m_mail->full_address_from);
1011 email_mail_data_t* result = NULL;
1013 error = email_get_mail_data(m_mail->mail_id, &result);
1014 if (EMAIL_ERROR_NONE != error) {
1015 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1016 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1019 if (m_mail->file_path_plain)
1020 free(m_mail->file_path_plain);
1021 m_mail->file_path_plain = strdup(result->file_path_plain);
1023 if ( m_mail->file_path_html)
1025 free(m_mail->file_path_html);
1028 if(result->file_path_html)
1030 m_mail->file_path_html = strdup(result->file_path_html);
1033 if (result->full_address_from) {
1035 from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1036 setSourceAddress(from);
1037 setSourceAddressValidity(true); //not needed to update in platform
1039 setCurrentFolderId(result->mailbox_id);
1041 error = email_free_mail_data(&result, 1);
1042 if (EMAIL_ERROR_NONE != error) {
1043 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1044 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1052 void Email::addMessageToDraft()
1056 MailSender::getInstance(); //start email service
1058 LoggerD("getUID: " << getUID());
1059 if ( getUID() > 0 && m_accountId > 0)
1065 DPL::Mutex::ScopedLock mx(&m_updateMutex);
1066 EmailAccountInfo account = getEmailAccount();
1067 m_accountId = account.getIntId(); //set account ID
1068 LoggerD("account ID : " << m_accountId);
1069 m_mail.Reset(EmailService::createMailData(account));
1077 email_mailbox_t* mailbox;
1078 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
1079 if (EMAIL_ERROR_NONE != error) {
1080 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1081 "Couldn't add message to mailbox. [" << error << "]");
1083 if ( mailbox->mailbox_id )
1085 m_mail->mailbox_id = mailbox->mailbox_id;
1086 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
1089 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
1090 if (EMAIL_ERROR_NONE != error) {
1091 LoggerE("Couldn't add message to mailbox");
1092 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1093 "Couldn't add message to mailbox. [" << error << "]");
1096 LoggerD("message id =" << m_mail->mail_id);
1098 setId(convertId(m_mail->mail_id));
1099 setFolderType(DRAFTBOX);
1100 setMessageStatus(MESSAGE_STATUS_DRAFT);
1102 updateAttachments(); //update Attachment.
1104 error = email_free_mailbox(&mailbox, 1);
1105 if (EMAIL_ERROR_NONE != error) {
1106 LoggerE("Failed to destroy mailbox: " << error);
1115 void Email::loadDraftMessage()
1119 email_mail_data_t* result = NULL;
1121 int error = email_get_mail_data(m_mail->mail_id, &result);
1122 if (EMAIL_ERROR_NONE != error) {
1123 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1124 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1127 if(result->body_download_status != 1)
1129 LoggerD("result->body_download_status " << result->body_download_status);
1130 int mail_id_array[1];
1131 email_mail_attribute_type attribute_type;
1132 email_mail_attribute_value_t attribute_value;
1134 mail_id_array[0] = result->mail_id;
1135 LoggerD("result->mail_id " << result->mail_id);
1136 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
1137 attribute_value.integer_type_value = 1;
1139 email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
1142 tm* time = localtime(&result->date_time);
1144 LoggerE("localtime failed");
1145 Throw(WrtDeviceApis::Commons::PlatformException);
1149 setReadStatus(result->flags_seen_field == 1);
1151 m_accountId = result->account_id;
1152 setConvId(result->thread_id);
1153 setPriority( EmailConverter::toMessagePriority( result->priority ) );
1154 setSize(result->mail_size);
1155 setCurrentFolderId(result->mailbox_id);
1157 if (m_mail->file_path_plain)
1159 free(m_mail->file_path_plain);
1161 if(result->file_path_plain)
1163 m_mail->file_path_plain = strdup(result->file_path_plain);
1166 if ( m_mail->file_path_html)
1168 free(m_mail->file_path_html);
1171 if(result->file_path_html)
1173 m_mail->file_path_html = strdup(result->file_path_html);
1176 if (result->full_address_from) {
1178 from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1179 setSourceAddress(from);
1180 setSourceAddressValidity(true); //not needed to update in platform
1181 setFrom(result->full_address_from);
1183 m_mail->save_status = result->save_status;
1185 error = email_free_mail_data(&result, 1);
1186 if (EMAIL_ERROR_NONE != error) {
1187 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1188 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1194 void Email::updateRecipients()
1198 if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1201 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1204 if (!getToValidity()) {
1205 std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1206 if (m_mail->full_address_to) {
1207 free(m_mail->full_address_to);
1209 m_mail->full_address_to = String::strdup(addressLine);
1210 setToValidity(true);
1213 if (!getCcValidity()) {
1214 std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1215 if (m_mail->full_address_cc) {
1216 free(m_mail->full_address_cc);
1218 m_mail->full_address_cc = String::strdup(addressLine);
1219 setCcValidity(true);
1222 if (!getBccValidity()) {
1223 std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1224 if (m_mail->full_address_bcc) {
1225 free(m_mail->full_address_bcc);
1227 m_mail->full_address_bcc = String::strdup(addressLine);
1228 setBccValidity(true);
1234 void Email::updateFrom()
1238 if (getFromValidity()) { return; }
1241 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1244 m_mail->account_id = m_accountId;
1246 if (m_mail->full_address_from) {
1247 free(m_mail->full_address_from);
1249 m_mail->full_address_from = String::strdup(getFrom());
1251 setFromValidity(true);
1256 void Email::updateAttachments()
1260 if (isAttachmentsValid()) { return; }
1263 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1266 email_attachment_data_t* attachment = NULL;
1267 int attachmentCount = 0;
1268 email_mailbox_t* mailBox = NULL;
1273 LoggerD("update attachments, msgId=" << getIdRef());
1274 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1275 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
1276 if (EMAIL_ERROR_NONE != error) {
1277 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1279 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1281 if (EMAIL_ERROR_NONE != error) {
1282 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1283 "Couldn't get mailbox. [" << error << "]");
1286 if ( attachmentCount > 0 )
1288 LoggerD("attachmentCount=" << attachmentCount);
1290 std::stringstream stream;
1291 for ( i = 0; i < attachmentCount; i++)
1293 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1294 int attachmentId = attachment[i].attachment_id;
1296 int error = email_delete_attachment(attachmentId);
1297 if (EMAIL_ERROR_NONE != error) {
1298 LoggerD("Error Num = " << error);
1299 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1300 "Error while adding attachment. [" << error << "]");
1307 error = email_free_attachment_data(&attachment, attachmentCount);
1308 if (EMAIL_ERROR_NONE != error) {
1309 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1316 std::size_t attachmentSize = getAttachmentsCount();
1317 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1319 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1320 if (attachmentSize > 0)
1322 email_attachment_data_t* attachment_info = NULL;
1323 for (std::size_t i = 0; i < attachmentSize; ++i)
1325 IAttachmentPtr att = getAttachment(i);
1327 LoggerD("skip att" << i);
1331 //copy attachment file
1332 std::stringstream cp_cmd;
1333 char buf[] = "/tmp/XXXXXX";
1334 mode_t mask = umask(S_IWGRP | S_IWOTH);
1335 error = mkstemp(buf);
1338 cp_cmd << COMMAND_NAME;
1339 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1340 cp_cmd << " \"" << att->getFullPath() << "\"";
1341 cp_cmd << " \"" << buf << "\"";
1343 attachment_info = EmailService::alloc<email_attachment_data_t>();
1344 attachment_info->attachment_name = String::strdup(att->getShortName());
1345 attachment_info->attachment_path = String::strdup( buf );
1346 attachment_info->save_status = true;
1347 attachment_info->inline_content_status = 0;
1348 if(att->getMimeType().size() > 0)
1350 attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1353 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1354 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1355 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1356 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1357 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1359 LoggerD("Copy Command=" << cp_cmd.str());
1360 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1361 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1362 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1363 LoggerD("buf=" << buf);
1364 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1369 int result = system(cp_cmd.str().c_str());
1371 if (0 != WIFEXITED(result)) {
1372 if (0 != WEXITSTATUS(result)) {
1373 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1376 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1377 "Command terminated abnormally.");
1380 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1384 LoggerD("add Attachment");
1385 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1386 if (EMAIL_ERROR_NONE != error) {
1387 LoggerD("error :" << error);
1388 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1392 LoggerD(" attachment id : " << attachment_info->attachment_id);
1393 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1394 LoggerD(" setNth(i+1) : " << i+1);
1395 att->setDownloaded(true);
1396 att->setIsInlineAttachment(FALSE);
1397 att->setAttachmentID(attachment_info->attachment_id);
1398 att->setMessage(SharedFromThis());
1402 if (attachment_info)
1403 EmailService::freeAttachment(attachment_info);
1407 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1408 if (inlineAttachmentSize > 0)
1410 email_attachment_data_t* attachment_info = NULL;
1411 for (std::size_t i = 0; i < inlineAttachmentSize; ++i)
1413 IAttachmentPtr att = getInlineAttachment(i);
1418 //copy attachment file
1419 std::stringstream cp_cmd;
1420 char buf[] = "/tmp/XXXXXX";
1421 mode_t mask = umask(S_IWGRP | S_IWOTH);
1422 error = mkstemp(buf);
1425 cp_cmd << COMMAND_NAME;
1426 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1427 cp_cmd << " \"" << att->getFullPath() << "\"";
1428 cp_cmd << " \"" << buf << "\"";
1430 attachment_info = EmailService::alloc<email_attachment_data_t>();
1431 attachment_info->attachment_name = String::strdup(att->getShortName());
1432 attachment_info->attachment_path = String::strdup( buf );
1433 attachment_info->save_status = true;
1434 attachment_info->inline_content_status = 1;
1435 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1436 if(att->getMimeType().size() > 0)
1438 attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1441 LoggerD("Copy Command=" << cp_cmd.str());
1442 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1443 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1444 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1445 LoggerD("buf=" << buf);
1446 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1449 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1452 int result = system(cp_cmd.str().c_str());
1454 if (0 != WIFEXITED(result)) {
1455 if (0 != WEXITSTATUS(result)) {
1456 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1459 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1460 "Command terminated abnormally.");
1463 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1468 LoggerD("add Attachment");
1469 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1470 if (EMAIL_ERROR_NONE != error) {
1471 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1472 "Error while adding attachment. [" << error << "]");
1476 LoggerD(" attachment id : " << attachment_info->attachment_id);
1477 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1478 LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1479 att->setDownloaded(true);
1480 att->setIsInlineAttachment(TRUE);
1481 att->setAttachmentID(attachment_info->attachment_id);
1482 att->setMessage(SharedFromThis());
1483 // att->setNth(i+1);
1484 att->setNth(i + (int)attachmentSize + 1);
1487 if (attachment_info)
1488 EmailService::freeAttachment(attachment_info);
1493 Catch(WrtDeviceApis::Commons::PlatformException) {
1496 int error = email_free_attachment_data(&attachment, attachmentCount);
1497 if (EMAIL_ERROR_NONE != error) {
1498 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1499 "Error while adding attachment data [" << error << "]");
1502 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1503 "Error attachment Update");
1510 void Email::updatePriority()
1514 if (isPriorityValid()) { return; }
1517 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1520 m_mail->priority = EmailConverter::toMailPriority( getPriority());
1521 setPriorityValid(true);
1526 int Email::getIntId() const
1528 return convertId(getIdRef());