emailservices/emailclientapi/src/emailmailboxcache.cpp
changeset 54 997a02608b3a
equal deleted inserted replaced
53:bf7eb7911fc5 54:997a02608b3a
       
     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: This file implements class CEmailMailboxCache.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailmailboxcache.h"
       
    19 #include "emailapiutils.h"
       
    20 
       
    21 static const TInt KCacheGranularity = 2;
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // 
       
    25 // ---------------------------------------------------------------------------
       
    26 CEmailMailboxCache* CEmailMailboxCache::NewL()
       
    27     {
       
    28     CEmailMailboxCache* cache = new ( ELeave ) CEmailMailboxCache();
       
    29     return cache;
       
    30     }
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // 
       
    34 // ---------------------------------------------------------------------------
       
    35 CEmailMailboxCache::~CEmailMailboxCache()
       
    36     {
       
    37     iEntries.Close();
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Start of cache transaction with clenanup support
       
    42 // ---------------------------------------------------------------------------
       
    43 void CEmailMailboxCache::StartCachingPushL()
       
    44     {
       
    45     TCleanupItem item( &CEmailMailboxCache::CleanupOp, this );
       
    46     CleanupStack::PushL( item );
       
    47     iState = ECaching;
       
    48     iEntries.Reset();
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 //  End of cache transaction
       
    53 // ---------------------------------------------------------------------------
       
    54 void CEmailMailboxCache::EndCachingPop()
       
    55     {
       
    56     iState = EComplete;
       
    57     CleanupStack::Pop();
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 // ---------------------------------------------------------------------------
       
    63 TBool CEmailMailboxCache::IsCached() const
       
    64     {
       
    65     return iState == EComplete;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // 
       
    70 // ---------------------------------------------------------------------------
       
    71 void CEmailMailboxCache::AddMailboxL( 
       
    72     CPluginData& aPluginData, 
       
    73     const TMailboxId& aMailboxId )
       
    74     {
       
    75     if ( iState != ECaching )
       
    76         {
       
    77         User::Leave( KErrNotReady );
       
    78         }
       
    79     if ( FindById( aMailboxId ) == KErrNotFound )
       
    80         {
       
    81         TCacheEntry entry( &aPluginData, aMailboxId );
       
    82         iEntries.AppendL( entry );
       
    83         }
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // 
       
    88 // ---------------------------------------------------------------------------
       
    89 CPluginData* CEmailMailboxCache::PluginDataL( const TMailboxId& aMailboxId ) const
       
    90     {
       
    91     CPluginData* pdata = NULL;
       
    92     const TInt index( FindById( aMailboxId ) );
       
    93     if ( index != KErrNotFound )
       
    94         {
       
    95         const TCacheEntry& entry = iEntries[ index ];
       
    96         pdata = entry.iPluginData;
       
    97         }
       
    98     return pdata;
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // 
       
   103 // ---------------------------------------------------------------------------
       
   104 CPluginData* CEmailMailboxCache::PluginDataL( const TUid& aPluginId ) const
       
   105     {
       
   106     CPluginData* pdata = NULL;
       
   107     const TInt index( FindByPluginIdL( aPluginId ) );
       
   108     if ( index != KErrNotFound )
       
   109         {
       
   110         const TCacheEntry& entry = iEntries[ index ];
       
   111         pdata = entry.iPluginData;
       
   112         }
       
   113     return pdata;
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // 
       
   118 // ---------------------------------------------------------------------------
       
   119 void CEmailMailboxCache::GetIdsL( REmailMailboxIdArray& aIdArray ) const
       
   120     {
       
   121     if ( iState != EComplete )
       
   122         {
       
   123         // cache not up to date
       
   124         User::Leave( KErrNotReady );
       
   125         }
       
   126     for ( TInt i=0; i < iEntries.Count(); i++ )
       
   127         {
       
   128         TMailboxId id = iEntries[i].iMailboxId;
       
   129         aIdArray.AppendL( id );
       
   130         }
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // 
       
   135 // ---------------------------------------------------------------------------
       
   136 TInt CEmailMailboxCache::FindById( const TMailboxId& aMailboxId ) const
       
   137     {
       
   138     TIdentityRelation<TCacheEntry> rel( CEmailMailboxCache::Equals );
       
   139 
       
   140     // don't care about plugin data because this is seach key only and
       
   141     // mailbox id is used for search
       
   142     TCacheEntry entry( NULL, aMailboxId );
       
   143     return iEntries.Find( entry, rel );
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // 
       
   148 // ---------------------------------------------------------------------------
       
   149 TInt CEmailMailboxCache::FindByPluginIdL( const TUid& aPluginId ) const
       
   150     {
       
   151     TIdentityRelation<TCacheEntry> rel( CEmailMailboxCache::PluginEquals );
       
   152     CPluginData* key = new ( ELeave ) CPluginData( aPluginId );
       
   153     TCacheEntry entry( key, TMailboxId() );
       
   154     TInt index = iEntries.Find( entry, rel ); 
       
   155     delete key;
       
   156     return index;
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // 
       
   161 // ---------------------------------------------------------------------------
       
   162 TBool CEmailMailboxCache::Equals( const TCacheEntry& a1, const TCacheEntry& a2 )
       
   163     {
       
   164     return ( a1.iMailboxId == a2.iMailboxId );
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // 
       
   169 // ---------------------------------------------------------------------------
       
   170 TBool CEmailMailboxCache::PluginEquals( const TCacheEntry& a1, const TCacheEntry& a2 )
       
   171     {
       
   172     return ( a1.iPluginData->Uid() == a2.iPluginData->Uid() );
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // Cleanup 
       
   177 // ---------------------------------------------------------------------------
       
   178 void CEmailMailboxCache::CleanupOp( TAny* aAny )
       
   179     {
       
   180     CEmailMailboxCache* cache = reinterpret_cast<CEmailMailboxCache*>( aAny );
       
   181     cache->iEntries.Reset();
       
   182     cache->iState = EEmpty;
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // 
       
   187 // ---------------------------------------------------------------------------
       
   188 CEmailMailboxCache::CEmailMailboxCache() : 
       
   189     iState( EEmpty ), 
       
   190     iEntries( KCacheGranularity )
       
   191     {
       
   192     }
       
   193 
       
   194 // End of file