phonebookengines/VirtualPhonebook/VPbkSimStore/src/CFindView.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Sim store filtered contact view implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CFindView.h"
       
    20 
       
    21 // From VPbkSimStore
       
    22 #include "CContactStore.h"
       
    23 #include "CContactView.h"
       
    24 #include "CViewContact.h"
       
    25 #include "CContactLink.h"
       
    26 
       
    27 // VPbkEng
       
    28 #include <MVPbkSimStoreOperation.h>
       
    29 #include <MVPbkSimCntView.h>
       
    30 #include <CVPbkSimContact.h>
       
    31 #include <RVPbkStreamedIntArray.h>
       
    32 #include <MVPbkContactViewObserver.h>
       
    33 #include <MVPbkSimCntStore.h>
       
    34 #include <MVPbkContactLink.h>
       
    35 #include <CVPbkContactFindPolicy.h>
       
    36 #include <CVPbkContactNameConstructionPolicy.h>
       
    37 #include <CVPbkContactViewSortPolicy.h>
       
    38 #include <MVPbkContactBookmarkCollection.h>
       
    39 #include <RVPbkStreamedIntArray.h>
       
    40 
       
    41 // System includes
       
    42 #include <cntviewbase.h>
       
    43 #include <featmgr.h>
       
    44 
       
    45 // Debugging headers
       
    46 #include <VPbkProfile.h>
       
    47 #include <VPbkDebug.h>
       
    48 
       
    49 namespace VPbkSimStore {
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // CFindView::CFindView
       
    53 // --------------------------------------------------------------------------
       
    54 //
       
    55 inline CFindView::CFindView( CContactView& aAllContactsView ) :        
       
    56         CFindViewBase( aAllContactsView, aAllContactsView, ETrue  )
       
    57     {
       
    58     }
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CFindView::~CFindView
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 CFindView::~CFindView()
       
    65     {
       
    66     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
    67         "VPbkSimStore:CFindView(%x)::~CFindView"), this );
       
    68          
       
    69     delete iFindOperation;
       
    70     delete iFindPolicy;
       
    71     iAlwaysIncluded.Close();
       
    72     iSIMMatchedContacts.Close();
       
    73     }
       
    74 
       
    75 // --------------------------------------------------------------------------
       
    76 // CFindView::ConstructL
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 void CFindView::ConstructL( const MDesCArray& aFindStrings,
       
    80         MVPbkContactViewObserver& aExternalViewObserver,
       
    81         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts,
       
    82         RFs& aRFs )
       
    83     {
       
    84     CVPbkContactFindPolicy::TParam findPolicyParams(
       
    85         iAllContactsView.Store().MasterFieldTypeList(), aRFs );
       
    86     iFindPolicy = CVPbkContactFindPolicy::NewL( findPolicyParams );
       
    87     BaseConstructL( aExternalViewObserver, aFindStrings, 
       
    88         *iFindPolicy );
       
    89     
       
    90     SetAlwaysIncludedContactsL( aAlwaysIncludedContacts );
       
    91     }
       
    92     
       
    93 // --------------------------------------------------------------------------
       
    94 // CFindView::NewLC
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 CFindView* CFindView::NewLC(      
       
    98         const MDesCArray& aFindStrings,
       
    99         CContactView& aAllContactsView,
       
   100         MVPbkContactViewObserver& aExternalViewObserver,
       
   101         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts,
       
   102         RFs& aRFs )
       
   103     {
       
   104     CFindView* self = new ( ELeave ) CFindView( aAllContactsView );
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL( aFindStrings, aExternalViewObserver,
       
   107         aAlwaysIncludedContacts, aRFs );
       
   108     CleanupStack::Check( self );
       
   109     return self;
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // CFindView::SetAlwaysIncludedContactsL
       
   114 // --------------------------------------------------------------------------
       
   115 //    
       
   116 void CFindView::SetAlwaysIncludedContactsL( 
       
   117         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts )
       
   118     {
       
   119     iAlwaysIncluded.Reset();
       
   120 
       
   121     if ( aAlwaysIncludedContacts )
       
   122         {
       
   123         MVPbkContactStore& store = ParentObject().ContactStore();
       
   124         const TInt count = aAlwaysIncludedContacts->Count();
       
   125         for ( TInt i = 0; i < count; ++i )
       
   126             {
       
   127             const CContactLink* bookmark =            
       
   128                 dynamic_cast<const CContactLink*>( 
       
   129                     &aAlwaysIncludedContacts->At( i ) );
       
   130             // If bookmark was from VPbkCntModel and if it's from same store
       
   131             // as this view then it's added to array.
       
   132             if ( bookmark &&
       
   133                  &bookmark->ContactStore() == &store )
       
   134                 {
       
   135                 iAlwaysIncluded.AppendL( bookmark->SimIndex() );
       
   136                 }
       
   137             }
       
   138         }
       
   139     }
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CFindView::ContactViewReady
       
   143 // --------------------------------------------------------------------------
       
   144 //    
       
   145 void CFindView::ContactViewReady( MVPbkContactViewBase& /*aView*/ )
       
   146     {
       
   147     // CFindView is only interested in ContactViewReadyForFiltering that
       
   148     // will start an asynchronous native match.
       
   149     }
       
   150     
       
   151 // --------------------------------------------------------------------------
       
   152 // CFindView::ContactViewReadyForFiltering
       
   153 // --------------------------------------------------------------------------
       
   154 //    
       
   155 void CFindView::ContactViewReadyForFiltering(
       
   156         MParentViewForFiltering& aView )
       
   157     {
       
   158     // Cancel existing match if aready active.
       
   159     delete iFindOperation;
       
   160     iFindOperation = NULL;
       
   161     
       
   162     // Parent view is ready. Start asynchronous find from native SIM view.
       
   163     TRAPD( error,
       
   164         {
       
   165         iFindOperation = 
       
   166             iAllContactsView.NativeView().ContactMatchingPrefixL(
       
   167                 FindStrings(), *this );
       
   168         });
       
   169     
       
   170     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   171         "VPbkSimStore:CFindView(%x)::ContactViewReadyForFiltering:\
       
   172         findOp(%x)"), this, iFindOperation ); 
       
   173         
       
   174     if ( error != KErrNone )
       
   175         {
       
   176         ContactViewError( aView, error, EFalse );
       
   177         }
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CFindView::MatchL
       
   182 // --------------------------------------------------------------------------
       
   183 //
       
   184 void CFindView::MatchL( RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   185     {
       
   186     CleanupResetAndDestroyPushL( aMatchedContacts );
       
   187     if (iAlwaysIncluded.Count() == 0)
       
   188         {
       
   189         // No always included contacts. The match can be done using
       
   190         // only the SIM view matched contacts.
       
   191         CViewContact* viewContact = CViewContact::NewL( iAllContactsView, 
       
   192             SortOrder() );
       
   193         CleanupStack::PushL(viewContact);
       
   194         MVPbkSimCntStore& simStore = iAllContactsView.Store().NativeStore();
       
   195         const TInt count = iSIMMatchedContacts.Count();
       
   196         for (TInt i = 0; i < count; ++i)
       
   197             {
       
   198             const TDesC8* cntBuf = simStore.AtL( iSIMMatchedContacts[i] );
       
   199             if ( cntBuf )
       
   200                 {
       
   201                 CVPbkSimContact* cnt = 
       
   202                     CVPbkSimContact::NewL( *cntBuf, simStore );
       
   203                 CleanupStack::PushL( cnt );
       
   204                 viewContact->SetSimContactL( *cnt );
       
   205                 if ( IsMatchL( *viewContact ) )
       
   206                     {
       
   207                     // Keep the order of the contacts same and move contact
       
   208                     // from iContactsModelMatchContacts to aMatchedContacts
       
   209                     aMatchedContacts.AppendL( cnt );
       
   210                     CleanupStack::Pop( cnt );
       
   211                     }
       
   212                 else
       
   213                     {
       
   214                     CleanupStack::PopAndDestroy( cnt );
       
   215                     }
       
   216                 }
       
   217             }   
       
   218         CleanupStack::PopAndDestroy( viewContact );
       
   219         }
       
   220     else
       
   221         {
       
   222         // Do it slowly by looping all the parent view contacts.
       
   223         const TInt contactCount = iParentView.ContactCountL();
       
   224         for ( TInt i = 0; i < contactCount; ++i )
       
   225             {
       
   226             // iParentView is always VPbkCntModel view and the contacts type
       
   227             // is CViewContact
       
   228             const CViewContact& candidate = static_cast<const CViewContact&>(
       
   229                 iParentView.ContactAtL( i ) );
       
   230             MatchContactL( candidate, aMatchedContacts );
       
   231             }
       
   232         }
       
   233     
       
   234     // Free the memory
       
   235     iSIMMatchedContacts.Reset(); 
       
   236     CleanupStack::Pop( &aMatchedContacts );
       
   237     }
       
   238         
       
   239 // --------------------------------------------------------------------------
       
   240 // CFindView::ViewFindCompleted
       
   241 // --------------------------------------------------------------------------
       
   242 //      
       
   243 void CFindView::ViewFindCompleted( 
       
   244         MVPbkSimCntView& aSimCntView,
       
   245         const RVPbkStreamedIntArray& aSimIndexArray )
       
   246     {
       
   247     TRAPD( error, ViewFindCompletedL( aSimCntView, aSimIndexArray ) );
       
   248     VPBK_PROFILE_END(VPbkProfile::ESimStoreFind);    
       
   249     
       
   250     if ( error != KErrNone )
       
   251         {
       
   252         ContactViewError( *this, error, EFalse );
       
   253         }
       
   254     }
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CFindView::ViewFindError
       
   258 // --------------------------------------------------------------------------
       
   259 //                  
       
   260 void CFindView::ViewFindError( 
       
   261         MVPbkSimCntView& /*aSimCntView*/, 
       
   262         TInt aError )
       
   263     {
       
   264     ContactViewError( *this, aError, EFalse );
       
   265     }
       
   266 
       
   267 // --------------------------------------------------------------------------
       
   268 // CFindView::UpdateFilterL
       
   269 // --------------------------------------------------------------------------
       
   270 //
       
   271 void CFindView::UpdateFilterL( 
       
   272         const MDesCArray& aFindWords,
       
   273         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts )
       
   274     {
       
   275     SetFindStringsL( aFindWords );
       
   276     SetAlwaysIncludedContactsL( aAlwaysIncludedContacts );
       
   277     ActivateContactMatchL();
       
   278     }
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CFindView::IsNativeMatchingRequestActive
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 TBool CFindView::IsNativeMatchingRequestActive()
       
   285     {
       
   286     if ( iFindOperation )
       
   287         {
       
   288         return ETrue;
       
   289         }
       
   290     return EFalse;
       
   291     }
       
   292     
       
   293 // --------------------------------------------------------------------------
       
   294 // CFindView::IsContactAlwaysIncluded
       
   295 // --------------------------------------------------------------------------
       
   296 //
       
   297 TBool CFindView::IsContactAlwaysIncluded( const CViewContact& aContact ) const
       
   298     {
       
   299     return iAlwaysIncluded.Find( aContact.SimIndex() ) != KErrNotFound;
       
   300     }
       
   301     
       
   302 // --------------------------------------------------------------------------
       
   303 // CFindView::DoContactAddedToViewL
       
   304 // --------------------------------------------------------------------------
       
   305 //
       
   306 void CFindView::DoContactAddedToViewL(
       
   307         MVPbkContactViewBase& aView, 
       
   308         TInt aIndex, 
       
   309         const MVPbkContactLink& /*aContactLink*/,
       
   310         RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   311     {
       
   312     CleanupResetAndDestroyPushL( aMatchedContacts );
       
   313     if ( &iParentView == &aView )
       
   314         {
       
   315         const CViewContact& viewContact = static_cast<const CViewContact&>(
       
   316             iParentView.ContactAtL( aIndex ) );
       
   317         if ( IsMatchL( viewContact ) )
       
   318             {
       
   319             MVPbkSimCntStore& simStore = 
       
   320                 iAllContactsView.Store().NativeStore();
       
   321             const TDesC8* cntBuf = simStore.AtL( viewContact.SimIndex() );
       
   322             if ( cntBuf )
       
   323                 {
       
   324                 CVPbkSimContact* cnt = 
       
   325                     CVPbkSimContact::NewL( *cntBuf, simStore );
       
   326                 CleanupStack::PushL( cnt );
       
   327                 
       
   328                 // We have to insert the new contact to the correct
       
   329                 // location. VPbk sort policy is used for that.
       
   330                 CVPbkContactViewSortPolicy::TParam param(
       
   331                     iAllContactsView.Store().MasterFieldTypeList(), SortOrder() );
       
   332                 CVPbkContactViewSortPolicy* sortPolicy = 
       
   333                     CVPbkContactViewSortPolicy::NewL( param );
       
   334                 CleanupStack::PushL( sortPolicy );
       
   335                 sortPolicy->SortStartL();
       
   336                 CViewContact* current = 
       
   337                     CViewContact::NewL( iAllContactsView, SortOrder() );
       
   338                 CleanupStack::PushL( current );
       
   339                 
       
   340                 const TInt count = aMatchedContacts.Count();
       
   341                 TInt index = KErrNotFound;
       
   342                 for ( TInt i = 0; i < count && index == KErrNotFound; ++i )
       
   343                     {
       
   344                     current->SetSimContactL( *aMatchedContacts[i] );
       
   345                     TInt res = 
       
   346                         sortPolicy->CompareContacts( viewContact, *current );
       
   347                     // CompareContacts returns -1, 0 or 1.
       
   348                     if ( res < 0 )
       
   349                         {
       
   350                         index = i;
       
   351                         }
       
   352                     }
       
   353                 sortPolicy->SortCompleted();
       
   354                 CleanupStack::PopAndDestroy( 2, sortPolicy ); // current
       
   355                 if ( index != KErrNotFound )
       
   356                     {
       
   357                     // Contact is inserted to matched contacts list
       
   358                     aMatchedContacts.InsertL( cnt, index );
       
   359                     }
       
   360                 else
       
   361                     {
       
   362                     // List is empty so append contact to it
       
   363                     aMatchedContacts.AppendL( cnt );
       
   364                     }
       
   365                 CleanupStack::Pop( cnt );
       
   366                 }
       
   367             }
       
   368         }
       
   369     CleanupStack::Pop( &aMatchedContacts );
       
   370     }
       
   371 
       
   372 // --------------------------------------------------------------------------
       
   373 // CFindView::ViewFindCompletedL
       
   374 // --------------------------------------------------------------------------
       
   375 //  
       
   376 void CFindView::ViewFindCompletedL( 
       
   377         MVPbkSimCntView& /*aSimCntView*/,
       
   378         const RVPbkStreamedIntArray& aSimIndexArray )
       
   379     {
       
   380     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   381         "VPbkSimStore:CFindView(%x)::ViewFindCompletedL:found %d contacts"),
       
   382          this, aSimIndexArray.Count() );
       
   383          
       
   384     // Native SIM view find is ready.
       
   385     
       
   386     // Clean the table
       
   387     iSIMMatchedContacts.Reset();
       
   388     
       
   389     // Copy matched indexes
       
   390     const TInt count( aSimIndexArray.Count() );
       
   391     for ( TInt i(0); i < count; ++i )
       
   392         {
       
   393         iSIMMatchedContacts.AppendL( aSimIndexArray[i] );
       
   394         }
       
   395     
       
   396     // The operation is not needed after indexes have been copied
       
   397     delete iFindOperation;
       
   398     iFindOperation = NULL;
       
   399     
       
   400     // Call base class matching function. It will send internal observer
       
   401     // event that this view is ready for filtering.
       
   402     MatchContactsL();
       
   403     // Send view ready event to both internal&external observers.
       
   404     SendViewStateEventToObservers();
       
   405     }
       
   406 
       
   407 // --------------------------------------------------------------------------
       
   408 // CFindView::MatchContactL
       
   409 // --------------------------------------------------------------------------
       
   410 //  
       
   411 void CFindView::MatchContactL( const CViewContact& aViewContact,
       
   412             RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   413     {
       
   414     CleanupResetAndDestroyPushL( aMatchedContacts );
       
   415     // aContact matches if it's one of the always included contacts OR
       
   416     // (if it's one of Contacts Model matched contacts AND it also
       
   417     // passes our own match).
       
   418     TInt matchArrayIndex = KErrNotFound;
       
   419     TBool matched = EFalse;
       
   420     if ( IsContactAlwaysIncluded( aViewContact ) )
       
   421         {
       
   422         // Remove from match array to save memory
       
   423         RemoveFromMatchArrayIfFound( aViewContact );
       
   424         matched = ETrue;
       
   425         }
       
   426     else if ( IsSIMMatchL( aViewContact, matchArrayIndex ) )
       
   427         {
       
   428         // Remove from match array to save memory
       
   429         iSIMMatchedContacts.Remove( matchArrayIndex );
       
   430         iSIMMatchedContacts.Compress();
       
   431         
       
   432         if ( IsMatchL( aViewContact ) )
       
   433             {
       
   434             matched = ETrue;
       
   435             }
       
   436         }
       
   437     
       
   438     if ( matched )
       
   439         {
       
   440         // Contact matched.
       
   441         // CFindView owns its contacts so create a copy
       
   442         MVPbkSimCntStore& simStore = iAllContactsView.Store().NativeStore();
       
   443         const TDesC8* cntBuf = simStore.AtL( aViewContact.SimIndex() );
       
   444         if ( cntBuf )
       
   445             {
       
   446             CVPbkSimContact* cnt = 
       
   447                 CVPbkSimContact::NewL( *cntBuf, simStore );
       
   448             CleanupStack::PushL( cnt );
       
   449             aMatchedContacts.AppendL( cnt );
       
   450             CleanupStack::Pop( cnt );
       
   451             }
       
   452         }
       
   453     CleanupStack::Pop( &aMatchedContacts ); 
       
   454     }
       
   455 
       
   456 // --------------------------------------------------------------------------
       
   457 // CFindView::RemoveFromMatchArrayIfFound
       
   458 // --------------------------------------------------------------------------
       
   459 //  
       
   460 void CFindView::RemoveFromMatchArrayIfFound( const CViewContact& aViewContact )
       
   461     {
       
   462     TInt index = FindFromMatchArray( aViewContact );
       
   463     if ( index != KErrNotFound )
       
   464         {
       
   465         iSIMMatchedContacts.Remove( index );
       
   466         iSIMMatchedContacts.Compress();
       
   467         }
       
   468     }
       
   469 
       
   470 // --------------------------------------------------------------------------
       
   471 // CFindView::IsSIMMatchL
       
   472 // --------------------------------------------------------------------------
       
   473 //  
       
   474 TBool CFindView::IsSIMMatchL( const CViewContact& aContact,
       
   475         TInt& aMatchArrayIndex ) const
       
   476     {
       
   477     aMatchArrayIndex = FindFromMatchArray( aContact );
       
   478     return aMatchArrayIndex != KErrNotFound;
       
   479     }
       
   480 
       
   481 // --------------------------------------------------------------------------
       
   482 // CFindView::FindFromMatchArray
       
   483 // --------------------------------------------------------------------------
       
   484 //  
       
   485 TInt CFindView::FindFromMatchArray( const CViewContact& aContact ) const
       
   486     {
       
   487     return iSIMMatchedContacts.Find( aContact.SimIndex() );
       
   488     }
       
   489 } // namespace VPbkSimStore 
       
   490 // End of File