landmarksui/uicontrols/src/CLmkSelectorIconMgr.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 "CLmkSelectorIconMgr.h"
       
    27 #include "LmkConsts.h"
       
    28 #include "CLmkUiUtils.h"
       
    29 #include <data_caging_path_literals.hrh>
       
    30 #include <gulicon.h>
       
    31 #include <AknIconArray.h>
       
    32 #include <avkon.mbg>
       
    33 #include <AknsConstants.h>
       
    34 #include <AknsUtils.h>
       
    35 #include <AknIconUtils.h>
       
    36 #include <AknUtils.h>
       
    37 #include <lmkerrors.h>
       
    38 
       
    39 
       
    40 // CONSTANTS
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace {
       
    43 const TInt KLmkIconCacheGranularity = 30;
       
    44 const TInt KLmkIconFilesGranularity = 5;
       
    45 #if defined(_DEBUG)
       
    46 
       
    47 _LIT(KPanicMsg,"CLmkSelectorIconMgr");
       
    48 
       
    49 void Panic( TPanicCode aReason )
       
    50     {
       
    51     User::Panic( KPanicMsg, aReason );
       
    52     }
       
    53 #endif
       
    54 }  // namespace
       
    55 
       
    56 // ----------------------------------------------------
       
    57 // CLmkSelectorIconMgr::TIconData
       
    58 // ----------------------------------------------------
       
    59 //
       
    60 CLmkSelectorIconMgr::TIconData::TIconData():
       
    61     iFileIndex( KErrNotFound ),
       
    62     iIconIndexWithinFile( KErrNotFound ),
       
    63     iIconType( EDefinedIcon ),
       
    64     iIconArrayId( KErrNotFound )
       
    65     {
       
    66     }
       
    67 
       
    68 // ============================ MEMBER FUNCTIONS ===============================
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CLmkSelectorIconMgr::CLmkSelectorIconMgr
       
    72 // C++ default constructor can NOT contain any code, that
       
    73 // might leave.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CLmkSelectorIconMgr::CLmkSelectorIconMgr( CAknIconArray& aArray )
       
    77     : iArray( &aArray ),
       
    78       iCache( KLmkIconCacheGranularity )
       
    79     {
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CLmkSelectorIconMgr::ConstructL
       
    84 // Symbian 2nd phase constructor can leave.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CLmkSelectorIconMgr::ConstructL()
       
    88     {
       
    89     iIconFiles = new( ELeave ) CDesCArrayFlat( KLmkIconFilesGranularity );
       
    90     ResetMgrL();
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CLmkSelectorIconMgr::NewL
       
    95 // Two-phased constructor.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 
       
    99 CLmkSelectorIconMgr* CLmkSelectorIconMgr::NewL(
       
   100     CAknIconArray& aArray )
       
   101     {
       
   102     CLmkSelectorIconMgr* self = new( ELeave ) CLmkSelectorIconMgr( aArray );
       
   103 
       
   104     CleanupStack::PushL( self );
       
   105     self->ConstructL();
       
   106     CleanupStack::Pop();
       
   107 
       
   108     return self;
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------
       
   112 // CLmkSelectorIconMgr::~CLmkSelectorIconMgr
       
   113 // ----------------------------------------------------
       
   114 //
       
   115 CLmkSelectorIconMgr::~CLmkSelectorIconMgr()
       
   116     {
       
   117     iCache.Close();
       
   118     delete iIconFiles;
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------
       
   122 // CLmkSelectorIconMgr::GetIconL
       
   123 // ----------------------------------------------------
       
   124 //
       
   125 TInt CLmkSelectorIconMgr::GetIconL(
       
   126     const TDesC& aMbmFileName,
       
   127     TInt aIconIdInFile )
       
   128     {
       
   129     TInt position( 0 );
       
   130     if ( iIconFiles->Find( aMbmFileName, position, ECmpNormal ) == 0 )
       
   131         { // same file has been used already and so it exists in the array
       
   132         TInt count = iCache.Count();
       
   133         for ( TInt i( 0 ); i < count; ++i )
       
   134             {
       
   135             if ( position == iCache[i].iFileIndex  &&
       
   136                 aIconIdInFile == iCache[i].iIconIndexWithinFile )
       
   137                 { // icon was found in icon manager!
       
   138                 // If Array Index was not set properly, then set the index propely
       
   139                 if (( iCache[i].iIconArrayId < 0) ||
       
   140                 	  (iCache[i].iIconArrayId > KLmkIconCacheGranularity) )
       
   141 	                {
       
   142 	                break;
       
   143 	                }
       
   144                 return iCache[i].iIconArrayId;
       
   145                 }
       
   146             }
       
   147         }
       
   148 
       
   149     // aMbmFileName does not yet exist in the file name array OR it exists but
       
   150     // this specific icon hasn't been requested yet:
       
   151 
       
   152     // create icon manager item:
       
   153     TInt iconMgrId = CreateIconMgrItemL( aMbmFileName, aIconIdInFile );
       
   154 
       
   155     // try to load new icon:
       
   156     iCache[iconMgrId].iIconArrayId =
       
   157         LoadAndAppendIconL( aMbmFileName, aIconIdInFile );
       
   158 
       
   159     return iCache[iconMgrId].iIconArrayId;
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------
       
   163 // CLmkSelectorIconMgr::GetDefaultIconL
       
   164 // ----------------------------------------------------
       
   165 //
       
   166 TInt CLmkSelectorIconMgr::GetDefaultIconL( TIconType aIcon )
       
   167     {
       
   168     TInt count = iCache.Count();
       
   169     for ( TInt i( 0  ); i < count ; ++i )
       
   170         {
       
   171         TIconData tmp = iCache[i];
       
   172         if ( tmp.iIconType == aIcon )
       
   173             { // default icon was found
       
   174             if ( tmp.iIconArrayId < 0 )
       
   175                 { // try to load new icon.
       
   176                 TPtrC iconFile = ( *iIconFiles )[tmp.iFileIndex];
       
   177                 tmp.iIconArrayId =
       
   178                     LoadAndAppendIconL( iconFile, tmp.iIconIndexWithinFile );
       
   179                 }
       
   180             return tmp.iIconArrayId;
       
   181             }
       
   182         }
       
   183     return KErrNotFound; // default icon was not found
       
   184     }
       
   185 
       
   186 // ----------------------------------------------------
       
   187 // CLmkSelectorIconMgr::SetDefaultIconL
       
   188 // ----------------------------------------------------
       
   189 //
       
   190 void CLmkSelectorIconMgr::SetDefaultIconL(
       
   191     TIconType aIcon,
       
   192     const TDesC& aMbmFileName,
       
   193     TInt aIconIdInFile )
       
   194     {
       
   195     if ( GetDefaultIconL( aIcon ) >= 0 )
       
   196         { // default icon already set
       
   197         User::Leave( KErrAlreadyExists );
       
   198         }
       
   199 
       
   200     TInt iconMgrId = CreateIconMgrItemL( aMbmFileName, aIconIdInFile );
       
   201     iCache[iconMgrId].iIconType = aIcon;
       
   202     // Added for updating Icon index
       
   203     iCache[iconMgrId].iIconArrayId =
       
   204         LoadAndAppendIconL( aMbmFileName, aIconIdInFile );
       
   205     }
       
   206 
       
   207 // ----------------------------------------------------
       
   208 // CLmkSelectorIconMgr::SetIconAtIndexZeroL
       
   209 // ----------------------------------------------------
       
   210 //
       
   211 void CLmkSelectorIconMgr::SetIconAtIndexZeroL(
       
   212     const TDesC& aMbmFileName,
       
   213     TAknsItemID aSkinID,
       
   214     TInt aIconId,
       
   215     TInt aMaskId )
       
   216     {
       
   217     __ASSERT_DEBUG( iArray->Count() >= 2,
       
   218                     Panic( KLmkPanicIndexesZeroOneNotSet ) );
       
   219 
       
   220     CGulIcon* icon = LoadIconL( aMbmFileName, aSkinID, aIconId, aMaskId );
       
   221     CleanupStack::PushL( icon );
       
   222 
       
   223     CGulIcon* old = iArray->At( 0 );
       
   224     iArray->Delete( 0 );
       
   225     delete old;
       
   226     iArray->InsertL( 0, icon );
       
   227 
       
   228     CleanupStack::Pop( icon ); // ownership transferred to icon array
       
   229     }
       
   230 
       
   231 // ----------------------------------------------------
       
   232 // CLmkSelectorIconMgr::SetIconAtIndexOneL
       
   233 // ----------------------------------------------------
       
   234 //
       
   235 void CLmkSelectorIconMgr::SetIconAtIndexOneL(
       
   236     const TDesC& aMbmFileName,
       
   237     TAknsItemID aSkinID,
       
   238     TInt aIconId,
       
   239     TInt aMaskId )
       
   240     {
       
   241     __ASSERT_DEBUG( iArray->Count() >= 2,
       
   242                     Panic( KLmkPanicIndexesZeroOneNotSet ) );
       
   243 
       
   244     CGulIcon* icon = LoadIconL( aMbmFileName, aSkinID, aIconId, aMaskId );
       
   245     CleanupStack::PushL( icon );
       
   246 
       
   247     CGulIcon* old = iArray->At( 1 );
       
   248     iArray->Delete( 1 );
       
   249     delete old;
       
   250     iArray->InsertL( 1, icon );
       
   251 
       
   252     CleanupStack::Pop( icon ); // ownership transferred to icon array
       
   253     }
       
   254 
       
   255 // ----------------------------------------------------
       
   256 // CLmkSelectorIconMgr::ResetMgrL
       
   257 // ----------------------------------------------------
       
   258 //
       
   259 void CLmkSelectorIconMgr::ResetMgrL()
       
   260     {
       
   261     iArray->ResetAndDestroy();
       
   262     iIconFiles->Reset();
       
   263     iCache.Reset();
       
   264 
       
   265     // By default set empty icons as selection icons:
       
   266 
       
   267     TFileName* iconFile = CLmkUiUtils::AvkonIconFileLC();
       
   268     CGulIcon* icon = LoadIconL( *iconFile,
       
   269                                 KAknsIIDQgnPropEmpty,
       
   270                                 EMbmAvkonQgn_prop_empty,
       
   271                                 EMbmAvkonQgn_prop_empty_mask );
       
   272     CleanupStack::PushL( icon );
       
   273     iArray->AppendL( icon );
       
   274     CleanupStack::Pop( icon ); // ownership transferred to icon array
       
   275     icon = NULL;
       
   276 
       
   277     icon = LoadIconL( *iconFile,
       
   278                       KAknsIIDQgnPropEmpty,
       
   279                       EMbmAvkonQgn_prop_empty,
       
   280                       EMbmAvkonQgn_prop_empty_mask );
       
   281     CleanupStack::PushL( icon );
       
   282     iArray->AppendL( icon );
       
   283     CleanupStack::Pop( icon ); // ownership transferred to icon array
       
   284     CleanupStack::PopAndDestroy( ); // iconFile
       
   285     }
       
   286 
       
   287 // ============================= HELPER METHODS ================================
       
   288 
       
   289 // ----------------------------------------------------
       
   290 // CLmkSelectorIconMgr::LoadIconL
       
   291 // ----------------------------------------------------
       
   292 //
       
   293 CGulIcon* CLmkSelectorIconMgr::LoadIconL(
       
   294     const TDesC& aMbmFileName,
       
   295     TInt aIconIdInFile )
       
   296     {
       
   297     CFbsBitmap* bitmap = NULL;
       
   298     CFbsBitmap* bitmapMask = NULL;
       
   299 
       
   300 	AknIconUtils::CreateIconLC(bitmap, bitmapMask, aMbmFileName,
       
   301                              aIconIdInFile, aIconIdInFile + 1);
       
   302     CGulIcon* icon = CGulIcon::NewL( bitmap, bitmapMask );
       
   303     CleanupStack::Pop( 2 ); // bitmapMask, bitmap. icon has now ownership
       
   304     return icon;
       
   305     }
       
   306 
       
   307 // ----------------------------------------------------
       
   308 // CLmkSelectorIconMgr::LoadIconL
       
   309 // ----------------------------------------------------
       
   310 //
       
   311 CGulIcon* CLmkSelectorIconMgr::LoadIconL(
       
   312     const TDesC& aMbmFileName,
       
   313     TAknsItemID aSkinID,
       
   314     TInt aIconId,
       
   315     TInt aMaskId )
       
   316     {
       
   317     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   318 
       
   319     CFbsBitmap* bitmap = NULL;
       
   320     CFbsBitmap* bitmapMask = NULL;
       
   321     AknsUtils::CreateIconLC( skin, aSkinID, bitmap, bitmapMask, aMbmFileName,
       
   322                              aIconId, aMaskId );
       
   323     CGulIcon* icon = CGulIcon::NewL( bitmap, bitmapMask );
       
   324     CleanupStack::Pop( 2 ); // bitmapMask, bitmap. icon has now ownership
       
   325     return icon;
       
   326     }
       
   327 
       
   328 // ----------------------------------------------------
       
   329 // CLmkSelectorIconMgr::LoadAndAppendIconL
       
   330 // ----------------------------------------------------
       
   331 //
       
   332 TInt CLmkSelectorIconMgr::LoadAndAppendIconL(
       
   333     const TDesC& aMbmFileName,
       
   334     TInt aIconIdInFile )
       
   335     {
       
   336     CGulIcon* icon = LoadIconL( aMbmFileName, aIconIdInFile );
       
   337     CleanupStack::PushL( icon );
       
   338     iArray->AppendL( icon );
       
   339     CleanupStack::Pop( icon );
       
   340     // Added for avoiding returning -1 as bitmap Index
       
   341     if (iArray->Count() > 0)
       
   342 	    {
       
   343 	    return iArray->Count() - 1;
       
   344 	    }
       
   345     else
       
   346 	    {
       
   347 	    return 0;
       
   348 	    }
       
   349     }
       
   350 
       
   351 // ----------------------------------------------------
       
   352 // CLmkSelectorIconMgr::CreateIconMgrItemL
       
   353 // ----------------------------------------------------
       
   354 //
       
   355 TInt CLmkSelectorIconMgr::CreateIconMgrItemL(
       
   356     const TDesC& aMbmFileName,
       
   357     TInt aIconIdInFile )
       
   358     {
       
   359     TIconData tmp;
       
   360 
       
   361 
       
   362     tmp.iFileIndex = AppendedIconFileIndexL( aMbmFileName );
       
   363     tmp.iIconIndexWithinFile = aIconIdInFile;
       
   364 
       
   365     User::LeaveIfError( iCache.Append( tmp ) );
       
   366     if (iCache.Count() > 0)
       
   367 	    {
       
   368 	    return iCache.Count() - 1;
       
   369 	    }
       
   370     else
       
   371 	    {
       
   372 	    return 0;
       
   373 	    }
       
   374     }
       
   375 
       
   376 // ----------------------------------------------------
       
   377 // CLmkSelectorIconMgr::AppendedIconFileIndexL
       
   378 // ----------------------------------------------------
       
   379 //
       
   380 TInt CLmkSelectorIconMgr::AppendedIconFileIndexL( const TDesC& aMbmFileName )
       
   381     {
       
   382     TInt position( 0 );
       
   383     if ( iIconFiles->Find( aMbmFileName, position, ECmpNormal ) != 0 )
       
   384         { // file does not yet exist in the file name array
       
   385         iIconFiles->AppendL( aMbmFileName );
       
   386         return iIconFiles->Count() - 1;
       
   387         }
       
   388     else
       
   389         { // file already exists, just return its position
       
   390         return position;
       
   391         }
       
   392     }
       
   393 
       
   394 //  End of File