idlehomescreen/widgetmanager/src/wmimageconverter.cpp
changeset 1 5315654608de
child 2 08c6ee43b396
equal deleted inserted replaced
0:f72a12da539e 1:5315654608de
       
     1 /*
       
     2 * Copyright (c) 2009 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 * CWmImageConverter implementation
       
    16 *
       
    17 */
       
    18 
       
    19 #include <fbs.h>
       
    20 #include <eikdef.h>
       
    21 #include <eikenv.h>
       
    22 #include <bautils.h>
       
    23 #include <imageconversion.h>
       
    24 #include <bitmaptransforms.h>
       
    25 #include <AknIconUtils.h>
       
    26 #include <AknsSkinInstance.h>
       
    27 #include <AknsUtils.h>
       
    28 #include <avkon.mbg>
       
    29 #include <apgcli.h>
       
    30 #include <SVGEngineInterfaceImpl.h>
       
    31 #include <widgetregistryconstants.h> // KWidgetUidLowerBound, KWidgetUidUpperBound
       
    32 
       
    33 #include "wmimageconverter.h"
       
    34 
       
    35 _LIT( KSkin, 		  "skin" );
       
    36 _LIT( KMif, 		  "mif" );
       
    37 _LIT( KUid,           "uid" );
       
    38 _LIT( KColon,		  ":" );
       
    39 _LIT( KSvgExt,        ".svg" );
       
    40 _LIT( KHexPrefix,     "0x" );
       
    41 const TUint KLeftParenthesis = '(';
       
    42 
       
    43 // ---------------------------------------------------------
       
    44 // CWmImageConverter::NewL
       
    45 // ---------------------------------------------------------
       
    46 //
       
    47 CWmImageConverter* CWmImageConverter::NewL( MConverterObserver* aObserver )
       
    48     {
       
    49     CWmImageConverter* self = 
       
    50         new(ELeave) CWmImageConverter();
       
    51     CleanupStack::PushL( self );    
       
    52     self->ConstructL( aObserver );
       
    53     CleanupStack::Pop(self);
       
    54     return self; 
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------
       
    58 // CWmImageConverter::CWmImageConverter 
       
    59 // ---------------------------------------------------------
       
    60 //
       
    61 CWmImageConverter::CWmImageConverter() 
       
    62     : CActive( EPriorityStandard )
       
    63     {
       
    64     iState = EIdle;
       
    65     iBitmap = NULL;
       
    66     iMask = NULL;
       
    67     iObserver = NULL;
       
    68     iConversionMethod = EUnrecognized;
       
    69     CActiveScheduler::Add( this );
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------
       
    73 // CWmImageConverter::ConstructL
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 void CWmImageConverter::ConstructL( MConverterObserver* aObserver )
       
    77     {
       
    78     User::LeaveIfError( iFs.Connect() );
       
    79     iFs.ShareProtected();
       
    80     iScaler = CBitmapScaler::NewL();
       
    81     iObserver = aObserver;
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------
       
    85 // CWmImageConverter::~CWmImageConverter
       
    86 // ---------------------------------------------------------
       
    87 //
       
    88 CWmImageConverter::~CWmImageConverter()
       
    89     {
       
    90     Cancel();
       
    91     delete iImageDecoder;
       
    92     iFs.Close(); 
       
    93     if (iBitmap) 
       
    94         {
       
    95         delete iBitmap;
       
    96         iBitmap = NULL;
       
    97         }    
       
    98     if (iMask) 
       
    99         {
       
   100         delete iMask; 
       
   101         iMask = NULL;
       
   102         }    
       
   103     delete iScaler;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // CWmImageConverter::HandleIconStringL
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 void CWmImageConverter::HandleIconStringL( 
       
   111                             TInt aWidth, TInt aHeight, 
       
   112                             const TDesC& aIconStr )
       
   113     {
       
   114     if ( aIconStr.Length() )
       
   115         {
       
   116         TAknsItemID skinItemId;
       
   117         skinItemId.iMajor = 0;
       
   118         skinItemId.iMinor = 0;
       
   119         TInt bitmapId( KErrNotFound );
       
   120         TInt maskId( KErrNotFound );
       
   121         TUid appUid;
       
   122         iFilename = KNullDesC;
       
   123         iScaleNeeded = EFalse;    
       
   124         iSize.SetSize( aWidth, aHeight );
       
   125         
       
   126         if ( ResolveSkinIdAndMifId( 
       
   127                 aIconStr, skinItemId, bitmapId, maskId, iFilename ) )
       
   128             {
       
   129             if ( bitmapId >= 0 && skinItemId.iMajor > 0 )
       
   130                 iConversionMethod = ESkinAndMifIcon;
       
   131             else if ( bitmapId >= 0 )
       
   132                 iConversionMethod = EMifIcon;
       
   133             else
       
   134                 iConversionMethod = ESkinIcon;
       
   135             CreateSkinOrMifIconL( 
       
   136                     skinItemId, bitmapId, maskId, iFilename );
       
   137             }
       
   138         else if ( ResolveUid( aIconStr, appUid ) )
       
   139             {
       
   140             iConversionMethod = EUidIcon;
       
   141             CreateIconFromUidL( appUid );
       
   142             }
       
   143         else if ( EndsWith( aIconStr, KSvgExt ) )
       
   144             {
       
   145             // filename_with_full_path.svg
       
   146             iConversionMethod = ESvgIcon;
       
   147             CreateIconFromSvgL( aIconStr );
       
   148             }
       
   149         else if ( BaflUtils::FileExists( iFs, aIconStr ) )
       
   150             {
       
   151             // filename_with_full_path.png/jpg
       
   152             iConversionMethod = EImageIcon;
       
   153             CreateIconFromOtherL( aIconStr );
       
   154             }
       
   155         else
       
   156             {
       
   157             iConversionMethod = EUnrecognized;
       
   158             User::Leave( KErrArgument );
       
   159             }
       
   160         }
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CWmImageConverter::CreateIconFromUidL
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 void CWmImageConverter::CreateIconFromUidL( const TUid& aUid )
       
   168     {
       
   169     CFbsBitmap* bitmap = NULL;
       
   170     CFbsBitmap* mask = NULL;
       
   171     
       
   172     if ( aUid.iUid >= KWidgetUidLowerBound &&
       
   173        aUid.iUid < KWidgetUidUpperBound  )
       
   174         {
       
   175         // AknsUtils::CreateAppIconLC panics incase of WRT
       
   176 
       
   177         RApaLsSession lsSession;
       
   178         User::LeaveIfError( lsSession.Connect() );
       
   179         CleanupClosePushL( lsSession );
       
   180     
       
   181         CArrayFixFlat<TSize>* sizeArray = new (ELeave) CArrayFixFlat<TSize>(3);
       
   182         CleanupStack::PushL( sizeArray );
       
   183         User::LeaveIfError( lsSession.GetAppIconSizes( aUid, *sizeArray ) );
       
   184         TInt sizeCount = sizeArray->Count();
       
   185         TSize size;
       
   186         if ( sizeCount > 0 )
       
   187             {
       
   188             for( TInt i=0; i < sizeArray->Count(); i++ )
       
   189                 {
       
   190                 size = (*sizeArray)[i];
       
   191                 if ( size == iSize )
       
   192                     {
       
   193                     break;
       
   194                     }
       
   195                 }
       
   196             }      
       
   197         CApaMaskedBitmap* maskedBmp = CApaMaskedBitmap::NewLC();
       
   198         User::LeaveIfError( lsSession.GetAppIcon( aUid, size, *maskedBmp ) );
       
   199         iBitmap = static_cast<CFbsBitmap*>( maskedBmp );  // ownership transfered
       
   200         
       
   201         iMask = new ( ELeave ) CFbsBitmap;
       
   202         User::LeaveIfError( iMask->Create( 
       
   203                                     maskedBmp->Mask()->SizeInPixels(), 
       
   204                                     maskedBmp->Mask()->DisplayMode() ) );
       
   205         // duplicate mask, origional is owned by maskedBmp
       
   206         iMask->Duplicate( maskedBmp->Mask()->Handle() );
       
   207         CleanupStack::Pop( maskedBmp );
       
   208         maskedBmp = NULL;
       
   209         CleanupStack::PopAndDestroy(sizeArray); 
       
   210         CleanupStack::PopAndDestroy( &lsSession );
       
   211 
       
   212         // scale or notify
       
   213         if ( size == iSize )
       
   214             {
       
   215             iObserver->NotifyCompletion( KErrNone );
       
   216             }
       
   217         else
       
   218             {
       
   219             iScaleNeeded = ETrue;
       
   220             ScaleBitmap( iSize.iWidth, iSize.iHeight );
       
   221             }
       
   222         }
       
   223     else if ( aUid.iUid != KNullUid.iUid )
       
   224         {
       
   225         MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   226         TInt err( KErrNone );
       
   227         TRAP( err,
       
   228                 {
       
   229                 AknsUtils::CreateAppIconLC( skin, aUid,
       
   230                         EAknsAppIconTypeContext, bitmap, mask );
       
   231                 CleanupStack::Pop( 2 ); // for trap
       
   232                 }
       
   233             );
       
   234         
       
   235         if( KErrNone != err )
       
   236             {
       
   237             // If icon is not created, try to create default application icon
       
   238             AknsUtils::CreateIconLC( skin,
       
   239                 KAknsIIDQgnMenuUnknownLst, bitmap, mask,
       
   240                 AknIconUtils::AvkonIconFileName(),
       
   241                 EMbmAvkonQgn_menu_unknown_lst,
       
   242                 EMbmAvkonQgn_menu_unknown_lst_mask );
       
   243             CleanupStack::Pop( 2 ); // for trap
       
   244             }
       
   245         
       
   246         iBitmap = bitmap;
       
   247         iMask = mask;
       
   248                 
       
   249         err = AknIconUtils::SetSize( iBitmap , iSize, EAspectRatioNotPreserved );
       
   250         if ( KErrNone == err )
       
   251             {
       
   252             err = AknIconUtils::SetSize( iMask , iSize, EAspectRatioNotPreserved );
       
   253             }
       
   254 
       
   255         // notify observer
       
   256         iObserver->NotifyCompletion( err );
       
   257         }
       
   258     else
       
   259         {
       
   260         User::Leave( KErrArgument );
       
   261         }
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------
       
   265 // CWmImageConverter::CreateIconFromSvgL
       
   266 // ---------------------------------------------------------
       
   267 //
       
   268 void CWmImageConverter::CreateIconFromSvgL( const TDesC& aFileName )
       
   269     {
       
   270     if ( iBitmap )
       
   271         {
       
   272         delete iBitmap;
       
   273         iBitmap = NULL;
       
   274         }
       
   275     if ( iMask )
       
   276         {
       
   277         delete iMask;
       
   278         iMask = NULL;
       
   279         }
       
   280 
       
   281     TDisplayMode mode = CEikonEnv::Static()->ScreenDevice()->DisplayMode();
       
   282     TFontSpec fontspec;
       
   283     
       
   284     CFbsBitmap* frameBuffer = new ( ELeave ) CFbsBitmap;
       
   285     CleanupStack::PushL( frameBuffer );
       
   286     frameBuffer->Create( iSize, mode );
       
   287     
       
   288     CSvgEngineInterfaceImpl* svgEngine = CSvgEngineInterfaceImpl::NewL( 
       
   289             frameBuffer, NULL, fontspec );
       
   290     svgEngine->SetDRMMode( EFalse );  
       
   291 
       
   292     TInt domHandle;
       
   293     CheckSvgErrorL( svgEngine->PrepareDom( aFileName, domHandle ) );
       
   294     
       
   295     CFbsBitmap* bitmap = new(ELeave) CFbsBitmap;    
       
   296     User::LeaveIfError( bitmap->Create( iSize, mode) );
       
   297     CleanupStack::PushL( bitmap );
       
   298     
       
   299     CFbsBitmap* mask = new(ELeave) CFbsBitmap;    
       
   300     User::LeaveIfError( mask->Create( iSize, EGray256 ) );
       
   301     CleanupStack::PushL( mask );
       
   302     
       
   303     CheckSvgErrorL( svgEngine->UseDom( domHandle, bitmap, mask ) );
       
   304 
       
   305     MSvgError* err;
       
   306     svgEngine->Start( err );
       
   307     CheckSvgErrorL( err );
       
   308     
       
   309     CheckSvgErrorL( svgEngine->UseDom( domHandle, NULL, NULL ) );
       
   310     CheckSvgErrorL( svgEngine->DeleteDom( domHandle ) );
       
   311 
       
   312     CleanupStack::Pop( mask );
       
   313     CleanupStack::Pop( bitmap );
       
   314     CleanupStack::PopAndDestroy( frameBuffer );
       
   315     
       
   316     iBitmap = bitmap;
       
   317     iMask = mask;
       
   318     iObserver->NotifyCompletion( KErrNone );
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------
       
   322 // CWmImageConverter::CheckSvgErrorL
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 void CWmImageConverter::CheckSvgErrorL( MSvgError* aError )
       
   326     {
       
   327     if ( aError )
       
   328         {
       
   329         User::LeaveIfError( aError->SystemErrorCode() );
       
   330         }
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------
       
   334 // CWmImageConverter::CreateIconFromOtherL
       
   335 // ---------------------------------------------------------
       
   336 //
       
   337 void CWmImageConverter::CreateIconFromOtherL( const TDesC& aFileName )
       
   338     {
       
   339     if ( IsActive() || iState != EIdle )
       
   340         {
       
   341         User::Leave( KErrNotReady );
       
   342         }
       
   343     
       
   344     if ( iImageDecoder ) delete iImageDecoder; iImageDecoder = NULL;
       
   345     if (iBitmap) {delete iBitmap; iBitmap = NULL;}
       
   346     if (iMask) {delete iMask; iMask = NULL;}
       
   347     
       
   348     iFilename.Copy( aFileName );
       
   349     
       
   350     // create the decoder
       
   351     iImageDecoder = CImageDecoder::FileNewL( iFs, iFilename );
       
   352     
       
   353     TSize size = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   354 
       
   355     // create the destination bitmap
       
   356     iBitmap = new (ELeave) CFbsBitmap();
       
   357     iBitmap->Create( size,
       
   358                      iImageDecoder->FrameInfo().iFrameDisplayMode ); 
       
   359     
       
   360     iMask = new (ELeave) CFbsBitmap();
       
   361     iMask->Create( size, EGray256 ); 
       
   362     
       
   363     if ( size != iSize )
       
   364         {
       
   365         iScaleNeeded = ETrue;
       
   366         }
       
   367     // start conversion to bitmap
       
   368     iState = EDecoding;
       
   369     iImageDecoder->Convert( &iStatus, *iBitmap, *iMask );
       
   370     SetActive();
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------
       
   374 // CWmImageConverter::DoCancel
       
   375 // ---------------------------------------------------------
       
   376 //
       
   377 void CWmImageConverter::DoCancel()
       
   378     {
       
   379     if( iState == EDecoding )
       
   380         {
       
   381         iImageDecoder->Cancel();        
       
   382         if ( iObserver )
       
   383             {
       
   384             iObserver->NotifyCompletion( KErrCancel );
       
   385             }
       
   386         }    
       
   387     else if( iState == EScalingBitmap ||
       
   388         iState == EScalingMask )
       
   389         {
       
   390         iScaler->Cancel();
       
   391         if ( iObserver )
       
   392             {
       
   393             iObserver->NotifyCompletion( KErrCancel );
       
   394             }
       
   395         }    
       
   396     else
       
   397         {
       
   398         // State is EIdle, do nothing
       
   399         }
       
   400     iState = EIdle;
       
   401     iScaleNeeded = EFalse;
       
   402     }
       
   403 
       
   404 // ---------------------------------------------------------
       
   405 // CWmImageConverter::RunL
       
   406 // ---------------------------------------------------------
       
   407 //
       
   408 void CWmImageConverter::RunL()
       
   409     {
       
   410     switch( iState ) 
       
   411         {
       
   412         case EDecoding:
       
   413             {
       
   414             if( iStatus.Int() == KErrNone )
       
   415                 {
       
   416                 if ( iScaleNeeded )
       
   417                     {
       
   418                     ScaleBitmap( iSize.iWidth, iSize.iHeight );
       
   419                     }
       
   420                 else
       
   421                     {
       
   422                     iState = EIdle;
       
   423                     if ( iObserver )
       
   424                         {
       
   425                         iObserver->NotifyCompletion( KErrNone );
       
   426                         }
       
   427                     }
       
   428                 break;
       
   429                 }
       
   430             else if( iStatus.Int() == KErrUnderflow ) 
       
   431                 {
       
   432                 iImageDecoder->ContinueConvert( &iStatus );
       
   433                 SetActive();
       
   434                 break;
       
   435                 }
       
   436             else if ( iStatus.Int() == KErrCorrupt )
       
   437                 {
       
   438                 iState = EIdle;
       
   439                 iScaleNeeded = EFalse;
       
   440                 if ( iBitmap )
       
   441                     {
       
   442                     delete iBitmap;
       
   443                     iBitmap = NULL;
       
   444                     }
       
   445                 if ( iMask )
       
   446                     {
       
   447                     delete iMask;
       
   448                     iMask = NULL;
       
   449                     }
       
   450                 if ( iObserver )
       
   451                     {
       
   452                     iObserver->NotifyCompletion( KErrCorrupt );
       
   453                     }
       
   454                 break;
       
   455                 }
       
   456             else
       
   457                 {
       
   458                 // Unknown error
       
   459                 iState = EIdle;
       
   460                 iScaleNeeded = EFalse;
       
   461                 if ( iBitmap )
       
   462                     {
       
   463                     delete iBitmap;
       
   464                     iBitmap = NULL;
       
   465                     }
       
   466                 if ( iMask )
       
   467                     {
       
   468                     delete iMask;
       
   469                     iMask = NULL;
       
   470                     }
       
   471                 if ( iObserver )
       
   472                     {
       
   473                     iObserver->NotifyCompletion( iStatus.Int() );
       
   474                     }
       
   475                 break;
       
   476                 }
       
   477             }
       
   478         case EScalingBitmap:
       
   479             {
       
   480             if( iStatus.Int() == KErrNone && iMask )
       
   481                 {
       
   482                 ScaleMask( iSize.iWidth, iSize.iHeight );
       
   483                 }
       
   484             else
       
   485                 {
       
   486                 iState = EIdle;
       
   487                 iScaleNeeded = EFalse;
       
   488                 if ( iObserver )
       
   489                     {
       
   490                     iObserver->NotifyCompletion( iStatus.Int() );
       
   491                     }                
       
   492                 }
       
   493             break;
       
   494             }
       
   495         case EScalingMask:
       
   496             {
       
   497             iState = EIdle;
       
   498             iScaleNeeded = EFalse;
       
   499             if ( iObserver )
       
   500                 {
       
   501                 iObserver->NotifyCompletion( iStatus.Int() );
       
   502                 }
       
   503             break;
       
   504             }
       
   505         default:
       
   506             break;
       
   507         }   
       
   508     }
       
   509 
       
   510 // ---------------------------------------------------------
       
   511 // CWmImageConverter::RunError
       
   512 // ---------------------------------------------------------
       
   513 //
       
   514 TInt CWmImageConverter::RunError(TInt /*aError*/)
       
   515     {
       
   516     // Our RunL does not contain any method calls that would leave, so this method
       
   517     // should never be called.
       
   518     iScaleNeeded = EFalse;
       
   519     return KErrNone;
       
   520     }
       
   521 
       
   522 // ---------------------------------------------------------
       
   523 // CWmImageConverter::ScaleBitmap
       
   524 // ---------------------------------------------------------
       
   525 //
       
   526 void CWmImageConverter::ScaleBitmap( TInt aWidth, TInt aHeight )
       
   527     {
       
   528     if ( !IsActive() && 
       
   529          iBitmap &&
       
   530         ( iState == EDecoding || iState == EIdle ) )
       
   531         {
       
   532         // the maintain aspect ratio is by default set to true
       
   533         iScaler->Scale( &iStatus, *iBitmap, TSize( aWidth,aHeight ), EFalse );
       
   534         iState = EScalingBitmap;
       
   535         SetActive();
       
   536         }
       
   537     }
       
   538 
       
   539 // ---------------------------------------------------------
       
   540 // CWmImageConverter::ScaleMask
       
   541 // ---------------------------------------------------------
       
   542 //
       
   543 void CWmImageConverter::ScaleMask( TInt aWidth, TInt aHeight )
       
   544     {
       
   545     if ( !IsActive() && 
       
   546         iState == EScalingBitmap &&
       
   547         iMask )
       
   548         {
       
   549         // the maintain aspect ratio is by default set to true
       
   550         iScaler->Scale( &iStatus, *iMask, TSize(aWidth,aHeight), EFalse );
       
   551         iState = EScalingMask;
       
   552         SetActive();
       
   553         }
       
   554     }
       
   555 
       
   556 // ---------------------------------------------------------
       
   557 // CWmImageConverter::Bitmap
       
   558 // ---------------------------------------------------------
       
   559 //
       
   560 CFbsBitmap* CWmImageConverter::Bitmap()
       
   561     {
       
   562     CFbsBitmap* bitmap = NULL;
       
   563     if (iState == EIdle &&
       
   564         iBitmap )
       
   565         {
       
   566         bitmap = iBitmap;  // ownership taken
       
   567         iBitmap = NULL;
       
   568         }
       
   569     return bitmap;
       
   570     }
       
   571 
       
   572 // ---------------------------------------------------------
       
   573 // CWmImageConverter::Mask
       
   574 // ---------------------------------------------------------
       
   575 //
       
   576 CFbsBitmap* CWmImageConverter::Mask()
       
   577     {
       
   578     CFbsBitmap* mask = NULL;
       
   579     if (iState == EIdle &&
       
   580         iMask )
       
   581         {
       
   582         mask = iMask;  // ownership taken
       
   583         iMask = NULL;
       
   584         }
       
   585     return mask;
       
   586     }
       
   587 
       
   588 // ---------------------------------------------------------
       
   589 // CWmImageConverter::EndsWith
       
   590 // ---------------------------------------------------------
       
   591 //
       
   592 TBool CWmImageConverter::EndsWith( const TDesC& aString, 
       
   593                                 const TDesC& aPattern )
       
   594     {
       
   595     return ( aString.Right( aPattern.Length() ) == aPattern );
       
   596     }
       
   597 
       
   598 // ---------------------------------------------------------------------------
       
   599 // CWmImageConverter::ResolveUid
       
   600 // ---------------------------------------------------------------------------
       
   601 //
       
   602 TBool CWmImageConverter::ResolveUid( 
       
   603                 const TDesC& aPath, TUid& aUid )
       
   604     {
       
   605     // Syntax: uid(0x12345678)
       
   606     TInt error = KErrNotFound;
       
   607     TInt pos = aPath.FindF( KUid );
       
   608     if( pos == 0 )
       
   609         {
       
   610         // Skip skin token
       
   611         pos += KUid().Length();
       
   612 
       
   613         // Initialize lexer
       
   614         TLex lex( aPath.Mid( pos ) );
       
   615 
       
   616         // Check left parenthesis
       
   617         if ( lex.Get() == KLeftParenthesis )
       
   618             {
       
   619             lex.SkipSpaceAndMark();
       
   620             lex.SkipCharacters();
       
   621             
       
   622             TPtrC mtoken = lex.MarkedToken();
       
   623             pos = mtoken.FindF( KHexPrefix );
       
   624             if ( pos == 0 )
       
   625                 {
       
   626                 TLex lex( mtoken.Mid( KHexPrefix().Length() ) );
       
   627                 TUint id = 0;
       
   628                 error = lex.Val( id, EHex );
       
   629                 aUid = TUid::Uid( (TInt)id );
       
   630                 }
       
   631             else
       
   632                 {
       
   633                 TInt id( 0 );
       
   634                 error = lex.Val( id );
       
   635                 aUid.iUid = id;
       
   636                 }
       
   637             }
       
   638         }
       
   639     
       
   640     return (error == KErrNone );
       
   641     }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // CWmImageConverter::ResolveSkinId
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 TBool CWmImageConverter::ResolveSkinId( 
       
   648                 const TDesC& aPath, TAknsItemID& aItemId )
       
   649     {
       
   650     // Syntax: skin(major minor)
       
   651     TInt error = KErrNotFound;
       
   652     TInt pos = aPath.FindF( KSkin );
       
   653     if( pos == 0 )
       
   654         {
       
   655         // Skip skin token
       
   656         pos += KSkin().Length();
       
   657 
       
   658         // Initialize lexer
       
   659         TLex lex( aPath.Mid( pos ) );
       
   660         
       
   661         lex.SkipSpaceAndMark();
       
   662         // Check left parenthesis
       
   663         if ( lex.Get() == KLeftParenthesis )
       
   664            {
       
   665            pos++;
       
   666            TLex lex( aPath.Mid( pos ) );
       
   667            lex.SkipSpaceAndMark();
       
   668            
       
   669            TPtrC mtoken = lex.MarkedToken();
       
   670            pos = mtoken.FindF( KHexPrefix );
       
   671            if ( pos == 0 )
       
   672               {
       
   673               TUint majorId( 0 );
       
   674               TUint minorId( 0 );              
       
   675               lex.Assign( mtoken.Mid( KHexPrefix().Length() ) );
       
   676               error = lex.Val( majorId, EHex );
       
   677               lex.SkipSpace();
       
   678               lex.SkipAndMark( KHexPrefix().Length() );
       
   679               error |= lex.Val( minorId, EHex );
       
   680               aItemId.Set( majorId, minorId );
       
   681               }
       
   682           else
       
   683               {
       
   684               TInt majorId(0);
       
   685               TInt minorId(0);
       
   686               error = lex.Val( majorId );
       
   687               lex.SkipSpace();
       
   688               error |= lex.Val( minorId );                      
       
   689               aItemId.Set( majorId, minorId );
       
   690               }
       
   691            }        
       
   692         }
       
   693 
       
   694     return (error == KErrNone );
       
   695     }
       
   696 
       
   697 // ---------------------------------------------------------------------------
       
   698 // CWmImageConverter::ResolveMifId
       
   699 // ---------------------------------------------------------------------------
       
   700 //
       
   701 TBool CWmImageConverter::ResolveMifId( 
       
   702                 const TDesC& aPath, TInt& aBitmapId,
       
   703                 TInt& aMaskId, TDes& aFileName )
       
   704     {
       
   705     // Syntax: mif(filename bimapId maskId)
       
   706    TInt error = KErrNotFound;
       
   707    TInt pos = aPath.FindF( KMif );
       
   708    if( pos == 0 )
       
   709         {
       
   710         // Skip mif token
       
   711         pos += KMif().Length();
       
   712         // Initialize lexer
       
   713         TLex lex( aPath.Mid( pos ) );
       
   714 
       
   715         // Check left parenthesis
       
   716         if ( lex.Get() == KLeftParenthesis )
       
   717            {
       
   718            lex.SkipSpaceAndMark();
       
   719            lex.SkipCharacters();
       
   720            // Resolve MifFile name
       
   721            aFileName.Copy(lex.MarkedToken());
       
   722            if( aFileName.Length()!= 0)
       
   723                {
       
   724                // Resolve bitmap id  
       
   725                lex.SkipSpace();
       
   726                error = lex.Val( aBitmapId );
       
   727                
       
   728                // Resolve mask id
       
   729                // dont return error if it is not found, that is ok
       
   730                lex.SkipSpace();
       
   731                lex.Val( aMaskId );
       
   732                }
       
   733            else
       
   734                {
       
   735                error = KErrNotFound;
       
   736                }
       
   737            }        
       
   738         }    
       
   739     return (error == KErrNone );
       
   740     }
       
   741 
       
   742 // ---------------------------------------------------------------------------
       
   743 // CWmImageConverter::ResolveSkinIdAndMifId
       
   744 // ---------------------------------------------------------------------------
       
   745 //
       
   746 TBool CWmImageConverter::ResolveSkinIdAndMifId( 
       
   747                 const TDesC& aPath, TAknsItemID& aItemId,
       
   748                 TInt& aBitmapId, TInt& aMaskId, TDes& aFileName )
       
   749    {
       
   750    // Syntax: skin(major minor):mif(filename bimapId maskId) 
       
   751    TBool result = ResolveSkinId( aPath, aItemId );
       
   752    if ( result )
       
   753 	   {
       
   754 	   TInt pos = aPath.FindF( KColon );
       
   755 	   if ( pos != KErrNotFound )
       
   756 	       {
       
   757 	       TPtrC ptr = aPath.Mid( pos+1 );
       
   758 	       result = ResolveMifId( ptr, aBitmapId, aMaskId, aFileName );
       
   759 	       }
       
   760 	   }
       
   761    else
       
   762        {
       
   763        result = ResolveMifId( aPath, aBitmapId, aMaskId, aFileName );
       
   764        }
       
   765    return result;
       
   766    }
       
   767 
       
   768 // ---------------------------------------------------------------------------
       
   769 // CWmImageConverter::CreateSkinOrMifIconL
       
   770 // ---------------------------------------------------------------------------
       
   771 //
       
   772 void CWmImageConverter::CreateSkinOrMifIconL( 
       
   773                 const TAknsItemID& aItemId, TInt aBitmapId, 
       
   774                 TInt aMaskId, const TDesC& aFileName )
       
   775     {
       
   776     iFilename = aFileName;
       
   777     CFbsBitmap* bitmap = NULL;
       
   778     CFbsBitmap* mask = NULL;
       
   779     
       
   780     // Load from skin 
       
   781     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   782     if ( skin && aItemId.iMajor != 0 && aItemId.iMinor != 0 )
       
   783         {
       
   784         TInt err( KErrNone );
       
   785         CAknsMaskedBitmapItemData* itemData = NULL;
       
   786         TRAP( err, itemData = 
       
   787                     static_cast<CAknsMaskedBitmapItemData*>(
       
   788                     skin->CreateUncachedItemDataL( aItemId, EAknsITMaskedBitmap ) ); );
       
   789         if( itemData && KErrNone == err )
       
   790             {
       
   791             CleanupStack::PushL( itemData );
       
   792             // Detach bitmaps
       
   793             bitmap = itemData->Bitmap();
       
   794             itemData->SetBitmap( NULL );
       
   795             mask = itemData->Mask();
       
   796             itemData->SetMask( NULL );
       
   797             CleanupStack::PopAndDestroy( itemData );
       
   798             }
       
   799         else 
       
   800             {
       
   801             // look in imagetable
       
   802             CAknsImageTableItemData* iconData = NULL;
       
   803             TRAP( err, iconData = static_cast<CAknsImageTableItemData*>(
       
   804                             skin->CreateUncachedItemDataL( aItemId, EAknsITImageTable ) ); );
       
   805             if( iconData && KErrNone == err )
       
   806                 {
       
   807                 CleanupStack::PushL( iconData );
       
   808                 if( iconData->NumberOfImages() )
       
   809                     {                        
       
   810                     TAknsItemID iconIId;
       
   811                     iconIId.Set( iconData->ImageIID(0) );
       
   812                     TRAP( err, AknsUtils::CreateIconL( 
       
   813                             skin, iconIId, bitmap, mask, KNullDesC, -1, -1 ); );
       
   814                     }
       
   815                 CleanupStack::PopAndDestroy( iconData );
       
   816                 }
       
   817             }
       
   818         
       
   819         if ( KErrNone == err && bitmap )
       
   820             {
       
   821             TInt err = AknIconUtils::SetSize( bitmap , iSize, EAspectRatioNotPreserved );
       
   822             if ( KErrNone == err  )
       
   823                 {
       
   824                 if ( mask )
       
   825                     {
       
   826                     err = AknIconUtils::SetSize( mask , iSize, EAspectRatioNotPreserved );
       
   827                     }
       
   828                 iBitmap = bitmap;
       
   829                 iMask = mask;
       
   830                 // notify observer
       
   831                 iObserver->NotifyCompletion( KErrNone );
       
   832                 return;
       
   833                 }
       
   834             else
       
   835                 {
       
   836                 if ( bitmap ) { delete bitmap; bitmap = NULL; }
       
   837                 if ( mask ){ delete mask; mask = NULL; }
       
   838                 }
       
   839             }
       
   840         }
       
   841     
       
   842     if( aBitmapId != KErrNotFound && !bitmap && 
       
   843        aFileName.Length() && BaflUtils::FileExists( iFs, aFileName ) )
       
   844         {
       
   845         if ( aMaskId != KErrNotFound )
       
   846           {
       
   847           // Create icon from Mif filename , bitmap id and mask id          
       
   848           AknIconUtils::CreateIconL(
       
   849                   bitmap, mask, *this, aBitmapId, aMaskId );
       
   850           }
       
   851         else
       
   852           {
       
   853           bitmap = AknIconUtils::CreateIconL( *this, aBitmapId );
       
   854           }
       
   855         }
       
   856     else
       
   857         {
       
   858         User::Leave( KErrArgument );
       
   859         }
       
   860     
       
   861     iBitmap = bitmap;
       
   862     iMask = mask;
       
   863             
       
   864     TInt err = AknIconUtils::SetSize( iBitmap , iSize, EAspectRatioNotPreserved );
       
   865     if ( KErrNone == err )
       
   866         {
       
   867         err = AknIconUtils::SetSize( iMask , iSize, EAspectRatioNotPreserved );
       
   868         }
       
   869 
       
   870     // notify observer
       
   871     iObserver->NotifyCompletion( err );
       
   872     }
       
   873 
       
   874 // ---------------------------------------------------------------------------
       
   875 // CWmImageConverter::SetLogoSize()
       
   876 // ---------------------------------------------------------------------------
       
   877 //
       
   878 void CWmImageConverter::SetLogoSize( const TSize& aSize )
       
   879     {
       
   880     iSize = aSize;
       
   881     }
       
   882 
       
   883 // ---------------------------------------------------------------------------
       
   884 // CWmImageConverter::ConversionMethod()
       
   885 // ---------------------------------------------------------------------------
       
   886 //
       
   887 CWmImageConverter::TConversionMethod CWmImageConverter::ConversionMethod()
       
   888     {
       
   889     return iConversionMethod;
       
   890     }
       
   891 
       
   892 // ---------------------------------------------------------------------------
       
   893 // CWmImageConverter::RetrieveIconFileHandleL
       
   894 // ---------------------------------------------------------------------------
       
   895 //
       
   896 void CWmImageConverter::RetrieveIconFileHandleL( 
       
   897     RFile& aFile, const TIconFileType /*aType*/ )
       
   898     {
       
   899 	User::LeaveIfError( aFile.Open( iFs, iFilename, EFileShareAny ) );
       
   900     }
       
   901 
       
   902 // ---------------------------------------------------------------------------
       
   903 // CWmImageConverter::Finished
       
   904 // ---------------------------------------------------------------------------
       
   905 //
       
   906 void CWmImageConverter::Finished()
       
   907     {
       
   908     // finishes using the icon file. No actions needed here.
       
   909     }
       
   910 
       
   911 
       
   912 // End of file
       
   913