predictivesearch/adapters/contacts/src/cpcscontactstore.cpp
branchRCL_3
changeset 58 d4f567ce2e7c
parent 45 34879f5cfc63
child 64 c1e8ba0c2b16
equal deleted inserted replaced
57:2666d9724c76 58:d4f567ce2e7c
    93 // ---------------------------------------------------------------------------------
    93 // ---------------------------------------------------------------------------------
    94 // Constructor
    94 // Constructor
    95 // ---------------------------------------------------------------------------------
    95 // ---------------------------------------------------------------------------------
    96 CPcsContactStore::CPcsContactStore():
    96 CPcsContactStore::CPcsContactStore():
    97     CActive( CActive::EPriorityLow ),
    97     CActive( CActive::EPriorityLow ),
    98     iAllContactLinksCount(0),
    98     iInitialContactCount( 0 ),
    99     iFetchedContactCount(0),
    99     iFetchBlockLowerNumber( 0 ),
   100     iContactViewReady(EFalse)
   100     iFetchBlockUpperNumber( 0 ),
       
   101     iVPbkCallbackCount( 0 ),
       
   102     iContactViewReady( EFalse )
   101 {
   103 {
   102     PRINT ( _L("Enter CPcsContactStore::CPcsContactStore") );
   104     PRINT ( _L("Enter CPcsContactStore::CPcsContactStore") );
   103     CActiveScheduler::Add( this );
   105     CActiveScheduler::Add( this );
   104     PRINT ( _L("End CPcsContactStore::CPcsContactStore") );
   106     PRINT ( _L("End CPcsContactStore::CPcsContactStore") );
   105 }
   107 }
   106 
   108 
   107 
       
   108 // ---------------------------------------------------------------------------------
   109 // ---------------------------------------------------------------------------------
   109 // CPcsContactStore::ConstructL() 
   110 // CPcsContactStore::ConstructL() 
   110 // 2nd phase constructor
   111 // 2nd phase constructor
   111 // ---------------------------------------------------------------------------------
   112 // ---------------------------------------------------------------------------------
   112 void CPcsContactStore::ConstructL(CVPbkContactManager&  aContactManager,
   113 void CPcsContactStore::ConstructL(CVPbkContactManager&  aContactManager,
   133     // Local timer for block fetch delay
   134     // Local timer for block fetch delay
   134     iTimer.CreateLocal();
   135     iTimer.CreateLocal();
   135     
   136     
   136     // Initial state
   137     // Initial state
   137 	iNextState = ECreateView;
   138 	iNextState = ECreateView;
   138 	IssueRequest();
   139 	IssueRequest( 0 );
   139 	
   140 	
   140     FeatureManager::InitializeLibL();
   141     FeatureManager::InitializeLibL();
   141     if( FeatureManager::FeatureSupported( KFeatureIdffContactsMycard ) )
   142     if( FeatureManager::FeatureSupported( KFeatureIdffContactsMycard ) )
   142         {
   143         {
   143         iMyCardSupported = ETrue;
   144         iMyCardSupported = ETrue;
   153 CPcsContactStore::~CPcsContactStore() 
   154 CPcsContactStore::~CPcsContactStore() 
   154 {
   155 {
   155 	PRINT ( _L("Enter CPcsContactStore::~CPcsContactStore") );
   156 	PRINT ( _L("Enter CPcsContactStore::~CPcsContactStore") );
   156 	
   157 	
   157 	delete iContactViewBase;
   158 	delete iContactViewBase;
   158 	iContactViewBase = NULL;
       
   159 	
       
   160 	delete iSortOrder;
   159 	delete iSortOrder;
   161 	iSortOrder = NULL;
   160     delete iSimContactItems;   
   162 	
       
   163     delete iSimContactItems;
       
   164     iSimContactItems = NULL ;
       
   165      
       
   166 	delete iWait;
   161 	delete iWait;
   167 	iWait = NULL;
       
   168 	
       
   169 	delete iUri;
   162 	delete iUri;
   170 	iUri = NULL;
       
   171 	
   163 	
   172 	iTimer.Cancel();
   164 	iTimer.Cancel();
   173     iTimer.Close();
   165     iTimer.Close();
   174 	iFieldsToCache.Close();	
   166 	iFieldsToCache.Close();	
   175 	
   167 	
   176 	iFs.Close();
   168 	iFs.Close();
   177 	
   169 
       
   170 	iInitialContactLinks.ResetAndDestroy();
       
   171     
   178 	if(IsActive())
   172 	if(IsActive())
   179 	{
   173 	{
   180 		Deque();
   174 		Deque();
   181 	}
   175 	}
   182 	
   176 	
   183 	delete iSortOrderMan;
   177 	delete iSortOrderMan;
   184 	
   178 	
   185 	PRINT ( _L("End CPcsContactStore::~CPcsContactStore") );
   179 	PRINT ( _L("End CPcsContactStore::~CPcsContactStore") );
   186 }
   180 }
   187 
   181 
   188 
       
   189 // ---------------------------------------------------------------------------------
   182 // ---------------------------------------------------------------------------------
   190 // Handles addition/deletion/modification of contacts
   183 // Handles addition/deletion/modification of contacts
   191 // ---------------------------------------------------------------------------------
   184 // ---------------------------------------------------------------------------------
   192 void CPcsContactStore::HandleStoreEventL(MVPbkContactStore& aContactStore, 
   185 void CPcsContactStore::HandleStoreEventL(MVPbkContactStore& aContactStore, 
   193                 TVPbkContactStoreEvent aStoreEvent)
   186                 TVPbkContactStoreEvent aStoreEvent)
   194 {
   187 {
   195    	PRINT ( _L("Enter CPcsContactStore::HandleStoreEventL") );
   188    	PRINT ( _L("Enter CPcsContactStore::HandleStoreEventL") );
   196 
   189    	
   197 	switch (aStoreEvent.iEventType) 
   190     PRINT2 ( _L("CPcsContactStore::HandleStoreEventL: URI=%S, event TVPbkContactStoreEventType::%d received"),
   198 	{
   191              &*iUri, aStoreEvent.iEventType);
       
   192    	        
       
   193     switch (aStoreEvent.iEventType) 
       
   194     {
   199 		case TVPbkContactStoreEvent::EContactAdded:
   195 		case TVPbkContactStoreEvent::EContactAdded:
       
   196         case TVPbkContactStoreEvent::EGroupAdded:
   200 		{
   197 		{
   201 		    PRINT ( _L("Add contact/group event received") );
       
   202 		    
       
   203 		    // Observer will be notified once the cache update is complete
   198 		    // Observer will be notified once the cache update is complete
   204 		    iOngoingCacheUpdate = ECacheUpdateContactAdded;
   199 		    iOngoingCacheUpdate = ECacheUpdateContactAdded;
   205 		                    
   200 		                    
   206 		    iContactManager->RetrieveContactL( *(aStoreEvent.iContactLink), *this );
   201 		    iContactManager->RetrieveContactL( *(aStoreEvent.iContactLink), *this );
   207 			break;
   202 			break;
   208 		}
   203 		}
   209 		
   204 
   210         case TVPbkContactStoreEvent::EUnknownChanges:
   205         case TVPbkContactStoreEvent::EUnknownChanges:
   211             {
   206         {
   212             iObserver->RemoveAll( *iUri );
   207             iObserver->RemoveAll( *iUri );      
       
   208             // To indicate that contact view is not ready when unknown changes happens 
   213             iContactViewReady = EFalse;
   209             iContactViewReady = EFalse;
   214             iContactViewBase->AddObserverL(*this);
   210             iContactViewBase->AddObserverL(*this);
   215             }
       
   216             break;
   211             break;
       
   212         }
   217 
   213 
   218 		case TVPbkContactStoreEvent::EContactDeleted:
   214 		case TVPbkContactStoreEvent::EContactDeleted:
       
   215         case TVPbkContactStoreEvent::EGroupDeleted:
   219 		case TVPbkContactStoreEvent::EContactChanged:
   216 		case TVPbkContactStoreEvent::EContactChanged:
   220 		case TVPbkContactStoreEvent::EGroupDeleted:
       
   221 		case TVPbkContactStoreEvent::EGroupChanged:
   217 		case TVPbkContactStoreEvent::EGroupChanged:
   222 		{
   218 		{
   223 		    if ( aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactChanged ||
       
   224 		         aStoreEvent.iEventType == TVPbkContactStoreEvent::EGroupChanged ) 
       
   225 		    {
       
   226 		    	PRINT ( _L("Change contact/group event received") );
       
   227 		    }
       
   228 		    else 
       
   229 		    {
       
   230 		    	PRINT ( _L("Delete contact/group event received") );
       
   231 		    }
       
   232 			
       
   233 			CVPbkContactIdConverter* converter = NULL;
   219 			CVPbkContactIdConverter* converter = NULL;
   234 			
   220 			
   235 		    TRAPD ( err, converter = CVPbkContactIdConverter::NewL( aContactStore ) );
   221 		    TRAPD ( err, converter = CVPbkContactIdConverter::NewL( aContactStore ) );
   236 
   222 
   237 	        TInt32 contactId(-1);
   223 	        TInt32 contactId(-1);
   293 {
   279 {
   294     return *iUri;
   280     return *iUri;
   295 }
   281 }
   296 
   282 
   297 // ---------------------------------------------------------------------------
   283 // ---------------------------------------------------------------------------
       
   284 //  Check next state of state machine for contacts retrieval and perform
       
   285 //  transition
       
   286 // --------------------------------------------------------------------------- 
       
   287 void CPcsContactStore::CheckNextState()
       
   288 {
       
   289     PRINT5 ( _L("CPcsContactStore::CheckNextState(state %d) %d:(%d..%d)/%d"),
       
   290              iNextState, iVPbkCallbackCount,
       
   291              iFetchBlockLowerNumber, iFetchBlockUpperNumber, iInitialContactCount );
       
   292 
       
   293     if ( iNextState == EComplete )
       
   294     {
       
   295         return;
       
   296     }
       
   297 
       
   298     // If during the caching of the contacts initially in the view we get some
       
   299     // contact events for other contact operations from the user, then it can
       
   300     // happen at the end of the caching process that iCallbackCount > iAllContactLinksCount.
       
   301     // We assume that it can never be that at the end of the caching it results
       
   302     // iCallbackCount < iAllContactLinksCount.
       
   303 
       
   304     // Check if caching is complete
       
   305     if( iFetchBlockUpperNumber == iInitialContactCount       // All contacts initially in the view were fetched
       
   306         && iVPbkCallbackCount >= iInitialContactCount )        // We got at least the same amount of callbacks
       
   307     {
       
   308         iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
       
   309         iNextState = EComplete;
       
   310         IssueRequest( 0 );
       
   311     }
       
   312     // We could have a number of callbacks that is the same of the fetched contacts
       
   313     // from the view even before we get the all the callbacks for the fetched contacts.
       
   314     // This can happen for instance if a contact is added by the user.
       
   315     // With the following condition it will happen just that the fetching of more
       
   316     // contacts from the initial view is happening before all the callbacks for
       
   317     // those are received.
       
   318     else if ( iVPbkCallbackCount >= iFetchBlockUpperNumber ) // Fetch next block
       
   319     {
       
   320         iObserver->UpdateCachingStatus(*iUri, ECachingInProgress);
       
   321         iNextState = EFetchContactBlock;
       
   322         // Delay the next fetch since contact fetch is CPU intensive,
       
   323         // this will give other threads a chance to use CPU
       
   324         IssueRequest( KTimerInterval ); // 100 milliseconds
       
   325     }
       
   326     else
       
   327     {
       
   328     // Otherwise, just pass through.
       
   329     }
       
   330 
       
   331 }
       
   332 
       
   333 // ---------------------------------------------------------------------------
   298 //  Callback Method. Called when one Retrieve operation is complete
   334 //  Callback Method. Called when one Retrieve operation is complete
   299 // --------------------------------------------------------------------------- 
   335 // --------------------------------------------------------------------------- 
   300 void CPcsContactStore::VPbkSingleContactOperationComplete(
   336 void CPcsContactStore::VPbkSingleContactOperationComplete(
   301         MVPbkContactOperationBase& aOperation, MVPbkStoreContact* aContact)
   337         MVPbkContactOperationBase& aOperation, MVPbkStoreContact* aContact)
   302 {    
   338 {
   303     iFetchedContactCount++;
   339     PRINT ( _L("Enter CPcsContactStore::VPbkSingleContactOperationComplete") );
   304 
   340 
   305 			       
   341     // We get this incremented even during add/del of contacts during caching
       
   342     iVPbkCallbackCount++;
       
   343     
   306 	// Handle the fetched contact....
   344 	// Handle the fetched contact....
   307 	TRAPD(err, HandleRetrievedContactL(aContact) );
   345 	TRAPD(err, HandleRetrievedContactL(aContact) );
   308 	if( err != KErrNone)
   346 	if( err != KErrNone)
   309 	{
   347 	{
   310 		iObserver->UpdateCachingStatus(*iUri, err);
   348 		iObserver->UpdateCachingStatus(*iUri, err);
   311 	}
   349 	}
   312 	
   350 
   313 	MVPbkContactOperationBase* Opr = &aOperation;
   351 	MVPbkContactOperationBase* Opr = &aOperation;
   314 	if ( Opr )
   352 	if ( Opr )
   315 	{
   353 	{
   316 		delete Opr;
   354 		delete Opr;
   317 		Opr = NULL;
   355 		Opr = NULL;
   318 	}
   356 	}
   319 	
   357 
   320 	// Update the iNextState variable to proper state
   358 	CheckNextState();
   321 	if( iFetchedContactCount == iAllContactLinksCount )
   359 
   322 	{
   360     PRINT ( _L("End CPcsContactStore::VPbkSingleContactOperationComplete") );
   323 	    // Fetch complete
   361 }
   324 	    // Update the caching status
   362     
   325 		iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
   363 // ---------------------------------------------------------------------------
   326 		iNextState = EComplete;
   364 //  Callback Method. Called when one Retrieve operation fails.
   327 		IssueRequest();
       
   328 	}
       
   329 	else if ( (iFetchedContactCount % KLinksToFetchInOneGo) == 0 )
       
   330 	{
       
   331 	    // Fetch next block
       
   332 		iNextState = EFetchContactBlock;
       
   333         // Delay the next fetch since contact fetch is CPU intensive,
       
   334         // this will give other threads a chance to use CPU
       
   335 	    if(!IsActive())
       
   336 	        {
       
   337             iTimer.After( iStatus, KTimerInterval); // 100 milliseconds
       
   338             SetActive();
       
   339 	        }
       
   340 
       
   341 	}
       
   342 	
       
   343 }
       
   344     
       
   345 // ---------------------------------------------------------------------------
       
   346 //  Callback Method.Called when one Retrieve operation fails.
       
   347 // ---------------------------------------------------------------------------
   365 // ---------------------------------------------------------------------------
   348 void CPcsContactStore::VPbkSingleContactOperationFailed(
   366 void CPcsContactStore::VPbkSingleContactOperationFailed(
   349         MVPbkContactOperationBase& /*aOperation*/, TInt /*aError*/ )
   367         MVPbkContactOperationBase& /*aOperation*/, TInt /*aError*/ )
   350 {
   368 {
   351   	PRINT ( _L("Enter CPcsContactStore::VPbkSingleContactOperationFailed") );
   369   	PRINT ( _L("Enter CPcsContactStore::VPbkSingleContactOperationFailed") );
   352 	iFetchedContactCount++;
   370 
   353 
   371   	iVPbkCallbackCount++;
   354 	// Update the iNextState variable to proper state
   372 
   355 	if( iFetchedContactCount == iAllContactLinksCount )
   373 	CheckNextState();
   356 	{
   374 
   357 		// Fetch complete
       
   358 		iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
       
   359 		iNextState = EComplete;
       
   360 		IssueRequest();
       
   361 	}
       
   362 	else if ( (iFetchedContactCount % KLinksToFetchInOneGo) == 0 ) 
       
   363 	{
       
   364 		// Fetch next block
       
   365 		iNextState = EFetchContactBlock;
       
   366         // Delay the next fetch since contact fetch is CPU intensive,
       
   367         // this will give other threads a chance to use CPU
       
   368         if(!IsActive())
       
   369             {
       
   370             iTimer.After( iStatus, KTimerInterval); // 100 milliseconds
       
   371             SetActive();
       
   372             }
       
   373 	}
       
   374 	
       
   375 	PRINT ( _L("End CPcsContactStore::VPbkSingleContactOperationFailed") );
   375 	PRINT ( _L("End CPcsContactStore::VPbkSingleContactOperationFailed") );
   376 }
   376 }
   377 
   377 
   378 // ---------------------------------------------------------------------------
   378 // ---------------------------------------------------------------------------
   379 // Handles the operations for a single contact after it is fetched
   379 // Handles the operations for a single contact after it is fetched
   397     if ( NULL == tmpLink )
   397     if ( NULL == tmpLink )
   398     {
   398     {
   399         CleanupStack::PopAndDestroy( aContact );
   399         CleanupStack::PopAndDestroy( aContact );
   400         return;
   400         return;
   401     }
   401     }
   402    
   402 
   403 	CPsData* phoneContact = CPsData::NewL();
   403 	CPsData* phoneContact = CPsData::NewL();
   404 
   404 
   405 	
       
   406 	// Fill the contact id
   405 	// Fill the contact id
   407 	CVPbkContactIdConverter* converter = NULL;
   406 	CVPbkContactIdConverter* converter = NULL;
   408 	TRAPD ( err, converter = CVPbkContactIdConverter::NewL( aContact->ParentStore() ) );
   407 	TRAPD ( err, converter = CVPbkContactIdConverter::NewL( aContact->ParentStore() ) );
   409 	
   408 	
   410 	if ( err == KErrNotSupported )
   409 	if ( err == KErrNotSupported )
   411 	{
   410 	{
   412 		// simdb domain
   411 		// simdb domain
   413 		PRINT ( _L("SIM domain data received") );
   412 		PRINT ( _L("CPcsContactStore::HandleRetrievedContactL: SIM domain data received") );
   414 		
   413 		
   415         // Set the contact link 
   414         // Set the contact link 
   416         HBufC8* extnInfo = tmpLink->PackLC();
   415         HBufC8* extnInfo = tmpLink->PackLC();
   417         phoneContact->SetDataExtension(extnInfo);
   416         phoneContact->SetDataExtension(extnInfo);
   418         CleanupStack::Pop( extnInfo );
   417         CleanupStack::Pop( extnInfo );
   517 // Fetches the data from a particular contact 
   516 // Fetches the data from a particular contact 
   518 // --------------------------------------------------------------------------- 
   517 // --------------------------------------------------------------------------- 
   519 void CPcsContactStore::GetDataForSingleContactL( MVPbkBaseContact& aContact,
   518 void CPcsContactStore::GetDataForSingleContactL( MVPbkBaseContact& aContact,
   520                                                  CPsData* aPhoneData )
   519                                                  CPsData* aPhoneData )
   521 {      
   520 {      
   522 
       
   523     for(TInt i =0; i < iFieldsToCache.Count(); i++)
   521     for(TInt i =0; i < iFieldsToCache.Count(); i++)
   524     {
   522     {
   525 	    aPhoneData->SetDataL(i, KNullDesC);
   523 	    aPhoneData->SetDataL(i, KNullDesC);
   526 		AddContactFieldsL( aContact, iFieldsToCache[i], aPhoneData);			
   524 		AddContactFieldsL( aContact, iFieldsToCache[i], aPhoneData);			
   527     }
   525     }
   528 	
       
   529 }
   526 }
   530 
   527 
   531 // ---------------------------------------------------------------------------
   528 // ---------------------------------------------------------------------------
   532 // Add the data from contact fields
   529 // Add the data from contact fields
   533 // --------------------------------------------------------------------------- 
   530 // --------------------------------------------------------------------------- 
   585 	
   582 	
   586 	CleanupStack::PopAndDestroy( itr ); 
   583 	CleanupStack::PopAndDestroy( itr ); 
   587 }
   584 }
   588 
   585 
   589 // ---------------------------------------------------------------------------
   586 // ---------------------------------------------------------------------------
       
   587 // Fetches all the initial contact links from vpbk 
       
   588 // --------------------------------------------------------------------------- 
       
   589 void CPcsContactStore::FetchAllInitialContactLinksL()
       
   590 {
       
   591     PRINT2 ( _L("CPcsContactStore::FetchAllInitialContactLinksL: URI=%S. Fetching %d contact links"),
       
   592              &*iUri, iInitialContactCount );
       
   593 
       
   594     __LATENCY_MARK ( _L("CPcsContactStore::FetchAllInitialContactLinksL ==== fetch contact links start") );
       
   595     
       
   596     for(TInt cnt = 0; cnt < iInitialContactCount; cnt++)
       
   597     {
       
   598         // Get the contact link
       
   599         MVPbkContactLink* tempLink = iContactViewBase->CreateLinkLC(cnt);
       
   600         iInitialContactLinks.AppendL( tempLink );
       
   601         CleanupStack::Pop();
       
   602     }
       
   603 
       
   604     __LATENCY_MARKEND ( _L("CPcsContactStore::FetchAllInitialContactLinksL ==== fetch contact links end") );                          
       
   605 
       
   606     // No callback to wait for next state
       
   607     iObserver->UpdateCachingStatus(*iUri, ECachingInProgress);
       
   608     iNextState = EFetchContactBlock;
       
   609     IssueRequest( 0 );
       
   610 }
       
   611 
       
   612 // ---------------------------------------------------------------------------
   590 // Fetches the data from the vpbk using the contact links 
   613 // Fetches the data from the vpbk using the contact links 
   591 // --------------------------------------------------------------------------- 
   614 // --------------------------------------------------------------------------- 
   592 void CPcsContactStore::FetchlinksL()
   615 void CPcsContactStore::FetchContactsBlockL()
   593 {		
   616 {
   594 
   617     iFetchBlockLowerNumber = iFetchBlockUpperNumber;
   595 	PRINT1 ( _L("CPcsContactStore::Total contacts downloaded = %d"),
   618     iFetchBlockUpperNumber += KLinksToFetchInOneGo;
   596 	          iFetchedContactCount );
   619     if ( iFetchBlockUpperNumber > iInitialContactCount )
   597 	
   620     {
   598     TInt blockCount = iFetchedContactCount + KLinksToFetchInOneGo;
   621         iFetchBlockUpperNumber = iInitialContactCount;
   599     
   622     }
   600     if( blockCount >= iAllContactLinksCount)
   623     
   601     	blockCount = iAllContactLinksCount;
   624     PRINT2 ( _L("CPcsContactStore::FetchContactsBlockL: Fetched %d contacts, fetching more until %d"),
   602     
   625              iFetchBlockLowerNumber, iFetchBlockUpperNumber );
   603 	for(TInt cnt = iFetchedContactCount; cnt < blockCount; cnt++)
   626 
   604 	{	
   627     for(TInt cnt = iFetchBlockLowerNumber; cnt < iFetchBlockUpperNumber; cnt++)
   605 		// Retrieve the contact 
   628     {
   606 		MVPbkContactLink* tempLink =iContactViewBase->CreateLinkLC(cnt);
   629         // Retrieve the contact
   607 		iContactManager->RetrieveContactL( *tempLink, *this );
   630         iContactManager->RetrieveContactL( *iInitialContactLinks[cnt], *this );
   608 		CleanupStack::PopAndDestroy(); // tempLink
   631     }
   609 	}
   632 
   610 
   633     // Destroy the array of temporary contact links when all
       
   634     // contacts from the initial view are retrieved
       
   635     if (iFetchBlockUpperNumber == iInitialContactCount)
       
   636     {
       
   637         iInitialContactLinks.ResetAndDestroy();
       
   638     }
   611 }
   639 }
   612 
   640 
   613 // ---------------------------------------------------------------------------
   641 // ---------------------------------------------------------------------------
   614 // Implements the view ready function of MVPbkContactViewObserver
   642 // Implements the view ready function of MVPbkContactViewObserver
   615 // --------------------------------------------------------------------------- 
   643 // --------------------------------------------------------------------------- 
   616 void CPcsContactStore::ContactViewReady(
   644 void CPcsContactStore::ContactViewReady(
   617                 MVPbkContactViewBase& aView ) 
   645                 MVPbkContactViewBase& aView ) 
   618 {
   646 {
   619     PRINT ( _L("Enter CPcsContactStore::ContactViewReady") );
   647     PRINT ( _L("Enter CPcsContactStore::ContactViewReady") );
       
   648 
   620     iFs.Close();
   649     iFs.Close();
   621     aView.RemoveObserver(*this);
   650     aView.RemoveObserver(*this);
   622     // Get the total number of contacts for this view
   651     // Get the total number of contacts for this view
   623     TRAPD(err,iAllContactLinksCount = aView.ContactCountL());
   652     TRAPD(err, iInitialContactCount = aView.ContactCountL());
   624     
   653     
   625     if(err != KErrNone)
   654     if(err != KErrNone)
   626     {
   655     {
   627     	PRINT( _L("CPcsContactStore::ContactViewReady - Unable to obtain contact count"));
   656     	PRINT( _L("CPcsContactStore::ContactViewReady: Unable to obtain Contacts count"));
       
   657         iObserver->UpdateCachingStatus(*iUri, ECachingCompleteWithErrors);
   628     	iNextState = EComplete;
   658     	iNextState = EComplete;
   629    		IssueRequest();
   659    		IssueRequest( 0 );
   630    		return;
   660    		return;
   631     }
   661     }
   632     
   662     
   633     PRINT1 ( _L("Total number of contacts for this view: %d"), iAllContactLinksCount);
   663     PRINT1 ( _L("CPcsContactStore::ContactViewReady: Total number of contacts for this view: %d"),
   634     if( iAllContactLinksCount == 0)
   664             iInitialContactCount );
   635     {
   665     if(iInitialContactCount == 0)
   636         // No Contactsb to cache, hence update the status accordingly
   666     {
       
   667         // No Contacts to cache, hence update the status accordingly
   637     	iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
   668     	iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
   638     	iNextState = EComplete;
   669     	iNextState = EComplete;
   639    		IssueRequest();
   670    		IssueRequest( 0 );
   640    		return;
   671    		return;
   641     }
   672     }
   642     iFetchedContactCount = 0;
   673     iFetchBlockLowerNumber = 0;
       
   674     iFetchBlockUpperNumber = 0;
       
   675     iVPbkCallbackCount = 0;
   643     
   676     
   644     // Change the iNextState to fetch the contacts 
   677     // Change the iNextState to fetch the contacts 
   645     if( iContactViewReady == EFalse)
   678     if(iContactViewReady == EFalse)
   646     {
   679     {
   647     	iObserver->UpdateCachingStatus(*iUri, ECachingInProgress);
   680     	iObserver->UpdateCachingStatus(*iUri, ECachingInProgress);
   648     	iContactViewReady = ETrue;
   681     	iContactViewReady = ETrue;
   649     	iNextState = EFetchContactBlock;
   682     	iNextState = EFetchAllLinks;
   650    		IssueRequest();
   683    		IssueRequest( 0 );
   651     }
   684     }
   652 	
   685 	
   653 	PRINT ( _L("End CPcsContactStore::ContactViewReady") );
   686 	PRINT ( _L("End CPcsContactStore::ContactViewReady") );
   654 }
   687 }
   655 
   688 
   656 // ---------------------------------------------------------------------------
   689 // ---------------------------------------------------------------------------
   657 // Implements the view unavailable function of MVPbkContactViewObserver
   690 // Implements the view unavailable function of MVPbkContactViewObserver
   658 // --------------------------------------------------------------------------- 
   691 // --------------------------------------------------------------------------- 
   659 void CPcsContactStore::ContactViewUnavailable(
   692 void CPcsContactStore::ContactViewUnavailable(
   660                 MVPbkContactViewBase& /*aView*/ )  
   693          MVPbkContactViewBase& /*aView*/ )  
   661 {
   694 {
   662     PRINT ( _L("Enter CPcsContactStore::ContactViewUnavailable") );
   695     PRINT ( _L("Enter CPcsContactStore::ContactViewUnavailable") );
   663     // Update the caching status to complete
   696     PRINT1 ( _L("CPcsContactStore::ContactViewUnavailable: URI=%S"),
       
   697              &*iUri );
       
   698 
       
   699     // Update the caching status to cachingComplete, the client of PCS--Cmail can
       
   700     // perform searching even when contactview of somestore is unavailable.
       
   701     // For Contacts and Group view we get one of this event brfore ContactViewReady
   664 	iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
   702 	iObserver->UpdateCachingStatus(*iUri, ECachingComplete);
   665     iFs.Close();
   703     iFs.Close();
   666 	PRINT ( _L("End CPcsContactStore::ContactViewUnavailable") );
   704 
       
   705     PRINT ( _L("End CPcsContactStore::ContactViewUnavailable") );
   667 }
   706 }
   668 
   707 
   669 // ---------------------------------------------------------------------------
   708 // ---------------------------------------------------------------------------
   670 // Implements the add contact function of MVPbkContactViewObserver
   709 // Implements the add contact function of MVPbkContactViewObserver
   671 // --------------------------------------------------------------------------- 
   710 // --------------------------------------------------------------------------- 
   672 void CPcsContactStore::ContactAddedToView(
   711 void CPcsContactStore::ContactAddedToView(
   673             MVPbkContactViewBase& /*aView*/, 
   712          MVPbkContactViewBase& /*aView*/, 
   674             TInt /*aIndex*/, 
   713          TInt /*aIndex*/, 
   675             const MVPbkContactLink& /*aContactLink*/ ) 
   714          const MVPbkContactLink& /*aContactLink*/ ) 
   676 {
   715 {
   677 }
   716 }
   678 
   717 
   679 // ---------------------------------------------------------------------------
   718 // ---------------------------------------------------------------------------
   680 // Implements the remove contact function of MVPbkContactViewObserver
   719 // Implements the remove contact function of MVPbkContactViewObserver
   681 // --------------------------------------------------------------------------- 
   720 // --------------------------------------------------------------------------- 
   682 void CPcsContactStore::ContactRemovedFromView(
   721 void CPcsContactStore::ContactRemovedFromView(
   683                 MVPbkContactViewBase& /*aView*/, 
   722          MVPbkContactViewBase& /*aView*/, 
   684                 TInt /*aIndex*/, 
   723          TInt /*aIndex*/, 
   685                 const MVPbkContactLink& /*aContactLink*/ )  
   724          const MVPbkContactLink& /*aContactLink*/ )  
   686 {
   725 {
   687 }
   726 }
   688 
   727 
   689 // ---------------------------------------------------------------------------
   728 // ---------------------------------------------------------------------------
   690 // Implements the view error function of MVPbkContactViewObserver
   729 // Implements the view error function of MVPbkContactViewObserver
   691 // --------------------------------------------------------------------------- 
   730 // --------------------------------------------------------------------------- 
   692 void CPcsContactStore::ContactViewError(
   731 void CPcsContactStore::ContactViewError(
   693             MVPbkContactViewBase& /*aView*/, 
   732          MVPbkContactViewBase& /*aView*/, 
   694             TInt /*aError*/, 
   733          TInt aError, 
   695             TBool /*aErrorNotified*/ )  
   734          TBool /*aErrorNotified*/ )  
   696 {
   735 {
       
   736     PRINT2 ( _L("CPcsContactStore::ContactViewError: URI=%S, Error=%d"),
       
   737              &*iUri, aError );
   697 }
   738 }
   698 
   739 
   699 // ---------------------------------------------------------------------------
   740 // ---------------------------------------------------------------------------
   700 // Creates the contact view to fetch data from this store
   741 // Creates the contact view to fetch data from this store
   701 // --------------------------------------------------------------------------- 
   742 // --------------------------------------------------------------------------- 
   702 void CPcsContactStore::CreateContactFetchViewL()
   743 void CPcsContactStore::CreateContactFetchViewL()
   703 {
   744 {
   704 	PRINT ( _L("Enter CPcsContactStore::CreateContactFetchViewL") );
   745 	PRINT ( _L("Enter CPcsContactStore::CreateContactFetchViewL") );
       
   746     PRINT1 ( _L("CPcsContactStore::CreateContactFetchViewL: URI=%S"),
       
   747              &*iUri );
   705 	
   748 	
   706 	// Create the view definition
   749 	// Create the view definition
   707     CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
   750     CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
   708 	CleanupStack::PushL( viewDef );
   751 	CleanupStack::PushL( viewDef );
   709 
   752 
   710   // Create the View Name for the view
   753     // Create the View Name for the view
   711   // The views are named as PCSView_<uri>		
   754     // The views are named as PCSView_<uri>		
   712 	HBufC* viewName = HBufC::NewL(KBufferMaxLen);
   755 	HBufC* viewName = HBufC::NewL(KBufferMaxLen);
   713 	viewName->Des().Append(KPcsViewPrefix);
   756 	viewName->Des().Append(KPcsViewPrefix);
   714 	viewName->Des().Append(iUri->Des());
   757 	viewName->Des().Append(iUri->Des());
   715 	CleanupStack::PushL(viewName);
   758 	CleanupStack::PushL(viewName);
   716 
   759 
   765 		                         *sortOrder);
   808 		                         *sortOrder);
   766 
   809 
   767         CleanupStack::Pop(); // iContactViewBase
   810         CleanupStack::Pop(); // iContactViewBase
   768         CleanupStack::PopAndDestroy(sortOrder);	
   811         CleanupStack::PopAndDestroy(sortOrder);	
   769         CleanupStack::PopAndDestroy(textbuffer);
   812         CleanupStack::PopAndDestroy(textbuffer);
   770     
   813 
   771     	// Close the resouce File
   814     	// Close the resouce File
   772         iResourceFile.Close();                        
   815         iResourceFile.Close();
   773 	
       
   774 	}
   816 	}
   775 	else if ( iUri->CompareC(KVPbkDefaultCntDbURI) == 0)
   817 	else if ( iUri->CompareC(KVPbkDefaultCntDbURI) == 0)
   776     {       
   818     {       
   777         // For phone contacts DB we use the shared "AllContacts" -view so that 
   819         // For phone contacts DB we use the shared "AllContacts" -view so that 
   778         // we don't need to generate a new view. This way we save some RAM
   820         // we don't need to generate a new view. This way we save some RAM
   805 		viewDef->SetUriL(iUri->Des());
   847 		viewDef->SetUriL(iUri->Des());
   806 		viewDef->SetType( EVPbkContactsView );
   848 		viewDef->SetType( EVPbkContactsView );
   807 			
   849 			
   808 		// Set name for the view
   850 		// Set name for the view
   809 		viewDef->SetNameL( *viewName );	
   851 		viewDef->SetNameL( *viewName );	
   810 		__LATENCY_MARK ( _L("CPcsContactStore::CreateContactFetchViewL===== create view start") );
   852 		__LATENCY_MARK ( _L("CPcsContactStore::CreateContactFetchViewL ==== create view start") );
   811     	iContactViewBase = iContactManager->CreateContactViewLC( 
   853     	iContactViewBase = iContactManager->CreateContactViewLC( 
   812 		                         *this, 
   854 		                         *this, 
   813 		                         *viewDef, 
   855 		                         *viewDef, 
   814 		                         *sortOrderPhone );
   856 		                         *sortOrderPhone );
   815 		__LATENCY_MARKEND ( _L("CPcsContactStore::CreateContactFetchViewL ==== create view end") );	                         
   857 		__LATENCY_MARKEND ( _L("CPcsContactStore::CreateContactFetchViewL ==== create view end") );	                         
   839 	TRequestStatus timerStatus;
   881 	TRequestStatus timerStatus;
   840 	iTimer.Cancel();
   882 	iTimer.Cancel();
   841 	
   883 	
   842     switch( iNextState)
   884     switch( iNextState)
   843     {
   885     {
   844 	   	case ECreateView :
   886         case ECreateView:
   845 			CreateContactFetchViewL();
   887             PRINT1 ( _L("CPcsContactStore::RunL(ECreateView): URI=%S. Create contact view to fetch data from store"),
   846 		    break;
   888                      &*iUri );
       
   889             CreateContactFetchViewL();
       
   890             break;
       
   891 
       
   892         case EFetchAllLinks:
       
   893             PRINT1 ( _L("CPcsContactStore::RunL(EFetchAllLinks): URI=%S. Get all contact links in initial view"),
       
   894                      &*iUri );
       
   895             FetchAllInitialContactLinksL();
       
   896             break;
   847    		
   897    		
   848 	   	case EFetchContactBlock:
   898 	   	case EFetchContactBlock:
   849 	   		PRINT ( _L("Issuing the fetch request for next block") );
   899             PRINT4 ( _L("CPcsContactStore::RunL(EFetchContactBlock): URI=%S. Issuing fetch request for next block of max %d contacts (%d/%d fetched)"),
   850 			FetchlinksL();
   900                      &*iUri, KLinksToFetchInOneGo, iFetchBlockUpperNumber, iInitialContactCount);
       
   901             FetchContactsBlockL();
   851 			break;
   902 			break;
   852 	   	
   903 	   	
   853 	   	case EComplete:
   904 	   	case EComplete:
   854 		    PRINT ( _L("Contacts Caching FINISHED") );
   905 		    PRINT3 ( _L("CPcsContactStore::RunL(EComplete): URI=%S. Contacts Caching FINISHED, (%d/%d contacts fetched)"),
   855 		    PRINT_BOOT_PERFORMANCE ( _L("Contacts Caching FINISHED") );
   906 		             &*iUri, iFetchBlockUpperNumber, iInitialContactCount );
       
   907 		    PRINT1_BOOT_PERFORMANCE ( _L("CPcsContactStore::RunL(EComplete): URI=%S. Contacts Caching FINISHED"),
       
   908 		                              &*iUri );
   856 		    break;
   909 		    break;
   857     }
   910     }
   858 }
   911 }
   859 
   912 
   860 // ---------------------------------------------------------------------------------
   913 // ---------------------------------------------------------------------------------
   861 // Called in case of any errros 
   914 // Called in case of any errros 
   862 // ---------------------------------------------------------------------------------
   915 // ---------------------------------------------------------------------------------
   863 TInt CPcsContactStore::RunError(TInt /*aError*/) 
   916 TInt CPcsContactStore::RunError(TInt /*aError*/) 
   864 {
   917 {
   865 	PRINT ( _L(" Enter CPcsContactStore:: CPcsContactStore::RunError()") );
   918 	PRINT ( _L("Enter CPcsContactStore::RunError") );
   866 
   919 
   867 	PRINT1 ( _L(" CPcsContactStore:: RunError().  Completing caching in contacts store %S with status ECachingCompleteWithErrors "), &(iUri->Des()));
   920 	PRINT1 ( _L("CPcsContactStore::RunError(): URI=%S. Completing caching with status ECachingCompleteWithErrors"),
       
   921 	         &*iUri );
   868     iObserver->UpdateCachingStatus(*iUri, ECachingCompleteWithErrors);
   922     iObserver->UpdateCachingStatus(*iUri, ECachingCompleteWithErrors);
   869  	PRINT ( _L(" End CPcsContactStore:: CPcsContactStore::RunError()") );
   923 
       
   924     PRINT ( _L("End CPcsContactStore::RunError") );
   870 	return KErrNone;
   925 	return KErrNone;
   871 }
   926 }
   872 
   927 
   873 // ---------------------------------------------------------------------------------
   928 // ---------------------------------------------------------------------------------
   874 // Read the fields to cache from the central repository
   929 // Read the fields to cache from the central repository
   906 }
   961 }
   907 
   962 
   908 // ---------------------------------------------------------------------------------
   963 // ---------------------------------------------------------------------------------
   909 // Issues request to active object to call RunL method
   964 // Issues request to active object to call RunL method
   910 // ---------------------------------------------------------------------------------
   965 // ---------------------------------------------------------------------------------
   911 void CPcsContactStore::IssueRequest()
   966 void CPcsContactStore::IssueRequest( TInt aTimeDelay )
   912 {
   967 {
   913 	TRequestStatus* status = &iStatus;
   968     if( IsActive() ) // cannot activate allready active object
   914 	User::RequestComplete( status, KErrNone );
   969         return;
   915 	SetActive();
   970 
   916 }
   971     if ( 0 == aTimeDelay )
   917 // Creates a sort order depending on the fields specified in the cenrep
   972     {
   918 // when calling this function pass the masterList (i.e list containing all the
   973         TRequestStatus* status = &iStatus;
   919 // vpbk fields)
   974         User::RequestComplete( status, KErrNone );
       
   975     }
       
   976     else
       
   977     {
       
   978         PRINT1 ( _L("CPcsContactStore::IssueRequest: Applying delay of %d milliseconds"),
       
   979                  aTimeDelay );
       
   980 
       
   981         iTimer.After( iStatus, aTimeDelay);
       
   982     }
       
   983 
       
   984     SetActive();
       
   985 }
       
   986 
       
   987 // ---------------------------------------------------------------------------------
       
   988 // Creates a sort order depending on the fields specified 
       
   989 // in the cenrep when calling this function pass the 
       
   990 // masterList (i.e list containing all the vpbk fields)
   920 // ---------------------------------------------------------------------------------
   991 // ---------------------------------------------------------------------------------
   921 void CPcsContactStore::CreateSortOrderL(const MVPbkFieldTypeList& aMasterList)
   992 void CPcsContactStore::CreateSortOrderL(const MVPbkFieldTypeList& aMasterList)
   922 {
   993 {
   923 		
   994 		
   924 	TInt count = aMasterList.FieldTypeCount();
   995 	TInt count = aMasterList.FieldTypeCount();