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));
459 LoggerD(" " << m_mail->date_time);
460 tm* time = localtime(&m_mail->date_time);
462 LoggerE("localtime failed");
463 Throw(WrtDeviceApis::Commons::PlatformException);
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(EmailUtils::stripAddress(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);
612 if(attachment[i].attachment_mime_type)
614 tmpAtt->setMimeType(attachment[i].attachment_mime_type);
617 LoggerD(" setNth(i+1) : " << i+1);
618 if(attachment[i].inline_content_status == 0)
620 appendAttachment(tmpAtt);
624 appendInlineAttachment(tmpAtt);
627 LoggerD(" append Attachment complete");
631 catch ( const WrtDeviceApis::Commons::Exception& ex )
633 LoggerE("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
642 error = email_free_attachment_data(&attachment, attachmentCount);
643 if (EMAIL_ERROR_NONE != error) {
644 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
651 void Email::readInfo()
657 LoggerD("m_mail is NULL");
658 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Info is NULL.");
661 setReadStatus(m_mail->flags_seen_field == 1);
663 m_accountId = m_mail->account_id;
664 setConvId(m_mail->thread_id);
665 setPriority( EmailConverter::toMessagePriority( m_mail->priority ) );
666 setSize(m_mail->mail_size);
667 setCurrentFolderId(m_mail->mailbox_id);
672 void Email::updateBody()
677 bool locationChecker = false;
679 if (isBodyValid()) { return; }
682 LoggerD("m_mail is NULL");
683 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
685 if (!m_mail->file_path_plain) {
686 LoggerD("file_path_plain is NULL");
687 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
691 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_plain);
692 if(locationChecker == true) // already exist palin body file on email frame work, we can not update that file so make new temp file
694 LoggerD("create new plain body");
695 char buf[] = "XXXXXX";
696 mode_t mask = umask(S_IWGRP | S_IWOTH);
697 error = mkstemp(buf);
699 std::string bodyFile = buf;
700 bodyFile = EmailUtils::TMP_DIR_PREFIX+bodyFile;
701 LoggerD("bodyFile : " << bodyFile);
703 FILE* f = fopen(bodyFile.c_str(), "w");
708 m_mail->file_path_plain = strdup(bodyFile.c_str());
711 FILE* f = fopen(m_mail->file_path_plain, "w");
713 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
718 LoggerD("Plain Body file open fail : " << m_mail->file_path_plain);
719 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
723 locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_html);
724 if((locationChecker == true) || (!m_mail->file_path_html)) // html file is not exist or exist on email frame work then create tmp html file
726 LoggerD("create new html body");
727 char buf[] = "XXXXXX";
728 mode_t mask = umask(S_IWGRP | S_IWOTH);
729 error = mkstemp(buf);
731 std::string htmlFile = buf;
732 htmlFile = EmailUtils::TMP_DIR_PREFIX+htmlFile;
733 LoggerD("htmlFile : " << htmlFile);
735 FILE* f = fopen(htmlFile.c_str(), "w");
739 m_mail->file_path_html = strdup(htmlFile.c_str());
741 if(!m_mail->file_path_html)
743 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
747 f = fopen(m_mail->file_path_html, "w");
749 if(strlen(getHtmlBody().c_str()) > 0) // check html data is exist if not exist copy plain body to html body
751 fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
755 // htmlBody is empty.
761 LoggerD("html Body file open fail : " << m_mail->file_path_html);
762 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "html Body File Open Error");
765 m_mail->body_download_status = 1;
767 setBodyValidity(true);
773 void Email::updateSubject()
777 if (isSubjectValid()) { return; }
779 LoggerD("update subject, msgId=" << getIdRef());
782 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
785 m_mail->subject = String::strdup(getSubjectRef());
786 setSubjectValidity(true);
791 void Email::updateReadStatus()
795 if (isReadStatusValid()) { return; }
797 LoggerD("update read, msgId=" << getIdRef());
800 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
803 m_mail->flags_seen_field = isRead();
804 setReadStatusValidity(true);
809 void Email::updateIsRead()
812 if (isReadChangeStatusValid()) {
813 // do not update if not changed
817 EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
818 setisReadChangeStatusValidity(true);
822 void Email::updateMessage()
825 email_meeting_request_t *meeting_req = NULL;
827 DPL::Mutex::ScopedLock mx(&m_updateMutex);
830 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
833 LoggerD("getCurrentFolder() = " << getCurrentFolder() );
834 LoggerD("m_mail->mail_id = " << m_mail->mail_id );
835 LoggerD("m_mail->file_path_plain = " << m_mail->file_path_plain );
836 LoggerD("m_mail->file_path_html = " << m_mail->file_path_html );
838 if(getCurrentFolder() == DRAFTBOX)
844 // updateAttachments();
850 email_attachment_data_t* attachment=NULL;
851 int attachmentCount = 0;
852 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
853 if (EMAIL_ERROR_NONE != error) {
854 LoggerW("email_get_attachment_data_list [" << error << "]");
857 error = email_get_meeting_request(m_mail->mail_id, &meeting_req);
858 if (EMAIL_ERROR_NONE != error) {
859 LoggerW("email_get_meeting_request() failed [%d]\n" << error);
862 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, meeting_req, 0);
863 if (EMAIL_ERROR_NONE != error) {
864 LoggerW("Nothing to update or error. [" << error << "]");
867 if(getCurrentFolder() == DRAFTBOX)
872 email_mail_data_t* result = NULL;
874 error = email_get_mail_data(m_mail->mail_id, &result);
875 if (EMAIL_ERROR_NONE != error) {
876 LoggerW("email_get_mail_data error. [" << error << "]");
880 if(result->body_download_status != 1)
882 LoggerD("result->body_download_status " << result->body_download_status);
883 int mail_id_count = 1;
884 int mail_id_array[1];
885 email_mail_attribute_type attribute_type;
886 email_mail_attribute_value_t attribute_value;
888 mail_id_array[0] = result->mail_id;
889 LoggerD("result->mail_id " << result->mail_id);
890 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
891 attribute_value.integer_type_value = 1;
893 error = email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
894 if (EMAIL_ERROR_NONE != error) {
895 LoggerW("email_update_mail_attribute error. [" << error << "]");
901 error = email_free_mail_data(&result, 1);
902 if (EMAIL_ERROR_NONE != error) {
903 LoggerW("email_free_mail_data error. [" << error << "]");
909 email_free_meeting_request(&meeting_req, 1);
914 error = email_free_attachment_data(&attachment, attachmentCount);
915 if (EMAIL_ERROR_NONE != error) {
916 LoggerW("email_free_attachment_data [" << error << "]");
923 void Email::createSendMessage()
927 MailSender::getInstance(); //start email service
928 LoggerD("getUID: " << getUID());
929 if ( getUID() > 0 && m_accountId > 0)
935 DPL::Mutex::ScopedLock mx(&m_updateMutex);
937 EmailAccountInfo account = getEmailAccount();
938 m_accountId = account.getIntId(); //set account ID
939 LoggerD("account ID : " << m_accountId);
940 m_mail.Reset(EmailService::createMailData(account));
948 email_mailbox_t* mailbox;
949 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox );
950 if (EMAIL_ERROR_NONE != error) {
951 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
952 "Couldn't add message to mailbox. [" << error << "]");
954 if ( mailbox->mailbox_id )
955 m_mail->mailbox_id = mailbox->mailbox_id;
956 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
958 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
959 if (EMAIL_ERROR_NONE != error) {
960 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
961 "Couldn't add message to mailbox. [" << error << "]");
964 LoggerD("m_mail->mail_id =" << m_mail->mail_id);
966 setId(convertId(m_mail->mail_id));
967 setFolderType(OUTBOX);
968 setMessageStatus(MESSAGE_STATUS_SENDING);
970 updateAttachments(); //update Attachment.
972 error = email_free_mailbox(&mailbox, 1);
973 if (EMAIL_ERROR_NONE != error) {
974 LoggerE("Failed to destroy mailbox: " << error);
977 LoggerD("m_mail->from" << m_mail->full_address_from);
979 email_mail_data_t* result = NULL;
981 error = email_get_mail_data(m_mail->mail_id, &result);
982 if (EMAIL_ERROR_NONE != error) {
983 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
984 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
987 if (m_mail->file_path_plain)
988 free(m_mail->file_path_plain);
989 m_mail->file_path_plain = strdup(result->file_path_plain);
991 if ( m_mail->file_path_html)
993 free(m_mail->file_path_html);
996 if(result->file_path_html)
998 m_mail->file_path_html = strdup(result->file_path_html);
1001 if (result->full_address_from) {
1003 from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1004 setSourceAddress(from);
1005 setSourceAddressValidity(true); //not needed to update in platform
1007 setCurrentFolderId(result->mailbox_id);
1009 error = email_free_mail_data(&result, 1);
1010 if (EMAIL_ERROR_NONE != error) {
1011 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1012 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1020 void Email::addMessageToDraft()
1024 MailSender::getInstance(); //start email service
1026 LoggerD("getUID: " << getUID());
1027 if ( getUID() > 0 && m_accountId > 0)
1033 DPL::Mutex::ScopedLock mx(&m_updateMutex);
1034 EmailAccountInfo account = getEmailAccount();
1035 m_accountId = account.getIntId(); //set account ID
1036 LoggerD("account ID : " << m_accountId);
1037 m_mail.Reset(EmailService::createMailData(account));
1045 email_mailbox_t* mailbox;
1046 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
1047 if (EMAIL_ERROR_NONE != error) {
1048 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1049 "Couldn't add message to mailbox. [" << error << "]");
1051 if ( mailbox->mailbox_id )
1053 m_mail->mailbox_id = mailbox->mailbox_id;
1054 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
1057 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
1058 if (EMAIL_ERROR_NONE != error) {
1059 LoggerE("Couldn't add message to mailbox");
1060 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1061 "Couldn't add message to mailbox. [" << error << "]");
1064 LoggerD("message id =" << m_mail->mail_id);
1066 setId(convertId(m_mail->mail_id));
1067 setFolderType(DRAFTBOX);
1068 setMessageStatus(MESSAGE_STATUS_DRAFT);
1070 updateAttachments(); //update Attachment.
1072 error = email_free_mailbox(&mailbox, 1);
1073 if (EMAIL_ERROR_NONE != error) {
1074 LoggerE("Failed to destroy mailbox: " << error);
1083 void Email::loadDraftMessage()
1087 email_mail_data_t* result = NULL;
1089 int error = email_get_mail_data(m_mail->mail_id, &result);
1090 if (EMAIL_ERROR_NONE != error) {
1091 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1092 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1095 if(result->body_download_status != 1)
1097 LoggerD("result->body_download_status " << result->body_download_status);
1098 int mail_id_count = 1;
1099 int mail_id_array[1];
1100 email_mail_attribute_type attribute_type;
1101 email_mail_attribute_value_t attribute_value;
1103 mail_id_array[0] = result->mail_id;
1104 LoggerD("result->mail_id " << result->mail_id);
1105 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
1106 attribute_value.integer_type_value = 1;
1108 email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
1111 tm* time = localtime(&result->date_time);
1113 LoggerE("localtime failed");
1114 Throw(WrtDeviceApis::Commons::PlatformException);
1118 setReadStatus(result->flags_seen_field == 1);
1120 m_accountId = result->account_id;
1121 setConvId(result->thread_id);
1122 setPriority( EmailConverter::toMessagePriority( result->priority ) );
1123 setSize(result->mail_size);
1124 setCurrentFolderId(result->mailbox_id);
1126 if (m_mail->file_path_plain)
1128 free(m_mail->file_path_plain);
1130 if(result->file_path_plain)
1132 m_mail->file_path_plain = strdup(result->file_path_plain);
1135 if ( m_mail->file_path_html)
1137 free(m_mail->file_path_html);
1140 if(result->file_path_html)
1142 m_mail->file_path_html = strdup(result->file_path_html);
1145 if (result->full_address_from) {
1147 from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1148 setSourceAddress(from);
1149 setSourceAddressValidity(true); //not needed to update in platform
1150 setFrom(result->full_address_from);
1152 m_mail->save_status = result->save_status;
1154 error = email_free_mail_data(&result, 1);
1155 if (EMAIL_ERROR_NONE != error) {
1156 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1157 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1163 void Email::updateRecipients()
1167 if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1170 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1173 if (!getToValidity()) {
1174 std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1175 if (m_mail->full_address_to) {
1176 free(m_mail->full_address_to);
1178 m_mail->full_address_to = String::strdup(addressLine);
1179 setToValidity(true);
1182 if (!getCcValidity()) {
1183 std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1184 if (m_mail->full_address_cc) {
1185 free(m_mail->full_address_cc);
1187 m_mail->full_address_cc = String::strdup(addressLine);
1188 setCcValidity(true);
1191 if (!getBccValidity()) {
1192 std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1193 if (m_mail->full_address_bcc) {
1194 free(m_mail->full_address_bcc);
1196 m_mail->full_address_bcc = String::strdup(addressLine);
1197 setBccValidity(true);
1203 void Email::updateFrom()
1207 if (getFromValidity()) { return; }
1210 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1213 m_mail->account_id = m_accountId;
1215 if (m_mail->full_address_from) {
1216 free(m_mail->full_address_from);
1218 m_mail->full_address_from = String::strdup(getFrom());
1220 setFromValidity(true);
1225 void Email::updateAttachments()
1229 if (isAttachmentsValid()) { return; }
1232 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1235 email_attachment_data_t* attachment = NULL;
1236 int attachmentCount = 0;
1237 email_mailbox_t* mailBox = NULL;
1242 LoggerD("update attachments, msgId=" << getIdRef());
1243 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1244 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
1245 if (EMAIL_ERROR_NONE != error) {
1246 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1248 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1250 if (EMAIL_ERROR_NONE != error) {
1251 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1252 "Couldn't get mailbox. [" << error << "]");
1255 if ( attachmentCount > 0 )
1257 LoggerD("attachmentCount=" << attachmentCount);
1259 std::stringstream stream;
1260 for ( i = 0; i < attachmentCount; i++)
1262 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1263 int attachmentId = attachment[i].attachment_id;
1265 int error = email_delete_attachment(attachmentId);
1266 if (EMAIL_ERROR_NONE != error) {
1267 LoggerD("Error Num = " << error);
1268 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1269 "Error while adding attachment. [" << error << "]");
1276 error = email_free_attachment_data(&attachment, attachmentCount);
1277 if (EMAIL_ERROR_NONE != error) {
1278 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1285 std::size_t attachmentSize = getAttachmentsCount();
1286 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1288 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1289 if (attachmentSize > 0)
1291 email_attachment_data_t* attachment_info = NULL;
1292 for (std::size_t i = 0; i < attachmentSize; ++i)
1294 IAttachmentPtr att = getAttachment(i);
1296 LoggerD("skip att" << i);
1300 //copy attachment file
1301 std::stringstream cp_cmd;
1302 char buf[] = "/tmp/XXXXXX";
1303 mode_t mask = umask(S_IWGRP | S_IWOTH);
1304 error = mkstemp(buf);
1307 cp_cmd << COMMAND_NAME;
1308 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1309 cp_cmd << " \"" << att->getFullPath() << "\"";
1310 cp_cmd << " \"" << buf << "\"";
1312 attachment_info = EmailService::alloc<email_attachment_data_t>();
1313 attachment_info->attachment_name = String::strdup(att->getShortName());
1314 attachment_info->attachment_path = String::strdup( buf );
1315 attachment_info->save_status = true;
1316 attachment_info->inline_content_status = 0;
1317 if(att->getMimeType().size() > 0)
1319 attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1322 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1323 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1324 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1325 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1326 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1328 LoggerD("Copy Command=" << cp_cmd.str());
1329 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1330 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1331 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1332 LoggerD("buf=" << buf);
1333 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1338 int result = system(cp_cmd.str().c_str());
1340 if (0 != WIFEXITED(result)) {
1341 if (0 != WEXITSTATUS(result)) {
1342 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1345 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1346 "Command terminated abnormally.");
1349 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1353 LoggerD("add Attachment");
1354 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1355 if (EMAIL_ERROR_NONE != error) {
1356 LoggerD("error :" << error);
1357 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1361 LoggerD(" attachment id : " << attachment_info->attachment_id);
1362 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1363 LoggerD(" setNth(i+1) : " << i+1);
1364 att->setDownloaded(true);
1365 att->setIsInlineAttachment(FALSE);
1366 att->setAttachmentID(attachment_info->attachment_id);
1367 att->setMessage(SharedFromThis());
1371 if (attachment_info)
1372 EmailService::freeAttachment(attachment_info);
1376 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1377 if (inlineAttachmentSize > 0)
1379 email_attachment_data_t* attachment_info = NULL;
1380 for (std::size_t i = 0; i < inlineAttachmentSize; ++i)
1382 IAttachmentPtr att = getInlineAttachment(i);
1387 //copy attachment file
1388 std::stringstream cp_cmd;
1389 char buf[] = "/tmp/XXXXXX";
1390 mode_t mask = umask(S_IWGRP | S_IWOTH);
1391 error = mkstemp(buf);
1394 cp_cmd << COMMAND_NAME;
1395 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1396 cp_cmd << " \"" << att->getFullPath() << "\"";
1397 cp_cmd << " \"" << buf << "\"";
1399 attachment_info = EmailService::alloc<email_attachment_data_t>();
1400 attachment_info->attachment_name = String::strdup(att->getShortName());
1401 attachment_info->attachment_path = String::strdup( buf );
1402 attachment_info->save_status = true;
1403 attachment_info->inline_content_status = 1;
1404 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1405 if(att->getMimeType().size() > 0)
1407 attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1410 LoggerD("Copy Command=" << cp_cmd.str());
1411 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1412 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1413 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1414 LoggerD("buf=" << buf);
1415 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1418 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1421 int result = system(cp_cmd.str().c_str());
1423 if (0 != WIFEXITED(result)) {
1424 if (0 != WEXITSTATUS(result)) {
1425 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1428 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1429 "Command terminated abnormally.");
1432 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1437 LoggerD("add Attachment");
1438 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1439 if (EMAIL_ERROR_NONE != error) {
1440 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1441 "Error while adding attachment. [" << error << "]");
1445 LoggerD(" attachment id : " << attachment_info->attachment_id);
1446 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1447 LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1448 att->setDownloaded(true);
1449 att->setIsInlineAttachment(TRUE);
1450 att->setAttachmentID(attachment_info->attachment_id);
1451 att->setMessage(SharedFromThis());
1452 // att->setNth(i+1);
1453 att->setNth(i + (int)attachmentSize + 1);
1456 if (attachment_info)
1457 EmailService::freeAttachment(attachment_info);
1462 Catch(WrtDeviceApis::Commons::PlatformException) {
1465 int error = email_free_attachment_data(&attachment, attachmentCount);
1466 if (EMAIL_ERROR_NONE != error) {
1467 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1468 "Error while adding attachment data [" << error << "]");
1471 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1472 "Error attachment Update");
1479 void Email::updatePriority()
1483 if (isPriorityValid()) { return; }
1486 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1489 m_mail->priority = EmailConverter::toMailPriority( getPriority());
1490 setPriorityValid(true);
1495 int Email::getIntId() const
1497 return convertId(getIdRef());