landmarksui/uicontrols/src/CLmkSelectorCache.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2002 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:    LandmarksUi Content File -
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include "CLmkSelectorCache.h"
       
    27 #include "CLmkUiItemBase.h"
       
    28 #include "CLmkListProviderBase.h"
       
    29 #include <lmkerrors.h>
       
    30 
       
    31 
       
    32 // CONSTANTS
       
    33 /// Unnamed namespace for local definitions
       
    34 namespace {
       
    35 
       
    36 const TInt KMaxCacheArraySize( 10 );
       
    37 #if defined(_DEBUG)
       
    38 _LIT( KPanicMsg, "CLmkSelectorCache" );
       
    39 
       
    40 void Panic( TPanicCode aReason )
       
    41     {
       
    42     User::Panic( KPanicMsg, aReason );
       
    43     }
       
    44 #endif
       
    45 }  // namespace
       
    46 
       
    47 // ============================ MEMBER FUNCTIONS ===============================
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CLmkSelectorCache::CLmkSelectorCache
       
    51 // C++ default constructor can NOT contain any code, that
       
    52 // might leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CLmkSelectorCache::CLmkSelectorCache( CLmkListProviderBase& aListProvider )
       
    56     : iListProvider( aListProvider )
       
    57     {
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CLmkSelectorCache::ConstructL
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CLmkSelectorCache::ConstructL()
       
    66     {
       
    67     iListProvider.AddObserverL( *this );
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CLmkSelectorCache::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CLmkSelectorCache* CLmkSelectorCache::NewL(
       
    76     CLmkListProviderBase& aListProvider )
       
    77     {
       
    78     CLmkSelectorCache* self = new( ELeave ) CLmkSelectorCache( aListProvider );
       
    79 
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop();
       
    83 
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------
       
    88 // CLmkSelectorCache::~CLmkSelectorCache
       
    89 // ----------------------------------------------------
       
    90 //
       
    91 CLmkSelectorCache::~CLmkSelectorCache()
       
    92     {
       
    93     iListProvider.RemoveObserver(*this);
       
    94     iItemList.ResetAndDestroy();
       
    95     iItemList.Close();
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CLmkSelectorCache::ItemAtL
       
   100 // Cache takes the ownership of item, therefore it receives a pointer,
       
   101 // but passes a reference to the lb model
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 CLmkUiItemBase& CLmkSelectorCache::ItemAtL( TInt aIndex )
       
   105     {
       
   106     __ASSERT_DEBUG( aIndex >= 0, Panic( KLmkPanicInvalidIndex ) );
       
   107     TInt count = iItemList.Count();
       
   108     for ( TInt i( 0 ); i < count; ++i )
       
   109         {
       
   110         if ( aIndex == iItemList[i]->iIndex )
       
   111             {
       
   112             CLinkedItem* tmp = iItemList[i];
       
   113             CleanupStack::PushL( tmp );
       
   114             iItemList.Remove( i );
       
   115             User::LeaveIfError( iItemList.Insert( tmp, 0 ) );
       
   116             CleanupStack::Pop( tmp );
       
   117             return *(iItemList[0]->iPtr);
       
   118             }
       
   119         }
       
   120     __ASSERT_DEBUG( iListProvider.ItemCount() > aIndex ,
       
   121                                 Panic(KLmkPanicInvalidIndex) );
       
   122     CLinkedItem* tmp = new(ELeave) CLinkedItem();
       
   123     CleanupStack::PushL( tmp );
       
   124     tmp->iIndex = aIndex;
       
   125     tmp->iPtr = iListProvider.ItemAtL( aIndex );
       
   126     User::LeaveIfError( iItemList.Insert( tmp, 0 ) );
       
   127     CleanupStack::Pop(); // tmp
       
   128 
       
   129     if ( iItemList.Count() > KMaxCacheArraySize )
       
   130         {
       
   131         delete iItemList[KMaxCacheArraySize];
       
   132         iItemList.Remove( KMaxCacheArraySize );
       
   133         }
       
   134     return *( iItemList[0]->iPtr );
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CLmkSelectorCache::ItemCount
       
   139 // ?implementation_description
       
   140 // (other items were commented in a header).
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 TInt CLmkSelectorCache::ItemCount() const
       
   144     {
       
   145     // dummy implementation:
       
   146     return iListProvider.ItemCount();
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CLmkSelectorCache::HandleListProviderEventL
       
   151 // ?implementation_description
       
   152 // (other items were commented in a header).
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CLmkSelectorCache::HandleListProviderEvent(
       
   156     TLmkListProviderEventType /*aEvent*/ )
       
   157     {
       
   158     iItemList.ResetAndDestroy();
       
   159     }
       
   160 
       
   161 // ----------------------------------------------------
       
   162 // CLmkSelectorCache::HandleListProviderError
       
   163 // ----------------------------------------------------
       
   164 //
       
   165 void CLmkSelectorCache::HandleListProviderError( TInt /*aError*/ )
       
   166     {
       
   167     iItemList.ResetAndDestroy();
       
   168     }
       
   169 
       
   170 // ----------------------------------------------------
       
   171 // CLmkSelectorCache::CLinkedItem
       
   172 // ----------------------------------------------------
       
   173 //
       
   174 CLmkSelectorCache::CLinkedItem::CLinkedItem()
       
   175     {
       
   176     }
       
   177 
       
   178 // ----------------------------------------------------
       
   179 // CLmkSelectorCache::~CLinkedItem
       
   180 // ----------------------------------------------------
       
   181 //
       
   182 CLmkSelectorCache::CLinkedItem::~CLinkedItem()
       
   183     {
       
   184     delete this->iPtr;
       
   185     }
       
   186 
       
   187 
       
   188 //  End of File