emailuis/nmframeworkadapter/src/nmframeworkadapter.cpp
changeset 27 9ba4404ef423
parent 23 2dc6caa42ec3
child 30 759dc5235cdb
equal deleted inserted replaced
23:2dc6caa42ec3 27:9ba4404ef423
   274     else {
   274     else {
   275         return err;
   275         return err;
   276     }
   276     }
   277 }
   277 }
   278 
   278 
       
   279 
       
   280 /*!
       
   281     Fetches all the messages from the given folder and appends their meta data
       
   282     into the given list.
       
   283 
       
   284     \param folder The folder instance.
       
   285     \param messageEnvelopeList The list where the data is stored to.
       
   286     \param maxEnvelopeCount The maximum number of messages to get.
       
   287 */
       
   288 void NmFrameworkAdapter::getMessagesFromFolderL(
       
   289      CFSMailFolder *folder,
       
   290      QList<NmMessageEnvelope*> &messageEnvelopeList,
       
   291      const int maxEnvelopeCount)
       
   292 {
       
   293     if (!folder || maxEnvelopeCount < 1) {
       
   294         return;
       
   295     }
       
   296 
       
   297     int blockSize = NmListMessagesBlock;
       
   298     int maxItemCount = NmMaxItemsInMessageList;
       
   299 
       
   300     if (maxEnvelopeCount < NmMaxItemsInMessageList) {
       
   301         maxItemCount = maxEnvelopeCount;
       
   302 
       
   303         if(maxEnvelopeCount < NmListMessagesBlock) {
       
   304             blockSize = maxEnvelopeCount;
       
   305         }
       
   306     }
       
   307 
       
   308     // First prepare all the parameters and select message details to be listed.
       
   309     TFSMailDetails details(EFSMsgDataEnvelope);
       
   310 
       
   311     // Set the sorting criteria.
       
   312     TFSMailSortCriteria criteria;
       
   313     criteria.iField = EFSMailSortByDate;
       
   314     criteria.iOrder = EFSMailDescending;
       
   315     RArray<TFSMailSortCriteria> sorting;
       
   316     CleanupClosePushL(sorting);
       
   317     sorting.Append(criteria);
       
   318    
       
   319     // Get the message list from the backend.
       
   320     MFSMailIterator* iterator(NULL);
       
   321     iterator = folder->ListMessagesL(details, sorting);
       
   322 
       
   323     if (iterator) {
       
   324         CleanupStack::PushL(iterator);
       
   325         RPointerArray<CFSMailMessage> messages;
       
   326         CleanupResetAndDestroy<CFSMailMessage>::PushL(messages);
       
   327 
       
   328         // The message list is fetched in blocks to prevent OOM in protocol
       
   329         // plugin side.
       
   330         bool moreMessagesToFollow(false);
       
   331         moreMessagesToFollow = iterator->NextL(TFSMailMsgId(), blockSize, messages);
       
   332 
       
   333         for (int i = blockSize;
       
   334              i < maxItemCount && moreMessagesToFollow;
       
   335              i += blockSize) {
       
   336             moreMessagesToFollow =
       
   337                 iterator->NextL(messages[i-1]->GetMessageId(), blockSize, messages);
       
   338         }
       
   339 
       
   340         // Add all the found emails into the result list.
       
   341         const TInt messageCount(messages.Count());
       
   342 
       
   343         for (TInt i = 0; i < messageCount; ++i) {
       
   344             NmMessageEnvelope *newEnvelope(NULL);
       
   345             newEnvelope = messages[i]->GetNmMessageEnvelope();
       
   346 
       
   347             if (newEnvelope) {
       
   348                 messageEnvelopeList.append(newEnvelope);
       
   349             }
       
   350         }
       
   351 
       
   352         CleanupStack::PopAndDestroy(&messages);
       
   353         CleanupStack::Pop(iterator);
       
   354         delete iterator;
       
   355         iterator = NULL;
       
   356     }
       
   357 
       
   358     CleanupStack::PopAndDestroy(); // sorting  
       
   359 }
       
   360 
       
   361 
   279 /*!
   362 /*!
   280     Returns list of envelopes from the backend for specific mailbox and folder.
   363     Returns list of envelopes from the backend for specific mailbox and folder.
   281 
   364 
   282     \param mailboxId Id of the mailbox containing the folder.
   365     \param mailboxId Id of the mailbox containing the folder.
   283     \param folderId Folder id.
   366     \param folderId Folder id.
   293 {
   376 {
   294     TRAPD(err, listMessagesL(mailboxId,folderId,messageEnvelopeList, NmMaxItemsInMessageList));
   377     TRAPD(err, listMessagesL(mailboxId,folderId,messageEnvelopeList, NmMaxItemsInMessageList));
   295     return err;
   378     return err;
   296 }
   379 }
   297 
   380 
       
   381 
       
   382 /*!
       
   383     Fetches the meta data for each message in the given mailbox and given
       
   384     folder.
       
   385 
       
   386     \param mailboxId The ID of the mailbox of which messages to list.
       
   387     \param folderId The ID of the folder of which messages to list.
       
   388     \param messageEnvelopeList The list where the message data is stored to.
       
   389                                Note that the ownership is transferred!
       
   390     \param maxAmountOfEnvelopes The maximum number of messages to list.
       
   391 
       
   392     \return If success, KErrNone, an error code otherwise.
       
   393 */
   298 int NmFrameworkAdapter::listMessages(
   394 int NmFrameworkAdapter::listMessages(
   299         const NmId& mailboxId,
   395         const NmId& mailboxId,
   300         const NmId& folderId,
   396         const NmId& folderId,
   301         QList<NmMessageEnvelope*> &messageEnvelopeList,
   397         QList<NmMessageEnvelope*> &messageEnvelopeList,
   302         const int maxAmountOfEnvelopes)
   398         const int maxAmountOfEnvelopes)
   303     {
   399 {
   304     TInt err = KErrNone;
   400     TInt err = KErrNone;
   305     TRAP(err, listMessagesL(mailboxId,folderId, messageEnvelopeList,maxAmountOfEnvelopes) );
   401     TRAP(err, listMessagesL(mailboxId,folderId, messageEnvelopeList,maxAmountOfEnvelopes) );
   306     return err;
   402     return err;
   307     }
   403 }
   308 
   404 
   309 /*!
   405 
   310     Leaving version of list messages
   406 /*!
   311  */
   407     Fetches the meta data for each message in the given mailbox and given
       
   408     folder. Note that this private method can leave.
       
   409 */
   312 void NmFrameworkAdapter::listMessagesL(
   410 void NmFrameworkAdapter::listMessagesL(
   313         const NmId &mailboxId,
   411         const NmId &mailboxId,
   314         const NmId &folderId,
   412         const NmId &folderId,
   315         QList<NmMessageEnvelope*> &messageEnvelopeList,
   413         QList<NmMessageEnvelope*> &messageEnvelopeList,
   316         const int maxAmountOfEnvelopes)
   414         const int maxAmountOfEnvelopes)
   317 {
   415 {
       
   416     // If we are requesting 0 or less mails, we can just return.
       
   417     if (maxAmountOfEnvelopes <= 0) {
       
   418         return;
       
   419     }
       
   420 
       
   421     CFSMailBox *mailbox(NULL);
       
   422     mailbox = mFSfw->GetMailBoxByUidL(mailboxId);
       
   423 
       
   424     if (!mailbox) {
       
   425         User::Leave(KErrNotFound);
       
   426     }
       
   427 
       
   428     CleanupStack::PushL(mailbox);
       
   429 
       
   430     CFSMailFolder* folder(NULL);
       
   431     folder = mFSfw->GetFolderByUidL(mailbox->GetId(), TFSMailMsgId(folderId));
       
   432 
       
   433     if (folder) {
       
   434         CleanupStack::PushL(folder);
       
   435         getMessagesFromFolderL(folder, messageEnvelopeList, maxAmountOfEnvelopes);
       
   436         CleanupStack::PopAndDestroy(folder);        
       
   437     }
       
   438 
       
   439     CleanupStack::PopAndDestroy(mailbox);
       
   440 }
       
   441 
       
   442 
       
   443 /*! 
       
   444     Returns list of messages from the backend for specific mailbox and folder.
       
   445 
       
   446     \param mailboxId Id of the mailbox containing the folder.
       
   447     \param folderId Folder id.
       
   448     \param messageMetaDataList Reference to pointer list to receive the envelope objects,
       
   449      ownership is transferred.
       
   450 
       
   451     \return Error code.
       
   452  */
       
   453 int NmFrameworkAdapter::listMessages(
       
   454     const NmId &mailboxId,
       
   455     const NmId &folderId,
       
   456     QList<NmMessage*> &messageList,
       
   457     const int maxAmountOfMessages)
       
   458 {
       
   459     TRAPD(err, listMessagesL(mailboxId,folderId,messageList, maxAmountOfMessages));
       
   460     return err;
       
   461 }
       
   462 
       
   463 
       
   464 /*!
       
   465     Leaving version of list messages with NmMessageList input
       
   466  */
       
   467 void NmFrameworkAdapter::listMessagesL(
       
   468         const NmId &mailboxId,
       
   469         const NmId &folderId,
       
   470         QList<NmMessage*> &messageList,
       
   471         const int maxAmountOfEnvelopes)
       
   472 {
   318     CFSMailBox * currentMailbox(NULL);
   473     CFSMailBox * currentMailbox(NULL);
   319     CFSMailFolder* folder(NULL);
   474     CFSMailFolder* folder(NULL);
   320 
   475 
   321     //If we are requesting 0 or less mails so we can return
   476     //If we are requesting 0 or less mails so we can return
   322     if( maxAmountOfEnvelopes <= 0)
   477     if( maxAmountOfEnvelopes <= 0)
   324         return;
   479         return;
   325         }
   480         }
   326 
   481 
   327     int blockSize = NmListMessagesBlock;
   482     int blockSize = NmListMessagesBlock;
   328     int maxLimit = NmMaxItemsInMessageList;
   483     int maxLimit = NmMaxItemsInMessageList;
       
   484 
   329     if( maxAmountOfEnvelopes < NmMaxItemsInMessageList )
   485     if( maxAmountOfEnvelopes < NmMaxItemsInMessageList )
   330         {
   486         {
   331         maxLimit = maxAmountOfEnvelopes;
   487         maxLimit = maxAmountOfEnvelopes;
   332         if(maxAmountOfEnvelopes < NmListMessagesBlock)
   488         if(maxAmountOfEnvelopes < NmListMessagesBlock)
   333             {
   489             {
   334             blockSize = maxAmountOfEnvelopes;
   490             blockSize = maxAmountOfEnvelopes;
   335             }
   491             }
   336         }
   492         }
   337    
   493    
   338 
       
   339     currentMailbox = mFSfw->GetMailBoxByUidL(mailboxId);
       
   340     if (!currentMailbox) {
       
   341         User::Leave(KErrNotFound);
       
   342     }
       
   343     CleanupStack::PushL(currentMailbox);
       
   344     folder = mFSfw->GetFolderByUidL(currentMailbox->GetId(), TFSMailMsgId(folderId));
       
   345 
       
   346     if (folder) {
       
   347         CleanupStack::PushL(folder);
       
   348         // First prepare all the parameters
       
   349         // select message details to be listed
       
   350         TFSMailDetails details(EFSMsgDataEnvelope);
       
   351 
       
   352         // set sorting criteria
       
   353         TFSMailSortCriteria criteria;
       
   354         criteria.iField = EFSMailSortByDate;
       
   355         criteria.iOrder = EFSMailDescending;
       
   356         RArray<TFSMailSortCriteria> sorting;
       
   357         CleanupClosePushL(sorting);
       
   358         sorting.Append(criteria);
       
   359 
       
   360         TFSMailMsgId currentMessageId; // first call contains NULL id as begin id
       
   361         // get messages list from the backend
       
   362         MFSMailIterator* iterator(NULL);
       
   363 
       
   364         iterator = folder->ListMessagesL(details, sorting);
       
   365         if (iterator) {
       
   366             CleanupStack::PushL(iterator);
       
   367             RPointerArray<CFSMailMessage> messages;
       
   368             CleanupResetAndDestroy<CFSMailMessage>::PushL(messages);
       
   369 
       
   370             //Message list is fetched in blocks to prevent OOM in protocol plugin side
       
   371             bool moreMessagesToFollow(false);
       
   372             moreMessagesToFollow = iterator->NextL(
       
   373                 TFSMailMsgId(), blockSize, messages);
       
   374             for ( int i = blockSize;
       
   375                   i < maxLimit && moreMessagesToFollow ;
       
   376                   i += blockSize ) {
       
   377                 moreMessagesToFollow = iterator->NextL(
       
   378                     messages[i-1]->GetMessageId(), blockSize, messages);
       
   379             }
       
   380 
       
   381             //Add all found emails to the result list
       
   382             for(TInt i=0; i<messages.Count(); i++) {
       
   383                 NmMessageEnvelope* newEnvelope(NULL);
       
   384                 newEnvelope = messages[i]->GetNmMessageEnvelope();
       
   385                 if (newEnvelope) {
       
   386                     messageEnvelopeList.append(newEnvelope);
       
   387                 }
       
   388             }
       
   389             CleanupStack::PopAndDestroy( &messages );
       
   390             CleanupStack::Pop(iterator);
       
   391             delete iterator;
       
   392            	iterator = NULL;
       
   393         }
       
   394         CleanupStack::PopAndDestroy(); // sorting
       
   395         CleanupStack::PopAndDestroy(folder);
       
   396     }
       
   397     CleanupStack::PopAndDestroy(currentMailbox);
       
   398 }
       
   399 
       
   400 /*! 
       
   401     Returns list of messages from the backend for specific mailbox and folder.
       
   402 
       
   403     \param mailboxId Id of the mailbox containing the folder.
       
   404     \param folderId Folder id.
       
   405     \param messageMetaDataList Reference to pointer list to receive the envelope objects,
       
   406      ownership is transferred.
       
   407 
       
   408     \return Error code.
       
   409  */
       
   410 int NmFrameworkAdapter::listMessages(
       
   411     const NmId &mailboxId,
       
   412     const NmId &folderId,
       
   413     QList<NmMessage*> &messageList,
       
   414     const int maxAmountOfMessages)
       
   415 {
       
   416     TRAPD(err, listMessagesL(mailboxId,folderId,messageList, maxAmountOfMessages));
       
   417     return err;
       
   418 }
       
   419 
       
   420 /*!
       
   421     Leaving version of list messages with NmMessageList input
       
   422  */
       
   423 void NmFrameworkAdapter::listMessagesL(
       
   424         const NmId &mailboxId,
       
   425         const NmId &folderId,
       
   426         QList<NmMessage*> &messageList,
       
   427         const int maxAmountOfEnvelopes)
       
   428 {
       
   429     CFSMailBox * currentMailbox(NULL);
       
   430     CFSMailFolder* folder(NULL);
       
   431 
       
   432     //If we are requesting 0 or less mails so we can return
       
   433     if( maxAmountOfEnvelopes <= 0)
       
   434         {
       
   435         return;
       
   436         }
       
   437 
       
   438     int blockSize = NmListMessagesBlock;
       
   439     int maxLimit = NmMaxItemsInMessageList;
       
   440     if( maxAmountOfEnvelopes < NmMaxItemsInMessageList )
       
   441         {
       
   442         maxLimit = maxAmountOfEnvelopes;
       
   443         if(maxAmountOfEnvelopes < NmListMessagesBlock)
       
   444             {
       
   445             blockSize = maxAmountOfEnvelopes;
       
   446             }
       
   447         }
       
   448    
       
   449 
       
   450     currentMailbox = mFSfw->GetMailBoxByUidL(mailboxId);
   494     currentMailbox = mFSfw->GetMailBoxByUidL(mailboxId);
   451     if (!currentMailbox) {
   495     if (!currentMailbox) {
   452         User::Leave(KErrNotFound);
   496         User::Leave(KErrNotFound);
   453     }
   497     }
   454     CleanupStack::PushL(currentMailbox);
   498     CleanupStack::PushL(currentMailbox);
   514         CleanupStack::PopAndDestroy(folder);
   558         CleanupStack::PopAndDestroy(folder);
   515     }
   559     }
   516     CleanupStack::PopAndDestroy(currentMailbox);
   560     CleanupStack::PopAndDestroy(currentMailbox);
   517 }
   561 }
   518 
   562 
       
   563 
   519 /*!
   564 /*!
   520     Starts an asynchronous search for messages with the given search strings.
   565     Starts an asynchronous search for messages with the given search strings.
   521     This is part of the public interface.
   566     This is part of the public interface.
   522 
   567 
   523     \see NmFrameworkAdapter::searchL().
   568     \see NmFrameworkAdapter::searchL().
   534         mSearchObserver = new NmMailboxSearchObserver();
   579         mSearchObserver = new NmMailboxSearchObserver();
   535     }
   580     }
   536 
   581 
   537     // Set connections for forwarding the signals emitted by the search
   582     // Set connections for forwarding the signals emitted by the search
   538     // observer.
   583     // observer.
   539     connect(mSearchObserver, SIGNAL(matchFound(const NmId &)),
   584     connect(mSearchObserver, SIGNAL(matchFound(const NmId &, const NmId &)),
   540             this, SIGNAL(matchFound(const NmId &)), Qt::UniqueConnection);
   585             this, SIGNAL(matchFound(const NmId &, const NmId &)), Qt::UniqueConnection);
   541     connect(mSearchObserver, SIGNAL(searchComplete()),
   586     connect(mSearchObserver, SIGNAL(searchComplete()),
   542             this, SIGNAL(searchComplete()), Qt::UniqueConnection);
   587             this, SIGNAL(searchComplete()), Qt::UniqueConnection);
   543 
   588 
   544     TRAPD(err, searchL(mailboxId, QList<NmId>(), searchStrings, *mSearchObserver));
   589     TRAPD(err, searchL(mailboxId, QList<NmId>(), searchStrings, *mSearchObserver));
   545     return err;
   590     return err;
   548 
   593 
   549 /*!
   594 /*!
   550     Cancels the search if one is ongoing.
   595     Cancels the search if one is ongoing.
   551 
   596 
   552     \param mailboxId The ID of the mailbox running the search.
   597     \param mailboxId The ID of the mailbox running the search.
       
   598     
   553 
   599 
   554     \return A possible error code.
   600     \return A possible error code.
   555 */
   601 */
   556 int NmFrameworkAdapter::cancelSearch(const NmId &mailboxId)
   602 int NmFrameworkAdapter::cancelSearch(const NmId &mailboxId)
   557 {
   603 {
   564     }
   610     }
   565 
   611 
   566     return err;
   612     return err;
   567 }
   613 }
   568 
   614 
       
   615 /*!
       
   616     Indicates application state information to protocol plugins
       
   617     \param mailboxId Id of active mailbox, 0 if application is closed.
       
   618     \param folderId Id of active folder, 0 if application is closed.
       
   619 */
       
   620 void NmFrameworkAdapter::updateActiveFolder(const NmId &mailboxId, const NmId &folderId)
       
   621 {
       
   622 	  // TODO ExtensionL from CFSMailClient & state update
       
   623     Q_UNUSED(mailboxId);
       
   624     Q_UNUSED(folderId);
       
   625 }
   569 
   626 
   570 /*!
   627 /*!
   571     Starts an asynchronous search for messages with the given search strings.
   628     Starts an asynchronous search for messages with the given search strings.
   572 
   629 
   573     \param mailboxId The mailbox to search from.
   630     \param mailboxId The mailbox to search from.
   649     const NmId &messagePartId)
   706     const NmId &messagePartId)
   650 {
   707 {
   651     QPointer<NmOperation> oper = new NmFwaMessagePartFetchingOperation(
   708     QPointer<NmOperation> oper = new NmFwaMessagePartFetchingOperation(
   652             mailboxId, folderId, messageId, messagePartId, *mFSfw);
   709             mailboxId, folderId, messageId, messagePartId, *mFSfw);
   653     return oper;
   710     return oper;
       
   711 }
       
   712 
       
   713 /*!
       
   714     Starts a message parts fetching operation.
       
   715 
       
   716     \param mailboxId Id of the mailbox containing the folder.
       
   717     \param folderId Id of the folder containing the message.
       
   718     \param messageId Id of message containing the message parts
       
   719     \param messagePartIds ids of message parts
       
   720 
       
   721     \return An NmOperation object for the operation, ownership is transferred to caller
       
   722  */
       
   723 QPointer<NmOperation> NmFrameworkAdapter::fetchMessageParts( 
       
   724     const NmId &mailboxId,
       
   725     const NmId &folderId,
       
   726     const NmId &messageId,
       
   727     const QList<NmId> &messagePartIds)
       
   728 {
       
   729     QPointer<NmOperation> oper = new NmFwaMessagePartsFetchingOperation(
       
   730             mailboxId, folderId, messageId, messagePartIds, *mFSfw);
       
   731     return oper;  
   654 }
   732 }
   655 
   733 
   656 /*!
   734 /*!
   657     Returns sharable file handle to message part content
   735     Returns sharable file handle to message part content
   658 
   736