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/assert.h>
26 #include <dpl/scoped_fclose.h>
27 #include <dpl/scoped_ptr.h>
28 #include <Commons/Exception.h>
29 #include <Commons/StringUtils.h>
30 #include "MessageFactory.h"
31 #include "Messaging.h"
33 #include "EmailService.h"
34 #include "EmailUtils.h"
35 #include "EmailConverter.h"
36 #include "MailSender.h"
38 #include "MessagingService.h"
39 #include "MessagingServiceManager.h"
40 #include "Attachment.h"
43 #define LOG_ENTER LoggerD("---> ENTER");
44 #define LOG_EXIT LoggerD("---> EXIT");
47 using namespace WrtDeviceApis::Commons;
53 const char* COMMAND_NAME = "/bin/cp";
54 const char* COMMAND_SWITCH_RECURSIVE = "-r";
55 //const char* COMMAND_SWITCH_FORCE = "-f";
58 Email::Email(const string& id) :
63 if (getIdRef().empty()) {
65 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
69 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
70 LoggerE("Exception: " << ex.DumpToString());
76 Email::Email(const std::string& id, int accountId) :
82 if (getIdRef().empty()) {
84 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
87 LoggerD("Account ID = " << accountId);
88 m_accountId = accountId;
91 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
92 LoggerE("Exception: " << ex.DumpToString());
99 Email::Email(EmailAccountInfo& account) : IMessage(EMAIL)
104 if (getIdRef().empty()) {
110 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
111 LoggerE("Exception: " << ex.DumpToString());
117 Email::Email(const FolderType folder) : IMessage(EMAIL)
122 if (folder == TEMP_FOLDER) {
126 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Folder Type is miss-matched");
129 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
130 LoggerE("Exception: " << ex.DumpToString());
147 int handle = MailSender::getInstance().send(
148 MessageFactory::convertToEmail(SharedFromThis())
155 void Email::sendCancel(int handle)
159 MailSender::getInstance().cancel(handle);
164 int Email::downloadBody()
167 return MailSync::getInstance().downloadBody( MessageFactory::convertToEmail(SharedFromThis()) );
171 void Email::downloadBodyCancel( int handle)
173 return MailSync::getInstance().cancelDownloadBody(handle);
176 int Email::downloadAttachment( const IAttachmentPtr& attachment)
179 return MailSync::getInstance().downloadAttachment( MessageFactory::convertToEmail(SharedFromThis()), attachment );
183 void Email::downloadAttachmentCancel( int handle)
186 return MailSync::getInstance().cancelDownloadAttachment(handle);
190 void Email::update(bool draftsOnly)
194 DPL::Mutex::ScopedLock mx(&m_updateMutex);
197 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
206 email_attachment_data_t* attachment=NULL;
207 int attachmentCount = 0;
208 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
209 if (EMAIL_ERROR_NONE != error) {
210 LoggerW("Nothing to update or error. [" << error << "]");
213 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, NULL, 0);
214 if (EMAIL_ERROR_NONE != error) {
215 LoggerW("Nothing to update or error. [" << error << "]");
220 error = email_free_attachment_data(&attachment, attachmentCount);
221 if (EMAIL_ERROR_NONE != error) {
222 LoggerW("Nothing to update or error. [" << error << "]");
229 int Email::getAccountID()
234 return m_mail->account_id;
246 return m_mail->mail_id;
253 int Email::isBodyDownloaded()
257 if ( m_mail->body_download_status == 1 ) // 1 is fully downloaded body.
266 bool Email::hasAttachment()
268 std::size_t attachmentSize = getAttachmentsCount();
269 if ( attachmentSize > 0)
275 void Email::readAllData()
280 void Email::moveToFolder(const FolderType newFolder)
285 moveToFolder(EmailConverter::toMailboxType(newFolder));
290 void Email::moveToFolder(const string& newFolder)
293 Assert(m_mail && "mail is NULL.");
297 int accountId = m_mail->account_id;
298 int mailId = getIntId();
300 emf_mailbox_t* mailbox = EmailService::alloc<emf_mailbox_t>();
301 mailbox->account_id = accountId;
302 mailbox->name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
304 int error = email_move_mail_to_mailbox(&mailId, 1, mailbox);
305 if (EMF_ERROR_NONE != error) {
307 WrtDeviceApis::Commons::PlatformException,
308 "Couldn't move mail to folder: " << newFolder << ". [" <<
312 error = email_free_mailbox(&mailbox, 1); //free
313 if (EMF_ERROR_NONE != error) {
314 LoggerE("Failed to destroy mailbox: " << error);
319 void Email::copyToFolder(const FolderType newFolder)
323 copyToFolder(EmailConverter::toMailboxName(newFolder));
328 void Email::copyToFolder(const string& newFolder)
332 Assert(m_mail && "mail is NULL.");
336 int accountId = m_mail->account_id;
337 ScopedMail mail(EmailService::cloneMail(m_mail.Get())); //clone
338 mail->mailbox_name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
339 mail->account_id = accountId;
342 email_attachment_data_t* attachment = NULL;
343 int attachmentCount = 0;
344 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
345 if (EMAIL_ERROR_NONE != error) {
346 ThrowMsg( WrtDeviceApis::Commons::PlatformException,
347 "Couldn't get attachment list: " << newFolder << ". [" << error << "]");
350 int mailId = EmailService::addMail( mail.Get(), attachment );
352 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cloned mail didn't get new id.");
360 EmailService::deleteMail(m_accountId, getIntId());
368 EmailAccountInfo account = IMessaging::getInstance().getCurrentEmailAccount();
369 m_accountId = account.getIntId();
370 ScopedMail mail(EmailService::createMail(account));
371 ScopedMailbox mailbox(
372 EmailService::getMailboxByType(account.getIntId(),
373 EMAIL_MAILBOX_TYPE_DRAFT)
375 setId(convertId(EmailService::addMailToMailbox(mail.Get(), mailbox.Get())));
376 setFolderType(DRAFTBOX);
377 setMessageStatus(MESSAGE_STATUS_DRAFT);
386 setFolderType(TEMP_FOLDER);
387 setMessageStatus(MESSAGE_STATUS_CREATED);
391 void Email::create( EmailAccountInfo& account )
395 m_accountId = account.getIntId();
396 //MailSender::getInstance();
398 LoggerD("account ID : " << m_accountId);
399 ScopedMail mail(EmailService::createMailData(account));
401 setEmailAccount(account); //save account
403 mail->mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
405 setId(convertId(EmailService::addMail(mail.Get(), NULL)));
406 setFolderType(DRAFTBOX);
407 setMessageStatus(MESSAGE_STATUS_DRAFT);
416 LoggerD("mail ID :" << getIntId());
417 m_mail.Reset(EmailService::readMail(getIntId())); //reset Mail Data
423 //if (m_mail->head) { readHeader(); }
424 //if (m_mail->body) { readBody(); }
425 //if (m_mail->info) { readInfo(); }
427 //m_mailbox.Reset(EmailService::getMailboxByMailId(m_accountId, getIntId()) );
428 //setFolderType(EmailConverter::toFolderType(m_mailbox->mailbox_type));
430 setFolderType(EmailConverter::toFolderType(m_mail->mailbox_type));
431 setMessageStatus(EmailConverter::toMessageStatus(m_mail->mailbox_type));
436 void Email::readHeader()
440 Assert(m_mail && "Header is NULL.");
442 if (m_mail->subject) {
443 setSubject(m_mail->subject);
446 if (m_mail->full_address_to) {
447 appendToRecipients(EmailUtils::stripAddressLine(m_mail->full_address_to));
450 if (m_mail->full_address_cc) {
451 appendCcRecipients(EmailUtils::stripAddressLine(m_mail->full_address_cc));
454 if (m_mail->full_address_bcc) {
455 appendBccRecipients(EmailUtils::stripAddressLine(m_mail->full_address_bcc));
460 t = localtime(&m_mail->date_time);
462 memset(&timeinfo, 0, sizeof(timeinfo));
463 memcpy(t, &timeinfo, sizeof(struct tm));
464 setDateTime(timeinfo);
468 if (m_mail->datetime[0] != '\0')
470 char buf[MAX_DATETIME_STRING_LENGTH];
471 char *targetBuf = m_mail->datetime;
474 memset(buf, 0x00, sizeof(buf));
475 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
476 timeinfo.tm_year = atoi(buf) - 1900;
478 memset(buf, 0x00, sizeof(buf));
479 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
480 timeinfo.tm_mon = atoi(buf) - 1;
482 memset(buf, 0x00, sizeof(buf));
483 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
484 timeinfo.tm_mday = atoi(buf);
486 memset(buf, 0x00, sizeof(buf));
487 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
488 timeinfo.tm_hour = atoi(buf);
490 memset(buf, 0x00, sizeof(buf));
491 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
492 timeinfo.tm_min = atoi(buf);
494 memset(buf, 0x00, sizeof(buf));
495 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
496 timeinfo.tm_sec = atoi(buf);
498 setDateTime(timeinfo);
506 struct tm tmpTime = *(localtime(&rawtime));
508 tmpTime.tm_year = m_mail->head->datetime.year;
509 tmpTime.tm_mon = m_mail->head->datetime.month;
510 tmpTime.tm_mday = m_mail->head->datetime.day;
511 tmpTime.tm_hour = m_mail->head->datetime.hour;
512 tmpTime.tm_min = m_mail->head->datetime.minute;
513 tmpTime.tm_sec = m_mail->head->datetime.second;
515 setDateTime(tmpTime);
518 if (m_mail->full_address_from) {
520 from.setRecipients(m_mail->full_address_from);
521 setSourceAddress(from);
522 setSourceAddressValidity(true); //not needed to update in platform
528 void Email::readBody() {
531 Assert(m_mail && "Body is NULL.");
533 if (m_mail->file_path_plain) {
534 LoggerD("m_mail->file_path_plain : " << m_mail->file_path_plain);
535 DPL::ScopedFClose file(::fopen(m_mail->file_path_plain, "r"));
537 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
538 "Cannot read body file: " << m_mail->file_path_plain);
540 fseek(file.Get(), 0, SEEK_END);
541 long int size = ftell(file.Get());
544 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
545 "Cannot read plain body size");
548 fseek(file.Get(), 0, SEEK_SET);
549 DPL::ScopedPtr<char> data(new char[size + 1]);
550 memset(data.Get(), 0, size + 1);
551 TEMP_FAILURE_RETRY(fread(data.Get(), 1, size, file.Get()) &&
552 ferror(file.Get()) == 0 ? -1 : 0);
557 if (m_mail->file_path_html) {
558 LoggerD("m_mail->file_path_html : " << m_mail->file_path_html);
559 DPL::ScopedFClose file(::fopen(m_mail->file_path_html, "r")); //auto close, scopedFClose
561 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
562 "Cannot read html body file: " << m_mail->file_path_html);
565 fseek(file.Get(), 0, SEEK_END);
566 long int size = ftell(file.Get());
569 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
570 "Cannot read html body size");
573 fseek(file.Get(), 0, SEEK_SET);
574 DPL::ScopedPtr<char> data(new char[size + 1]);
575 memset(data.Get(), 0, size + 1);
576 TEMP_FAILURE_RETRY(fread(data.Get(), 1, size, file.Get()) &&
577 ferror(file.Get()) == 0 ? -1 : 0);
578 setHtmlBody(data.Get()); //setHtmlBody declarate in Email Calss.
582 //email_attachment_info_t* attachment = NULL;
583 email_attachment_data_t* attachment = NULL;
584 int attachmentCount = 0;
586 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
587 if (EMAIL_ERROR_NONE != error) {
588 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
591 if ( attachment && attachmentCount > 0)
593 LoggerD("reading attachments , attahcment count = " << attachmentCount);
596 for ( i = 0; i < attachmentCount; i++)
599 LoggerD("attachment ID :" << attachment[i].attachment_id << " name :" << attachment[i].attachment_name << " download :" << attachment[i].save_status << "savefile :" << attachment[i].attachment_path);
600 LoggerD("attachment inline status :" << attachment[i].inline_content_status);
601 IAttachmentPtr tmpAtt(new Attachment());
602 if (attachment[i].attachment_path)
603 tmpAtt->init(attachment[i].attachment_path, false);
607 LoggerD("attachment[i].inline_content_status : " << attachment[i].inline_content_status);
608 tmpAtt->rename(std::string(attachment[i].attachment_name));
609 tmpAtt->setAttachmentID(attachment[i].attachment_id);
610 tmpAtt->setDownloaded(attachment[i].save_status);
611 tmpAtt->setIsInlineAttachment((bool)attachment[i].inline_content_status);
613 LoggerD(" setNth(i+1) : " << i+1);
614 if(attachment[i].inline_content_status == 0)
616 appendAttachment(tmpAtt);
620 appendInlineAttachment(tmpAtt);
623 LoggerD(" append Attachment complete");
627 catch ( const WrtDeviceApis::Commons::Exception& ex )
629 LoggerE("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
638 error = email_free_attachment_data(&attachment, attachmentCount);
639 if (EMAIL_ERROR_NONE != error) {
640 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
647 void Email::readInfo()
653 LoggerD("m_mail is NULL");
654 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Info is NULL.");
657 setReadStatus(m_mail->flags_seen_field == 1);
659 m_accountId = m_mail->account_id;
660 setConvId(m_mail->thread_id);
661 setPriority( EmailConverter::toMessagePriority( m_mail->priority ) );
662 setSize(m_mail->mail_size);
667 void Email::updateBody()
672 bool locationChecker = false;
674 if (isBodyValid()) { return; }
677 LoggerD("m_mail is NULL");
678 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
680 if (!m_mail->file_path_plain) {
681 LoggerD("file_path_plain is NULL");
682 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
686 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_plain);
687 if(locationChecker == true) // already exist palin body file on email frame work, we can not update that file so make new temp file
689 LoggerD("create new plain body");
690 char buf[] = "XXXXXX";
691 mode_t mask = umask(S_IWGRP | S_IWOTH);
692 error = mkstemp(buf);
694 std::string bodyFile = buf;
695 bodyFile = EmailUtils::TMP_DIR_PREFIX+bodyFile;
696 LoggerD("bodyFile : " << bodyFile);
698 FILE* f = fopen(bodyFile.c_str(), "w");
703 m_mail->file_path_plain = strdup(bodyFile.c_str());
706 FILE* f = fopen(m_mail->file_path_plain, "w");
708 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
713 LoggerD("Plain Body file open fail : " << m_mail->file_path_plain);
714 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
719 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_html);
720 if((locationChecker == true) || (!m_mail->file_path_html)) // html file is not exist or exist on email frame work then create tmp html file
722 LoggerD("create new html body");
723 char buf[] = "XXXXXX";
724 mode_t mask = umask(S_IWGRP | S_IWOTH);
725 error = mkstemp(buf);
727 std::string htmlFile = buf;
728 htmlFile = EmailUtils::TMP_DIR_PREFIX+htmlFile;
729 LoggerD("htmlFile : " << htmlFile);
731 FILE* f = fopen(htmlFile.c_str(), "w");
735 m_mail->file_path_html = strdup(htmlFile.c_str());
737 if(!m_mail->file_path_html)
739 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
743 f = fopen(m_mail->file_path_html, "w");
745 if(strlen(getHtmlBody().c_str()) > 0) // check html data is exist if not exist copy plain body to html body
747 fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
751 // htmlBody is empty.
757 LoggerD("html Body file open fail : " << m_mail->file_path_html);
758 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "html Body File Open Error");
761 m_mail->body_download_status = 1;
763 setBodyValidity(true);
769 void Email::updateSubject()
773 if (isSubjectValid()) { return; }
775 LoggerD("update subject, msgId=" << getIdRef());
778 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
781 m_mail->subject = String::strdup(getSubjectRef());
782 setSubjectValidity(true);
787 void Email::updateReadStatus()
791 if (isReadStatusValid()) { return; }
793 LoggerD("update read, msgId=" << getIdRef());
796 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
799 m_mail->flags_seen_field = isRead();
800 setReadStatusValidity(true);
805 void Email::updateIsRead()
808 if (isReadChangeStatusValid()) {
809 // do not update if not changed
813 EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
814 setisReadChangeStatusValidity(true);
818 void Email::updateMessage()
821 email_meeting_request_t *meeting_req = NULL;
823 DPL::Mutex::ScopedLock mx(&m_updateMutex);
826 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
829 LoggerD("getCurrentFolder() = " << getCurrentFolder() );
830 LoggerD("m_mail->mail_id = " << m_mail->mail_id );
831 LoggerD("m_mail->file_path_plain = " << m_mail->file_path_plain );
832 LoggerD("m_mail->file_path_html = " << m_mail->file_path_html );
834 if(getCurrentFolder() == DRAFTBOX)
846 email_attachment_data_t* attachment=NULL;
847 int attachmentCount = 0;
848 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
849 if (EMAIL_ERROR_NONE != error) {
850 LoggerW("email_get_attachment_data_list [" << error << "]");
853 error = email_get_meeting_request(m_mail->mail_id, &meeting_req);
854 if (EMAIL_ERROR_NONE != error) {
855 LoggerW("email_get_meeting_request() failed [%d]\n" << error);
858 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, meeting_req, 0);
859 if (EMAIL_ERROR_NONE != error) {
860 LoggerW("Nothing to update or error. [" << error << "]");
864 email_free_meeting_request(&meeting_req, 1);
869 error = email_free_attachment_data(&attachment, attachmentCount);
870 if (EMAIL_ERROR_NONE != error) {
871 LoggerW("email_free_attachment_data [" << error << "]");
878 void Email::createSendMessage()
882 MailSender::getInstance(); //start email service
883 LoggerD("getUID: " << getUID());
884 if ( getUID() > 0 && m_accountId > 0)
890 DPL::Mutex::ScopedLock mx(&m_updateMutex);
892 EmailAccountInfo account = getEmailAccount();
893 m_accountId = account.getIntId(); //set account ID
894 LoggerD("account ID : " << m_accountId);
895 m_mail.Reset(EmailService::createMailData(account));
903 email_mailbox_t* mailbox;
904 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox );
905 if (EMAIL_ERROR_NONE != error) {
906 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
907 "Couldn't add message to mailbox. [" << error << "]");
909 if ( mailbox->mailbox_id )
910 m_mail->mailbox_id = mailbox->mailbox_id;
911 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
913 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
914 if (EMAIL_ERROR_NONE != error) {
915 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
916 "Couldn't add message to mailbox. [" << error << "]");
919 LoggerD("m_mail->mail_id =" << m_mail->mail_id);
921 setId(convertId(m_mail->mail_id));
922 setFolderType(OUTBOX);
923 setMessageStatus(MESSAGE_STATUS_SENDING);
925 updateAttachments(); //update Attachment.
927 error = email_free_mailbox(&mailbox, 1);
928 if (EMAIL_ERROR_NONE != error) {
929 LoggerE("Failed to destroy mailbox: " << error);
932 LoggerD("m_mail->from" << m_mail->full_address_from);
934 email_mail_data_t* result = NULL;
936 error = email_get_mail_data(m_mail->mail_id, &result);
937 if (EMAIL_ERROR_NONE != error) {
938 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
939 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
943 if (m_mail->file_path_plain)
944 free(m_mail->file_path_plain);
945 m_mail->file_path_plain = strdup(result->file_path_plain);
947 if ( m_mail->file_path_html)
949 free(m_mail->file_path_html);
952 if(result->file_path_html)
954 m_mail->file_path_html = strdup(result->file_path_html);
957 error = email_free_mail_data(&result, 1);
958 if (EMAIL_ERROR_NONE != error) {
959 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
960 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
968 void Email::addMessageToDraft()
972 MailSender::getInstance(); //start email service
974 LoggerD("getUID: " << getUID());
975 if ( getUID() > 0 && m_accountId > 0)
981 DPL::Mutex::ScopedLock mx(&m_updateMutex);
983 EmailAccountInfo account = getEmailAccount();
984 m_accountId = account.getIntId(); //set account ID
985 LoggerD("account ID : " << m_accountId);
986 m_mail.Reset(EmailService::createMailData(account));
994 email_mailbox_t* mailbox;
995 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
996 if (EMAIL_ERROR_NONE != error) {
997 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
998 "Couldn't add message to mailbox. [" << error << "]");
1000 if ( mailbox->mailbox_id )
1001 m_mail->mailbox_id = mailbox->mailbox_id;
1002 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
1004 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
1005 if (EMAIL_ERROR_NONE != error) {
1006 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1007 "Couldn't add message to mailbox. [" << error << "]");
1010 LoggerD("message id =" << m_mail->mail_id);
1012 setId(convertId(m_mail->mail_id));
1013 setFolderType(DRAFTBOX);
1014 setMessageStatus(MESSAGE_STATUS_DRAFT);
1016 updateAttachments(); //update Attachment.
1018 error = email_free_mailbox(&mailbox, 1);
1019 if (EMAIL_ERROR_NONE != error) {
1020 LoggerE("Failed to destroy mailbox: " << error);
1023 LoggerD("m_mail->from" << m_mail->full_address_from);
1025 email_mail_data_t* result = NULL;
1027 error = email_get_mail_data(m_mail->mail_id, &result);
1028 if (EMAIL_ERROR_NONE != error) {
1029 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1030 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1033 if(result->body_download_status != 1)
1035 LoggerD("result->body_download_status " << result->body_download_status);
1036 int mail_id_count = 1;
1037 int mail_id_array[1];
1038 email_mail_attribute_type attribute_type;
1039 email_mail_attribute_value_t attribute_value;
1041 mail_id_array[0] = m_mail->mail_id;
1042 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
1043 attribute_value.integer_type_value = 1;
1045 email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
1051 if (m_mail->file_path_plain)
1052 free(m_mail->file_path_plain);
1053 m_mail->file_path_plain = strdup(result->file_path_plain);
1055 if ( m_mail->file_path_html)
1057 free(m_mail->file_path_html);
1060 if(result->file_path_html)
1062 m_mail->file_path_html = strdup(result->file_path_html);
1065 error = email_free_mail_data(&result, 1);
1066 if (EMAIL_ERROR_NONE != error) {
1067 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1068 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1076 void Email::updateRecipients()
1080 if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1083 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1086 if (!getToValidity()) {
1087 std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1088 if (m_mail->full_address_to) {
1089 free(m_mail->full_address_to);
1091 m_mail->full_address_to = String::strdup(addressLine);
1092 setToValidity(true);
1095 if (!getCcValidity()) {
1096 std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1097 if (m_mail->full_address_cc) {
1098 free(m_mail->full_address_cc);
1100 m_mail->full_address_cc = String::strdup(addressLine);
1101 setCcValidity(true);
1104 if (!getBccValidity()) {
1105 std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1106 if (m_mail->full_address_bcc) {
1107 free(m_mail->full_address_bcc);
1109 m_mail->full_address_bcc = String::strdup(addressLine);
1110 setBccValidity(true);
1116 void Email::updateFrom()
1120 if (getFromValidity()) { return; }
1123 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1126 m_mail->account_id = m_accountId;
1128 if (m_mail->full_address_from) {
1129 free(m_mail->full_address_from);
1131 m_mail->full_address_from = String::strdup(getFrom());
1133 setFromValidity(true);
1138 void Email::updateAttachments()
1142 if (isAttachmentsValid()) { return; }
1145 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1148 email_attachment_data_t* attachment = NULL;
1149 int attachmentCount = 0;
1150 email_mailbox_t* mailBox = NULL;
1155 LoggerD("update attachments, msgId=" << getIdRef());
1156 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1157 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
1158 if (EMAIL_ERROR_NONE != error) {
1159 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1161 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1163 if (EMAIL_ERROR_NONE != error) {
1164 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1165 "Couldn't get mailbox. [" << error << "]");
1168 if ( attachmentCount > 0 )
1170 LoggerD("attachmentCount=" << attachmentCount);
1172 std::stringstream stream;
1173 for ( i = 0; i < attachmentCount; i++)
1175 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1176 int attachmentId = attachment[i].attachment_id;
1178 int error = email_delete_attachment(attachmentId);
1179 if (EMAIL_ERROR_NONE != error) {
1180 LoggerD("Error Num = " << error);
1181 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1182 "Error while adding attachment. [" << error << "]");
1189 error = email_free_attachment_data(&attachment, attachmentCount);
1190 if (EMAIL_ERROR_NONE != error) {
1191 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1198 std::size_t attachmentSize = getAttachmentsCount();
1199 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1201 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1202 if (attachmentSize > 0)
1204 email_attachment_data_t* attachment_info = NULL;
1205 for (std::size_t i = 0; i < attachmentSize; ++i)
1207 IAttachmentPtr att = getAttachment(i);
1209 LoggerD("skip att" << i);
1213 //copy attachment file
1214 std::stringstream cp_cmd;
1215 char buf[] = "/tmp/XXXXXX";
1216 mode_t mask = umask(S_IWGRP | S_IWOTH);
1217 error = mkstemp(buf);
1220 cp_cmd << COMMAND_NAME;
1221 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1222 cp_cmd << " \"" << att->getFullPath() << "\"";
1223 cp_cmd << " \"" << buf << "\"";
1225 attachment_info = EmailService::alloc<email_attachment_data_t>();
1226 attachment_info->attachment_name = String::strdup(att->getShortName());
1227 attachment_info->attachment_path = String::strdup( buf );
1228 attachment_info->save_status = true;
1229 attachment_info->inline_content_status = 0;
1231 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1232 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1233 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1234 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1236 LoggerD("Copy Command=" << cp_cmd.str());
1237 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1238 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1239 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1240 LoggerD("buf=" << buf);
1241 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1246 int result = system(cp_cmd.str().c_str());
1248 if (0 != WIFEXITED(result)) {
1249 if (0 != WEXITSTATUS(result)) {
1250 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1253 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1254 "Command terminated abnormally.");
1257 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1261 LoggerD("add Attachment");
1262 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1263 if (EMAIL_ERROR_NONE != error) {
1264 LoggerD("error :" << error);
1265 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1269 LoggerD(" attachment id : " << attachment_info->attachment_id);
1270 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1271 LoggerD(" setNth(i+1) : " << i+1);
1272 att->setDownloaded(true);
1273 att->setIsInlineAttachment(FALSE);
1274 att->setAttachmentID(attachment_info->attachment_id);
1275 att->setMessage(SharedFromThis());
1279 if (attachment_info)
1280 EmailService::freeAttachment(attachment_info);
1284 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1285 if (inlineAttachmentSize > 0)
1287 email_attachment_data_t* attachment_info = NULL;
1288 for (std::size_t i = 0; i < inlineAttachmentSize; ++i)
1290 IAttachmentPtr att = getInlineAttachment(i);
1295 //copy attachment file
1296 std::stringstream cp_cmd;
1297 char buf[] = "/tmp/XXXXXX";
1298 mode_t mask = umask(S_IWGRP | S_IWOTH);
1299 error = mkstemp(buf);
1302 cp_cmd << COMMAND_NAME;
1303 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1304 cp_cmd << " \"" << att->getFullPath() << "\"";
1305 cp_cmd << " \"" << buf << "\"";
1307 attachment_info = EmailService::alloc<email_attachment_data_t>();
1308 attachment_info->attachment_name = String::strdup(att->getShortName());
1309 attachment_info->attachment_path = String::strdup( buf );
1310 attachment_info->save_status = true;
1311 attachment_info->inline_content_status = 1;
1312 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1314 LoggerD("Copy Command=" << cp_cmd.str());
1315 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1316 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1317 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1318 LoggerD("buf=" << buf);
1319 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1324 int result = system(cp_cmd.str().c_str());
1326 if (0 != WIFEXITED(result)) {
1327 if (0 != WEXITSTATUS(result)) {
1328 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1331 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1332 "Command terminated abnormally.");
1335 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1340 LoggerD("add Attachment");
1341 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1342 if (EMAIL_ERROR_NONE != error) {
1343 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1344 "Error while adding attachment. [" << error << "]");
1348 LoggerD(" attachment id : " << attachment_info->attachment_id);
1349 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1350 LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1351 att->setDownloaded(true);
1352 att->setIsInlineAttachment(TRUE);
1353 att->setAttachmentID(attachment_info->attachment_id);
1354 att->setMessage(SharedFromThis());
1355 // att->setNth(i+1);
1356 att->setNth(i + (int)attachmentSize + 1);
1359 if (attachment_info)
1360 EmailService::freeAttachment(attachment_info);
1368 Catch(WrtDeviceApis::Commons::PlatformException) {
1371 int error = email_free_attachment_data(&attachment, attachmentCount);
1372 if (EMAIL_ERROR_NONE != error) {
1373 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1374 "Error while adding attachment data [" << error << "]");
1377 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1378 "Error attachment Update");
1385 void Email::updatePriority()
1389 if (isPriorityValid()) { return; }
1392 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1395 m_mail->priority = EmailConverter::toMailPriority( getPriority());
1396 setPriorityValid(true);
1401 int Email::getIntId() const
1403 return convertId(getIdRef());