upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / MailSync.cpp
index 5d559e2..43e9583 100755 (executable)
  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
  */
 #include <utility>
-#include <emf-types.h>
-#include <Emf_Mapi.h>
-#include <Emf_Mapi_Network.h>
-#include <Emf_Mapi_Init.h>
-#include <Emf_Mapi_Account.h>
+#include <email-types.h>
+#include <email-api.h>
+#include <email-api-network.h>
+#include <email-api-init.h>
+#include <email-api-account.h>
 
 #include <dpl/log/log.h>
 #include <dpl/assert.h>
@@ -163,11 +163,29 @@ void MailSync::cancelDownloadBody(int handle)
        
 }
 
+void MailSync::cancelDownloadAttachment(int handle)
+{
+       Assert(handle > -1);
+
+       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
+       {
+               LogDebug("Don't find cancel DownloadAttachment handle : " << handle);
+       }
+       
+}
+
 int MailSync::downloadAttachment(const Api::Messaging::IEmailPtr& mail, const Api::Messaging::IAttachmentPtr& attachment)
 {      
        if ( mail && attachment )
        {       
-               int mailId = mail->convertId(mail->getIdRef());         // get mail id
+               //int mailId = mail->convertId(mail->getIdRef());               // get mail id
                LogDebug("start attachment, mail Id : " << mail->getIdRef());
                SyncRequestIterator it;
                for( it = m_SyncRequests.begin(); it != m_SyncRequests.end(); ++it)
@@ -224,7 +242,7 @@ int MailSync::syncAccountInternal( const Api::Messaging::IMessagingServicePtr& m
        unsigned emf_handle = 0;
        
        memset(&mailbox, 0, sizeof(emf_mailbox_t));
-       mailbox.name = NULL;    /* all folders. */
+       mailbox.name = NULL;                            /* all folders. */
        mailbox.account_id = account_id; //set account id.
        int slot_size;
        
@@ -324,24 +342,31 @@ int MailSync::syncFolderInternal(const Api::Messaging::IMessagingServicePtr& mes
        LogDebug("Folder Name=" << folderName << " limit : " << limit);
 
        int account_id = messagingService->getAccountID();
-       emf_mailbox_t mailbox;
-       gchar *src_box =g_strdup( folderName.c_str() );
-       unsigned emf_handle = 0;
-       
+
+       unsigned emf_handle = 0;        
        int err = 0;
-       memset(&mailbox, 0, sizeof(emf_mailbox_t));
-       mailbox.name = src_box;
-       mailbox.account_id = account_id;
        int slot_size = 0;
-               
+       emf_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) {
+                       ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+                       "Couldn't add message to mailbox. [" << err << "]");
+       }       
+       
        if ( limit < 0 )
                slot_size = m_default_slot_size;
        else
                slot_size = limit;
        
-       email_set_mail_slot_size(0, src_box, slot_size);
+       err = email_set_mail_slot_size(0, mailbox_name, slot_size);
+       if (EMF_ERROR_NONE != err) {
+                       ThrowMsg(WrtDeviceApis::Commons::PlatformException,
+                       "Couldn't set mail solt size. [" << err << "]");
+       }
        
-       err = email_sync_header(&mailbox, &emf_handle);
+       err = email_sync_header(mailbox, &emf_handle);
        if (err != EMF_ERROR_NONE) {
                LogDebug("fail to sync folders - err : " << err);
        }
@@ -353,10 +378,13 @@ int MailSync::syncFolderInternal(const Api::Messaging::IMessagingServicePtr& mes
                m_SyncRequests.insert(std::make_pair(emf_handle, data));
        }
 
-       if (src_box) {
-               g_free(src_box);
-       }
+       if (mailbox_name)
+               free(mailbox_name);
 
+       if ( mailbox )
+               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
+                       LogDebug("fail to email_free_mailbox - err ");
+       
        return emf_handle;
 }
        
@@ -378,7 +406,7 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                LogDebug(" Debus mailID = " <<  mailId <<  " , Nth = " << nth );
                
                SyncRequestIterator it = m_SyncRequests.begin();
-               for ( it; it != m_SyncRequests.end(); it++ )
+               for ( ; it != m_SyncRequests.end(); it++ )
                {
                        const Api::Messaging::IEmailPtr& mail = it->second.mail;
                        
@@ -401,7 +429,7 @@ void MailSync::OnEventReceived(const DBus::MessageEvent& event)
                                                        emf_attachment_info_t *attach_info = NULL;
                                                        memset(&mailbox, 0, sizeof(emf_mailbox_t));
                                                        mailbox.account_id = acountId; //set account id.
-                                                       int attachmentId = attachment->getAttachmentID();
+                                                       //int attachmentId = attachment->getAttachmentID();
                                                        std::stringstream stream;
                                                        stream << attachment->getAttachmentID();
                                                        LogDebug("Attachment ID = " << stream.str());
@@ -550,19 +578,26 @@ int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int a
        int err = 0;
        int mailId = mail->convertId(mail->getIdRef());
        unsigned int emf_handle = 0;
-
-
-       LogDebug("folder type = " << mail->getCurrentFolder());
        
-       emf_mailbox_t mailbox;
-       memset(&mailbox, 0, sizeof(emf_mailbox_t));
+       LogDebug("folder type = " << mail->getCurrentFolder());
 
-       mailbox.name = strdup("INBOX");
-       
-       mailbox.account_id = account_id;        //set account id.       
-       LogDebug("mailbox.account_id " << mailbox.account_id << " mailId : " << mailId);
-       err = email_download_body(&mailbox, mailId, 0, &emf_handle);
+       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) {
                LogDebug("fail to downloadBody - err : " << err);
                ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadBody request. [" << err << "]");
@@ -575,6 +610,10 @@ int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int a
                m_SyncRequests.insert(std::make_pair(emf_handle, data));
        }
 
+       if ( mailbox )
+               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
+                       LogDebug("fail to email_free_mailbox - err ");
+
        return emf_handle;
 
 }
