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