filemanager/GFLM/src/CGflmDirectoryListingCache.cpp
branchRCL_3
changeset 21 65326cf895ed
equal deleted inserted replaced
20:491b3ed49290 21:65326cf895ed
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  Provides directory listing cache
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CGflmDirectoryListingCache.h"
       
    22 #include "GFLM.hrh"
       
    23 #include "GflmUtils.h"
       
    24 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    25 #include <f32file.h>
       
    26 #else
       
    27 #include <f32file.h>
       
    28 #include <f32file_private.h>
       
    29 #endif
       
    30 #include <e32std.h>
       
    31 
       
    32 
       
    33 // CONSTANTS
       
    34 const TInt KListingGranularity = 0x2000; // 8KB
       
    35 
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CGflmDirectoryListingCache::CGflmDirectoryListingCache
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CGflmDirectoryListingCache::CGflmDirectoryListingCache(
       
    46         RFs& aFss,
       
    47         const TBool& aCancelIndicator ) :
       
    48     iFss( aFss ),
       
    49     iCancelIndicator( aCancelIndicator )
       
    50     {
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CGflmDirectoryListingCache::NewL
       
    55 // Two-phased constructor.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CGflmDirectoryListingCache* CGflmDirectoryListingCache::NewL(
       
    59         RFs& aFss,
       
    60         const TBool& aCancelIndicator )
       
    61     {
       
    62     return new( ELeave ) CGflmDirectoryListingCache(
       
    63         aFss, aCancelIndicator );
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CGflmDirectoryListingCache::~CGflmDirectoryListingCache()
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CGflmDirectoryListingCache::~CGflmDirectoryListingCache()
       
    71     {
       
    72     delete iCache;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CGflmDirectoryListingCache::ClearCache()
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void CGflmDirectoryListingCache::ClearCache()
       
    80     {
       
    81     iClearCache = ETrue;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CGflmDirectoryListingCache::ListingL()
       
    86 // Looks if a directory's listing is already loaded and returns it. If the
       
    87 // the listing is not found, it's read and added to the cache.
       
    88 // (other items were commented in a header).
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 const CGflmDirectoryListingCache::CEntryArray*
       
    92     CGflmDirectoryListingCache::ListingL( const TDesC& aDirectory )
       
    93     {
       
    94 
       
    95     TIMESTAMP( "GFLM listing fetch started: " )
       
    96 
       
    97     // Clear directory cache first if needed
       
    98     if ( iClearCache )
       
    99         {
       
   100         delete iCache;
       
   101         iCache = NULL;
       
   102         iClearCache = EFalse;
       
   103         }
       
   104 
       
   105     // Check if directory is already cached
       
   106     TBool found( EFalse );
       
   107     if ( iCache && !aDirectory.Compare( *( iCache->iDirectory ) ) )
       
   108         {
       
   109         found = ETrue;
       
   110         }
       
   111 
       
   112     if ( !found )
       
   113         {
       
   114         // Read directory to cache
       
   115         CListingNamePair* newPair = CListingNamePair::NewLC( aDirectory );
       
   116         GetDirL( *newPair );
       
   117         CleanupStack::Pop( newPair );
       
   118         delete iCache;
       
   119         iCache = NULL;
       
   120         iCache = newPair;
       
   121         }
       
   122 
       
   123     TIMESTAMP( "GFLM listing fetch ended: " )
       
   124 
       
   125     return iCache->iListing;
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CGflmDirectoryListingCache::GetDirL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CGflmDirectoryListingCache::GetDirL( CListingNamePair& aPair )
       
   133     {
       
   134     RDir dir;
       
   135     User::LeaveIfError( dir.Open(
       
   136         iFss,
       
   137         *aPair.iDirectory,
       
   138         KEntryAttNormal | KEntryAttDir ) );
       
   139     CleanupClosePushL( dir );
       
   140 
       
   141     TInt err( KErrNone );
       
   142 
       
   143 	do
       
   144 		{
       
   145         if ( iCancelIndicator )
       
   146             {
       
   147             // User has canceled the operation, abort
       
   148             User::Leave( KErrCancel );
       
   149             }
       
   150 
       
   151 		err = dir.Read( iEntryBuffer );
       
   152 		if ( err == KErrNone || err == KErrEof )
       
   153 			{
       
   154 			TInt entryCount( iEntryBuffer.Count() );
       
   155 			if ( !entryCount )
       
   156 			    {
       
   157 				break;
       
   158 				}
       
   159             for ( TInt i( 0 ); i < entryCount; i++ )
       
   160 			    {
       
   161                 const TEntry& entry( iEntryBuffer[ i ] );
       
   162                 TInt entrySize( EntrySize( entry, ETrue ) );
       
   163                 aPair.iListing->AppendL( entry, entrySize );
       
   164 				}
       
   165 			}
       
   166 
       
   167 		}while ( err == KErrNone );
       
   168 
       
   169     if ( err != KErrNone && err != KErrEof )
       
   170         {
       
   171         User::Leave( err );
       
   172         }
       
   173 
       
   174     CleanupStack::PopAndDestroy( &dir );
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CGflmDirectoryListingCache::CListingNamePair::~CListingNamePair()
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 CGflmDirectoryListingCache::CListingNamePair::~CListingNamePair()
       
   182     {
       
   183     delete iDirectory;
       
   184 
       
   185     if ( iListing )
       
   186         {
       
   187         iListing->Reset();
       
   188         delete iListing;
       
   189         }
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CGflmDirectoryListingCache::CListingNamePair::ConstructL()
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CGflmDirectoryListingCache::CListingNamePair::ConstructL(
       
   197         const TDesC& aDirectory )
       
   198     {
       
   199     iDirectory = aDirectory.AllocL();
       
   200     iListing = new( ELeave ) CEntryArray( KListingGranularity );
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CGflmDirectoryListingCache::CListingNamePair::NewLC()
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 CGflmDirectoryListingCache::CListingNamePair*
       
   208     CGflmDirectoryListingCache::CListingNamePair::NewLC(
       
   209         const TDesC& aDirectory )
       
   210     {
       
   211     CListingNamePair* self = new ( ELeave ) CListingNamePair();
       
   212     CleanupStack::PushL( self );
       
   213     self->ConstructL( aDirectory );
       
   214     return self;
       
   215     }
       
   216 
       
   217 //  End of File