homescreensrv_plat/sapi_menucontent/mcsservice/src/mcsiconutility.cpp
branchRCL_3
changeset 114 a5a39a295112
equal deleted inserted replaced
113:0efa10d348c0 114:a5a39a295112
       
     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         // The line below was commented out to improve the appearance of scaled
       
   159         // icons of java applications.
       
   160         // AknInternalIconUtils::SetAppIcon(aBitmap);
       
   161         
       
   162         // Detach and delete
       
   163         appIcon->SetBitmap( NULL );
       
   164         appIcon->SetMask( NULL );
       
   165         delete appIcon;
       
   166 
       
   167         ret = ETrue;
       
   168         }
       
   169         
       
   170     CleanupStack::PopAndDestroy( 2 ); // lsSession, apaBmp (0)
       
   171 
       
   172     // These are both safe
       
   173     if ( aBitmap ) 
       
   174         {
       
   175         CleanupStack::PushL( aBitmap ); // (1)
       
   176         CleanupStack::PushL( aMask ); // (2)
       
   177         }
       
   178     
       
   179     return ret;
       
   180     }
       
   181 
       
   182 
       
   183 // ---------------------------------------------------------
       
   184 // ParseIconFilePathL
       
   185 // ---------------------------------------------------------
       
   186 //
       
   187 
       
   188 LOCAL_C void ParseIconFilePathL( const TDesC& aIconFilePath, RBuf& aIconFilePathBuf )
       
   189 	{
       
   190 	TParsePtrC fileParsePtrC(aIconFilePath);
       
   191 	if( !fileParsePtrC.PathPresent())
       
   192 		{
       
   193 		aIconFilePathBuf.CreateL(KMifIconPathLenght + aIconFilePath.Length());
       
   194 		aIconFilePathBuf.Append(KMifIconPath);
       
   195 		}
       
   196 	else 
       
   197 	    {
       
   198 		aIconFilePathBuf.CreateL(aIconFilePath.Length());
       
   199 	    }
       
   200 	aIconFilePathBuf.Append(aIconFilePath);
       
   201 	}
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // LoadSkinL
       
   205 // ---------------------------------------------------------
       
   206 //
       
   207 LOCAL_C CAknIcon* LoadSkinL( TUint majorId, TUint minorId )
       
   208     {
       
   209     CAknIcon* icon = NULL;
       
   210     CFbsBitmap* bitmap( 0 );
       
   211     CFbsBitmap* mask( 0 );
       
   212 
       
   213     TAknsItemID skinId;
       
   214     skinId.Set( majorId, minorId );
       
   215 
       
   216     AknsUtils::CreateIconLC(
       
   217         AknsUtils::SkinInstance(),
       
   218         skinId,
       
   219         bitmap,
       
   220         mask,
       
   221         KNullDesC,
       
   222         KErrNotFound,
       
   223         KErrNotFound );
       
   224 
       
   225     icon = CAknIcon::NewL();
       
   226     icon->SetBitmap( bitmap );
       
   227     icon->SetMask( mask );
       
   228 
       
   229      // bitmap and icon, AknsUtils::CreateIconLC doesn't specify the order
       
   230     CleanupStack::Pop( 2 );
       
   231 
       
   232     return icon;
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------
       
   236 // LoadSkinL
       
   237 // ---------------------------------------------------------
       
   238 //
       
   239 LOCAL_C CAknIcon* LoadAppSkinL( TUid& aAppUid )
       
   240     {
       
   241     CAknIcon* icon = NULL;
       
   242     CFbsBitmap* bitmap( 0 );
       
   243     CFbsBitmap* mask( 0 );
       
   244     
       
   245     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   246     
       
   247     TRAPD( err,  
       
   248         { 
       
   249         AknsUtils::CreateAppIconLC( skin, aAppUid, EAknsAppIconTypeList, bitmap, mask );            
       
   250         CleanupStack::Pop(2); //for trap
       
   251        }
       
   252         );
       
   253     if( err == KErrNone )
       
   254         {
       
   255         icon = CAknIcon::NewL();
       
   256         icon->SetBitmap( bitmap );
       
   257         icon->SetMask( mask );
       
   258         }
       
   259     return icon;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // GetSkinL
       
   264 // ---------------------------------------------------------
       
   265 //
       
   266 LOCAL_C CAknIcon* GetSkinL( const CLiwDefaultMap* aMap )
       
   267 	{
       
   268 	CAknIcon* icon = NULL;
       
   269 	TUint majorId( 0 );
       
   270     TUint minorId( 0 );
       
   271     TPtrC idString( KNullDesC );
       
   272     
       
   273     TLiwVariant variant;
       
   274     variant.PushL();
       
   275 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   276 	attrName.Copy( KMenuAttrIconSkinMajorId );
       
   277     
       
   278     if( aMap->FindL( attrName, variant))
       
   279     	{
       
   280     	variant.Get( idString );
       
   281 		User::LeaveIfError( MenuUtils::GetTUint( idString, majorId ) );
       
   282 		attrName.Copy( KMenuAttrIconSkinMinorId );
       
   283 	    if( aMap->FindL( attrName, variant))
       
   284 	    	{
       
   285 	    	variant.Get( idString );
       
   286 			User::LeaveIfError( MenuUtils::GetTUint( idString, minorId ) );
       
   287 			icon = LoadSkinL( majorId, minorId );
       
   288 			}
       
   289 		}
       
   290 	CleanupStack::PopAndDestroy( &variant );
       
   291 	return icon;
       
   292 	}
       
   293 
       
   294 // ---------------------------------------------------------
       
   295 // GetIconL
       
   296 // ---------------------------------------------------------
       
   297 //
       
   298 LOCAL_C CAknIcon* GetIconL( const CLiwDefaultMap* aMap )
       
   299 	{
       
   300 	CAknIcon* icon = NULL;
       
   301     TUint iconId( 0 );
       
   302     TUint maskId( 0 );
       
   303 	TPtrC attrStr( KNullDesC );
       
   304 	TPtrC iconFilePath;
       
   305 			
       
   306 	TLiwVariant variantIconFile;
       
   307 	TLiwVariant variantIconId;
       
   308 	TLiwVariant variantMaskId;
       
   309 	variantIconFile.PushL();
       
   310 	variantIconId.PushL();
       
   311 	variantMaskId.PushL();
       
   312 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   313 	attrName.Copy( KMenuAttrIconFile );
       
   314 	    
       
   315 	if( aMap->FindL( attrName, variantIconFile))
       
   316 		{
       
   317 		variantIconFile.Get( iconFilePath );
       
   318 		attrName.Copy( KMenuAttrIconId );
       
   319 		if( aMap->FindL( attrName, variantIconId) )
       
   320 			{
       
   321 			variantIconId.Get( attrStr );
       
   322 			User::LeaveIfError( MenuUtils::GetTUint( attrStr, iconId ) );
       
   323 			attrName.Copy( KMenuAttrMaskId );
       
   324 			if( aMap->FindL( attrName, variantMaskId) )
       
   325 				{
       
   326 				variantMaskId.Get( attrStr );
       
   327 				User::LeaveIfError( MenuUtils::GetTUint( attrStr, maskId ) );
       
   328 				CFbsBitmap* bitmap( 0 );
       
   329 				CFbsBitmap* mask( 0 );
       
   330 				RBuf pathBuf;
       
   331 				pathBuf.CleanupClosePushL();
       
   332 				ParseIconFilePathL(iconFilePath, pathBuf);
       
   333 				AknIconUtils::CreateIconLC( bitmap, mask, 
       
   334 						pathBuf, iconId, maskId ); 
       
   335 				icon = CAknIcon::NewL();
       
   336 				icon->SetBitmap( bitmap );
       
   337 				icon->SetMask( mask );
       
   338 				CleanupStack::Pop( 2 );
       
   339 				CleanupStack::PopAndDestroy( &pathBuf );			
       
   340 				}
       
   341 			}
       
   342 		}
       
   343     CleanupStack::PopAndDestroy( &variantMaskId );
       
   344     CleanupStack::PopAndDestroy( &variantIconId );
       
   345     CleanupStack::PopAndDestroy( &variantIconFile );
       
   346 	return icon;
       
   347 	}
       
   348 
       
   349 
       
   350 
       
   351 
       
   352 // ---------------------------------------------------------
       
   353 // GetDefaultSkinL
       
   354 // ---------------------------------------------------------
       
   355 //
       
   356 LOCAL_C CAknIcon* GetDefaultSkinL( const CLiwDefaultMap* aMap )
       
   357 	{
       
   358 	CAknIcon* icon = NULL;
       
   359 	TPtrC attrValueUid(KNullDesC);
       
   360 	TPtrC attrValueNative(KNullDesC);
       
   361 	TPtrC attrValueChildren(KNullDesC);
       
   362 	
       
   363 	TLiwVariant variantUid;
       
   364 	variantUid.PushL();
       
   365 	TLiwVariant variantNative;
       
   366 	variantNative.PushL();
       
   367 	TLiwVariant variantChildren;
       
   368 	variantChildren.PushL();
       
   369 	TBuf8<KMenuMaxAttrNameLen> attrName;
       
   370 	
       
   371 	//APPLICATION
       
   372 	attrName.Copy( KMenuAttrUid );
       
   373 	if( aMap->FindL( attrName, variantUid))
       
   374 		{
       
   375 		variantUid.Get( attrValueUid );
       
   376 	      
       
   377 		if( attrValueUid == KMenuSatUiUid )
       
   378 			{
       
   379 			CMcsSatHandler* satHandler = CMcsSatHandler::NewL();
       
   380 			CleanupStack::PushL( satHandler );
       
   381 			icon = satHandler->LoadIconL();
       
   382 			CleanupStack::PopAndDestroy( satHandler );
       
   383 			}
       
   384 		if( !icon )
       
   385 			{
       
   386 			attrName.Copy( KMenuAttrNative );
       
   387 			if( aMap->FindL( attrName, variantNative) )
       
   388 				{
       
   389 				variantNative.Get( attrValueNative );
       
   390 				if( attrValueNative == KMenuFalse )
       
   391 					{
       
   392 					CFbsBitmap* bitmap( 0 );
       
   393 					CFbsBitmap* mask( 0 );
       
   394 					//for native
       
   395 					if( CreateMbmIconLC2( bitmap, mask, attrValueUid ) )
       
   396 						{
       
   397 						icon = CAknIcon::NewL();
       
   398 						icon->SetBitmap( bitmap );
       
   399 						icon->SetMask( mask );
       
   400 						CleanupStack::Pop( 2 );
       
   401 						}
       
   402 					}
       
   403 				}
       
   404 			if( !icon )
       
   405 				 {
       
   406 				 TUint appUid;
       
   407 				 TUid uid;
       
   408 				 TInt err = MenuUtils::GetTUint( attrValueUid, appUid );
       
   409 				 if ( !err )
       
   410 					 {
       
   411 					 uid = TUid::Uid( appUid );
       
   412 					 icon = LoadAppSkinL( uid );
       
   413 					 }
       
   414 				 }
       
   415 			if( !icon )
       
   416 				{
       
   417 				icon = LoadSkinL(
       
   418 						KAknsIIDQgnMenuUnknownLst.iMajor,
       
   419 						KAknsIIDQgnMenuUnknownLst.iMinor );
       
   420 				}
       
   421 			}
       
   422 		}
       
   423     
       
   424     //FOLDER
       
   425     TUint childrenCount;
       
   426    
       
   427 	attrName.Copy( KChildrenCount );
       
   428 	if( aMap->FindL( attrName, variantChildren))
       
   429 		{
       
   430 		variantChildren.Get( attrValueChildren );
       
   431 		TInt err = MenuUtils::GetTUint( attrValueChildren, childrenCount );
       
   432 		if( childrenCount > 0 )
       
   433 		    {
       
   434 		    icon = LoadSkinL(
       
   435 		        KAknsIIDQgnPropFolderAppsMedium.iMajor,
       
   436 		        KAknsIIDQgnPropFolderAppsMedium.iMinor ); 
       
   437 		    }
       
   438 		else
       
   439 		    {
       
   440             icon = LoadSkinL(
       
   441                 KAknsIIDQgnMenuFolderEmpty.iMajor,
       
   442                 KAknsIIDQgnMenuFolderEmpty.iMinor );
       
   443             }
       
   444         }
       
   445 	CleanupStack::PopAndDestroy( &variantChildren );
       
   446 	CleanupStack::PopAndDestroy( &variantNative );
       
   447 	CleanupStack::PopAndDestroy( &variantUid );
       
   448     return icon;
       
   449 	}
       
   450 
       
   451 // ================= MEMBER FUNCTIONS =======================
       
   452 
       
   453 
       
   454 // ---------------------------------------------------------
       
   455 // MenuIconUtility::GetItemIconL
       
   456 // ---------------------------------------------------------
       
   457 //
       
   458 CAknIcon* McsIconUtility::GetItemIconL( const CLiwDefaultMap* aMap )
       
   459 	{
       
   460 	CAknIcon* icon = NULL;
       
   461 	
       
   462 	// Try to get the skin of the item
       
   463 	TRAP_IGNORE(icon = GetSkinL( aMap ));
       
   464 	if ( icon )
       
   465 		{
       
   466 		return icon;
       
   467 		}
       
   468 
       
   469 	// Try to get the icon of the item (skin failed)
       
   470 	TRAP_IGNORE(icon = GetIconL( aMap ));
       
   471 	if ( icon )
       
   472 		{
       
   473 		return icon;
       
   474 		}
       
   475 
       
   476 	// Return a default (skin and icon failed)
       
   477 	icon = GetDefaultSkinL( aMap );
       
   478 	return icon;
       
   479 	}
       
   480