emailservices/emailclientapi/src/messageiterator.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2009 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: This file implements class CMessageIterator.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailmessage.h"
       
    19 #include "messageiterator.h"
       
    20 #include "emailapiutils.h"
       
    21 #include "CFSMailPlugin.h"
       
    22 #include "emailapiutils.h"
       
    23 #include "emailclientapi.hrh"
       
    24 #include "MFSMailIterator.h"
       
    25 
       
    26 // number of messages in chunk to retrive from protocol plugin. Actual chunk
       
    27 // size is one less because last element is used for reference to next chunk
       
    28 // retrieval. See ReadNextChunkL() for details.
       
    29 const TInt KMessageChunkSize = 5;
       
    30 
       
    31 const TInt KUndefined = -1;
       
    32 // -----------------------------------------------------------------------------
       
    33 // 
       
    34 // -----------------------------------------------------------------------------
       
    35 CMessageIterator* CMessageIterator::NewL( 
       
    36     MFSMailIterator* aIterator,
       
    37     CPluginData& aPluginData,
       
    38     TUint aCount )
       
    39     {
       
    40     CMessageIterator* iter = new ( ELeave ) 
       
    41         CMessageIterator( aIterator, aPluginData, aCount );
       
    42     return iter;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // 
       
    47 // -----------------------------------------------------------------------------
       
    48 CMessageIterator::~CMessageIterator()
       
    49     {
       
    50     iPluginData.ReleaseInstance();
       
    51     TInt count = iMessageArray.Count();
       
    52     iMessageArray.ResetAndDestroy();
       
    53     for (TInt i = count; i < iFsMessageArray.Count(); i++)
       
    54         {
       
    55         delete iFsMessageArray[i];
       
    56         }
       
    57     iFsMessageArray.Reset();
       
    58     delete iIterator;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // 
       
    63 // -----------------------------------------------------------------------------
       
    64 TEmailTypeId CMessageIterator::InterfaceId() const
       
    65     {
       
    66     return KEmailIFUidMessageIterator;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // 
       
    71 // -----------------------------------------------------------------------------
       
    72 void CMessageIterator::Release()
       
    73     {
       
    74     delete this;
       
    75     }
       
    76                 
       
    77 // -----------------------------------------------------------------------------
       
    78 // Returns next message
       
    79 // Messages are retrieved from protocol plugin in chunks of x messages and
       
    80 // when NextL has be called for x times, next chunk is retrieved. This
       
    81 // is trade-off between RAM consumption and speed.
       
    82 // -----------------------------------------------------------------------------
       
    83 MEmailMessage* CMessageIterator::NextL()
       
    84     {
       
    85     if ( iCursor == KUndefined )
       
    86         {
       
    87         // Buffer empty, read the first chunk
       
    88         if ( !ReadNextChunkL() )
       
    89             {
       
    90             // No messages found, 
       
    91             // Reset the cursor and return NULL
       
    92             iCursor = KUndefined;
       
    93             iStartMsgId = TFSMailMsgId();            
       
    94             return NULL;
       
    95             }
       
    96         // Items found, return the first item in the buffer
       
    97         iCursor = 0;
       
    98         }
       
    99     else
       
   100         {
       
   101         // Iterate to the next item
       
   102         iCursor++;
       
   103         }
       
   104     return ReadFromChunkL();
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // 
       
   109 // -----------------------------------------------------------------------------
       
   110 MEmailMessage* CMessageIterator::PreviousL()
       
   111     {
       
   112     if ( iCursor == KUndefined )
       
   113         {
       
   114         iStartMsgId = TFSMailMsgId();            
       
   115         // Buffer empty, client should first call NextL
       
   116         return NULL; 
       
   117         }
       
   118     // Iterate to the previous item
       
   119     iCursor--;
       
   120     return ReadFromChunkL();
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // 
       
   125 // -----------------------------------------------------------------------------
       
   126 TBool CMessageIterator::ReadNextChunkL( )
       
   127     {
       
   128     TBool chunkFound = EFalse;
       
   129 
       
   130     // Delete previous items if exist
       
   131     CleanCache();
       
   132     iHasMoreNextItems = iIterator->NextL(
       
   133             iStartMsgId,
       
   134             KMessageChunkSize, 
       
   135             iFsMessageArray );
       
   136     if ( iFsMessageArray.Count() > 0)
       
   137         {
       
   138         chunkFound = ETrue;
       
   139         // Set the cursor to the first item 
       
   140         iCursor = 0;
       
   141         iHasMorePrevItems = ETrue;
       
   142         }
       
   143     return chunkFound;
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // 
       
   148 // -----------------------------------------------------------------------------
       
   149 TBool CMessageIterator::ReadPreviousChunkL( )
       
   150     {
       
   151     TBool chunkFound = EFalse;
       
   152 
       
   153     // Delete previous items if exist
       
   154     CleanCache();
       
   155     RPointerArray<CFSMailMessage> tmp;
       
   156     iHasMorePrevItems = iIterator->PreviousL(
       
   157             iStartMsgId,
       
   158             KMessageChunkSize, 
       
   159             tmp );
       
   160     TInt count = tmp.Count();
       
   161     if ( count > 0)
       
   162         {
       
   163         // It seems that items have been found
       
   164         // Set the cursor to the end of array
       
   165         iCursor = count - 1;
       
   166         iHasMoreNextItems = ETrue;
       
   167         chunkFound = ETrue;
       
   168         // Revise the order of the buffer.
       
   169         // Create complete cache, so it is easier to iterate, 
       
   170         // as now we have to start from the end of cache
       
   171         for (TInt i = count; i > 0; i--)
       
   172             {
       
   173             CFSMailMessage* msg = tmp[i-1];
       
   174             iFsMessageArray.AppendL( msg );
       
   175             AddToCacheL( msg );
       
   176             }        
       
   177         }
       
   178         
       
   179     return chunkFound;
       
   180     }
       
   181 
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // 
       
   185 // -----------------------------------------------------------------------------
       
   186 MEmailMessage* CMessageIterator::ReadFromChunkL()
       
   187     {
       
   188     if ( iCursor >= KMessageChunkSize )
       
   189         {
       
   190         if ( iHasMoreNextItems )
       
   191             {
       
   192             iStartMsgId = iFsMessageArray[iCursor-1]->GetMessageId();
       
   193             if ( !ReadNextChunkL() )
       
   194                 {
       
   195                 // No more items found, 
       
   196                 // set the cursor to the previous item,
       
   197                 // and return NULL.
       
   198                 iCursor--;
       
   199                 return NULL;
       
   200                 }
       
   201             }
       
   202         else
       
   203             {
       
   204             // No more items found, 
       
   205             // set the cursor to the previous item,
       
   206             // and return NULL.
       
   207             iCursor--;
       
   208             return NULL;
       
   209             }
       
   210         }            
       
   211     else if ( iCursor < 0 )
       
   212         {
       
   213         iStartMsgId = iFsMessageArray[0]->GetMessageId();
       
   214         if ( iHasMorePrevItems )
       
   215             {
       
   216             if ( !ReadPreviousChunkL() )
       
   217                 {
       
   218                 // Buffer is empty now, client should call nextL, 
       
   219                 // and start reading from the start of the buffer
       
   220                 iCursor = KUndefined;   
       
   221                 return NULL;               
       
   222                 }
       
   223             }
       
   224         else
       
   225             {
       
   226             // Buffer is empty now, client should call nextL, 
       
   227             // and start reading from the start of the buffer
       
   228             iCursor = KUndefined;   
       
   229             return NULL;
       
   230             }
       
   231         }
       
   232    
       
   233     if ( iCursor < iFsMessageArray.Count() ) 
       
   234         {
       
   235         /* There are items to read in the cache */
       
   236         if ( iCursor >= iMessageArray.Count() )
       
   237             {
       
   238             AddToCacheL( iFsMessageArray[iCursor] );
       
   239             }
       
   240         return iMessageArray[iCursor];
       
   241         }
       
   242     else
       
   243         {
       
   244         // No more items found, 
       
   245         // set the cursor to the previous item,
       
   246         // and return NULL.
       
   247         iCursor--;
       
   248         return NULL;
       
   249         }
       
   250     }    
       
   251     
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // 
       
   255 // -----------------------------------------------------------------------------
       
   256 void CMessageIterator::AddToCacheL( CFSMailMessage* aFsMsg )
       
   257     {
       
   258     CEmailMessage* message = CEmailMessage::NewL( iPluginData, aFsMsg, EAPIOwns );
       
   259     iMessageArray.AppendL(message);    
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // 
       
   264 // -----------------------------------------------------------------------------
       
   265 void CMessageIterator::CleanCache()
       
   266     {
       
   267     // This is strange loop indeed
       
   268     // Both arrays has items that are deleted, iMessageArray has objects that point to iFsMessageArray
       
   269     // Index in both arrays goes syncronously
       
   270     // To prevent double destruction, first delete iMessageArray onjects and then the rest of the iFsMessageArray 
       
   271     TInt count = iMessageArray.Count();
       
   272     iMessageArray.ResetAndDestroy();
       
   273     for (TInt i = count; i < iFsMessageArray.Count(); i++)
       
   274         {
       
   275         iFsMessageArray.Remove(count);
       
   276         }
       
   277     iFsMessageArray.Reset();    
       
   278     }
       
   279 // -----------------------------------------------------------------------------
       
   280 // 
       
   281 // -----------------------------------------------------------------------------
       
   282 TUint CMessageIterator::Count() const
       
   283     {
       
   284     return iCount;
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // 
       
   289 // -----------------------------------------------------------------------------
       
   290 void CMessageIterator::ConstructL()
       
   291     {
       
   292     iPlugin = iPluginData.ClaimInstanceL();
       
   293     
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // 
       
   298 // -----------------------------------------------------------------------------
       
   299 CMessageIterator::CMessageIterator( 
       
   300     MFSMailIterator* aIterator, 
       
   301     CPluginData& aPluginData,
       
   302     TUint aCount ) : 
       
   303     iIterator( aIterator ), 
       
   304     iPluginData( aPluginData ), 
       
   305     iStartMsgId( TFSMailMsgId() ),
       
   306     iState( EReadNextMessageChunk ),
       
   307     iFsMessageArray( KMessageChunkSize ),
       
   308     iCount( aCount ),
       
   309     iCursor ( KUndefined ),
       
   310     iFirstMsgId( TFSMailMsgId() ),
       
   311     iHasMoreNextItems( ETrue ),
       
   312     iHasMorePrevItems( ETrue )    
       
   313     {
       
   314     }
       
   315     
       
   316 // End of file.