phonebookui/Phonebook2/ccapplication/ccacommlauncherplugin/src/ccappcommlauncherimagedecoding.cpp
branchCompilerCompatibility
changeset 6 c5deda9d60e3
parent 4 f5f19add8e1f
parent 3 04ab22b956c2
child 17 1a455957d2fe
equal deleted inserted replaced
4:f5f19add8e1f 6:c5deda9d60e3
     1 /*
       
     2 * Copyright (c) 2008-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:  Implementation of the utility class for asynchronously decoding the header thumbnail image
       
    15 *
       
    16 */
       
    17 
       
    18 #include <bitmaptransforms.h>
       
    19 #include "ccappcommlauncherimagedecoding.h"
       
    20 
       
    21 const TInt KDelay = 500000; // 0.5s
       
    22 // ---------------------------------------------------------------------------
       
    23 // Two-phase construction
       
    24 // ---------------------------------------------------------------------------
       
    25 //
       
    26 CCCAppCommLauncherImageDecoding* CCCAppCommLauncherImageDecoding::NewL(
       
    27         CCCAppCommLauncherHeaderControl* aHeader, const TDesC8* aBitmapData, const TDesC* aImageFile)
       
    28     {
       
    29     CCCAppCommLauncherImageDecoding* self = new (ELeave) CCCAppCommLauncherImageDecoding(aHeader);
       
    30     CleanupStack::PushL(self);
       
    31     self->ConstructL(aBitmapData, aImageFile);
       
    32     CleanupStack::Pop(self);
       
    33     return self;
       
    34     }
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // First phase (C++) constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CCCAppCommLauncherImageDecoding::CCCAppCommLauncherImageDecoding(CCCAppCommLauncherHeaderControl* aHeader) : 
       
    41 CActive( CActive::EPriorityStandard ), iHeader(aHeader)
       
    42     { 
       
    43     CActiveScheduler::Add(this); 
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // ConstructL, second phase constructor
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 void CCCAppCommLauncherImageDecoding::ConstructL(const TDesC8* aBitmapData, const TDesC* aImageFile)
       
    51     {
       
    52     if ( aImageFile )
       
    53         {
       
    54         iImageFullName = aImageFile->AllocL();
       
    55         }
       
    56     if ( aBitmapData )
       
    57         {
       
    58         iBitmapData = aBitmapData->AllocL();
       
    59         }
       
    60     User::LeaveIfError( iFs.Connect() );
       
    61     iTimer.CreateLocal();
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // Destructor
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CCCAppCommLauncherImageDecoding::~CCCAppCommLauncherImageDecoding()
       
    69     {
       
    70     DoCancel();
       
    71     if (iBitmap)
       
    72         {
       
    73         delete iBitmap;
       
    74         iBitmap = NULL;
       
    75         }
       
    76     if (iBitmapData)
       
    77         {
       
    78         delete iBitmapData;
       
    79         iBitmapData = NULL;
       
    80         }
       
    81     if (iImageFullName)
       
    82         {
       
    83         delete iImageFullName;
       
    84         }
       
    85     iFs.Close();
       
    86     iTimer.Close();
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Starts the decoding process
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CCCAppCommLauncherImageDecoding::StartL( TSize aImageSize )
       
    94     {
       
    95     iDecoderState = ECcaConvertThumbnailImage;
       
    96     iBitmapSize = aImageSize;
       
    97     CreateBitmapL();
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Called by the active object framework when the decoding (request) is
       
   102 // completed.
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CCCAppCommLauncherImageDecoding::RunL() 
       
   106     {
       
   107     User::LeaveIfError( iStatus.Int() );
       
   108     switch ( iDecoderState )
       
   109 	    {
       
   110 	    case ECcaConvertThumbnailImage:
       
   111 	    	{
       
   112 	        iDecoderState = ECcaScaleThumbnail;
       
   113 	        ScaleBitmapL();
       
   114 	        break;
       
   115 	    	}
       
   116 	
       
   117 	    case ECcaScaleThumbnail:
       
   118 	    	{
       
   119 	    	if ( iImageFullName )
       
   120 	    		{
       
   121 	            iDecoderState = ECcaReadImageFromFile;
       
   122 	            SetPriority( EPriorityIdle );
       
   123 	            iTimer.After( iStatus, KDelay ); 
       
   124 	            SetActive();
       
   125 	    		}
       
   126 	    	// don't break here
       
   127 	    	}
       
   128 	    	
       
   129 	    case ECcaScaleImage:
       
   130 	    	{
       
   131 	        // Ownership of the bitmap is transferred
       
   132 	        iHeader->SetBitmap( iBitmap );
       
   133 	        iBitmap = NULL;
       
   134 	        delete iBitmapScaler;
       
   135 	        iBitmapScaler = NULL;
       
   136 	        delete iImgDecoder;
       
   137 	        iImgDecoder = NULL;
       
   138 	        break;
       
   139 	    	}
       
   140 	        
       
   141 	    case ECcaReadImageFromFile:
       
   142 	    	{
       
   143 	    	if ( iImageFullName )
       
   144 	    		{
       
   145 	            iDecoderState = ECcaConvertImageFromFile;
       
   146 	            CreateBitmapL();
       
   147 	    		}
       
   148 	    	break;
       
   149 	    	}
       
   150 	         
       
   151 	    case ECcaConvertImageFromFile:
       
   152 	    	{
       
   153 	        iDecoderState = ECcaScaleImage;
       
   154 	        SetPriority( EPriorityStandard );
       
   155 	        ScaleBitmapL();
       
   156 	        break;
       
   157 	    	}
       
   158 	        
       
   159 	    default:
       
   160 	        break;
       
   161 	    }
       
   162     }
       
   163 // ---------------------------------------------------------------------------
       
   164 // Called when the decoding (request) is cancelled for some reason.
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 void CCCAppCommLauncherImageDecoding::DoCancel()
       
   168     { 
       
   169     if (iImgDecoder)
       
   170         {
       
   171         iImgDecoder->Cancel();
       
   172         delete iImgDecoder;
       
   173         iImgDecoder = NULL;
       
   174         }
       
   175     if ( iBitmapScaler )
       
   176         {
       
   177         iBitmapScaler->Cancel();
       
   178         delete iBitmapScaler;
       
   179         iBitmapScaler = NULL;
       
   180         }
       
   181     iTimer.Cancel();
       
   182     }
       
   183 
       
   184 void CCCAppCommLauncherImageDecoding::ScaleBitmapL()
       
   185     {   
       
   186     iBitmapScaler = CBitmapScaler::NewL();
       
   187     iBitmapScaler->Scale( &iStatus, *iBitmap, iBitmapSize );
       
   188     SetActive();
       
   189     }
       
   190 
       
   191 void CCCAppCommLauncherImageDecoding::CreateBitmapL()
       
   192     {
       
   193     if ( iDecoderState == ECcaConvertThumbnailImage )
       
   194         {
       
   195         iImgDecoder = CImageDecoder::DataNewL( iFs, *iBitmapData, CImageDecoder::EOptionAlwaysThread );
       
   196         }
       
   197     else if ( iDecoderState == ECcaConvertImageFromFile )
       
   198         {
       
   199         // leaaves if file doesn't exist
       
   200         TRAPD ( initDecoder, iImgDecoder = CImageDecoder::FileNewL( iFs, *iImageFullName, CImageDecoder::EOptionAlwaysThread ) );
       
   201         if ( initDecoder )
       
   202         	{
       
   203 	        delete iBitmapScaler;
       
   204 	        iBitmapScaler = NULL;
       
   205 	        delete iImgDecoder;
       
   206 	        iImgDecoder = NULL;
       
   207         	return;
       
   208         	}
       
   209         }
       
   210    
       
   211    if ( !iBitmap )
       
   212        {
       
   213        TFrameInfo info = iImgDecoder->FrameInfo();
       
   214        iBitmap = new ( ELeave ) CFbsBitmap;
       
   215        User::LeaveIfError( iBitmap->Create( info.iOverallSizeInPixels, info.iFrameDisplayMode ));
       
   216        }
       
   217  
       
   218     iStatus = KRequestPending;
       
   219     iImgDecoder->Convert( &iStatus, *iBitmap );
       
   220     SetActive();
       
   221     }