uiservicetab/vimpststorage/src/cvimpststoragevpbkstorehandler.cpp
branchRCL_3
changeset 28 3104fc151679
parent 27 2b7283837edb
child 29 9a48e301e94b
equal deleted inserted replaced
27:2b7283837edb 28:3104fc151679
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Class that provides handling of vpbk store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cvimpststoragevpbkstorehandler.h"
       
    21 
       
    22 #include "mvimpststoragevpbkstorehandler.h"
       
    23 
       
    24 //vpbk interfaces
       
    25 #include <MVPbkContactStoreObserver.h>
       
    26 #include <CVPbkContactManager.h>
       
    27 #include <MVPbkContactLink.h>
       
    28 #include <CVPbkContactStoreUriArray.h>
       
    29 #include <MVPbkContactOperationBase.h>
       
    30 #include <MVPbkContactStore.h>
       
    31 #include <MVPbkContactStoreProperties.h>
       
    32 #include <MVPbkContactView.h>
       
    33 #include <MVPbkContactStoreList.h>
       
    34 #include <MVPbkContactFieldTextData.h>
       
    35 #include <MVPbkContactFieldUriData.h>
       
    36 #include <MVPbkContactFieldBinaryData.h>
       
    37 #include <MVPbkContactFieldData.h>
       
    38 #include <CVPbkContactViewDefinition.h>
       
    39 
       
    40 #include <MVPbkViewContact.h>
       
    41 #include <MVPbkStoreContact.h>
       
    42 #include <TVPbkContactStoreUriPtr.h>
       
    43 #include <MVPbkStoreContactFieldCollection.h>
       
    44 #include <MVPbkStoreContactField.h>
       
    45 #include <MVPbkFieldType.h>
       
    46 #include <VPbkEng.rsg>
       
    47 #include <CVPbkContactLinkArray.h>
       
    48 #include <CVPbkFieldTypeSelector.h>
       
    49 #include <CPbk2SortOrderManager.h>
       
    50 #include <MVPbkContactFieldBinaryData.h>
       
    51 
       
    52 #include "uiservicetabtracer.h"
       
    53 #include "tvimpstconsts.h"
       
    54 #include <cntdb.h>
       
    55 
       
    56 // ================= MEMBER FUNCTIONS =======================
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CVIMPSTStorageVPbkStoreHandler::
       
    60 //      CVIMPSTStorageVPbkStoreHandler
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CVIMPSTStorageVPbkStoreHandler:: CVIMPSTStorageVPbkStoreHandler( MVIMPSTStorageVPbkStoreHandler& aStoreHandler,TBool aLocalStore  ):
       
    64 	CActive( CActive::EPriorityIdle ),
       
    65 	iStoreHandler(aStoreHandler ),
       
    66 	iStoreEventType( EVPbkUnknownEvent ),
       
    67 	iFetcingIndex( 0 ),
       
    68 	iLocalStore( aLocalStore )
       
    69 	{
       
    70 	TRACER_AUTO;
       
    71     CActiveScheduler::Add( this );
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CVIMPSTStorageVPbkStoreHandler::ConstructL
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 void CVIMPSTStorageVPbkStoreHandler::ConstructL(const TDesC& aStoreName,const TDesC& aServiceName )
       
    79     { 
       
    80 	TRACER_AUTO;
       
    81     iAddIdInProgress = NULL;
       
    82     iStoreName = aStoreName.AllocL();
       
    83     iServiceName = aServiceName.AllocL();
       
    84     CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
       
    85     uriArray->AppendL( TVPbkContactStoreUriPtr( aStoreName ) );    
       
    86     if( !uriArray->IsIncluded( TVPbkContactStoreUriPtr( KPhoneBookDbURI ) ) )
       
    87 	    {
       
    88 	    TRACE( "phonebook store apended" );
       
    89 	    uriArray->AppendL( TVPbkContactStoreUriPtr( KPhoneBookDbURI )   );	
       
    90 	    }
       
    91     iContactManager = CVPbkContactManager::NewL( *uriArray, NULL );
       
    92     CleanupStack::PopAndDestroy( uriArray );
       
    93 	TRACE( "iContactManager created" );
       
    94     iStores = &iContactManager->ContactStoresL();
       
    95     iStores->OpenAllL( *this );
       
    96     TRACE( " OpenAllL called" );
       
    97     iSortOrderManager = CPbk2SortOrderManager::NewL( iContactManager->FieldTypes(), NULL ); 
       
    98    
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CVIMPSTStorageVPbkStoreHandler::NewL
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 CVIMPSTStorageVPbkStoreHandler* CVIMPSTStorageVPbkStoreHandler::NewL( const TDesC& aContactDb,
       
   106 																	const TDesC& aServiceName,
       
   107 													                MVIMPSTStorageVPbkStoreHandler& aStoreHandler,
       
   108 													                TBool aLocalStore  )
       
   109 	{
       
   110 	TRACER_AUTO;
       
   111 	CVIMPSTStorageVPbkStoreHandler* self = NewLC( aContactDb ,aServiceName, aStoreHandler,aLocalStore );
       
   112 	CleanupStack::Pop( self );
       
   113 	
       
   114 	return self;
       
   115 	}
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CVIMPSTStorageVPbkStoreHandler::NewLC
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 CVIMPSTStorageVPbkStoreHandler* CVIMPSTStorageVPbkStoreHandler::NewLC( const TDesC& aContactDb,
       
   122 																	const TDesC& aServiceName,
       
   123 																	MVIMPSTStorageVPbkStoreHandler& aStoreHandler,
       
   124 																	TBool aLocalStore   )
       
   125     {
       
   126 	TRACER_AUTO;
       
   127     CVIMPSTStorageVPbkStoreHandler* self =
       
   128         new (ELeave) CVIMPSTStorageVPbkStoreHandler( aStoreHandler ,aLocalStore );
       
   129     CleanupStack::PushL( self );
       
   130     self->ConstructL( aContactDb,aServiceName );
       
   131    
       
   132     return self;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // CVIMPSTStorageVPbkStoreHandler::
       
   137 //      ~CVIMPSTStorageVPbkStoreHandler
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 CVIMPSTStorageVPbkStoreHandler:: ~CVIMPSTStorageVPbkStoreHandler()
       
   141     {
       
   142 	TRACER_AUTO;
       
   143     if( iWait.IsStarted() )
       
   144 	    {
       
   145 	    iWait.AsyncStop();	
       
   146 	    }
       
   147    	Cancel();
       
   148    	ResetAndDestroyRArray();
       
   149    	iUnCommittedVPbkContacts.ResetAndDestroy();
       
   150    	delete iAddIdInProgress;
       
   151    	delete iContactOperation;
       
   152     delete iContactLink;
       
   153     delete iStoreContact;
       
   154     delete iContactView;
       
   155     if ( iContactManager )
       
   156         {
       
   157         TRAP_IGNORE( iContactManager->ContactStoresL().CloseAll( *this ) );
       
   158         TRACE( "CloseAll called" );
       
   159         delete iContactManager;
       
   160         }
       
   161     delete iStoreName;
       
   162     delete iSortOrderManager;
       
   163     delete iServiceName;
       
   164    
       
   165     }
       
   166  // ---------------------------------------------------------------------------
       
   167 // CVIMPSTStorageVPbkStoreHandler::ContactViewReady
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CVIMPSTStorageVPbkStoreHandler::ContactViewReady( 
       
   171     MVPbkContactViewBase& aView )
       
   172     {
       
   173 	TRACER_AUTO;
       
   174     if ( iContactView == &aView && ( iStoreEventType == EVPbkUnknownEvent ) )
       
   175         { 
       
   176         iStoreEventType = EVPbkContactReading ;          
       
   177         TRAP_IGNORE( HandleContactViewReadyL( *iContactView ) );
       
   178         }
       
   179    
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CVIMPSTStorageVPbkStoreHandler::ContactViewUnavailable
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void CVIMPSTStorageVPbkStoreHandler::ContactViewUnavailable( 
       
   187     MVPbkContactViewBase& /*aView*/ )
       
   188     {
       
   189 	TRACER_AUTO;
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // CVIMPSTStorageVPbkStoreHandler::ContactAddedToView
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 void CVIMPSTStorageVPbkStoreHandler::ContactAddedToView( 
       
   197     MVPbkContactViewBase& /*aView*/, 
       
   198     TInt /*aIndex*/, const MVPbkContactLink& /*aContactLink*/ )
       
   199     {
       
   200 	TRACER_AUTO;
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CVIMPSTStorageVPbkStoreHandler::ContactRemovedFromView
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CVIMPSTStorageVPbkStoreHandler::ContactRemovedFromView( 
       
   208     MVPbkContactViewBase& /*aView*/, 
       
   209     TInt /*aIndex*/, const MVPbkContactLink& /*aContactLink*/ )
       
   210     {
       
   211 	TRACER_AUTO;
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // CVIMPSTStorageVPbkStoreHandler::ContactViewError
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 void CVIMPSTStorageVPbkStoreHandler::ContactViewError( 
       
   219     MVPbkContactViewBase& /*aView*/, 
       
   220     TInt /*aError*/, TBool /*aErrorNotified*/ )
       
   221     {
       
   222 	TRACER_AUTO;
       
   223     }
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // CVIMPSTStorageVPbkStoreHandler::StoreReady
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CVIMPSTStorageVPbkStoreHandler::StoreReady( 
       
   230     MVPbkContactStore& /*aContactStore*/ )
       
   231     {
       
   232 	TRACER_AUTO;
       
   233     }
       
   234     
       
   235 // ---------------------------------------------------------------------------
       
   236 // CVIMPSTStorageVPbkStoreHandler::StoreUnavailable
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CVIMPSTStorageVPbkStoreHandler::StoreUnavailable( 
       
   240     MVPbkContactStore& /*aContactStore*/, 
       
   241     TInt /*aReason*/ )
       
   242     {
       
   243 	TRACER_AUTO;
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // CVIMPSTStorageVPbkStoreHandler::HandleStoreEventL
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 MVPbkContactStore* CVIMPSTStorageVPbkStoreHandler::GetDefaultStoreL( const TDesC& aUri )
       
   251 	 {
       
   252 	TRACER_AUTO;
       
   253 	 return iStores->Find( TVPbkContactStoreUriPtr( aUri ) );	
       
   254 	 }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CVIMPSTStorageVPbkStoreHandler::HandleStoreEventL
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 void CVIMPSTStorageVPbkStoreHandler::HandleStoreEventL( 
       
   261     MVPbkContactStore& aContactStore , 
       
   262     TVPbkContactStoreEvent aStoreEvent )
       
   263     {
       
   264 	TRACER_AUTO;
       
   265     if( iStore == &aContactStore )
       
   266 	    {
       
   267 	    TRACE( "store event" );
       
   268 	  	switch( aStoreEvent.iEventType )
       
   269 		    {
       
   270 		    case TVPbkContactStoreEvent::EContactAdded:
       
   271 			    {
       
   272 			    TRACE( "EContactAdded event");
       
   273 			    delete iContactLink;
       
   274 			    iContactLink = NULL;
       
   275 				MVPbkContactLink* contactLink = aStoreEvent.iContactLink->CloneLC();
       
   276 			 	iContactLink = 	contactLink;
       
   277 				CleanupStack::Pop(); // contactLink
       
   278 				IssueRequest(); // complete the request	
       
   279 				TRACE( " EContactAdded done" );
       
   280 			    break;	
       
   281 			    }
       
   282 			case TVPbkContactStoreEvent::EContactDeleted:
       
   283 			    {
       
   284 			    TRACE( "EContactDeleted event" );
       
   285 			   	MVPbkContactLink* contactLink = aStoreEvent.iContactLink->CloneLC();
       
   286 			   	delete iContactLink;
       
   287 		    	iContactLink = NULL;
       
   288 		 		iContactLink = 	contactLink;
       
   289 		 		CleanupStack::Pop(); // contactLink
       
   290 				TVIMPSTVPbkStoreEvent storeEvent(EVPbkContactDeleted, iContactLink );
       
   291 		    	iStoreHandler.HandleVPbkStoreEventL( storeEvent ) ;	
       
   292 		    	TRACE( "EContactDeleted done" );
       
   293 			    break;	
       
   294 			    }
       
   295 			case TVPbkContactStoreEvent::EContactChanged:
       
   296 				{
       
   297 				TRACE( " EContactChanged event" );
       
   298 				delete iContactLink;
       
   299 				iContactLink = NULL;
       
   300 				MVPbkContactLink* contactLink = aStoreEvent.iContactLink->CloneLC();
       
   301 				iContactLink = 	contactLink;
       
   302 				CleanupStack::Pop(); // contactLink
       
   303 				iStoreEventType = EVPbkContactChanged;
       
   304 				IssueRequest(); // complete the request which will begin retriving the iContactLink
       
   305 				TRACE("EContactChanged done");
       
   306 				break;	
       
   307 				}
       
   308 			case TVPbkContactStoreEvent::EUnknownChanges:
       
   309 			    {
       
   310 			    TRACE( " EUnknownChanges event" );
       
   311 			    iStoreEventType = EVPbkUnknownChanges;
       
   312 			    TVIMPSTVPbkStoreEvent storeEvent(iStoreEventType);
       
   313 			    iStoreHandler.HandleVPbkStoreEventL( storeEvent ) ;
       
   314 			    TRACE( "EUnknownChanges done" );
       
   315 			    break;	
       
   316 			    }
       
   317 		    default:
       
   318 			 	break;
       
   319 		    }
       
   320 	    }
       
   321    
       
   322     }
       
   323 // ---------------------------------------------------------------------------
       
   324 // CVIMPSTStorageVPbkStoreHandler::OpenComplete
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 void CVIMPSTStorageVPbkStoreHandler::OpenComplete()
       
   328     {
       
   329 	TRACER_AUTO;
       
   330     // stores OpenAll completed
       
   331     TRAP_IGNORE( DoHnadleOpenCompleteL() );
       
   332    
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // CVIMPSTStorageVPbkStoreHandler::DoHnadleOpenCompleteL
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 void CVIMPSTStorageVPbkStoreHandler::DoHnadleOpenCompleteL()
       
   340     {
       
   341 	TRACER_AUTO;
       
   342     iStore = GetDefaultStoreL( *iStoreName ) ;
       
   343     CVPbkContactViewDefinition* viewDefinition = 
       
   344         				CVPbkContactViewDefinition::NewL();
       
   345     CleanupStack::PushL( viewDefinition );    
       
   346     viewDefinition->SetType( EVPbkContactsView );
       
   347     viewDefinition->SetUriL( *iStoreName );
       
   348     if ( iStore )
       
   349         {
       
   350         TRACE( "store view creating");
       
   351         MVPbkContactView* tmpView = iStore->CreateViewLC(
       
   352 						            *viewDefinition,
       
   353 						            *this,
       
   354 						            iSortOrderManager->SortOrder() );        
       
   355         CleanupStack::Pop(); // tmpView            
       
   356         TRACE( "store view created");        
       
   357         iContactView = tmpView;
       
   358         }
       
   359     CleanupStack::PopAndDestroy();  //viewDefinition, can not pop ny name
       
   360     
       
   361     }
       
   362 	
       
   363 // ---------------------------------------------------------------------------
       
   364 // CVIMPSTStorageVPbkStoreHandler::ContactOperationCompleted
       
   365 // ---------------------------------------------------------------------------
       
   366 //
       
   367 void CVIMPSTStorageVPbkStoreHandler::ContactOperationCompleted(
       
   368     TContactOpResult /*aResult*/ )
       
   369     {
       
   370 	TRACER_AUTO;
       
   371     // called when contact commit is successful
       
   372     iOperationError = KErrNone;
       
   373     if( iWait.IsStarted() )
       
   374 	    {
       
   375 	    iWait.AsyncStop();	
       
   376 	    }
       
   377     TRAP_IGNORE( CommitNextPendingContactsL() );
       
   378 	
       
   379 	}
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // CVIMPSTStorageVPbkStoreHandler::ContactOperationFailed
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 void CVIMPSTStorageVPbkStoreHandler::ContactOperationFailed( 
       
   386     TContactOp /*aOpCode*/, TInt aErrorCode, TBool /*aErrorNotified*/ )
       
   387     {
       
   388 	TRACER_AUTO;
       
   389     // called when contact commit is unsuccessful
       
   390     iOperationError = aErrorCode;
       
   391     if( iWait.IsStarted() )
       
   392 	    {
       
   393 	    iWait.AsyncStop();	
       
   394 	    }
       
   395     TRAP_IGNORE( CommitNextPendingContactsL() );
       
   396 	
       
   397     }    
       
   398  
       
   399  
       
   400 // --------------------------------------------------------------------------
       
   401 // CVIMPSTStorageVPbkStoreHandler::StepComplete
       
   402 // --------------------------------------------------------------------------
       
   403 //
       
   404 void CVIMPSTStorageVPbkStoreHandler::StepComplete(
       
   405         MVPbkContactOperationBase& /*aOperation*/,
       
   406         TInt /*aStepSize*/ )
       
   407     {
       
   408 	TRACER_AUTO;
       
   409     // called when batch operation call
       
   410     iOperationError = KErrNone;
       
   411     
       
   412     }
       
   413 
       
   414 
       
   415 // --------------------------------------------------------------------------
       
   416 // CVIMPSTStorageVPbkStoreHandler::StepFailed
       
   417 // --------------------------------------------------------------------------
       
   418 //
       
   419 TBool CVIMPSTStorageVPbkStoreHandler::StepFailed(
       
   420         MVPbkContactOperationBase& /*aOperation*/,
       
   421         TInt /*aStepSize*/,
       
   422         TInt aError )
       
   423     {
       
   424 	TRACER_AUTO;
       
   425     // called when batch operation call
       
   426     iOperationError = aError;
       
   427    
       
   428    	return EFalse;
       
   429     }
       
   430 
       
   431 // --------------------------------------------------------------------------
       
   432 // CVIMPSTStorageVPbkStoreHandler::OperationComplete
       
   433 // --------------------------------------------------------------------------
       
   434 //
       
   435 void CVIMPSTStorageVPbkStoreHandler::OperationComplete(
       
   436         MVPbkContactOperationBase& aOperation )
       
   437     {
       
   438 	TRACER_AUTO;
       
   439     // called when batch operation call for opening the multiple store
       
   440     if ( &aOperation == iContactOperation )
       
   441         {
       
   442         delete iContactOperation;
       
   443         iContactOperation = NULL;
       
   444         }
       
   445     iOperationError = KErrNone;
       
   446     if( iWait.IsStarted() )
       
   447 	    {
       
   448 	    iWait.AsyncStop();	
       
   449 	    }
       
   450 	
       
   451     }   
       
   452 
       
   453 //----------------------------------------------------------------------
       
   454 //CVIMPSTStorageVPbkStoreHandler::VPbkSingleContactOperationComplete()
       
   455 //----------------------------------------------------------------------
       
   456 void CVIMPSTStorageVPbkStoreHandler::VPbkSingleContactOperationComplete( MVPbkContactOperationBase& aOperation,
       
   457                                             				MVPbkStoreContact* aContact )
       
   458     {  
       
   459 	TRACER_AUTO;
       
   460     // called when contact details is retrived
       
   461     TRAP_IGNORE( DoHnadleSingleContactOperationCompleteL( aOperation, aContact ) );
       
   462     
       
   463     }
       
   464 
       
   465 //----------------------------------------------------------------------
       
   466 //CVIMPSTStorageVPbkStoreHandler::DoHnadleSingleContactOperationCompleteL()
       
   467 //----------------------------------------------------------------------
       
   468 void CVIMPSTStorageVPbkStoreHandler::DoHnadleSingleContactOperationCompleteL( MVPbkContactOperationBase& aOperation,
       
   469                                             								 MVPbkStoreContact* aContact )
       
   470     {  
       
   471 	TRACER_AUTO;
       
   472     TRACE( "StoreEventType: %d",iStoreEventType);
       
   473     // called when contact details is retrived
       
   474     if( iStoreContact )
       
   475         {
       
   476         delete 	iStoreContact;
       
   477         iStoreContact = NULL;
       
   478         }
       
   479     iStoreContact = aContact ; // iStoreContact  owns thea Contact
       
   480     if( ( &aOperation == iContactOperation) && iStoreContact )
       
   481         {
       
   482         TRACE( "inside if (operation) ");
       
   483         if( iWait.IsStarted() )
       
   484 		    {
       
   485 		    iWait.AsyncStop();
       
   486 		    }
       
   487         delete iContactOperation;
       
   488         iContactOperation = NULL;
       
   489         TRACE( " before iCommitOperationInProgress: %d",iCommitOperationInProgress);
       
   490         iCommitOperationInProgress = EFalse;
       
   491         TRACE( "After iCommitOperationInProgress: %d",iCommitOperationInProgress);
       
   492 
       
   493         if( iStoreEventType != EVPbkUpdatingAvatar )
       
   494             {
       
   495             TRACE( " inside if (iStoreEventType != EVPbkUpdatingAvatar ) ");
       
   496             if( !iLocalStore && iStoreEventType == EVPbkContactRetriving )
       
   497                 {
       
   498                 TRACE( " EVPbkContactRetriving");
       
   499 		    	RetriveFieldDataAndSendEventL( *iStoreContact );
       
   500 			   }
       
   501             else 
       
   502                 {
       
   503                 TRACE( "inside else start");
       
   504                 HBufC* userId = GetContactFieldDataLC( *iStoreContact, R_VPBK_FIELD_TYPE_IMPP );
       
   505                 HBufC* firstname = GetContactFieldDataLC( *iStoreContact,R_VPBK_FIELD_TYPE_FIRSTNAME );
       
   506                 HBufC* lastname = GetContactFieldDataLC( *iStoreContact,R_VPBK_FIELD_TYPE_LASTNAME );
       
   507 
       
   508                 HBufC* disname = NULL;
       
   509                 /*
       
   510                  *	There are three scenarios where I can form the Display name.
       
   511                  *	1. First and last name are present. 
       
   512                  *	2. First name is present but no last name is present.
       
   513                  *	3. Lastname is there and no first name.
       
   514                  *	4. if none, then assigning NULL 
       
   515                  */
       
   516                 if ( firstname && lastname )
       
   517                     {
       
   518                     disname = HBufC::NewLC( firstname->Des().Length() + lastname->Des().Length()+ 1 );
       
   519                     TPtr disnamePtr( disname->Des() );
       
   520                     disnamePtr.Append(*firstname);
       
   521                     if(firstname->Length() && lastname->Length() )
       
   522                         {
       
   523                         disnamePtr.Append(KSpace);
       
   524                         }
       
   525                     disnamePtr.Append(*lastname);
       
   526                     }
       
   527                 else
       
   528                     {
       
   529                     disname = KNullDesC().AllocLC();
       
   530                     }
       
   531                 HBufC8* avatarContent = GetContactBinaryFieldDataLC( *iStoreContact,R_VPBK_FIELD_TYPE_THUMBNAILPIC );
       
   532                 TRACE( "iStoreHandler.HandleVPbkStoreEventL: %d", iStoreEventType);
       
   533                 TPtr useridPtr = userId->Des();
       
   534                 TRACE( "inside else UserId : %S", &useridPtr);
       
   535                 TVIMPSTVPbkStoreEvent storeEvent(iStoreEventType,
       
   536                         iContactLink,
       
   537                         NULL,
       
   538                         *userId,
       
   539                         *disname,
       
   540                         *avatarContent);
       
   541                 iStoreHandler.HandleVPbkStoreEventL( storeEvent ) ;
       
   542                 CleanupStack::PopAndDestroy(5); // disname,lastname,firstname,userId,avatarContent 
       
   543                 TRACE( " inside else end");
       
   544                 }
       
   545             delete iAddIdInProgress;
       
   546             iAddIdInProgress = NULL;
       
   547             }
       
   548 
       
   549         iOperationError = KErrNone;
       
   550         if( iStoreEventType == EVPbkContactReading && iContactView )
       
   551             {
       
   552             TRACE( " if after iOperationError");
       
   553             HandleContactViewReadyL( *iContactView );   
       
   554             }
       
   555         
       
   556         else if(!iCommitOperationInProgress )
       
   557             {
       
   558             TRACE( "else if after if after iOperationError");
       
   559             CommitNextPendingContactsL();// commit only if any pending contact
       
   560             }
       
   561        
       
   562         }
       
   563     }
       
   564 //----------------------------------------------------------------------
       
   565 //CVIMPSTStorageVPbkStoreHandler::VPbkSingleContactOperationComplete()
       
   566 //----------------------------------------------------------------------
       
   567 void CVIMPSTStorageVPbkStoreHandler::VPbkSingleContactOperationFailed( MVPbkContactOperationBase& aOperation,
       
   568                                                                    TInt aError)
       
   569     {
       
   570 	TRACER_AUTO;
       
   571 	if( &aOperation == iContactOperation )
       
   572         {
       
   573         delete iContactOperation;
       
   574         iContactOperation = NULL;
       
   575         }
       
   576     iOperationError = aError;
       
   577     iCommitOperationInProgress = EFalse;
       
   578     if( iWait.IsStarted() )
       
   579 	    {
       
   580 	    iWait.AsyncStop();	
       
   581 	    }
       
   582     TRAP_IGNORE( CommitNextPendingContactsL() );// commit only if any pending contact
       
   583 	
       
   584     }
       
   585 // --------------------------------------------------------------------------
       
   586 // CVIMPSTStorageVPbkStoreHandler::HandleContactViewReadyL
       
   587 // --------------------------------------------------------------------------
       
   588 //
       
   589 void CVIMPSTStorageVPbkStoreHandler::HandleContactViewReadyL( MVPbkContactViewBase& aView  )
       
   590 	{
       
   591 	TRACER_AUTO;
       
   592 	TInt contactCount = aView.ContactCountL();
       
   593 	TRACE( "begin");
       
   594 	if( contactCount == iFetcingIndex  && iStoreEventType == EVPbkContactReading )
       
   595 		{
       
   596 		TRACE( "reading completed" );
       
   597 		iStoreEventType = EVPbkContactReadCompleted;
       
   598 		TVIMPSTVPbkStoreEvent storeEvent(iStoreEventType);
       
   599 		iStoreHandler.HandleVPbkStoreEventL( storeEvent ) ;
       
   600 		}
       
   601 	else if( iFetcingIndex < contactCount && iFetcingIndex>=0 )
       
   602 		{
       
   603 		TRACE( " reading");
       
   604 		const MVPbkViewContact& contact =  aView.ContactAtL( iFetcingIndex );
       
   605 		MVPbkContactLink* contactLink = contact.CreateLinkLC();
       
   606 		delete iContactLink;
       
   607 		iContactLink = NULL;
       
   608 		iContactLink = 	contactLink;
       
   609 		CleanupStack::Pop(); // contactLink
       
   610 		IssueRequest();
       
   611 		iFetcingIndex++;
       
   612 		}
       
   613 	
       
   614 	}
       
   615 // --------------------------------------------------------------------------
       
   616 // CVIMPSTStorageVPbkStoreHandler::IssueRequest
       
   617 // --------------------------------------------------------------------------
       
   618 //
       
   619 void CVIMPSTStorageVPbkStoreHandler::IssueRequest()
       
   620     {
       
   621 	TRACER_AUTO;
       
   622     if( !IsActive() )
       
   623 	    {
       
   624 	    TRequestStatus* status = &iStatus;
       
   625 	    User::RequestComplete( status, KErrNone );
       
   626 	    SetActive();
       
   627 	    }
       
   628 	
       
   629     }
       
   630 	
       
   631 // --------------------------------------------------------------------------
       
   632 // CVIMPSTStorageVPbkStoreHandler::RunL
       
   633 // --------------------------------------------------------------------------
       
   634 //
       
   635 void CVIMPSTStorageVPbkStoreHandler::RunL()
       
   636     {
       
   637 	TRACER_AUTO;
       
   638     if( iContactLink )
       
   639 	    {
       
   640 	    delete iContactOperation;
       
   641 	    iContactOperation = NULL;
       
   642 	    iContactOperation = iContactManager->RetrieveContactL( *iContactLink, *this );	
       
   643 	    }
       
   644 	
       
   645     }
       
   646 
       
   647 // --------------------------------------------------------------------------
       
   648 // CVIMPSTStorageVPbkStoreHandler::DoCancel
       
   649 // --------------------------------------------------------------------------
       
   650 //
       
   651 void CVIMPSTStorageVPbkStoreHandler::DoCancel()
       
   652     {
       
   653 	TRACER_AUTO;
       
   654     }
       
   655 
       
   656 // --------------------------------------------------------------------------
       
   657 // CVIMPSTStorageVPbkStoreHandler::RunError
       
   658 // --------------------------------------------------------------------------
       
   659 //
       
   660 TInt CVIMPSTStorageVPbkStoreHandler::RunError( TInt aError )
       
   661     {  
       
   662 	TRACER_AUTO;
       
   663     return aError;
       
   664     }
       
   665 
       
   666 //////////////////////////////////////////////////////////
       
   667 // ---------------------------------------------------------------------------
       
   668 // CVIMPSTStorageVPbkStoreHandler::CreateVPbkContactL
       
   669 // ---------------------------------------------------------------------------
       
   670 // 
       
   671 TInt CVIMPSTStorageVPbkStoreHandler::CreateVPbkContactL(const TDesC& aUserId, 
       
   672         						 						const TDesC& aDisplayName )   
       
   673 	{
       
   674 	TRACER_AUTO;
       
   675 	TRACE( "aUserId = %S", &aUserId );
       
   676 	
       
   677 	if( iAddIdInProgress && iAddIdInProgress->Compare( aUserId ) == 0 )
       
   678 		{
       
   679 		TPtr iAddIdInProgressPtr = iAddIdInProgress->Des();
       
   680 		TRACE("iAddIdInProgressPtr = %S", &iAddIdInProgressPtr );
       
   681 		TRACE("already Adding" );
       
   682 		return KErrAlreadyExists;	
       
   683 		}
       
   684 	TRACE( "After If" );
       
   685 	delete iAddIdInProgress;
       
   686 	iAddIdInProgress = NULL;
       
   687 	iAddIdInProgress = aUserId.AllocL();
       
   688 	
       
   689     if(iStore)
       
   690 		{
       
   691 		MVPbkStoreContact* storedContact = iStore->CreateNewContactLC(); //1
       
   692 		if( !storedContact )   
       
   693 			{
       
   694 			return NULL;    
       
   695 			}
       
   696 		if(aUserId.Length()!= 0)
       
   697 			{
       
   698 			TRACE( " valid xsp Id" );
       
   699 			TInt prefixLocation = aUserId.Locate( ':' );
       
   700 			const MVPbkContactStoreProperties& storeProperties =  storedContact->ParentStore().StoreProperties();
       
   701 			const MVPbkFieldTypeList& supportedFieldTypes = storeProperties.SupportedFields();
       
   702 			TInt fieldCount = supportedFieldTypes.FieldTypeCount();
       
   703 
       
   704 			for( TInt index = 0 ; index < fieldCount ; index++ )
       
   705 				{        
       
   706 				const MVPbkFieldType* fieldType = &(supportedFieldTypes.FieldTypeAt(index));
       
   707 				if ( fieldType && ( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_IMPP ))
       
   708 					{
       
   709 					TRACE(" XSP Id-IMMP field creating" );
       
   710 					MVPbkStoreContactField* fielduri = storedContact->CreateFieldLC(*fieldType);  //2
       
   711 					// Check for prefix and remove if found
       
   712 					//append the internettelephoney field.
       
   713 					HBufC* fieldData = NULL;
       
   714 					TInt serFieldLength = aUserId.Length();
       
   715 					if( prefixLocation >=0 && ( prefixLocation < serFieldLength ) )
       
   716 						{
       
   717 						fieldData = aUserId.Right(serFieldLength - prefixLocation - 1).AllocLC(); //3
       
   718 						}
       
   719 					else
       
   720 						{
       
   721 						fieldData = aUserId.AllocLC(); // 3
       
   722 						}
       
   723 					if (fieldData)
       
   724 						{
       
   725 						TPtr fieldDataPtr( fieldData->Des() );
       
   726 						MVPbkContactFieldUriData& address = MVPbkContactFieldUriData::Cast( fielduri->FieldData() );
       
   727 						// TODO check here about servicename:userId
       
   728 						address.SetUriL(*iServiceName,*fieldData);
       
   729 						// fielduri ownership is transfered to storedContact
       
   730 						storedContact->AddFieldL(fielduri);   
       
   731 						CleanupStack::PopAndDestroy(fieldData); //3 
       
   732 						CleanupStack::Pop(); //fielduri 2         
       
   733 						}//End if (fieldData)
       
   734 					else
       
   735 						{
       
   736 						CleanupStack::PopAndDestroy(); //2 fielduri 	
       
   737 						}
       
   738 					TRACE( " XSP Id-IMMP field created" );
       
   739 					}//end of if
       
   740 				else if ( fieldType &&  
       
   741 					( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_FIRSTNAME ))
       
   742 					{
       
   743 					TRACE( " first name field creating" );
       
   744 					HBufC* firstName = NULL;
       
   745 					
       
   746 					// if there is no display name then allocate hte user id as displayname.
       
   747 					if(!(aDisplayName.Length()))
       
   748                        {
       
   749                         // if aServiceField is sip:user1@ouped111147.nmp.nokia.com, then 
       
   750                         // rippedaServiceField will be user1@ouped111147.nmp.nokia.com
       
   751                         TPtrC rippedaServiceField = aUserId.Mid(prefixLocation+1);
       
   752                         //append first name.
       
   753                         TInt atLocation = rippedaServiceField.Locate( '@' ); 
       
   754                         if( atLocation > 0 && ( atLocation < rippedaServiceField.Length() ) )
       
   755                             {
       
   756                             // atLocation must be greater that 0
       
   757                             firstName = rippedaServiceField.Left( atLocation ).AllocLC() ;  //2 
       
   758                             }
       
   759                         else
       
   760                             {
       
   761                             firstName = rippedaServiceField.AllocLC(); //2
       
   762                             }
       
   763                         }
       
   764 					// there is a display name hence allocate it.
       
   765 					else
       
   766 					    {
       
   767 					    firstName =  aDisplayName.AllocLC();//2
       
   768 					    }
       
   769 					
       
   770 					if (firstName)
       
   771 						{
       
   772 						MVPbkStoreContactField* firstNamefield = storedContact->CreateFieldLC(*fieldType); //2
       
   773 						MVPbkContactFieldTextData& firstNameText = MVPbkContactFieldTextData::Cast( firstNamefield->FieldData() );
       
   774 						firstNameText.SetTextL(*firstName);
       
   775 						storedContact->AddFieldL(firstNamefield); // firstNamefield ownership transfered to storedContact
       
   776 						CleanupStack::Pop();  //firstNamefield  2 
       
   777 						CleanupStack::PopAndDestroy(firstName); // 3
       
   778 						}//End if (firstName)
       
   779 
       
   780 					TRACE( "first name field created" );                    
       
   781 					}//end of if.
       
   782 				}//end of for.
       
   783 			TRACE( "iCommitOperationInProgress = %d",iCommitOperationInProgress );
       
   784 			if( iCommitOperationInProgress )
       
   785 			    {
       
   786 			    TRACE("inside if" );
       
   787 			    iUnCommittedVPbkContacts.AppendL( storedContact ); //takes ownership
       
   788 			    CleanupStack::Pop(); //storedContact
       
   789 			    }
       
   790 			else
       
   791 			    {
       
   792 			    TRACE( "inside else");
       
   793 			    iCommitOperationInProgress = ETrue;
       
   794 			    if(iStoreContact)
       
   795 			        {
       
   796 			        delete iStoreContact;
       
   797 			        iStoreContact = NULL;
       
   798 			        }
       
   799 			    TRACE( "contact fields added" );
       
   800 			    iStoreContact = storedContact;
       
   801 			    CleanupStack::Pop(); //storedContact
       
   802 			    iStoreContact->CommitL(*this);
       
   803 			    iStoreEventType = EVPbkContactAdded;
       
   804 			    }
       
   805 			TRACE( "contact commited");
       
   806 			//Start the wait untill adding of new contact is complete
       
   807 			}
       
   808 		}
       
   809     //return the added contact 
       
   810     TRACE( "iOperationError =%d",iOperationError );
       
   811 	      
       
   812 	return iOperationError; 
       
   813 	}
       
   814 
       
   815 // ---------------------------------------------------------------------------
       
   816 // CVIMPSTStorageVPbkStoreHandler::CommitNextPendingContactsL
       
   817 // ---------------------------------------------------------------------------
       
   818 //  
       
   819 void CVIMPSTStorageVPbkStoreHandler::CommitNextPendingContactsL() 
       
   820     {
       
   821 	
       
   822 	TRACER_AUTO;
       
   823     TRACE("iStoreEventType = %d",iStoreEventType );
       
   824     if( !iCommitOperationInProgress && iUnCommittedVPbkContacts.Count() && iStoreEventType != EVPbkUpdatingAvatar)
       
   825         {
       
   826         TRACE( " Commit next" );
       
   827         iCommitOperationInProgress = ETrue;
       
   828         if(iStoreContact)
       
   829             {
       
   830             delete iStoreContact;
       
   831             iStoreContact = NULL;
       
   832             }
       
   833         TRACE( "comming..." );
       
   834         iStoreContact = iUnCommittedVPbkContacts[0];
       
   835         iUnCommittedVPbkContacts.Remove(0);
       
   836         iUnCommittedVPbkContacts.Compress();
       
   837         iStoreContact->CommitL(*this);
       
   838         iStoreEventType = EVPbkContactSynchronizing;
       
   839         TRACE( " Commit called" );
       
   840         }
       
   841     else if(!iCommitOperationInProgress && iPendingAvatars.Count()  && iStoreEventType != EVPbkUpdatingAvatar)
       
   842         {
       
   843         TRACE( "Commit next pending avatar" );
       
   844         TVIMPSTAvatarData avatarData = iPendingAvatars[0];
       
   845         UpdateAvatarFieldDataL( *avatarData.iContactLink, *avatarData.iAvatarContent );
       
   846         iPendingAvatars.Remove( 0 );
       
   847         delete avatarData.iContactLink;
       
   848         avatarData.iContactLink = NULL;
       
   849         delete avatarData.iAvatarContent;
       
   850         avatarData.iAvatarContent = NULL;
       
   851         iPendingAvatars.Compress();
       
   852         }
       
   853     
       
   854     }
       
   855 
       
   856 // ---------------------------------------------------------------------------
       
   857 // CVIMPSTStorageVPbkStoreHandler::RemoveVPbkContactL
       
   858 // ---------------------------------------------------------------------------
       
   859 //  
       
   860 TInt CVIMPSTStorageVPbkStoreHandler::RemoveVPbkContactL(const CVPbkContactLinkArray& aContactLinkArray ) 
       
   861 	{
       
   862 	TRACER_AUTO;
       
   863 	// text now holds the name (or first field of the contact) 
       
   864     delete iContactOperation;
       
   865     iContactOperation = NULL;
       
   866     iContactOperation = iContactManager->DeleteContactsL(aContactLinkArray, *this  );
       
   867     if( !iWait.IsStarted() )
       
   868 	    {
       
   869 	    iWait.Start();	
       
   870 	    }
       
   871 	TRACE( "iOperationError=%d",iOperationError ); 
       
   872 	 
       
   873     return iOperationError;
       
   874   	}
       
   875 
       
   876 // ---------------------------------------------------------------------------
       
   877 // CVIMPSTStorageVPbkStoreHandler::RetrieveVPbkContactL
       
   878 // ---------------------------------------------------------------------------
       
   879 //  
       
   880 TInt CVIMPSTStorageVPbkStoreHandler::RetrieveVPbkContactL(const TDesC8& aPackedLinks) 
       
   881 	{
       
   882 	TRACER_AUTO;  
       
   883 	iStoreEventType = EVPbkContactRetriving;
       
   884 	CVPbkContactLinkArray* linkArray = CVPbkContactLinkArray::NewLC(aPackedLinks, *iStores );
       
   885     // text now holds the name (or first field of the contact) 
       
   886     TInt count = linkArray->Count();
       
   887     TRACE( "count=%d",count );   
       
   888     for ( TInt i=0; i<count; i++ )
       
   889 	    {
       
   890 	    delete iContactLink;
       
   891 	    iContactLink = NULL;
       
   892 	    // local variable caz cant push member variable to cleanup stack
       
   893 	    MVPbkContactLink* cloneLink = linkArray->At( i ).CloneLC();
       
   894 	    iContactLink = cloneLink;
       
   895 	    CleanupStack::Pop(); // cloneLink
       
   896 	    
       
   897 	    delete iContactOperation;
       
   898     	iContactOperation = NULL;
       
   899     	
       
   900 	    iContactOperation = iContactManager->RetrieveContactL( *iContactLink, *this  );
       
   901 	    if( !iWait.IsStarted() )
       
   902 		    {
       
   903 		    iWait.Start();	
       
   904 		    }
       
   905 	    }
       
   906     CleanupStack::PopAndDestroy();//linkArray
       
   907    
       
   908     return iOperationError;
       
   909     }
       
   910  
       
   911                 
       
   912                 
       
   913 // ---------------------------------------------------------------------------
       
   914 // CVIMPSTStorageVPbkStoreHandler::UpdateAvatarFieldDataL()
       
   915 // ---------------------------------------------------------------------------
       
   916 //
       
   917 TInt CVIMPSTStorageVPbkStoreHandler::UpdateAvatarFieldDataL(const MVPbkContactLink& aContactLink,
       
   918 														    const TDesC8& aAvatartData )
       
   919     {
       
   920 	TRACER_AUTO;
       
   921   
       
   922     // local variable caz cant push member variable to cleanup stack
       
   923     MVPbkContactLink* cloneLink = aContactLink.CloneLC();
       
   924     TRACE( "iCommitOperationInProgress: %d",iCommitOperationInProgress );  
       
   925     TRACE( "iUnCommittedVPbkContacts: %d",iUnCommittedVPbkContacts.Count() );
       
   926     if(iCommitOperationInProgress || iUnCommittedVPbkContacts.Count() || iStoreEventType == EVPbkUpdatingAvatar )
       
   927         {
       
   928         TRACE( "Inside if" ); 
       
   929         TVIMPSTAvatarData avatarData = {
       
   930                                        cloneLink,
       
   931                                        aAvatartData.AllocL()
       
   932                                       };
       
   933         iPendingAvatars.Append( avatarData );
       
   934         CleanupStack::Pop(); // cloneLink
       
   935         return KErrInUse;
       
   936         }
       
   937     iCommitOperationInProgress = ETrue;
       
   938     TRACE( "Inside after if" ); 
       
   939     delete iContactLink;
       
   940     iContactLink = NULL;
       
   941     iContactLink = cloneLink;
       
   942     CleanupStack::Pop(); // cloneLink
       
   943     
       
   944     //before updating the avatar, check if there are any pending operation already, if there are any,
       
   945     // then delete them and create the next one.
       
   946     if(iContactOperation)
       
   947         {
       
   948         delete iContactOperation;
       
   949         iContactOperation = NULL;
       
   950         }
       
   951     iStoreEventType = EVPbkUpdatingAvatar;
       
   952     iContactOperation = iContactManager->RetrieveContactL( *iContactLink, *this  );
       
   953     if( !iWait.IsStarted() )
       
   954         {
       
   955         iWait.Start();  
       
   956         }    
       
   957      // exception KErrInUse If another asynchronous operation is 
       
   958     // already in progress.
       
   959     // exception KErrAccessDenied if the contact can not be modified.
       
   960     iStoreContact->LockL(*this);
       
   961   
       
   962 
       
   963     //wait till lock operation is succeded
       
   964     if(!iWait.IsStarted() ) 
       
   965 	    {
       
   966 	    iWait.Start();	
       
   967 	    }
       
   968 	TInt fieldcount = iStoreContact->Fields().FieldCount();
       
   969 	for( TInt j = 0 ; j < fieldcount ; j++ )
       
   970 		{
       
   971 		const MVPbkFieldType* fieldType = iStoreContact->Fields().FieldAt(j).BestMatchingFieldType();
       
   972 		if( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_THUMBNAILPIC )
       
   973 			{
       
   974 			iStoreContact->RemoveField(j);
       
   975 			break;
       
   976 			}
       
   977 		}
       
   978 	if( aAvatartData.Length() )
       
   979 		{
       
   980 		const MVPbkContactStoreProperties& storeProperties =  iStoreContact->ParentStore().StoreProperties();
       
   981 	    const MVPbkFieldTypeList& supportedFieldTypes = storeProperties.SupportedFields();
       
   982 	    TInt fieldCount = supportedFieldTypes.FieldTypeCount();
       
   983 	   	for( TInt index = 0 ; index < fieldCount ; index++ )
       
   984 	        {        
       
   985 	        const MVPbkFieldType* fieldType = &( supportedFieldTypes.FieldTypeAt(index) );
       
   986 		   //find the relevant voip fields/email id fileds.
       
   987 	        if ( fieldType && ( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_THUMBNAILPIC ) )
       
   988 	            {
       
   989 				//updating avatar field in the virtual phonebook
       
   990 				//found the required fieldtype
       
   991 				MVPbkStoreContactField* storeField = iStoreContact->CreateFieldLC(*fieldType);
       
   992 				// The aField instance can not be used after AddFieldL
       
   993 				//storeField ownership is transferred 
       
   994 				TInt fieldIndex = iStoreContact->AddFieldL(storeField);
       
   995 				MVPbkContactFieldData& dataField = iStoreContact->Fields().FieldAt( fieldIndex ).FieldData();
       
   996 				MVPbkContactFieldBinaryData& data = MVPbkContactFieldBinaryData::Cast(dataField);
       
   997 				data.SetBinaryDataL(aAvatartData);
       
   998 				CleanupStack::Pop(); // storeField  
       
   999 				break;                   
       
  1000 				}
       
  1001 	        }
       
  1002 		}
       
  1003 	TRACE( "Inside after if(avatardata.length)" ); 
       
  1004 	iStoreEventType = EVPbkContactChanged;
       
  1005 	TRACE( "Inside after if(avatardata.length ): %d",iStoreEventType ); 
       
  1006 	iStoreContact->CommitL(*this);
       
  1007 	//wait for delete Avatar operation to complete.
       
  1008 	if(!iWait.IsStarted() ) 
       
  1009 		{
       
  1010 		iWait.Start();	
       
  1011 		}
       
  1012 	
       
  1013 	return iOperationError;
       
  1014     }
       
  1015 
       
  1016 // ---------------------------------------------------------------------------
       
  1017 // CVIMPSTStorageVPbkStoreHandler::RemoveAvatarFieldL()
       
  1018 // ---------------------------------------------------------------------------
       
  1019 //
       
  1020 TInt CVIMPSTStorageVPbkStoreHandler::RemoveAvatarFieldL(MVPbkStoreContact& aStoreContact)
       
  1021 	{
       
  1022 	TRACER_AUTO;
       
  1023 	aStoreContact.LockL(*this);
       
  1024     //wait till lock operation is succeded
       
  1025     if(!iWait.IsStarted() ) 
       
  1026 	    {
       
  1027 	    iWait.Start();	
       
  1028 	    }
       
  1029 	TInt fieldcount = aStoreContact.Fields().FieldCount();
       
  1030 	for( TInt j = 0 ; j < fieldcount ; j++ )
       
  1031 		{
       
  1032 		const MVPbkFieldType* fieldType = aStoreContact.Fields().FieldAt(j).BestMatchingFieldType();
       
  1033 		if( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_THUMBNAILPIC )
       
  1034 			{
       
  1035 			aStoreContact.RemoveField(j);
       
  1036 			break;
       
  1037 			}
       
  1038 		}
       
  1039 	aStoreContact.CommitL(*this);
       
  1040 	//wait for delete Avatar operation to complete.
       
  1041 	if(!iWait.IsStarted() ) 
       
  1042 		{
       
  1043 		iWait.Start();	
       
  1044 		}
       
  1045 	TRACE( "iOperationError =%d",iOperationError );   
       
  1046 	
       
  1047 	return iOperationError;	
       
  1048 	}
       
  1049 
       
  1050 // ---------------------------------------------------------------------------
       
  1051 // CVIMPSTStorageVPbkStoreHandler::GetContactFieldDataLC()
       
  1052 // ---------------------------------------------------------------------------
       
  1053 // 
       
  1054 HBufC* CVIMPSTStorageVPbkStoreHandler::GetContactFieldDataLC(const MVPbkStoreContact& aContact,
       
  1055 												    		 TInt aFieldResId ) 
       
  1056     {
       
  1057 	TRACER_AUTO;
       
  1058     HBufC* retValue = NULL;
       
  1059     const MVPbkStoreContactFieldCollection& fieldCollection = aContact.Fields();
       
  1060 	TInt fieldCount = fieldCollection.FieldCount();
       
  1061     TRACE( " fieldCount%d",fieldCount );   
       
  1062 	for( TInt index = 0 ; index < fieldCount ;  index++ )
       
  1063 		{
       
  1064 		const MVPbkStoreContactField& field = fieldCollection.FieldAt( index );
       
  1065 		const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
  1066 		//find the relevant xSP field
       
  1067 		if ( fieldType && ( fieldType->FieldTypeResId() == aFieldResId ) )
       
  1068 			{
       
  1069 			const MVPbkContactFieldData& fieldData = field.FieldData();
       
  1070 			switch( fieldData.DataType() )
       
  1071 				{
       
  1072 				case EVPbkFieldStorageTypeText:
       
  1073 					{
       
  1074 					TRACE(" Text" );   
       
  1075 					retValue = MVPbkContactFieldTextData::Cast(fieldData).Text().AllocLC();
       
  1076 					break;					
       
  1077 					}
       
  1078 				case EVPbkFieldStorageTypeUri:				
       
  1079 					{
       
  1080 					TRACE(" Uri ");   
       
  1081 					retValue = MVPbkContactFieldUriData::Cast(fieldData).Text().AllocLC();
       
  1082 					break;	
       
  1083 					}
       
  1084 				
       
  1085 				}//End switch
       
  1086 			if( aFieldResId == R_VPBK_FIELD_TYPE_IMPP && retValue )
       
  1087 				{
       
  1088 				TRACE( " IMPP fild found");   
       
  1089 				// TODO RAMC : Nikhil/Mukesh
       
  1090 				// in case of srver contacts it also find the first valid xsp id
       
  1091 				// not valid through NULL
       
  1092 				// which can be clarified 
       
  1093 				// earlier it show selection query in UI which one to use
       
  1094 				TPtrC scheme = MVPbkContactFieldUriData::Cast(fieldData).Scheme();
       
  1095 				if( scheme.Compare( *iServiceName ) == 0 )
       
  1096 					{
       
  1097 					TRACE( " actual xsp Id found");   
       
  1098 					// actual xsp id found break the loop and return 
       
  1099 					break;	
       
  1100 					}
       
  1101 				else 
       
  1102 					{
       
  1103 					// actual xsp not yet found continue looking
       
  1104 					CleanupStack::PopAndDestroy(); // retValue;
       
  1105 					retValue = NULL;	
       
  1106 					}
       
  1107 				}
       
  1108 			else if( retValue )
       
  1109 				{
       
  1110 				// break this loop once the given aFieldResId is found
       
  1111 				// with valid data
       
  1112 				break;	
       
  1113 				}
       
  1114 			}
       
  1115 		}
       
  1116 	if( !retValue )
       
  1117 		{
       
  1118 		TRACE( " NULL value");   
       
  1119 		// if NULL assign valid ptr
       
  1120 		retValue = KNullDesC().AllocLC();	
       
  1121 		}
       
  1122 	
       
  1123 	return retValue;// ownership is return to caller
       
  1124     }
       
  1125 
       
  1126 // ---------------------------------------------------------------------------
       
  1127 // CVIMPSTStorageVPbkStoreHandler::GetContactBinaryFieldDataLC()
       
  1128 // ---------------------------------------------------------------------------
       
  1129 // 
       
  1130 HBufC8* CVIMPSTStorageVPbkStoreHandler::GetContactBinaryFieldDataLC(const MVPbkStoreContact& aContact,
       
  1131                                                              TInt aFieldResId ) 
       
  1132     {
       
  1133 	TRACER_AUTO;  
       
  1134     HBufC8* retValue = NULL;
       
  1135     const MVPbkStoreContactFieldCollection& fieldCollection = aContact.Fields();
       
  1136     TInt fieldCount = fieldCollection.FieldCount();
       
  1137     TRACE( "fieldCount%d",fieldCount );   
       
  1138     for( TInt index = 0 ; index < fieldCount ;  index++ )
       
  1139         {
       
  1140         const MVPbkStoreContactField& field = fieldCollection.FieldAt( index );
       
  1141         const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
  1142         //find the relevant xSP field
       
  1143         if ( fieldType && ( fieldType->FieldTypeResId() == aFieldResId ) )
       
  1144             {
       
  1145             const MVPbkContactFieldData& fieldData = field.FieldData();
       
  1146             switch( fieldData.DataType() )
       
  1147                 {
       
  1148                 case EVPbkFieldStorageTypeBinary:
       
  1149                     {
       
  1150                     TRACE( " Text" );
       
  1151                     retValue = MVPbkContactFieldBinaryData::Cast(fieldData).BinaryData().AllocLC();
       
  1152                     break;                  
       
  1153                     }
       
  1154                 }//End switch
       
  1155              if( retValue )
       
  1156                 {
       
  1157                 // break this loop once the given aFieldResId is found
       
  1158                 // with valid data
       
  1159                 break;  
       
  1160                 }
       
  1161             }
       
  1162         }
       
  1163     if( !retValue )
       
  1164         {
       
  1165         TRACE( " NULL value");   
       
  1166         // if NULL assign valid ptr
       
  1167         retValue = KNullDesC8().AllocLC();   
       
  1168         }
       
  1169    
       
  1170     return retValue;// ownership is return to caller
       
  1171     }
       
  1172 
       
  1173 
       
  1174 // ---------------------------------------------------------
       
  1175 // CVIMPSTStorageVPbkStoreHandler::RetriveFieldDataAndSendEventL
       
  1176 // 
       
  1177 // ---------------------------------------------------------		
       
  1178 void CVIMPSTStorageVPbkStoreHandler::RetriveFieldDataAndSendEventL(const MVPbkStoreContact& aContact ) 
       
  1179 	{
       
  1180 	TRACER_AUTO;  
       
  1181 	HBufC* retValue = NULL;
       
  1182     const MVPbkStoreContactFieldCollection& fieldCollection = aContact.Fields();
       
  1183 	TInt fieldCount = fieldCollection.FieldCount();
       
  1184     for( TInt index = 0 ; index < fieldCount ;  index++ )
       
  1185 		{
       
  1186 		const MVPbkStoreContactField& field = fieldCollection.FieldAt( index );
       
  1187 		const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
  1188 		//find the relevant xSP field
       
  1189 		//if ( fieldType && ( fieldType->FieldTypeResId() == aFieldResId ) )
       
  1190 		if ( fieldType && (( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_IMPP )
       
  1191 		                     || (fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_LANDPHONEHOME )
       
  1192 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_MOBILEPHONEHOME )
       
  1193 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_LANDPHONEWORK )
       
  1194 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_MOBILEPHONEWORK )
       
  1195 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_VOIPWORK )
       
  1196 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_VOIPHOME )
       
  1197 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_LANDPHONEGEN )
       
  1198 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_MOBILEPHONEGEN )
       
  1199 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_VOIPGEN )
       
  1200 		                     ||(fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_SIP ) ))
       
  1201 			{
       
  1202 			const MVPbkContactFieldData& fieldData = field.FieldData();
       
  1203 			switch( fieldData.DataType() )
       
  1204 				{
       
  1205 			    case EVPbkFieldStorageTypeText:
       
  1206                     {
       
  1207                     TRACE( "Text" );   
       
  1208                     retValue = MVPbkContactFieldTextData::Cast(fieldData).Text().AllocLC();
       
  1209                     break;                  
       
  1210                     }
       
  1211 				case EVPbkFieldStorageTypeUri:				
       
  1212 					{
       
  1213 					TRACE( "begin" ) ;   
       
  1214 					retValue = MVPbkContactFieldUriData::Cast(fieldData).Text().AllocLC();
       
  1215 					break;	
       
  1216 					}
       
  1217 				
       
  1218 				}//End switch
       
  1219 			if( retValue )
       
  1220 				{
       
  1221 				TRACE( " begin" );   
       
  1222 				TVIMPSTVPbkStoreEvent storeEvent(iStoreEventType,
       
  1223 									NULL,
       
  1224 									NULL,
       
  1225 									*retValue,
       
  1226 									KNullDesC );
       
  1227 				iStoreHandler.HandleVPbkStoreEventL( storeEvent ) ;	
       
  1228 				CleanupStack::PopAndDestroy(); // retValue;
       
  1229 				retValue = NULL;
       
  1230 				}
       
  1231 			}
       
  1232 		}
       
  1233 	 
       
  1234 	}
       
  1235 
       
  1236 // ---------------------------------------------------------
       
  1237 // CVIMPSTStorageVPbkStoreHandler::RemoveAllVPbkContactsL
       
  1238 // 
       
  1239 // ---------------------------------------------------------		
       
  1240 TInt CVIMPSTStorageVPbkStoreHandler::RemoveAllVPbkContactsL()
       
  1241 	{
       
  1242 	TRACER_AUTO; 
       
  1243 	TRACE( " before if" ); 
       
  1244 	if(iAddIdInProgress)
       
  1245 	    {
       
  1246 	    TPtr iAddIdInProgressPtr = iAddIdInProgress->Des();
       
  1247 	    TRACE( "iAddIdInProgressPtr  = %S", &iAddIdInProgressPtr ); 
       
  1248 	    delete iAddIdInProgress;
       
  1249 	    iAddIdInProgress = NULL;
       
  1250 	    }
       
  1251 	// intialize it to efalse as, somewhere it would have missed.
       
  1252 	iCommitOperationInProgress = EFalse;
       
  1253 	TRACE( " After if"); 
       
  1254 	TInt count = 0;
       
  1255 	if( iContactView )
       
  1256 		{
       
  1257 		count = iContactView->ContactCountL();	
       
  1258 		}
       
  1259 	TRACE( "count = %d",count) ;   
       
  1260 	TPtr iStoreNamePtr(iStoreName->Des() );
       
  1261 	TInt index = iStoreNamePtr.Find(_L(":") );
       
  1262 	if( index >= 0 && count )
       
  1263 		{
       
  1264 		TPtrC store = iStoreNamePtr.Right( iStoreNamePtr.Length() - index -3 );	
       
  1265 		CContactIdArray* contactIds = CContactIdArray::NewLC();
       
  1266 		CContactDatabase* contactDb = NULL;
       
  1267 		//Open the database
       
  1268 		TRAPD(err, contactDb = CContactDatabase::OpenL( store ) );
       
  1269 		if(err == KErrNone )
       
  1270 			{	
       
  1271 			CleanupStack::PushL( contactDb );
       
  1272 			// rest if already old ids exist
       
  1273 			contactIds->Reset();
       
  1274 			TContactIter iterator(*contactDb);
       
  1275 			TContactItemId contactId = iterator.FirstL();	
       
  1276 			while(contactId != KNullContactId)
       
  1277 				{
       
  1278 				TRACE( "in side while" );   
       
  1279 				// store into iContactIds for later deletion of contacts
       
  1280 				contactIds->AddL(contactId );
       
  1281 				// get the next contactids
       
  1282 				contactId = iterator.NextL();	
       
  1283 				}
       
  1284 			contactDb->DeleteContactsL(*contactIds);
       
  1285 			// reset old ids
       
  1286 			contactIds->Reset();
       
  1287 			CleanupStack::PopAndDestroy( contactDb );
       
  1288 			}
       
  1289 		CleanupStack::PopAndDestroy( contactIds );
       
  1290 		}
       
  1291 	
       
  1292 	return count;	
       
  1293 	}
       
  1294 
       
  1295 // ---------------------------------------------------------------------------
       
  1296 // CVIMPSTStorageVPbkStoreHandler::ResetAndDestroyRArray
       
  1297 // ---------------------------------------------------------------------------
       
  1298 //
       
  1299 void CVIMPSTStorageVPbkStoreHandler::ResetAndDestroyRArray()
       
  1300     {
       
  1301 	TRACER_AUTO;
       
  1302     TInt count = iPendingAvatars.Count();
       
  1303     while( count )
       
  1304         {
       
  1305         TVIMPSTAvatarData contactInf = iPendingAvatars[ 0 ];
       
  1306         iPendingAvatars.Remove(0 );
       
  1307         delete contactInf.iAvatarContent;
       
  1308         contactInf.iAvatarContent = NULL;  
       
  1309         delete contactInf.iContactLink;
       
  1310         contactInf.iContactLink = NULL;                      
       
  1311          // not exist need to add 
       
  1312         iPendingAvatars.Compress(); 
       
  1313         count = iPendingAvatars.Count();
       
  1314         }
       
  1315     iPendingAvatars.Reset();
       
  1316    
       
  1317     }
       
  1318 // End of file