8f848900797b46c56e84f83cfbba14312e5e7794
[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 ||
410         status == NOTI_DOWNLOAD_ATTACH_FAIL )
411         {       
412                 int nth = handle;
413                 LogDebug(" Debus mailID = " <<  mailId <<  " , Nth = " << nth );
414                 
415                 SyncRequestIterator it = m_SyncRequests.begin();
416                 for ( ; it != m_SyncRequests.end(); it++ )
417                 {
418                         const Api::Messaging::IEmailPtr& mail = it->second.mail;
419                         
420                         LogDebug(" request mailID = " <<  mail->getUID() <<  " , Nth = " << (it->second.attachment)->getNth() );
421                         if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
422                         {       
423                                 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
424                                 if ( mail && requestReceiver )
425                                 {
426                                         Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
427                                 
428                                         if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
429                                         {
430                                                 LogDebug(" Attachment Finish " );
431                                                 int acountId = mail->getAccountID();
432                                                 const Api::Messaging::IAttachmentPtr& attachment = it->second.attachment;
433                                                 if (attachment)
434                                                 {
435 //                                                      email_mailbox_t mailbox ;
436 //                                                      email_attachment_info_t *attach_info = NULL;
437 //                                                      memset(&mailbox, 0, sizeof(email_mailbox_t));
438 //                                                      mailbox.account_id = acountId; //set account id.
439                                                         //int attachmentId = attachment->getAttachmentID();
440 //                                                      std::stringstream stream;
441 //                                                      stream << attachment->getAttachmentID();
442 //                                                      int err = email_get_attachment_info(mail->getUID(), stream.str().c_str() , &attach_info );
443
444                                                         email_attachment_data_t* attachment_data = NULL;
445                                                         int attachmentId = attachment->getAttachmentID();
446                                                         LogDebug("Attachment ID = " << attachmentId);
447                                                         
448                                                         int err = email_get_attachment_data(attachmentId, &attachment_data);
449                                                         if (err == EMAIL_ERROR_NONE) {
450                                                                 LogDebug("attachment Name : " << attachment_data->attachment_name);
451                                                                 LogDebug("attachment ID : " << attachment_data->attachment_id);
452                                                                 attachment->init(attachment_data->attachment_path, false);
453                                                         }
454                                                         else
455                                                         {
456                                                                 LogDebug("fail to email_get_attachment_data - err : " << err);
457                                                         }
458                                                         
459                                                 }
460                                                 
461                                         }
462                                         else 
463                                         {       
464                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
465                                                 LogDebug(" Attachment Failed " );
466                                         }
467
468                                         requestReceiver->ManualAnswer(event);
469                                         m_SyncRequests.erase( handle );         
470                                 
471                                 }
472
473                         break;
474                         }
475                 } 
476         }
477         else if ( status == NOTI_DOWNLOAD_ATTACH_START ) 
478         {
479                 LogDebug("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
480                 return;
481         }
482
483     SyncRequestIterator it = m_SyncRequests.find(handle);
484     if ( m_SyncRequests.end() != it)
485     {   
486                 int syncType = it->second.syncType;
487                 LogDebug(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
488
489                 switch(syncType)
490                 {
491                         case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
492                         {       
493                                 LogDebug(" Sync Account");
494                                 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
495                                 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
496                                 
497                                 if ( messagingService && requestReceiver)
498                                 {       
499                                         Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
500                                         if ( status == NOTI_DOWNLOAD_FINISH )
501                                         {       
502                                                 LogDebug("Sync Success");
503                                                 requestReceiver->ManualAnswer(event);
504                                                 m_SyncRequests.erase( handle );                                                         
505                                         }
506                                         else if ( status == NOTI_DOWNLOAD_FAIL )
507                                         {       
508                                                 LogDebug("Sync Fail");
509                                         event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
510                                                 requestReceiver->ManualAnswer(event);
511                                                 m_SyncRequests.erase( handle );
512                                         }       
513                                 }
514                                 else
515                                 {
516                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
517                                 }
518                                 
519                                 break;
520                         }
521                         case MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER:
522                         {       
523                                 LogDebug("Sync Folder");
524                                 const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
525                                 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
526                                 
527                                 if ( messagingService && requestReceiver)
528                                 {       
529                                         Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
530                                         if ( status == NOTI_DOWNLOAD_FINISH )
531                                         {       
532                                                 LogDebug("Sync Success");
533                                                 requestReceiver->ManualAnswer(event);
534                                                 m_SyncRequests.erase( handle );                                                         
535                                         }
536                                         else if ( status == NOTI_DOWNLOAD_FAIL )
537                                         {       
538                                                 LogDebug("Sync Fail");
539                                         event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
540                                                 requestReceiver->ManualAnswer(event);
541                                                 m_SyncRequests.erase( handle );
542                                         }
543                                 }
544                                 else
545                                 {
546                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
547                                 }
548                                 break;
549                         }
550                         case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
551                         {       
552                                 LogDebug(" DownLoad Body");
553                                 const Api::Messaging::IEmailPtr& mail = it->second.mail;        // IEmailPtr
554                                 Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
555                                 if (mail && requestReceiver) {
556                                         Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
557                                         if ( status == NOTI_DOWNLOAD_BODY_FINISH )
558                                         {
559                                                 requestReceiver->ManualAnswer(event);
560                                                 m_SyncRequests.erase( handle );
561                                         }
562                                         else if ( status == NOTI_DOWNLOAD_BODY_FAIL )
563                                         {
564                                                 event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
565                                                 requestReceiver->ManualAnswer(event);
566                                                 m_SyncRequests.erase( handle );
567                                         }              
568                         }
569                                 else
570                                 {
571                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
572                                 }
573                                 
574                                 break;
575                         }
576                         
577                 }
578
579                 
580         }
581                                 
582 }
583
584 int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int account_id)
585 {
586         
587         LogDebug("downloadInternal");
588
589         int err = 0;
590         int mailId = mail->convertId(mail->getIdRef());
591         unsigned int email_handle = 0;
592         
593         LogDebug("folder type = " << mail->getCurrentFolder());
594         LogDebug("mail ID :" << mailId );
595
596         err = email_download_body(mailId, 0, &email_handle);
597         if (err != EMAIL_ERROR_NONE) {
598                 LogDebug("fail to downloadBody - err : " << err);
599                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
600         }
601         else
602         {
603                 LogDebug("Insert downloadBody request");
604                 LogDebug("handle : " << email_handle);
605                 SyncRequestData data = SyncRequestData(email_handle,    MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
606                 m_SyncRequests.insert(std::make_pair(email_handle, data));
607         }
608
609         return email_handle;
610
611 }
612
613 int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail, int account_id, const Api::Messaging::IAttachmentPtr& attachment)
614 {
615         LogDebug("downloadAttachmentInternal");
616         Assert(mail);
617         
618         int err = 0;
619         int nth = 0;
620         int mailId = mail->convertId(mail->getIdRef());
621         unsigned email_handle = 0;
622         
623 //      email_mail_data_t* result = NULL;
624
625         int error;
626 //      error = email_get_mail_data(mailId, &result);
627         if (EMAIL_ERROR_NONE != error) {
628                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
629                                  "Couldn't find message " << mailId << ". [" << error << "]");
630         }
631
632         LogDebug("Attachment ID :" << attachment->getAttachmentID());   
633         std::stringstream stream;
634         
635         if (stream.fail()) {
636                 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
637                                  "Couldn't convert e-mail attachment id");
638         }
639
640         //get nth 
641         std::vector<Api::Messaging::IAttachmentPtr> attachments = mail->getAttachments();
642         for ( unsigned int i = 0 ; i < attachments.size() ; i ++)
643         {
644                 LogDebug( "attachment ID is = " << attachments[i]->getAttachmentID());
645                 if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
646                 {
647                         nth = i;
648                         break;
649                 }
650         }
651         
652         stream << nth+1;
653         LogDebug("nth = " << stream.str());
654         LogDebug("attachment Order Index = " << stream.str());
655         err = email_download_attachment(mailId, nth+1 , &email_handle);
656         if (err != EMAIL_ERROR_NONE) {
657                 LogDebug("fail to downloadAttachment - err : " << err);
658                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
659         }
660         else
661         {
662                 LogDebug("Insert downloadAttachment request");
663                 LogDebug("handle : " << email_handle);
664                 SyncRequestData data = SyncRequestData( email_handle,   
665                         MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
666                 m_SyncRequests.insert(std::make_pair(email_handle, data));              
667         }
668
669 //      if ( result )
670 //              if  (  EMAIL_ERROR_NONE != email_free_mail_data( &result , 1) )
671 //                      LogDebug("fail to email_free_mail_data - err ");
672         
673         return email_handle;
674 }
675
676 void MailSync::cancelEmailJobInternal(int accountId, int handle)
677 {
678         LogDebug("cancel Email Job, account : " <<accountId << " handle : " << handle);
679
680         int error = email_cancel_job(accountId, handle,EMAIL_CANCELED_BY_USER);
681         if (EMAIL_ERROR_NONE != error) {
682                 LogDebug("error code : " << error );
683                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
684                  "Couldn't cancel email job handle: " << handle);
685         }
686
687         m_SyncRequests.erase(handle);
688 }
689
690
691 }
692 }
693 }