browserui/browser/FavouritesSrc/BrowserFaviconHandler.cpp
branchRCL_3
changeset 64 6385c4c93049
parent 63 4baee4f15982
child 65 8e6fa1719340
equal deleted inserted replaced
63:4baee4f15982 64:6385c4c93049
     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::RequestFavicons()
       
   125 // ----------------------------------------------------------------------------
       
   126 //      
       
   127 void CBrowserFaviconHandler::RequestFavicons( CFavouritesItemList* aFavItems )
       
   128     {
       
   129     TInt count = aFavItems->Count();
       
   130     while(count)
       
   131         {            
       
   132             CFavouritesItem& item = *aFavItems->At( count - 1); // index starts from 0
       
   133             CGulIcon *favIcon = NULL;           
       
   134                         
       
   135             // Request Favicon from Engine  - there should be new API for request, but no har to use it
       
   136             if ( item.Url().Length() )
       
   137                 iFavicon = iApiProvider.BrCtlInterface().GetBitmapData(item.Url(), TBrCtlDefs::EBitmapFavicon );
       
   138 
       
   139             if ( favIcon )
       
   140                 {                
       
   141                 delete iFavicon;
       
   142                 iFavicon = NULL;
       
   143                 }
       
   144             count--;
       
   145         }
       
   146     }
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // CBrowserFaviconHandler::StartGetFaviconsL()
       
   150 // ----------------------------------------------------------------------------
       
   151 //		
       
   152 void CBrowserFaviconHandler::StartGetFaviconsL( CFavouritesItemList* aFavItems )
       
   153 	{
       
   154 	Cancel();
       
   155 	iBmpScaler->Cancel();
       
   156 		
       
   157 	if ( aFavItems->Count() )
       
   158 		{
       
   159 		// New request for favicons: 
       
   160 		iFavItems = aFavItems;
       
   161 		
       
   162 	    // Initialisation.
       
   163 		iFavItemsCurrentIndex = 0;      // will be incremented before favicon getting
       
   164         iFaviconsFound = 0;             // keeps track of favicons found
       
   165 		iFaviconArrayIndices->Reset();  // refresh the array mapping uid's to favicons
       
   166 		
       
   167 		// Initiate the getting of favicons	
       
   168 		GetFaviconL();
       
   169 		}
       
   170 	}
       
   171 	
       
   172 // ----------------------------------------------------------------------------
       
   173 // CBrowserFaviconHandler::GetFaviconL()
       
   174 // ----------------------------------------------------------------------------
       
   175 //		
       
   176 void CBrowserFaviconHandler::GetFaviconL()
       
   177 	{	
       
   178 	// The whole list has already been checked - we're finished
       
   179 	if ( iFavItemsCurrentIndex >= iFavItems->Count() )
       
   180 		{
       
   181 		iObserver.DrawFavicons();
       
   182 		}
       
   183 	else
       
   184 	    {
       
   185 	    // Step through the item array until we find a favicon, or reach the end
       
   186 		do
       
   187 			{
       
   188 			iWasLastItemFavicon = EFalse;
       
   189 			
       
   190 			CFavouritesItem& item = *iFavItems->At( iFavItemsCurrentIndex );			
       
   191 			if ( iFavicon )
       
   192 				{
       
   193 				// Make sure ongoing scaling is cancelled (if any)
       
   194 				Cancel();
       
   195 				iBmpScaler->Cancel();
       
   196 				
       
   197 				delete iFavicon;
       
   198 				iFavicon = NULL;
       
   199 				}
       
   200 						
       
   201 			// Get Favicon from Engine
       
   202 			if ( item.Url().Length() )
       
   203 				{
       
   204 				iFavicon = iApiProvider.BrCtlInterface().GetBitmapData(item.Url(), TBrCtlDefs::EBitmapFavicon );
       
   205 				}
       
   206 
       
   207 			// Asynchronously scales the favicon and stores it in an array
       
   208 			if ( iFavicon )
       
   209 				{
       
   210 				iWasLastItemFavicon = ETrue;	
       
   211 				RescaleFaviconL();
       
   212 				iFaviconsFound++;
       
   213 				__ASSERT_DEBUG( !( iFavItemsCurrentIndex > iFavItems->Count() ),
       
   214 								Util::Panic( Util::EOutOfRange ) );
       
   215 				break;
       
   216 				}
       
   217 			} while ( ++iFavItemsCurrentIndex < iFavItems->Count() );
       
   218 		}
       
   219 		
       
   220 		// If we exited the 'do' and the last item wasn't a favicon
       
   221 		// it means we're finished
       
   222 		if ( !iWasLastItemFavicon &&  ( iFaviconsFound %  KNumFaviconsToRedraw ) )
       
   223 			{
       
   224 			iObserver.DrawFavicons();
       
   225 			}
       
   226 	}
       
   227 	
       
   228 // ----------------------------------------------------------------------------
       
   229 // CBrowserFaviconHandler::RescaleFaviconL()
       
   230 // ----------------------------------------------------------------------------
       
   231 //
       
   232 void CBrowserFaviconHandler::RescaleFaviconL()
       
   233 	{
       
   234 	iHasMask = (iFavicon->Mask() != NULL);
       
   235 	if (iHasMask)
       
   236 		{
       
   237 		iBmpScaler->StartScalingL( *iFavicon->Mask(), iFaviconSize, ETrue );
       
   238 		}
       
   239 	else
       
   240 		{
       
   241 		iBmpScaler->StartScalingL( *iFavicon->Bitmap(), iFaviconSize, EFalse );
       
   242 		}
       
   243 	//  Complete two asynchronous requests.
       
   244 	iStatus = KRequestPending;
       
   245 	SetActive();
       
   246 
       
   247 	}
       
   248 	
       
   249 // ----------------------------------------------------------------------------
       
   250 // CBrowserFaviconHandler::AppendToIconArray()
       
   251 // ----------------------------------------------------------------------------
       
   252 //		
       
   253 TInt CBrowserFaviconHandler::AppendToIconArray()
       
   254 	{
       
   255 	TInt err( KErrNone );
       
   256 	TInt arrayIndex(-1);
       
   257 	
       
   258 	// Add the favicon to the icon array
       
   259 	TRAP( err, iIconArray->AppendL( iFavicon ); );
       
   260 
       
   261 	// Favicon was successfully added to the array
       
   262 	if ( !err )
       
   263 		{		
       
   264 		arrayIndex = iIconArray->Count() - 1;
       
   265 		
       
   266 		// Keep track of the index of the favicon in the icon array
       
   267 		if ( arrayIndex > -1 )
       
   268 			{
       
   269 			TFaviconIndex faviconIndex;			
       
   270 			faviconIndex.iFavouritesItemIndex = iFavItems->At( iFavItemsCurrentIndex )->Uid();
       
   271 			faviconIndex.iIndex = arrayIndex;		
       
   272 			TRAP( err, iFaviconArrayIndices->AppendL( faviconIndex ); )
       
   273 			
       
   274 			// If there was an error remove the favicon from the icon array
       
   275 			if ( err )
       
   276 				{
       
   277 				iIconArray->Delete( iIconArray->Count() - 1 );				
       
   278 				}
       
   279 			else
       
   280 				{
       
   281 				iFavicon = NULL; // favicon ownership passed
       
   282 				iFavItemsCurrentIndex++; // we can go to the next item now
       
   283 				}
       
   284 			}
       
   285 		}
       
   286 		
       
   287 	// Ownership of the favicon was not passed, so we need to delete it
       
   288 	if ( err )
       
   289 		{
       
   290 		iFaviconsFound--; // one less favicon in the array
       
   291 		}	
       
   292 		
       
   293 	return err;
       
   294 	}
       
   295 	
       
   296 	
       
   297 // ----------------------------------------------------------------------------
       
   298 // CBrowserFaviconHandler::GetFaviconArrayIndex()
       
   299 // ----------------------------------------------------------------------------
       
   300 //
       
   301 TInt CBrowserFaviconHandler::GetFaviconArrayIndex( const CFavouritesItem& aItem )
       
   302 	{
       
   303 	TInt arrayIndex( KErrNotFound );
       
   304 	
       
   305 	// Find the index of the favicon in the icon array
       
   306 	for ( TInt i=0; i<iFaviconArrayIndices->Count(); i++ )
       
   307 		{
       
   308 		// if uid is 0 then don't associate with favicon.  for adaptive bookmarks
       
   309 		if ( aItem.Uid() && aItem.Uid() == iFaviconArrayIndices->At(i).iFavouritesItemIndex )
       
   310 			{
       
   311 			arrayIndex = iFaviconArrayIndices->At(i).iIndex;
       
   312 			break;
       
   313 			}			
       
   314 		}	
       
   315 	return arrayIndex;
       
   316 	}
       
   317 
       
   318 // ----------------------------------------------------------------------------
       
   319 // CBrowserFaviconHandler::ScalingCompletedL()
       
   320 // ----------------------------------------------------------------------------
       
   321 //		
       
   322 void CBrowserFaviconHandler::ScalingCompletedL(CFbsBitmap* aResult, const TRect& /*aTargetRect*/)
       
   323 	{
       
   324 	// Check that favicon hasn't already disappeared (i.e. if race cond in which scaler returns 
       
   325 	// after you've already left and deleted the icon handle concerned
       
   326 	if(NULL == iFavicon)
       
   327 		{
       
   328 		return;
       
   329 		}
       
   330 	
       
   331 	
       
   332 	// Add the favicon to the icon array (which also contains other icons)
       
   333 	if (iHasMask)
       
   334 		{
       
   335 		iFavicon->SetMask(aResult);
       
   336 		iHasMask = EFalse;
       
   337 		iBmpScaler->StartScalingL( *iFavicon->Bitmap(), iFaviconSize, EFalse );
       
   338 		}
       
   339 	else
       
   340 		{
       
   341 		iFavicon->SetBitmap(aResult);
       
   342 		iFavicon->SetBitmapsOwnedExternally(EFalse);
       
   343 		TInt err = AppendToIconArray();
       
   344 		
       
   345 		// Complete the AO
       
   346 		TRequestStatus* status = &iStatus;
       
   347 		User::RequestComplete( status, err );	
       
   348 		}
       
   349 	}
       
   350 // ----------------------------------------------------------------------------
       
   351 // CBrowserFaviconHandler::DoCancel()
       
   352 // ----------------------------------------------------------------------------
       
   353 //	
       
   354 void CBrowserFaviconHandler::DoCancel()
       
   355 	{
       
   356 	TRequestStatus* s = &iStatus;
       
   357 	User::RequestComplete( s, KErrNone );
       
   358 	}
       
   359 	
       
   360 		
       
   361 // ----------------------------------------------------------------------------
       
   362 // CBrowserFaviconHandler::RunL()
       
   363 // ----------------------------------------------------------------------------
       
   364 //		
       
   365 void CBrowserFaviconHandler::RunL()
       
   366 	{		
       
   367 	TBool drawFavicons( EFalse );
       
   368 		
       
   369 	// Redraw the favicons at a predefined frequency
       
   370 	if ( iFaviconsFound % KNumFaviconsToRedraw == 0 )
       
   371 		{
       
   372 		drawFavicons = ETrue;
       
   373 		}
       
   374 	else
       
   375 		{
       
   376 		// We're not getting any more favicons: draw any undrawn ones
       
   377 		if ( iStatus != KErrNone )
       
   378 			{
       
   379 			// do a final draw
       
   380 			drawFavicons = ETrue;
       
   381 			}
       
   382 		}
       
   383 	
       
   384 	if ( drawFavicons && iFaviconsFound )
       
   385 		{
       
   386 		iObserver.DrawFavicons();
       
   387 		}
       
   388 	
       
   389 	// Continue getting favicons if no errors
       
   390 	if ( iStatus == KErrNone )
       
   391 		{
       
   392 		GetFaviconL();
       
   393 		}	
       
   394 	}
       
   395 
       
   396 // ----------------------------------------------------------------------------
       
   397 // CBrowserFaviconHandler::UpdateIconArray()
       
   398 // ----------------------------------------------------------------------------
       
   399 //	
       
   400 void CBrowserFaviconHandler::UpdateIconArray(CArrayPtr<CGulIcon>* aIconArray)
       
   401     {
       
   402    	iIconArray = aIconArray; 
       
   403     }
       
   404  
       
   405 // End of File