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