browserui/browser/FavouritesSrc/BrowserFaviconHandler.cpp
changeset 51 48e827313edd
parent 37 481242ead638
child 53 f427d27b98d8
equal deleted inserted replaced
37:481242ead638 51:48e827313edd
     1 /*
       
     2 * Copyright (c) 2005 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 the License "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 *      Implementation of CBrowserFaviconHandler
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "BrowserFaviconHandler.h"
       
    23 #include "ApiProvider.h"
       
    24 #include "BrowserUtil.h"
       
    25 #include <e32base.h>
       
    26 #include <brctldefs.h>
       
    27 #include <fbs.h>
       
    28 #include <MdaImageConverter.h>
       
    29 #include <favouritesitemlist.h>
       
    30 #include <favouritesitem.h>
       
    31 
       
    32 // number of favicons to get before redrawing
       
    33 const TInt KNumFaviconsToRedraw = 3	;
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // CBrowserFaviconHandler::CBrowserFaviconHandler()
       
    37 // ----------------------------------------------------------------------------
       
    38 //
       
    39 CBrowserFaviconHandler::CBrowserFaviconHandler( 
       
    40 										MApiProvider& aApiProvider,
       
    41 										MBrowserFaviconObserver& aObserver,
       
    42 										TSize aFaviconSize ) :
       
    43 	CActive( CActive::EPriorityIdle ),
       
    44 	iApiProvider( aApiProvider ),
       
    45 	iObserver( aObserver ),
       
    46 	iFaviconSize( aFaviconSize )
       
    47 	{
       
    48 	CActiveScheduler::Add( this );	
       
    49 	}
       
    50 
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // CBrowserFaviconHandler::~CBrowserFaviconHandler()
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 CBrowserFaviconHandler::~CBrowserFaviconHandler()
       
    57 	{
       
    58 	Cancel();
       
    59 	if(iBmpScaler!=NULL)
       
    60 		{
       
    61 			iBmpScaler->Cancel();
       
    62 		}
       
    63 	delete iBmpScaler;
       
    64 	delete iFaviconArrayIndices;
       
    65 	delete iFavicon;
       
    66 	}
       
    67 
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 // CBrowserFaviconHandler::NewL()
       
    71 // ----------------------------------------------------------------------------
       
    72 //	
       
    73 CBrowserFaviconHandler* CBrowserFaviconHandler::NewL( 
       
    74 										MApiProvider& aApiProvider,
       
    75 										CArrayPtr<CGulIcon>* aIconArray,
       
    76 										MBrowserFaviconObserver& aObserver,
       
    77 										TSize aFaviconSize )
       
    78 	{
       
    79 	CBrowserFaviconHandler* self = 
       
    80 					CBrowserFaviconHandler::NewLC( 	aApiProvider, 
       
    81 													aIconArray, 
       
    82 													aObserver,
       
    83 													aFaviconSize );
       
    84     
       
    85     CleanupStack::Pop();
       
    86     return self;	
       
    87 	}
       
    88 	
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // CBrowserFaviconHandler::NewLC()
       
    92 // ----------------------------------------------------------------------------
       
    93 //	
       
    94 CBrowserFaviconHandler* CBrowserFaviconHandler::NewLC(
       
    95 										MApiProvider& aApiProvider,
       
    96 										CArrayPtr<CGulIcon>* aIconArray,
       
    97 										MBrowserFaviconObserver& aObserver,
       
    98 										TSize aFaviconSize )
       
    99 	{
       
   100 	CBrowserFaviconHandler* self = 
       
   101 					new ( ELeave ) CBrowserFaviconHandler( 	aApiProvider,
       
   102 															aObserver,
       
   103 															aFaviconSize );
       
   104 	CleanupStack::PushL( self );
       
   105     self->ConstructL( aIconArray );
       
   106     return self;	
       
   107 	}
       
   108 	
       
   109 
       
   110 // ----------------------------------------------------------------------------
       
   111 // CBrowserFaviconHandler::ConstructL()
       
   112 // ----------------------------------------------------------------------------
       
   113 //		
       
   114 void CBrowserFaviconHandler::ConstructL( CArrayPtr<CGulIcon>* aIconArray )
       
   115 	{
       
   116 	iIconArray = aIconArray; // not owned
       
   117 	iFaviconArrayIndices = new ( ELeave ) CArrayFixFlat<TFaviconIndex>( 4 );
       
   118 		
       
   119 	// Construct Favicon Engine
       
   120 	iBmpScaler = CBrowserFaviconScaler::NewL(*this);
       
   121 	}
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CBrowserFaviconHandler::StartGetFaviconsL()
       
   125 // ----------------------------------------------------------------------------
       
   126 //		
       
   127 void CBrowserFaviconHandler::StartGetFaviconsL( CFavouritesItemList* aFavItems )
       
   128 	{
       
   129 	Cancel();
       
   130 	iBmpScaler->Cancel();
       
   131 		
       
   132 	if ( aFavItems->Count() )
       
   133 		{
       
   134 		// New request for favicons: 
       
   135 		iFavItems = aFavItems;
       
   136 		
       
   137 	    // Initialisation.
       
   138 		iFavItemsCurrentIndex = 0;      // will be incremented before favicon getting
       
   139         iFaviconsFound = 0;             // keeps track of favicons found
       
   140 		iFaviconArrayIndices->Reset();  // refresh the array mapping uid's to favicons
       
   141 		
       
   142 		// Initiate the getting of favicons	
       
   143 		GetFaviconL();
       
   144 		}
       
   145 	}
       
   146 	
       
   147 // ----------------------------------------------------------------------------
       
   148 // CBrowserFaviconHandler::GetFaviconL()
       
   149 // ----------------------------------------------------------------------------
       
   150 //		
       
   151 void CBrowserFaviconHandler::GetFaviconL()
       
   152 	{	
       
   153 	// The whole list has already been checked - we're finished
       
   154 	if ( iFavItemsCurrentIndex >= iFavItems->Count() )
       
   155 		{
       
   156 		iObserver.DrawFavicons();
       
   157 		}
       
   158 	else
       
   159 	    {
       
   160 	    // Step through the item array until we find a favicon, or reach the end
       
   161 		do
       
   162 			{
       
   163 			iWasLastItemFavicon = EFalse;
       
   164 			
       
   165 			CFavouritesItem& item = *iFavItems->At( iFavItemsCurrentIndex );
       
   166 			HBufC* url = HBufC::NewLC( item.Url().Length() );
       
   167 			url->Des().Copy( item.Url() );
       
   168 			
       
   169 			if ( iFavicon )
       
   170 				{
       
   171 				// Make sure ongoing scaling is cancelled (if any)
       
   172 				Cancel();
       
   173 				iBmpScaler->Cancel();
       
   174 				
       
   175 				delete iFavicon;
       
   176 				iFavicon = NULL;
       
   177 				}
       
   178 						
       
   179 			// Get Favicon from Engine
       
   180 			if ( item.Url().Length() )
       
   181 				{
       
   182 				iFavicon = iApiProvider.BrCtlInterface().GetBitmapData( 
       
   183 										*url, TBrCtlDefs::EBitmapFavicon );
       
   184 				}
       
   185             CleanupStack::PopAndDestroy();//url
       
   186 			// Asynchronously scales the favicon and stores it in an array
       
   187 			if ( iFavicon )
       
   188 				{
       
   189 				iWasLastItemFavicon = ETrue;	
       
   190 				RescaleFaviconL();
       
   191 				iFaviconsFound++;
       
   192 				__ASSERT_DEBUG( !( iFavItemsCurrentIndex > iFavItems->Count() ),
       
   193 								Util::Panic( Util::EOutOfRange ) );
       
   194 				break;
       
   195 				}
       
   196 			} while ( ++iFavItemsCurrentIndex < iFavItems->Count() );
       
   197 		}
       
   198 		
       
   199 		// If we exited the 'do' and the last item wasn't a favicon
       
   200 		// it means we're finished
       
   201 		if ( !iWasLastItemFavicon &&  ( iFaviconsFound %  KNumFaviconsToRedraw ) )
       
   202 			{
       
   203 			iObserver.DrawFavicons();
       
   204 			}
       
   205 	}
       
   206 	
       
   207 // ----------------------------------------------------------------------------
       
   208 // CBrowserFaviconHandler::RescaleFaviconL()
       
   209 // ----------------------------------------------------------------------------
       
   210 //
       
   211 void CBrowserFaviconHandler::RescaleFaviconL()
       
   212 	{
       
   213 	iHasMask = (iFavicon->Mask() != NULL);
       
   214 	if (iHasMask)
       
   215 		{
       
   216 		iBmpScaler->StartScalingL( *iFavicon->Mask(), iFaviconSize, ETrue );
       
   217 		}
       
   218 	else
       
   219 		{
       
   220 		iBmpScaler->StartScalingL( *iFavicon->Bitmap(), iFaviconSize, EFalse );
       
   221 		}
       
   222 	//  Complete two asynchronous requests.
       
   223 	iStatus = KRequestPending;
       
   224 	SetActive();
       
   225 
       
   226 	}
       
   227 	
       
   228 // ----------------------------------------------------------------------------
       
   229 // CBrowserFaviconHandler::AppendToIconArray()
       
   230 // ----------------------------------------------------------------------------
       
   231 //		
       
   232 TInt CBrowserFaviconHandler::AppendToIconArray()
       
   233 	{
       
   234 	TInt err( KErrNone );
       
   235 	TInt arrayIndex(-1);
       
   236 	
       
   237 	// Add the favicon to the icon array
       
   238 	TRAP( err, iIconArray->AppendL( iFavicon ); );
       
   239 
       
   240 	// Favicon was successfully added to the array
       
   241 	if ( !err )
       
   242 		{		
       
   243 		arrayIndex = iIconArray->Count() - 1;
       
   244 		
       
   245 		// Keep track of the index of the favicon in the icon array
       
   246 		if ( arrayIndex > -1 )
       
   247 			{
       
   248 			TFaviconIndex faviconIndex;			
       
   249 			faviconIndex.iFavouritesItemIndex = iFavItems->At( iFavItemsCurrentIndex )->Uid();
       
   250 			faviconIndex.iIndex = arrayIndex;		
       
   251 			TRAP( err, iFaviconArrayIndices->AppendL( faviconIndex ); )
       
   252 			
       
   253 			// If there was an error remove the favicon from the icon array
       
   254 			if ( err )
       
   255 				{
       
   256 				iIconArray->Delete( iIconArray->Count() - 1 );				
       
   257 				}
       
   258 			else
       
   259 				{
       
   260 				iFavicon = NULL; // favicon ownership passed
       
   261 				iFavItemsCurrentIndex++; // we can go to the next item now
       
   262 				}
       
   263 			}
       
   264 		}
       
   265 		
       
   266 	// Ownership of the favicon was not passed, so we need to delete it
       
   267 	if ( err )
       
   268 		{
       
   269 		iFaviconsFound--; // one less favicon in the array
       
   270 		}	
       
   271 		
       
   272 	return err;
       
   273 	}
       
   274 	
       
   275 	
       
   276 // ----------------------------------------------------------------------------
       
   277 // CBrowserFaviconHandler::GetFaviconArrayIndex()
       
   278 // ----------------------------------------------------------------------------
       
   279 //
       
   280 TInt CBrowserFaviconHandler::GetFaviconArrayIndex( const CFavouritesItem& aItem )
       
   281 	{
       
   282 	TInt arrayIndex( KErrNotFound );
       
   283 	
       
   284 	// Find the index of the favicon in the icon array
       
   285 	for ( TInt i=0; i<iFaviconArrayIndices->Count(); i++ )
       
   286 		{
       
   287 		// if uid is 0 then don't associate with favicon.  for adaptive bookmarks
       
   288 		if ( aItem.Uid() && aItem.Uid() == iFaviconArrayIndices->At(i).iFavouritesItemIndex )
       
   289 			{
       
   290 			arrayIndex = iFaviconArrayIndices->At(i).iIndex;
       
   291 			break;
       
   292 			}			
       
   293 		}	
       
   294 	return arrayIndex;
       
   295 	}
       
   296 
       
   297 // ----------------------------------------------------------------------------
       
   298 // CBrowserFaviconHandler::ScalingCompletedL()
       
   299 // ----------------------------------------------------------------------------
       
   300 //		
       
   301 void CBrowserFaviconHandler::ScalingCompletedL(CFbsBitmap* aResult, const TRect& /*aTargetRect*/)
       
   302 	{
       
   303 	// Check that favicon hasn't already disappeared (i.e. if race cond in which scaler returns 
       
   304 	// after you've already left and deleted the icon handle concerned
       
   305 	if(NULL == iFavicon)
       
   306 		{
       
   307 		return;
       
   308 		}
       
   309 	
       
   310 	
       
   311 	// Add the favicon to the icon array (which also contains other icons)
       
   312 	if (iHasMask)
       
   313 		{
       
   314 		iFavicon->SetMask(aResult);
       
   315 		iHasMask = EFalse;
       
   316 		iBmpScaler->StartScalingL( *iFavicon->Bitmap(), iFaviconSize, EFalse );
       
   317 		}
       
   318 	else
       
   319 		{
       
   320 		iFavicon->SetBitmap(aResult);
       
   321 		iFavicon->SetBitmapsOwnedExternally(EFalse);
       
   322 		TInt err = AppendToIconArray();
       
   323 		
       
   324 		// Complete the AO
       
   325 		TRequestStatus* status = &iStatus;
       
   326 		User::RequestComplete( status, err );	
       
   327 		}
       
   328 	}
       
   329 // ----------------------------------------------------------------------------
       
   330 // CBrowserFaviconHandler::DoCancel()
       
   331 // ----------------------------------------------------------------------------
       
   332 //	
       
   333 void CBrowserFaviconHandler::DoCancel()
       
   334 	{
       
   335 	TRequestStatus* s = &iStatus;
       
   336 	User::RequestComplete( s, KErrNone );
       
   337 	}
       
   338 	
       
   339 		
       
   340 // ----------------------------------------------------------------------------
       
   341 // CBrowserFaviconHandler::RunL()
       
   342 // ----------------------------------------------------------------------------
       
   343 //		
       
   344 void CBrowserFaviconHandler::RunL()
       
   345 	{		
       
   346 	TBool drawFavicons( EFalse );
       
   347 		
       
   348 	// Redraw the favicons at a predefined frequency
       
   349 	if ( iFaviconsFound % KNumFaviconsToRedraw == 0 )
       
   350 		{
       
   351 		drawFavicons = ETrue;
       
   352 		}
       
   353 	else
       
   354 		{
       
   355 		// We're not getting any more favicons: draw any undrawn ones
       
   356 		if ( iStatus != KErrNone )
       
   357 			{
       
   358 			// do a final draw
       
   359 			drawFavicons = ETrue;
       
   360 			}
       
   361 		}
       
   362 	
       
   363 	if ( drawFavicons && iFaviconsFound )
       
   364 		{
       
   365 		iObserver.DrawFavicons();
       
   366 		}
       
   367 	
       
   368 	// Continue getting favicons if no errors
       
   369 	if ( iStatus == KErrNone )
       
   370 		{
       
   371 		GetFaviconL();
       
   372 		}	
       
   373 	}
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // CBrowserFaviconHandler::UpdateIconArray()
       
   377 // ----------------------------------------------------------------------------
       
   378 //	
       
   379 void CBrowserFaviconHandler::UpdateIconArray(CArrayPtr<CGulIcon>* aIconArray)
       
   380     {
       
   381    	iIconArray = aIconArray; 
       
   382     }
       
   383  
       
   384 // End of File