2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 * @author Pawel Misiak (p.misiak@samsung.com)
30 #include <email-types.h>
31 #include <email-api.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
34 #include <dpl/scoped_fclose.h>
35 #include <dpl/scoped_ptr.h>
36 #include <Commons/Exception.h>
37 #include <Commons/StringUtils.h>
38 #include <API/Messaging/MessageFactory.h>
39 #include "Messaging.h"
41 #include "EmailService.h"
42 #include "EmailUtils.h"
43 #include "EmailConverter.h"
44 #include "MailSender.h"
46 #include "MessagingService.h"
47 #include "MessagingServiceManager.h"
48 #include "Attachment.h"
50 #define LOG_ENTER LogDebug("---> ENTER");
51 #define LOG_EXIT LogDebug("---> EXIT");
54 using namespace TizenApis::Api::Messaging;
55 using namespace WrtDeviceApis::Commons;
62 const char* COMMAND_NAME = "/bin/cp";
63 const char* COMMAND_SWITCH_RECURSIVE = "-r";
64 //const char* COMMAND_SWITCH_FORCE = "-f";
67 Email::Email(const string& id) :
72 if (getIdRef().empty()) {
74 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
78 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
79 LogError("Exception: " << ex.DumpToString());
85 Email::Email(const std::string& id, int accountId) :
91 if (getIdRef().empty()) {
93 ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
96 LogDebug("Account ID = " << accountId);
97 m_accountId = accountId;
100 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
101 LogError("Exception: " << ex.DumpToString());
108 Email::Email(Api::Messaging::EmailAccountInfo& account) : IMessage(EMAIL)
113 if (getIdRef().empty()) {
119 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
120 LogError("Exception: " << ex.DumpToString());
126 Email::Email(const Api::Messaging::FolderType folder) : IMessage(EMAIL)
131 if (folder == Api::Messaging::TEMP_FOLDER) {
135 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Folder Type is miss-matched");
138 catch (const WrtDeviceApis::Commons::PlatformException& ex) {
139 LogError("Exception: " << ex.DumpToString());
156 int handle = MailSender::getInstance().send(
157 Api::Messaging::MessageFactory::convertToEmail(SharedFromThis())
164 void Email::sendCancel(int handle)
168 MailSender::getInstance().cancel(handle);
173 int Email::downloadBody()
176 return MailSync::getInstance().downloadBody( Api::Messaging::MessageFactory::convertToEmail(SharedFromThis()) );
180 void Email::downloadBodyCancel( int handle)
182 return MailSync::getInstance().cancelDownloadBody(handle);
185 int Email::downloadAttachment( const Api::Messaging::IAttachmentPtr& attachment)
188 return MailSync::getInstance().downloadAttachment( Api::Messaging::MessageFactory::convertToEmail(SharedFromThis()), attachment );
192 void Email::downloadAttachmentCancel( int handle)
195 return MailSync::getInstance().cancelDownloadAttachment(handle);
199 void Email::update(bool draftsOnly)
203 DPL::Mutex::ScopedLock mx(&m_updateMutex);
206 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
215 email_attachment_data_t* attachment=NULL;
216 int attachmentCount = 0;
217 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
218 if (EMAIL_ERROR_NONE != error) {
219 LogWarning("Nothing to update or error. [" << error << "]");
222 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, NULL, 0);
223 if (EMAIL_ERROR_NONE != error) {
224 LogWarning("Nothing to update or error. [" << error << "]");
229 error = email_free_attachment_data(&attachment, attachmentCount);
230 if (EMAIL_ERROR_NONE != error) {
231 LogWarning("Nothing to update or error. [" << error << "]");
235 LogDebug("getCurrentFolder() = " << getCurrentFolder() );
236 if (!draftsOnly || (getCurrentFolder() == Api::Messaging::DRAFTBOX)) {
245 LogWarning("Updating only read status. Folder: " << getCurrentFolder());
250 email_attachment_data_t* attachment=NULL;
251 int attachmentCount = 0;
252 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
253 if (EMAIL_ERROR_NONE != error) {
254 LogWarning("Nothing to update or error. [" << error << "]");
257 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, NULL, 0);
258 if (EMAIL_ERROR_NONE != error) {
259 LogWarning("Nothing to update or error. [" << error << "]");
266 int Email::getAccountID()
271 return m_mail->account_id;
283 return m_mail->mail_id;
290 int Email::isBodyDownloaded()
294 if ( m_mail->body_download_status == 1 ) // 1 is fully downloaded body.
303 bool Email::hasAttachment()
305 std::size_t attachmentSize = getAttachmentsCount();
306 if ( attachmentSize > 0)
312 void Email::readAllData()
317 void Email::moveToFolder(const FolderType newFolder)
322 moveToFolder(EmailConverter::toMailboxType(newFolder));
327 void Email::moveToFolder(const string& newFolder)
330 Assert(m_mail && "mail is NULL.");
334 int accountId = m_mail->account_id;
335 int mailId = getIntId();
337 emf_mailbox_t* mailbox = EmailService::alloc<emf_mailbox_t>();
338 mailbox->account_id = accountId;
339 mailbox->name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
341 int error = email_move_mail_to_mailbox(&mailId, 1, mailbox);
342 if (EMF_ERROR_NONE != error) {
344 WrtDeviceApis::Commons::PlatformException,
345 "Couldn't move mail to folder: " << newFolder << ". [" <<
349 error = email_free_mailbox(&mailbox, 1); //free
350 if (EMF_ERROR_NONE != error) {
351 LogError("Failed to destroy mailbox: " << error);
356 void Email::copyToFolder(const FolderType newFolder)
360 copyToFolder(EmailConverter::toMailboxName(newFolder));
365 void Email::copyToFolder(const string& newFolder)
369 Assert(m_mail && "mail is NULL.");
373 int accountId = m_mail->account_id;
374 ScopedMail mail(EmailService::cloneMail(m_mail.Get())); //clone
375 mail->mailbox_name = (NULL != newFolder.c_str() ? strdup(newFolder.c_str()) : NULL);
376 mail->account_id = accountId;
379 email_attachment_data_t* attachment = NULL;
380 int attachmentCount = 0;
381 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
382 if (EMAIL_ERROR_NONE != error) {
383 ThrowMsg( WrtDeviceApis::Commons::PlatformException,
384 "Couldn't get attachment list: " << newFolder << ". [" << error << "]");
387 int mailId = EmailService::addMail( mail.Get(), attachment );
389 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cloned mail didn't get new id.");
397 EmailService::deleteMail(m_accountId, getIntId());
405 EmailAccountInfo account = IMessaging::getInstance().getCurrentEmailAccount();
406 m_accountId = account.getIntId();
407 ScopedMail mail(EmailService::createMail(account));
408 ScopedMailbox mailbox(
409 EmailService::getMailboxByType(account.getIntId(),
410 EMAIL_MAILBOX_TYPE_DRAFT)
412 setId(convertId(EmailService::addMailToMailbox(mail.Get(), mailbox.Get())));
413 setFolderType(Api::Messaging::DRAFTBOX);
414 setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
423 setFolderType(Api::Messaging::TEMP_FOLDER);
424 setMessageStatus(Api::Messaging::MESSAGE_STATUS_CREATED);
428 void Email::create( Api::Messaging::EmailAccountInfo& account )
432 m_accountId = account.getIntId();
433 //MailSender::getInstance();
435 LogDebug("account ID : " << m_accountId);
436 ScopedMail mail(EmailService::createMailData(account));
438 setEmailAccount(account); //save account
440 mail->mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
442 setId(convertId(EmailService::addMail(mail.Get(), NULL)));
443 setFolderType(Api::Messaging::DRAFTBOX);
444 setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
453 LogDebug("mail ID :" << getIntId());
454 m_mail.Reset(EmailService::readMail(getIntId())); //reset Mail Data
460 //if (m_mail->head) { readHeader(); }
461 //if (m_mail->body) { readBody(); }
462 //if (m_mail->info) { readInfo(); }
464 //m_mailbox.Reset(EmailService::getMailboxByMailId(m_accountId, getIntId()) );
465 //setFolderType(EmailConverter::toFolderType(m_mailbox->mailbox_type));
467 setFolderType(EmailConverter::toFolderType(m_mail->mailbox_type));
472 void Email::readHeader()
476 Assert(m_mail && "Header is NULL.");
478 if (m_mail->subject) {
479 setSubject(m_mail->subject);
482 if (m_mail->full_address_to) {
483 appendToRecipients(EmailUtils::stripAddressLine(m_mail->full_address_to));
486 if (m_mail->full_address_cc) {
487 appendCcRecipients(EmailUtils::stripAddressLine(m_mail->full_address_cc));
490 if (m_mail->full_address_bcc) {
491 appendBccRecipients(EmailUtils::stripAddressLine(m_mail->full_address_bcc));
496 t = localtime(&m_mail->date_time);
498 memcpy(t, &timeinfo, sizeof(struct tm));
499 setDateTime(timeinfo);
503 if (m_mail->datetime[0] != '\0')
505 char buf[MAX_DATETIME_STRING_LENGTH];
506 char *targetBuf = m_mail->datetime;
509 memset(buf, 0x00, sizeof(buf));
510 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
511 timeinfo.tm_year = atoi(buf) - 1900;
513 memset(buf, 0x00, sizeof(buf));
514 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
515 timeinfo.tm_mon = atoi(buf) - 1;
517 memset(buf, 0x00, sizeof(buf));
518 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
519 timeinfo.tm_mday = atoi(buf);
521 memset(buf, 0x00, sizeof(buf));
522 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
523 timeinfo.tm_hour = atoi(buf);
525 memset(buf, 0x00, sizeof(buf));
526 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
527 timeinfo.tm_min = atoi(buf);
529 memset(buf, 0x00, sizeof(buf));
530 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
531 timeinfo.tm_sec = atoi(buf);
533 setDateTime(timeinfo);
541 struct tm tmpTime = *(localtime(&rawtime));
543 tmpTime.tm_year = m_mail->head->datetime.year;
544 tmpTime.tm_mon = m_mail->head->datetime.month;
545 tmpTime.tm_mday = m_mail->head->datetime.day;
546 tmpTime.tm_hour = m_mail->head->datetime.hour;
547 tmpTime.tm_min = m_mail->head->datetime.minute;
548 tmpTime.tm_sec = m_mail->head->datetime.second;
550 setDateTime(tmpTime);
553 if (m_mail->full_address_from) {
555 from.setRecipients(m_mail->full_address_from);
556 setSourceAddress(from);
557 setSourceAddressValidity(true); //not needed to update in platform
563 void Email::readBody() {
566 Assert(m_mail && "Body is NULL.");
568 if (m_mail->file_path_plain) {
569 DPL::ScopedFClose file(::fopen(m_mail->file_path_plain, "r"));
571 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
572 "Cannot read body file: " << m_mail->file_path_plain);
574 fseek(file.Get(), 0, SEEK_END);
575 long int size = ftell(file.Get());
576 fseek(file.Get(), 0, SEEK_SET);
577 DPL::ScopedPtr<char> data(new char[size + 1]);
578 memset(data.Get(), 0, size + 1);
579 fread(data.Get(), 1, size, file.Get());
584 if (m_mail->file_path_html) {
585 DPL::ScopedFClose file(::fopen(m_mail->file_path_html, "r")); //auto close, scopedFClose
587 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
588 "Cannot read html body file: " << m_mail->file_path_html);
591 fseek(file.Get(), 0, SEEK_END);
592 long int size = ftell(file.Get());
593 fseek(file.Get(), 0, SEEK_SET);
594 DPL::ScopedPtr<char> data(new char[size + 1]);
595 memset(data.Get(), 0, size + 1);
596 fread(data.Get(), 1, size, file.Get());
597 setHtmlBody(data.Get()); //setHtmlBody declarate in Email Calss.
601 //email_attachment_info_t* attachment = NULL;
602 email_attachment_data_t* attachment = NULL;
603 int attachmentCount = 0;
605 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
606 if (EMAIL_ERROR_NONE != error) {
607 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
610 if ( attachment && attachmentCount > 0)
612 LogDebug("reading attachments , attahcment count = " << attachmentCount);
615 for ( i = 0; i < attachmentCount; i++)
618 LogDebug("attachment ID :" << attachment[i].attachment_id << " name :" << attachment[i].attachment_name << " download :" << attachment[i].save_status << "savefile :" << attachment[i].attachment_path);
620 IAttachmentPtr tmpAtt(new Attachment());
621 if (attachment[i].attachment_path)
622 tmpAtt->init(attachment[i].attachment_path, false);
626 tmpAtt->rename(std::string(attachment[i].attachment_name));
627 tmpAtt->setAttachmentID(attachment[i].attachment_id);
628 tmpAtt->setDownloaded(attachment[i].save_status);
629 //tmpAtt->setMimeType(std::string(attachment[i].attachment_mime_type));
632 appendAttachment(tmpAtt);
634 LogDebug(" append Attachment complete");
638 catch ( const WrtDeviceApis::Commons::Exception& ex )
640 LogError("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
647 error = email_free_attachment_data(&attachment, attachmentCount);
648 if (EMAIL_ERROR_NONE != error) {
649 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
657 void Email::readInfo()
661 Assert(m_mail && "Info is NULL.");
663 setReadStatus(m_mail->flags_seen_field == 1);
665 m_accountId = m_mail->account_id;
667 setPriority( EmailConverter::toMessagePriority( m_mail->priority ) );
668 setSize(m_mail->mail_size);
673 void Email::updateBody()
677 if (isBodyValid()) { return; }
680 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
682 if (!m_mail->file_path_plain) {
683 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
685 // fix email empty plain text
687 if (!m_mail->file_path_html) {
688 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
691 if ((!m_mail->file_path_html) && (strlen(getHtmlBody().c_str()) > 0) ) {
692 std::string htmlFile = tmpnam(NULL);
693 FILE* f = fopen(htmlFile.c_str(), "w");
695 m_mail->file_path_html = strdup(htmlFile.c_str());
697 if(!m_mail->file_path_html)
699 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
704 FILE* f = fopen(m_mail->file_path_plain, "w");
706 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
711 LogDebug("Plain Body file is NULL.");
712 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
715 //update html body (if exist)
716 if (m_mail->file_path_html)
719 f = fopen(m_mail->file_path_html, "w");
721 //fprintf(f, getHtmlBody().c_str());
722 fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
726 // fix email empty plain text
730 LogDebug("Html Body file is NULL.");
731 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "HTML Body File Open Error");
734 m_mail->body_download_status = true;
736 setBodyValidity(true);
741 void Email::updateSubject()
745 if (isSubjectValid()) { return; }
747 LogDebug("update subject, msgId=" << getIdRef());
750 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
753 m_mail->subject = String::strdup(getSubjectRef());
754 setSubjectValidity(true);
759 void Email::updateReadStatus()
763 if (isReadStatusValid()) { return; }
765 LogDebug("update read, msgId=" << getIdRef());
768 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
771 m_mail->flags_seen_field = isRead();
772 setReadStatusValidity(true);
777 void Email::updateIsRead()
780 if (isReadChangeStatusValid()) {
781 // do not update if not changed
785 EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
786 setisReadChangeStatusValidity(true);
790 void Email::updateMessage()
793 email_meeting_request_t *meeting_req = NULL;
795 DPL::Mutex::ScopedLock mx(&m_updateMutex);
798 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
801 LogDebug("getCurrentFolder() = " << getCurrentFolder() );
803 if(getCurrentFolder() == Api::Messaging::DRAFTBOX)
815 email_attachment_data_t* attachment=NULL;
816 int attachmentCount = 0;
817 int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
818 if (EMAIL_ERROR_NONE != error) {
819 LogWarning("email_get_attachment_data_list [" << error << "]");
822 error = email_get_meeting_request(m_mail->mail_id, &meeting_req);
823 if (EMAIL_ERROR_NONE != error) {
824 LogWarning("email_get_meeting_request() failed [%d]\n" << error);
827 error = email_update_mail( m_mail.Get(), attachment, attachmentCount, meeting_req, 0);
828 if (EMAIL_ERROR_NONE != error) {
829 LogWarning("Nothing to update or error. [" << error << "]");
833 email_free_meeting_request(&meeting_req, 1);
839 void Email::createSendMessage()
843 MailSender::getInstance(); //start email service
845 if ( getUID() > 0 && m_accountId > 0)
851 DPL::Mutex::ScopedLock mx(&m_updateMutex);
853 Api::Messaging::EmailAccountInfo account = getEmailAccount();
854 m_accountId = account.getIntId(); //set account ID
855 LogDebug("account ID : " << m_accountId);
856 m_mail.Reset(EmailService::createMailData(account));
864 email_mailbox_t* mailbox;
865 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox );
866 if (EMAIL_ERROR_NONE != error) {
867 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
868 "Couldn't add message to mailbox. [" << error << "]");
870 if ( mailbox->mailbox_id )
871 m_mail->mailbox_id = mailbox->mailbox_id;
872 LogDebug("mail MailBox id :" << m_mail->mailbox_id);
874 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
875 if (EMAIL_ERROR_NONE != error) {
876 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
877 "Couldn't add message to mailbox. [" << error << "]");
880 LogDebug("message id =" << m_mail->mail_id);
882 setId(convertId(m_mail->mail_id));
883 setFolderType(OUTBOX);
884 setMessageStatus(Api::Messaging::MESSAGE_STATUS_SENDING);
886 updateAttachments(); //update Attachment.
888 error = email_free_mailbox(&mailbox, 1);
889 if (EMAIL_ERROR_NONE != error) {
890 LogError("Failed to destroy mailbox: " << error);
893 LogDebug("m_mail->from" << m_mail->full_address_from);
895 email_mail_data_t* result = NULL;
897 error = email_get_mail_data(m_mail->mail_id, &result);
898 if (EMAIL_ERROR_NONE != error) {
899 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
900 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
903 if (m_mail->file_path_plain)
904 free(m_mail->file_path_plain);
905 m_mail->file_path_plain = strdup(result->file_path_plain);
907 if ( m_mail->file_path_html)
909 free(m_mail->file_path_html);
912 if(result->file_path_html)
914 m_mail->file_path_html = strdup(result->file_path_html);
917 error = email_free_mail_data(&result, 1);
918 if (EMAIL_ERROR_NONE != error) {
919 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
920 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
928 void Email::addMessageToDraft()
932 MailSender::getInstance(); //start email service
934 if ( getUID() > 0 && m_accountId > 0)
940 DPL::Mutex::ScopedLock mx(&m_updateMutex);
942 Api::Messaging::EmailAccountInfo account = getEmailAccount();
943 m_accountId = account.getIntId(); //set account ID
944 LogDebug("account ID : " << m_accountId);
945 m_mail.Reset(EmailService::createMailData(account));
953 email_mailbox_t* mailbox;
954 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
955 if (EMAIL_ERROR_NONE != error) {
956 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
957 "Couldn't add message to mailbox. [" << error << "]");
959 if ( mailbox->mailbox_id )
960 m_mail->mailbox_id = mailbox->mailbox_id;
961 LogDebug("mail MailBox id :" << m_mail->mailbox_id);
963 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
964 if (EMAIL_ERROR_NONE != error) {
965 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
966 "Couldn't add message to mailbox. [" << error << "]");
969 LogDebug("message id =" << m_mail->mail_id);
971 setId(convertId(m_mail->mail_id));
972 setFolderType(DRAFTBOX);
973 setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
975 updateAttachments(); //update Attachment.
977 error = email_free_mailbox(&mailbox, 1);
978 if (EMAIL_ERROR_NONE != error) {
979 LogError("Failed to destroy mailbox: " << error);
982 LogDebug("m_mail->from" << m_mail->full_address_from);
984 email_mail_data_t* result = NULL;
986 error = email_get_mail_data(m_mail->mail_id, &result);
987 if (EMAIL_ERROR_NONE != error) {
988 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
989 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
992 if (m_mail->file_path_plain)
993 free(m_mail->file_path_plain);
994 m_mail->file_path_plain = strdup(result->file_path_plain);
996 if ( m_mail->file_path_html)
998 free(m_mail->file_path_html);
1001 if(result->file_path_html)
1003 m_mail->file_path_html = strdup(result->file_path_html);
1006 error = email_free_mail_data(&result, 1);
1007 if (EMAIL_ERROR_NONE != error) {
1008 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1009 "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1017 void Email::updateRecipients()
1021 if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1024 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1027 if (!getToValidity()) {
1028 std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1029 if (m_mail->full_address_to) {
1030 free(m_mail->full_address_to);
1032 m_mail->full_address_to = String::strdup(addressLine);
1033 setToValidity(true);
1036 if (!getCcValidity()) {
1037 std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1038 if (m_mail->full_address_cc) {
1039 free(m_mail->full_address_cc);
1041 m_mail->full_address_cc = String::strdup(addressLine);
1042 setCcValidity(true);
1045 if (!getBccValidity()) {
1046 std::string addressLine = EmailUtils::formatAddressLine(
1047 getBccRecipients());
1048 if (m_mail->full_address_bcc) {
1049 free(m_mail->full_address_bcc);
1051 m_mail->full_address_cc = String::strdup(addressLine);
1052 setBccValidity(true);
1058 void Email::updateFrom()
1062 if (getFromValidity()) { return; }
1065 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1068 m_mail->account_id = m_accountId;
1070 if (m_mail->full_address_from) {
1071 free(m_mail->full_address_from);
1073 m_mail->full_address_from = String::strdup(getFrom());
1075 setFromValidity(true);
1080 void Email::updateAttachments()
1084 if (isAttachmentsValid()) { return; }
1087 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1090 email_attachment_data_t* attachment = NULL;
1091 int attachmentCount = 0;
1092 email_mailbox_t* mailBox = NULL;
1097 LogDebug("update attachments, msgId=" << getIdRef());
1098 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount); //get Attachment list
1099 if (EMAIL_ERROR_NONE != error) {
1100 ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1102 // error = email_get_mailbox_by_name(m_mail->account_id, m_mail->mailbox_name, &mailBox);
1103 // if (EMAIL_ERROR_NONE != error) {
1104 // ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1105 // "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1108 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1110 if (EMAIL_ERROR_NONE != error) {
1111 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1112 "Couldn't get mailbox. [" << error << "]");
1115 if ( attachmentCount > 0 )
1118 std::stringstream stream;
1119 for ( i = 0; i < attachmentCount; i++)
1121 LogDebug("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1122 int attachmentId = attachment[i].attachment_id;
1124 // stream << attachmentId;
1125 // int error = email_delete_attachment(m_mail->mail_id, (const char*)(stream.str()).c_str());
1126 int error = email_delete_attachment(attachmentId);
1127 if (EMAIL_ERROR_NONE != error) {
1128 LogDebug("Error Num = " << error);
1129 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1130 "Error while adding attachment. [" << error << "]");
1137 error = email_free_attachment_data(&attachment, attachmentCount);
1138 if (EMAIL_ERROR_NONE != error) {
1139 ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1146 std::size_t attachmentSize = getAttachmentsCount();
1147 LogDebug("update attachments, attachmentSize=" << attachmentSize);
1148 if (attachmentSize > 0) {
1149 // email_attachment_info_t* attachment_info = NULL;
1150 email_attachment_data_t* attachment_info = NULL;
1151 for (std::size_t i = 0; i < attachmentSize; ++i) {
1152 IAttachmentPtr att = getAttachment(i);
1157 //copy attachment file
1158 std::stringstream cp_cmd;
1159 char buf[] = "/tmp/XXXXXX";
1160 int i = mkstemp(buf);
1162 cp_cmd << COMMAND_NAME;
1163 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1164 cp_cmd << " \"" << att->getFullPath() << "\"";
1165 cp_cmd << " \"" << buf << "\"";
1167 // attachment_info = EmailService::alloc<email_attachment_info_t>();
1168 attachment_info = EmailService::alloc<email_attachment_data_t>();
1169 attachment_info->attachment_name = String::strdup(att->getShortName());
1170 attachment_info->attachment_path = String::strdup( buf );
1171 // attachment_info->next = NULL;
1172 attachment_info->save_status = true;
1174 LogDebug("Copy Command=" << cp_cmd.str());
1176 int result = system(cp_cmd.str().c_str());
1178 if (0 != WIFEXITED(result)) {
1179 if (0 != WEXITSTATUS(result)) {
1180 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1183 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1184 "Command terminated abnormally.");
1187 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1190 LogDebug("add Attachment");
1191 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1192 if (EMAIL_ERROR_NONE != error) {
1193 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1194 "Error while adding attachment. [" << error << "]");
1198 LogDebug(" attachment id : " << attachment_info->attachment_id);
1199 //IMessagePtr msg = DPL::DynamicPointerCast<IMessage>(this);
1200 att->setDownloaded(true);
1201 att->setAttachmentID(attachment_info->attachment_id);
1202 att->setMessage(SharedFromThis());
1206 if (attachment_info)
1207 EmailService::freeAttachment(attachment_info);
1213 Catch(WrtDeviceApis::Commons::PlatformException) {
1216 int error = email_free_attachment_data(&attachment, attachmentCount);
1217 if (EMAIL_ERROR_NONE != error) {
1218 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1219 "Error while adding attachment data [" << error << "]");
1222 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1223 "Error attachment Update");
1230 void Email::updatePriority()
1234 if (isPriorityValid()) { return; }
1237 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1240 m_mail->priority = EmailConverter::toMailPriority( getPriority());
1241 setPriorityValid(true);
1246 int Email::getIntId() const
1248 return convertId(getIdRef());