Update change log and spec for wrt-plugins-tizen_0.4.38
[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     LoggerD(" " << m_mail->date_time);
460     tm* time = localtime(&m_mail->date_time);
461     if (!time) {
462         LoggerE("localtime failed");
463         Throw(WrtDeviceApis::Commons::PlatformException);
464     }
465     setDateTime(*time);
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(EmailUtils::stripAddress(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                                         if(attachment[i].attachment_mime_type)
613                                         {
614                                                 tmpAtt->setMimeType(attachment[i].attachment_mime_type);
615                                         }
616                                         tmpAtt->setNth(i+1);
617                                         LoggerD(" setNth(i+1) : " << i+1);
618                                         if(attachment[i].inline_content_status == 0)
619                                         {
620                                         appendAttachment(tmpAtt);
621                                         }
622                                         else
623                                         {
624                                                 appendInlineAttachment(tmpAtt);
625                                         }
626                                 }
627                                 LoggerD(" append Attachment complete");
628
629
630                         }
631                         catch ( const WrtDeviceApis::Commons::Exception& ex )
632                         {
633                                 LoggerE("Error while trying to append attachment " << attachment[i].attachment_path << ": " << ex.DumpToString());
634                         }
635
636                 }
637
638         }
639
640         if (attachment)
641         {
642                 error = email_free_attachment_data(&attachment, attachmentCount);
643                 if (EMAIL_ERROR_NONE != error) {
644                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
645                 }
646         }
647
648         LOG_EXIT
649 }
650
651 void Email::readInfo()
652 {
653         LOG_ENTER
654
655         if (!m_mail)
656         {
657                 LoggerD("m_mail is NULL");
658                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Info is NULL.");
659         }
660
661         setReadStatus(m_mail->flags_seen_field == 1);
662
663         m_accountId = m_mail->account_id;
664         setConvId(m_mail->thread_id);
665         setPriority( EmailConverter::toMessagePriority( m_mail->priority ) );
666         setSize(m_mail->mail_size);
667         setCurrentFolderId(m_mail->mailbox_id);
668
669         LOG_EXIT
670 }
671
672 void Email::updateBody()
673 {
674         LOG_ENTER
675
676         int error = 0;
677         bool locationChecker = false;
678
679         if (isBodyValid()) { return; }
680
681         if (!m_mail) {
682                 LoggerD("m_mail is NULL");
683                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
684         }
685         if (!m_mail->file_path_plain) {
686                 LoggerD("file_path_plain is NULL");
687                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body is NULL.");
688         }
689
690         // For Plain Body
691         locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_plain);
692         if(locationChecker == true)     // already exist palin body file on email frame work, we can not update that file so make new temp file
693         {
694                 LoggerD("create new plain body");
695                 char buf[] = "XXXXXX";
696                 mode_t mask = umask(S_IWGRP | S_IWOTH);
697                 error = mkstemp(buf);
698                 umask(mask);
699                 std::string bodyFile = buf;
700                 bodyFile = EmailUtils::TMP_DIR_PREFIX+bodyFile;
701                 LoggerD("bodyFile : " << bodyFile);
702
703                 FILE* f = fopen(bodyFile.c_str(), "w");
704                 if (NULL != f) {
705                         fclose(f);
706                 }
707
708                 m_mail->file_path_plain = strdup(bodyFile.c_str());
709         }
710
711         FILE* f = fopen(m_mail->file_path_plain, "w");
712         if (NULL != f) {
713                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
714                 fclose(f);
715         }
716         else
717         {
718                 LoggerD("Plain Body file open fail : " << m_mail->file_path_plain);
719                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Plain Body File Open Error");
720         }
721
722         // For Html Body
723         locationChecker = EmailUtils::isFrameworkEmailData(m_mail->file_path_html);
724         if((locationChecker == true) || (!m_mail->file_path_html))      // html file is not exist or exist on email frame work then create tmp html file
725         {
726                 LoggerD("create new html body");
727                 char buf[] = "XXXXXX";
728                 mode_t mask = umask(S_IWGRP | S_IWOTH);
729                 error = mkstemp(buf);
730                 umask(mask);
731                 std::string htmlFile = buf;
732                 htmlFile = EmailUtils::TMP_DIR_PREFIX+htmlFile;
733                 LoggerD("htmlFile : " << htmlFile);
734
735                 FILE* f = fopen(htmlFile.c_str(), "w");
736                 if (NULL != f) {
737                         fclose(f);
738                 }
739                 m_mail->file_path_html = strdup(htmlFile.c_str());
740
741                 if(!m_mail->file_path_html)
742                 {
743                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Html Body file is NULL.");
744                 }
745         }
746
747         f = fopen(m_mail->file_path_html, "w");
748         if (NULL != f) {
749                 if(strlen(getHtmlBody().c_str()) > 0)   // check html data is exist if not exist copy plain body to html body
750                 {
751                         fwrite(getHtmlBody().c_str(), strlen(getHtmlBody().c_str()), 1, f);
752                 }
753                 else
754                 {
755                         // htmlBody is empty.
756                 }
757                 fclose(f);
758         }
759         else
760         {
761                 LoggerD("html Body file open fail : " << m_mail->file_path_html);
762                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "html Body File Open Error");
763         }
764
765         m_mail->body_download_status = 1;
766
767         setBodyValidity(true);
768
769         LOG_EXIT
770
771 }
772
773 void Email::updateSubject()
774 {
775     LOG_ENTER
776
777     if (isSubjectValid()) { return; }
778
779     LoggerD("update subject, msgId=" << getIdRef());
780
781     if (!m_mail) {
782         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
783     }
784
785     m_mail->subject = String::strdup(getSubjectRef());
786     setSubjectValidity(true);
787
788     LOG_EXIT
789 }
790
791 void Email::updateReadStatus()
792 {
793     LOG_ENTER
794
795     if (isReadStatusValid()) { return; }
796
797     LoggerD("update read, msgId=" << getIdRef());
798
799     if (!m_mail) {
800         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
801     }
802
803     m_mail->flags_seen_field = isRead();
804     setReadStatusValidity(true);
805
806     LOG_EXIT
807 }
808
809 void Email::updateIsRead()
810 {
811     LOG_ENTER
812     if (isReadChangeStatusValid()) {
813         // do not update if not changed
814         return;
815     }
816
817     EmailService::updateSeenFlag(getAccountID(), getIntId(), isReadChangeStatus());
818     setisReadChangeStatusValidity(true);
819     LOG_EXIT
820 }
821
822 void Email::updateMessage()
823 {
824         LOG_ENTER
825         email_meeting_request_t *meeting_req = NULL;
826
827         DPL::Mutex::ScopedLock mx(&m_updateMutex);
828
829         if (!m_mail) {
830                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
831         }
832
833         LoggerD("getCurrentFolder() = " << getCurrentFolder() );
834         LoggerD("m_mail->mail_id = " << m_mail->mail_id );
835         LoggerD("m_mail->file_path_plain = " << m_mail->file_path_plain );
836         LoggerD("m_mail->file_path_html = " << m_mail->file_path_html );
837
838         if(getCurrentFolder() == DRAFTBOX)
839         {
840                 updateBody();
841                 updateSubject();
842                 updateRecipients();
843 //              updateFrom();
844 //              updateAttachments();
845                 updatePriority();
846         }
847         updateReadStatus();
848
849         //get attachment
850         email_attachment_data_t* attachment=NULL;
851         int attachmentCount = 0;
852         int error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);
853         if (EMAIL_ERROR_NONE != error) {
854                 LoggerW("email_get_attachment_data_list [" << error << "]");
855         }
856
857         error = email_get_meeting_request(m_mail->mail_id, &meeting_req);
858         if (EMAIL_ERROR_NONE != error) {
859                 LoggerW("email_get_meeting_request() failed [%d]\n" << error);
860         }
861
862         error = email_update_mail( m_mail.Get(), attachment, attachmentCount, meeting_req, 0);
863         if (EMAIL_ERROR_NONE != error) {
864                 LoggerW("Nothing to update or error. [" << error << "]");
865         }
866
867         if(getCurrentFolder() == DRAFTBOX)
868         {
869         
870                 updateAttachments();
871                 
872                 email_mail_data_t* result = NULL;
873                 
874                 error = email_get_mail_data(m_mail->mail_id, &result);
875                 if (EMAIL_ERROR_NONE != error) {
876                         LoggerW("email_get_mail_data error. [" << error << "]");
877                 }
878                 else
879                 {
880                         if(result->body_download_status != 1)
881                         {
882                                 LoggerD("result->body_download_status " << result->body_download_status);
883                                 int mail_id_count = 1;
884                                 int mail_id_array[1];
885                                 email_mail_attribute_type attribute_type;
886                                 email_mail_attribute_value_t attribute_value;
887
888                                 mail_id_array[0] = result->mail_id;
889                                 LoggerD("result->mail_id " << result->mail_id);
890                                 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
891                                 attribute_value.integer_type_value = 1;
892
893                                 error =  email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
894                                 if (EMAIL_ERROR_NONE != error) {
895                                         LoggerW("email_update_mail_attribute error. [" << error << "]");
896                                 }
897                         }
898                 }
899                 if(result != NULL)
900                 {
901                         error = email_free_mail_data(&result, 1);
902                         if (EMAIL_ERROR_NONE != error) {
903                                 LoggerW("email_free_mail_data error. [" << error << "]");
904                         }
905                 }
906         }
907
908         if(meeting_req) {
909                 email_free_meeting_request(&meeting_req, 1);
910         }
911
912         if (attachment)
913         {
914                 error = email_free_attachment_data(&attachment, attachmentCount);
915                 if (EMAIL_ERROR_NONE != error) {
916                         LoggerW("email_free_attachment_data [" << error << "]");
917                 }
918         }
919
920         LOG_EXIT
921 }
922
923 void Email::createSendMessage()
924 {
925         LOG_ENTER
926
927     MailSender::getInstance(); //start email service
928         LoggerD("getUID:  " << getUID());
929         if ( getUID() > 0 && m_accountId > 0)
930         {
931                 update();
932         }
933         else
934         {
935         DPL::Mutex::ScopedLock mx(&m_updateMutex);
936
937                 EmailAccountInfo account = getEmailAccount();
938                 m_accountId = account.getIntId();       //set account ID
939                 LoggerD("account ID : " << m_accountId);
940                 m_mail.Reset(EmailService::createMailData(account));
941
942                 updateSubject();
943                 updateBody();
944                 updateRecipients();
945                 updatePriority();
946                 updateReadStatus();
947
948                 email_mailbox_t* mailbox;
949                 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_OUTBOX, &mailbox );
950                 if (EMAIL_ERROR_NONE != error) {
951                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
952                         "Couldn't add message to mailbox. [" << error << "]");
953                 }
954                 if ( mailbox->mailbox_id )
955                         m_mail->mailbox_id = mailbox->mailbox_id;
956                 LoggerD("mail MailBox id :" << m_mail->mailbox_id);
957
958                 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
959                 if (EMAIL_ERROR_NONE != error) {
960                        ThrowMsg(WrtDeviceApis::Commons::PlatformException,
961                                 "Couldn't add message to mailbox. [" << error << "]");
962                 }
963
964                 LoggerD("m_mail->mail_id =" << m_mail->mail_id);
965
966                 setId(convertId(m_mail->mail_id));
967                 setFolderType(OUTBOX);
968                 setMessageStatus(MESSAGE_STATUS_SENDING);
969
970                 updateAttachments();    //update Attachment.
971
972                 error = email_free_mailbox(&mailbox, 1);
973                 if (EMAIL_ERROR_NONE != error) {
974                         LoggerE("Failed to destroy mailbox: " << error);
975                 }
976
977                 LoggerD("m_mail->from" << m_mail->full_address_from);
978
979                 email_mail_data_t* result = NULL;
980
981                 error = email_get_mail_data(m_mail->mail_id, &result);
982                 if (EMAIL_ERROR_NONE != error) {
983                     ThrowMsg(WrtDeviceApis::Commons::PlatformException,
984                              "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
985                 }
986
987                 if (m_mail->file_path_plain)
988                         free(m_mail->file_path_plain);
989                 m_mail->file_path_plain = strdup(result->file_path_plain);
990
991                 if ( m_mail->file_path_html)
992                 {
993                         free(m_mail->file_path_html);
994                 }
995
996                 if(result->file_path_html)
997                 {
998                         m_mail->file_path_html = strdup(result->file_path_html);
999                 }
1000
1001                 if (result->full_address_from) {
1002                         Recipients from;
1003                         from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1004                         setSourceAddress(from);
1005                         setSourceAddressValidity(true); //not needed to update in platform
1006                 }
1007                 setCurrentFolderId(result->mailbox_id);
1008
1009                 error = email_free_mail_data(&result, 1);
1010                 if (EMAIL_ERROR_NONE != error) {
1011                     ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1012                              "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1013                 }
1014
1015     }
1016         LOG_EXIT
1017 }
1018
1019
1020 void Email::addMessageToDraft()
1021 {
1022     LOG_ENTER
1023
1024     MailSender::getInstance(); //start email service
1025
1026         LoggerD("getUID:  " << getUID());
1027     if ( getUID() > 0 && m_accountId > 0)
1028     {
1029                 update();
1030     }
1031     else
1032     {
1033                 DPL::Mutex::ScopedLock mx(&m_updateMutex);
1034                 EmailAccountInfo account = getEmailAccount();
1035                 m_accountId = account.getIntId();       //set account ID
1036                 LoggerD("account ID : " << m_accountId);
1037                 m_mail.Reset(EmailService::createMailData(account));
1038
1039                 updateSubject();
1040                 updateBody();
1041                 updateRecipients();
1042                 updatePriority();
1043                 updateReadStatus();
1044
1045                 email_mailbox_t* mailbox;
1046                 int error = email_get_mailbox_by_mailbox_type(m_accountId, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
1047                 if (EMAIL_ERROR_NONE != error) {
1048                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1049                         "Couldn't add message to mailbox. [" << error << "]");
1050                 }
1051                 if ( mailbox->mailbox_id )
1052                 {
1053                         m_mail->mailbox_id = mailbox->mailbox_id;
1054                         LoggerD("mail MailBox id :" << m_mail->mailbox_id);
1055                 }
1056
1057                 error = email_add_mail(m_mail.Get(), NULL, 0, NULL, 0);
1058                 if (EMAIL_ERROR_NONE != error) {
1059                         LoggerE("Couldn't add message to mailbox");
1060                        ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1061                                 "Couldn't add message to mailbox. [" << error << "]");
1062                 }
1063
1064                 LoggerD("message id =" << m_mail->mail_id);
1065
1066                 setId(convertId(m_mail->mail_id));
1067                 setFolderType(DRAFTBOX);
1068                 setMessageStatus(MESSAGE_STATUS_DRAFT);
1069
1070                 updateAttachments();    //update Attachment.
1071
1072                 error = email_free_mailbox(&mailbox, 1);
1073                 if (EMAIL_ERROR_NONE != error) {
1074                         LoggerE("Failed to destroy mailbox: " << error);
1075                 }
1076
1077                 loadDraftMessage();
1078     }
1079
1080     LOG_EXIT
1081 }
1082
1083 void Email::loadDraftMessage()
1084 {
1085     LOG_ENTER
1086
1087         email_mail_data_t* result = NULL;
1088         
1089         int error = email_get_mail_data(m_mail->mail_id, &result);
1090         if (EMAIL_ERROR_NONE != error) {
1091                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1092                                  "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1093         }
1094
1095         if(result->body_download_status != 1)
1096         {
1097                 LoggerD("result->body_download_status " << result->body_download_status);
1098                 int mail_id_count = 1;
1099                 int mail_id_array[1];
1100                 email_mail_attribute_type attribute_type;
1101                 email_mail_attribute_value_t attribute_value;
1102
1103                 mail_id_array[0] = result->mail_id;
1104                 LoggerD("result->mail_id " << result->mail_id);
1105                 attribute_type = EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS;
1106                 attribute_value.integer_type_value = 1;
1107
1108                 email_update_mail_attribute(m_accountId, mail_id_array, 1, EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, attribute_value);
1109         }
1110
1111     tm* time = localtime(&result->date_time);
1112     if (!time) {
1113         LoggerE("localtime failed");
1114         Throw(WrtDeviceApis::Commons::PlatformException);
1115     }
1116     setDateTime(*time);
1117
1118         setReadStatus(result->flags_seen_field == 1);
1119
1120         m_accountId = result->account_id;
1121         setConvId(result->thread_id);
1122         setPriority( EmailConverter::toMessagePriority( result->priority ) );
1123         setSize(result->mail_size);
1124         setCurrentFolderId(result->mailbox_id);
1125
1126         if (m_mail->file_path_plain)
1127         {
1128                 free(m_mail->file_path_plain);
1129         }
1130         if(result->file_path_plain)
1131         {
1132                 m_mail->file_path_plain = strdup(result->file_path_plain);
1133         }
1134
1135         if ( m_mail->file_path_html)
1136         {
1137                 free(m_mail->file_path_html);
1138         }
1139
1140         if(result->file_path_html)
1141         {
1142                 m_mail->file_path_html = strdup(result->file_path_html);
1143         }
1144
1145         if (result->full_address_from) {
1146                 Recipients from;
1147                 from.setRecipients(EmailUtils::stripAddress(result->full_address_from));
1148                 setSourceAddress(from);
1149                 setSourceAddressValidity(true); //not needed to update in platform
1150                 setFrom(result->full_address_from);
1151         }
1152         m_mail->save_status = result->save_status;
1153
1154         error = email_free_mail_data(&result, 1);
1155         if (EMAIL_ERROR_NONE != error) {
1156                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1157                                  "Couldn't find message " << m_mail->mail_id << ". [" << error << "]");
1158         }
1159
1160     LOG_EXIT
1161 }
1162
1163 void Email::updateRecipients()
1164 {
1165     LOG_ENTER
1166
1167     if (getToValidity() && getCcValidity() && getBccValidity()) { return; }
1168
1169     if (!m_mail) {
1170         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1171     }
1172
1173     if (!getToValidity()) {
1174         std::string addressLine = EmailUtils::formatAddressLine(getToRecipients());
1175         if (m_mail->full_address_to) {
1176             free(m_mail->full_address_to);
1177         }
1178         m_mail->full_address_to = String::strdup(addressLine);
1179         setToValidity(true);
1180     }
1181
1182     if (!getCcValidity()) {
1183         std::string addressLine = EmailUtils::formatAddressLine(getCcRecipients());
1184         if (m_mail->full_address_cc) {
1185             free(m_mail->full_address_cc);
1186         }
1187         m_mail->full_address_cc = String::strdup(addressLine);
1188         setCcValidity(true);
1189     }
1190
1191     if (!getBccValidity()) {
1192         std::string addressLine = EmailUtils::formatAddressLine(getBccRecipients());
1193         if (m_mail->full_address_bcc) {
1194             free(m_mail->full_address_bcc);
1195         }
1196         m_mail->full_address_bcc = String::strdup(addressLine);
1197         setBccValidity(true);
1198     }
1199
1200     LOG_EXIT
1201 }
1202
1203 void Email::updateFrom()
1204 {
1205     LOG_ENTER
1206
1207     if (getFromValidity()) { return; }
1208
1209     if (!m_mail) {
1210         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1211     }
1212
1213     m_mail->account_id = m_accountId;
1214
1215     if (m_mail->full_address_from) {
1216           free(m_mail->full_address_from);
1217     }
1218     m_mail->full_address_from = String::strdup(getFrom());
1219
1220     setFromValidity(true);
1221
1222     LOG_EXIT
1223 }
1224
1225 void Email::updateAttachments()
1226 {
1227     LOG_ENTER
1228
1229     if (isAttachmentsValid()) { return; }
1230
1231     if (!m_mail) {
1232         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail is NULL.");
1233     }
1234
1235     email_attachment_data_t* attachment = NULL;
1236     int attachmentCount = 0;
1237     email_mailbox_t* mailBox = NULL;
1238     int error = 0;
1239
1240     Try {
1241                  //clean attachment
1242                 LoggerD("update attachments, msgId=" << getIdRef());
1243                 LoggerD("m_mail->mail_id=" << m_mail->mail_id);
1244                 error = email_get_attachment_data_list( m_mail->mail_id, &attachment, &attachmentCount);        //get Attachment list
1245                 if (EMAIL_ERROR_NONE != error) {
1246                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, "Couldn't get attachment list: ");
1247                 }
1248                 error = email_get_mailbox_by_mailbox_id(m_mail->mailbox_id, &mailBox);
1249
1250                 if (EMAIL_ERROR_NONE != error) {
1251                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1252                         "Couldn't get mailbox. [" << error << "]");
1253                 }
1254
1255                 if ( attachmentCount > 0 )
1256                 {
1257                         LoggerD("attachmentCount=" << attachmentCount);
1258                         int i = 0;
1259                         std::stringstream stream;
1260                         for ( i = 0; i < attachmentCount; i++)
1261                         {
1262                                 LoggerD("file name:" << attachment[i].attachment_name << " file path :" << attachment[i].attachment_path);
1263                                 int attachmentId = attachment[i].attachment_id;
1264
1265                                 int error = email_delete_attachment(attachmentId);
1266                                 if (EMAIL_ERROR_NONE != error) {
1267                                     LoggerD("Error Num = " << error);
1268                                     ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1269                                              "Error while adding attachment. [" << error << "]");
1270                                 }
1271                                 stream.str("");
1272                         }
1273
1274                         if ( attachment )
1275                         {
1276                                 error = email_free_attachment_data(&attachment, attachmentCount);
1277                                 if (EMAIL_ERROR_NONE != error) {
1278                                         ThrowMsg( WrtDeviceApis::Commons::PlatformException, " attachment_data free error! ");
1279                                 }
1280                         }
1281
1282                 }
1283
1284                 //set Attachment
1285                 std::size_t attachmentSize = getAttachmentsCount();
1286                 std::size_t inlineAttachmentSize = getInlineAttachmentsCount();
1287
1288                 LoggerD("update attachments, attachmentSize=" << attachmentSize);
1289                 if (attachmentSize > 0)
1290                 {
1291                         email_attachment_data_t* attachment_info = NULL;
1292                         for (std::size_t i = 0; i < attachmentSize; ++i)
1293                         {
1294                                 IAttachmentPtr att = getAttachment(i);
1295                                 if (!att) {
1296                                         LoggerD("skip att" << i);
1297                                         continue;
1298                                 }
1299
1300                                 //copy attachment file
1301                                 std::stringstream cp_cmd;
1302                                 char buf[] = "/tmp/XXXXXX";
1303                                 mode_t mask = umask(S_IWGRP | S_IWOTH);
1304                                 error = mkstemp(buf);
1305                                 umask(mask);
1306
1307                                 cp_cmd << COMMAND_NAME;
1308                                 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1309                                 cp_cmd << " \"" << att->getFullPath() << "\"";
1310                                 cp_cmd << " \"" << buf << "\"";
1311
1312                                 attachment_info = EmailService::alloc<email_attachment_data_t>();
1313                                 attachment_info->attachment_name = String::strdup(att->getShortName());
1314                                 attachment_info->attachment_path = String::strdup( buf );
1315                                 attachment_info->save_status = true;
1316                                 attachment_info->inline_content_status = 0;
1317                                 if(att->getMimeType().size() > 0)
1318                                 {
1319                                         attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1320                                 }
1321
1322                                 LoggerD("attachment_info->attachment_name=" << attachment_info->attachment_name);
1323                                 LoggerD("attachment_info->attachment_path=" << attachment_info->attachment_path);
1324                                 LoggerD("attachment_info->save_status=" << attachment_info->save_status);
1325                                 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1326                                 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1327
1328                                 LoggerD("Copy Command=" << cp_cmd.str());
1329                                 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1330                                         LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1331                                         if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1332                                                 LoggerD("buf=" << buf);
1333                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1334                                         }
1335                                 }
1336
1337 /*
1338                                 int result = system(cp_cmd.str().c_str());
1339                                 if (-1 != result) {
1340                                         if (0 != WIFEXITED(result)) {
1341                                                 if (0 != WEXITSTATUS(result)) {
1342                                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1343                                                 }
1344                                         } else {
1345                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1346                                                 "Command terminated abnormally.");
1347                                         }
1348                                 } else {
1349                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1350                                 }
1351 */
1352
1353                                 LoggerD("add Attachment");
1354                                 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1355                                 if (EMAIL_ERROR_NONE != error) {
1356                                         LoggerD("error :" << error);
1357                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error while adding attachment. [" << error << "]");
1358                                 }
1359                                 else
1360                                 {
1361                                         LoggerD(" attachment id : " << attachment_info->attachment_id);
1362                                         LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1363                                         LoggerD(" setNth(i+1) : " << i+1);
1364                                         att->setDownloaded(true);
1365                                         att->setIsInlineAttachment(FALSE);
1366                                         att->setAttachmentID(attachment_info->attachment_id);
1367                                         att->setMessage(SharedFromThis());
1368                                         att->setNth(i+1);
1369                                 }
1370
1371                                 if (attachment_info)
1372                                         EmailService::freeAttachment(attachment_info);
1373                         }
1374
1375                 }
1376                 LoggerD("update attachments, inlineAttachmentSize=" << inlineAttachmentSize);
1377                 if (inlineAttachmentSize > 0)
1378                 {
1379                         email_attachment_data_t* attachment_info = NULL;
1380                         for (std::size_t i = 0; i < inlineAttachmentSize; ++i)
1381                         {
1382                                 IAttachmentPtr att = getInlineAttachment(i);
1383                                 if (!att) {
1384                                         continue;
1385                                 }
1386
1387                                 //copy attachment file
1388                                 std::stringstream cp_cmd;
1389                                 char buf[] = "/tmp/XXXXXX";
1390                                 mode_t mask = umask(S_IWGRP | S_IWOTH);
1391                                 error = mkstemp(buf);
1392                                 umask(mask);
1393
1394                                 cp_cmd << COMMAND_NAME;
1395                                 cp_cmd << " " << COMMAND_SWITCH_RECURSIVE;
1396                                 cp_cmd << " \"" << att->getFullPath() << "\"";
1397                                 cp_cmd << " \"" << buf << "\"";
1398
1399                                 attachment_info = EmailService::alloc<email_attachment_data_t>();
1400                                 attachment_info->attachment_name = String::strdup(att->getShortName());
1401                                 attachment_info->attachment_path = String::strdup( buf );
1402                                 attachment_info->save_status = true;
1403                                 attachment_info->inline_content_status = 1;
1404                                 LoggerD("attachment_info->inline_content_status=" << attachment_info->inline_content_status);
1405                                 if(att->getMimeType().size() > 0)
1406                                 {
1407                                         attachment_info->attachment_mime_type = String::strdup(att->getMimeType());
1408                                 }
1409
1410                                 LoggerD("Copy Command=" << cp_cmd.str());
1411                                 if (EINA_TRUE != ecore_file_is_dir(att->getFullPath().c_str())) {
1412                                         LoggerD("att->getFullPath().c_str()=" << att->getFullPath().c_str());
1413                                         if (EINA_TRUE != ecore_file_cp(att->getFullPath().c_str(), buf)) {
1414                                                 LoggerD("buf=" << buf);
1415                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to copy file");
1416                                         }
1417                                 }
1418                                 LoggerD("attachment_info->attachment_mime_type=" << attachment_info->attachment_mime_type);
1419
1420 /*
1421                                 int result = system(cp_cmd.str().c_str());
1422                                 if (-1 != result) {
1423                                         if (0 != WIFEXITED(result)) {
1424                                                 if (0 != WEXITSTATUS(result)) {
1425                                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed.");
1426                                                 }
1427                                         } else {
1428                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1429                                                 "Command terminated abnormally.");
1430                                         }
1431                                 } else {
1432                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command.");
1433                                 }
1434 */
1435
1436
1437                                 LoggerD("add Attachment");
1438                                 int error = email_add_attachment(m_mail->mail_id, attachment_info);
1439                                 if (EMAIL_ERROR_NONE != error) {
1440                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1441                                      "Error while adding attachment. [" << error << "]");
1442                                 }
1443                                 else
1444                                 {
1445                                         LoggerD(" attachment id : " << attachment_info->attachment_id);
1446                                         LoggerD(" attachment inline : " << attachment_info->inline_content_status);
1447                                         LoggerD(" setNth(i+(int)attachmentSize+1) : " << i+(int)attachmentSize+1);
1448                                         att->setDownloaded(true);
1449                                         att->setIsInlineAttachment(TRUE);
1450                                         att->setAttachmentID(attachment_info->attachment_id);
1451                                         att->setMessage(SharedFromThis());
1452 //                                      att->setNth(i+1);
1453                                         att->setNth(i + (int)attachmentSize + 1);
1454                                 }
1455
1456                                 if (attachment_info)
1457                                         EmailService::freeAttachment(attachment_info);
1458                         }
1459
1460                 }
1461         }
1462         Catch(WrtDeviceApis::Commons::PlatformException) {
1463            if (attachment)
1464            {
1465                 int error = email_free_attachment_data(&attachment, attachmentCount);
1466                 if (EMAIL_ERROR_NONE != error) {
1467                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1468                                      "Error while adding attachment data [" << error << "]");
1469                 }
1470
1471                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1472                                      "Error attachment Update");
1473            }
1474         }
1475
1476     LOG_EXIT
1477 }
1478
1479 void Email::updatePriority()
1480 {
1481     LOG_ENTER
1482
1483     if (isPriorityValid()) { return; }
1484
1485     if (!m_mail) {
1486         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Mail not allocated.");
1487     }
1488
1489     m_mail->priority = EmailConverter::toMailPriority( getPriority());
1490     setPriorityValid(true);
1491
1492     LOG_EXIT
1493 }
1494
1495 int Email::getIntId() const
1496 {
1497     return convertId(getIdRef());
1498 }
1499
1500 }
1501 }