phonebookui/Phonebook2/NamesListExtension/src/cpbk2mycard.cpp
changeset 0 e686773b3f54
child 3 04ab22b956c2
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  .
       
    15 *
       
    16 */
       
    17 
       
    18 // class header
       
    19 #include "cpbk2mycard.h"
       
    20 
       
    21 // Pbk2
       
    22 #include <MPbk2AppUi.h>
       
    23 #include <CPbk2ApplicationServices.h>
       
    24 #include "mpbk2mycardobserver.h"
       
    25 #include <MVPbkFieldType.h>
       
    26 #include <Pbk2InternalUID.h>
       
    27 #include "pbk2nameslistexiconid.hrh"
       
    28 #include "Pbk2NamesListExUID.h"
       
    29 
       
    30 // Virtual Phonebook
       
    31 #include <VPbkContactStoreUris.h>
       
    32 #include <TVPbkContactStoreUriPtr.h>
       
    33 #include <MVPbkContactStore.h>
       
    34 #include <MVPbkContactStore2.h>
       
    35 #include <CVPbkContactManager.h>
       
    36 #include <MVPbkContactStoreList.h>
       
    37 #include <MVPbkContactOperationBase.h>
       
    38 #include <MVPbkContactLink.h>
       
    39 #include <MVPbkStoreContact.h>
       
    40 
       
    41 // thumbnail loading
       
    42 #include <CPbk2ImageManager.h>
       
    43 #include <TPbk2ImageManagerParams.h>
       
    44 #include <VPbkEng.rsg>
       
    45 #include <fbs.h>
       
    46 #include <AknUtils.h>
       
    47 #include <aknlayoutscalable_apps.cdl.h>
       
    48 
       
    49 TSize CalculateThumbnailSize()
       
    50     {
       
    51     TAknWindowComponentLayout layout( 
       
    52             TAknWindowComponentLayout::Compose(
       
    53         AknLayoutScalable_Apps::list_double_large_graphic_phob2_pane( 0 ),
       
    54         AknLayoutScalable_Apps::list_double_large_graphic_phob2_pane_g1( 0 ) ) );    
       
    55     
       
    56     TRect mainPane;
       
    57     AknLayoutUtils::LayoutMetricsRect(
       
    58         AknLayoutUtils::EMainPane, mainPane );
       
    59     TAknLayoutRect layoutRect;
       
    60     layoutRect.LayoutRect(
       
    61         mainPane,
       
    62         layout.LayoutLine() );
       
    63     return layoutRect.Rect().Size();
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CPbk2MyCard::NewL
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 CPbk2MyCard* CPbk2MyCard::NewL( MVPbkContactStore& aPhoneStore )
       
    71     {
       
    72     CPbk2MyCard* self = new (ELeave) CPbk2MyCard( aPhoneStore );
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CPbk2MyCard::ConstructL
       
    81 // --------------------------------------------------------------------------
       
    82 //
       
    83 void CPbk2MyCard::ConstructL()
       
    84     {
       
    85 	// first get application services and contact manager
       
    86 	iAppServices = CPbk2ApplicationServices::InstanceL();
       
    87 	iContactManager = &iAppServices->ContactManager();
       
    88 	
       
    89 	// create image manager for fetching the thumbnail
       
    90 	iImageManager = CPbk2ImageManager::NewL( *iContactManager );
       
    91     iPhoneStore->OpenL(*this);
       
    92     }
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // CPbk2MyCard::CPbk2MyCard
       
    96 // --------------------------------------------------------------------------
       
    97 //
       
    98 CPbk2MyCard::CPbk2MyCard( MVPbkContactStore& aPhoneStore )
       
    99 :iMyCardState( EUnkown ), 
       
   100  iPhoneStore( &aPhoneStore ),
       
   101  iIconId( TUid::Uid( KPbk2ControlCmdItemExtensionUID ), 1 ),
       
   102  iEmptyId( TUid::Uid( KPbk2NamesListExtPluginUID2 ), 
       
   103              EPbk2EceNoMyCardThumbnailIconId )
       
   104     {
       
   105     }
       
   106 
       
   107 // --------------------------------------------------------------------------
       
   108 // CPbk2MyCard::~CPbk2MyCard
       
   109 // --------------------------------------------------------------------------
       
   110 //
       
   111 CPbk2MyCard::~CPbk2MyCard()
       
   112     {
       
   113 	
       
   114 	
       
   115 	delete iThumbBitmap;
       
   116 	delete iMyCardStoreContact;
       
   117 	delete iImageManager;
       
   118     delete iMyCardLink;
       
   119     delete iOperation;
       
   120     Release( iAppServices );
       
   121     iObserverArray.Reset();
       
   122     if(iPhoneStore)
       
   123         {
       
   124         iPhoneStore->Close(*this);
       
   125         }
       
   126     
       
   127     }
       
   128 
       
   129 // --------------------------------------------------------------------------
       
   130 // CPbk2MyCard::SetObserverL
       
   131 // --------------------------------------------------------------------------
       
   132 //
       
   133 void CPbk2MyCard::SetObserverL(MPbk2MyCardObserver& aObserver)
       
   134 	{
       
   135 	iObserverArray.AppendL(&aObserver);
       
   136 	}
       
   137 
       
   138 // --------------------------------------------------------------------------
       
   139 // CPbk2MyCard::RemoveObserver
       
   140 // --------------------------------------------------------------------------
       
   141 //
       
   142 void CPbk2MyCard::RemoveObserver(MPbk2MyCardObserver& aObserver)
       
   143 	{
       
   144 	const TInt index(iObserverArray.Find(&aObserver));
       
   145 	if(index >= 0)
       
   146 		{
       
   147 		iObserverArray.Remove(index);
       
   148 		}
       
   149 	}
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2MyCard::NotifyObservers
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 void CPbk2MyCard::NotifyObservers( MPbk2MyCardObserver::TMyCardStatusEvent aEvent )
       
   156 	{
       
   157 	const TInt count(iObserverArray.Count());
       
   158 	for(TInt i = 0 ; i < count ; ++i)
       
   159 		{
       
   160 		iObserverArray[i]->MyCardEvent( aEvent );	
       
   161 		}
       
   162 	}
       
   163 
       
   164 // --------------------------------------------------------------------------
       
   165 // CPbk2MyCard::MyCardLink
       
   166 // --------------------------------------------------------------------------
       
   167 //
       
   168 MVPbkContactLink* CPbk2MyCard::MyCardLink() const
       
   169 	{
       
   170 	return iMyCardLink;
       
   171 	}
       
   172 
       
   173 // --------------------------------------------------------------------------
       
   174 // CPbk2MyCard::MyCardBitmap
       
   175 // --------------------------------------------------------------------------
       
   176 //
       
   177 CFbsBitmap* CPbk2MyCard::MyCardBitmap() const
       
   178 	{
       
   179 	// if fetched
       
   180 	if( iThumbBitmap )
       
   181 		{
       
   182 		// create duplicate bitmap
       
   183 		CFbsBitmap* dstbitmap = new (ELeave) CFbsBitmap;
       
   184 		CleanupStack::PushL( dstbitmap );
       
   185 	
       
   186 		//Get the handle to source bitmap
       
   187 		TInt srchandle = iThumbBitmap->Handle();
       
   188 		
       
   189 		//Duplicate the bitmap handle. Increases the RefCount of bitmap 
       
   190 		//managed but Fbs Server
       
   191 		User::LeaveIfError( dstbitmap->Duplicate( srchandle ) );
       
   192 		CleanupStack::Pop( dstbitmap );
       
   193 		return dstbitmap;
       
   194 		}
       
   195 	return NULL;
       
   196 	}
       
   197 
       
   198 // --------------------------------------------------------------------------
       
   199 // CPbk2MyCard::MyCardIconId
       
   200 // --------------------------------------------------------------------------
       
   201 //
       
   202 const TPbk2IconId& CPbk2MyCard::MyCardIconId() const
       
   203 	{
       
   204 	// if my card thumbnail exists
       
   205 	if( iThumbBitmap )
       
   206 		{
       
   207 		return iIconId;
       
   208 		}
       
   209 	return iEmptyId;
       
   210 	}
       
   211 
       
   212 // --------------------------------------------------------------------------
       
   213 // CPbk2MyCard::MyCardStoreContact
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 MVPbkStoreContact* CPbk2MyCard::MyCardStoreContact() const
       
   217     {
       
   218     return iMyCardStoreContact;
       
   219     }
       
   220 
       
   221 // --------------------------------------------------------------------------
       
   222 // CPbk2MyCard::VPbkSingleContactLinkOperationComplete
       
   223 // --------------------------------------------------------------------------
       
   224 //
       
   225 void CPbk2MyCard::VPbkSingleContactLinkOperationComplete(
       
   226         MVPbkContactOperationBase& /*aOperation*/,
       
   227         MVPbkContactLink* aLink )
       
   228     {
       
   229     // iOperation still owns the contact link until this method returns.
       
   230     // we can't delete iOperation here
       
   231     delete iMyCardLink;
       
   232     iMyCardLink = aLink;
       
   233     iMyCardState = EExisting;
       
   234     
       
   235     NotifyObservers( MPbk2MyCardObserver::EStateStatusResolved );
       
   236     
       
   237     // fetch store contact
       
   238     iRetrieveOperation = iContactManager->RetrieveContactL( *iMyCardLink, *this );
       
   239     }
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // CPbk2MyCard::VPbkSingleContactLinkOperationFailed
       
   243 // --------------------------------------------------------------------------
       
   244 //
       
   245 void CPbk2MyCard::VPbkSingleContactLinkOperationFailed(
       
   246         MVPbkContactOperationBase& /*aOperation*/,
       
   247         TInt /*aError*/ )
       
   248     {
       
   249     delete iOperation;
       
   250     iOperation = NULL;
       
   251     delete iMyCardLink;
       
   252     iMyCardLink = NULL;
       
   253     iMyCardState = ENonExisting;
       
   254     
       
   255     NotifyObservers( MPbk2MyCardObserver::EStateStatusResolved );
       
   256     }
       
   257 
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // CPbk2MyCard::VPbkSingleContactOperationComplete
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CPbk2MyCard::VPbkSingleContactOperationComplete(
       
   264         MVPbkContactOperationBase& /*aOperation*/,
       
   265         MVPbkStoreContact* aContact )
       
   266     {
       
   267     delete iRetrieveOperation;
       
   268     iRetrieveOperation = NULL;
       
   269     
       
   270     delete iMyCardStoreContact;
       
   271     iMyCardStoreContact = aContact;
       
   272     
       
   273     // load thumbnail
       
   274     LoadThumbnailL( *iMyCardStoreContact );
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // CPbk2MyCard::VPbkSingleContactOperationFailed
       
   279 // ---------------------------------------------------------------------------
       
   280 //
       
   281 void CPbk2MyCard::VPbkSingleContactOperationFailed(
       
   282         MVPbkContactOperationBase& /*aOperation*/, 
       
   283         TInt /*aError*/ )
       
   284     {
       
   285 	// cannot fetch store contact
       
   286     delete iRetrieveOperation;
       
   287     iRetrieveOperation = NULL;
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // CPbk2MyCard::Pbk2ImageGetComplete
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CPbk2MyCard::Pbk2ImageGetComplete(
       
   295     MPbk2ImageOperation& /*aOperation*/,
       
   296     CFbsBitmap* aBitmap )
       
   297     {
       
   298 	delete iImageOperation;
       
   299 	iImageOperation = NULL;
       
   300 	//delete existing
       
   301 	delete iThumbBitmap;
       
   302 	iThumbBitmap = aBitmap;
       
   303 	
       
   304 	// inform observers
       
   305 	NotifyObservers( MPbk2MyCardObserver::EStateThumbnailLoaded );
       
   306     }
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // CPbk2MyCard::Pbk2ImageGetFailed
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 void CPbk2MyCard::Pbk2ImageGetFailed( 
       
   313     MPbk2ImageOperation& /*aOperation*/,
       
   314     TInt /*aError*/ )
       
   315     {
       
   316 	// image fetching failed
       
   317     delete iImageOperation;
       
   318     iImageOperation = NULL;
       
   319     // inform observers
       
   320     NotifyObservers( MPbk2MyCardObserver::EStateThumbnailNotFound );
       
   321     }
       
   322 
       
   323 // --------------------------------------------------------------------------
       
   324 // CPbk2MyCard::LoadThumbnailL
       
   325 // --------------------------------------------------------------------------
       
   326 //
       
   327 void CPbk2MyCard::LoadThumbnailL( MVPbkBaseContact& aContact )
       
   328 	{
       
   329 	// cancel previous operations
       
   330 	delete iImageOperation;
       
   331 	iImageOperation = NULL;
       
   332 
       
   333 	const MVPbkFieldType* thumbType = iContactManager->FieldTypes().Find(
       
   334 			R_VPBK_FIELD_TYPE_THUMBNAILPIC );
       
   335     // if there's picture field
       
   336 	if( thumbType )
       
   337 		{
       
   338 		if( iImageManager->HasImage( aContact, *thumbType ) )
       
   339 			{
       
   340 			TSize size( CalculateThumbnailSize() );
       
   341 		
       
   342 			// Define parameters for thumbnail
       
   343 			TPbk2ImageManagerParams params;
       
   344 			TInt useCropping = 0x0008;
       
   345 			// set params
       
   346 			params.iSize = size;
       
   347 			params.iFlags = TPbk2ImageManagerParams::EScaleImage |
       
   348 							TPbk2ImageManagerParams::EKeepAspectRatio |
       
   349 							useCropping;	//CROP IMAGE	
       
   350 			// contact has image. load it.
       
   351 			iImageOperation = iImageManager->GetImageAsyncL( 
       
   352 				&params, aContact, *thumbType, *this );
       
   353 			}
       
   354 		// no image
       
   355 		else
       
   356 			{
       
   357 			// reset thumbnail
       
   358 			delete iThumbBitmap;
       
   359 			iThumbBitmap = NULL;
       
   360 			// inform observers
       
   361 			NotifyObservers( MPbk2MyCardObserver::EStateThumbnailNotFound );
       
   362 			}
       
   363 		}
       
   364 	}
       
   365 
       
   366 // --------------------------------------------------------------------------
       
   367 // CPbk2MyCard::StoreReady
       
   368 // --------------------------------------------------------------------------
       
   369 //
       
   370 void CPbk2MyCard::StoreReady( MVPbkContactStore& aContactStore )
       
   371     {
       
   372     delete iOperation;
       
   373     iOperation = NULL;
       
   374     
       
   375     MVPbkContactStore2* phoneStoreExtension =
       
   376         static_cast<MVPbkContactStore2*>(aContactStore.ContactStoreExtension(
       
   377                 KMVPbkContactStoreExtension2Uid));
       
   378     if ( phoneStoreExtension )
       
   379 		{
       
   380 		TRAPD( err, iOperation = phoneStoreExtension->OwnContactLinkL(*this) );
       
   381 		if( err )
       
   382 			{
       
   383 			// TODO: How is this handled?
       
   384 			}
       
   385 		}
       
   386     }
       
   387 
       
   388 // --------------------------------------------------------------------------
       
   389 // CPbk2MyCard::StoreUnavailable
       
   390 // --------------------------------------------------------------------------
       
   391 //
       
   392 void CPbk2MyCard::StoreUnavailable(
       
   393         MVPbkContactStore& /*aContactStore*/,
       
   394         TInt /*aReason*/ )
       
   395     {
       
   396     }
       
   397 
       
   398 // --------------------------------------------------------------------------
       
   399 // CPbk2MyCard::HandleStoreEventL
       
   400 // --------------------------------------------------------------------------
       
   401 //
       
   402 void CPbk2MyCard::HandleStoreEventL(
       
   403         MVPbkContactStore& /*aContactStore*/,
       
   404         TVPbkContactStoreEvent aStoreEvent )
       
   405     {
       
   406 	// my card is not set yet
       
   407     if( !iMyCardLink &&
       
   408         aStoreEvent.iContactLink &&
       
   409         ( aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactAdded || 
       
   410           aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactChanged ) ) 
       
   411         {
       
   412 		// check is new or changed contact own contact
       
   413         delete iOperation;
       
   414         iOperation = NULL;
       
   415         
       
   416         MVPbkContactStore2* phoneStoreExtension =
       
   417             static_cast<MVPbkContactStore2*>(iPhoneStore->ContactStoreExtension(
       
   418                     KMVPbkContactStoreExtension2Uid));
       
   419         if ( phoneStoreExtension )
       
   420         	{
       
   421 			iOperation = phoneStoreExtension->OwnContactLinkL( *this );
       
   422         	}
       
   423         }
       
   424     else if( iMyCardLink && aStoreEvent.iContactLink )
       
   425     	{
       
   426    		if( aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactDeleted )
       
   427 			{
       
   428 			// Check is MyCard deleted
       
   429 			if( iMyCardLink->IsSame( *aStoreEvent.iContactLink ) )
       
   430 				{
       
   431 				delete iMyCardLink;
       
   432 				iMyCardLink = NULL;
       
   433 				iMyCardState = ENonExisting;
       
   434 				NotifyObservers( MPbk2MyCardObserver::EStateStatusResolved );
       
   435 				
       
   436 				// remove thumbnail and inform
       
   437 				delete iThumbBitmap;
       
   438 				iThumbBitmap = NULL;
       
   439 				// inform also that thumbnail is removed
       
   440 				NotifyObservers( MPbk2MyCardObserver::EStateThumbnailNotFound );
       
   441 				}
       
   442 			}
       
   443    		// contact has changed, check the thumbnail
       
   444    		else if( aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactChanged  )
       
   445    			{
       
   446 			// Check is MyCard changed
       
   447 			if( iMyCardLink->IsSame( *aStoreEvent.iContactLink ) )
       
   448 				{
       
   449 				// fetch store contact and check thumbnail
       
   450 				iRetrieveOperation = 
       
   451 						iContactManager->RetrieveContactL( *iMyCardLink, *this );
       
   452 				}
       
   453    			}
       
   454     	}// else if
       
   455     }
       
   456 
       
   457 // end of file