phonebookui/Phonebook2/UIControls/src/CPbk2ThumbnailManager.cpp
changeset 0 e686773b3f54
child 3 04ab22b956c2
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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: Thumbnail manager for the social phonebook.  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // class header
       
    20 #include "CPbk2ThumbnailManager.h"
       
    21 
       
    22 // external includes 
       
    23 #include <CPbk2ImageManager.h>
       
    24 #include <CVPbkContactManager.h>
       
    25 #include <MVPbkStoreContact.h>
       
    26 #include <MVPbkContactOperationBase.h>
       
    27 #include <TPbk2ImageManagerParams.h>
       
    28 #include <CVPbkContactLinkArray.h>
       
    29 #include <MVPbkContactLink.h>
       
    30 #include <MVPbkFieldType.h>
       
    31 #include <barsread.h>	//tresource reader
       
    32 #include <gulicon.h>  
       
    33 
       
    34 //INTERNAL
       
    35 #include "CPbk2ContactViewListBox.h"
       
    36 #include "CPbk2IconArray.h"
       
    37 
       
    38 
       
    39 // Virtual Phonebook
       
    40 #include <MVPbkBaseContact.h>
       
    41 #include <MVPbkFieldType.h>
       
    42 #include <CVPbkContactManager.h>
       
    43 #include <TVPbkFieldTypeMapping.h>
       
    44 #include <MVPbkContactViewBase.h>
       
    45 
       
    46 //Pbk2
       
    47 #include <Pbk2UIControls.rsg>
       
    48 #include <Pbk2UID.h>
       
    49 
       
    50 // Default size for thumbnail images
       
    51 #define KDefaultThumbnailSize TSize(36,36)
       
    52 // icon offset. Thumbnail indexing starts from 10000 in CPbk2IconArray, so that there is 0 - 9999 indexes free for 
       
    53 // phonebook's own icons
       
    54 const TInt KIconIndexOffset = 10000;
       
    55 // loading limit for thumbnails
       
    56 const TInt KLoadingLimit = 100;
       
    57 // loading queue limit
       
    58 const TInt KQueueLimit = 25;
       
    59 // Denotes Start Index for an item in the any Queue
       
    60 const TInt KStartIndex = 0;
       
    61 
       
    62 /*
       
    63  * Helper class for mapping between contact link + image + index on listbox
       
    64  */
       
    65 NONSHARABLE_CLASS( CPbk2TmItem ) : public CBase
       
    66 	{
       
    67 public:	// constructor & destructor
       
    68 	
       
    69 	// Takes ownership of items
       
    70 	static CPbk2TmItem* NewL( MVPbkContactLink* aLink, 
       
    71 							  TInt aListboxIndex );
       
    72 	~CPbk2TmItem();
       
    73 	
       
    74 public:	//new functions
       
    75 	
       
    76 	/*
       
    77 	 * Returns contact link
       
    78 	 */
       
    79 	inline MVPbkContactLink* GetLink();
       
    80 		
       
    81 	
       
    82 	/*
       
    83 	 * Returns corresponding bitmap
       
    84 	 */
       
    85 	inline CFbsBitmap* GetBitmap();
       
    86 		
       
    87 	/*
       
    88 	 * Returns index on listbox
       
    89 	 */
       
    90 	inline TInt	GetListboxIndex() const;
       
    91 		
       
    92 	
       
    93 	/*
       
    94 	 * Returns index on Pbk2IconArray
       
    95 	 */
       
    96 	inline TInt GetIconArrayIndex() const;
       
    97 		
       
    98 	
       
    99 	/*
       
   100 	 * Returns icon id
       
   101 	 */
       
   102 	inline const TPbk2IconId& GetIconId() const;
       
   103 		
       
   104 	/*
       
   105 	 * Getter for has thumbnail flag
       
   106 	 */
       
   107 	inline TBool HasThumbnail();
       
   108 	
       
   109 	/*
       
   110 	 * deletes link and bitmap 
       
   111 	 */
       
   112 	inline void DeleteBitmap();
       
   113 	
       
   114 	/*
       
   115 	 * Setter for bitmap, deletes old one if set
       
   116 	 */
       
   117 	inline void SetBitmap( CFbsBitmap* aBitmap );
       
   118 		
       
   119 	
       
   120 	/*
       
   121 	 * Setter for listbox index
       
   122 	 */
       
   123 	inline void SetListboxIndex( TInt aIndex );
       
   124 		
       
   125 	/*
       
   126 	 * Setter for Pbk2IconArray index
       
   127 	 */
       
   128 	inline void SetIconArrayIndexAndId( TInt aIndex, TPbk2IconId aId );
       
   129 		
       
   130 	/*
       
   131 	 * Setter for has thumbnail flag
       
   132 	 */
       
   133 	inline void SetHasThumbnail( TBool aValue );
       
   134 	
       
   135 	/*
       
   136 	 * Compare function for sorting
       
   137 	 */
       
   138 	static TInt CompareByListboxIndex( 
       
   139 			const CPbk2TmItem& aItem1, const CPbk2TmItem& aItem2 );
       
   140 	
       
   141 private:	// constructor
       
   142 	
       
   143 	CPbk2TmItem( MVPbkContactLink* aLink, 
       
   144 				 TInt aListboxIndex );
       
   145 	
       
   146 private:	//data
       
   147 	// OWN: contact link
       
   148 	MVPbkContactLink* 	iLink;
       
   149 	// OWN: thumbnail
       
   150 	CFbsBitmap* 		iBitmap;
       
   151 	// index to listbox
       
   152 	TInt				iIndexOnListbox;
       
   153 	// index to icon array
       
   154 	TInt				iPbkIconArrayIndex;
       
   155 	// icon id
       
   156 	TPbk2IconId 		iIconId;
       
   157 	// informs whether the item has thumbnail image on contact database
       
   158 	TBool				iHasThumbnail;
       
   159 	};
       
   160 
       
   161 
       
   162 // --------------------------------------------------------------------------
       
   163 // CPbk2TmItem::NewL
       
   164 // --------------------------------------------------------------------------
       
   165 //
       
   166 CPbk2TmItem* CPbk2TmItem::NewL( MVPbkContactLink* aLink, 
       
   167 								TInt aListboxIndex )
       
   168     {
       
   169     CPbk2TmItem* self = new( ELeave )CPbk2TmItem( aLink, 
       
   170 												  aListboxIndex );
       
   171     return self;	
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CPbk2TmItem::CPbk2TmItem
       
   176 // --------------------------------------------------------------------------
       
   177 //
       
   178 CPbk2TmItem::CPbk2TmItem( MVPbkContactLink* aLink, 
       
   179 						  TInt aListboxIndex ):	
       
   180     iLink( aLink ),
       
   181 	iIndexOnListbox( aListboxIndex ),
       
   182 	iHasThumbnail( EFalse )	
       
   183 	{
       
   184 	}
       
   185 
       
   186 // --------------------------------------------------------------------------
       
   187 // CPbk2TmItem::~CPbk2TmItem
       
   188 // --------------------------------------------------------------------------
       
   189 //
       
   190 CPbk2TmItem::~CPbk2TmItem()
       
   191 	{
       
   192 	delete iLink;
       
   193 	delete iBitmap;
       
   194 	}
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // CPbk2TmItem::CompareByListboxIndex
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 TInt CPbk2TmItem::CompareByListboxIndex( 
       
   201 		const CPbk2TmItem& aItem1, const CPbk2TmItem& aItem2 )
       
   202 	{
       
   203 	TInt i = aItem1.GetListboxIndex();
       
   204 	TInt j = aItem2.GetListboxIndex();
       
   205 	return i-j;
       
   206 	}
       
   207 
       
   208 // --------------------------------------------------------------------------
       
   209 // CPbk2TmItem::GetLink
       
   210 // --------------------------------------------------------------------------
       
   211 //
       
   212 inline MVPbkContactLink* CPbk2TmItem::GetLink()
       
   213 	{
       
   214 	return iLink;
       
   215 	}
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // CPbk2TmItem::GetBitmap
       
   219 // --------------------------------------------------------------------------
       
   220 //
       
   221 inline CFbsBitmap*	CPbk2TmItem::GetBitmap()
       
   222 	{
       
   223 	return iBitmap;
       
   224 	}
       
   225 
       
   226 
       
   227 // --------------------------------------------------------------------------
       
   228 // CPbk2TmItem::GetListboxIndex
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 inline TInt CPbk2TmItem::GetListboxIndex() const
       
   232 	{
       
   233 	return iIndexOnListbox;
       
   234 	}
       
   235 
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CPbk2TmItem::GetIconArrayIndex
       
   239 // --------------------------------------------------------------------------
       
   240 //
       
   241 inline TInt CPbk2TmItem::GetIconArrayIndex() const
       
   242 	{
       
   243 	return iPbkIconArrayIndex;
       
   244 	}
       
   245 
       
   246 
       
   247 // --------------------------------------------------------------------------
       
   248 // CPbk2TmItem::HasThumbnail
       
   249 // --------------------------------------------------------------------------
       
   250 //
       
   251 inline TBool CPbk2TmItem::HasThumbnail()
       
   252 	{
       
   253 	return iHasThumbnail;
       
   254 	}
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CPbk2TmItem::GetIconId
       
   258 // --------------------------------------------------------------------------
       
   259 //
       
   260 inline const TPbk2IconId& CPbk2TmItem::GetIconId() const
       
   261 	{
       
   262 	return iIconId;
       
   263 	}
       
   264 
       
   265 // --------------------------------------------------------------------------
       
   266 // CPbk2TmItem::ClearValues
       
   267 // --------------------------------------------------------------------------
       
   268 //
       
   269 inline void CPbk2TmItem::DeleteBitmap( )
       
   270 	{
       
   271 	delete iBitmap;
       
   272 	iBitmap = NULL;
       
   273 	}
       
   274 
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CPbk2TmItem::SetBitmap
       
   278 // --------------------------------------------------------------------------
       
   279 //
       
   280 inline void CPbk2TmItem::SetBitmap( CFbsBitmap* aBitmap )
       
   281 	{
       
   282 	delete iBitmap;
       
   283 	iBitmap = aBitmap;
       
   284 	}
       
   285 
       
   286 
       
   287 // --------------------------------------------------------------------------
       
   288 // CPbk2TmItem::SetListboxIndex
       
   289 // --------------------------------------------------------------------------
       
   290 //
       
   291 inline void CPbk2TmItem::SetListboxIndex( TInt aIndex )
       
   292 	{
       
   293 	iIndexOnListbox = aIndex;
       
   294 	}
       
   295 
       
   296 
       
   297 // --------------------------------------------------------------------------
       
   298 // CPbk2TmItem::SetIconArrayIndexAndId
       
   299 // --------------------------------------------------------------------------
       
   300 //
       
   301 inline void CPbk2TmItem::SetIconArrayIndexAndId( TInt aIndex, TPbk2IconId aId )
       
   302 	{
       
   303 	iPbkIconArrayIndex = aIndex;
       
   304 	iIconId = aId;
       
   305 	}
       
   306 
       
   307 // --------------------------------------------------------------------------
       
   308 // CPbk2TmItem::SetHasThumbnail
       
   309 // --------------------------------------------------------------------------
       
   310 //
       
   311 inline void CPbk2TmItem::SetHasThumbnail( TBool aValue )
       
   312 	{
       
   313 	iHasThumbnail = aValue;
       
   314 	}
       
   315 
       
   316 
       
   317 /*********************************** THUMBNAIL MANAGER ************************************/
       
   318 
       
   319 
       
   320 // --------------------------------------------------------------------------
       
   321 // CPbk2ThumbnailManager* CPbk2ThumbnailManager::NewL
       
   322 // --------------------------------------------------------------------------
       
   323 //
       
   324 EXPORT_C CPbk2ThumbnailManager* CPbk2ThumbnailManager::NewL( CVPbkContactManager& aContactManager )
       
   325    {
       
   326    CPbk2ThumbnailManager* self = new (ELeave) CPbk2ThumbnailManager( aContactManager );
       
   327    CleanupStack::PushL(self);
       
   328    self->ConstructL();
       
   329    CleanupStack::Pop();
       
   330    return self;
       
   331    }
       
   332 
       
   333 
       
   334 // --------------------------------------------------------------------------
       
   335 // CPbk2ThumbnailManager::CPbk2ThumbnailManager
       
   336 // --------------------------------------------------------------------------
       
   337 //
       
   338 CPbk2ThumbnailManager::CPbk2ThumbnailManager( 
       
   339     CVPbkContactManager& aContactManager ) :
       
   340         iState( EIdle ),
       
   341         iContactManager( aContactManager ),
       
   342         iDefaultIconIndex( KErrNotFound )
       
   343 	{
       
   344 	}
       
   345 
       
   346 
       
   347 // --------------------------------------------------------------------------
       
   348 // CPbk2ThumbnailManager::~CPbk2ThumbnailManager()
       
   349 // --------------------------------------------------------------------------
       
   350 //
       
   351 CPbk2ThumbnailManager::~CPbk2ThumbnailManager()
       
   352 	{
       
   353 	delete iInProgressItemToBeRemoved;
       
   354 	delete iThumbOperation;
       
   355     delete iManager;
       
   356     delete iRetrieveOperation;
       
   357     delete iStoreContact;
       
   358     iContactThumbnails.ResetAndDestroy();
       
   359     iLoadingQueue.Reset();
       
   360     iPriorityArray.Reset();
       
   361     }
       
   362 
       
   363 
       
   364 // --------------------------------------------------------------------------
       
   365 // CPbk2ThumbnailManager::ConstructL()
       
   366 // --------------------------------------------------------------------------
       
   367 //
       
   368 void CPbk2ThumbnailManager::ConstructL()
       
   369 	{
       
   370 	// image manager
       
   371 	iManager = CPbk2ImageManager::NewL( iContactManager );
       
   372 	// read file type for thumbnail field
       
   373 	ReadFieldTypeL();
       
   374 	//reset thumbnail array
       
   375 	iContactThumbnails.Reset();
       
   376 	}
       
   377 
       
   378 
       
   379 // --------------------------------------------------------------------------
       
   380 // CPbk2ThumbnailManager::SetObserver()
       
   381 // --------------------------------------------------------------------------
       
   382 //
       
   383 void CPbk2ThumbnailManager::SetObserver( MPbk2ThumbnailManagerObserver& aObserver )
       
   384 	{
       
   385 	iObserver = &aObserver;
       
   386 	}
       
   387 	
       
   388 // --------------------------------------------------------------------------
       
   389 // CPbk2ThumbnailManager::RemoveObserver()
       
   390 // --------------------------------------------------------------------------
       
   391 //
       
   392 void CPbk2ThumbnailManager::RemoveObserver()
       
   393 	{
       
   394 	iObserver = NULL;
       
   395 	}
       
   396 
       
   397 // --------------------------------------------------------------------------
       
   398 // CPbk2ThumbnailManager::ThumbnailCount()	
       
   399 // --------------------------------------------------------------------------
       
   400 //
       
   401 TInt CPbk2ThumbnailManager::ThumbnailCount()	
       
   402 	{
       
   403 	return iContactThumbnails.Count();
       
   404 	}
       
   405 
       
   406 
       
   407 // --------------------------------------------------------------------------
       
   408 // CPbk2ThumbnailManager::GetPbkIconIndex()
       
   409 // --------------------------------------------------------------------------
       
   410 //
       
   411 TInt CPbk2ThumbnailManager::GetPbkIconIndex( TInt aListboxIndex, const MVPbkBaseContact& aContactLink )
       
   412 	{
       
   413 	TBool reLoadThumbnail = EFalse;
       
   414 	TInt arrIndex = iDefaultIconIndex;
       
   415 	
       
   416 	// check that requested thumbnail is allready added to thumbnail array
       
   417 	if( aListboxIndex < iContactThumbnails.Count() )
       
   418 		{
       
   419 		CPbk2TmItem* item = iContactThumbnails[ aListboxIndex ];
       
   420 		
       
   421         // This is because when find is in use, the indexes are not mapped
       
   422 		// directly to thumbnail managers indexes
       
   423 		if( !item->GetLink()->RefersTo( aContactLink ) )
       
   424 		    {
       
   425             // try to find correct one from the array based on contact link
       
   426             item = FindItem( aContactLink );
       
   427 		    }
       
   428 		
       
   429 		if( item )
       
   430 		    {
       
   431             // if item has a thumbnail image, but it is not loaded yet ( queue )
       
   432             if( item->HasThumbnail() && !item->GetBitmap() )
       
   433                 {
       
   434                 reLoadThumbnail = ETrue;
       
   435                 }
       
   436             else
       
   437                 {
       
   438                 arrIndex = item->GetIconArrayIndex();
       
   439                 }
       
   440 		    }
       
   441 			
       
   442 		//if item and thumbnail index was not found ( queue limit ), reload thumbnail
       
   443 		if( reLoadThumbnail )
       
   444 			{
       
   445 			TBool reOrderItem = ETrue;
       
   446 			TInt res = iLoadingQueue.Find( item );
       
   447 			//remove from array if duplicate
       
   448 			if( res != KErrNotFound )
       
   449 				{
       
   450 				// if item's position is 0 or 1, dont reorder
       
   451 				if( res <= 1 )
       
   452 					{
       
   453 					reOrderItem = EFalse;
       
   454 					}
       
   455 				// else remove item from the array for reordering
       
   456 				else
       
   457 					{
       
   458 					iLoadingQueue.Remove( res );
       
   459 					}
       
   460 				}
       
   461 			
       
   462 			// if item is to be reordered
       
   463 			if( reOrderItem )
       
   464 				{
       
   465 				// if there are more than 2 items
       
   466 				if( iLoadingQueue.Count() > 2 && iState == ELoading )
       
   467 					{
       
   468 					// insert to second position, first one is under loading
       
   469 					iLoadingQueue.Insert( item, 1 );
       
   470 					}
       
   471 				// else append to first or second
       
   472 				else
       
   473 					{
       
   474 					iLoadingQueue.Append( item );
       
   475 					}
       
   476 				}
       
   477 			
       
   478 			//if idle, start loading
       
   479 			if( iState == EIdle )
       
   480 				{
       
   481 				StartLoading();
       
   482 				}
       
   483 			}
       
   484 		}
       
   485 	return arrIndex;
       
   486 	}
       
   487 
       
   488 // --------------------------------------------------------------------------
       
   489 // CPbk2ThumbnailManager::FindItem()
       
   490 // --------------------------------------------------------------------------
       
   491 //
       
   492 CPbk2TmItem* CPbk2ThumbnailManager::FindItem( const MVPbkBaseContact& aContactLink )
       
   493 	{
       
   494 	const TInt count = iContactThumbnails.Count();
       
   495 	for( TInt i = 0; i < count; ++i )
       
   496 		{
       
   497 		if( iContactThumbnails[ i ]->GetLink()->RefersTo( aContactLink ) )
       
   498 			{
       
   499 			return iContactThumbnails[i];
       
   500 			}
       
   501 		}
       
   502 	// shouldn't come here ever
       
   503 	return NULL;
       
   504 	}
       
   505 
       
   506 // --------------------------------------------------------------------------
       
   507 // CPbk2ThumbnailManager::SetDefaultIconId()
       
   508 // --------------------------------------------------------------------------
       
   509 //
       
   510 void CPbk2ThumbnailManager::SetDefaultIconId( TPbk2IconId aDefaultIconId )
       
   511 	{
       
   512 	if( iIconArray )
       
   513 		{
       
   514 		iDefaultIconIndex = iIconArray->FindIcon( aDefaultIconId );
       
   515 		}
       
   516 	iDefaultIconId = aDefaultIconId;
       
   517 	}
       
   518 
       
   519 
       
   520 // --------------------------------------------------------------------------
       
   521 // CPbk2ThumbnailManager::GetDefaultIconIndex()
       
   522 // --------------------------------------------------------------------------
       
   523 //
       
   524 const TPbk2IconId& CPbk2ThumbnailManager::GetDefaultIconId()
       
   525 	{
       
   526 	return iDefaultIconId;
       
   527 	}
       
   528 
       
   529 
       
   530 // --------------------------------------------------------------------------
       
   531 // CPbk2ThumbnailManager::SetPbkIconArray()
       
   532 // --------------------------------------------------------------------------
       
   533 //
       
   534 void CPbk2ThumbnailManager::SetPbkIconArray( CPbk2IconArray* aIconArray )
       
   535 	{
       
   536 	iIconArray = aIconArray;
       
   537 	}
       
   538 
       
   539 
       
   540 // --------------------------------------------------------------------------
       
   541 // CPbk2ThumbnailManager::RemoveIconArray()
       
   542 // --------------------------------------------------------------------------
       
   543 //
       
   544 void CPbk2ThumbnailManager::RemoveIconArray()
       
   545 	{
       
   546 	// no more icon array
       
   547 	iIconArray = NULL;
       
   548 	const TInt count = iContactThumbnails.Count();
       
   549 	
       
   550 	// set all the indexes to default because there is no items in icon array to refer to
       
   551 	for( TInt i = 0; i < count; ++i )
       
   552 		{
       
   553 		iContactThumbnails[i]->SetIconArrayIndexAndId( iDefaultIconIndex, iDefaultIconId );
       
   554 		}
       
   555 	iIconIdCounter = 0;
       
   556 	}
       
   557 
       
   558 
       
   559 // --------------------------------------------------------------------------
       
   560 // CPbk2ThumbnailManager::IncreaseIndexes()
       
   561 // --------------------------------------------------------------------------
       
   562 //
       
   563 void CPbk2ThumbnailManager::IncreaseIndexes( TInt aListboxIndex )
       
   564 	{
       
   565 	TInt i;
       
   566 	TInt index = 0;
       
   567 	const TInt count = iContactThumbnails.Count();
       
   568 	// go through all contacts
       
   569 	for( i = 0; i < count; ++i )
       
   570 		{
       
   571 		index = iContactThumbnails[i]->GetListboxIndex();
       
   572 		// increase by one all the indexes that are bigger than newly added index
       
   573 		if( index  >= aListboxIndex )
       
   574 			{
       
   575 			iContactThumbnails[i]->SetListboxIndex( index+1 );
       
   576 			}
       
   577 		}
       
   578 	}
       
   579 
       
   580 
       
   581 // --------------------------------------------------------------------------
       
   582 // CPbk2ThumbnailManager::DecreaseIndexes()
       
   583 // --------------------------------------------------------------------------
       
   584 //
       
   585 void CPbk2ThumbnailManager::DecreaseIndexes( TInt aListboxIndex )
       
   586 	{
       
   587 	TInt i;
       
   588 	const TInt count = iContactThumbnails.Count();
       
   589 	TInt index = 0;
       
   590 	
       
   591 	// go through items
       
   592 	for( i = 0; i < count; ++i )
       
   593 		{
       
   594 		index = iContactThumbnails[i]->GetListboxIndex();
       
   595 		// decrease all indexes that were bigger than removed index
       
   596 		if( index > aListboxIndex )
       
   597 			{
       
   598 			iContactThumbnails[i]->SetListboxIndex( index-1 );
       
   599 			}
       
   600 		}
       
   601 	}
       
   602 
       
   603 // --------------------------------------------------------------------------
       
   604 // CPbk2ThumbnailManager::Reset()
       
   605 // --------------------------------------------------------------------------
       
   606 //
       
   607 void CPbk2ThumbnailManager::Reset()
       
   608     {
       
   609     // clear cache
       
   610     iContactThumbnails.ResetAndDestroy();
       
   611     iPriorityArray.Reset();
       
   612     
       
   613     // reset operations and queue
       
   614     delete iRetrieveOperation;
       
   615     iRetrieveOperation = NULL;
       
   616     delete iThumbOperation;
       
   617     iThumbOperation = NULL;
       
   618     iState = EIdle;
       
   619     iLoadingQueue.Reset();
       
   620     }
       
   621 
       
   622 // --------------------------------------------------------------------------
       
   623 // CPbk2ThumbnailManager::SetThumbnailIconSize()
       
   624 // --------------------------------------------------------------------------
       
   625 //
       
   626 void CPbk2ThumbnailManager::SetThumbnailIconSize( TSize aSize )
       
   627 	{
       
   628 	iIconSize = aSize;
       
   629 	}
       
   630 
       
   631 // --------------------------------------------------------------------------
       
   632 // CPbk2ThumbnailManager::GetThumbnailIconSize()
       
   633 // --------------------------------------------------------------------------
       
   634 //
       
   635 const TSize& CPbk2ThumbnailManager::GetThumbnailIconSize()
       
   636 	{
       
   637 	return iIconSize;
       
   638 	}
       
   639 
       
   640 // --------------------------------------------------------------------------
       
   641 // CPbk2ThumbnailManager::LoadThumbnailL()
       
   642 // --------------------------------------------------------------------------
       
   643 //
       
   644 void CPbk2ThumbnailManager::LoadThumbnailL( const MVPbkContactLink& aContactLink, TInt aListboxIndex )
       
   645 	{
       
   646 	// check if the thumbnail is already loaded
       
   647 	if( aListboxIndex < iContactThumbnails.Count() &&
       
   648 		iContactThumbnails[aListboxIndex]->GetLink()->IsSame( aContactLink ) )
       
   649 		{
       
   650 		CFbsBitmap* tmp = iContactThumbnails[ aListboxIndex ]->GetBitmap();
       
   651 		// if there is icon for the contact allready loaded
       
   652 		if( tmp )
       
   653 			{
       
   654 			//inform observer 
       
   655 			if( iObserver )
       
   656 				{
       
   657 				//if icon array exists
       
   658 				if( iIconArray )
       
   659 					{
       
   660 					//create icon and pass it to the array
       
   661 					CGulIcon* icon = CGulIcon::NewL( DuplicateBitmapL( tmp ) );
       
   662 					// counter to add icon ids
       
   663 					TPbk2IconId iconID( TUid::Uid(KPbk2UID3 ), 
       
   664 							KIconIndexOffset + iIconIdCounter );
       
   665 					iIconIdCounter++;
       
   666 					iIconArray->AppendIconL( icon, iconID );
       
   667 					// store the index
       
   668 					iContactThumbnails[ aListboxIndex ]->SetIconArrayIndexAndId( iIconArray->FindIcon( iconID ), iconID );
       
   669 					iObserver->ThumbnailLoadingComplete( KErrNone,iContactThumbnails[ aListboxIndex]->GetListboxIndex() );
       
   670 					}
       
   671 				}
       
   672 			}
       
   673 		else
       
   674 			{
       
   675 			// has a default icon
       
   676 			iObserver->ThumbnailLoadingComplete( KErrNotFound,iContactThumbnails[ aListboxIndex]->GetListboxIndex() );
       
   677 			}
       
   678 		}
       
   679 	// new contact, add new CPbk2TmItem to thumbnail array
       
   680 	else
       
   681 		{
       
   682 		// create new contact item. Thumbnail is not yet loaded so it's set to NULL
       
   683 		CPbk2TmItem* item = CPbk2TmItem::NewL( aContactLink.CloneLC(), aListboxIndex );
       
   684 		CleanupStack::Pop();
       
   685 		// set default icon index
       
   686 		item->SetIconArrayIndexAndId( iDefaultIconIndex,iDefaultIconId );
       
   687 		item->SetHasThumbnail( ETrue );
       
   688 		
       
   689 		
       
   690 		// if thumbnail to be loaded is added middle of the list (new contact), handle indexes
       
   691 		const TInt lastIndex = iContactThumbnails.Count() - 1;
       
   692 		if( lastIndex >= 0 && 
       
   693 				aListboxIndex <= iContactThumbnails[ lastIndex ]->GetListboxIndex() )
       
   694 			{
       
   695 			// make room for the new thumbnail. Function does nothing if priority array is not full 
       
   696 			// This is because we need to get the thumbnail showing right away when new contact is added
       
   697 			MakeRoomForNextThumbnail();
       
   698 			// increase indexes after this added index
       
   699 			IncreaseIndexes( aListboxIndex );
       
   700 			}
       
   701 		
       
   702 		// add item
       
   703 		TLinearOrder<CPbk2TmItem> sorter( CPbk2TmItem::CompareByListboxIndex );
       
   704 		iContactThumbnails.InsertInOrderL( item, sorter );
       
   705 		
       
   706 		// also add item to loading queue. This is because the actual array is sorted between thumbnail loading
       
   707 		// list doesn't own the items
       
   708 		// if added item's listbox index is smaller than already added, add to start end on loading queue
       
   709 		const TInt queueCount = iLoadingQueue.Count();
       
   710 		if( aListboxIndex <  queueCount && queueCount > 2  )
       
   711 			{
       
   712 			//add to second place, first one is under loading
       
   713 			iLoadingQueue.Insert( item, 1 );
       
   714 			}
       
   715 		// just add last place on the list
       
   716 		else
       
   717 			{
       
   718 			iLoadingQueue.Append( item );
       
   719 			}
       
   720 		
       
   721 		//if idle, start loading
       
   722 		if( iState == EIdle )
       
   723 			{
       
   724 			StartLoading();
       
   725 			}
       
   726 		}
       
   727 	}
       
   728 
       
   729 // --------------------------------------------------------------------------
       
   730 // CPbk2ThumbnailManager::RemoveThumbnail()
       
   731 // --------------------------------------------------------------------------
       
   732 //
       
   733 TInt CPbk2ThumbnailManager::RemoveThumbnail( const MVPbkContactLink& aContactLink, TInt aListboxIndex  )
       
   734 	{
       
   735 	TInt i;
       
   736 	const TInt count = iContactThumbnails.Count();
       
   737 	CPbk2TmItem* item = NULL;
       
   738 	TInt listBoxIndex = 0;
       
   739 	
       
   740 	for( i = 0; i < count; ++i )
       
   741 		{
       
   742 		item = iContactThumbnails[i];
       
   743 		if( item->GetLink()->IsSame( aContactLink ) )
       
   744 			{
       
   745 			// store listbox index before updating
       
   746 			listBoxIndex = item->GetListboxIndex();
       
   747 		
       
   748             // handle indexes
       
   749 		    DecreaseIndexes( aListboxIndex );
       
   750 
       
   751 			// remove item from the list
       
   752 			iContactThumbnails.Remove( i );
       
   753 			
       
   754 			// check that the icon is not a default icon
       
   755 			if( item->GetIconArrayIndex() != iDefaultIconIndex )
       
   756 				{
       
   757 				if( iIconArray )
       
   758 					{
       
   759 					//inform icon array to remove the icon
       
   760 					iIconArray->RemoveIcon( item->GetIconId() );
       
   761 					}
       
   762 				//update indexes after that
       
   763 				UpdateIconIndexes();
       
   764 				}
       
   765 			
       
   766 			//remove item from priority array if in it
       
   767 			const TInt index = iPriorityArray.Find( item->GetLink() );
       
   768 			//remove if found
       
   769 			if( index != KErrNotFound )
       
   770 				{
       
   771 				iPriorityArray.Remove( index );
       
   772 				}
       
   773 			
       
   774 			//The item might be even in the loading queue
       
   775 			//Remove from loading queue
       
   776 			//remove item from priority array if in it
       
   777             const TInt loadIndex = iLoadingQueue.Find( item );
       
   778             //remove if found
       
   779             if( loadIndex != KErrNotFound )
       
   780                 {
       
   781                 iLoadingQueue.Remove( loadIndex );
       
   782                 }
       
   783 			
       
   784 			// inform observer
       
   785 			if( iObserver )
       
   786 				{
       
   787 				iObserver->ThumbnailRemoved( *item->GetLink(),listBoxIndex );
       
   788 				}
       
   789 			
       
   790 			//The item at 0th position, denotes the current item
       
   791 			//whose thumbnail load is in progress.			
       
   792 			if ( KStartIndex == loadIndex && !iInProgressItemToBeRemoved )
       
   793 			    {
       
   794 			    //Remove it when its safe
       
   795 			    iInProgressItemToBeRemoved = item;
       
   796 			    }
       
   797 			else
       
   798 			    {
       
   799 			    //can be safely deleted immediately
       
   800 			    delete item;
       
   801 			    }
       
   802 			return KErrNone;
       
   803 			}
       
   804 		}
       
   805 	return KErrNotFound;
       
   806 	}
       
   807 
       
   808 
       
   809 // --------------------------------------------------------------------------
       
   810 // CPbk2ThumbnailManager::UpdateIconIndexes()
       
   811 // --------------------------------------------------------------------------
       
   812 //
       
   813 void CPbk2ThumbnailManager::UpdateIconIndexes(  )
       
   814 	{
       
   815 	// if icon array exists
       
   816 	if( iIconArray )
       
   817 		{
       
   818 		const TInt count = iContactThumbnails.Count();
       
   819 		for( TInt i = 0; i < count; ++i )
       
   820 			{
       
   821 			if( !( iDefaultIconId == iContactThumbnails[ i ]->GetIconId() ) ) 
       
   822 				{
       
   823 				// icon is removed from the CPbk2IconArray, update indexes
       
   824 				TPbk2IconId id = iContactThumbnails[ i ]->GetIconId();	
       
   825 				iContactThumbnails[ i ]->SetIconArrayIndexAndId( iIconArray->FindIcon( id ), id );
       
   826 				}
       
   827 			}
       
   828 		}
       
   829 	}
       
   830 
       
   831 
       
   832 
       
   833 // --------------------------------------------------------------------------
       
   834 // CPbk2ThumbnailManager::DuplicateBitmapL()
       
   835 // --------------------------------------------------------------------------
       
   836 //
       
   837 CFbsBitmap* CPbk2ThumbnailManager::DuplicateBitmapL( CFbsBitmap* aSourceBitmap ) 
       
   838 	{
       
   839 	//Target bitmap.
       
   840 	CFbsBitmap* dstbitmap = new (ELeave) CFbsBitmap;
       
   841 	CleanupStack::PushL( dstbitmap );
       
   842 
       
   843 	//Get the handle to source bitmap
       
   844 	TInt srchandle = aSourceBitmap->Handle();
       
   845 
       
   846 	//Duplicate the bitmap handle. Increases the RefCount of bitmap 
       
   847 	//managed but Fbs Server
       
   848 	User::LeaveIfError( dstbitmap->Duplicate( srchandle ) );
       
   849 	CleanupStack::Pop( dstbitmap );
       
   850 	return dstbitmap;
       
   851 	}
       
   852 
       
   853 
       
   854 // --------------------------------------------------------------------------
       
   855 // CPbk2ThumbnailManager::StartLoading()
       
   856 // --------------------------------------------------------------------------
       
   857 //
       
   858 void CPbk2ThumbnailManager::StartLoading( )
       
   859 	{
       
   860 	//its safe to delete the item here
       
   861 	if ( iInProgressItemToBeRemoved )
       
   862 	    {
       
   863 	    delete iInProgressItemToBeRemoved;
       
   864 	    iInProgressItemToBeRemoved = NULL;
       
   865 	    }
       
   866 	
       
   867     TInt count = iLoadingQueue.Count();
       
   868     
       
   869     // limit loading queue
       
   870     while( count > KQueueLimit )
       
   871         {
       
   872         iLoadingQueue.Remove( --count );
       
   873         }
       
   874         
       
   875     if( count )
       
   876 		{
       
   877 		// manager is loading
       
   878 		iState = ELoading;
       
   879 		// take first item from the queue
       
   880 		CPbk2TmItem* item = iLoadingQueue[KStartIndex];
       
   881 		// take first item from the loading queue
       
   882 		TRAPD( err, iRetrieveOperation = 
       
   883 			iContactManager.RetrieveContactL( *item->GetLink(), *this ) );
       
   884 		if( err )
       
   885 			{
       
   886 			// error occured -> remove item from the queue and continue
       
   887 			iLoadingQueue.Remove( KStartIndex );
       
   888 			//load next one
       
   889 			StartLoading();
       
   890 			}
       
   891 		}
       
   892 		
       
   893 	// no more items
       
   894 	else
       
   895 		{
       
   896 		// loading queue is empty, inform observer
       
   897 		if( iObserver )
       
   898 			{
       
   899 			iObserver->LoadingFinished();
       
   900 			}
       
   901 		// default state
       
   902 		iState = EIdle;
       
   903 		}
       
   904 	}
       
   905 
       
   906 // --------------------------------------------------------------------------
       
   907 // CPbk2ThumbnailManager::VPbkSingleContactOperationComplete
       
   908 // --------------------------------------------------------------------------
       
   909 //
       
   910 void CPbk2ThumbnailManager::VPbkSingleContactOperationComplete
       
   911         ( MVPbkContactOperationBase& /*aOperation*/ , MVPbkStoreContact* aContact )
       
   912     {
       
   913     delete iRetrieveOperation;
       
   914     iRetrieveOperation = NULL;
       
   915 
       
   916     delete iStoreContact;
       
   917     iStoreContact = aContact;
       
   918     
       
   919     // store contact found, fetch thumbnail for it
       
   920     DoLoadThumbnail( *iStoreContact );
       
   921     }
       
   922 
       
   923 // --------------------------------------------------------------------------
       
   924 // CPbk2ThumbnailManager::VPbkSingleContactOperationFailed
       
   925 // --------------------------------------------------------------------------
       
   926 //
       
   927 void CPbk2ThumbnailManager::VPbkSingleContactOperationFailed
       
   928         ( MVPbkContactOperationBase& /*aOperation*/ ,TInt /*aError*/ )
       
   929     {
       
   930     if( iObserver && !iInProgressItemToBeRemoved && iLoadingQueue.Count() )
       
   931     	{
       
   932     	//inform observer
       
   933     	//We should not inform the observer with wrong index,
       
   934     	//hence check for iInProgressItemToBeRemoved
       
   935     	iObserver->ThumbnailLoadingComplete( KErrNotFound, iLoadingQueue[ KStartIndex ]->GetListboxIndex() );
       
   936     	
       
   937     	//remove item from the queue
       
   938     	iLoadingQueue.Remove( KStartIndex );
       
   939     	}    
       
   940     //next
       
   941     StartLoading();
       
   942     }
       
   943 
       
   944 
       
   945 // --------------------------------------------------------------------------
       
   946 // CPbk2ThumbnailManager::DoLoadThumbnail()
       
   947 // --------------------------------------------------------------------------
       
   948 //
       
   949 void CPbk2ThumbnailManager::DoLoadThumbnail( 
       
   950 		MVPbkBaseContact& aContact )
       
   951 	{
       
   952     TInt err = KErrNotFound;
       
   953 
       
   954     //if the item in progress has been removed then it will be filled in iInProgressItemToBeRemoved
       
   955     //If such condition arises then we need not load the thumbnail at all
       
   956     
       
   957     // check if the contact has an image
       
   958 	if( !iInProgressItemToBeRemoved && iManager->HasImage( aContact, *iFieldType ) && iLoadingQueue.Count() )
       
   959 		{
       
   960 		// contact has a thumbnail
       
   961 		iLoadingQueue[ KStartIndex ]->SetHasThumbnail( ETrue ); 
       
   962 		
       
   963 		/*
       
   964 		 * If priority array is full, this function removes first item from the array and 
       
   965 		 * frees all the allocations related.
       
   966 		 */
       
   967 		MakeRoomForNextThumbnail();
       
   968 			
       
   969 		// if there is room, load thumbnail
       
   970 		if( iPriorityArray.Count() <= KLoadingLimit )
       
   971 			{
       
   972 			//add current item to priority array
       
   973 			iPriorityArray.Append( iLoadingQueue[ KStartIndex ]->GetLink() );
       
   974 			
       
   975 			// start loading picture for the current item
       
   976 			delete iThumbOperation;
       
   977 			iThumbOperation = NULL;
       
   978 			// Define parameters for thumbnail
       
   979 			TPbk2ImageManagerParams params;
       
   980 			// if iconsize is not set
       
   981 			if( iIconSize.iHeight == 0 &&  iIconSize.iWidth == 0 )
       
   982 				{
       
   983 				iIconSize = KDefaultThumbnailSize;
       
   984 				}
       
   985 			TInt useCropping = 0x0008;
       
   986 			// set params
       
   987 			params.iSize = iIconSize;
       
   988 			params.iFlags = TPbk2ImageManagerParams::EScaleImage |
       
   989 							TPbk2ImageManagerParams::EKeepAspectRatio |
       
   990 							useCropping;	//CROP IMAGE	//TODO change value
       
   991 			// fetch the image
       
   992 			TRAP( err, iThumbOperation =
       
   993 				iManager->GetImageAsyncL( &params, aContact, *iFieldType, *this ) );
       
   994 			}
       
   995 		// no room at this moment
       
   996 		else
       
   997 			{
       
   998 			//has image, but cannot load it
       
   999 			err = KErrNone;
       
  1000 			//remove item from the queue
       
  1001 			iLoadingQueue.Remove( KStartIndex );
       
  1002 			// start loading next one
       
  1003 			StartLoading();
       
  1004 			}
       
  1005 		}
       
  1006 	
       
  1007 	// contact has no image or error occured, move to next contact
       
  1008 	if( err )
       
  1009 		{
       
  1010 		if ( iLoadingQueue.Count() )
       
  1011 		    {
       
  1012             if( iObserver )
       
  1013                 {
       
  1014                 //inform observer
       
  1015                 iObserver->ThumbnailLoadingComplete( KErrNotFound, iLoadingQueue[ KStartIndex ]->GetListboxIndex() );
       
  1016                 }
       
  1017             //no thumbnail
       
  1018             iLoadingQueue[ KStartIndex ]->SetHasThumbnail( EFalse );
       
  1019             // set default icon index and id
       
  1020             iLoadingQueue[ KStartIndex ]->SetIconArrayIndexAndId( iDefaultIconIndex, iDefaultIconId );
       
  1021             //remove item from the queue
       
  1022             iLoadingQueue.Remove( KStartIndex );
       
  1023 		    }
       
  1024 		// start loading next one
       
  1025 		StartLoading();
       
  1026 		}
       
  1027 	}
       
  1028 
       
  1029 
       
  1030 // --------------------------------------------------------------------------
       
  1031 // CPbk2ThumbnailManager::MakeRoomForNextThumbnail
       
  1032 // --------------------------------------------------------------------------
       
  1033 //
       
  1034 void CPbk2ThumbnailManager::MakeRoomForNextThumbnail()
       
  1035 	{
       
  1036 	// if the loading limit for thumbnails is reached and optimizing is on, make room for next thumbnail
       
  1037 	if( iPriorityArray.Count() >= KLoadingLimit )
       
  1038 		{
       
  1039 		// remove first one from the queue
       
  1040 		CPbk2TmItem* rem = NULL;
       
  1041 		const TInt count = iContactThumbnails.Count();
       
  1042 		for( TInt i = 0; i < count; ++i )
       
  1043 			{
       
  1044 			if( iContactThumbnails[ i ]->GetLink()->IsSame( *iPriorityArray[ KStartIndex ] ) )
       
  1045 				{
       
  1046 				rem = iContactThumbnails[i];
       
  1047 				break;
       
  1048 				}
       
  1049 			}
       
  1050 		// if item was found
       
  1051 		if( rem )
       
  1052 			{
       
  1053 			// remove from priority array
       
  1054 			iPriorityArray.Remove( 0 );
       
  1055 			
       
  1056 			//remove icon from Pbk2IconArray
       
  1057 			if( iIconArray )
       
  1058 				{
       
  1059 				iIconArray->RemoveIcon( rem->GetIconId() );
       
  1060 				}
       
  1061 			// remove memory allocations from heap
       
  1062 			rem->DeleteBitmap();
       
  1063 			//set default icon id and index
       
  1064 			rem->SetIconArrayIndexAndId( iDefaultIconIndex,iDefaultIconId );
       
  1065 			//update indexes for other items
       
  1066 			UpdateIconIndexes();
       
  1067 			}
       
  1068 		}
       
  1069 	}
       
  1070 
       
  1071 
       
  1072 
       
  1073 // --------------------------------------------------------------------------
       
  1074 // CPbk2ThumbnailManager::Pbk2ImageGetComplete
       
  1075 // --------------------------------------------------------------------------
       
  1076 //
       
  1077 void CPbk2ThumbnailManager::Pbk2ImageGetComplete
       
  1078         ( MPbk2ImageOperation& /*aOperation*/, CFbsBitmap* aBitmap )
       
  1079     {
       
  1080     //if the item in progress has been removed then it will be filled in iInProgressItemToBeRemoved
       
  1081     //If such condition arises then we need not load the thumbnail at all
       
  1082     
       
  1083     // store loaded bitmap
       
  1084     if ( !iInProgressItemToBeRemoved && iLoadingQueue.Count() )
       
  1085         {
       
  1086         iLoadingQueue[ KStartIndex ]->SetBitmap( aBitmap );
       
  1087         
       
  1088         // inform the observer
       
  1089         if( iObserver )
       
  1090             {
       
  1091             // if icon array exists
       
  1092             if( iIconArray )
       
  1093                 {
       
  1094                 //create icon and pass it to the array
       
  1095                 TPbk2IconId iconID( TUid::Uid( KPbk2UID3 ), iIconIdCounter + KIconIndexOffset );
       
  1096                 iIconIdCounter++;
       
  1097                 
       
  1098                 TRAPD( err, AppendIconL( iconID, aBitmap ) );	
       
  1099                 if( err == KErrNone )
       
  1100                     {
       
  1101                     // set icond index and id
       
  1102                     iLoadingQueue[ KStartIndex ]->SetIconArrayIndexAndId( iIconArray->FindIcon( iconID ), iconID );
       
  1103                     // inform observer
       
  1104                     iObserver->ThumbnailLoadingComplete( KErrNone, iLoadingQueue[ KStartIndex ]->GetListboxIndex() );
       
  1105                     }
       
  1106                 }
       
  1107             }
       
  1108         
       
  1109         //remove item from the queue
       
  1110         iLoadingQueue.Remove( KStartIndex );
       
  1111         }
       
  1112     else
       
  1113         {
       
  1114         //None to take ownership. so delete here
       
  1115         delete aBitmap;
       
  1116         }   
       
  1117     
       
  1118     // delete operation
       
  1119     delete iThumbOperation;
       
  1120     iThumbOperation = NULL;
       
  1121     // next
       
  1122     StartLoading();
       
  1123     }
       
  1124 
       
  1125 // --------------------------------------------------------------------------
       
  1126 // CPbk2ThumbnailManager::Pbk2ImageGetFailed
       
  1127 // --------------------------------------------------------------------------
       
  1128 //
       
  1129 void CPbk2ThumbnailManager::Pbk2ImageGetFailed
       
  1130         ( MPbk2ImageOperation& /*aOperation*/, TInt /*aError*/ )
       
  1131     {
       
  1132     delete iThumbOperation;
       
  1133     iThumbOperation = NULL;
       
  1134     
       
  1135     if( iObserver && !iInProgressItemToBeRemoved && iLoadingQueue.Count() )
       
  1136     	{
       
  1137     	//inform observer
       
  1138     	//We should not inform the observer with wrong index,
       
  1139     	//hence check for iInProgressItemToBeRemoved
       
  1140     	iObserver->ThumbnailLoadingComplete( KErrNotFound, iLoadingQueue[ KStartIndex ]->GetListboxIndex() );
       
  1141     	
       
  1142     	//remove item from the queue
       
  1143     	iLoadingQueue.Remove( KStartIndex );    	    
       
  1144     	}
       
  1145     
       
  1146     // next
       
  1147     StartLoading();
       
  1148     }
       
  1149 
       
  1150 // --------------------------------------------------------------------------
       
  1151 // CPbk2ThumbnailManager::ContactViewReady
       
  1152 // --------------------------------------------------------------------------
       
  1153 //
       
  1154 void CPbk2ThumbnailManager::ContactViewReady( MVPbkContactViewBase& aView )
       
  1155     {
       
  1156     TRAP_IGNORE( DoContactViewReadyL( aView ) );
       
  1157     }
       
  1158 
       
  1159 // --------------------------------------------------------------------------
       
  1160 // CPbk2ThumbnailManager::DoContactViewReadyL
       
  1161 // --------------------------------------------------------------------------
       
  1162 //
       
  1163 void CPbk2ThumbnailManager::DoContactViewReadyL( MVPbkContactViewBase& aView )
       
  1164     {   
       
  1165     // get contact count
       
  1166     const TInt contactCount = aView.ContactCountL();
       
  1167     const TInt thumbnailCount = iContactThumbnails.Count();
       
  1168     
       
  1169     if( thumbnailCount == 0 )
       
  1170         {
       
  1171         // first time initialization
       
  1172         for( TInt i = 0; i < contactCount; ++i )
       
  1173             {
       
  1174             CPbk2TmItem* item = CPbk2TmItem::NewL( aView.CreateLinkLC( i ), i );
       
  1175             CleanupStack::Pop(); // link
       
  1176             CleanupStack::PushL( item );
       
  1177             // set default icon index
       
  1178             item->SetIconArrayIndexAndId( iDefaultIconIndex,iDefaultIconId );
       
  1179             item->SetHasThumbnail( ETrue );
       
  1180             
       
  1181             // add item
       
  1182             iContactThumbnails.AppendL( item );
       
  1183             CleanupStack::Pop( item );
       
  1184             }
       
  1185         }
       
  1186     else
       
  1187         {
       
  1188         // there is items in the listbox that are not loaded yet. If there is favorite contacts, 
       
  1189         // those are added afterwards to the list. normal items are added first.
       
  1190         const TInt itemCount = contactCount - thumbnailCount;
       
  1191         for( TInt i = 0; i < itemCount; ++i )
       
  1192             {            
       
  1193             MVPbkContactLink* link = aView.CreateLinkLC( i );
       
  1194             // if link exists and some thumbnails are not loaded
       
  1195             if( link  )
       
  1196                 {
       
  1197                 // add to thumb manager and load thumbnail
       
  1198                 LoadThumbnailL( *link,i );
       
  1199                 // created link
       
  1200                 CleanupStack::PopAndDestroy();
       
  1201                 }
       
  1202             }
       
  1203         }  
       
  1204     }
       
  1205 
       
  1206 // --------------------------------------------------------------------------
       
  1207 // CPbk2ThumbnailManager::ContactAddedToView
       
  1208 // --------------------------------------------------------------------------
       
  1209 //
       
  1210 void CPbk2ThumbnailManager::ContactViewUnavailable( 
       
  1211     MVPbkContactViewBase& /*aView*/ )
       
  1212     {
       
  1213     // ignored
       
  1214     }
       
  1215 
       
  1216 // --------------------------------------------------------------------------
       
  1217 // CPbk2ThumbnailManager::ContactAddedToView
       
  1218 // --------------------------------------------------------------------------
       
  1219 //
       
  1220 void CPbk2ThumbnailManager::ContactAddedToView(
       
  1221     MVPbkContactViewBase& /*aView*/, 
       
  1222     TInt aIndex, 
       
  1223     const MVPbkContactLink& aContactLink )
       
  1224     {
       
  1225     TRAP_IGNORE( LoadThumbnailL( aContactLink, aIndex ) );
       
  1226     }
       
  1227 
       
  1228 // --------------------------------------------------------------------------
       
  1229 // CPbk2ThumbnailManager::ContactViewError
       
  1230 // --------------------------------------------------------------------------
       
  1231 //
       
  1232 void CPbk2ThumbnailManager::ContactRemovedFromView(
       
  1233     MVPbkContactViewBase& /*aView*/, 
       
  1234     TInt aIndex, 
       
  1235     const MVPbkContactLink& aContactLink )
       
  1236     {
       
  1237     RemoveThumbnail( aContactLink, aIndex );
       
  1238     }
       
  1239 
       
  1240 // --------------------------------------------------------------------------
       
  1241 // CPbk2ThumbnailManager::ContactViewError
       
  1242 // --------------------------------------------------------------------------
       
  1243 //
       
  1244 void CPbk2ThumbnailManager::ContactViewError(
       
  1245     MVPbkContactViewBase& /*aView*/, 
       
  1246     TInt /*aError*/, 
       
  1247     TBool /*aErrorNotified*/ )
       
  1248     {
       
  1249     // ignored
       
  1250     }
       
  1251 
       
  1252 // --------------------------------------------------------------------------
       
  1253 // CPbk2ThumbnailManager::ReadFieldTypeL
       
  1254 // --------------------------------------------------------------------------
       
  1255 //
       
  1256 void CPbk2ThumbnailManager::ReadFieldTypeL()
       
  1257     {
       
  1258     TResourceReader reader;
       
  1259     CCoeEnv::Static()->CreateResourceReaderLC
       
  1260         ( reader, R_PBK2_THUMBNAIL_FIELD_TYPE );
       
  1261     const TInt count = reader.ReadInt16();
       
  1262 
       
  1263     TVPbkFieldTypeMapping mapping( reader );
       
  1264     iFieldType = mapping.FindMatch( iContactManager.FieldTypes() );
       
  1265     if( !iFieldType )
       
  1266         {
       
  1267         User::Leave( KErrNotFound );
       
  1268         }
       
  1269     CleanupStack::PopAndDestroy(); // CreateResourceReaderLC
       
  1270     }
       
  1271 
       
  1272 
       
  1273 // --------------------------------------------------------------------------
       
  1274 // CPbk2ThumbnailManager::AppendIconL
       
  1275 // --------------------------------------------------------------------------
       
  1276 //
       
  1277 void CPbk2ThumbnailManager::AppendIconL( 
       
  1278     TPbk2IconId& aIcon, CFbsBitmap* aBitmap )
       
  1279     {
       
  1280     // create icon
       
  1281     CGulIcon* icon = CGulIcon::NewLC();
       
  1282     icon->SetBitmap( DuplicateBitmapL( aBitmap ) );
       
  1283     iIconArray->AppendIconL( icon, aIcon );
       
  1284     CleanupStack::Pop( icon );
       
  1285     }
       
  1286 
       
  1287 // end of file