emailuis/nmailui/src/nmattachmentmanager.cpp
changeset 30 759dc5235cdb
parent 27 9ba4404ef423
child 44 c2d07d913565
equal deleted inserted replaced
27:9ba4404ef423 30:759dc5235cdb
    28 
    28 
    29 */
    29 */
    30 NmAttachmentManager::NmAttachmentManager(NmUiEngine &uiEngine) :
    30 NmAttachmentManager::NmAttachmentManager(NmUiEngine &uiEngine) :
    31     mUiEngine(uiEngine),
    31     mUiEngine(uiEngine),
    32     mFetchOperation(NULL),
    32     mFetchOperation(NULL),
       
    33     mMsgFetchOperation(NULL),
    33     mFetchObserver(NULL),
    34     mFetchObserver(NULL),
    34     mAttaId(0),
    35     mAttaId(0),
    35     mProgressValue(0),
    36     mProgressValue(0),
    36     mIsFetching(false)
    37     mIsFetching(false)
    37 {
    38 {
    38 
    39     NM_FUNCTION;
    39 }
    40 }
    40 
    41 
    41 /*!
    42 /*!
    42 
    43 
    43 */
    44 */
    44 NmAttachmentManager::~NmAttachmentManager()
    45 NmAttachmentManager::~NmAttachmentManager()
    45 {
    46 {
    46     // cancel fetch
    47     NM_FUNCTION;
       
    48     
       
    49     // fetch operation deleted in cancel fetch
    47     cancelFetch();
    50     cancelFetch();
    48 }
    51 }
    49 
    52 
    50 /*!
    53 /*!
    51     Starts attachment fetch. If earlier fetch operation exist it is cancelled.
    54     Starts attachment fetch. If earlier fetch operation exist it is cancelled.
    55         const NmId &mailboxId, 
    58         const NmId &mailboxId, 
    56         const NmId &folderId, 
    59         const NmId &folderId, 
    57         const NmId &messageId, 
    60         const NmId &messageId, 
    58         const NmId &messagePartId)
    61         const NmId &messagePartId)
    59 {
    62 {
       
    63     NM_FUNCTION;
    60     // cancel old fetch operation, Does nothing if fetch not ongoing
    64     // cancel old fetch operation, Does nothing if fetch not ongoing
    61     cancelFetch();
    65     cancelFetch();
    62 
    66 
    63     mFetchOperation = mUiEngine.fetchMessagePart(
    67     mFetchOperation = mUiEngine.fetchMessagePart(
    64             mailboxId, folderId, messageId, messagePartId);
    68             mailboxId, folderId, messageId, messagePartId);
    65     
    69     
    66     if (mFetchOperation) {
    70     if (mFetchOperation) {
    67         mAttaId = messagePartId;
    71         mAttaId = messagePartId;
    68         mIsFetching = true;
    72         mIsFetching = true;
    69         QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
    73         QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
    70                 this, SLOT(attachmentFetchCompleted(int)));
    74                 this, SLOT(completeAttachmentFetch(int)));
    71         
    75         
    72         QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
    76         QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
    73                 this, SLOT(changeProgress(int)));
    77                 this, SLOT(changeProgress(int)));
    74         // set progress to 5 % already in start
    78         // set progress to 5 % already in start
    75         changeProgress(NmAttachmentManagerInitialProgressPercent);
    79         changeProgress(NmAttachmentManagerInitialProgressPercent);
    77 }
    81 }
    78 
    82 
    79 /*!
    83 /*!
    80     Fetch attachments to a message. 
    84     Fetch attachments to a message. 
    81     Set observer with setObserver method to get process and complete events
    85     Set observer with setObserver method to get process and complete events
    82 */
    86     
    83 void NmAttachmentManager::fetchAttachments(
    87     \return bool true if fetch started, otherwise false.
       
    88 */
       
    89 bool NmAttachmentManager::fetchAttachments(
    84         const NmId &mailboxId, 
    90         const NmId &mailboxId, 
    85         const NmId &folderId, 
    91         const NmId &folderId, 
    86         const NmId &messageId,
    92         const NmId &messageId,
    87         QList<NmId> &messagePartIds)
    93         QList<NmId> &messagePartIds)
    88 {
    94 {
       
    95     NM_FUNCTION;
       
    96     bool result = false;
    89     // cancel old fetch operation, Does nothing if fetch not ongoing
    97     // cancel old fetch operation, Does nothing if fetch not ongoing
    90     // We don't wan't to cancel message fetching operation, because
    98     cancelFetch();
    91     // it might still be finishing.
       
    92     if (!mMsgFetchOperation) {
       
    93         cancelFetch();
       
    94     }
       
    95     
    99     
    96     if (messagePartIds.count() > 0) {
   100     if (messagePartIds.count() > 0) {
    97         mFetchOperation = mUiEngine.fetchMessageParts(
   101         mFetchOperation = mUiEngine.fetchMessageParts(
    98             mailboxId,
   102             mailboxId,
    99             folderId,
   103             folderId,
   100             messageId,
   104             messageId,
   101             messagePartIds);
   105             messagePartIds);
   102     }
   106         
   103     
   107         if (mFetchOperation) {
   104     if (mFetchOperation) {
   108             mAttaId = 0;
   105         mAttaId = 0;
   109             mIsFetching = true;
   106         mIsFetching = true;
   110             QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
   107         QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
   111                     this, SLOT(completeAttachmentFetch(int)));
   108                 this, SLOT(attachmentFetchCompleted(int)));
   112             
   109         
   113             QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
   110         QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
   114                     this, SLOT(changeProgress(int)));
   111                 this, SLOT(changeProgress(int)));
   115         
   112         
   116             result = true;
   113     }
   117         }
       
   118     }
       
   119     return result;
   114 }
   120 }
   115 
   121 
   116 /*!
   122 /*!
   117     Fetch all message parts to a message. Fetches also message part data structure,
   123     Fetch all message parts to a message. Fetches also message part data structure,
   118     if not fetched already.
   124     if not fetched already.
   122 void NmAttachmentManager::fetchAllMessageParts(
   128 void NmAttachmentManager::fetchAllMessageParts(
   123         const NmId &mailboxId, 
   129         const NmId &mailboxId, 
   124         const NmId &folderId, 
   130         const NmId &folderId, 
   125         const NmId &messageId)
   131         const NmId &messageId)
   126 {
   132 {
   127     fetchMsg = mUiEngine.message(mailboxId,folderId,messageId);
   133     NM_FUNCTION;
       
   134     mFetchMsg = mUiEngine.message(mailboxId,folderId,messageId);
   128     
   135     
   129     // Check if we have part data structure.    
   136     // Check if we have part data structure.    
   130     if (fetchMsg->childParts().count() == 0 &&
   137     if (mFetchMsg->childParts().count() == 0 &&
   131         fetchMsg->fetchedSize() < fetchMsg->size()) {
   138         mFetchMsg->fetchedSize() < mFetchMsg->size()) {
   132 
   139 
   133         // cancel old fetch operation, Does nothing if fetch not ongoing
   140         // cancel old fetch operation, Does nothing if fetch not ongoing
   134         cancelFetch();
   141         cancelFetch();
   135         
   142         
   136         // Fetch the message.
   143         // Fetch the message.
   137         mMsgFetchOperation = mUiEngine.fetchMessage(
   144         mMsgFetchOperation = mUiEngine.fetchMessage(
   138             fetchMsg->envelope().mailboxId(),
   145             mFetchMsg->envelope().mailboxId(),
   139             fetchMsg->envelope().folderId(),
   146             mFetchMsg->envelope().folderId(),
   140             fetchMsg->envelope().messageId());
   147             mFetchMsg->envelope().messageId());
   141         
   148         
   142         mAttaId = 0;
   149         mAttaId = 0;
   143         mIsFetching = true;
   150         mIsFetching = true;
   144 
   151 
   145         if (mMsgFetchOperation) {
   152         if (mMsgFetchOperation) {
   146             QObject::connect(mMsgFetchOperation,
   153             QObject::connect(mMsgFetchOperation,
   147                     SIGNAL(operationCompleted(int)),
   154                     SIGNAL(operationCompleted(int)),
   148                     this,
   155                     this,
   149                     SLOT(messageFetched(int)));
   156                     SLOT(completeMessageFetch(int)));
   150         }
   157         }
   151         
   158         
   152     }
   159     }
   153     else {
   160     else {
   154         messageFetched(NmNoError);
   161         completeMessageFetch(NmNoError);
   155     }
   162     }
   156 }
   163 }
   157 
   164 
   158 /*!
   165 /*!
   159     Retruns true if fetch operation is ongoing
   166     Retruns true if fetch operation is ongoing
   160 */
   167 */
   161 bool NmAttachmentManager::isFetching() const
   168 bool NmAttachmentManager::isFetching() const
   162 {
   169 {
       
   170     NM_FUNCTION;
       
   171     
   163     return mIsFetching;
   172     return mIsFetching;
   164 }
   173 }
   165 
   174 
   166 /*!
   175 /*!
   167     Retrunrs part id of attachment if fetch operation is ongoing. Zero id is returned otherwise
   176     Retrunrs part id of attachment if fetch operation is ongoing. Zero id is returned otherwise
   168 */
   177 */
   169 NmId NmAttachmentManager::partIdUnderFetch() const
   178 NmId NmAttachmentManager::partIdUnderFetch() const
   170 {
   179 {
       
   180     NM_FUNCTION;
       
   181     
   171     return mAttaId;
   182     return mAttaId;
   172 }
   183 }
   173 
   184 
   174 /*!
   185 /*!
   175     Cancels fetch operation. Does nothing if fetch not ongoing
   186     Cancels fetch operation. Does nothing if fetch not ongoing
   176 */
   187 */
   177 void NmAttachmentManager::cancelFetch()
   188 void NmAttachmentManager::cancelFetch()
   178 {
   189 {
       
   190     NM_FUNCTION;
   179     if (mFetchOperation && mFetchOperation->isRunning()) { 
   191     if (mFetchOperation && mFetchOperation->isRunning()) { 
   180         mFetchOperation->cancelOperation();
   192         mFetchOperation->cancelOperation();
   181     }
   193     }
   182     if (mMsgFetchOperation && mMsgFetchOperation->isRunning()) {
   194     if (mMsgFetchOperation && mMsgFetchOperation->isRunning()) {
   183         mMsgFetchOperation->cancelOperation();
   195         mMsgFetchOperation->cancelOperation();
   190 /*!
   202 /*!
   191     Used by message part fetch operation
   203     Used by message part fetch operation
   192 */
   204 */
   193 void NmAttachmentManager::changeProgress(int value)
   205 void NmAttachmentManager::changeProgress(int value)
   194 {
   206 {
       
   207     NM_FUNCTION;
       
   208     
   195     if (mFetchObserver && value > mProgressValue) {
   209     if (mFetchObserver && value > mProgressValue) {
   196         mProgressValue = value;
   210         mProgressValue = value;
   197         mFetchObserver->progressChanged(value);
   211         mFetchObserver->progressChanged(value);
   198     }
   212     }
   199 }
   213 }
   200 
   214 
   201 /*!
   215 /*!
   202     Used by message part fetch operation
   216     Used by message part fetch operation
   203 */
   217 */
   204 void NmAttachmentManager::attachmentFetchCompleted(int result)
   218 void NmAttachmentManager::completeAttachmentFetch(int result)
   205 {
   219 {
       
   220     NM_FUNCTION;
       
   221     
   206     if (mFetchObserver) {
   222     if (mFetchObserver) {
   207         mFetchObserver->fetchCompleted(result);
   223         mFetchObserver->fetchCompleted(result);
   208     }
   224     }
   209     mAttaId = 0;
   225     mAttaId = 0;
   210     mProgressValue = 0;
   226     mProgressValue = 0;
   212 }
   228 }
   213 
   229 
   214 /*!
   230 /*!
   215     Used by message fetch operation
   231     Used by message fetch operation
   216 */
   232 */
   217 void NmAttachmentManager::messageFetched(int result)
   233 void NmAttachmentManager::completeMessageFetch(int result)
   218 {
   234 {
   219     QObject::disconnect(mFetchOperation,
   235     NM_FUNCTION;
   220                 SIGNAL(operationCompleted(int)),
       
   221                 this,
       
   222                 SLOT(messageFetched(int)));
       
   223     
   236     
   224     if (result == NmNoError) {
   237     if (result == NmNoError) {
   225         
   238         
   226         // Reload message
   239         // Reload message
   227         fetchMsg = mUiEngine.message(
   240         mFetchMsg = mUiEngine.message(
   228             fetchMsg->envelope().mailboxId(),
   241             mFetchMsg->envelope().mailboxId(),
   229             fetchMsg->envelope().folderId(),
   242             mFetchMsg->envelope().folderId(),
   230             fetchMsg->envelope().messageId());
   243             mFetchMsg->envelope().messageId());
   231         
   244         
   232         if (fetchMsg) {
   245         if (mFetchMsg) {
   233             QList<NmId> partIds;
   246             QList<NmId> partIds;
   234             NmMessagePart *part;
   247             NmMessagePart *part;
   235             foreach (part, fetchMsg->childParts()) {
   248             foreach (part, mFetchMsg->childParts()) {
   236                 if (part->size() > part->fetchedSize()) {
   249                 if (part->size() > part->fetchedSize()) {
   237                     partIds.append(part->partId());
   250                     partIds.append(part->partId());
   238                 }
   251                 }
   239             }
   252             }
   240             if (partIds.count() > 0) {
   253             if (partIds.count() > 0) {
       
   254                 mMsgFetchOperation = NULL;
   241                 fetchAttachments(
   255                 fetchAttachments(
   242                     fetchMsg->envelope().mailboxId(),
   256                     mFetchMsg->envelope().mailboxId(),
   243                     fetchMsg->envelope().folderId(),
   257                     mFetchMsg->envelope().folderId(),
   244                     fetchMsg->envelope().messageId(),
   258                     mFetchMsg->envelope().messageId(),
   245                     partIds);
   259                     partIds);
       
   260                 
       
   261                 if (mFetchOperation) {
       
   262                     mAttaId = 0;
       
   263                     mIsFetching = true;
       
   264                     QObject::connect(mFetchOperation, SIGNAL(operationCompleted(int)),
       
   265                             this, SLOT(completeAttachmentFetch(int)));
       
   266                     
       
   267                     QObject::connect(mFetchOperation, SIGNAL(operationProgressChanged(int)),
       
   268                             this, SLOT(changeProgress(int)));
       
   269                 
       
   270                     result = true;
       
   271                 }
   246             }
   272             }
   247             else {
   273             else {
   248                 mFetchObserver->fetchCompleted(result);
   274                 mFetchObserver->fetchCompleted(NmNoError);
   249             }
   275             }
   250         }
   276         }
   251         else {
   277         else {
   252             mFetchObserver->fetchCompleted(NmNotFoundError);
   278             mFetchObserver->fetchCompleted(NmNotFoundError);
   253         }
   279         }
   260 /*!
   286 /*!
   261     Sets fetch observer
   287     Sets fetch observer
   262 */
   288 */
   263 void NmAttachmentManager::setObserver(NmAttachmentFetchObserver *observer)
   289 void NmAttachmentManager::setObserver(NmAttachmentFetchObserver *observer)
   264 {
   290 {
       
   291     NM_FUNCTION;
       
   292     
   265     mFetchObserver = observer;
   293     mFetchObserver = observer;
   266     // send progress event wheng observer changes if fetch ongoing 
   294     // send progress event wheng observer changes if fetch ongoing 
   267     // to get progress bar updating
   295     // to get progress bar updating
   268     if (mIsFetching) {
   296     if (mIsFetching) {
   269         changeProgress(mProgressValue);
   297         changeProgress(mProgressValue);
   273 /*!
   301 /*!
   274     Clear observer
   302     Clear observer
   275 */
   303 */
   276 void NmAttachmentManager::clearObserver()
   304 void NmAttachmentManager::clearObserver()
   277 {
   305 {
       
   306     NM_FUNCTION;
       
   307     
   278     mFetchObserver = NULL;
   308     mFetchObserver = NULL;
   279 }
   309 }
   280 
   310 
   281 /*!
   311 /*!
   282     Returns progress value if fetch ongoing. Otherwise returns 0.
   312     Returns progress value if fetch ongoing. Otherwise returns 0.
   283 */
   313 */
   284 int NmAttachmentManager::progressValue() const
   314 int NmAttachmentManager::progressValue() const
   285 {
   315 {
       
   316     NM_FUNCTION;
       
   317     
   286     return mProgressValue;
   318     return mProgressValue;
   287 }
   319 }
   288 
   320