Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / Messaging / MailSync.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 <utility>
19 #include <email-types.h>
20 #include <email-api.h>
21 #include <email-api-network.h>
22 #include <email-api-init.h>
23 #include <email-api-account.h>
24
25 #include <dpl/log/log.h>
26 #include <dpl/assert.h>
27 #include <Commons/Exception.h>
28 #include <Commons/ThreadPool.h>
29 #include "ReqReceiverMessage.h"
30 #include "EventMessagingService.h"
31 #include "IAttachment.h"
32
33 #include "MailSync.h"
34 #include "SyncNetworkStatus.h"
35 #include "IMessage.h"
36 //for vconf
37 #include <vconf.h>
38 #include <vconf-keys.h>
39
40 namespace {
41 const char* DBUS_FILTER_NETWORK_STATUS =
42     "type='signal',interface='User.Email.NetworkStatus'";
43 //const char* DBUS_FILTER_EMAIL_RECEIVED =
44 //    "type='signal',interface='User.Email.StorageChange'";
45 }
46
47 namespace DeviceAPI {
48 namespace Messaging {
49
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
52
53 MailSync& MailSync::getInstance()
54 {
55     static MailSync instance;
56     return instance;
57 }
58
59 MailSync::MailSync() :
60     m_dbusThread(new DPL::Thread()),
61     m_dbus(new DBus::Connection())
62 {
63     m_dbusThread->Run();
64
65     m_dbus->setWorkerThread(m_dbusThread.Get());
66     m_dbus->AddListener(this);
67     m_dbus->SwitchAllListenersToThread(
68         ThreadPool::getInstance().getThreadRef(ThreadEnum::MESSAGING_THREAD)
69         );
70     m_dbus->addFilter(DBUS_FILTER_NETWORK_STATUS);
71     m_dbus->open(DBUS_BUS_SYSTEM);
72
73     //start email service
74     int error = email_service_begin();
75         if (error == EMAIL_ERROR_NONE) {
76             LogDebug("Email service Begin\n");
77             if (EMAIL_ERROR_NONE == email_open_db()) {
78                     LogDebug("Email open DB success\n");
79             }
80             else{
81                     LogDebug("Email open DB failed\n");
82         }
83         }
84         else{
85             LogDebug("Email service not started\n");
86             LogDebug("Error : " << error);
87         }
88
89         //get retriving count
90         int slot_size = -1;
91         vconf_get_int("db/private/email-service/slot_size", &(slot_size));
92         if ( slot_size > 0 )
93                 m_default_slot_size = slot_size;
94
95         LogDebug( "Slot Size : " << m_default_slot_size );
96         
97 }
98
99 MailSync::~MailSync()
100 {
101     m_dbus->RemoveListener(this);
102     m_dbus->close();
103
104     m_dbusThread->Quit();
105
106     //close email service
107         if (EMAIL_ERROR_NONE == email_close_db()) {
108                 LogDebug("Email Close DB Success\n");
109                 if (EMAIL_ERROR_NONE == email_service_end())
110                 {
111                 LogDebug("Email service close Success\n");
112                 }
113                 else
114                 {
115                 LogDebug("Email service end failed\n");
116         }
117         }
118         else{
119                 LogDebug("Email Close DB failed\n");
120 }
121 }
122
123 int MailSync::downloadBody( const IEmailPtr& mail )
124 {       
125         if ( mail )
126         {       
127                 int mailId = mail->convertId(mail->getIdRef());         // get mail id
128                 LogDebug("start attachment, mail Id : " << mail->getIdRef());
129                 SyncRequestIterator it;
130                 int synType;
131                 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
132                 {
133                         synType = it->second.syncType;
134                         LogDebug("synType : " << synType);
135
136                         if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY )
137                         {
138                                 LogDebug("skip");
139                                 continue;
140                         }
141                         const IEmailPtr& email = it->second.mail;
142                         LogDebug ( "Requests mail ID:" <<  mail->convertId(email->getIdRef()) );
143                         if ( mailId == mail->convertId(email->getIdRef()))
144                         {
145                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
146                                         "download Body: " << mailId << " already requested to be download Body.");
147                         }
148                 }               
149         }
150         
151         return downloadBodyInternal(mail, mail->getAccountID());
152         
153 }
154
155 void MailSync::cancelDownloadBody(int handle)
156 {
157         Assert(handle > -1);
158
159         SyncRequestIterator it = m_SyncRequests.find(handle);
160         if ( m_SyncRequests.end() != it)
161         {
162                 if (  it->second.messagingService )
163                         cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
164                 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
165         }
166         else
167         {
168                 LogDebug("Don't find cancel DownloadBody handle : " << handle);
169                 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
170         }
171         
172 }
173
174 void MailSync::cancelDownloadAttachment(int handle)
175 {
176         Assert(handle > -1);
177
178         SyncRequestIterator it = m_SyncRequests.find(handle);
179         if ( m_SyncRequests.end() != it)
180         {
181                 if (  it->second.messagingService )
182                         cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
183                 //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
184         }
185         else
186         {
187                 LogDebug("Don't find cancel DownloadAttachment handle : " << handle);
188         }
189         
190 }
191
192 int MailSync::downloadAttachment(const IEmailPtr& mail, const IAttachmentPtr& attachment)
193 {       
194         if ( mail && attachment )
195         {       
196                 //int mailId = mail->convertId(mail->getIdRef());               // get mail id
197                 LogDebug("start attachment, mail Id : " << mail->getIdRef());
198                 SyncRequestIterator it;
199                 int synType;
200                 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
201                 {
202                         synType = it->second.syncType;
203                         LogDebug("synType : " << synType);
204
205                         if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT )
206                         {
207                                 LogDebug("skip");
208                                 continue;
209                         }
210
211                         const IAttachmentPtr& iattachment = it->second.attachment;
212                         LogDebug ( "Requests Attachment ID:" <<  iattachment->getAttachmentID() << " Input Attachment ID: "  << attachment->getAttachmentID());
213                         if (iattachment->getAttachmentID() == attachment->getAttachmentID())
214                         {
215                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
216                                         "Attachment: " << attachment->getAttachmentID() << " already requested to be download Attachment.");
217                         }
218                 }
219         }
220
221         return downloadAttachmentInternal(mail, mail->getAccountID(), attachment);
222
223 }
224
225 int MailSync::syncAccount(const IMessagingServicePtr& messagingService, const int limit)
226 {
227         LogDebug("syncAccount : " << limit);
228         if (messagingService)
229         {
230                 int accountId = messagingService->getAccountID();
231                 LogDebug("start sync, account Id : " << accountId);
232                 SyncRequestIterator it;
233                 int synType;
234                 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
235                 {
236                         synType = it->second.syncType;
237                         LogDebug("synType : " << synType);
238                         
239                         if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC )
240                         {
241                                 LogDebug("skip");
242                                 continue;
243                         }
244                 
245                         const IMessagingServicePtr& messagingService = it->second.messagingService;
246                         if (messagingService && messagingService->getAccountID() == accountId)
247                         {
248                                 LogDebug("accountId is sync requested already");
249 //                              ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
250                         }
251                 }
252                 return syncAccountInternal(messagingService, limit);
253         }
254         else
255         {
256                 LogDebug("messagingService is NULL");
257         }
258
259         LogDebug("end sync" );
260         return -1;
261 }       
262
263 int MailSync::syncAccountInternal( const IMessagingServicePtr& messagingService, const int limit)
264 {
265         LogDebug("syncAccountInternal limit: " << limit);
266         
267         int account_id = messagingService->getAccountID();
268         int err = 0;
269 //      email_mailbox_t mailbox ;
270         int email_handle = 0;
271         
272 //      memset(&mailbox, 0, sizeof(email_mailbox_t));
273 //      mailbox.mailbox_name = NULL;                            /* all folders. */
274 //      mailbox.account_id = account_id; //set account id.
275         int slot_size;
276         
277         if ( limit < 0 )
278                 slot_size = m_default_slot_size;
279         else
280                 slot_size = limit;
281         
282         email_set_mail_slot_size(0, 0, slot_size);
283                         
284 //      LogDebug("mailbox.account_id " << mailbox.account_id );
285 //      err = email_sync_header(&mailbox, &email_handle);
286 //      LogDebug("emf_handle " << emf_handle);
287
288         err = email_sync_header(account_id, 0, &email_handle);
289         LogDebug("email_handle " << email_handle);
290
291         if (err != EMAIL_ERROR_NONE) {
292                 LogDebug("fail to sync all folders - err : " << err);
293         }
294         else
295         {
296                 LogDebug("Insert sync request");
297                 SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
298                 m_SyncRequests.insert(std::make_pair(email_handle, data));
299         }
300
301         return email_handle;
302 }
303
304 void MailSync::syncAccountCancel(const int handle)
305 {
306         LogDebug("sync cancel");
307         Assert(handle > -1);
308
309         SyncRequestIterator it = m_SyncRequests.find(handle);
310         if ( m_SyncRequests.end() != it)
311         {
312                 if (  it->second.messagingService )
313                         cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
314         }
315         else
316         {
317                 LogDebug("Don't find cancel Sync handle : " << handle);
318                 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
319         }       
320 }
321
322 int MailSync::syncFolder(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
323 {
324         LogDebug("sync folder : " << folder_id << " limit : " << limit);
325         
326         if (messagingService)
327         {
328                 int accountId = messagingService->getAccountID();
329                 LogDebug("start sync, account Id : " << accountId);
330                 SyncRequestIterator it;
331                 int synType;
332                 for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
333                 {
334                         synType = it->second.syncType;
335                         LogDebug("synType : " << synType);
336                         
337                         if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER )
338                         {
339                                 LogDebug("skip");
340                                 continue;
341                         }
342                 
343                         const IMessagingServicePtr& messagingService = it->second.messagingService;
344                         if (messagingService && messagingService->getAccountID() == accountId 
345                                 && it->second.folderId == folder_id)
346                         {
347                                 LogDebug("accountId is syncFolder requested already");
348 //                              ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
349                         }
350                 }
351                 
352                 return syncFolderInternal(messagingService, folder_id, limit);
353         }
354         else
355         {
356                 LogDebug("messagingService is NULL");
357         }
358
359         return -1;
360                 
361 }
362
363 void MailSync::syncFolderCancel(const int handle)
364 {
365         LogDebug("sync Folder Cancel");
366         Assert(handle > -1);
367
368         SyncRequestIterator it = m_SyncRequests.find(handle);
369         if ( m_SyncRequests.end() != it)
370         {
371                 if (  it->second.messagingService )
372                         cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
373         }
374         else
375         {
376                 LogDebug("Don't find cancel Sync Folder handle : " << handle);
377                 //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
378         }
379 }
380
381 int MailSync::syncFolderInternal(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
382 {
383         LogDebug("Folder Id=" << folder_id << " limit : " << limit);
384
385         int account_id = messagingService->getAccountID();
386
387         int email_handle = 0;
388         int err = 0;
389         int slot_size = 0;
390         email_mailbox_t* mailbox = NULL;
391         
392 //      char* mailbox_name = strdup(folderName.c_str());
393         
394         err = email_get_mailbox_by_mailbox_id(folder_id, &mailbox);
395         if (EMAIL_ERROR_NONE != err) {
396                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
397                         "Couldn't set mail solt size. [" << err << "]");
398         }       
399
400         if (mailbox == NULL) {
401                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,     "Couldn't get mailbox");
402         }
403         
404 //      char* mailbox_name = strdup(mailbox->mailbox_name);
405         
406         if ( limit < 0 )
407                 slot_size = m_default_slot_size;
408         else
409                 slot_size = limit;
410         
411         err = email_set_mail_slot_size(0, mailbox->mailbox_id, slot_size);
412         if (EMAIL_ERROR_NONE != err) {
413                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
414                         "Couldn't set mail solt size. [" << err << "]");
415         }
416         
417 //      err = email_sync_header(mailbox, &email_handle);
418         err = email_sync_header(account_id, mailbox->mailbox_id, &email_handle);
419         if (err != EMAIL_ERROR_NONE) {
420                 LogDebug("fail to sync folders - err : " << err);
421         }
422         else
423         {
424                 LogDebug("Insert sync request");
425
426                 SyncRequestData data = SyncRequestData(email_handle,    MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER, messagingService, folder_id);
427                 m_SyncRequests.insert(std::make_pair(email_handle, data));
428         }
429
430 //      if (mailbox_name)
431 //              free(mailbox_name);
432
433         if ( mailbox != NULL )
434         {
435                 if  (  EMAIL_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
436                         LogDebug("fail to email_free_mailbox - err ");
437                 mailbox = NULL;
438         }
439         
440         return email_handle;
441 }
442         
443
444 // TODO Copied from former solution, refactor it.
445 void MailSync::OnEventReceived(const DBus::MessageEvent& event)
446 {
447         LogDebug("OnEventReceived");
448         SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
449         int mailId = syncNetworkStatus->getMailId();    //if email body download mode.
450         int status = syncNetworkStatus->getStatus();
451         int handle = syncNetworkStatus->getHandle();
452
453         LogDebug("mailId : " << mailId << " status : " << status << " handle : " << handle);
454         // if Attachment Load
455         if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||status == NOTI_DOWNLOAD_ATTACH_FAIL )
456         {
457                 int nth = handle;
458                 LogDebug(" Debus mailID = " <<  mailId <<  " , Nth = " << nth );
459
460                 SyncRequestIterator it = m_SyncRequests.begin();
461                 for ( ; it != m_SyncRequests.end(); it++ )
462                 {
463                         const IEmailPtr& mail = it->second.mail;
464
465                         LogDebug(" request mailID = " <<  mail->getUID() <<  " , Nth = " << (it->second.attachment)->getNth() );
466                         if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
467                         {
468                                 EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
469                                 if ( mail && requestReceiver )
470                                 {
471                                         EventMessagingServicePtr event = mail->getMessagingServiceEvent();
472 //                                      EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
473
474                                         if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
475                                         {
476                                                 LogDebug(" Attachment Finish " );
477                                                 const IAttachmentPtr& attachment = it->second.attachment;
478                                                 if (attachment)
479                                                 {
480
481                                                         email_attachment_data_t* attachment_data = NULL;
482                                                         int attachmentId = attachment->getAttachmentID();
483                                                         LogDebug("Attachment ID = " << attachmentId);
484
485                                                         int err = email_get_attachment_data(attachmentId, &attachment_data);
486                                                         if (err == EMAIL_ERROR_NONE) {
487                                                                 LogDebug("attachment Name : " << attachment_data->attachment_name);
488                                                                 LogDebug("attachment ID : " << attachment_data->attachment_id);
489                                                                 attachment->init(attachment_data->attachment_path, false);
490                                                         }
491                                                         else
492                                                         {
493                                                                 LogDebug("fail to email_get_attachment_data - err : " << err);
494                                                         }
495
496                                                         if ( attachment_data != NULL )
497                                                         {
498                                                                 email_free_attachment_data(&attachment_data, 1);
499                                                         }
500
501                                                 }
502
503                                         }
504                                         else
505                                         {
506                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
507                                                 LogDebug(" Attachment Failed " );
508                                         }
509
510                                         LogDebug("remove downloadAttachment request");
511                                         LogDebug("handle : " << it->first );
512                                         requestReceiver->ManualAnswer(event);
513                                         m_SyncRequests.erase(it->first);
514
515                                 }
516                                 break;
517                         }
518                 } 
519         }
520         else if ( status == NOTI_DOWNLOAD_ATTACH_START )
521         {
522                 LogDebug("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
523                 return;
524         }
525
526         SyncRequestIterator it = m_SyncRequests.find(handle);
527         if ( m_SyncRequests.end() != it)
528         {
529                 int syncType = it->second.syncType;
530                 LogDebug(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
531
532                 switch(syncType)
533                 {
534                         case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
535                         {
536                                 LogDebug(" Sync Account");
537                                 const IMessagingServicePtr& messagingService = it->second.messagingService;
538                                 EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
539
540                                 if ( messagingService && requestReceiver)
541                                 {
542                                         EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
543                                         // get event using handle from m_opRequests;
544                                         if ( status == NOTI_DOWNLOAD_FINISH )
545                                         {
546                                                 LogDebug("Sync Success");
547                                                 requestReceiver->ManualAnswer(event);
548                                                 m_SyncRequests.erase( handle );
549                                         }
550                                         else if ( status == NOTI_DOWNLOAD_FAIL )
551                                         {
552                                                 LogDebug("Sync Fail");
553                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
554                                                 requestReceiver->ManualAnswer(event);
555                                                 m_SyncRequests.erase( handle );
556                                         }
557                                 }
558                                 else
559                                 {
560                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
561                                 }
562                                 break;
563                         }
564                         case MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER:
565                         {
566                                 LogDebug("Sync Folder");
567                                 const IMessagingServicePtr& messagingService = it->second.messagingService;
568                                 EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
569
570                                 if ( messagingService && requestReceiver)
571                                 {
572                                         EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
573                                         if ( status == NOTI_DOWNLOAD_FINISH )
574                                         {
575                                                 LogDebug("Sync Success");
576                                                         requestReceiver->ManualAnswer(event);
577                                                 m_SyncRequests.erase( handle );
578                                         }
579                                         else if ( status == NOTI_DOWNLOAD_FAIL )
580                                         {
581                                                 LogDebug("Sync Fail");
582                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
583                                                 requestReceiver->ManualAnswer(event);
584                                                 m_SyncRequests.erase( handle );
585                                         }
586                                 }
587                                 else
588                                 {
589                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
590                                 }
591                                 break;
592                         }
593                         case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
594                         {
595                                 LogDebug(" DownLoad Body");
596                                 const IEmailPtr& mail = it->second.mail;        // IEmailPtr
597                                 EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
598                                 if (mail && requestReceiver) {
599                                         EventMessagingServicePtr event = mail->getMessagingServiceEvent();
600 //                                      EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
601                         
602                                         if ( status == NOTI_DOWNLOAD_BODY_FINISH )
603                                         {
604                                                 event->m_message->readAllData();
605                                                 requestReceiver->ManualAnswer(event);
606                                                 m_SyncRequests.erase( handle );
607                                         }
608                                         else if ( status == NOTI_DOWNLOAD_BODY_FAIL )
609                                         {
610                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
611                                                 requestReceiver->ManualAnswer(event);
612                                                 m_SyncRequests.erase( handle );
613                                         }
614                                 }
615                                 else
616                                 {
617                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
618                                 }
619                                 break;
620                         }
621                 }
622         }
623 }
624
625 int MailSync::downloadBodyInternal( const IEmailPtr& mail, int account_id)
626 {
627         
628         LogDebug("downloadInternal");
629
630         int err = 0;
631         int mailId = mail->convertId(mail->getIdRef());
632         int email_handle = 0;
633         
634         LogDebug("folder type = " << mail->getCurrentFolder());
635         LogDebug("mail ID :" << mailId );
636
637         err = email_download_body(mailId, 0, &email_handle);
638         if (err != EMAIL_ERROR_NONE) {
639                 LogDebug("fail to downloadBody - err : " << err);
640                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
641         }
642         else
643         {
644                 LogDebug("Insert downloadBody request");
645                 LogDebug("handle : " << email_handle);
646                 SyncRequestData data = SyncRequestData(email_handle,    MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
647                 m_SyncRequests.insert(std::make_pair(email_handle, data));
648         }
649
650         return email_handle;
651
652 }
653
654 int MailSync::downloadAttachmentInternal(const IEmailPtr& mail, int account_id, const IAttachmentPtr& attachment)
655 {
656         LogDebug("downloadAttachmentInternal");
657         Assert(mail);
658         
659         int err = 0;
660         int nth = 0;
661         int mailId = mail->convertId(mail->getIdRef());
662         int email_handle = 0;
663         
664         email_mail_data_t* result = NULL;
665
666         int error;
667         error = email_get_mail_data(mailId, &result);
668         if (EMAIL_ERROR_NONE != error) {
669                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
670                                  "Couldn't find message " << mailId << ". [" << error << "]");
671         }
672
673         LogDebug("Attachment ID :" << attachment->getAttachmentID());   
674
675         //get nth 
676         std::vector<IAttachmentPtr> attachments = mail->getAttachments();
677         std::vector<IAttachmentPtr> inlineAttachments = mail->getInlineAttachments();
678         unsigned int attachmentSize = attachments.size();
679         unsigned int inlineAttachmentSize = inlineAttachments.size();
680
681         LogDebug( "attachment sizes = " << attachmentSize << ",inline attachment size : " << inlineAttachmentSize);     
682         for ( unsigned int i = 0 ; i < attachmentSize ; i ++)
683         {
684                 LogDebug( "attachment ID is = " << attachments[i]->getAttachmentID());
685                 if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
686                 {
687 //                      nth = i;
688                         nth = attachments[i]->getNth();
689                         break;
690                 }
691         }
692         for ( unsigned int i = 0 ; i < inlineAttachmentSize ; i ++)
693         {
694                 LogDebug( "inline attachment ID is = " << inlineAttachments[i]->getAttachmentID());
695                 if (inlineAttachments[i]->getAttachmentID() == attachment->getAttachmentID())
696                 {
697 //                      nth = i;
698                         nth = inlineAttachments[i]->getNth();
699                         break;
700                 }
701         }
702         
703         LogDebug("nth = " << nth);      
704         LogDebug("attachment Order Index = " << nth);
705         err = email_download_attachment(mailId, nth , &email_handle);
706         if (err != EMAIL_ERROR_NONE) {
707                 LogDebug("fail to downloadAttachment - err : " << err);
708                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
709         }
710         else
711         {
712                 LogDebug("Insert downloadAttachment request");
713                 LogDebug("handle : " << email_handle);
714                 SyncRequestData data = SyncRequestData( email_handle,
715                         MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
716                 m_SyncRequests.insert(std::make_pair(email_handle, data));
717         }
718
719         if ( result )
720                 if  (  EMAIL_ERROR_NONE != email_free_mail_data( &result , 1) )
721                         LogDebug("fail to email_free_mail_data - err ");
722         
723         return email_handle;
724 }
725
726 void MailSync::cancelEmailJobInternal(int accountId, int handle)
727 {
728         LogDebug("cancel Email Job, account : " <<accountId << " handle : " << handle);
729
730         int error = email_cancel_job(accountId, handle,EMAIL_CANCELED_BY_USER);
731         if (EMAIL_ERROR_NONE != error) {
732                 LogDebug("error code : " << error );
733                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
734                  "Couldn't cancel email job handle: " << handle);
735         }
736
737         m_SyncRequests.erase(handle);
738 }
739
740
741 }
742 }