Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / MailSync.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 43e9583..fa1b65f
@@ -74,9 +74,9 @@ MailSync::MailSync() :
     m_dbus->open(DBUS_BUS_SYSTEM);
 
     //start email service
-       if (EMF_ERROR_NONE == email_service_begin()) {
+       if (EMAIL_ERROR_NONE == email_service_begin()) {
            LogDebug("Email service Begin\n");
-           if (EMF_ERROR_NONE == email_open_db()) {
+           if (EMAIL_ERROR_NONE == email_open_db()) {
                    LogDebug("Email open DB success\n");
            }
            else{
@@ -105,9 +105,9 @@ MailSync::~MailSync()
     m_dbusThread->Quit();
 
     //close email service
-       if (EMF_ERROR_NONE == email_close_db()) {
+       if (EMAIL_ERROR_NONE == email_close_db()) {
                LogDebug("Email Close DB Success\n");
-               if (EMF_ERROR_NONE == email_service_end())
+               if (EMAIL_ERROR_NONE == email_service_end())
                {
                LogDebug("Email service close Success\n");
                }
@@ -238,12 +238,12 @@ int MailSync::syncAccountInternal( const Api::Messaging::IMessagingServicePtr& m
        
        int account_id = messagingService->getAccountID();
        int err = 0;
-       emf_mailbox_t mailbox ;
-       unsigned emf_handle = 0;
+//     email_mailbox_t mailbox ;
+       unsigned email_handle = 0;
        
-       memset(&mailbox, 0, sizeof(emf_mailbox_t));
-       mailbox.name = NULL;                            /* all folders. */
-       mailbox.account_id = account_id; //set account id.
+//     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 )
@@ -251,23 +251,26 @@ int MailSync::syncAccountInternal( const Api::Messaging::IMessagingServicePtr& m
        else
                slot_size = limit;
        
-       email_set_mail_slot_size(0, NULL, slot_size);
+       email_set_mail_slot_size(0, 0, slot_size);
                        
-       LogDebug("mailbox.account_id " << mailbox.account_id );
-       err = email_sync_header(&mailbox, &emf_handle);
-       LogDebug("emf_handle " << emf_handle);
+//     LogDebug("mailbox.account_id " << mailbox.account_id );
+//     err = email_sync_header(&mailbox, &email_handle);
+//     LogDebug("emf_handle " << emf_handle);
 
-       if (err != EMF_ERROR_NONE) {
+       err = email_sync_header(account_id, 0, &email_handle);
+       LogDebug("email_handle " << email_handle);
+
+       if (err != EMAIL_ERROR_NONE) {
                LogDebug("fail to sync all folders - err : " << err);
        }
        else
        {
                LogDebug("Insert sync request");
-               SyncRequestData data = SyncRequestData(emf_handle,      MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
-               m_SyncRequests.insert(std::make_pair(emf_handle, data));
+               SyncRequestData data = SyncRequestData(email_handle,    MESSAGING_SERVICE_SYNC_TYPE_SYNC, messagingService);
+               m_SyncRequests.insert(std::make_pair(email_handle, data));
        }
 
-       return emf_handle;
+       return email_handle;
 }
 
 void MailSync::syncAccountCancel(const int handle)
@@ -288,9 +291,9 @@ void MailSync::syncAccountCancel(const int handle)
        }       
 }
 
-int MailSync::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingService, const std::string& folderName, const int limit)
+int MailSync::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingService, const int folder_id, const int limit)
 {
-       LogDebug("sync folder : " << folderName << " limit : " << limit);
+       LogDebug("sync folder : " << folder_id << " limit : " << limit);
        
        if (messagingService)
        {
@@ -301,14 +304,14 @@ int MailSync::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingSe
                {
                        const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
                        if (messagingService && messagingService->getAccountID() == accountId 
-                               && it->second.folderName == folderName)
+                               && it->second.folderId == folder_id)
                        {
                                ThrowMsg(WrtDeviceApis::Commons::PlatformException,
                                          "Sync: " << accountId << " already requested to be Sync Account.");
                        }
                }
                
-               return syncFolderInternal(messagingService, folderName, limit);
+               return syncFolderInternal(messagingService, folder_id, limit);
        }
        else
        {
@@ -337,87 +340,90 @@ void MailSync::syncFolderCancel(const int handle)
        }
 }
 
-int MailSync::syncFolderInternal(const Api::Messaging::IMessagingServicePtr& messagingService, const std::string& folderName, const int limit)
+int MailSync::syncFolderInternal(const Api::Messaging::IMessagingServicePtr& messagingService, const int folder_id, const int limit)
 {
-       LogDebug("Folder Name=" << folderName << " limit : " << limit);
+       LogDebug("Folder Id=" << folder_id << " limit : " << limit);
 
        int account_id = messagingService->getAccountID();
 
-       unsigned emf_handle = 0;        
+       unsigned email_handle = 0;      
        int err = 0;
        int slot_size = 0;
-       emf_mailbox_t* mailbox;
-       char* mailbox_name = strdup(folderName.c_str());
+       email_mailbox_t* mailbox;
+       
+//     char* mailbox_name = strdup(folderName.c_str());
        
-       err = email_get_mailbox_by_name(account_id, mailbox_name, &mailbox );
-       if (EMF_ERROR_NONE != err) {
+       err = email_get_mailbox_by_mailbox_id(folder_id, &mailbox);
+       if (EMAIL_ERROR_NONE != err) {
                        ThrowMsg(WrtDeviceApis::Commons::PlatformException,
-                       "Couldn't add message to mailbox. [" << err << "]");
+                       "Couldn't set mail solt size. [" << err << "]");
        }       
        
+//     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_name, slot_size);
-       if (EMF_ERROR_NONE != err) {
+       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, &emf_handle);
-       if (err != EMF_ERROR_NONE) {
+//     err = email_sync_header(mailbox, &email_handle);
+       err = email_sync_header(account_id, mailbox->mailbox_id, &email_handle);
+       if (err != EMAIL_ERROR_NONE) {
                LogDebug("fail to sync folders - err : " << err);
        }
        else
        {
                LogDebug("Insert sync request");
 
-               SyncRequestData data = SyncRequestData(emf_handle,      MESSAGING_SERVICE_SYNC_TYPE_SYNC_FOLDER, messagingService, folderName);
-               m_SyncRequests.insert(std::make_pair(emf_handle, data));
+               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_name)
+//             free(mailbox_name);
 
        if ( mailbox )
-               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
+               if  (  EMAIL_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
                        LogDebug("fail to email_free_mailbox - err ");
        
-       return emf_handle;
+       return email_handle;
 }
        
 
 // TODO Copied from former solution, refactor it.
 void MailSync::OnEventReceived(const DBus::MessageEvent& event)
 {
-    LogDebug("OnEventReceived");
-    SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
-    int mailId = syncNetworkStatus->getMailId();       //if email body download mode.
-    int status = syncNetworkStatus->getStatus();
-    int handle = syncNetworkStatus->getHandle();
+       LogDebug("OnEventReceived");
+       SyncNetworkStatusPtr syncNetworkStatus(new SyncNetworkStatus(event.GetArg0()));
+       int mailId = syncNetworkStatus->getMailId();    //if email body download mode.
+       int status = syncNetworkStatus->getStatus();
+       int handle = syncNetworkStatus->getHandle();
 
        // if Attachment Load
-       if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||
-       status == NOTI_DOWNLOAD_ATTACH_FAIL )
-       {       
+       if ( status == NOTI_DOWNLOAD_ATTACH_FINISH ||status == NOTI_DOWNLOAD_ATTACH_FAIL )
+       {
                int nth = handle;
                LogDebug(" Debus mailID = " <<  mailId <<  " , Nth = " << nth );
-               
+
                SyncRequestIterator it = m_SyncRequests.begin();
                for ( ; it != m_SyncRequests.end(); it++ )
                {
                        const Api::Messaging::IEmailPtr& mail = it->second.mail;
-                       
+
                        LogDebug(" request mailID = " <<  mail->getUID() <<  " , Nth = " << (it->second.attachment)->getNth() );
                        if ( mail->getUID() == mailId && nth == (it->second.attachment)->getNth())
-                       {       
+                       {
                                Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
                                if ( mail && requestReceiver )
                                {
                                        Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
-                               
+
                                        if (status == NOTI_DOWNLOAD_ATTACH_FINISH)
                                        {
                                                LogDebug(" Attachment Finish " );
@@ -425,107 +431,109 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                                                const Api::Messaging::IAttachmentPtr& attachment = it->second.attachment;
                                                if (attachment)
                                                {
-                                                       emf_mailbox_t mailbox ;
-                                                       emf_attachment_info_t *attach_info = NULL;
-                                                       memset(&mailbox, 0, sizeof(emf_mailbox_t));
-                                                       mailbox.account_id = acountId; //set account id.
+//                                                     email_mailbox_t mailbox ;
+//                                                     email_attachment_info_t *attach_info = NULL;
+//                                                     memset(&mailbox, 0, sizeof(email_mailbox_t));
+//                                                     mailbox.account_id = acountId; //set account id.
                                                        //int attachmentId = attachment->getAttachmentID();
-                                                       std::stringstream stream;
-                                                       stream << attachment->getAttachmentID();
-                                                       LogDebug("Attachment ID = " << stream.str());
-                                                       
-                                                       int err = email_get_attachment_info( &mailbox,  mail->getUID(), stream.str().c_str() , &attach_info );
-                                                       if (err == EMF_ERROR_NONE) {
-                                                               LogDebug("attachment Name : " << attach_info->name);
-                                                               LogDebug("attachment ID : " << attach_info->attachment_id);
-                                                               attachment->init(attach_info->savename, false);
+//                                                     std::stringstream stream;
+//                                                     stream << attachment->getAttachmentID();
+//                                                     int err = email_get_attachment_info(mail->getUID(), stream.str().c_str() , &attach_info );
+
+                                                       email_attachment_data_t* attachment_data = NULL;
+                                                       int attachmentId = attachment->getAttachmentID();
+                                                       LogDebug("Attachment ID = " << attachmentId);
+
+                                                       int err = email_get_attachment_data(attachmentId, &attachment_data);
+                                                       if (err == EMAIL_ERROR_NONE) {
+                                                               LogDebug("attachment Name : " << attachment_data->attachment_name);
+                                                               LogDebug("attachment ID : " << attachment_data->attachment_id);
+                                                               attachment->init(attachment_data->attachment_path, false);
                                                        }
                                                        else
                                                        {
-                                                               LogDebug("fail to email_get_attachment_info - err : " << err);
+                                                               LogDebug("fail to email_get_attachment_data - err : " << err);
                                                        }
-                                                       
+
                                                }
-                                               
+
                                        }
-                                       else 
-                                       {       
+                                       else
+                                       {
                                                event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
                                                LogDebug(" Attachment Failed " );
                                        }
 
                                        requestReceiver->ManualAnswer(event);
-                                       m_SyncRequests.erase( handle );         
-                               
-                               }
+                                       m_SyncRequests.erase( handle );
 
-                       break;
+                               }
+                               break;
                        }
                } 
        }
-       else if ( status == NOTI_DOWNLOAD_ATTACH_START ) 
+       else if ( status == NOTI_DOWNLOAD_ATTACH_START )
        {
                LogDebug("DownLoading... attachment : size = " << syncNetworkStatus->getErrorCode());
                return;
        }
 
-    SyncRequestIterator it = m_SyncRequests.find(handle);
-    if ( m_SyncRequests.end() != it)
-    {  
+       SyncRequestIterator it = m_SyncRequests.find(handle);
+       if ( m_SyncRequests.end() != it)
+       {
                int syncType = it->second.syncType;
                LogDebug(" Sync ... handle : " << handle << " status : " << status << " SyncType: " << syncType);
 
                switch(syncType)
                {
                        case MESSAGING_SERVICE_SYNC_TYPE_SYNC:
-                       {       
+                       {
                                LogDebug(" Sync Account");
                                const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
                                Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
-                               
+
                                if ( messagingService && requestReceiver)
-                               {       
+                               {
                                        Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
                                        if ( status == NOTI_DOWNLOAD_FINISH )
-                                       {       
+                                       {
                                                LogDebug("Sync Success");
-                                               requestReceiver->ManualAnswer(event);
-                                               m_SyncRequests.erase( handle );                                                         
+                                               requestReceiver->ManualAnswer(event);
+                                               m_SyncRequests.erase( handle );
                                        }
                                        else if ( status == NOTI_DOWNLOAD_FAIL )
-                                       {       
+                                       {
                                                LogDebug("Sync Fail");
-                                       event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
+                                               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:
-                       {       
+                       {
                                LogDebug("Sync Folder");
                                const Api::Messaging::IMessagingServicePtr& messagingService = it->second.messagingService;
                                Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = messagingService->getRequestReceiver();
-                               
+
                                if ( messagingService && requestReceiver)
-                               {       
+                               {
                                        Api::Messaging::EventMessagingServicePtr event = messagingService->getMessagingServiceEvent();
                                        if ( status == NOTI_DOWNLOAD_FINISH )
-                                       {       
+                                       {
                                                LogDebug("Sync Success");
-                                               requestReceiver->ManualAnswer(event);
-                                               m_SyncRequests.erase( handle );                                                         
+                                                       requestReceiver->ManualAnswer(event);
+                                               m_SyncRequests.erase( handle );
                                        }
                                        else if ( status == NOTI_DOWNLOAD_FAIL )
-                                       {       
+                                       {
                                                LogDebug("Sync Fail");
-                                       event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
+                                               event->setExceptionCode( WrtDeviceApis::Commons::ExceptionCodes::UnknownException );
                                                requestReceiver->ManualAnswer(event);
                                                m_SyncRequests.erase( handle );
                                        }
@@ -537,7 +545,7 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                                break;
                        }
                        case MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY:
-                       {       
+                       {
                                LogDebug(" DownLoad Body");
                                const Api::Messaging::IEmailPtr& mail = it->second.mail;        // IEmailPtr
                                Api::Messaging::EventMessagingServiceReqReceiver* requestReceiver = mail->getRequestReceiver();
@@ -545,6 +553,7 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                                        Api::Messaging::EventMessagingServicePtr event = mail->getMessagingServiceEvent();
                                        if ( status == NOTI_DOWNLOAD_BODY_FINISH )
                                        {
+                                               event->m_message->readAllData();
                                                requestReceiver->ManualAnswer(event);
                                                m_SyncRequests.erase( handle );
                                        }
@@ -553,21 +562,16 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                                                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 Api::Messaging::IEmailPtr& mail, int account_id)
@@ -577,44 +581,25 @@ int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int a
 
        int err = 0;
        int mailId = mail->convertId(mail->getIdRef());
-       unsigned int emf_handle = 0;
+       unsigned int email_handle = 0;
        
        LogDebug("folder type = " << mail->getCurrentFolder());
-
-       char *mailbox_name =NULL;
-       err = email_get_mailbox_name_by_mail_id(mailId, &mailbox_name);
-       if (EMF_ERROR_NONE != err) {
-                       ThrowMsg(WrtDeviceApis::Commons::PlatformException,
-                       "Couldn't add message to mailbox. [" << err << "]");
-       }
        LogDebug("mail ID :" << mailId );
-       LogDebug("mailbox name : " << mailbox_name );   
 
-       emf_mailbox_t* mailbox;
-       err = email_get_mailbox_by_name(mail->getAccountID(), mailbox_name, &mailbox );
-       if (EMF_ERROR_NONE != err) {
-                       ThrowMsg(WrtDeviceApis::Commons::PlatformException,
-                       "Couldn't add message to mailbox. [" << err << "]");
-       }
-       
-       err = email_download_body(mailbox, mailId, 0, &emf_handle);
-       if (err != EMF_ERROR_NONE) {
+       err = email_download_body(mailId, 0, &email_handle);
+       if (err != EMAIL_ERROR_NONE) {
                LogDebug("fail to downloadBody - err : " << err);
                ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
        }
        else
        {
                LogDebug("Insert downloadBody request");
-               LogDebug("handle : " << emf_handle);
-               SyncRequestData data = SyncRequestData(emf_handle,      MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
-               m_SyncRequests.insert(std::make_pair(emf_handle, data));
+               LogDebug("handle : " << email_handle);
+               SyncRequestData data = SyncRequestData(email_handle,    MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_BODY, mail);
+               m_SyncRequests.insert(std::make_pair(email_handle, data));
        }
 
-       if ( mailbox )
-               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
-                       LogDebug("fail to email_free_mailbox - err ");
-
-       return emf_handle;
+       return email_handle;
 
 }
 
@@ -626,24 +611,17 @@ int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail,
        int err = 0;
        int nth = 0;
        int mailId = mail->convertId(mail->getIdRef());
-       unsigned emf_handle = 0;
+       unsigned email_handle = 0;
        
-       char *mailbox_name =NULL;
-       err = email_get_mailbox_name_by_mail_id(mailId, &mailbox_name);
-       if (EMF_ERROR_NONE != err) {
-                       ThrowMsg(WrtDeviceApis::Commons::PlatformException,
-                       "Couldn't add message to mailbox. [" << err << "]");
-       }
-       LogDebug("mail ID :" << mailId );
-       LogDebug("mailbox name : " << mailbox_name );   
+       email_mail_data_t* result = NULL;
 
-       emf_mailbox_t* mailbox;
-       err = email_get_mailbox_by_name(mail->getAccountID(), mailbox_name, &mailbox );
-       if (EMF_ERROR_NONE != err) {
+       int error;
+       error = email_get_mail_data(mailId, &result);
+       if (EMAIL_ERROR_NONE != error) {
                        ThrowMsg(WrtDeviceApis::Commons::PlatformException,
-                       "Couldn't add message to mailbox. [" << err << "]");
+                                "Couldn't find message " << mailId << ". [" << error << "]");
        }
-       
+
        LogDebug("Attachment ID :" << attachment->getAttachmentID());   
        std::stringstream stream;
        
@@ -667,33 +645,33 @@ int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail,
        stream << nth+1;
        LogDebug("nth = " << stream.str());
        LogDebug("attachment Order Index = " << stream.str());
-       err = email_download_attachment(mailbox, mailId, stream.str().c_str() , &emf_handle);
-       if (err != EMF_ERROR_NONE) {
+       err = email_download_attachment(mailId, nth+1 , &email_handle);
+       if (err != EMAIL_ERROR_NONE) {
                LogDebug("fail to downloadAttachment - err : " << err);
                ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
        }
        else
        {
                LogDebug("Insert downloadAttachment request");
-               LogDebug("handle : " << emf_handle);
-               SyncRequestData data = SyncRequestData( emf_handle,     
+               LogDebug("handle : " << email_handle);
+               SyncRequestData data = SyncRequestData( email_handle,
                        MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
-               m_SyncRequests.insert(std::make_pair(emf_handle, data));                
+               m_SyncRequests.insert(std::make_pair(email_handle, data));
        }
 
-       if ( mailbox )
-               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
-                       LogDebug("fail to email_free_mailbox - err ");
+       if ( result )
+               if  (  EMAIL_ERROR_NONE != email_free_mail_data( &result , 1) )
+                       LogDebug("fail to email_free_mail_data - err ");
        
-       return emf_handle;
+       return email_handle;
 }
 
 void MailSync::cancelEmailJobInternal(int accountId, int handle)
 {
        LogDebug("cancel Email Job, account : " <<accountId << " handle : " << handle);
 
-       int error = email_cancel_job(accountId, handle);
-       if (EMF_ERROR_NONE != error) {
+       int error = email_cancel_job(accountId, handle,EMAIL_CANCELED_BY_USER);
+       if (EMAIL_ERROR_NONE != error) {
                LogDebug("error code : " << error );
                ThrowMsg(WrtDeviceApis::Commons::PlatformException,
                 "Couldn't cancel email job handle: " << handle);