phonebookui/Phonebook2/CommonUI/src/CPbk2IconArray.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:  An icon array for Phonebook 2 icons.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <CPbk2IconArray.h>
       
    21 
       
    22 // Phonebook 2
       
    23 #include <CPbk2IconFactory.h>
       
    24 #include <CPbk2IconInfo.h>
       
    25 #include <MPbk2FieldPropertyArray.h>
       
    26 #include <MPbk2FieldProperty.h>
       
    27 #include <TPbk2AppIconId.h>
       
    28 #include <fbs.h>
       
    29 
       
    30 // System includes
       
    31 #include <barsread.h>       // TResourceReader
       
    32 #include <gulicon.h>        // CGulIcon
       
    33 
       
    34 // Debugging headers
       
    35 #include <Pbk2Debug.h>
       
    36 
       
    37 
       
    38 /// Unnamed namespace for local definitions
       
    39 namespace {
       
    40 
       
    41 const TInt KGranularity = 10;
       
    42 
       
    43 #ifdef _DEBUG
       
    44 
       
    45 enum TPanicCode
       
    46     {
       
    47     EPanicLogic_ReadArrayL
       
    48     };
       
    49 
       
    50 void Panic(TInt aReason)
       
    51     {
       
    52     _LIT( KPanicText, "CPbk2IconArray" );
       
    53     User::Panic( KPanicText, aReason );
       
    54     }
       
    55 
       
    56 #endif // _DEBUG
       
    57 
       
    58 } /// namespace
       
    59 
       
    60 
       
    61 /**
       
    62  * An array that hides TPbk2IconId.h include from header.
       
    63  */
       
    64 class CPbk2IconArray::CIdArray : public CArrayFixFlat<TPbk2IconId>
       
    65     {
       
    66     public: // Constructor
       
    67 
       
    68         /**
       
    69          * Constructor.
       
    70          */
       
    71         CIdArray();
       
    72     };
       
    73 
       
    74 // --------------------------------------------------------------------------
       
    75 // CPbk2IconArray::CIdArray::CIdArray
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 CPbk2IconArray::CIdArray::CIdArray() :
       
    79         CArrayFixFlat<TPbk2IconId>( KGranularity )
       
    80     {
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------
       
    84 // CPbk2IconArray::CPbk2IconArray
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 CPbk2IconArray::CPbk2IconArray() :
       
    88         CArrayPtrFlat<CGulIcon>( KGranularity ),
       
    89         iArrayId( EPbk2NullIconArrayId )
       
    90     {
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CPbk2IconArray::~CPbk2IconArray
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 CPbk2IconArray::~CPbk2IconArray()
       
    98     {
       
    99     // Free contents of this array for leave-safety
       
   100     ResetAndDestroy();
       
   101     delete iIconFactory;
       
   102     delete iIdMap;
       
   103     }
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CPbk2IconArray::NewL
       
   107 // --------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C CPbk2IconArray* CPbk2IconArray::NewL( TResourceReader& aReader )
       
   110     {
       
   111     CPbk2IconArray* self = new( ELeave ) CPbk2IconArray;
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL( aReader );
       
   114     CleanupStack::Pop( self );
       
   115     return self;
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------
       
   119 // CPbk2IconArray::NewL
       
   120 // --------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C CPbk2IconArray* CPbk2IconArray::NewL(
       
   123         const MPbk2FieldPropertyArray& aFieldProperties )
       
   124     {
       
   125     CPbk2IconArray* self = new( ELeave ) CPbk2IconArray;
       
   126     CleanupStack::PushL( self );
       
   127     self->ConstructL( aFieldProperties );
       
   128     CleanupStack::Pop( self );
       
   129     return self;
       
   130     }
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // CPbk2IconArray::NewL
       
   134 // --------------------------------------------------------------------------
       
   135 //
       
   136 EXPORT_C CPbk2IconArray* CPbk2IconArray::NewL( TResourceReader& aReader,
       
   137         const CPbk2IconInfoContainer& aIconInfoContainer )
       
   138     {
       
   139     CPbk2IconArray* self = new( ELeave ) CPbk2IconArray;
       
   140     CleanupStack::PushL( self );
       
   141     self->ConstructL( aReader, aIconInfoContainer );
       
   142     CleanupStack::Pop( self );
       
   143     return self;
       
   144     }
       
   145 
       
   146 // --------------------------------------------------------------------------
       
   147 // CPbk2IconArray::BaseConstructL
       
   148 // --------------------------------------------------------------------------
       
   149 //
       
   150 void CPbk2IconArray::BaseConstructL()
       
   151     {
       
   152     iIdMap = new ( ELeave ) CIdArray;
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CPbk2IconArray::ConstructL
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 void CPbk2IconArray::ConstructL()
       
   160     {
       
   161     BaseConstructL();
       
   162     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC();
       
   163     AppendDefaultIconsL( *factory );
       
   164     CleanupStack::PopAndDestroy( factory );
       
   165     }
       
   166 
       
   167 // --------------------------------------------------------------------------
       
   168 // CPbk2IconArray::ConstructL
       
   169 // --------------------------------------------------------------------------
       
   170 //
       
   171 void CPbk2IconArray::ConstructL( TResourceReader& aReader )
       
   172     {
       
   173     BaseConstructL();
       
   174     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC();
       
   175     DoAppendIconsFromResourceL( aReader, *factory, ETrue );
       
   176     CleanupStack::PopAndDestroy( factory );
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CPbk2IconArray::ConstructL
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 void CPbk2IconArray::ConstructL
       
   184         ( const MPbk2FieldPropertyArray& aFieldProperties )
       
   185     {
       
   186     BaseConstructL();
       
   187     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC();
       
   188     AppendDefaultIconsL( *factory );
       
   189 
       
   190     const TInt count = aFieldProperties.Count();
       
   191     for ( TInt i = 0; i < count; ++i )
       
   192         {
       
   193         const TPbk2IconId& id = aFieldProperties.At(i).IconId();
       
   194         if ( FindIcon( id ) == KErrNotFound )
       
   195             {
       
   196             ReserveMemoryL( 1 ); // Reserve space for new icon
       
   197             AppendIconWithMappingL( *factory, id );
       
   198             }
       
   199         }
       
   200     CleanupStack::PopAndDestroy( factory );
       
   201 
       
   202     iArrayId = EPbk2FieldTypeIconArrayId;
       
   203     }
       
   204 
       
   205 // --------------------------------------------------------------------------
       
   206 // CPbk2IconArray::ConstructL
       
   207 // --------------------------------------------------------------------------
       
   208 //
       
   209 void CPbk2IconArray::ConstructL( TResourceReader& aReader,
       
   210         const CPbk2IconInfoContainer& aIconInfoContainer )
       
   211     {
       
   212     BaseConstructL();
       
   213     AppendIconsFromResourceL( aReader, aIconInfoContainer );
       
   214     }
       
   215     
       
   216 // --------------------------------------------------------------------------
       
   217 // CPbk2IconArray::Id
       
   218 // --------------------------------------------------------------------------
       
   219 //
       
   220 EXPORT_C TPbk2IconArrayId CPbk2IconArray::Id() const
       
   221     {
       
   222     return iArrayId;
       
   223     }
       
   224 
       
   225 // --------------------------------------------------------------------------
       
   226 // CPbk2IconArray::FindIcon
       
   227 // --------------------------------------------------------------------------
       
   228 //
       
   229 EXPORT_C TInt CPbk2IconArray::FindIcon( const TPbk2IconId& aIconId ) const
       
   230     {
       
   231     const TInt count = iIdMap->Count();
       
   232     for ( TInt i = 0; i < count; ++i )
       
   233         {
       
   234         if ( (*iIdMap)[i] == aIconId )
       
   235             {
       
   236             return i;
       
   237             }
       
   238         }
       
   239     return KErrNotFound;
       
   240     }
       
   241 
       
   242 // --------------------------------------------------------------------------
       
   243 // CPbk2IconArray::FindAndCreateIconL
       
   244 // --------------------------------------------------------------------------
       
   245 //
       
   246 EXPORT_C TInt CPbk2IconArray::FindAndCreateIconL
       
   247         ( const TPbk2IconId& aIconId )
       
   248     {
       
   249     TInt index = FindIcon( aIconId );
       
   250     if ( index == KErrNotFound )
       
   251         {
       
   252         if ( !iIconFactory )
       
   253             {
       
   254             iIconFactory = CPbk2IconFactory::NewL();
       
   255             }        
       
   256         ReserveMemoryL( 1 ); // reserve memory for the new icon
       
   257         AppendIconWithMappingL( *iIconFactory, aIconId );
       
   258         // last index
       
   259         index = Count() - 1;
       
   260         }
       
   261     else
       
   262         {
       
   263         // Delete factory it's not needed
       
   264         delete iIconFactory;
       
   265         iIconFactory = NULL;
       
   266         }
       
   267     return index;
       
   268     }
       
   269 
       
   270 // --------------------------------------------------------------------------
       
   271 // CPbk2IconArray::AppendIconsFromResourceL
       
   272 // --------------------------------------------------------------------------
       
   273 //
       
   274 EXPORT_C void CPbk2IconArray::AppendIconsFromResourceL(
       
   275         TResourceReader& aReader,
       
   276         const CPbk2IconInfoContainer& aIconInfoContainer )
       
   277     {
       
   278     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC( aIconInfoContainer );
       
   279     DoAppendIconsFromResourceL( aReader, *factory, EFalse );
       
   280     CleanupStack::PopAndDestroy( factory );
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CPbk2IconArray::RefreshL
       
   285 // --------------------------------------------------------------------------
       
   286 //
       
   287 EXPORT_C void CPbk2IconArray::RefreshL()
       
   288     {
       
   289     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC();
       
   290     DoRefreshL( *factory );
       
   291     CleanupStack::PopAndDestroy( factory );
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CPbk2IconArray::RefreshL
       
   296 // --------------------------------------------------------------------------
       
   297 //
       
   298 EXPORT_C void CPbk2IconArray::RefreshL(
       
   299         const CPbk2IconInfoContainer& aIconInfoContainer )
       
   300     {
       
   301     CPbk2IconFactory* factory = CPbk2IconFactory::NewLC( aIconInfoContainer );
       
   302     DoRefreshL( *factory );
       
   303     CleanupStack::PopAndDestroy( factory );
       
   304     }
       
   305     
       
   306 // --------------------------------------------------------------------------
       
   307 // CPbk2IconArray::AppendIconL
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C void CPbk2IconArray::AppendIconL( 
       
   311         CGulIcon* aIcon,  const TPbk2IconId& aIconId ) 
       
   312     {
       
   313     ReserveMemoryL( 1 ); // Reserve space for new icon and iconId
       
   314     AppendL( aIcon );
       
   315     iIdMap->AppendL( aIconId );
       
   316     }
       
   317 
       
   318 // --------------------------------------------------------------------------
       
   319 // CPbk2IconArray::AppendIconL
       
   320 // --------------------------------------------------------------------------
       
   321 //
       
   322 EXPORT_C void CPbk2IconArray::AppendIconL( CPbk2IconInfo* aIconInfo ) 
       
   323     {
       
   324     if ( !iIconFactory )
       
   325         {
       
   326         iIconFactory = CPbk2IconFactory::NewL();
       
   327         }        
       
   328 
       
   329     iIconFactory->AppendIconL(aIconInfo );
       
   330     AppendIconWithMappingL(*iIconFactory, aIconInfo->Pbk2IconId());
       
   331     }
       
   332 
       
   333 // --------------------------------------------------------------------------
       
   334 // CPbk2IconArray::RemoveIcon
       
   335 // --------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C TInt CPbk2IconArray::RemoveIcon(
       
   338 		const TPbk2IconId& aIconId )
       
   339 	{
       
   340 	// search the icon first
       
   341 	const TInt count = iIdMap->Count();
       
   342 	for ( TInt i = 0; i < count; ++i )
       
   343 		{
       
   344 		if ( (*iIdMap)[i] == aIconId )
       
   345 			{
       
   346 			//remove id from the array
       
   347 			iIdMap->Delete( i );
       
   348 			// get object
       
   349 			CGulIcon* icon = At(i);
       
   350 			delete icon;
       
   351 			//remove pointer from the array 
       
   352 			Delete( i );
       
   353 			return KErrNone;
       
   354 			}
       
   355 		}
       
   356 	return KErrNotFound;
       
   357 	}
       
   358 
       
   359 // --------------------------------------------------------------------------
       
   360 // CPbk2IconArray::AppendDefaultIconsL
       
   361 // --------------------------------------------------------------------------
       
   362 //
       
   363 void CPbk2IconArray::AppendDefaultIconsL(
       
   364         const CPbk2IconFactory& aIconFactory )
       
   365     {
       
   366     TPbk2AppIconId markId( EPbk2qgn_indi_marked_add );
       
   367     TPbk2AppIconId emptyId( EPbk2qgn_prop_nrtyp_empty );
       
   368     ReserveMemoryL( 2 ); // num of added icons
       
   369     AppendIconWithMappingL( aIconFactory, markId );
       
   370     AppendIconWithMappingL( aIconFactory, emptyId );
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CPbk2IconArray::DoAppendIconsFromResourceL
       
   375 // --------------------------------------------------------------------------
       
   376 //
       
   377 void CPbk2IconArray::DoAppendIconsFromResourceL( TResourceReader& aReader,
       
   378         const CPbk2IconFactory& aIconFactory, TBool aUpdateId )
       
   379     {
       
   380     TInt8 id = aReader.ReadInt8();
       
   381     if ( aUpdateId )
       
   382         {
       
   383         iArrayId = static_cast<TPbk2IconArrayId>( id );
       
   384         }
       
   385 
       
   386     TInt count = aReader.ReadInt16();
       
   387     ReserveMemoryL( count );
       
   388 
       
   389     while ( count-- > 0 )
       
   390         {
       
   391         TPbk2IconId iconId( aReader );
       
   392         AppendIconWithMappingL( aIconFactory, iconId );
       
   393         }
       
   394     }
       
   395 
       
   396 // --------------------------------------------------------------------------
       
   397 // CPbk2IconArray::AppendIconWithMappingL
       
   398 // --------------------------------------------------------------------------
       
   399 //
       
   400 void CPbk2IconArray::AppendIconWithMappingL
       
   401         ( const CPbk2IconFactory& aIconFactory, const TPbk2IconId& aIconId )
       
   402     {
       
   403     CGulIcon* gulIcon = aIconFactory.CreateIconLC( aIconId );
       
   404     __ASSERT_DEBUG(gulIcon, Panic(EPanicLogic_ReadArrayL));
       
   405     AppendL( gulIcon );
       
   406     CleanupStack::Pop( gulIcon );
       
   407     iIdMap->AppendL( aIconId );
       
   408     }
       
   409 
       
   410 // --------------------------------------------------------------------------
       
   411 // CPbk2IconArray::ReserveMemoryL
       
   412 // --------------------------------------------------------------------------
       
   413 //
       
   414 void CPbk2IconArray::ReserveMemoryL( TInt aAmount )
       
   415     {
       
   416     SetReserveL( Count() + aAmount );
       
   417     iIdMap->SetReserveL( iIdMap->Count() + aAmount );
       
   418     }
       
   419 
       
   420 // --------------------------------------------------------------------------
       
   421 // CPbk2IconArray::DoRefreshL
       
   422 // --------------------------------------------------------------------------
       
   423 //
       
   424 void CPbk2IconArray::DoRefreshL( const CPbk2IconFactory& aFactory )
       
   425     {
       
   426     const TInt count = iIdMap->Count();
       
   427     for (TInt i = 0; i < count; ++i)
       
   428         {
       
   429         const TPbk2IconId& iconId = iIdMap->At(i);
       
   430         CGulIcon* gulIcon = aFactory.CreateIconL( iconId );
       
   431         if ( gulIcon )
       
   432             {
       
   433             // replace the old icon only if new one was loaded
       
   434             delete At(i);
       
   435             At(i) = gulIcon;
       
   436             }
       
   437         }
       
   438     }
       
   439 
       
   440 //  End of File