a250d303b0e668c2b7f7eb489842e45b12bbc585
[framework/web/wrt-plugins-tizen.git] / src / Messaging / Email.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17  
18 #include <unistd.h>
19 #include <cstdio>
20 #include <cstddef>
21 #include <cstdlib>
22 #include <ctime>
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"
32 #include "Email.h"
33 #include "EmailService.h"
34 #include "EmailUtils.h"
35 #include "EmailConverter.h"
36 #include "MailSender.h"
37 #include "MailSync.h"
38 #include "MessagingService.h"
39 #include "MessagingServiceManager.h"
40 #include "Attachment.h"
41 #include <Logger.h>
42
43 #define LOG_ENTER LoggerD("---> ENTER");
44 #define LOG_EXIT LoggerD("---> EXIT");
45
46 using namespace std;
47 using namespace WrtDeviceApis::Commons;
48
49 namespace DeviceAPI {
50 namespace Messaging {
51
52 namespace {
53 const char* COMMAND_NAME = "/bin/cp";
54 const char* COMMAND_SWITCH_RECURSIVE = "-r";
55 //const char* COMMAND_SWITCH_FORCE = "-f";
56 }
57
58 Email::Email(const string& id) :
59         IMessage(EMAIL, id) {
60         LOG_ENTER
61
62         try {
63                 if (getIdRef().empty()) {
64                         //exception
65                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
66                 }
67                                 
68                 reload();
69         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
70                 LoggerE("Exception: " << ex.DumpToString());
71         }
72
73         LOG_EXIT
74 }
75
76 Email::Email(const std::string& id, int accountId) :
77         IMessage(EMAIL, id) 
78 {
79         LOG_ENTER
80
81         try {
82                 if (getIdRef().empty()) {
83                         //exception
84                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, " message id is invalid");
85                 }
86                 
87                 LoggerD("Account ID = " << accountId);
88                 m_accountId = accountId;
89                 reload();
90                 
91         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
92                 LoggerE("Exception: " << ex.DumpToString());
93         }
94
95
96         LOG_EXIT
97 }
98
99 Email::Email(EmailAccountInfo& account) : IMessage(EMAIL)
100 {
101     LOG_ENTER
102
103     try {
104          if (getIdRef().empty()) {
105                 create(account);
106          }
107         
108         reload();
109     }
110     catch (const WrtDeviceApis::Commons::PlatformException& ex) {
111         LoggerE("Exception: " << ex.DumpToString());
112     }
113
114     LOG_EXIT
115 }
116
117 Email::Email(const FolderType folder) : IMessage(EMAIL)
118 {
119     LOG_ENTER
120
121     try {
122          if (folder == TEMP_FOLDER) {
123                 create();
124          }
125            else {
126               ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Folder Type is miss-matched");
127            }
128     }
129     catch (const WrtDeviceApis::Commons::PlatformException& ex) {
130         LoggerE("Exception: " << ex.DumpToString());
131     }
132
133     LOG_EXIT
134 }
135
136 Email::~Email()
137 {
138     LoggerD("ENTER");
139 }
140
141 int Email::send()
142 {
143     LOG_ENTER
144
145 //    update();
146
147     int handle = MailSender::getInstance().send(
148         MessageFactory::convertToEmail(SharedFromThis())
149         );
150
151     return handle;
152     LOG_EXIT
153 }
154
155 void Email::sendCancel(int handle)
156 {
157     LOG_ENTER
158
159     MailSender::getInstance().cancel(handle);
160
161     LOG_EXIT
162 }
163
164 int Email::downloadBody()
165 {
166         LOG_ENTER
167         return MailSync::getInstance().downloadBody( MessageFactory::convertToEmail(SharedFromThis()) );
168         LOG_EXIT
169 }
170
171 void Email::downloadBodyCancel( int handle)
172 {       
173         return MailSync::getInstance().cancelDownloadBody(handle);
174 }
175
176 int Email::downloadAttachment( const IAttachmentPtr& attachment)
177 {
178         LOG_ENTER
179         return MailSync::getInstance().downloadAttachment( MessageFactory::convertToEmail(SharedFromThis()), attachment );
180         LOG_EXIT
181 }
182
183 void Email::downloadAttachmentCancel( int handle)
184 {       
185         LOG_ENTER
186         return MailSync::getInstance().cancelDownloadAttachment(handle);
187         LOG_EXIT
188 }
189
190 void Email::update(bool draftsOnly)
191 {
192     LOG_ENTER
193
194     DPL::Mutex::ScopedLock mx(&m_updateMutex);
195
196     if (!m_mail) {
197         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
198     }
199
200     updateSubject();
201     updateBody();
202     updateRecipients();
203     updateAttachments();
204     updateReadStatus();
205
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 << "]");
211     }
212
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 << "]");
216     }
217
218     if (attachment)
219     {
220             error = email_free_attachment_data(&attachment, attachmentCount);
221             if (EMAIL_ERROR_NONE != error) {
222                   LoggerW("Nothing to update or error. [" << error << "]");
223             }
224     }
225
226     LOG_EXIT
227 }
228
229 int Email::getAccountID()
230 {
231         LOG_ENTER
232
233         if ( m_mail )
234                 return m_mail->account_id;
235         else
236                 return -1;
237         
238         LOG_EXIT
239 }
240
241 int Email::getUID()
242 {
243         LOG_ENTER
244         
245         if (m_mail)
246                 return m_mail->mail_id;
247         else
248                 return -1;
249
250         LOG_EXIT
251 }
252
253 int Email::isBodyDownloaded()
254 {
255
256         if (m_mail)
257                 if ( m_mail->body_download_status == 1 )        // 1 is fully downloaded body.
258                         return true;
259                 else
260                         return false;
261         else
262                 return false;
263
264 }
265
266 bool Email::hasAttachment()
267 {       
268         std::size_t attachmentSize = getAttachmentsCount();
269         if ( attachmentSize > 0)
270                         return true;
271                 else
272                         return false;
273 }
274
275 void Email::readAllData()
276 {
277     reload();
278 }
279
280 void Email::moveToFolder(const FolderType newFolder)
281 {
282 /*
283
284     LOG_ENTER
285     moveToFolder(EmailConverter::toMailboxType(newFolder));
286     LOG_EXIT
287 */
288 }
289
290 void Email::moveToFolder(const string& newFolder)
291 {
292 /*
293         Assert(m_mail && "mail is NULL.");
294         
295         update();
296
297         int accountId = m_mail->account_id;
298         int mailId = getIntId();
299
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);
303
304         int error = email_move_mail_to_mailbox(&mailId, 1, mailbox);
305         if (EMF_ERROR_NONE != error) {
306         ThrowMsg(
307             WrtDeviceApis::Commons::PlatformException,
308             "Couldn't move mail to folder: " << newFolder << ". [" <<
309             error << "]");
310         }
311         
312         error = email_free_mailbox(&mailbox, 1);        //free
313         if (EMF_ERROR_NONE != error) {
314             LoggerE("Failed to destroy mailbox: " << error);
315         }
316 */
317 }
318
319 void Email::copyToFolder(const FolderType newFolder)
320 {
321     LOG_ENTER
322
323     copyToFolder(EmailConverter::toMailboxName(newFolder));
324
325     LOG_EXIT
326 }
327
328 void Email::copyToFolder(const string& newFolder)
329 {
330         LOG_ENTER
331 /*              
332         Assert(m_mail && "mail is NULL.");
333
334         update();
335
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;
340         
341         //Attachment
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 << "]");
348         }
349         
350         int mailId = EmailService::addMail( mail.Get(), attachment );
351         if (0 == mailId) {
352             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cloned mail didn't get new id.");
353         }
354 */      
355     LOG_EXIT
356 }
357
358 void Email::remove()
359 {
360     EmailService::deleteMail(m_accountId, getIntId());
361 }
362
363 #if 0
364 void Email::create()
365 {
366     LOG_ENTER
367
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)
374         );
375     setId(convertId(EmailService::addMailToMailbox(mail.Get(), mailbox.Get())));
376     setFolderType(DRAFTBOX);
377     setMessageStatus(MESSAGE_STATUS_DRAFT);
378
379     LOG_EXIT
380 }
381 #endif
382
383 void Email::create()
384 {
385     LOG_ENTER
386     setFolderType(TEMP_FOLDER);
387     setMessageStatus(MESSAGE_STATUS_CREATED);
388     LOG_EXIT
389 }
390
391 void Email::create( EmailAccountInfo& account )
392 {
393         LOG_ENTER
394
395         m_accountId = account.getIntId();
396         //MailSender::getInstance();
397         
398       LoggerD("account ID : " << m_accountId);
399         ScopedMail mail(EmailService::createMailData(account));
400         
401         setEmailAccount(account);       //save account
402
403         mail->mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
404         
405         setId(convertId(EmailService::addMail(mail.Get(), NULL)));
406         setFolderType(DRAFTBOX);
407         setMessageStatus(MESSAGE_STATUS_DRAFT);
408         
409     LOG_EXIT
410 }
411
412 void Email::reload()
413 {
414     LOG_ENTER
415
416     LoggerD("mail ID :" << getIntId());
417     m_mail.Reset(EmailService::readMail(getIntId()));           //reset Mail Data
418
419     readHeader();
420     readBody();
421     readInfo();
422
423     //if (m_mail->head) { readHeader(); }
424     //if (m_mail->body) { readBody(); }
425     //if (m_mail->info) { readInfo(); }
426
427     //m_mailbox.Reset(EmailService::getMailboxByMailId(m_accountId, getIntId()) );
428     //setFolderType(EmailConverter::toFolderType(m_mailbox->mailbox_type));
429     
430     setFolderType(EmailConverter::toFolderType(m_mail->mailbox_type));
431     setMessageStatus(EmailConverter::toMessageStatus(m_mail->mailbox_type));
432
433     LOG_EXIT
434 }
435
436 void Email::readHeader()
437 {
438     LOG_ENTER
439
440     Assert(m_mail && "Header is NULL.");
441
442     if (m_mail->subject) {
443         setSubject(m_mail->subject);
444     }
445
446     if (m_mail->full_address_to) {
447         appendToRecipients(EmailUtils::stripAddressLine(m_mail->full_address_to));
448     }
449
450     if (m_mail->full_address_cc) {
451         appendCcRecipients(EmailUtils::stripAddressLine(m_mail->full_address_cc));
452     }
453
454     if (m_mail->full_address_bcc) {
455         appendBccRecipients(EmailUtils::stripAddressLine(m_mail->full_address_bcc));
456     }
457
458 #if 1
459     struct tm *t = NULL;
460     t = localtime(&m_mail->date_time);
461     struct tm timeinfo;
462         memset(&timeinfo, 0, sizeof(timeinfo));
463     memcpy(t, &timeinfo, sizeof(struct tm));
464     setDateTime(timeinfo); 
465
466 #else
467
468         if (m_mail->datetime[0] != '\0')
469         {
470                 char buf[MAX_DATETIME_STRING_LENGTH];
471                 char *targetBuf = m_mail->datetime;
472                 struct tm timeinfo;
473
474                 memset(buf, 0x00, sizeof(buf));
475                 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
476                 timeinfo.tm_year = atoi(buf) - 1900;
477
478                 memset(buf, 0x00, sizeof(buf));
479                 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
480                 timeinfo.tm_mon =  atoi(buf) - 1;
481
482                 memset(buf, 0x00, sizeof(buf));
483                 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
484                 timeinfo.tm_mday =      atoi(buf);
485
486                 memset(buf, 0x00, sizeof(buf));
487                 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
488                 timeinfo.tm_hour = atoi(buf);
489
490                 memset(buf, 0x00, sizeof(buf));
491                 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
492                 timeinfo.tm_min = atoi(buf);
493
494                 memset(buf, 0x00, sizeof(buf));
495                 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
496                 timeinfo.tm_sec = atoi(buf);
497                 
498                 setDateTime(timeinfo);
499         }
500
501 #endif
502         
503 #if 0
504     time_t rawtime;
505     time(&rawtime);
506     struct tm tmpTime = *(localtime(&rawtime));
507
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;
514     mktime(&tmpTime);
515     setDateTime(tmpTime);
516 #endif
517
518     if (m_mail->full_address_from) {
519         Recipients from;
520         from.setRecipients(m_mail->full_address_from);
521         setSourceAddress(from);
522         setSourceAddressValidity(true); //not needed to update in platform
523     }
524
525     LOG_EXIT
526 }
527
528 void Email::readBody() {
529         LOG_ENTER
530
531         Assert(m_mail && "Body is NULL.");
532
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"));
536                 if (!file) {
537                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
538                                         "Cannot read body file: " << m_mail->file_path_plain);
539                 }
540                 fseek(file.Get(), 0, SEEK_END);
541                 long int size = ftell(file.Get());
542
543                 if (size < 0) {
544                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
545                                         "Cannot read plain body size");
546                 }
547                 
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);
553                 setBody(data.Get());
554         }
555
556         //html body.
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
560                 if (!file) {
561                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
562                                         "Cannot read html body file: " << m_mail->file_path_html);
563                 }
564
565                 fseek(file.Get(), 0, SEEK_END);
566                 long int size = ftell(file.Get());
567
568                 if (size < 0) {
569                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
570                                         "Cannot read html body size");
571                 }
572                 
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.
579         }
580
581         //Attachment
582         //email_attachment_info_t* attachment = NULL;
583         email_attachment_data_t* attachment = NULL;
584         int attachmentCount = 0;
585         
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: ");
589         }
590         
591         if ( attachment && attachmentCount > 0)
592         {
593                 LoggerD("reading attachments , attahcment count = " << attachmentCount);
594                 int i = 0;
595
596                 for ( i = 0; i < attachmentCount; i++)
597                 {
598                         Try {
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);
604                         
605                                 if (tmpAtt)
606                                 {
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                                         tmpAtt->setNth(i+1);
613                                         LoggerD(" setNth(i+1) : " << i+1);
614                                         if(attachment[i].inline_content_status == 0)
615                                         {
616                                         appendAttachment(tmpAtt);
617                                         }
618                                         else
619                                         {
620                                                 appendInlineAttachment(tmpAtt);
621                                         }
622                                 }
623                                 LoggerD(" append Attachment complete");
624
625                         
626                         }
627                         catch ( const WrtDeviceApis::Commons::Exception& ex )
628                         {
629                                 LoggerE("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
630                         }
631                         
632                 }
633
634         }
635         
636         if (attachment)
637         {
638                 error = email_free_attachment_data(&attachment, attachmentCount);
639                 if (EMAIL_ERROR_NONE != error) {
640                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
641                 }
642         }
643
644         LOG_EXIT
645 }
646
647 void Email::readInfo()
648 {
649         LOG_ENTER
650
651         if (!m_mail)
652         {
653                 LoggerD("m_mail is NULL");
654                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Info is NULL.");
655         }
656
657         setReadStatus(m_mail->flags_seen_field == 1);
658
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);
663
664         LOG_EXIT
665 }
666
667 void Email::updateBody()
668 {
669         LOG_ENTER
670  
671         int error = 0;
672         bool locationChecker = false;
673  
674         if (isBodyValid()) { return; }
675  
676         if (!m_mail) {
677                 LoggerD("m_mail is NULL");
678                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
679         }
680         if (!m_mail->file_path_plain) {
681                 LoggerD("file_path_plain is NULL");
682                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
683         }
684
685         // For Plain Body
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
688         {
689                 LoggerD("create new plain body");
690                 char buf[] = "XXXXXX";
691                 mode_t mask = umask(S_IWGRP | S_IWOTH);
692                 error = mkstemp(buf);
693                 umask(mask);
694                 std::string bodyFile = buf;
695                 bodyFile = EmailUtils::TMP_DIR_PREFIX+bodyFile;
696                 LoggerD("bodyFile : " << bodyFile);
697
698                 FILE* f = fopen(bodyFile.c_str(), "w");
699                 if (NULL != f) {
700                         fclose(f);
701                 }
702
703                 m_mail->file_path_plain = strdup(bodyFile.c_str());
704         }
705
706         FILE* f = fopen(m_mail->file_path_plain, "w");
707         if (NULL != f) {
708                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
709                 fclose(f);
710         }
711         else
712         {
713                 LoggerD("Plain Body file open fail : " << m_mail->file_path_plain);
714                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
715         }
716
717
718         // For Html Body
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
721         {
722                 LoggerD("create new html body");
723                 char buf[] = "XXXXXX";
724                 mode_t mask = umask(S_IWGRP | S_IWOTH);
725                 error = mkstemp(buf);
726                 umask(mask);
727                 std::string htmlFile = buf;
728                 htmlFile = EmailUtils::TMP_DIR_PREFIX+htmlFile;
729                 LoggerD("htmlFile : " << htmlFile);
730
731                 FILE* f = fopen(htmlFile.c_str(), "w");
732                 if (NULL != f) {
733                         fclose(f);
734                 }
735                 m_mail->file_path_html = strdup(htmlFile.c_str());
736
737                 if(!m_mail->file_path_html)
738                 {
739                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
740                 }
741         }
742
743         f = fopen(m_mail->file_path_html, "w");
744         if (NULL != f) {
745                 if(strlen(getHtmlBody().c_str()) > 0)   // check html data is exist if not exist copy plain body to html body
746                 {
747                         fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
748                 }
749                 else
750                 {
751                         // htmlBody is empty.
752                 }
753                 fclose(f);
754         }
755         else
756         {
757                 LoggerD("html Body file open fail : " << m_mail->file_path_html);
758                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "html Body File Open Error");
759         }
760
761         m_mail->body_download_status = 1;
762
763         setBodyValidity(true);
764
765         LOG_EXIT
766
767 }
768
769 void Email::updateSubject()
770 {
771     LOG_ENTER
772
773     if (isSubjectValid()) { return; }
774
775     LoggerD("update subject, msgId=" << getIdRef());
776
777     if (!m_mail) {
778         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
779     }
780     
781     m_mail->subject = String::strdup(getSubjectRef());
782     setSubjectValidity(true);
783
784     LOG_EXIT
785 }
786
787 void Email::updateReadStatus()
788 {
789     LOG_ENTER
790
791     if (isReadStatusValid()) { return; }
792
793     LoggerD("update read, msgId=" << getIdRef());
794
795     if (!m_mail) {
796         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
797     }
798
799     m_mail->flags_seen_field = isRead();
800     setReadStatusValidity(true);
801
802     LOG_EXIT
803 }
804
805 void Email::updateIsRead()
806 {
807     LOG_ENTER
808     if (isReadChangeStatusValid()) {
809         // do not update if not changed
810         return;
811     }
812
813     EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
814     setisReadChangeStatusValidity(true);
815     LOG_EXIT
816 }
817
818 void Email::updateMessage()
819 {
820         LOG_ENTER
821         email_meeting_request_t *meeting_req = NULL;
822
823         DPL::Mutex::ScopedLock mx(&m_updateMutex);
824
825         if (!m_mail) {
826                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
827         }
828
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 );
833
834         if(getCurrentFolder() == DRAFTBOX)
835         {
836                 updateBody();
837                 updateSubject();
838                 updateRecipients();
839                 updateFrom();
840                 updateAttachments();
841                 updatePriority();
842         }
843         updateReadStatus();
844
845         //get attachment
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 << "]");
851         }
852
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);
856         }
857
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 << "]");
861         }
862
863         if(meeting_req) {
864                 email_free_meeting_request(&meeting_req, 1);
865         }
866
867         if (attachment)
868         {
869                 error = email_free_attachment_data(&attachment, attachmentCount);
870                 if (EMAIL_ERROR_NONE != error) {
871                         LoggerW("email_free_attachment_data [" << error << "]");
872                 }               
873         }
874
875         LOG_EXIT
876 }
877
878 void Email::createSendMessage()
879 {
880         LOG_ENTER
881
882     MailSender::getInstance(); //start email service
883         LoggerD("getUID:  " << getUID());
884         if ( getUID() > 0 && m_accountId > 0)
885         {       
886                 update();
887         }
888         else
889         {       
890         DPL::Mutex::ScopedLock mx(&m_updateMutex);
891                 
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));
896                 
897                 updateSubject();
898                 updateBody();
899                 updateRecipients();
900                 updatePriority();
901                 updateReadStatus();
902                 
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 << "]");
908                 }
909                 if ( mailbox->mailbox_id )
910                         m_mail->mailbox_id = mailbox->mailbox_id;
911                 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
912
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 << "]");
917                 }
918                 
919                 LoggerD("m_mail->mail_id =" << m_mail->mail_id);
920                 
921                 setId(convertId(m_mail->mail_id));
922                 setFolderType(OUTBOX);
923                 setMessageStatus(MESSAGE_STATUS_SENDING);
924
925                 updateAttachments();    //update Attachment.
926                 
927                 error = email_free_mailbox(&mailbox, 1);
928                 if (EMAIL_ERROR_NONE != error) {
929                         LoggerE("Failed to destroy mailbox: " << error);
930                 }
931
932                 LoggerD("m_mail->from" << m_mail->full_address_from);
933
934                 email_mail_data_t* result = NULL;
935
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 << "]");
940                 }
941
942
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);
946                 
947                 if ( m_mail->file_path_html)
948                 {
949                         free(m_mail->file_path_html);
950                 }
951
952                 if(result->file_path_html)
953                 {
954                         m_mail->file_path_html = strdup(result->file_path_html);
955                 }
956                 
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 << "]");
961                 }               
962
963     }
964         LOG_EXIT
965 }
966
967
968 void Email::addMessageToDraft()
969 {
970     LOG_ENTER
971
972     MailSender::getInstance(); //start email service
973         
974         LoggerD("getUID:  " << getUID());
975     if ( getUID() > 0 && m_accountId > 0)
976     {   
977                 update();
978     }
979     else
980     {   
981                 DPL::Mutex::ScopedLock mx(&m_updateMutex);
982                 
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));
987                 
988                 updateSubject();
989                 updateBody();
990                 updateRecipients();
991                 updatePriority();
992                 updateReadStatus();
993                 
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 << "]");
999                 }
1000                 if ( mailbox->mailbox_id )
1001                         m_mail->mailbox_id = mailbox->mailbox_id;
1002                 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
1003
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 << "]");
1008                 }
1009                 
1010                 LoggerD("message id =" << m_mail->mail_id);
1011                 
1012                 setId(convertId(m_mail->mail_id));
1013                 setFolderType(DRAFTBOX);
1014                 setMessageStatus(MESSAGE_STATUS_DRAFT);
1015
1016                 updateAttachments();    //update Attachment.
1017                 
1018                 error = email_free_mailbox(&mailbox, 1);
1019                 if (EMAIL_ERROR_NONE != error) {
1020                         LoggerE("Failed to destroy mailbox: " << error);
1021                 }
1022
1023                 LoggerD("m_mail->from" << m_mail->full_address_from);
1024
1025                 email_mail_data_t* result = NULL;
1026
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 << "]");
1031                 }
1032
1033                 if(result->body_download_status != 1)
1034                 {
1035                         LoggerD("result->body_download_status " << result->body_download_status);
1036                         int mail_id_count = 1;
1037                         int mail_id_array[1];
1038                         email_mail_attribute_type attribute_type;
1039                         email_mail_attribute_value_t attribute_value;
1040                 
1041                         mail_id_array[0] = m_mail->mail_id;
1042                         attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
1043                         attribute_value.integer_type_value = 1;
1044                         
1045                         email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
1046                 }
1047
1048                 readHeader();
1049                 readInfo();
1050
1051                 if (m_mail->file_path_plain)
1052                         free(m_mail->file_path_plain);
1053                 m_mail->file_path_plain = strdup(result->file_path_plain);
1054                 
1055                 if ( m_mail->file_path_html)
1056                 {
1057                         free(m_mail->file_path_html);
1058                 }
1059
1060                 if(result->file_path_html)
1061                 {
1062                         m_mail->file_path_html = strdup(result->file_path_html);
1063                 }
1064                 
1065                 error = email_free_mail_data(&result, 1);
1066                 if (EMAIL_ERROR_NONE != error) {
1067                     ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1068                              "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1069                 }               
1070
1071     }
1072
1073     LOG_EXIT
1074 }
1075
1076 void Email::updateRecipients()
1077 {
1078     LOG_ENTER
1079
1080     if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1081
1082     if (!m_mail) {
1083         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1084     }
1085
1086     if (!getToValidity()) {
1087         std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1088         if (m_mail->full_address_to) {
1089             free(m_mail->full_address_to);
1090         }
1091         m_mail->full_address_to = String::strdup(addressLine);
1092         setToValidity(true);
1093     }
1094
1095     if (!getCcValidity()) {
1096         std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1097         if (m_mail->full_address_cc) {
1098             free(m_mail->full_address_cc);
1099         }
1100         m_mail->full_address_cc = String::strdup(addressLine);
1101         setCcValidity(true);
1102     }
1103
1104     if (!getBccValidity()) {
1105         std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1106         if (m_mail->full_address_bcc) {
1107             free(m_mail->full_address_bcc);
1108         }
1109         m_mail->full_address_bcc = String::strdup(addressLine);
1110         setBccValidity(true);
1111     }
1112
1113     LOG_EXIT
1114 }
1115
1116 void Email::updateFrom()
1117 {
1118     LOG_ENTER
1119
1120     if (getFromValidity()) { return; }
1121
1122     if (!m_mail) {
1123         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1124     }
1125
1126     m_mail->account_id = m_accountId;
1127
1128     if (m_mail->full_address_from) {
1129           free(m_mail->full_address_from);
1130     }
1131     m_mail->full_address_from = String::strdup(getFrom());
1132
1133     setFromValidity(true);
1134
1135     LOG_EXIT
1136 }
1137
1138 void Email::updateAttachments()
1139 {
1140     LOG_ENTER
1141
1142     if (isAttachmentsValid()) { return; }
1143
1144     if (!m_mail) {
1145         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1146     }
1147
1148     email_attachment_data_t* attachment = NULL;
1149     int attachmentCount = 0;            
1150     email_mailbox_t* mailBox = NULL;
1151     int error = 0;
1152
1153     Try {
1154                  //clean attachment
1155                 LoggerD("update attachments, msgId=" << getIdRef());
1156                 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1157                 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);        //get Attachment list
1158                 if (EMAIL_ERROR_NONE != error) {
1159                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1160                 }
1161                 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1162                 
1163                 if (EMAIL_ERROR_NONE != error) {
1164                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1165                         "Couldn't get mailbox. [" << error << "]");
1166                 }       
1167                  
1168                 if ( attachmentCount > 0 )
1169                 {
1170                         LoggerD("attachmentCount=" << attachmentCount);
1171                         int i = 0;
1172                         std::stringstream stream;
1173                         for ( i = 0; i < attachmentCount; i++)
1174                         {
1175                                 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1176                                 int attachmentId = attachment[i].attachment_id;
1177
1178                                 int error = email_delete_attachment(attachmentId);
1179                                 if (EMAIL_ERROR_NONE != error) {
1180                                     LoggerD("Error Num = " << error);
1181                                     ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1182                                              "Error while adding attachment. [" << error << "]");
1183                                 }
1184                                 stream.str("");
1185                         }
1186
1187                         if ( attachment )
1188                         {
1189                                 error = email_free_attachment_data(&attachment, attachmentCount);
1190                                 if (EMAIL_ERROR_NONE != error) {
1191                                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1192                                 }
1193                         }
1194
1195                 }
1196                 
1197                 //set Attachment
1198                 std::size_t attachmentSize = getAttachmentsCount();
1199                 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1200                 
1201                 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1202                 if (attachmentSize > 0)
1203                 {
1204                         email_attachment_data_t* attachment_info = NULL;
1205                         for (std::size_t i = 0; i < attachmentSize; ++i)
1206                         {
1207                                 IAttachmentPtr att = getAttachment(i);
1208                                 if (!att) {
1209                                         LoggerD("skip att" << i);
1210                                         continue;
1211                                 }
1212
1213                                 //copy attachment file
1214                                 std::stringstream cp_cmd;
1215                                 char buf[] = "/tmp/XXXXXX";
1216                                 mode_t mask = umask(S_IWGRP | S_IWOTH);
1217                                 error = mkstemp(buf);
1218                                 umask(mask);
1219
1220                                 cp_cmd << COMMAND_NAME;
1221                                 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1222                                 cp_cmd << " \"" << att->getFullPath() << "\"";
1223                                 cp_cmd << " \"" << buf << "\"";
1224
1225                                 attachment_info = EmailService::alloc<email_attachment_data_t>();                                       
1226                                 attachment_info->attachment_name = String::strdup(att->getShortName());
1227                                 attachment_info->attachment_path = String::strdup( buf );
1228                                 attachment_info->save_status = true;
1229                                 attachment_info->inline_content_status = 0;
1230
1231                                 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1232                                 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1233                                 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1234                                 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1235
1236                                 LoggerD("Copy Command=" << cp_cmd.str());
1237                                 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1238                                         LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1239                                         if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1240                                                 LoggerD("buf=" << buf);
1241                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1242                                         }
1243                                 }
1244
1245 /*
1246                                 int result = system(cp_cmd.str().c_str());
1247                                 if (-1 != result) {
1248                                         if (0 != WIFEXITED(result)) {
1249                                                 if (0 != WEXITSTATUS(result)) {
1250                                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1251                                                 }
1252                                         } else {
1253                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1254                                                 "Command terminated abnormally.");
1255                                         }
1256                                 } else {
1257                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1258                                 }
1259 */
1260
1261                                 LoggerD("add Attachment");
1262                                 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1263                                 if (EMAIL_ERROR_NONE != error) {
1264                                         LoggerD("error :" << error);
1265                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1266                                 }
1267                                 else 
1268                                 {       
1269                                         LoggerD(" attachment id : " << attachment_info->attachment_id);
1270                                         LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1271                                         LoggerD(" setNth(i+1) : " << i+1);
1272                                         att->setDownloaded(true);
1273                                         att->setIsInlineAttachment(FALSE);
1274                                         att->setAttachmentID(attachment_info->attachment_id);
1275                                         att->setMessage(SharedFromThis());
1276                                         att->setNth(i+1);
1277                                 }
1278                                 
1279                                 if (attachment_info)
1280                                         EmailService::freeAttachment(attachment_info);
1281                         }       
1282                         
1283                 }
1284                 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1285                 if (inlineAttachmentSize > 0) 
1286                 {
1287                         email_attachment_data_t* attachment_info = NULL;
1288                         for (std::size_t i = 0; i < inlineAttachmentSize; ++i) 
1289                         {
1290                                 IAttachmentPtr att = getInlineAttachment(i);
1291                                 if (!att) {
1292                                         continue;
1293                                 }
1294
1295                                 //copy attachment file
1296                                 std::stringstream cp_cmd;
1297                                 char buf[] = "/tmp/XXXXXX";
1298                                 mode_t mask = umask(S_IWGRP | S_IWOTH);
1299                                 error = mkstemp(buf);
1300                                 umask(mask);
1301
1302                                 cp_cmd << COMMAND_NAME;
1303                                 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1304                                 cp_cmd << " \"" << att->getFullPath() << "\"";
1305                                 cp_cmd << " \"" << buf << "\"";
1306
1307                                 attachment_info = EmailService::alloc<email_attachment_data_t>();                                       
1308                                 attachment_info->attachment_name = String::strdup(att->getShortName());
1309                                 attachment_info->attachment_path = String::strdup( buf );
1310                                 attachment_info->save_status = true;
1311                                 attachment_info->inline_content_status = 1;
1312                                 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1313
1314                                 LoggerD("Copy Command=" << cp_cmd.str());
1315                                 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1316                                         LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1317                                         if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1318                                                 LoggerD("buf=" << buf);
1319                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1320                                         }
1321                                 }
1322
1323 /*
1324                                 int result = system(cp_cmd.str().c_str());
1325                                 if (-1 != result) {
1326                                         if (0 != WIFEXITED(result)) {
1327                                                 if (0 != WEXITSTATUS(result)) {
1328                                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1329                                                 }
1330                                         } else {
1331                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1332                                                 "Command terminated abnormally.");
1333                                         }
1334                                 } else {
1335                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1336                                 }
1337 */
1338
1339
1340                                 LoggerD("add Attachment");
1341                                 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1342                                 if (EMAIL_ERROR_NONE != error) {
1343                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1344                                      "Error while adding attachment. [" << error << "]");
1345                                 }
1346                                 else 
1347                                 {       
1348                                         LoggerD(" attachment id : " << attachment_info->attachment_id);                         
1349                                         LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1350                                         LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1351                                         att->setDownloaded(true);
1352                                         att->setIsInlineAttachment(TRUE);
1353                                         att->setAttachmentID(attachment_info->attachment_id);
1354                                         att->setMessage(SharedFromThis());
1355 //                                      att->setNth(i+1);
1356                                         att->setNth(i + (int)attachmentSize + 1);
1357                                 }
1358                                 
1359                                 if (attachment_info)
1360                                         EmailService::freeAttachment(attachment_info);
1361                         }       
1362                         
1363                 }
1364         
1365
1366         
1367         } 
1368         Catch(WrtDeviceApis::Commons::PlatformException) {
1369            if (attachment) 
1370            {
1371                 int error = email_free_attachment_data(&attachment, attachmentCount);
1372                 if (EMAIL_ERROR_NONE != error) {
1373                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1374                                      "Error while adding attachment data [" << error << "]");
1375                 }
1376
1377                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1378                                      "Error attachment Update");
1379            }
1380         }
1381
1382     LOG_EXIT
1383 }
1384
1385 void Email::updatePriority()
1386 {
1387     LOG_ENTER
1388
1389     if (isPriorityValid()) { return; }
1390
1391     if (!m_mail) {
1392         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1393     }
1394
1395     m_mail->priority = EmailConverter::toMailPriority( getPriority());
1396     setPriorityValid(true);
1397
1398     LOG_EXIT
1399 }
1400
1401 int Email::getIntId() const
1402 {
1403     return convertId(getIdRef());
1404 }
1405
1406 }
1407 }