emailuis/nmailui/src/nmattachmentmanager.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmuiheaders.h"
       
    19 
       
    20 static const int NmAttachmentManagerInitialProgressPercent = 5;
       
    21 
       
    22 /*!
       
    23     \class NmAttachmentManager
       
    24     \brief Attachment download manager, shares attachment download between editor and viewer
       
    25 */
       
    26 
       
    27 /*!
       
    28 
       
    29 */
       
    30 NmAttachmentManager::NmAttachmentManager(NmUiEngine &uiEngine) :
       
    31     mUiEngine(uiEngine),
       
    32     mFetchOperation(NULL),
       
    33     mMsgFetchOperation(NULL),
       
    34     mFetchObserver(NULL),
       
    35     mFetchMsg(NULL),
       
    36     mAttaId(0),
       
    37     mProgressValue(0),
       
    38     mIsFetching(false)
       
    39 {
       
    40     NM_FUNCTION;
       
    41 }
       
    42 
       
    43 /*!
       
    44 
       
    45 */
       
    46 NmAttachmentManager::~NmAttachmentManager()
       
    47 {
       
    48     NM_FUNCTION;
       
    49     
       
    50     // fetch operation deleted in cancel fetch
       
    51     cancelFetch();
       
    52     
       
    53     delete mFetchMsg;
       
    54     mFetchMsg = NULL;
       
    55 }
       
    56 
       
    57 /*!
       
    58     Starts attachment fetch. If earlier fetch operation exist it is cancelled.
       
    59     Set observer with setObserver method to get process and complete events
       
    60 */
       
    61 void NmAttachmentManager::fetchAttachment(
       
    62         const NmId &mailboxId, 
       
    63         const NmId &folderId, 
       
    64         const NmId &messageId, 
       
    65         const NmId &messagePartId)
       
    66 {
       
    67     NM_FUNCTION;
       
    68     // cancel old fetch operation, Does nothing if fetch not ongoing
       
    69     cancelFetch();
       
    70 
       
    71     mFetchOperation = mUiEngine.fetchMessagePart(
       
    72             mailboxId, folderId, messageId, messagePartId);
       
    73     
       
    74     if (mFetchOperation) {
       
    75         mAttaId = messagePartId;
       
    76         mIsFetching = true;
       
    77         QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
       
    78                 this, SLOT(completeAttachmentFetch(int)));
       
    79         
       
    80         QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
       
    81                 this, SLOT(changeProgress(int)));
       
    82         // set progress to 5 % already in start
       
    83         changeProgress(NmAttachmentManagerInitialProgressPercent);
       
    84     }
       
    85 }
       
    86 
       
    87 /*!
       
    88     Fetch attachments to a message. 
       
    89     Set observer with setObserver method to get progress and complete events
       
    90     
       
    91     \return bool true if fetch started, otherwise false.
       
    92 */
       
    93 bool NmAttachmentManager::fetchAttachments(
       
    94         const NmId &mailboxId, 
       
    95         const NmId &folderId, 
       
    96         const NmId &messageId,
       
    97         QList<NmId> &messagePartIds)
       
    98 {
       
    99     NM_FUNCTION;
       
   100     bool result(false);
       
   101     // cancel old fetch operation, Does nothing if fetch not ongoing
       
   102     cancelFetch();
       
   103     
       
   104     if (messagePartIds.count() > 0) {
       
   105         mFetchOperation = mUiEngine.fetchMessageParts(
       
   106             mailboxId,
       
   107             folderId,
       
   108             messageId,
       
   109             messagePartIds);
       
   110         
       
   111         if (mFetchOperation) {
       
   112             mAttaId = 0;
       
   113             mIsFetching = true;
       
   114             QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
       
   115                     this, SLOT(completeAttachmentFetch(int)));
       
   116             
       
   117             QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
       
   118                     this, SLOT(changeProgress(int)));
       
   119         
       
   120             result = true;
       
   121         }
       
   122     }
       
   123     return result;
       
   124 }
       
   125 
       
   126 /*!
       
   127     Fetch all message parts to a message. Fetches also message part data structure,
       
   128     if not fetched already.
       
   129       If earlier fetch operation exist it is cancelled and deleted.
       
   130     Set observer with setObserver method to get progress and complete events
       
   131 */
       
   132 void NmAttachmentManager::fetchAllMessageParts(
       
   133         const NmId &mailboxId, 
       
   134         const NmId &folderId, 
       
   135         const NmId &messageId)
       
   136 {
       
   137     NM_FUNCTION;
       
   138     mFetchMsg = mUiEngine.message(mailboxId,folderId,messageId);
       
   139     
       
   140     // Check if we have part data structure.    
       
   141     if (mFetchMsg && mFetchMsg->childParts().count() == 0 &&
       
   142         mFetchMsg->fetchedSize() < mFetchMsg->size()) {
       
   143 
       
   144         // cancel old fetch operation, Does nothing if fetch not ongoing
       
   145         cancelFetch();
       
   146         
       
   147         // Fetch the message.
       
   148         mMsgFetchOperation = mUiEngine.fetchMessage(
       
   149             mFetchMsg->envelope().mailboxId(),
       
   150             mFetchMsg->envelope().folderId(),
       
   151             mFetchMsg->envelope().messageId());
       
   152         
       
   153         mAttaId = 0;
       
   154         mIsFetching = true;
       
   155 
       
   156         if (mMsgFetchOperation) {
       
   157             QObject::connect(mMsgFetchOperation,
       
   158                     SIGNAL(operationCompleted(int)),
       
   159                     this,
       
   160                     SLOT(completeMessageFetch(int)));
       
   161         }
       
   162         else {
       
   163             completeMessageFetch(NmGeneralError);
       
   164         }
       
   165         
       
   166     }
       
   167     else {
       
   168         completeMessageFetch(NmNoError);
       
   169     }
       
   170 }
       
   171 
       
   172 /*!
       
   173     Retruns true if fetch operation is ongoing
       
   174 */
       
   175 bool NmAttachmentManager::isFetching() const
       
   176 {
       
   177     NM_FUNCTION;
       
   178     
       
   179     return mIsFetching;
       
   180 }
       
   181 
       
   182 /*!
       
   183     Retrunrs part id of attachment if fetch operation is ongoing. Zero id is returned otherwise
       
   184 */
       
   185 NmId NmAttachmentManager::partIdUnderFetch() const
       
   186 {
       
   187     NM_FUNCTION;
       
   188     
       
   189     return mAttaId;
       
   190 }
       
   191 
       
   192 /*!
       
   193     Cancels fetch operation. Does nothing if fetch not ongoing
       
   194 */
       
   195 void NmAttachmentManager::cancelFetch()
       
   196 {
       
   197     NM_FUNCTION;
       
   198     if (mFetchOperation && mFetchOperation->isRunning()) { 
       
   199         mFetchOperation->cancelOperation();
       
   200     }
       
   201     if (mMsgFetchOperation && mMsgFetchOperation->isRunning()) {
       
   202         mMsgFetchOperation->cancelOperation();
       
   203     }
       
   204     mIsFetching = false;
       
   205     mAttaId = 0;
       
   206     mProgressValue = 0;
       
   207 }
       
   208 
       
   209 /*!
       
   210     Used by message part fetch operation
       
   211 */
       
   212 void NmAttachmentManager::changeProgress(int value)
       
   213 {
       
   214     NM_FUNCTION;
       
   215     
       
   216     if (mFetchObserver && value > mProgressValue) {
       
   217         mProgressValue = value;
       
   218         mFetchObserver->progressChanged(value);
       
   219     }
       
   220 }
       
   221 
       
   222 /*!
       
   223     Used by message part fetch operation
       
   224 */
       
   225 void NmAttachmentManager::completeAttachmentFetch(int result)
       
   226 {
       
   227     NM_FUNCTION;
       
   228     
       
   229     if (mFetchObserver) {
       
   230         mFetchObserver->fetchCompleted(result);
       
   231     }
       
   232     mAttaId = 0;
       
   233     mProgressValue = 0;
       
   234     mIsFetching = false;
       
   235 }
       
   236 
       
   237 /*!
       
   238     Used by message fetch operation
       
   239 */
       
   240 void NmAttachmentManager::completeMessageFetch(int result)
       
   241 {
       
   242     NM_FUNCTION;
       
   243     
       
   244     if (result == NmNoError) {
       
   245         
       
   246         if (mFetchMsg) {
       
   247             
       
   248             NmId mailboxId = mFetchMsg->envelope().mailboxId();
       
   249             NmId folderId = mFetchMsg->envelope().folderId();
       
   250             NmId messageId = mFetchMsg->envelope().messageId();
       
   251             
       
   252             // Delete object
       
   253             delete mFetchMsg;
       
   254             mFetchMsg = NULL;
       
   255             
       
   256             // Reload message
       
   257             mFetchMsg = mUiEngine.message(mailboxId,folderId,messageId);
       
   258             
       
   259             if (mFetchMsg) {
       
   260                 QList<NmId> partIds;
       
   261                 NmMessagePart *part;
       
   262                 foreach (part, mFetchMsg->childParts()) {
       
   263                     if (part->size() > part->fetchedSize()) {
       
   264                         partIds.append(part->partId());
       
   265                     }
       
   266                 }
       
   267                 if (partIds.count() > 0) {
       
   268                     mMsgFetchOperation = NULL;
       
   269                     fetchAttachments(
       
   270                         mFetchMsg->envelope().mailboxId(),
       
   271                         mFetchMsg->envelope().folderId(),
       
   272                         mFetchMsg->envelope().messageId(),
       
   273                         partIds);
       
   274                 }
       
   275                 else {
       
   276                     mFetchObserver->fetchCompleted(NmNoError);
       
   277                 }
       
   278                 // Delete object
       
   279                 delete mFetchMsg;
       
   280                 mFetchMsg = NULL;
       
   281             }
       
   282             else {
       
   283                 mFetchObserver->fetchCompleted(NmNotFoundError);
       
   284             }
       
   285         }
       
   286         else {
       
   287             mFetchObserver->fetchCompleted(NmNotFoundError);
       
   288         }
       
   289     }
       
   290     else {
       
   291         mFetchObserver->fetchCompleted(result);
       
   292     }
       
   293 }
       
   294 
       
   295 /*!
       
   296     Sets fetch observer
       
   297 */
       
   298 void NmAttachmentManager::setObserver(NmAttachmentFetchObserver *observer)
       
   299 {
       
   300     NM_FUNCTION;
       
   301     
       
   302     mFetchObserver = observer;
       
   303     // send progress event wheng observer changes if fetch ongoing 
       
   304     // to get progress bar updating
       
   305     if (mIsFetching) {
       
   306         changeProgress(mProgressValue);
       
   307     }
       
   308 }
       
   309 
       
   310 /*!
       
   311     Clear observer
       
   312 */
       
   313 void NmAttachmentManager::clearObserver()
       
   314 {
       
   315     NM_FUNCTION;
       
   316     
       
   317     mFetchObserver = NULL;
       
   318 }
       
   319 
       
   320 /*!
       
   321     Returns progress value if fetch ongoing. Otherwise returns 0.
       
   322 */
       
   323 int NmAttachmentManager::progressValue() const
       
   324 {
       
   325     NM_FUNCTION;
       
   326     
       
   327     return mProgressValue;
       
   328 }