searchfw/plugins/contactsplugin/src/contactssearcher.cpp
changeset 0 f979ecb2b13e
child 5 42814f902fe6
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Plugin for contactsplugin information search.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //SYSTEM INCLUDES
       
    20 #include <utf.h>
       
    21 #include <e32std.h>
       
    22 #include <searchcondition.h>
       
    23 #include <searchtextsearcher.h>
       
    24 #include <searchdocumentid.h>
       
    25 #include <searchlightresult.h>
       
    26 #include <searchresult.h>
       
    27 #include <searchcommon.h>
       
    28 #include <CVPbkContactManager.h>
       
    29 #include <CVPbkContactStoreUriArray.h>
       
    30 #include <CVPbkContactFieldIterator.h>
       
    31 #include <CVPbkFieldTypeRefsList.h>
       
    32 #include <CVPbkContactLinkArray.h>
       
    33 #include <TVPbkContactStoreUriPtr.h>
       
    34 #include <MVPbkContactOperationBase.h>
       
    35 #include <MVPbkContactStoreList.h>
       
    36 #include <MVPbkContactStore.h>
       
    37 #include <MVPbkStoreContact.h>
       
    38 #include <MVPbkContactFieldData.h>
       
    39 #include <MVPbkContactFieldTextData.h>
       
    40 #include <MVPbkContactFieldDateTimeData.h>
       
    41 #include <MVPbkContactStoreProperties.h>
       
    42 #include <MVPbkContactLink.h>
       
    43 #include <VPbkEng.rsg>
       
    44 #include <VPbkContactStoreUris.h>
       
    45 #include <TVPbkWordParserCallbackParam.h>
       
    46 #include <TVPbkContactStoreUriPtr.h>
       
    47 #include <searchpluginobserver.h>
       
    48 
       
    49 #include <MVPbkContactViewBase.h>
       
    50 #include <MVPbkContactStoreInfo.h>
       
    51 #include <CVPbkContactViewDefinition.h>
       
    52 #include <CVPbkSortOrder.h>
       
    53 #include <MVPbkContactViewFiltering.h>
       
    54 #include <barsc.h>
       
    55 #include <barsread.h>
       
    56 #include <contactssearchresource.rsg>
       
    57 #include <bautils.h>
       
    58 #include <data_caging_path_literals.hrh>
       
    59 //USER INCLUDES
       
    60 #include "contactssearcher.h"
       
    61 #include "contactssearchplugindefines.h"
       
    62 #include <featmgr.h>
       
    63 //FORWARD DECLARATION
       
    64 TInt VPbkParseDataL( TAny* aParam );
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------------
       
    68 // 1st phase constructor
       
    69 // ---------------------------------------------------------------------------------
       
    70 //
       
    71 CContactsSearcher* CContactsSearcher::NewL( const RArray<TUid>& aContentIdArray, 
       
    72                                 const CSearchCondition& aCondition, 
       
    73                                 const TUid& aPluginId,
       
    74                                 MSearchPluginObserver& aObserver )
       
    75     {
       
    76 	CContactsSearcher* self = new ( ELeave ) CContactsSearcher( aPluginId );
       
    77 	CleanupStack::PushL( self );
       
    78 	self->ConstructL( aContentIdArray, aCondition,aObserver );
       
    79 	CleanupStack::Pop( self );
       
    80 	return self;
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------------
       
    85 // Destructor
       
    86 // ---------------------------------------------------------------------------------
       
    87 //
       
    88 CContactsSearcher::~CContactsSearcher() 
       
    89     {
       
    90     delete iFilterView;
       
    91     delete iContactViewBase;
       
    92 	
       
    93 	ifSession.Close();
       
    94 	if( iCurrentlyOpenStores)
       
    95         {	
       
    96         // Close all the stores opened
       
    97         if(iContactManager)
       
    98         {
       
    99             TRAP_IGNORE(iContactManager->ContactStoresL().CloseAll( *this ));
       
   100         }
       
   101         if(iCurrentlyOpenStores)
       
   102         {
       
   103             const TInt count = iCurrentlyOpenStores->Count();
       
   104             for ( TInt i = count - 1; i >= 0; --i )
       
   105                 {
       
   106                 iCurrentlyOpenStores->Remove( (*iCurrentlyOpenStores)[i] );
       
   107                 }
       
   108            delete iCurrentlyOpenStores;
       
   109 		       iCurrentlyOpenStores = NULL;
       
   110         	
       
   111         }
       
   112         }
       
   113     if ( iContactManager )
       
   114         {
       
   115  		delete iContactManager;
       
   116 		iContactManager = NULL;	
       
   117         }
       
   118 
       
   119     if ( iTextSearcher )
       
   120         {
       
   121  		delete iTextSearcher;
       
   122 		iTextSearcher = NULL;	
       
   123         }
       
   124         
       
   125    
       
   126     
       
   127    
       
   128     if(iAllContactItemsFromDb)
       
   129     {
       
   130         iAllContactItemsFromDb->ResetAndDestroy();   	
       
   131     	delete iAllContactItemsFromDb;
       
   132     	iAllContactItemsFromDb = NULL;
       
   133     }
       
   134     iContactArrayMapper.Reset();
       
   135     iCotnactDeletedList.Reset();
       
   136    	iHeavyResultsArray.ResetAndDestroy();
       
   137 	
       
   138     FeatureManager::UnInitializeLib();
       
   139 	
       
   140 }
       
   141 
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // A function that returns the contact link at given index of the iContactLinkArray
       
   145 // ---------------------------------------------------------------------------
       
   146 // 
       
   147 //
       
   148 MVPbkContactLink* CContactsSearcher::GetContactLinkAtIndexLC(TInt aIndex)
       
   149     {
       
   150     if(aIndex < iContactArrayMapper.Count()  && (EFalse == iCotnactDeletedList[aIndex] ))
       
   151         {
       
   152         if ( iAllContactItemsFromDb ) 
       
   153         	{
       
   154         	return iAllContactItemsFromDb->At(iContactArrayMapper[aIndex]).CloneLC();
       
   155         	}
       
   156         }
       
   157     else
       
   158         {
       
   159   	    return NULL;
       
   160         }
       
   161 
       
   162 return NULL;
       
   163 }
       
   164 
       
   165 // ---------------------------------------------------------------------------------
       
   166 // Releases the resources
       
   167 // ---------------------------------------------------------------------------------
       
   168 //
       
   169 void CContactsSearcher::Destroy() 
       
   170     {
       
   171 	if( IsActive() ) 
       
   172 	    {
       
   173 		CActive::Cancel();
       
   174 	    }
       
   175 	 else
       
   176 	    {
       
   177 	    CleanUp(); 	
       
   178 	    }   
       
   179 	    delete this; 
       
   180 }
       
   181 
       
   182 // ---------------------------------------------------------------------------------
       
   183 // Starts the search
       
   184 // ---------------------------------------------------------------------------------
       
   185 //
       
   186 void CContactsSearcher::SearchL()
       
   187     { 
       
   188     iSearchCancelled = EFalse;
       
   189 
       
   190     
       
   191    	iHeavyResultsArray.ResetAndDestroy();
       
   192    //	iContactLinkIndexArray.Reset();
       
   193    
       
   194     if(iAllContactItemsFromDb)
       
   195     {
       
   196      
       
   197 	   iAllContactItemsFromDb->ResetAndDestroy();   
       
   198     }
       
   199     iContactManager->ContactStoresL().OpenAllL( *this );
       
   200    
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------------
       
   204 // Returns true,f search is in progress
       
   205 // ---------------------------------------------------------------------------------
       
   206 //
       
   207 TBool CContactsSearcher::IsSearching() 
       
   208     {
       
   209 	return IsActive();
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------------
       
   213 // Cancels the search
       
   214 // ---------------------------------------------------------------------------------
       
   215 //
       
   216 void CContactsSearcher::CancelSearch() 
       
   217     {
       
   218     iSearchCancelled = ETrue;
       
   219 	CActive::Cancel();
       
   220 	
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------------
       
   224 // Gets the results 
       
   225 // ---------------------------------------------------------------------------------
       
   226 //
       
   227 void CContactsSearcher::GetResultsL( 
       
   228 	const RPointerArray<CSearchDocumentId>& aDocumentIdArray )
       
   229     {
       
   230 	
       
   231     //Check the aDocumentIdArray
       
   232     // For all the matching documentIds, fetch the corresponding
       
   233     // Heavy Result from iHeavyResultsArray and append it to heavyResultResponse
       
   234 	
       
   235 	RPointerArray<CSearchResult> heavyResultResponse; 
       
   236 	//Search through the aDocumentIdArray, and select those heavy results
       
   237 	// which belong to this plugin with the corresponding document id
       
   238 	for( TInt j = 0; j < aDocumentIdArray.Count(); j++ ) 
       
   239 	    {
       
   240 	    TUid pluginId;
       
   241 	    pluginId.iUid = aDocumentIdArray[j]->PluginId().iUid;
       
   242 	    if ( (  pluginId.iUid == iPluginId.iUid ) &&
       
   243 	         (aDocumentIdArray[j]->RepositoryId() == KSearchCClassContactsUid.iUid)
       
   244 	       )  
       
   245 	        {
       
   246 	        
       
   247             TInt myArrayIndex =aDocumentIdArray[j]->DocumentId()  ;
       
   248 
       
   249 	        heavyResultResponse.AppendL(iHeavyResultsArray[myArrayIndex]);        
       
   250 
       
   251 	        }             
       
   252 	    }
       
   253 		
       
   254 	iObserver->ResultsRetrieveCompleteL( heavyResultResponse );
       
   255 	heavyResultResponse.Reset();
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------------
       
   259 // Cancels the result retrieval
       
   260 // ---------------------------------------------------------------------------------
       
   261 //
       
   262 void CContactsSearcher::CancelResultsRetrieve() 
       
   263     {
       
   264 	
       
   265     }
       
   266 
       
   267 
       
   268 //---------------------------------------------------------------------------------
       
   269 // Returns the search progress
       
   270 //---------------------------------------------------------------------------------
       
   271 //
       
   272 void CContactsSearcher::GetSearchProgressL( TUid& /*aContentClassId*/, TInt& /*aCurrentDocument*/, TInt& /*aTotalDocuments*/ )
       
   273     {
       
   274 	
       
   275     }
       
   276 
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------------------------------
       
   280 // Called when a result is found matching the search criteria
       
   281 // ---------------------------------------------------------------------------------
       
   282 //
       
   283 void CContactsSearcher::HitL( TInt /*aResult*/ ) 
       
   284     {
       
   285 	// If search is still in progress, then process the search result
       
   286 	if( !iSearchCancelled ) 
       
   287 	    {
       
   288 		CSearchDocumentId* searchDocId = NULL;
       
   289 		
       
   290 		TBuf8<KBufferLength> tempDes; 
       
   291 		
       
   292 	    
       
   293 	    // Set the repository id
       
   294 		searchDocId = CSearchDocumentId::NewL( iTotalHits,iPluginId );
       
   295 		searchDocId->SetRepositoryIdL( KSearchCClassContactsUid.iUid );
       
   296 		CSearchLightResult* searchLightResult = CSearchLightResult::NewL( searchDocId );
       
   297         CleanupStack::PushL(searchLightResult);		
       
   298 		searchLightResult->SetContentClassId( KSearchCClassContactsUid );
       
   299 		searchLightResult->SetServiceId( KNullUid );
       
   300 		
       
   301 		 //HBufC8 *repositoryId = HBufC8::NewL( KBufferLength ) ;
       
   302          //repositoryId->Des().Append(KSpace);
       
   303 	     searchDocId->SetRepositoryIdL( KSearchCClassContactsUid.iUid );
       
   304          //delete repositoryId; 
       
   305          //repositoryId = NULL;
       
   306 	
       
   307 		iContactArrayMapper.Append(iCurrentDatatBaseItemIndex - 1);
       
   308 		
       
   309 	    // Form the heavy result and append it to the iHeavyResultsArray
       
   310 	    CSearchResult* tmpHeavyRes = NULL;
       
   311 	    tmpHeavyRes = CSearchResult::NewL(searchLightResult);
       
   312 	    CleanupStack::PushL(tmpHeavyRes);
       
   313 	    //Set the Title for the heavy result
       
   314 	    if ( iCurrentTitle )
       
   315 	        {
       
   316 		    tmpHeavyRes->SetTitleL( *iCurrentTitle );
       
   317 	        delete iCurrentTitle; iCurrentTitle = NULL;
       
   318 	        }
       
   319 	    
       
   320 	    //Set the snippet for the heavy result
       
   321 	    if ( iCurrentSnippet )
       
   322 	        {
       
   323 		    tmpHeavyRes->SetSnippetL(*iCurrentSnippet);
       
   324 	        delete iCurrentSnippet;iCurrentSnippet = NULL;
       
   325 	        }
       
   326 	    
       
   327 	    // Check if the contact is a phone contact or a sim contact
       
   328 	    // Accordingly set the folder id
       
   329 	    
       
   330 	    TInt tempIndex = iContactArrayMapper[iTotalHits];
       
   331 	    TVPbkContactStoreUriPtr uri = iAllContactItemsFromDb->At(tempIndex).ContactStore().StoreProperties().Uri();       
       
   332 	    TVPbkContactStoreUriPtr phoneMemoryUri( VPbkContactStoreUris::DefaultCntDbUri() );
       
   333  
       
   334         if ( uri.Compare( phoneMemoryUri,TVPbkContactStoreUriPtr::EContactStoreUriStoreType) == 0 )
       
   335             {
       
   336              searchLightResult->SetContentFolderId( KSearchCFolderPhoneUid );  
       
   337             }
       
   338         else
       
   339             {
       
   340          
       
   341 			searchLightResult->SetContentFolderId( KSearchCFolderSIMUid );         	
       
   342             }
       
   343 	    //Set the SetSnippetFormatL
       
   344 	    HBufC8 *snippetFormat = HBufC8::NewL( KBufferLength ) ;
       
   345 	    snippetFormat->Des().Append(KSpace);
       
   346 		tmpHeavyRes->SetSnippetFormatL( snippetFormat->Des() );
       
   347 		delete snippetFormat; 
       
   348 		snippetFormat = NULL;
       
   349 	    
       
   350 	    //Append the heavy result to the iHeavyResultsArray
       
   351 	    iHeavyResultsArray.AppendL(tmpHeavyRes);
       
   352 				
       
   353 		// Increment the iTotalHits and call ResultFoundL()
       
   354 		iTotalHits++;              
       
   355 	    iObserver->ResultFoundL( searchLightResult, iTotalHits, iTotalNumOfContactsSearched );
       
   356 
       
   357 
       
   358 	   CleanupStack::Pop(tmpHeavyRes); 
       
   359 	   CleanupStack::Pop(searchLightResult); 
       
   360 	   
       
   361 
       
   362 	   }
       
   363 	
       
   364 }
       
   365 
       
   366 HBufC8* CContactsSearcher::LaunchInfoL( const CSearchDocumentId& aDocumentID )
       
   367 {
       
   368      if((aDocumentID.PluginId() == iPluginId) &&
       
   369        (aDocumentID.RepositoryId() == KSearchCClassContactsUid.iUid))
       
   370          {
       
   371            //HBufC *temp = HBufC::NewL(KMaxFileName) ;
       
   372            //TPtr docIdPtr = temp->Des();
       
   373 								
       
   374            TInt myArrayIndex = aDocumentID.DocumentId();
       
   375 		   //CnvUtfConverter::ConvertToUnicodeFromUtf8( docIdPtr,aDocumentID.DocumentId() );
       
   376 		   //TLex16 myDocId(docIdPtr);
       
   377 	       if(myArrayIndex < iContactArrayMapper.Count()  && (EFalse == iCotnactDeletedList[myArrayIndex] ))
       
   378 	    	   {
       
   379                  if ( iAllContactItemsFromDb ) 
       
   380                 	 {
       
   381 	        	     MVPbkContactLink* launchContact = iAllContactItemsFromDb->At(iContactArrayMapper[myArrayIndex]).CloneLC();
       
   382 	                 
       
   383 	                 HBufC8 *launchInfo = launchContact->PackLC();
       
   384 	                 CleanupStack::Pop();
       
   385 	                 CleanupStack::Pop(); 
       
   386 	                 delete launchContact;
       
   387 	                 launchContact= NULL;
       
   388 	                 return launchInfo;
       
   389 	                 }
       
   390 	    	   }
       
   391 	       
       
   392            else
       
   393         	   {
       
   394                User::Leave( KErrNotFound );
       
   395         	   }
       
   396          
       
   397          }
       
   398      
       
   399      return NULL;
       
   400      
       
   401 	
       
   402 }
       
   403 // ---------------------------------------------------------------------------
       
   404 // From class MVPbkContactStoreListObserver.
       
   405 // ---------------------------------------------------------------------------
       
   406 //
       
   407 void CContactsSearcher::OpenComplete()
       
   408     {
       
   409      // If atleast one store is ready, 
       
   410      //       set iStoreReadyForAccessing = ETrue so that we can start the serach    
       
   411      // else
       
   412      //       Finish the search as no stores are available 
       
   413      if(iAtLeastOneStoreReady)
       
   414         {
       
   415         iStoreReadyForAccessing = ETrue;
       
   416         TRequestStatus* status = &iStatus;
       
   417     	User::RequestComplete( status, KErrNone );
       
   418     	SetActive();
       
   419     
       
   420         }
       
   421     else
       
   422         {
       
   423         //No stores available, so set the appropriate flag
       
   424         iNoContactStoreAvailable = ETrue;	
       
   425         }
       
   426 
       
   427   
       
   428     }
       
   429     
       
   430 // ---------------------------------------------------------------------------
       
   431 // From class MVPbkContactStoreObserver.
       
   432 // ---------------------------------------------------------------------------
       
   433 //
       
   434 void CContactsSearcher::StoreReady(MVPbkContactStore& aStore)
       
   435     {
       
   436     // This function is called every time a store is ready to be 
       
   437     // accessed.
       
   438     const TDesC& uri = aStore.StoreProperties().Uri().UriDes();
       
   439     if ( !iCurrentlyOpenStores->IsIncluded( uri ) )
       
   440         {
       
   441         TRAP_IGNORE(iCurrentlyOpenStores->AppendL( uri ));
       
   442         }
       
   443     // We know that atleast one store is ready, so we can set this flag to ETrue
       
   444     iAtLeastOneStoreReady = ETrue;
       
   445      
       
   446     
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // From class MVPbkContactStoreObserver.
       
   451 // ---------------------------------------------------------------------------
       
   452 //
       
   453 void CContactsSearcher::StoreUnavailable(
       
   454         MVPbkContactStore& /*aContactStore*/, TInt /*aReason*/)
       
   455     {
       
   456         //The store is not availble..so do notthing....
       
   457         //TRequestStatus* status = &iStatus;
       
   458         //User::RequestComplete( status, KErrNone );
       
   459      	//SetActive();
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------------------------
       
   463 // From class MVPbkContactStoreObserver.
       
   464 // ---------------------------------------------------------------------------
       
   465 //
       
   466 void CContactsSearcher::HandleStoreEventL(MVPbkContactStore& aContactStore, 
       
   467                 TVPbkContactStoreEvent aStoreEvent)
       
   468     {
       
   469     
       
   470     //if there is deletetion of contact, then we need to update
       
   471     MVPbkContactStore* storeInList = iContactManager->ContactStoresL().Find(aContactStore.StoreProperties().Uri());
       
   472 	  TInt contactLocation = 0;
       
   473 	if (storeInList !=NULL)
       
   474 	{
       
   475 		switch(aStoreEvent.iEventType)
       
   476 		{
       
   477 			case TVPbkContactStoreEvent::EContactDeleted:
       
   478 			contactLocation =  iAllContactItemsFromDb->Find(*aStoreEvent.iContactLink);	
       
   479 			if( KErrNotFound != contactLocation)
       
   480 			{
       
   481 				TInt tempLoc = iContactArrayMapper.Find(contactLocation);
       
   482 				if( KErrNotFound != tempLoc)
       
   483 				{
       
   484 					iCotnactDeletedList[tempLoc] = ETrue;
       
   485 				}
       
   486 			}
       
   487 			break;
       
   488 			
       
   489 			default:
       
   490 				break;
       
   491 		}
       
   492 			
       
   493 	}
       
   494       // Dont't show store events
       
   495      TRequestStatus* status = &iStatus;
       
   496      User::RequestComplete( status, KErrNone );
       
   497      SetActive();
       
   498     }
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 //  Callback Method.Called when one Retrieve operation is complete
       
   502 // ---------------------------------------------------------------------------
       
   503 //   
       
   504 void CContactsSearcher::VPbkSingleContactOperationComplete(
       
   505         MVPbkContactOperationBase& /*aOperation*/, MVPbkStoreContact* aContact)
       
   506     {
       
   507 
       
   508     delete iVPbkOperation;
       
   509     iVPbkOperation = NULL;
       
   510     
       
   511     // Delete the retrieved link
       
   512     //iAllContactItemsFromDb->Delete( iCurrentDatatBaseItemIndex );
       
   513     
       
   514     // Handle the fetched contact....
       
   515     TRAPD( res, HandleRetrievedContactL( aContact ) );
       
   516     if ( res != KErrNone )
       
   517         {
       
   518     
       
   519         }
       
   520     
       
   521     if(iAllContactsSearched)
       
   522         {
       
   523         TRequestStatus* status = &iStatus;
       
   524 	    User::RequestComplete( status, KErrNone );
       
   525 	    SetActive();
       
   526 	   
       
   527         }
       
   528         
       
   529     }
       
   530     
       
   531 // ---------------------------------------------------------------------------
       
   532 //  Callback Method.Called when one Retrieve operation fails.
       
   533 // ---------------------------------------------------------------------------
       
   534 //   
       
   535 void CContactsSearcher::VPbkSingleContactOperationFailed(
       
   536         MVPbkContactOperationBase& /*aOperation*/, TInt /*aError*/ )
       
   537     {
       
   538     //delete the operation, since its completed
       
   539     delete iVPbkOperation;
       
   540     iVPbkOperation = NULL;
       
   541     
       
   542     }
       
   543 
       
   544 
       
   545 
       
   546 // ---------------------------------------------------------------------------------
       
   547 // Implements cancellation of an outstanding request.
       
   548 // ---------------------------------------------------------------------------------
       
   549 //
       
   550 void CContactsSearcher::DoCancel() 
       
   551     {
       
   552 	
       
   553     }
       
   554 
       
   555 
       
   556 // ---------------------------------------------------------------------------------
       
   557 // The function is called by the active scheduler 
       
   558 // ---------------------------------------------------------------------------------
       
   559 //
       
   560 void CContactsSearcher::RunL() 
       
   561     {
       
   562     
       
   563 	if(  !DoActualSearchL() )
       
   564 	    {      
       
   565 	    ReportFinishedL();   
       
   566     	
       
   567 	    }
       
   568 	
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------------------------------
       
   572 // Called in case of any errros 
       
   573 // ---------------------------------------------------------------------------------
       
   574 //
       
   575 TInt CContactsSearcher::RunError(TInt /*aError*/) 
       
   576     {
       
   577 	return KErrNone;
       
   578     }
       
   579 
       
   580 // ---------------------------------------------------------------------------------
       
   581 // CContactsSearcher::CContactsSearcher() 
       
   582 // Constructor
       
   583 // ---------------------------------------------------------------------------------
       
   584 //
       
   585 CContactsSearcher::CContactsSearcher( const TUid& aPluginId ) :
       
   586 	CActive( CActive::EPriorityStandard ), 
       
   587 	iPluginId( aPluginId ),
       
   588 	iTotalNumOfContactsSearched(0),
       
   589 	iTotalHits(0),
       
   590 	iStoreReadyForAccessing(EFalse),
       
   591     iAtLeastOneStoreReady(EFalse),
       
   592     iAllContactsSearched (EFalse),
       
   593     iNoContactStoreAvailable (EFalse),
       
   594     iSearchStarted (EFalse),
       
   595     iSearchCancelled(EFalse)
       
   596     {
       
   597 	CActiveScheduler::Add( this );
       
   598     }
       
   599 
       
   600 // ---------------------------------------------------------------------------------
       
   601 // CContactsSearcher::ConstructL() 
       
   602 // 2nd phase constructor
       
   603 // ---------------------------------------------------------------------------------
       
   604 //
       
   605 void CContactsSearcher::ConstructL( const RArray<TUid>& /*aContentIdArray*/, 
       
   606 		                             const CSearchCondition& aCondition,
       
   607 		                             MSearchPluginObserver& aObserver ) 
       
   608     {
       
   609 
       
   610     iObserver = &aObserver;
       
   611 	User::LeaveIfError( ifSession.Connect() );
       
   612 
       
   613 	// initialize virtual phonebook with phonebook and sim contact uris.
       
   614 	CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
       
   615 	uriArray->AppendL( TVPbkContactStoreUriPtr(VPbkContactStoreUris::DefaultCntDbUri()));
       
   616     uriArray->AppendL( TVPbkContactStoreUriPtr( VPbkContactStoreUris::SimGlobalAdnUri() ) );
       
   617     
       
   618     
       
   619     iContactManager = CVPbkContactManager::NewL(*uriArray);
       
   620     CleanupStack::PopAndDestroy(uriArray); 
       
   621     
       
   622     // Create the container for currently open stores
       
   623     iCurrentlyOpenStores = CVPbkContactStoreUriArray::NewL();
       
   624      
       
   625     // create containers for holding the contacts data      
       
   626     iAllContactItemsFromDb = CVPbkContactLinkArray::NewL();
       
   627    
       
   628     
       
   629     //Create the text searcher
       
   630  	SetPriority( EPriorityStandard );
       
   631 	iTextSearcher = CSearchTextSearcher::NewL( *this );
       
   632     iTextSearcher->SetParametersL( aCondition );
       
   633 	
       
   634     iIsFilterView = EFalse;
       
   635     FeatureManager::InitializeLibL();
       
   636     }
       
   637 
       
   638 // ---------------------------------------------------------------------------------
       
   639 // Called when search is finished
       
   640 // ---------------------------------------------------------------------------------
       
   641 //
       
   642 void CContactsSearcher::ReportFinishedL()
       
   643     {
       
   644         //Search is completed, call the appropriate function
       
   645         for( TInt j = 0; j < iHeavyResultsArray.Count(); j++ ) 
       
   646         {
       
   647         	
       
   648            iCotnactDeletedList.Append(EFalse);  
       
   649         }
       
   650 		iObserver->SearchCompletedL( iTotalHits, iTotalNumOfContactsSearched );
       
   651        
       
   652     }
       
   653 
       
   654 // ---------------------------------------------------------------------------------
       
   655 // Will be called when the CActive::Cancel() is called.. by DoCancel()
       
   656 // ---------------------------------------------------------------------------------
       
   657 //
       
   658 void CContactsSearcher::CleanUp() 
       
   659     {	
       
   660     //Cleanup the resources
       
   661     // delete all the data members owned.
       
   662     
       
   663     delete iCurrentTitle;
       
   664 	iCurrentTitle = NULL;	
       
   665     
       
   666     delete iCurrentSnippet;
       
   667 	iCurrentSnippet = NULL;	
       
   668     
       
   669     delete iContactsDataTobeSearched ;
       
   670 	iContactsDataTobeSearched  = NULL;	
       
   671     
       
   672     delete iCurrentContactDataString;
       
   673 	iCurrentContactDataString = NULL;	
       
   674     
       
   675      
       
   676     if( iAllContactItemsFromDb)
       
   677         {
       
   678     	delete iAllContactItemsFromDb;
       
   679     	iAllContactItemsFromDb = NULL ;
       
   680         }
       
   681     
       
   682     
       
   683     if(iFieldTypeRefList )
       
   684         {    
       
   685         iFieldTypeRefList->Reset();
       
   686     	delete iFieldTypeRefList;
       
   687 		iFieldTypeRefList = NULL;	
       
   688         }
       
   689      delete iVPbkOperation;
       
   690 	 iVPbkOperation = NULL;
       
   691 	   
       
   692 	 delete iSearchStrings;
       
   693 	 iSearchStrings = NULL;
       
   694 	   
       
   695 	 delete iBufferForQuery;
       
   696 	 iBufferForQuery = NULL;
       
   697     
       
   698    
       
   699     
       
   700     }
       
   701 
       
   702 
       
   703 // ---------------------------------------------------------------------------------
       
   704 // Does the search for the file.  Called from RunL. 
       
   705 // Return:  ETrue: if more note items need to be searched. 
       
   706 //          EFalse: otherwise (i.e if iAllContactsSearched is set to true)
       
   707 // ---------------------------------------------------------------------------------
       
   708 //
       
   709 TBool CContactsSearcher::DoActualSearchL()
       
   710 	{
       
   711 	
       
   712 	    if (iNoContactStoreAvailable || iAllContactsSearched )
       
   713 	        { 
       
   714 	        // Either store is not available, or all data has been searched
       
   715 	        // so retrun EFalse
       
   716 		    return EFalse; 
       
   717 	        }
       
   718 	
       
   719     	if(iStoreReadyForAccessing ) 
       
   720 	        {   
       
   721             // Start the search if not yet started 	
       
   722 	        if(!iSearchStarted)
       
   723 	           {
       
   724 	   	       iSearchStarted = ETrue;
       
   725 		       FetchContactItemsFromDbAndSearchL();
       
   726 	 	       }
       
   727 	       
       
   728 	        }
       
   729 	    
       
   730 	    return ETrue;
       
   731 
       
   732 	}
       
   733 
       
   734 // ---------------------------------------------------------------------------
       
   735 // From class MVPbkContactFindObserver.
       
   736 // When using the FindL, check that the field type of the find result is correct
       
   737 // since the underlying db find isnt perfect, ex. uses SQL to optimize the find.
       
   738 // Test application doesn't check the contact fields, it might be possible
       
   739 // the a field whose type was not given in Find has been matched.
       
   740 // ---------------------------------------------------------------------------
       
   741 //
       
   742 
       
   743 // ---------------------------------------------------------------------------
       
   744 //  Fetches the contact items from the database.
       
   745 // ---------------------------------------------------------------------------
       
   746 //
       
   747 void CContactsSearcher::FetchContactItemsFromDbAndSearchL(void)
       
   748     {
       
   749     // We just fire a find function with null value to get all the links
       
   750     // in the contact store. Once we get the links, we extract data from them 
       
   751     // to be searched, and then pass the same to the text searcher
       
   752     
       
   753     CVPbkSortOrder* sortOrder = CVPbkSortOrder::NewL( iContactManager->FieldTypes() );
       
   754     CleanupStack::PushL( sortOrder );
       
   755         
       
   756     CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
       
   757     CleanupStack::PushL( viewDef );
       
   758     viewDef->SetType( EVPbkCompositeView );
       
   759          
       
   760     MVPbkContactStoreList& stores = iContactManager->ContactStoresL();
       
   761     TInt totalNumberOfContacts = 0;
       
   762     for ( TInt i(0); i < iCurrentlyOpenStores->Count(); ++i)
       
   763         {
       
   764    
       
   765         TInt numberOfContacts = stores.Find((*iCurrentlyOpenStores)[i])->StoreInfo().NumberOfContactsL();
       
   766         totalNumberOfContacts += numberOfContacts;
       
   767         if ( numberOfContacts != 0)
       
   768             {
       
   769             CVPbkContactViewDefinition* subViewDef = CVPbkContactViewDefinition::NewL();
       
   770             CleanupStack::PushL( subViewDef );
       
   771             subViewDef->SetUriL( (*iCurrentlyOpenStores)[i].UriDes() );
       
   772             viewDef->AddSubViewL( subViewDef );
       
   773             CleanupStack::Pop( subViewDef ); // Ownership changed.
       
   774             }
       
   775         }
       
   776     if (totalNumberOfContacts == 0)
       
   777         {
       
   778         iAllContactsSearched = ETrue;
       
   779         TRequestStatus* status = &iStatus;
       
   780         User::RequestComplete( status, KErrNone );
       
   781         SetActive();
       
   782         }
       
   783     iContactViewBase = iContactManager->CreateContactViewLC( 
       
   784                    *this, *viewDef, *sortOrder );
       
   785                 
       
   786     CleanupStack::Pop(); // CreateContactViewLC
       
   787     CleanupStack::PopAndDestroy( viewDef );
       
   788     CleanupStack::PopAndDestroy( sortOrder );
       
   789     }
       
   790     
       
   791 
       
   792     
       
   793 // ---------------------------------------------------------------------------
       
   794 // Handles the operations for a single contact after it is fetched
       
   795 // ---------------------------------------------------------------------------
       
   796 //   
       
   797 void CContactsSearcher::HandleRetrievedContactL( 
       
   798         MVPbkStoreContact* aContact )
       
   799     {
       
   800     
       
   801     // Take the ownership
       
   802     aContact->PushL();
       
   803     
       
   804     MVPbkContactGroup* myContactGroup= aContact->Group();
       
   805     //The retrieved contact can be a contact item or a contact group, Handle accordingly
       
   806     if(NULL == myContactGroup)
       
   807         {
       
   808         // The fetched contact item (and not a contact group.)
       
   809         //Fetch all the editable data of the fetched contact
       
   810         // and pass it to the iTextSearcher for searching	
       
   811         GetDataForSingleContactL( *aContact );
       
   812         iTotalNumOfContactsSearched++;
       
   813         if(!iSearchCancelled)
       
   814             {
       
   815             iTextSearcher->SearchL( *iCurrentContactDataString );	
       
   816             }
       
   817          
       
   818         }
       
   819     else
       
   820         {
       
   821     
       
   822         }
       
   823     
       
   824     CleanupStack::PopAndDestroy(); // aContact
       
   825     if(iCurrentContactDataString)
       
   826        {
       
   827        delete iCurrentContactDataString;
       
   828        iCurrentContactDataString = NULL;
       
   829        }
       
   830    
       
   831    
       
   832     // Start reading next contact
       
   833     if ( iAllContactItemsFromDb)  
       
   834     	{	
       
   835     	if ( !iSearchCancelled && (iAllContactItemsFromDb->Count() > iCurrentDatatBaseItemIndex ))
       
   836 	        {
       
   837 	        const MVPbkContactLink& link = iAllContactItemsFromDb->At( iCurrentDatatBaseItemIndex );
       
   838 	        iCurrentDatatBaseItemIndex++;
       
   839 	        
       
   840 	        iVPbkOperation = iContactManager->RetrieveContactL( link, *this );
       
   841 	        
       
   842 	        }
       
   843 	   		else
       
   844 	        {
       
   845 	        // All the contacts have been read so set the iAllContactsSearched flag
       
   846 	        iAllContactsSearched = ETrue;
       
   847 	       
       
   848 		    } 
       
   849     	}
       
   850       
       
   851      
       
   852     }
       
   853    
       
   854     
       
   855 // ---------------------------------------------------------------------------
       
   856 // Fetches the data from a particular contact 
       
   857 // ---------------------------------------------------------------------------
       
   858 //     
       
   859 void CContactsSearcher::GetDataForSingleContactL( MVPbkBaseContact& aContact )
       
   860     {
       
   861      // Fetches all the editable fields for a particular contact. The AddContactFieldsL() 
       
   862      // function fetches the data for a particular entry of the contact.
       
   863      //  iCurrentContactDataString will contain the final space separated string 
       
   864     if(iCurrentContactDataString)
       
   865         {
       
   866       	delete iCurrentContactDataString;
       
   867       	iCurrentContactDataString = NULL;
       
   868         }
       
   869     delete iCurrentSnippet;
       
   870    	iCurrentSnippet = NULL;
       
   871     
       
   872     // First fetch the last name and first name
       
   873     AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LASTNAME);
       
   874     AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FIRSTNAME);
       
   875     AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_COMPANYNAME);
       
   876     
       
   877     // Now the iCurrentContactDataString contains last name and first name. 
       
   878     // This forms  the title for our contact.
       
   879     if(iCurrentContactDataString)
       
   880         {
       
   881         if(iCurrentTitle)
       
   882             {
       
   883         	delete iCurrentTitle;
       
   884         	iCurrentTitle = NULL;
       
   885             }
       
   886         iCurrentTitle = HBufC::NewL(iCurrentContactDataString->Des().Length()); 
       
   887         TPtr ptr( iCurrentTitle->Des() );
       
   888         ptr.Append(iCurrentContactDataString->Des());
       
   889         //delete iCurrentContactDataString; iCurrentContactDataString = NULL;
       
   890         }
       
   891     else
       
   892         {
       
   893         iCurrentTitle = HBufC::NewL(KBufferLength);
       
   894       	TPtr ptr( iCurrentTitle->Des() );
       
   895       	ptr.Append( KUnnamedContact );
       
   896         }
       
   897         
       
   898     if(iCurrentSnippet)
       
   899             {
       
   900         	delete iCurrentSnippet;
       
   901         	iCurrentSnippet = NULL;
       
   902             }
       
   903     
       
   904      // Add the Phone number
       
   905     AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_MOBILEPHONEGEN); 
       
   906           
       
   907 	//AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LASTNAME);
       
   908 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LASTNAMEREADING);
       
   909 	//AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FIRSTNAME);
       
   910 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FIRSTNAMEREADING);
       
   911 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_PREFIX);
       
   912 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SUFFIX);
       
   913 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SECONDNAME);
       
   914 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LANDPHONEHOME);
       
   915 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_MOBILEPHONEHOME);
       
   916 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VIDEONUMBERHOME);
       
   917 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FAXNUMBERHOME);  
       
   918 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VOIPHOME); 
       
   919 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_EMAILHOME);
       
   920 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_URLHOME); 
       
   921 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLABELHOME);
       
   922 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOHOME  );
       
   923 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDREXTHOME); 
       
   924 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRSTREETHOME); 
       
   925 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLOCALHOME);  
       
   926 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRREGIONHOME); 
       
   927 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOSTCODEHOME );
       
   928 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRCOUNTRYHOME);
       
   929 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_JOBTITLE); 
       
   930 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_COMPANYNAME);
       
   931 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LANDPHONEWORK); 
       
   932 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_MOBILEPHONEWORK);
       
   933 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VIDEONUMBERWORK);
       
   934 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FAXNUMBERWORK);  
       
   935 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VOIPWORK); 
       
   936 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_EMAILWORK);
       
   937 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_URLWORK);  
       
   938 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLABELWORK);  
       
   939 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOWORK);  
       
   940 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDREXTWORK); 
       
   941 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRSTREETWORK); 
       
   942 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLOCALWORK);  
       
   943 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRREGIONWORK); 
       
   944 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOSTCODEWORK );
       
   945 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRCOUNTRYWORK);
       
   946 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LANDPHONEGEN);
       
   947 	//AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_MOBILEPHONEGEN); 
       
   948 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VIDEONUMBERGEN); 
       
   949 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_FAXNUMBERGEN);
       
   950 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_VOIPGEN);  
       
   951 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_POC);
       
   952 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SWIS );
       
   953 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SIP);
       
   954 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_EMAILGEN); 
       
   955 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_URLGEN);
       
   956 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLABELGEN);
       
   957 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOGEN);
       
   958 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDREXTGEN); 
       
   959 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRSTREETGEN);  
       
   960 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRLOCALGEN);
       
   961 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRREGIONGEN);  
       
   962 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRPOSTCODEGEN);
       
   963 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ADDRCOUNTRYGEN); 
       
   964 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_PAGERNUMBER);
       
   965 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_DTMFSTRING);  
       
   966 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_WVADDRESS);
       
   967 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_DATE);
       
   968 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_NOTE);  
       
   969 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_THUMBNAILPIC);
       
   970 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_RINGTONE); 
       
   971 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_CALLEROBJIMG);
       
   972 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_CALLEROBJTEXT);  
       
   973 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_MIDDLENAME);  
       
   974 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_DEPARTMENT ); 
       
   975 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ASSTNAME); 
       
   976 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SPOUSE);
       
   977 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_CHILDREN); 
       
   978 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ASSTPHONE);
       
   979 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_CARPHONE); 
       
   980     AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_ANNIVERSARY);
       
   981 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_SYNCCLASS);
       
   982 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_LOCPRIVACY);
       
   983 	AddContactFieldsL( aContact,R_VPBK_FIELD_TYPE_GENLABEL);
       
   984     
       
   985     //Set the snippet if not set yet
       
   986     if(!iCurrentSnippet) 
       
   987         {
       
   988         iCurrentSnippet =HBufC8::NewL(KSpace().Length()); 
       
   989         iCurrentSnippet->Des().Append( KSpace );
       
   990         }        
       
   991    
       
   992 }
       
   993 
       
   994 
       
   995 // ---------------------------------------------------------------------------
       
   996 // Add the data from contact fields
       
   997 // ---------------------------------------------------------------------------
       
   998 //     
       
   999 void CContactsSearcher::AddContactFieldsL(MVPbkBaseContact& aContact,TInt afieldtype)
       
  1000     {
       
  1001 	 
       
  1002 	 const MVPbkFieldType*  myContactDataField = 
       
  1003         iContactManager->FieldTypes().Find( afieldtype );
       
  1004 
       
  1005     CVPbkBaseContactFieldTypeIterator* itr = 
       
  1006         CVPbkBaseContactFieldTypeIterator::NewLC( *myContactDataField, 
       
  1007             aContact.Fields() );
       
  1008     while ( itr->HasNext() )
       
  1009         {
       
  1010         
       
  1011         const MVPbkBaseContactField* field = itr->Next();
       
  1012         // last name field is text data
       
  1013         HBufC* fieldToAdd = NULL;
       
  1014         
       
  1015        //Check if the contact filed is a date field or a text field
       
  1016         if( (field->FieldData()).DataType() == EVPbkFieldStorageTypeDateTime)
       
  1017             {
       
  1018         
       
  1019             const MVPbkContactFieldDateTimeData& myDate = MVPbkContactFieldDateTimeData::Cast( field->FieldData() );
       
  1020             TTime dateValue = myDate.DateTime();
       
  1021             TDateTime  xyz = dateValue.DateTime();
       
  1022             TInt yearDate = xyz.Year();
       
  1023             TInt monthDate = xyz.Month() + 1; //Add 1,since month starts from 0
       
  1024             TInt dayDate = xyz.Day() + 1;//Add 1,since day starts from 0
       
  1025                 
       
  1026             //Form the date filed .. DD/MM/YYYY
       
  1027             fieldToAdd = HBufC::NewLC( KMaxFileName );
       
  1028 		    TPtr fieldToAddPtr( fieldToAdd->Des() );
       
  1029 		    fieldToAddPtr.AppendNum( dayDate );
       
  1030 		    fieldToAddPtr.Append( KForwardSlash );
       
  1031 	    	fieldToAddPtr.AppendNum( monthDate );
       
  1032 		    fieldToAddPtr.Append( KForwardSlash );
       
  1033 		
       
  1034 	    	//The year is shown as YYYY. So append appropriate 
       
  1035 		   // number of zeros in the year
       
  1036 		   if(yearDate < 10)
       
  1037 		       {
       
  1038 			   fieldToAddPtr.AppendNum(0);
       
  1039 			   fieldToAddPtr.AppendNum(0);
       
  1040 			   fieldToAddPtr.AppendNum(0);
       
  1041 		       }
       
  1042 		  else if(yearDate < 100)
       
  1043 		      {
       
  1044 			  fieldToAddPtr.AppendNum(0);
       
  1045 			  fieldToAddPtr.AppendNum(0);
       
  1046 		      }
       
  1047 		 else if(yearDate < 1000)
       
  1048 		      {
       
  1049 		      fieldToAddPtr.AppendNum(0);
       
  1050 		
       
  1051 		      }
       
  1052 		
       
  1053 		fieldToAddPtr.AppendNum( yearDate );
       
  1054 		
       
  1055 			
       
  1056 		}
       
  1057         else if( (field->FieldData()).DataType() == EVPbkFieldStorageTypeText)
       
  1058             {
       
  1059         	
       
  1060             const MVPbkContactFieldTextData& data = 
       
  1061             MVPbkContactFieldTextData::Cast( field->FieldData() );
       
  1062         
       
  1063             fieldToAdd = data.Text().AllocLC();
       
  1064             }
       
  1065        else
       
  1066            {
       
  1067            CleanupStack::PopAndDestroy( itr );
       
  1068             return;	
       
  1069            }
       
  1070 
       
  1071       if(!iCurrentSnippet)
       
  1072          {
       
  1073         
       
  1074          switch(afieldtype)
       
  1075 		     {
       
  1076 			
       
  1077 			 case R_VPBK_FIELD_TYPE_LANDPHONEGEN:
       
  1078 			 case R_VPBK_FIELD_TYPE_LANDPHONEWORK:
       
  1079              case R_VPBK_FIELD_TYPE_LANDPHONEHOME:	
       
  1080              case R_VPBK_FIELD_TYPE_MOBILEPHONEWORK:
       
  1081              case R_VPBK_FIELD_TYPE_MOBILEPHONEGEN:
       
  1082              case R_VPBK_FIELD_TYPE_MOBILEPHONEHOME:		
       
  1083 		 
       
  1084 		     if(fieldToAdd->Des().Length())
       
  1085                  {
       
  1086                  iCurrentSnippet = HBufC8::NewL(KSpace().Length() + fieldToAdd->Des().Length() ) ;
       
  1087                  TPtr8 ptr8(iCurrentSnippet->Des()) ;
       
  1088                  TPtr16 ptr16(fieldToAdd->Des()) ;
       
  1089 	             CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr8,ptr16 );
       
  1090                  }
       
  1091                  break;
       
  1092 			default:
       
  1093 	    	     break;
       
  1094 	     	}
       
  1095             
       
  1096          	
       
  1097         }
       
  1098         if(iCurrentContactDataString)
       
  1099             {
       
  1100             iCurrentContactDataString = iCurrentContactDataString->ReAllocL( 
       
  1101                                         iCurrentContactDataString->Length() + 
       
  1102                                         KSpace().Length() + fieldToAdd->Des().Length() );	
       
  1103                                 
       
  1104             }
       
  1105         else
       
  1106             {
       
  1107     	    iCurrentContactDataString =HBufC::NewL(KSpace().Length() + fieldToAdd->Des().Length() ) ;
       
  1108     
       
  1109             }
       
  1110    	
       
  1111         iCurrentContactDataString->Des().Append( fieldToAdd->Des() );
       
  1112         iCurrentContactDataString->Des().Append( KSpace );
       
  1113         CleanupStack::PopAndDestroy( fieldToAdd );
       
  1114         }
       
  1115     
       
  1116     CleanupStack::PopAndDestroy( itr );
       
  1117 
       
  1118     }
       
  1119 
       
  1120 void CContactsSearcher::ContactViewReadyL(
       
  1121                 MVPbkContactViewBase& aView )
       
  1122     {
       
  1123     aView.RemoveObserver( *this );
       
  1124     if ( !iIsFilterView )
       
  1125         {    
       
  1126         iSearchStrings = new( ELeave ) CDesCArrayFlat( 2 );
       
  1127         for ( TInt i = 0; i < iTextSearcher->SearchKeyword()->MdcaCount(); ++i )
       
  1128             {
       
  1129             TPtrC searchWord = iTextSearcher->SearchKeyword()->MdcaPoint( i );
       
  1130             TVPbkWordParserCallbackParam param( &searchWord, iSearchStrings );
       
  1131             VPbkParseDataL( &param );
       
  1132             }
       
  1133     
       
  1134         if (iSearchStrings->MdcaCount() == 0)
       
  1135     	    {
       
  1136     	    iSearchStrings->AppendL(KNullDesC);
       
  1137     	    }
       
  1138 		 
       
  1139         iFilterView = iContactViewBase->ViewFiltering()->CreateFilteredViewLC(*this, *iSearchStrings, NULL );
       
  1140         CleanupStack::Pop();
       
  1141         iIsFilterView = ETrue;
       
  1142         return;
       
  1143         }
       
  1144     delete iVPbkOperation;
       
  1145     iVPbkOperation = NULL;
       
  1146 	   
       
  1147     delete iSearchStrings;
       
  1148     iSearchStrings = NULL;
       
  1149 	   
       
  1150     delete iBufferForQuery;
       
  1151     iBufferForQuery = NULL;
       
  1152 	
       
  1153     TInt cnt = aView.ContactCountL();
       
  1154     for ( TInt i = 0; i < cnt; ++i )
       
  1155         {
       
  1156         if ( iAllContactItemsFromDb ) 
       
  1157             {
       
  1158             iAllContactItemsFromDb->AppendL( aView.CreateLinkLC(i) );	
       
  1159             CleanupStack::Pop();
       
  1160             }
       
  1161         }
       
  1162 		
       
  1163     if ( iAllContactItemsFromDb ) 
       
  1164         {
       
  1165         if ( iAllContactItemsFromDb->Count() >0)
       
  1166             {
       
  1167             // If there are still contacts available, then retrieve them one by one
       
  1168             const MVPbkContactLink& link = iAllContactItemsFromDb->At( iCurrentDatatBaseItemIndex );
       
  1169             iCurrentDatatBaseItemIndex++;
       
  1170 		        //Retrieve the contact 
       
  1171             TRAP_IGNORE( iVPbkOperation = iContactManager->RetrieveContactL( link, *this ) ); 
       
  1172             }
       
  1173         else
       
  1174             {
       
  1175             // All contacts searched, set the appropriate flag
       
  1176             iAllContactsSearched = ETrue;
       
  1177             
       
  1178             // Active object needs to be activated.
       
  1179             TRequestStatus* status = &iStatus;
       
  1180             User::RequestComplete( status, KErrNone );
       
  1181             SetActive();
       
  1182             }
       
  1183         }
       
  1184     }
       
  1185 // --------------------------------------------------------------------
       
  1186 //Called when a view is ready for use.
       
  1187 // --------------------------------------------------------------------
       
  1188 //
       
  1189 void CContactsSearcher::ContactViewReady(
       
  1190                 MVPbkContactViewBase& aView )
       
  1191     {
       
  1192     TRAP_IGNORE(ContactViewReadyL(aView));
       
  1193     }
       
  1194 
       
  1195 // --------------------------------------------------------------------
       
  1196 // Called when a view is unavailable for a while. 
       
  1197 // --------------------------------------------------------------------
       
  1198 //
       
  1199 void CContactsSearcher::ContactViewUnavailable(
       
  1200                 MVPbkContactViewBase& aView )
       
  1201     {
       
  1202 	TRequestStatus* status = &iStatus;
       
  1203     User::RequestComplete( status, KErrNone );
       
  1204 	SetActive();
       
  1205 	return;
       
  1206     }
       
  1207 
       
  1208 // --------------------------------------------------------------------
       
  1209 // Called when a contact has been added to the view. 
       
  1210 // --------------------------------------------------------------------
       
  1211 //
       
  1212 void CContactsSearcher::ContactAddedToView(
       
  1213                 MVPbkContactViewBase& aView, 
       
  1214                 TInt aIndex, 
       
  1215                 const MVPbkContactLink& aContactLink )
       
  1216     {
       
  1217     }
       
  1218 
       
  1219 // --------------------------------------------------------------------
       
  1220 // Called when an error occurs in the view.
       
  1221 // --------------------------------------------------------------------
       
  1222 //
       
  1223 void CContactsSearcher::ContactRemovedFromView(
       
  1224                 MVPbkContactViewBase& aView, 
       
  1225                 TInt aIndex, 
       
  1226                 const MVPbkContactLink& aContactLink )
       
  1227     {
       
  1228     }
       
  1229 
       
  1230 // --------------------------------------------------------------------
       
  1231 // Called when a contact has been removed from a view.
       
  1232 // --------------------------------------------------------------------
       
  1233 //
       
  1234 void CContactsSearcher::ContactViewError(
       
  1235                 MVPbkContactViewBase& aView, 
       
  1236                 TInt aError, 
       
  1237                 TBool aErrorNotified )
       
  1238     {
       
  1239     }
       
  1240 
       
  1241 // ---------------------------------------------------------------------------
       
  1242 // A function that parser field data into words
       
  1243 // This function is used as a callback in the iContactManager->FindL()
       
  1244 // ---------------------------------------------------------------------------
       
  1245 // 
       
  1246 TInt VPbkParseDataL( TAny* aParam )
       
  1247     {
       
  1248     TVPbkWordParserCallbackParam* param = 
       
  1249     static_cast<TVPbkWordParserCallbackParam*>( aParam );
       
  1250     const TText* ptr = param->iStringToParse->Ptr();
       
  1251 	const TText* end = ptr + param->iStringToParse->Length();
       
  1252 	const TText* startOfWord = NULL;
       
  1253 	
       
  1254 	if ( FeatureManager::FeatureSupported( KFeatureIdJapanese ) ||
       
  1255 	     FeatureManager::FeatureSupported( KFeatureIdChinese )  ||
       
  1256 	     FeatureManager::FeatureSupported( KFeatureIdThai ) )
       
  1257 	    {
       
  1258 	    while(ptr != end)
       
  1259 	        {
       
  1260 	        if (*ptr >= 0x4e00)
       
  1261 	            {
       
  1262 	            TPtrC addWord(ptr, 1);
       
  1263 	            param->iWordArray->AppendL(addWord);	
       
  1264 	            }
       
  1265 		    ptr++;
       
  1266 	        }
       
  1267 	    }
       
  1268 	else
       
  1269 		{
       
  1270 	    FOREVER
       
  1271 		    {
       
  1272 		    // Other than alpha digit are separators
       
  1273 		    if (ptr==end || !TChar(*ptr).IsAlphaDigit())
       
  1274 			    {
       
  1275 			    if (startOfWord)
       
  1276 				    {
       
  1277 				    TPtrC addWord(startOfWord,ptr-startOfWord);
       
  1278 				    param->iWordArray->AppendL(addWord);
       
  1279 				    startOfWord=NULL;
       
  1280 				    }
       
  1281 			    if (ptr==end)
       
  1282 				    break;
       
  1283 			    }
       
  1284 		    else if (!startOfWord)
       
  1285 			    startOfWord=ptr;
       
  1286 		    ptr++;
       
  1287 		    }
       
  1288 		}
       
  1289 	return KErrNone;
       
  1290     }    
       
  1291 
       
  1292