skins/AknSkins/srvsrc/AknsSrvWallpaperCache.cpp
changeset 0 05e9090e2422
child 38 a10f447f38bd
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2003-2008 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:  Wallpaper Cache
       
    15 */
       
    16 
       
    17 // INCLUDE FILES
       
    18 #include <e32base.h>
       
    19 #include "AknsSrvWallpaperCache.h"
       
    20 #include "aknssrvwallpaperscaling.h"
       
    21 
       
    22 
       
    23 /**
       
    24 * Symbian constructor.
       
    25 */ 
       
    26 CAknsSrvWallpaperCache* CAknsSrvWallpaperCache::NewL( TInt aCacheSize )
       
    27     {
       
    28     CAknsSrvWallpaperCache* self =  new(ELeave) CAknsSrvWallpaperCache( );
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL( aCacheSize );
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 /**
       
    36 * Constructor.
       
    37 */
       
    38 CAknsSrvWallpaperCache::CAknsSrvWallpaperCache( )
       
    39     {    
       
    40     }
       
    41 
       
    42 /**
       
    43 * Symbian 2nd phase constructor.
       
    44 */
       
    45 void CAknsSrvWallpaperCache::ConstructL( TInt aCacheSize )
       
    46     {
       
    47     iCacheSize = aCacheSize;
       
    48     }
       
    49 
       
    50 /**
       
    51 * Destructor.
       
    52 */
       
    53 CAknsSrvWallpaperCache::~CAknsSrvWallpaperCache( )
       
    54     {
       
    55     ResetAndDestory();
       
    56     }
       
    57 
       
    58 /**
       
    59 * Add a image to wallpaper cache
       
    60 */
       
    61 TAknsSrvWallpaper* CAknsSrvWallpaperCache::AddL( RFs& aRFs, const TDesC& aFileName, 
       
    62         const TSize aTrgSize, const TSize aMaxSize )
       
    63     {
       
    64     if ( aTrgSize ==  TSize(-1, -1) )
       
    65         {
       
    66         TryDecodeImageL( aRFs,aFileName );
       
    67         return NULL;
       
    68         }
       
    69     
       
    70     TBool cached = EFalse;
       
    71     TAknsSrvWallpaper* wp = NULL;
       
    72     
       
    73     wp = CachedImage( aFileName );
       
    74     if ( !wp )
       
    75         {
       
    76         wp = new TAknsSrvWallpaper;
       
    77         ZeroItem ( *wp );
       
    78         }
       
    79     else
       
    80         {
       
    81         cached = ETrue;
       
    82         }
       
    83 
       
    84     
       
    85     _LIT( KSvgFileExt, ".svg" );
       
    86     TBool isSvgFormat = aFileName.Right(4).CompareF( KSvgFileExt ) == 0;
       
    87     
       
    88     TBool needDecodePortrait = EFalse;
       
    89     TBool needDecodeLandscape = EFalse;    
       
    90 
       
    91     if( aTrgSize.iHeight >= aTrgSize.iWidth ) //Portait
       
    92         {
       
    93         if ( aTrgSize != iPrtSize || ( !wp->iPortrait && !wp->iPortraitMask) )
       
    94             {
       
    95             needDecodePortrait = ETrue;
       
    96             iPrtSize = aTrgSize;
       
    97             }
       
    98         }
       
    99     else //Landscape
       
   100         {
       
   101         if ( aTrgSize != iLscSize || ( !wp->iLandscape && !wp->iLandscapeMask ) )
       
   102             {
       
   103             needDecodeLandscape = ETrue;
       
   104             iLscSize = aTrgSize;
       
   105             }
       
   106         }
       
   107     
       
   108     if( isSvgFormat )   
       
   109         {
       
   110         if( needDecodePortrait )
       
   111             {
       
   112             CAknsSrvSVGImageDecoder* svgdecoder = CAknsSrvSVGImageDecoder::NewL();
       
   113             CleanupStack::PushL( svgdecoder );
       
   114             svgdecoder->DecodeImageL(
       
   115                 aFileName,
       
   116                 aTrgSize,
       
   117                 wp->iPortrait,
       
   118                 wp->iPortraitMask );
       
   119             CleanupStack::PopAndDestroy( svgdecoder );
       
   120             }
       
   121         if( needDecodeLandscape )
       
   122             {
       
   123             CAknsSrvSVGImageDecoder* svgdecoder = CAknsSrvSVGImageDecoder::NewL();
       
   124             CleanupStack::PushL( svgdecoder );
       
   125             svgdecoder->DecodeImageL(
       
   126                 aFileName,
       
   127                 aTrgSize,
       
   128                 wp->iLandscape,
       
   129                 wp->iLandscapeMask );
       
   130             CleanupStack::PopAndDestroy( svgdecoder );
       
   131             }
       
   132         }
       
   133     else
       
   134         {
       
   135         if( needDecodePortrait )
       
   136             {
       
   137             CAknsSrvImageConverter::DecodeImageL(
       
   138                 aRFs,
       
   139                 aFileName,
       
   140                 aTrgSize,
       
   141                 wp->iPortrait,
       
   142                 wp->iPortraitMask,
       
   143                 aMaxSize );
       
   144             }
       
   145         if( needDecodeLandscape )
       
   146             {
       
   147             CAknsSrvImageConverter::DecodeImageL(
       
   148                 aRFs,
       
   149                 aFileName,
       
   150                 aTrgSize,
       
   151                 wp->iLandscape,
       
   152                 wp->iLandscapeMask,
       
   153                 aMaxSize );
       
   154             }
       
   155         }
       
   156     wp->iName.Copy( aFileName );
       
   157     if ( !cached )
       
   158         {
       
   159         RemoveOldestItem();        
       
   160         iCache.Append( wp );
       
   161         }
       
   162     return wp;
       
   163     }
       
   164 
       
   165 /**
       
   166 * Try Decode image
       
   167 */ 
       
   168 void CAknsSrvWallpaperCache::TryDecodeImageL( RFs& aRFs,const TDesC& aFileName )
       
   169     {
       
   170     if ( CachedImage( aFileName ) )
       
   171         {
       
   172         return;
       
   173         }
       
   174     _LIT( KSvgFileExt, ".svg" );
       
   175     TBool isSvgFormat = (aFileName.Right(4).CompareF( KSvgFileExt ) == 0);
       
   176     if ( !isSvgFormat )
       
   177         {
       
   178         CFbsBitmap* bitmap = NULL;
       
   179         CFbsBitmap* mask = NULL;
       
   180         CAknsSrvImageConverter::DecodeImageL(
       
   181             aRFs,
       
   182             aFileName,
       
   183             TSize(-1,-1),
       
   184             bitmap,
       
   185             mask,
       
   186             TSize(4000,4000) );
       
   187         delete bitmap;
       
   188         delete mask;
       
   189         }
       
   190     }
       
   191 
       
   192 /**
       
   193 * Remove a image from wallpaper cache
       
   194 */ 
       
   195 void CAknsSrvWallpaperCache::Remove( const TDesC& aFileName )
       
   196     {
       
   197     for ( int i =0;i < iCache.Count();i++ )
       
   198         {
       
   199         if ( !iCache[i]->iName.Compare( aFileName ) )
       
   200             {
       
   201             DestoryItem( *iCache[i] );
       
   202             iCache.Remove(i);
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207 /**
       
   208 * Reset wallpaper cache
       
   209 */ 
       
   210 void CAknsSrvWallpaperCache::ResetAndDestory( )
       
   211     {
       
   212     for ( int i =0; i < iCache.Count(); i++ )
       
   213         {
       
   214         DestoryItem( *iCache[i] );
       
   215         }
       
   216     iCache.Reset();
       
   217     }
       
   218 
       
   219 /**
       
   220 * Destory one TAknsSrvWallpaper item
       
   221 */ 
       
   222 void CAknsSrvWallpaperCache::DestoryItem( TAknsSrvWallpaper& aItem )
       
   223     {    
       
   224     delete aItem.iPortrait;
       
   225     delete aItem.iPortraitMask;
       
   226     delete aItem.iLandscape;
       
   227     delete aItem.iLandscapeMask;
       
   228    
       
   229     ZeroItem( aItem );
       
   230     }
       
   231 
       
   232 /**
       
   233 * Reset one TAknsSrvWallpaper item
       
   234 */
       
   235 void CAknsSrvWallpaperCache::ZeroItem( TAknsSrvWallpaper& aItem )
       
   236     {
       
   237     aItem.iPortrait = NULL;
       
   238     aItem.iPortraitMask = NULL;
       
   239     aItem.iLandscape = NULL;
       
   240     aItem.iLandscapeMask = NULL;
       
   241     
       
   242     aItem.iName.Zero();
       
   243     }
       
   244 
       
   245 /**
       
   246 * Remove the oldest wallpaper from cache
       
   247 */
       
   248 void CAknsSrvWallpaperCache::RemoveOldestItem()
       
   249     {
       
   250     if ( iCache.Count()>0 && iCache.Count() == iCacheSize )
       
   251         {
       
   252         DestoryItem( *iCache[0] );
       
   253         iCache.Remove(0);
       
   254         }
       
   255     }
       
   256 
       
   257 /**
       
   258 * Check if a image is in wallpaper cache
       
   259 */ 
       
   260 TBool CAknsSrvWallpaperCache::IsCached( const TDesC& aFileName )
       
   261     {
       
   262     if ( CachedImage( aFileName ) )
       
   263         {
       
   264         return ETrue;
       
   265         }
       
   266     return EFalse;
       
   267     }
       
   268 
       
   269 /**
       
   270 * Find in cache
       
   271 */ 
       
   272 TAknsSrvWallpaper* CAknsSrvWallpaperCache::CachedImage( const TDesC& aFileName )
       
   273     {
       
   274     TAknsSrvWallpaper* ret = NULL;
       
   275     for ( int i =0;i < iCache.Count();i++ )
       
   276         {
       
   277         if ( !iCache[i]->iName.Compare( aFileName ))
       
   278             {
       
   279             ret = iCache[i];
       
   280             }
       
   281         }
       
   282     return ret;
       
   283     }
       
   284 
       
   285 
       
   286 //  End of File