@@ -582,22 +621,31 @@ int MailSync::downloadBodyInternal( const Api::Messaging::IEmailPtr& mail, int a
 int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail, int account_id, const Api::Messaging::IAttachmentPtr& attachment)
 {
        LogDebug("downloadAttachmentInternal");
-
+       Assert(mail);
+       
        int err = 0;
        int nth = 0;
-       int idx = 0;
        int mailId = mail->convertId(mail->getIdRef());
        unsigned emf_handle = 0;
        
-       emf_mailbox_t mailbox;
-       //gchar *src_box = g_strdup(folder_name);
-       memset(&mailbox, 0, sizeof(emf_mailbox_t));
-       mailbox.account_id = account_id;        //set account id.
-       LogDebug("mailbox.account_id " << mailbox.account_id );
-       LogDebug("Attachment ID :" << attachment->getAttachmentID());
+       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 << "]");
+       }
        
+       LogDebug("Attachment ID :" << attachment->getAttachmentID());   
        std::stringstream stream;
-       //stream << attachment->getAttachmentID();
        
        if (stream.fail()) {
                ThrowMsg(WrtDeviceApis::Commons::UnknownException,
@@ -606,7 +654,7 @@ int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail,
 
        //get nth 
        std::vector<Api::Messaging::IAttachmentPtr> attachments = mail->getAttachments();
-       for ( int i = 0 ; i < attachments.size() ; i ++)
+       for ( unsigned int i = 0 ; i < attachments.size() ; i ++)
        {
                LogDebug( "attachment ID is = " << attachments[i]->getAttachmentID());
                if (attachments[i]->getAttachmentID() == attachment->getAttachmentID())
@@ -618,9 +666,8 @@ 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);
+       err = email_download_attachment(mailbox, mailId, stream.str().c_str() , &emf_handle);
        if (err != EMF_ERROR_NONE) {
                LogDebug("fail to downloadAttachment - err : " << err);
                ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to initialize downloadAttachment request. [" << err << "]");
@@ -631,11 +678,14 @@ int MailSync::downloadAttachmentInternal(const Api::Messaging::IEmailPtr& mail,
                LogDebug("handle : " << emf_handle);
                SyncRequestData data = SyncRequestData( emf_handle,     
                        MESSAGING_SERVICE_SYNC_TYPE_DOWNLOAD_ATTACHMENT, mail, attachment );
-               m_SyncRequests.insert(std::make_pair(emf_handle, data));
+               m_SyncRequests.insert(std::make_pair(emf_handle, data));                
        }
 
-       return emf_handle;
+       if ( mailbox )
+               if  (  EMF_ERROR_NONE != email_free_mailbox( &mailbox , 1) )
+                       LogDebug("fail to email_free_mailbox - err ");
        
+       return emf_handle;
 }
 
 void MailSync::cancelEmailJobInternal(int accountId, int handle)