phonebookui/Phonebook2/CommonUI/src/CPbk2IconInfoContainer.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Phonebook 2 icon info container.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <CPbk2IconInfoContainer.h>
       
    21 
       
    22 // Phonebook 2
       
    23 #include "CPbk2IconInfo.h"
       
    24 
       
    25 // System includes
       
    26 #include <barsread.h>
       
    27 #include <coemain.h>
       
    28 
       
    29 // Debugging headers
       
    30 #include <Pbk2Debug.h>
       
    31 
       
    32 // Unnamed namespace for local definitions
       
    33 namespace {
       
    34 
       
    35 /**
       
    36  * Creates a resource reader.
       
    37  *
       
    38  * @param aReader       Will contain a filled reader.
       
    39  * @param aResourceId   Id of the resource to be loaded.
       
    40  * @param aCoeEnv       Coe environment to be used for resource reading.
       
    41  */
       
    42 void CreateReaderLC( 
       
    43         TResourceReader& aReader,  
       
    44         TInt aResourceId,
       
    45         CCoeEnv* aCoeEnv = NULL )
       
    46     {
       
    47     CCoeEnv* coeEnv = aCoeEnv;
       
    48 
       
    49     if ( !coeEnv )
       
    50         {
       
    51         coeEnv = CCoeEnv::Static();
       
    52         }
       
    53 
       
    54     coeEnv->CreateResourceReaderLC( aReader, aResourceId );
       
    55     }
       
    56 
       
    57 } /// namespace
       
    58 
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CPbk2IconInfoContainer::CPbk2IconInfoContainer
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 inline CPbk2IconInfoContainer::CPbk2IconInfoContainer()
       
    65     {
       
    66     }
       
    67 
       
    68 // --------------------------------------------------------------------------
       
    69 // CPbk2IconInfoContainer::~CPbk2IconInfoContainer
       
    70 // --------------------------------------------------------------------------
       
    71 //
       
    72 CPbk2IconInfoContainer::~CPbk2IconInfoContainer()
       
    73     {
       
    74     iInfoArray.ResetAndDestroy();
       
    75     delete iMbmFileNames;
       
    76     }
       
    77 
       
    78 // --------------------------------------------------------------------------
       
    79 // CPbk2IconInfoContainer::NewL
       
    80 // --------------------------------------------------------------------------
       
    81 //
       
    82 EXPORT_C CPbk2IconInfoContainer* CPbk2IconInfoContainer::NewL
       
    83         ( TResourceReader& aReader )
       
    84     {
       
    85     CPbk2IconInfoContainer* self = new( ELeave ) CPbk2IconInfoContainer;
       
    86     CleanupStack::PushL(self);
       
    87     self->ConstructL( aReader );
       
    88     CleanupStack::Pop(self);
       
    89     return self;
       
    90     }
       
    91 
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CPbk2IconInfoContainer::NewL
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CPbk2IconInfoContainer* CPbk2IconInfoContainer::NewL
       
    98         ( TInt aResourceId, CCoeEnv* aCoeEnv /*=NULL*/ )
       
    99     {
       
   100     CPbk2IconInfoContainer* self = new( ELeave ) CPbk2IconInfoContainer;
       
   101     CleanupStack::PushL(self);
       
   102     TResourceReader reader;
       
   103     CreateReaderLC(reader, aResourceId, aCoeEnv);
       
   104     self->ConstructL( reader );
       
   105     CleanupStack::PopAndDestroy(); // reader (buffer)
       
   106     CleanupStack::Pop(self);
       
   107     return self;
       
   108     }
       
   109 
       
   110 // --------------------------------------------------------------------------
       
   111 // CPbk2IconInfoContainer::NewL
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C CPbk2IconInfoContainer* CPbk2IconInfoContainer::NewL()
       
   115     {
       
   116     CPbk2IconInfoContainer* self = new( ELeave ) CPbk2IconInfoContainer;
       
   117     CleanupStack::PushL(self);
       
   118     self->ConstructL();
       
   119     CleanupStack::Pop(self);
       
   120     return self;
       
   121     }
       
   122 
       
   123 // --------------------------------------------------------------------------
       
   124 // CPbk2IconInfoContainer::ConstructL
       
   125 // --------------------------------------------------------------------------
       
   126 //
       
   127 void CPbk2IconInfoContainer::ConstructL( TResourceReader& aReader )
       
   128     {
       
   129     // Not so many MBM files
       
   130     const TInt granularity = 4;
       
   131     iMbmFileNames = new( ELeave ) CDesCArrayFlat( granularity );
       
   132     AppendIconsFromResourceL( aReader );
       
   133 
       
   134     }
       
   135 
       
   136 // --------------------------------------------------------------------------
       
   137 // CPbk2IconInfoContainer::ConstructL
       
   138 // --------------------------------------------------------------------------
       
   139 //
       
   140 void CPbk2IconInfoContainer::ConstructL()
       
   141     {
       
   142     // Not so many MBM files
       
   143     const TInt granularity = 4;
       
   144     iMbmFileNames = new( ELeave ) CDesCArrayFlat( granularity );
       
   145     }
       
   146 
       
   147 // --------------------------------------------------------------------------
       
   148 // CPbk2IconInfoContainer::Find
       
   149 // --------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C const CPbk2IconInfo* CPbk2IconInfoContainer::Find
       
   152         ( const TPbk2IconId& aIconId ) const
       
   153     {
       
   154     TInt index = FindInfo( aIconId );
       
   155     if ( index == KErrNotFound )
       
   156         {
       
   157         return NULL;
       
   158         }
       
   159     return iInfoArray[index];
       
   160     }
       
   161 
       
   162 // --------------------------------------------------------------------------
       
   163 // CPbk2IconInfoContainer::AppendIconsFromResourceL
       
   164 // --------------------------------------------------------------------------
       
   165 //
       
   166 EXPORT_C void CPbk2IconInfoContainer::AppendIconsFromResourceL
       
   167         ( TInt aResourceId )
       
   168     {
       
   169     TResourceReader reader;
       
   170     CreateReaderLC(reader, aResourceId);
       
   171     AppendIconsFromResourceL(reader);
       
   172     CleanupStack::PopAndDestroy(); // reader
       
   173     }
       
   174 
       
   175 // --------------------------------------------------------------------------
       
   176 // CPbk2IconInfoContainer::AppendIconsFromResourceL
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 EXPORT_C void CPbk2IconInfoContainer::AppendIconsFromResourceL
       
   180         ( TResourceReader& aReader )
       
   181     {
       
   182     // Read from resource
       
   183     TInt count = aReader.ReadInt16();
       
   184     while (count-- > 0)
       
   185         {
       
   186         CPbk2IconInfo* iconInfo =
       
   187                 CPbk2IconInfo::NewLC( aReader, *iMbmFileNames );
       
   188         AppendIconL( iconInfo );
       
   189         CleanupStack::Pop( iconInfo );
       
   190         }
       
   191     }
       
   192 
       
   193 // --------------------------------------------------------------------------
       
   194 // CPbk2IconInfoContainer::AppendIconL
       
   195 // --------------------------------------------------------------------------
       
   196 //
       
   197 EXPORT_C void CPbk2IconInfoContainer::AppendIconL(
       
   198         CPbk2IconInfo* aIconInfo )
       
   199     {
       
   200     const TInt index = FindInfo( aIconInfo->Pbk2IconId() );
       
   201     if ( index != KErrNotFound )
       
   202         {
       
   203         // Delete existing entry in info array
       
   204         delete iInfoArray[index];
       
   205         iInfoArray.Remove( index );
       
   206         }
       
   207     iInfoArray.AppendL( aIconInfo );
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2IconInfoContainer::FindInfo
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 TInt CPbk2IconInfoContainer::FindInfo( const TPbk2IconId& aIconId ) const
       
   215     {
       
   216     TInt ret = KErrNotFound;
       
   217 
       
   218     const TInt count = iInfoArray.Count();
       
   219     for (TInt i = 0; i < count; ++i)
       
   220         {
       
   221         if (iInfoArray[i]->Pbk2IconId() == aIconId )
       
   222             {
       
   223             ret = i;
       
   224             break;
       
   225             }
       
   226         }
       
   227 
       
   228     return ret;
       
   229     }
       
   230 
       
   231 // End of File