homescreensrv_plat/sapi_menucontent/mcsservice/src/mcsiconutility.cpp
changeset 0 79c6a41cd166
child 2 b7904b40483f
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <AknsUtils.h> 
       
    22 #include <mcsmenu.h>
       
    23 #include <mcsmenuitem.h>
       
    24 #include <mcsmenuutils.h>
       
    25 #include <SATDomainPSKeys.h>
       
    26 #include <e32property.h>
       
    27 #include <apgcli.h>
       
    28 #include <AknInternalIconUtils.h>
       
    29 
       
    30 
       
    31 #include <liwcommon.h>
       
    32 
       
    33 
       
    34 
       
    35 //#include "mcsextendedmenuitem.h"
       
    36 #include "mcsiconutility.h"
       
    37 #include "mcssathandler.h"
       
    38 #include <mcsdef.h>
       
    39 
       
    40 // Constants
       
    41 _LIT( KMifIconPath, "\\resource\\apps\\" );
       
    42 const TUint32 KMifIconPathLenght = 18;
       
    43 const TInt KJavaIconWidth = 88;			// Java icon default width
       
    44 const TInt KJavaIconHeight = 88;		// Java icon default height
       
    45 
       
    46 
       
    47 
       
    48 // ================= LOCAL FUNCTIONS ========================
       
    49 
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CreateMbmIconLC2 - Creates bitmap
       
    53 // -----------------------------------------------------------------------------
       
    54 //    
       
    55 LOCAL_C TBool CreateMbmIconLC2(CFbsBitmap*& aBitmap, CFbsBitmap*& aMask, const TDesC& aAppUid ) 
       
    56     {
       
    57     TBool ret = EFalse;
       
    58     TSize size( KJavaIconWidth, KJavaIconHeight );
       
    59 
       
    60     aBitmap = NULL;
       
    61     aMask = NULL;
       
    62     
       
    63     RApaLsSession lsSession;
       
    64     User::LeaveIfError( lsSession.Connect() );
       
    65     CleanupClosePushL( lsSession ); // lsSession (1)
       
    66     
       
    67     TUint appUidUint;
       
    68     
       
    69     // Gets app TUid from aAppUid descryptor
       
    70     User::LeaveIfError( MenuUtils::GetTUint( aAppUid, appUidUint ) );
       
    71     TUid appUid( TUid::Uid( appUidUint) );
       
    72     
       
    73     // Tries to find icon with default size.
       
    74     CApaMaskedBitmap* apaBmp = CApaMaskedBitmap::NewLC();
       
    75     TInt apaErr = lsSession.GetAppIcon( appUid, size, *apaBmp );
       
    76     if ( apaErr == KErrNotFound ) 
       
    77         {
       
    78         // Icon not found.
       
    79         // AppArc tries to find equal to or smaller than the specified size.
       
    80         // Get all icon sizes and search for next bigger icon size.
       
    81         CArrayFixFlat<TSize>* sizes = new(ELeave) CArrayFixFlat<TSize>( 5 );
       
    82         apaErr = lsSession.GetAppIconSizes(appUid, *sizes);
       
    83         if ( !apaErr )
       
    84             {
       
    85             if ( sizes->Count() ) 
       
    86                 {
       
    87                 // There are other icon sizes
       
    88                 TInt idx = -1;
       
    89                 TInt square( size.iWidth * size.iHeight );
       
    90                 for ( TInt i = 0; i < sizes->Count(); i++ ) 
       
    91                     {
       
    92                     if ( ( sizes->At( i ).iWidth * sizes->At( i ).iHeight ) > square ) 
       
    93                         {
       
    94                         idx = i;
       
    95                         break;
       
    96                         }
       
    97                     }
       
    98                     
       
    99                 if ( idx >= 0 ) 
       
   100                     {
       
   101                     // Next bigger icon.
       
   102                     size = sizes->At( idx );
       
   103                     }
       
   104                 else 
       
   105                     {
       
   106                     // Just get the first available.
       
   107                     size = sizes->At( 0 );
       
   108                     }
       
   109                 }
       
   110             
       
   111             // Gets icon with new size.
       
   112             apaErr = lsSession.GetAppIcon( appUid, size, *apaBmp );
       
   113             }
       
   114 
       
   115         // Reset and destroy.
       
   116         sizes->Reset();
       
   117         delete sizes;
       
   118         sizes = NULL;
       
   119         }
       
   120         
       
   121     // Some builds requires brackets
       
   122     // logical correction was also made
       
   123     if ( ( apaErr != KErrNone ) &&
       
   124          ( apaErr != KErrNotFound ) &&
       
   125          ( apaErr != KErrNotSupported ) ) 
       
   126         {
       
   127         // System wide error.
       
   128         User::LeaveIfError( apaErr );
       
   129         }
       
   130     
       
   131     if( !apaErr && 
       
   132         apaBmp && 
       
   133         apaBmp->Mask() &&
       
   134         apaBmp->Handle() && 
       
   135         apaBmp->Mask()->Handle()) 
       
   136         {
       
   137         // AIF-based icon
       
   138         CFbsBitmap* iconOwnedBitmap = new (ELeave) CFbsBitmap(); 
       
   139         CleanupStack::PushL( iconOwnedBitmap ); // iob (3)
       
   140         CFbsBitmap* iconOwnedMask = new (ELeave) CFbsBitmap();
       
   141         CleanupStack::PushL( iconOwnedMask ); // iom (4)
       
   142         User::LeaveIfError(
       
   143             iconOwnedBitmap->Duplicate( apaBmp->Handle() ) );
       
   144         User::LeaveIfError(
       
   145             iconOwnedMask->Duplicate( apaBmp->Mask()->Handle() ) );
       
   146         
       
   147         CAknIcon* tmpIcon = CAknIcon::NewL();
       
   148 
       
   149         // Ownership is transferred
       
   150         tmpIcon->SetBitmap( iconOwnedBitmap );
       
   151         tmpIcon->SetMask( iconOwnedMask );
       
   152         // Ownership of tmpIcon is transferred
       
   153         CAknIcon* appIcon = AknIconUtils::CreateIconL( tmpIcon );
       
   154         CleanupStack::Pop( 2 ); // iom, iob (2)
       
   155 
       
   156         aBitmap = appIcon->Bitmap();
       
   157         aMask = appIcon->Mask();
       
   158         AknInternalIconUtils::SetAppIcon(aBitmap);
       
   159         
       
   160         // Detach and delete
       
   161         appIcon->SetBitmap( NULL );
       
   162         appIcon->SetMask( NULL );
       
   163         delete appIcon;
       
   164 
       
   165         ret = ETrue;
       
   166         }
       
   167         
       
   168     CleanupStack::PopAndDestroy( 2 ); // lsSession, apaBmp (0)
       
   169 
       
   170     // These are both safe
       
   171     if ( aBitmap ) 
       
   172         {
       
   173         CleanupStack::PushL( aBitmap ); // (1)
       
   174         CleanupStack::PushL( aMask ); // (2)
       
   175         }
       
   176     
       
   177     return ret;
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // ParseIconFilePathL
       
   183 // ---------------------------------------------------------
       
   184 //
       
   185 
       
   186 LOCAL_C void ParseIconFilePathL( const TDesC& aIconFilePath, RBuf& aIconFilePathBuf )
       
   187 	{
       
   188 	TParsePtrC fileParsePtrC(aIconFilePath);
       
   189 	if( !fileParsePtrC.PathPresent())
       
   190 		{
       
   191 		aIconFilePathBuf.CreateL(KMifIconPathLenght + aIconFilePath.Length());
       
   192 		aIconFilePathBuf.Append(KMifIconPath);
       
   193 		}
       
   194 	else 
       
   195 	    {
       
   196 		aIconFilePathBuf.CreateL(aIconFilePath.Length());
       
   197 	    }
       
   198 	aIconFilePathBuf.Append(aIconFilePath);
       
   199 	}
       
   200 
       
   201 // ---------------------------------------------------------
       
   202 // LoadSkinL
       
   203 // ---------------------------------------------------------
       
   204 //
       
   205 LOCAL_C CAknIcon* LoadSkinL( TUint majorId, TUint minorId )
       
   206     {
       
   207     CAknIcon* icon = NULL;
       
   208     CFbsBitmap* bitmap( 0 );
       
   209     CFbsBitmap* mask( 0 );
       
   210 
       
   211     TAknsItemID skinId;
       
   212     skinId.Set( majorId, minorId );
       
   213 
       
   214     AknsUtils::CreateIconLC(
       
   215         AknsUtils::SkinInstance(),
       
   216         skinId,
       
   217         bitmap,
       
   218         mask,
       
   219         KNullDesC,
       
   220         KErrNotFound,
       
   221         KErrNotFound );
       
   222 
       
   223     icon = CAknIcon::NewL();
       
   224     icon->SetBitmap( bitmap );
       
   225     icon->SetMask( mask );
       
   226 
       
   227      // bitmap and icon, AknsUtils::CreateIconLC doesn't specify the order
       
   228     CleanupStack::Pop( 2 );
       
   229 
       
   230     return icon;
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------
       
   234 // LoadSkinL
       
   235 // ---------------------------------------------------------
       
   236 //
       
   237 LOCAL_C CAknIcon* LoadAppSkinL( TUid& aAppUid )
       
   238     {
       
   239     CAknIcon* icon = NULL;
       
   240     CFbsBitmap* bitmap( 0 );
       
   241     CFbsBitmap* mask( 0 );
       
   242     
       
   243     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   244     
       
   245     TRAPD( err,  
       
   246         { 
       
   247         AknsUtils::CreateAppIconLC( skin, aAppUid, EAknsAppIconTypeList, bitmap, mask );            
       
   248         CleanupStack::Pop(2); //for trap
       
   249        }
       
   250         );
       
   251     if( err == KErrNone )
       
   252         {
       
   253         icon = CAknIcon::NewL();
       
   254         icon->SetBitmap( bitmap );
       
   255         icon->SetMask( mask );
       
   256         }
       
   257     return icon;
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------
       
   261 // GetSkinL
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 LOCAL_C CAknIcon* GetSkinL( const CLiwDefaultMap* aMap )
       
   265 	{
       
   266 	CAknIcon* icon = NULL;
       
   267 	TUint majorId( 0 );
       
   268     TUint minorId( 0 );
       
   269     TPtrC idString( KNullDesC );
       
   270     
       
   271     TLiwVariant variant;
       
   272     variant.PushL();
       
   273 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   274 	attrName.Copy( KMenuAttrIconSkinMajorId );
       
   275     
       
   276     if( aMap->FindL( attrName, variant))
       
   277     	{
       
   278     	variant.Get( idString );
       
   279 		User::LeaveIfError( MenuUtils::GetTUint( idString, majorId ) );
       
   280 		attrName.Copy( KMenuAttrIconSkinMinorId );
       
   281 	    if( aMap->FindL( attrName, variant))
       
   282 	    	{
       
   283 	    	variant.Get( idString );
       
   284 			User::LeaveIfError( MenuUtils::GetTUint( idString, minorId ) );
       
   285 			icon = LoadSkinL( majorId, minorId );
       
   286 			}
       
   287 		}
       
   288 	CleanupStack::PopAndDestroy( &variant );
       
   289 	return icon;
       
   290 	}
       
   291 
       
   292 // ---------------------------------------------------------
       
   293 // GetIconL
       
   294 // ---------------------------------------------------------
       
   295 //
       
   296 LOCAL_C CAknIcon* GetIconL( const CLiwDefaultMap* aMap )
       
   297 	{
       
   298 	CAknIcon* icon = NULL;
       
   299     TUint iconId( 0 );
       
   300     TUint maskId( 0 );
       
   301 	TPtrC attrStr( KNullDesC );
       
   302 	TPtrC iconFilePath;
       
   303 			
       
   304 	TLiwVariant variantIconFile;
       
   305 	TLiwVariant variantIconId;
       
   306 	TLiwVariant variantMaskId;
       
   307 	variantIconFile.PushL();
       
   308 	variantIconId.PushL();
       
   309 	variantMaskId.PushL();
       
   310 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   311 	attrName.Copy( KMenuAttrIconFile );
       
   312 	    
       
   313 	if( aMap->FindL( attrName, variantIconFile))
       
   314 		{
       
   315 		variantIconFile.Get( iconFilePath );
       
   316 		attrName.Copy( KMenuAttrIconId );
       
   317 		if( aMap->FindL( attrName, variantIconId) )
       
   318 			{
       
   319 			variantIconId.Get( attrStr );
       
   320 			User::LeaveIfError( MenuUtils::GetTUint( attrStr, iconId ) );
       
   321 			attrName.Copy( KMenuAttrMaskId );
       
   322 			if( aMap->FindL( attrName, variantMaskId) )
       
   323 				{
       
   324 				variantMaskId.Get( attrStr );
       
   325 				User::LeaveIfError( MenuUtils::GetTUint( attrStr, maskId ) );
       
   326 				CFbsBitmap* bitmap( 0 );
       
   327 				CFbsBitmap* mask( 0 );
       
   328 				RBuf pathBuf;
       
   329 				pathBuf.CleanupClosePushL();
       
   330 				ParseIconFilePathL(iconFilePath, pathBuf);
       
   331 				AknIconUtils::CreateIconLC( bitmap, mask, 
       
   332 						pathBuf, iconId, maskId ); 
       
   333 				icon = CAknIcon::NewL();
       
   334 				icon->SetBitmap( bitmap );
       
   335 				icon->SetMask( mask );
       
   336 				CleanupStack::Pop( 2 );
       
   337 				CleanupStack::PopAndDestroy( &pathBuf );			
       
   338 				}
       
   339 			}
       
   340 		}
       
   341     CleanupStack::PopAndDestroy( &variantMaskId );
       
   342     CleanupStack::PopAndDestroy( &variantIconId );
       
   343     CleanupStack::PopAndDestroy( &variantIconFile );
       
   344 	return icon;
       
   345 	}
       
   346 
       
   347 
       
   348 
       
   349 
       
   350 // ---------------------------------------------------------
       
   351 // GetDefaultSkinL
       
   352 // ---------------------------------------------------------
       
   353 //
       
   354 LOCAL_C CAknIcon* GetDefaultSkinL( const CLiwDefaultMap* aMap )
       
   355 	{
       
   356 	CAknIcon* icon = NULL;
       
   357 	TPtrC attrValueUid(KNullDesC);
       
   358 	TPtrC attrValueNative(KNullDesC);
       
   359 	TPtrC attrValueChildren(KNullDesC);
       
   360 	
       
   361 	TLiwVariant variantUid;
       
   362 	variantUid.PushL();
       
   363 	TLiwVariant variantNative;
       
   364 	variantNative.PushL();
       
   365 	TLiwVariant variantChildren;
       
   366 	variantChildren.PushL();
       
   367 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   368 	
       
   369 	//APPLICATION
       
   370 	attrName.Copy( KMenuAttrUid );
       
   371 	if( aMap->FindL( attrName, variantUid))
       
   372 		{
       
   373 		variantUid.Get( attrValueUid );
       
   374 	      
       
   375 		if( attrValueUid == KMenuSatUiUid )
       
   376 			{
       
   377 			CMcsSatHandler* satHandler = CMcsSatHandler::NewL();
       
   378 			CleanupStack::PushL( satHandler );
       
   379 			icon = satHandler->LoadIconL();
       
   380 			CleanupStack::PopAndDestroy( satHandler );
       
   381 			}
       
   382 		if( !icon )
       
   383 			{
       
   384 			attrName.Copy( KMenuAttrNative );
       
   385 			if( aMap->FindL( attrName, variantNative) )
       
   386 				{
       
   387 				variantNative.Get( attrValueNative );
       
   388 				if( attrValueNative == KMenuFalse )
       
   389 					{
       
   390 					CFbsBitmap* bitmap( 0 );
       
   391 					CFbsBitmap* mask( 0 );
       
   392 					//for native
       
   393 					if( CreateMbmIconLC2( bitmap, mask, attrValueUid ) )
       
   394 						{
       
   395 						icon = CAknIcon::NewL();
       
   396 						icon->SetBitmap( bitmap );
       
   397 						icon->SetMask( mask );
       
   398 						CleanupStack::Pop( 2 );
       
   399 						}
       
   400 					}
       
   401 				}
       
   402 			if( !icon )
       
   403 				 {
       
   404 				 TUint appUid;
       
   405 				 TUid uid;
       
   406 				 TInt err = MenuUtils::GetTUint( attrValueUid, appUid );
       
   407 				 if ( !err )
       
   408 					 {
       
   409 					 uid = TUid::Uid( appUid );
       
   410 					 icon = LoadAppSkinL( uid );
       
   411 					 }
       
   412 				 }
       
   413 			if( !icon )
       
   414 				{
       
   415 				icon = LoadSkinL(
       
   416 						KAknsIIDQgnMenuUnknownLst.iMajor,
       
   417 						KAknsIIDQgnMenuUnknownLst.iMinor );
       
   418 				}
       
   419 			}
       
   420 		}
       
   421     
       
   422     //FOLDER
       
   423     TUint childrenCount;
       
   424    
       
   425 	attrName.Copy( KChildrenCount );
       
   426 	if( aMap->FindL( attrName, variantChildren))
       
   427 		{
       
   428 		variantChildren.Get( attrValueChildren );
       
   429 		TInt err = MenuUtils::GetTUint( attrValueChildren, childrenCount );
       
   430 		if( childrenCount > 0 )
       
   431 		    {
       
   432 		    icon = LoadSkinL(
       
   433 		        KAknsIIDQgnPropFolderAppsMedium.iMajor,
       
   434 		        KAknsIIDQgnPropFolderAppsMedium.iMinor ); 
       
   435 		    }
       
   436 		else
       
   437 		    {
       
   438             icon = LoadSkinL(
       
   439                 KAknsIIDQgnMenuFolderEmpty.iMajor,
       
   440                 KAknsIIDQgnMenuFolderEmpty.iMinor );
       
   441             }
       
   442         }
       
   443 	CleanupStack::PopAndDestroy( &variantChildren );
       
   444 	CleanupStack::PopAndDestroy( &variantNative );
       
   445 	CleanupStack::PopAndDestroy( &variantUid );
       
   446     return icon;
       
   447 	}
       
   448 
       
   449 // ================= MEMBER FUNCTIONS =======================
       
   450 
       
   451 
       
   452 // ---------------------------------------------------------
       
   453 // MenuIconUtility::GetItemIconL
       
   454 // ---------------------------------------------------------
       
   455 //
       
   456 CAknIcon* McsIconUtility::GetItemIconL( const CLiwDefaultMap* aMap )
       
   457 	{
       
   458 	CAknIcon* icon = NULL;
       
   459 	
       
   460 	// Try to get the skin of the item
       
   461 	TRAP_IGNORE(icon = GetSkinL( aMap ));
       
   462 	if ( icon )
       
   463 		{
       
   464 		return icon;
       
   465 		}
       
   466 
       
   467 	// Try to get the icon of the item (skin failed)
       
   468 	TRAP_IGNORE(icon = GetIconL( aMap ));
       
   469 	if ( icon )
       
   470 		{
       
   471 		return icon;
       
   472 		}
       
   473 
       
   474 	// Return a default (skin and icon failed)
       
   475 	icon = GetDefaultSkinL( aMap );
       
   476 	return icon;
       
   477 	}
       
   478