emailuis/nmailui/src/nmattachmentmanager.cpp
changeset 40 2c62ef3caffd
parent 23 2dc6caa42ec3
child 30 759dc5235cdb
--- a/emailuis/nmailui/src/nmattachmentmanager.cpp	Fri May 14 04:17:40 2010 +0300
+++ b/emailuis/nmailui/src/nmattachmentmanager.cpp	Fri May 14 04:41:45 2010 +0300
@@ -77,6 +77,85 @@
 }
 
 /*!
+    Fetch attachments to a message. 
+    Set observer with setObserver method to get process and complete events
+*/
+void NmAttachmentManager::fetchAttachments(
+        const NmId &mailboxId, 
+        const NmId &folderId, 
+        const NmId &messageId,
+        QList<NmId> &messagePartIds)
+{
+    // cancel old fetch operation, Does nothing if fetch not ongoing
+    // We don't wan't to cancel message fetching operation, because
+    // it might still be finishing.
+    if (!mMsgFetchOperation) {
+        cancelFetch();
+    }
+    
+    if (messagePartIds.count() > 0) {
+        mFetchOperation = mUiEngine.fetchMessageParts(
+            mailboxId,
+            folderId,
+            messageId,
+            messagePartIds);
+    }
+    
+    if (mFetchOperation) {
+        mAttaId = 0;
+        mIsFetching = true;
+        QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
+                this, SLOT(attachmentFetchCompleted(int)));
+        
+        QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
+                this, SLOT(changeProgress(int)));
+        
+    }
+}
+
+/*!
+    Fetch all message parts to a message. Fetches also message part data structure,
+    if not fetched already.
+      If earlier fetch operation exist it is cancelled and deleted.
+    Set observer with setObserver method to get process and complete events
+*/
+void NmAttachmentManager::fetchAllMessageParts(
+        const NmId &mailboxId, 
+        const NmId &folderId, 
+        const NmId &messageId)
+{
+    fetchMsg = mUiEngine.message(mailboxId,folderId,messageId);
+    
+    // Check if we have part data structure.    
+    if (fetchMsg->childParts().count() == 0 &&
+        fetchMsg->fetchedSize() < fetchMsg->size()) {
+
+        // cancel old fetch operation, Does nothing if fetch not ongoing
+        cancelFetch();
+        
+        // Fetch the message.
+        mMsgFetchOperation = mUiEngine.fetchMessage(
+            fetchMsg->envelope().mailboxId(),
+            fetchMsg->envelope().folderId(),
+            fetchMsg->envelope().messageId());
+        
+        mAttaId = 0;
+        mIsFetching = true;
+
+        if (mMsgFetchOperation) {
+            QObject::connect(mMsgFetchOperation,
+                    SIGNAL(operationCompleted(int)),
+                    this,
+                    SLOT(messageFetched(int)));
+        }
+        
+    }
+    else {
+        messageFetched(NmNoError);
+    }
+}
+
+/*!
     Retruns true if fetch operation is ongoing
 */
 bool NmAttachmentManager::isFetching() const
@@ -100,6 +179,9 @@
     if (mFetchOperation && mFetchOperation->isRunning()) { 
         mFetchOperation->cancelOperation();
     }
+    if (mMsgFetchOperation && mMsgFetchOperation->isRunning()) {
+        mMsgFetchOperation->cancelOperation();
+    }
     mIsFetching = false;
     mAttaId = 0;
     mProgressValue = 0;
@@ -130,6 +212,52 @@
 }
 
 /*!
+    Used by message fetch operation
+*/
+void NmAttachmentManager::messageFetched(int result)
+{
+    QObject::disconnect(mFetchOperation,
+                SIGNAL(operationCompleted(int)),
+                this,
+                SLOT(messageFetched(int)));
+    
+    if (result == NmNoError) {
+        
+        // Reload message
+        fetchMsg = mUiEngine.message(
+            fetchMsg->envelope().mailboxId(),
+            fetchMsg->envelope().folderId(),
+            fetchMsg->envelope().messageId());
+        
+        if (fetchMsg) {
+            QList<NmId> partIds;
+            NmMessagePart *part;
+            foreach (part, fetchMsg->childParts()) {
+                if (part->size() > part->fetchedSize()) {
+                    partIds.append(part->partId());
+                }
+            }
+            if (partIds.count() > 0) {
+                fetchAttachments(
+                    fetchMsg->envelope().mailboxId(),
+                    fetchMsg->envelope().folderId(),
+                    fetchMsg->envelope().messageId(),
+                    partIds);
+            }
+            else {
+                mFetchObserver->fetchCompleted(result);
+            }
+        }
+        else {
+            mFetchObserver->fetchCompleted(NmNotFoundError);
+        }
+    }
+    else {
+        mFetchObserver->fetchCompleted(result);
+    }
+}
+
+/*!
     Sets fetch observer
 */
 void NmAttachmentManager::setObserver(NmAttachmentFetchObserver *observer)