summaryrefslogtreecommitdiff
path: root/mobile_src/Messaging/MailSync.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mobile_src/Messaging/MailSync.cpp')
-rw-r--r--mobile_src/Messaging/MailSync.cpp785
1 files changed, 785 insertions, 0 deletions
diff --git a/mobile_src/Messaging/MailSync.cpp b/mobile_src/Messaging/MailSync.cpp
new file mode 100644
index 0000000..f5395ce
--- /dev/null
+++ b/mobile_src/Messaging/MailSync.cpp
@@ -0,0 +1,785 @@
+//
+// Tizen Web Device API
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <utility>
+#include <email-types.h>
+#include <email-api.h>
+#include <email-api-network.h>
+#include <email-api-init.h>
+#include <email-api-account.h>
+
+#include <Commons/Exception.h>
+#include <Commons/ThreadPool.h>
+#include "ReqReceiverMessage.h"
+#include "EventMessagingService.h"
+#include "IAttachment.h"
+
+#include "MailSync.h"
+#include "SyncNetworkStatus.h"
+#include "IMessage.h"
+//for vconf
+#include <vconf.h>
+#include <vconf-keys.h>
+
+#include <Logger.h>
+
+namespace {
+const char* DBUS_FILTER_NETWORK_STATUS =
+ "type='signal',interface='User.Email.NetworkStatus'";
+//const char* DBUS_FILTER_EMAIL_RECEIVED =
+// "type='signal',interface='User.Email.StorageChange'";
+}
+
+namespace DeviceAPI {
+namespace Messaging {
+
+using namespace WrtDeviceApis::Commons;
+
+MailSync& MailSync::getInstance()
+{
+ static MailSync instance;
+ return instance;
+}
+
+MailSync::MailSync() :
+ m_dbusThread(new DPL::Thread()),
+ m_dbus(new DBus::Connection())
+{
+ m_dbusThread->Run();
+
+ m_dbus->setWorkerThread(m_dbusThread.Get());
+ m_dbus->AddListener(this);
+ m_dbus->SwitchAllListenersToThread(
+ ThreadPool::getInstance().getThreadRef(ThreadEnum::MESSAGING_THREAD)
+ );
+ m_dbus->addFilter(DBUS_FILTER_NETWORK_STATUS);
+ m_dbus->open(DBUS_BUS_SYSTEM);
+
+ //start email service
+ int error = email_service_begin();
+ if (error == EMAIL_ERROR_NONE) {
+ LoggerD("Email service Begin\n");
+ if (EMAIL_ERROR_NONE == email_open_db()) {
+ LoggerD("Email open DB success\n");
+ }
+ else{
+ LoggerD("Email open DB failed\n");
+ }
+ }
+ else{
+ LoggerD("Email service not started\n");
+ LoggerD("Error : " << error);
+ }
+
+ //get retriving count
+ int slot_size = -1;
+ vconf_get_int("db/private/email-service/slot_size", &(slot_size));
+ if ( slot_size > 0 )
+ m_default_slot_size = slot_size;
+
+ LoggerD( "Slot Size : " << m_default_slot_size );
+
+}
+
+MailSync::~MailSync()
+{
+ m_dbus->RemoveListener(this);
+ m_dbus->close();
+
+ m_dbusThread->Quit();
+
+ //close email service
+ if (EMAIL_ERROR_NONE == email_close_db()) {
+ LoggerD("Email Close DB Success\n");
+ if (EMAIL_ERROR_NONE == email_service_end())
+ {
+ LoggerD("Email service close Success\n");
+ }
+ else
+ {
+ LoggerD("Email service end failed\n");
+ }
+ }
+ else{
+ LoggerD("Email Close DB failed\n");
+}
+}
+
+int MailSync::downloadBody( const IEmailPtr& mail )
+{
+ if ( mail )
+ {
+ int mailId = mail->convertId(mail->getIdRef()); // get mail id
+ LoggerD("start attachment, mail Id : " << mail->getIdRef());
+ SyncRequestIterator it;
+ int synType;
+ for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
+ {
+ synType = it->second.syncType;
+ LoggerD("synType : " << synType);
+
+ if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY )
+ {
+ LoggerD("skip");
+ continue;
+ }
+ const IEmailPtr& email = it->second.mail;
+ LoggerD ( "Requests mail ID:" << mail->convertId(email->getIdRef()) );
+ if ( mailId == mail->convertId(email->getIdRef()))
+ {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "download Body: " << mailId << " already requested to be download Body.");
+ }
+ }
+ }
+
+ return downloadBodyInternal(mail, mail->getAccountID());
+
+}
+
+void MailSync::cancelDownloadBody(int handle)
+{
+ SyncRequestIterator it = m_SyncRequests.find(handle);
+ if ( m_SyncRequests.end() != it)
+ {
+ if ( it->second.messagingService )
+ cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
+ //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
+ }
+ else
+ {
+ LoggerD("Don't find cancel DownloadBody handle : " << handle);
+ //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
+ }
+
+}
+
+void MailSync::cancelDownloadAttachment(int handle)
+{
+
+ SyncRequestIterator it = m_SyncRequests.find(handle);
+ if ( m_SyncRequests.end() != it)
+ {
+ if ( it->second.messagingService )
+ cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
+ //cancelDownloadBodyInternal(it->second.mail, it->second.handle );
+ }
+ else
+ {
+ LoggerD("Don't find cancel DownloadAttachment handle : " << handle);
+ }
+
+}
+
+int MailSync::downloadAttachment(const IEmailPtr& mail, const IAttachmentPtr& attachment)
+{
+ if ( mail && attachment )
+ {
+ //int mailId = mail->convertId(mail->getIdRef()); // get mail id
+ LoggerD("start attachment, mail Id : " << mail->getIdRef());
+ SyncRequestIterator it;
+ int synType;
+ for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
+ {
+ synType = it->second.syncType;
+ LoggerD("synType : " << synType);
+
+ if( synType != MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT )
+ {
+ LoggerD("skip");
+ continue;
+ }
+
+ const IAttachmentPtr& iattachment = it->second.attachment;
+ LoggerD ( "Requests Attachment ID:" << iattachment->getAttachmentID() << " Input Attachment ID: " << attachment->getAttachmentID());
+ if (iattachment->getAttachmentID() == attachment->getAttachmentID())
+ {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "Attachment: " << attachment->getAttachmentID() << " already requested to be download Attachment.");
+ }
+ }
+ }
+
+ return downloadAttachmentInternal(mail, mail->getAccountID(), attachment);
+
+}
+
+int MailSync::syncAccount(const IMessagingServicePtr& messagingService, const int limit)
+{
+ LoggerD("syncAccount : " << limit);
+ if (messagingService)
+ {
+ int accountId = messagingService->getAccountID();
+ LoggerD("start sync, account Id : " << accountId);
+ SyncRequestIterator it;
+ int synType;
+ for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
+ {
+ synType = it->second.syncType;
+ LoggerD("synType : " << synType);
+
+ if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC )
+ {
+ LoggerD("skip");
+ continue;
+ }
+
+ const IMessagingServicePtr& messagingService = it->second.messagingService;
+ if (messagingService && messagingService->getAccountID() == accountId)
+ {
+ LoggerD("accountId is sync requested already");
+// ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
+ }
+ }
+ return syncAccountInternal(messagingService, limit);
+ }
+ else
+ {
+ LoggerD("messagingService is NULL");
+ }
+
+ LoggerD("end sync" );
+ return -1;
+}
+
+int MailSync::syncAccountInternal( const IMessagingServicePtr& messagingService, const int limit)
+{
+ LoggerD("syncAccountInternal limit: " << limit);
+
+ int account_id = messagingService->getAccountID();
+ int err = 0;
+// email_mailbox_t mailbox ;
+ int email_handle = 0;
+
+// memset(&mailbox, 0, sizeof(email_mailbox_t));
+// mailbox.mailbox_name = NULL; /* all folders. */
+// mailbox.account_id = account_id; //set account id.
+ int slot_size;
+
+ if ( limit < 0 )
+ slot_size = m_default_slot_size;
+ else
+ slot_size = limit;
+
+ email_set_mail_slot_size(0, 0, slot_size);
+
+// LoggerD("mailbox.account_id " << mailbox.account_id );
+// err = email_sync_header(&mailbox, &email_handle);
+// LoggerD("emf_handle " << emf_handle);
+
+ err = email_sync_header(account_id, 0, &email_handle);
+ LoggerD("email_handle " << email_handle);
+
+ if (err != EMAIL_ERROR_NONE) {
+ LoggerD("fail to sync all folders - err : " << err);
+ }
+ else
+ {
+ LoggerD("Insert sync request");
+ SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
+ m_SyncRequests.insert(std::make_pair(email_handle, data));
+ }
+
+ return email_handle;
+}
+
+void MailSync::syncAccountCancel(const int handle)
+{
+ LoggerD("sync cancel");
+
+ SyncRequestIterator it = m_SyncRequests.find(handle);
+ if ( m_SyncRequests.end() != it)
+ {
+ if ( it->second.messagingService )
+ cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
+ }
+ else
+ {
+ LoggerD("Don't find cancel Sync handle : " << handle);
+ //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
+ }
+}
+
+int MailSync::syncFolder(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
+{
+ LoggerD("sync folder : " << folder_id << " limit : " << limit);
+
+ if (messagingService)
+ {
+ int accountId = messagingService->getAccountID();
+ LoggerD("start sync, account Id : " << accountId);
+ SyncRequestIterator it;
+ int synType;
+ for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
+ {
+ synType = it->second.syncType;
+ LoggerD("synType : " << synType);
+
+ if( synType != MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER )
+ {
+ LoggerD("skip");
+ continue;
+ }
+
+ const IMessagingServicePtr& messagingService = it->second.messagingService;
+ if (messagingService && messagingService->getAccountID() == accountId
+ && it->second.folderId == folder_id)
+ {
+ LoggerD("accountId is syncFolder requested already");
+// ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Sync: " << accountId << " already requested to be Sync Account.");
+ }
+ }
+
+ return syncFolderInternal(messagingService, folder_id, limit);
+ }
+ else
+ {
+ LoggerD("messagingService is NULL");
+ }
+
+ return -1;
+
+}
+
+void MailSync::syncFolderCancel(const int handle)
+{
+ LoggerD("sync Folder Cancel");
+
+ SyncRequestIterator it = m_SyncRequests.find(handle);
+ if ( m_SyncRequests.end() != it)
+ {
+ if ( it->second.messagingService )
+ cancelEmailJobInternal( it->second.messagingService->getAccountID() , it->second.handle);
+ }
+ else
+ {
+ LoggerD("Don't find cancel Sync Folder handle : " << handle);
+ //ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Email: " << mailId << " not found.");
+ }
+}
+
+int MailSync::syncFolderInternal(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
+{
+ LoggerD("Folder Id=" << folder_id << " limit : " << limit);
+
+ int account_id = messagingService->getAccountID();
+
+ int email_handle = 0;
+ int err = 0;
+ int slot_size = 0;
+ email_mailbox_t* mailbox = NULL;
+
+// char* mailbox_name = strdup(folderName.c_str());
+
+ err = email_get_mailbox_by_mailbox_id(folder_id, &mailbox);
+ if (EMAIL_ERROR_NONE != err) {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "Couldn't set mail solt size. [" << err << "]");
+ }
+
+ if (mailbox == NULL) {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't get mailbox");
+ }
+
+// char* mailbox_name = strdup(mailbox->mailbox_name);
+
+ if ( limit < 0 )
+ slot_size = m_default_slot_size;
+ else
+ slot_size = limit;
+
+ err = email_set_mail_slot_size(0, mailbox->mailbox_id, slot_size);
+ if (EMAIL_ERROR_NONE != err) {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "Couldn't set mail solt size. [" << err << "]");
+ }
+
+// err = email_sync_header(mailbox, &email_handle);
+ err = email_sync_header(account_id, mailbox->mailbox_id, &email_handle);
+ if (err != EMAIL_ERROR_NONE) {
+ LoggerD("fail to sync folders - err : " << err);
+ }
+ else
+ {
+ LoggerD("Insert sync request");
+
+ SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER, messagingService, folder_id);
+ m_SyncRequests.insert(std::make_pair(email_handle, data));
+ }
+
+// if (mailbox_name)
+// free(mailbox_name);
+
+ if ( mailbox != NULL )
+ {
+ if ( EMAIL_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
+ LoggerD("fail to email_free_mailbox - err ");
+ mailbox = NULL;
+ }
+
+ return email_handle;
+}
+
+
+// TODO Copied from former solution, refactor it.
+void MailSync::OnEventReceived(const DBus::MessageEvent& event)
+{
+ LoggerD("OnEventReceived");
+ SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
+ int mailId = syncNetworkStatus->getMailId(); //if email body download mode.
+ int status = syncNetworkStatus->getStatus();
+ int handle = syncNetworkStatus->getHandle();
+ int errCode = syncNetworkStatus->getErrorCode();
+
+ LoggerD("mailId : " << mailId << " status : " << status << " handle : " << handle << " errCode : " << errCode);
+ // if Attachment Load
+ if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||status == NOTI_DOWNLOAD_ATTACH_FAIL )
+ {
+ int nth = handle;
+ LoggerD(" Debus mailID = " << mailId << " , Nth = " << nth );
+
+ SyncRequestIterator it = m_SyncRequests.begin();
+ for ( ; it != m_SyncRequests.end(); it++ )
+ {
+ const IEmailPtr& mail = it->second.mail;
+
+ LoggerD(" request mailID = " << mail->getUID() << " , Nth = " << (it->second.attachment)->getNth() );
+ if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
+ {
+ EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
+ if ( mail && requestReceiver )
+ {
+ EventMessagingServicePtr event = mail->getMessagingServiceEvent();
+// EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
+
+ if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
+ {
+ LoggerD(" Attachment Finish " );
+ const IAttachmentPtr& attachment = it->second.attachment;
+ if (attachment)
+ {
+
+ email_attachment_data_t* attachment_data = NULL;
+ int attachmentId = attachment->getAttachmentID();
+ LoggerD("Attachment ID = " << attachmentId);
+
+ int err = email_get_attachment_data(attachmentId, &attachment_data);
+ if (err == EMAIL_ERROR_NONE) {
+ LoggerD("attachment Name : " << attachment_data->attachment_name);
+ LoggerD("attachment ID : " << attachment_data->attachment_id);
+
+ attachment->init(attachment_data->attachment_path, false);
+ if(attachment_data->attachment_mime_type)
+ {
+ LoggerD("attachment attachment_mime_type : " << attachment_data->attachment_mime_type);
+ attachment->setMimeType(attachment_data->attachment_mime_type);
+ }
+ LoggerD("attachment getMimeTypes : " << attachment->getMimeType());
+ }
+ else
+ {
+ LoggerD("fail to email_get_attachment_data - err : " << err);
+ }
+
+ if ( attachment_data != NULL )
+ {
+ email_free_attachment_data(&attachment_data, 1);
+ }
+
+ }
+
+ }
+ else
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
+ LoggerD(" Attachment Failed " );
+ }
+
+ LoggerD("remove downloadAttachment request");
+ LoggerD("handle : " << it->first );
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase(it->first);
+
+ }
+ break;
+ }
+ }
+ }
+ else if ( status == NOTI_DOWNLOAD_ATTACH_START )
+ {
+ LoggerD("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
+ return;
+ }
+
+ SyncRequestIterator it = m_SyncRequests.find(handle);
+ if ( m_SyncRequests.end() != it)
+ {
+ int syncType = it->second.syncType;
+ LoggerD(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
+
+ switch(syncType)
+ {
+ case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
+ {
+ LoggerD(" Sync Account");
+ const IMessagingServicePtr& messagingService = it->second.messagingService;
+ EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
+
+ if ( messagingService && requestReceiver)
+ {
+ EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
+ // get event using handle from m_opRequests;
+ if (event)
+ {
+ if ( status == NOTI_DOWNLOAD_FINISH )
+ {
+ LoggerD("Sync Success");
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ else if ( status == NOTI_DOWNLOAD_FAIL )
+ {
+ LoggerD("Sync Fail : " << errCode);
+ if(errCode == EMAIL_ERROR_CANCELLED)
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::EventCancelledException );
+ }
+ else
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
+ }
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ }
+ }
+ else
+ {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
+ }
+ break;
+ }
+ case MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER:
+ {
+ LoggerD("Sync Folder");
+ const IMessagingServicePtr& messagingService = it->second.messagingService;
+ EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
+
+ if ( messagingService && requestReceiver)
+ {
+ EventMessagingServicePtr event = messagingService->getEventFromHandle(handle);
+ if (event)
+ {
+ if ( status == NOTI_DOWNLOAD_FINISH )
+ {
+ LoggerD("Sync Success");
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ else if ( status == NOTI_DOWNLOAD_FAIL )
+ {
+ LoggerD("Sync Fail : " << errCode);
+ if(errCode == EMAIL_ERROR_CANCELLED)
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::EventCancelledException );
+ }
+ else
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
+ }
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ }
+ }
+ else
+ {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
+ }
+ break;
+ }
+ case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
+ {
+ LoggerD(" DownLoad Body");
+ const IEmailPtr& mail = it->second.mail; // IEmailPtr
+ EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
+ if (mail && requestReceiver) {
+ EventMessagingServicePtr event = mail->getMessagingServiceEvent();
+
+ if ( status == NOTI_DOWNLOAD_BODY_FINISH || (status == NOTI_DOWNLOAD_BODY_FAIL && errCode == EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED))
+ {
+ if(errCode != EMAIL_ERROR_MAIL_IS_ALREADY_DOWNLOADED)
+ {
+ event->m_message->readAllData();
+ }
+
+ std::vector<IAttachmentPtr> attachments = mail->getAttachments();
+ std::vector<IAttachmentPtr> inlineAttachments = mail->getInlineAttachments();
+
+ for (unsigned int idx = 0; idx < attachments.size() ; idx++ )
+ {
+ LoggerD("set Attachment ID = " << attachments[idx]->getAttachmentID());
+ attachments[idx]->setMessage(event->m_message);
+
+ }
+ for (unsigned int idx = 0; idx < inlineAttachments.size() ; idx++ )
+ {
+ LoggerD("set inline Attachment ID = " << inlineAttachments[idx]->getAttachmentID());
+ inlineAttachments[idx]->setMessage(event->m_message);
+ }
+
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ else if ( status == NOTI_DOWNLOAD_BODY_FAIL )
+ {
+ event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
+ requestReceiver->ManualAnswer(event);
+ m_SyncRequests.erase( handle );
+ }
+ }
+ else
+ {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "request Receiver is NULL. ");
+ }
+ break;
+ }
+ }
+ }
+}
+
+int MailSync::downloadBodyInternal( const IEmailPtr& mail, int account_id)
+{
+
+ LoggerD("downloadInternal");
+
+ int err = 0;
+ int mailId = mail->convertId(mail->getIdRef());
+ int email_handle = 0;
+
+ LoggerD("folder type = " << mail->getCurrentFolder());
+ LoggerD("mail ID :" << mailId );
+
+ err = email_download_body(mailId, 0, &email_handle);
+ if (err != EMAIL_ERROR_NONE) {
+ LoggerD("fail to downloadBody - err : " << err);
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
+ }
+ else
+ {
+ LoggerD("Insert downloadBody request");
+ LoggerD("handle : " << email_handle);
+ SyncRequestData data = SyncRequestData(email_handle, MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
+ m_SyncRequests.insert(std::make_pair(email_handle, data));
+ }
+
+ return email_handle;
+
+}
+
+int MailSync::downloadAttachmentInternal(const IEmailPtr& mail, int account_id, const IAttachmentPtr& attachment)
+{
+ LoggerD("downloadAttachmentInternal");
+ if (!mail) {
+ LoggerE("mail is NULL");
+ Throw(WrtDeviceApis::Commons::PlatformException);
+ }
+
+ int err = 0;
+ int nth = 0;
+ int mailId = mail->convertId(mail->getIdRef());
+ int email_handle = 0;
+
+ email_mail_data_t* result = NULL;
+
+ int error;
+ error = email_get_mail_data(mailId, &result);
+ if (EMAIL_ERROR_NONE != error) {
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "Couldn't find message " << mailId << ". [" << error << "]");
+ }
+
+ LoggerD("Attachment ID :" << attachment->getAttachmentID());
+
+ //get nth
+ std::vector<IAttachmentPtr> attachments = mail->getAttachments();
+ std::vector<IAttachmentPtr> inlineAttachments = mail->getInlineAttachments();
+ unsigned int attachmentSize = attachments.size();
+ unsigned int inlineAttachmentSize = inlineAttachments.size();
+
+ LoggerD( "attachment sizes = " << attachmentSize << ",inline attachment size : " << inlineAttachmentSize);
+ for ( unsigned int i = 0 ; i < attachmentSize ; i ++)
+ {
+ LoggerD( "attachment ID is = " << attachments[i]->getAttachmentID());
+ if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
+ {
+// nth = i;
+ nth = attachments[i]->getNth();
+ break;
+ }
+ }
+ for ( unsigned int i = 0 ; i < inlineAttachmentSize ; i ++)
+ {
+ LoggerD( "inline attachment ID is = " << inlineAttachments[i]->getAttachmentID());
+ if (inlineAttachments[i]->getAttachmentID() == attachment->getAttachmentID())
+ {
+// nth = i;
+ nth = inlineAttachments[i]->getNth();
+ break;
+ }
+ }
+
+ LoggerD("nth = " << nth);
+ LoggerD("attachment Order Index = " << nth);
+ err = email_download_attachment(mailId, nth , &email_handle);
+ if (err != EMAIL_ERROR_NONE) {
+ LoggerD("fail to downloadAttachment - err : " << err);
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
+ }
+ else
+ {
+ LoggerD("Insert downloadAttachment request");
+ LoggerD("handle : " << email_handle);
+ SyncRequestData data = SyncRequestData( email_handle,
+ MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
+ m_SyncRequests.insert(std::make_pair(email_handle, data));
+ }
+
+ if ( result )
+ if ( EMAIL_ERROR_NONE != email_free_mail_data( &result , 1) )
+ LoggerD("fail to email_free_mail_data - err ");
+
+ return email_handle;
+}
+
+void MailSync::cancelEmailJobInternal(int accountId, int handle)
+{
+ LoggerD("cancel Email Job, account : " <<accountId << " handle : " << handle);
+
+ int error = email_cancel_job(accountId, handle,EMAIL_CANCELED_BY_USER);
+ if (EMAIL_ERROR_NONE != error) {
+ LoggerD("error code : " << error );
+ ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+ "Couldn't cancel email job handle: " << handle);
+ }
+
+ m_SyncRequests.erase(handle);
+}
+
+
+}
+}