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;
90 setMessageStatus(MESSAGE_STATUS_LOADED);
92 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
93 LoggerE("Exception: " << ex.DumpToString());
100 Email::Email(EmailAccountInfo& account) : IMessage(EMAIL)
105 if (getIdRef().empty()) {
111 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
112 LoggerE("Exception: " << ex.DumpToString());
118 Email::Email(const FolderType folder) : IMessage(EMAIL)
123 if (folder == TEMP_FOLDER) {
127 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Folder Type is miss-matched");
130 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
131 LoggerE("Exception: " << ex.DumpToString());
148 int handle = MailSender::getInstance().send(
149 MessageFactory::convertToEmail(SharedFromThis())
156 void Email::sendCancel(int handle)
160 MailSender::getInstance().cancel(handle);
165 int Email::downloadBody()
168 return MailSync::getInstance().downloadBody( MessageFactory::convertToEmail(SharedFromThis()) );
172 void Email::downloadBodyCancel( int handle)
174 return MailSync::getInstance().cancelDownloadBody(handle);
177 int Email::downloadAttachment( const IAttachmentPtr& attachment)
180 return MailSync::getInstance().downloadAttachment( MessageFactory::convertToEmail(SharedFromThis()), attachment );
184 void Email::downloadAttachmentCancel( int handle)
187 return MailSync::getInstance().cancelDownloadAttachment(handle);
191 void Email::update(bool draftsOnly)
195 DPL::Mutex::ScopedLock mx(&m_updateMutex);
198 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
207 email_attachment_data_t* attachment=NULL;
208 int attachmentCount = 0;
209 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
210 if (EMAIL_ERROR_NONE != error) {
211 LoggerW("Nothing to update or error. [" << error << "]");
214 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, NULL, 0);
215 if (EMAIL_ERROR_NONE != error) {
216 LoggerW("Nothing to update or error. [" << error << "]");
221 error = email_free_attachment_data(&attachment, attachmentCount);
222 if (EMAIL_ERROR_NONE != error) {
223 LoggerW("Nothing to update or error. [" << error << "]");
230 int Email::getAccountID()
235 return m_mail->account_id;
247 return m_mail->mail_id;
254 int Email::isBodyDownloaded()
258 if ( m_mail->body_download_status == 1 ) // 1 is fully downloaded body.
267 bool Email::hasAttachment()
269 std::size_t attachmentSize = getAttachmentsCount();
270 if ( attachmentSize > 0)
276 void Email::readAllData()
281 void Email::moveToFolder(const FolderType newFolder)
286 moveToFolder(EmailConverter::toMailboxType(newFolder));
291 void Email::moveToFolder(const string& newFolder)
294 Assert(m_mail && "mail is NULL.");
298 int accountId = m_mail->account_id;
299 int mailId = getIntId();
301 emf_mailbox_t* mailbox = EmailService::alloc<emf_mailbox_t>();
302 mailbox->account_id = accountId;
303 mailbox->name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
305 int error = email_move_mail_to_mailbox(&mailId, 1, mailbox);
306 if (EMF_ERROR_NONE != error) {
308 WrtDeviceApis::Commons::PlatformException,
309 "Couldn't move mail to folder: " << newFolder << ". [" <<
313 error = email_free_mailbox(&mailbox, 1); //free
314 if (EMF_ERROR_NONE != error) {
315 LoggerE("Failed to destroy mailbox: " << error);
320 void Email::copyToFolder(const FolderType newFolder)
324 copyToFolder(EmailConverter::toMailboxName(newFolder));
329 void Email::copyToFolder(const string& newFolder)
333 Assert(m_mail && "mail is NULL.");
337 int accountId = m_mail->account_id;
338 ScopedMail mail(EmailService::cloneMail(m_mail.Get())); //clone
339 mail->mailbox_name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
340 mail->account_id = accountId;
343 email_attachment_data_t* attachment = NULL;
344 int attachmentCount = 0;
345 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
346 if (EMAIL_ERROR_NONE != error) {
347 ThrowMsg( WrtDeviceApis::Commons::PlatformException,
348 "Couldn't get attachment list: " << newFolder << ". [" << error << "]");
351 int mailId = EmailService::addMail( mail.Get(), attachment );
353 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cloned mail didn't get new id.");
361 EmailService::deleteMail(m_accountId, getIntId());
369 EmailAccountInfo account = IMessaging::getInstance().getCurrentEmailAccount();
370 m_accountId = account.getIntId();
371 ScopedMail mail(EmailService::createMail(account));
372 ScopedMailbox mailbox(
373 EmailService::getMailboxByType(account.getIntId(),
374 EMAIL_MAILBOX_TYPE_DRAFT)
376 setId(convertId(EmailService::addMailToMailbox(mail.Get(), mailbox.Get())));
377 setFolderType(DRAFTBOX);
378 setMessageStatus(MESSAGE_STATUS_DRAFT);
387 setFolderType(TEMP_FOLDER);
388 setMessageStatus(MESSAGE_STATUS_CREATED);
392 void Email::create( EmailAccountInfo& account )
396 m_accountId = account.getIntId();
397 //MailSender::getInstance();
399 LoggerD("account ID : " << m_accountId);
400 ScopedMail mail(EmailService::createMailData(account));
402 setEmailAccount(account); //save account
404 mail->mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
406 setId(convertId(EmailService::addMail(mail.Get(), NULL)));
407 setFolderType(DRAFTBOX);
408 setMessageStatus(MESSAGE_STATUS_DRAFT);
417 LoggerD("mail ID :" << getIntId());
418 m_mail.Reset(EmailService::readMail(getIntId())); //reset Mail Data
424 //if (m_mail->head) { readHeader(); }
425 //if (m_mail->body) { readBody(); }
426 //if (m_mail->info) { readInfo(); }
428 //m_mailbox.Reset(EmailService::getMailboxByMailId(m_accountId, getIntId()) );
429 //setFolderType(EmailConverter::toFolderType(m_mailbox->mailbox_type));
431 setFolderType(EmailConverter::toFolderType(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 = true;
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 (m_mail->file_path_plain)
1034 free(m_mail->file_path_plain);
1035 m_mail->file_path_plain = strdup(result->file_path_plain);
1037 if ( m_mail->file_path_html)
1039 free(m_mail->file_path_html);
1042 if(result->file_path_html)
1044 m_mail->file_path_html = strdup(result->file_path_html);
1047 error = email_free_mail_data(&result, 1);
1048 if (EMAIL_ERROR_NONE != error) {
1049 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1050 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1058 void Email::updateRecipients()
1062 if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1065 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1068 if (!getToValidity()) {
1069 std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1070 if (m_mail->full_address_to) {
1071 free(m_mail->full_address_to);
1073 m_mail->full_address_to = String::strdup(addressLine);
1074 setToValidity(true);
1077 if (!getCcValidity()) {
1078 std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1079 if (m_mail->full_address_cc) {
1080 free(m_mail->full_address_cc);
1082 m_mail->full_address_cc = String::strdup(addressLine);
1083 setCcValidity(true);
1086 if (!getBccValidity()) {
1087 std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1088 if (m_mail->full_address_bcc) {
1089 free(m_mail->full_address_bcc);
1091 m_mail->full_address_bcc = String::strdup(addressLine);
1092 setBccValidity(true);
1098 void Email::updateFrom()
1102 if (getFromValidity()) { return; }
1105 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1108 m_mail->account_id = m_accountId;
1110 if (m_mail->full_address_from) {
1111 free(m_mail->full_address_from);
1113 m_mail->full_address_from = String::strdup(getFrom());
1115 setFromValidity(true);
1120 void Email::updateAttachments()
1124 if (isAttachmentsValid()) { return; }
1127 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1130 email_attachment_data_t* attachment = NULL;
1131 int attachmentCount = 0;
1132 email_mailbox_t* mailBox = NULL;
1137 LoggerD("update attachments, msgId=" << getIdRef());
1138 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1139 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
1140 if (EMAIL_ERROR_NONE != error) {
1141 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1143 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1145 if (EMAIL_ERROR_NONE != error) {
1146 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1147 "Couldn't get mailbox. [" << error << "]");
1150 if ( attachmentCount > 0 )
1152 LoggerD("attachmentCount=" << attachmentCount);
1154 std::stringstream stream;
1155 for ( i = 0; i < attachmentCount; i++)
1157 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1158 int attachmentId = attachment[i].attachment_id;
1160 int error = email_delete_attachment(attachmentId);
1161 if (EMAIL_ERROR_NONE != error) {
1162 LoggerD("Error Num = " << error);
1163 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1164 "Error while adding attachment. [" << error << "]");
1171 error = email_free_attachment_data(&attachment, attachmentCount);
1172 if (EMAIL_ERROR_NONE != error) {
1173 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1180 std::size_t attachmentSize = getAttachmentsCount();
1181 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1183 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1184 if (attachmentSize > 0)
1186 email_attachment_data_t* attachment_info = NULL;
1187 for (std::size_t i = 0; i < attachmentSize; ++i)
1189 IAttachmentPtr att = getAttachment(i);
1191 LoggerD("skip att" << i);
1195 //copy attachment file
1196 std::stringstream cp_cmd;
1197 char buf[] = "/tmp/XXXXXX";
1198 mode_t mask = umask(S_IWGRP | S_IWOTH);
1199 error = mkstemp(buf);
1202 cp_cmd << COMMAND_NAME;
1203 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1204 cp_cmd << " \"" << att->getFullPath() << "\"";
1205 cp_cmd << " \"" << buf << "\"";
1207 attachment_info = EmailService::alloc<email_attachment_data_t>();
1208 attachment_info->attachment_name = String::strdup(att->getShortName());
1209 attachment_info->attachment_path = String::strdup( buf );
1210 attachment_info->save_status = true;
1211 attachment_info->inline_content_status = 0;
1213 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1214 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1215 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1216 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1218 LoggerD("Copy Command=" << cp_cmd.str());
1219 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1220 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1221 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1222 LoggerD("buf=" << buf);
1223 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1228 int result = system(cp_cmd.str().c_str());
1230 if (0 != WIFEXITED(result)) {
1231 if (0 != WEXITSTATUS(result)) {
1232 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1235 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1236 "Command terminated abnormally.");
1239 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1243 LoggerD("add Attachment");
1244 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1245 if (EMAIL_ERROR_NONE != error) {
1246 LoggerD("error :" << error);
1247 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1251 LoggerD(" attachment id : " << attachment_info->attachment_id);
1252 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1253 LoggerD(" setNth(i+1) : " << i+1);
1254 att->setDownloaded(true);
1255 att->setIsInlineAttachment(FALSE);
1256 att->setAttachmentID(attachment_info->attachment_id);
1257 att->setMessage(SharedFromThis());
1261 if (attachment_info)
1262 EmailService::freeAttachment(attachment_info);
1266 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1267 if (inlineAttachmentSize > 0)
1269 email_attachment_data_t* attachment_info = NULL;
1270 for (std::size_t i = 0; i < inlineAttachmentSize; ++i)
1272 IAttachmentPtr att = getInlineAttachment(i);
1277 //copy attachment file
1278 std::stringstream cp_cmd;
1279 char buf[] = "/tmp/XXXXXX";
1280 mode_t mask = umask(S_IWGRP | S_IWOTH);
1281 error = mkstemp(buf);
1284 cp_cmd << COMMAND_NAME;
1285 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1286 cp_cmd << " \"" << att->getFullPath() << "\"";
1287 cp_cmd << " \"" << buf << "\"";
1289 attachment_info = EmailService::alloc<email_attachment_data_t>();
1290 attachment_info->attachment_name = String::strdup(att->getShortName());
1291 attachment_info->attachment_path = String::strdup( buf );
1292 attachment_info->save_status = true;
1293 attachment_info->inline_content_status = 1;
1294 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1296 LoggerD("Copy Command=" << cp_cmd.str());
1297 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1298 LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1299 if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1300 LoggerD("buf=" << buf);
1301 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1306 int result = system(cp_cmd.str().c_str());
1308 if (0 != WIFEXITED(result)) {
1309 if (0 != WEXITSTATUS(result)) {
1310 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1313 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1314 "Command terminated abnormally.");
1317 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1322 LoggerD("add Attachment");
1323 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1324 if (EMAIL_ERROR_NONE != error) {
1325 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1326 "Error while adding attachment. [" << error << "]");
1330 LoggerD(" attachment id : " << attachment_info->attachment_id);
1331 LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1332 LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1333 att->setDownloaded(true);
1334 att->setIsInlineAttachment(TRUE);
1335 att->setAttachmentID(attachment_info->attachment_id);
1336 att->setMessage(SharedFromThis());
1337 // att->setNth(i+1);
1338 att->setNth(i + (int)attachmentSize + 1);
1341 if (attachment_info)
1342 EmailService::freeAttachment(attachment_info);
1350 Catch(WrtDeviceApis::Commons::PlatformException) {
1353 int error = email_free_attachment_data(&attachment, attachmentCount);
1354 if (EMAIL_ERROR_NONE != error) {
1355 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1356 "Error while adding attachment data [" << error << "]");
1359 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1360 "Error attachment Update");
1367 void Email::updatePriority()
1371 if (isPriorityValid()) { return; }
1374 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1377 m_mail->priority = EmailConverter::toMailPriority( getPriority());
1378 setPriorityValid(true);
1383 int Email::getIntId() const
1385 return convertId(getIdRef());