phonebookengines/VirtualPhonebook/VPbkSimStore/src/CFindView.cpp
changeset 0 e686773b3f54
child 32 2828b4d142c0
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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     if (iAlwaysIncluded.Count() == 0)
       
   187         {
       
   188         // No always included contacts. The match can be done using
       
   189         // only the SIM view matched contacts.
       
   190         CViewContact* viewContact = CViewContact::NewL( iAllContactsView, 
       
   191             SortOrder() );
       
   192         CleanupStack::PushL(viewContact);
       
   193         MVPbkSimCntStore& simStore = iAllContactsView.Store().NativeStore();
       
   194         const TInt count = iSIMMatchedContacts.Count();
       
   195         for (TInt i = 0; i < count; ++i)
       
   196             {
       
   197             const TDesC8* cntBuf = simStore.AtL( iSIMMatchedContacts[i] );
       
   198             if ( cntBuf )
       
   199                 {
       
   200                 CVPbkSimContact* cnt = 
       
   201                     CVPbkSimContact::NewL( *cntBuf, simStore );
       
   202                 CleanupStack::PushL( cnt );
       
   203                 viewContact->SetSimContactL( *cnt );
       
   204                 if ( IsMatchL( *viewContact ) )
       
   205                     {
       
   206                     // Keep the order of the contacts same and move contact
       
   207                     // from iContactsModelMatchContacts to aMatchedContacts
       
   208                     aMatchedContacts.AppendL( cnt );
       
   209                     CleanupStack::Pop( cnt );
       
   210                     }
       
   211                 else
       
   212                     {
       
   213                     CleanupStack::PopAndDestroy( cnt );
       
   214                     }
       
   215                 }
       
   216             }   
       
   217         CleanupStack::PopAndDestroy( viewContact );
       
   218         }
       
   219     else
       
   220         {
       
   221         // Do it slowly by looping all the parent view contacts.
       
   222         const TInt contactCount = iParentView.ContactCountL();
       
   223         for ( TInt i = 0; i < contactCount; ++i )
       
   224             {
       
   225             // iParentView is always VPbkCntModel view and the contacts type
       
   226             // is CViewContact
       
   227             const CViewContact& candidate = static_cast<const CViewContact&>(
       
   228                 iParentView.ContactAtL( i ) );
       
   229             MatchContactL( candidate, aMatchedContacts );
       
   230             }
       
   231         }
       
   232     
       
   233     // Free the memory
       
   234     iSIMMatchedContacts.Reset(); 
       
   235     }
       
   236         
       
   237 // --------------------------------------------------------------------------
       
   238 // CFindView::ViewFindCompleted
       
   239 // --------------------------------------------------------------------------
       
   240 //      
       
   241 void CFindView::ViewFindCompleted( 
       
   242         MVPbkSimCntView& aSimCntView,
       
   243         const RVPbkStreamedIntArray& aSimIndexArray )
       
   244     {
       
   245     TRAPD( error, ViewFindCompletedL( aSimCntView, aSimIndexArray ) );
       
   246     VPBK_PROFILE_END(VPbkProfile::ESimStoreFind);    
       
   247     
       
   248     if ( error != KErrNone )
       
   249         {
       
   250         ContactViewError( *this, error, EFalse );
       
   251         }
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CFindView::ViewFindError
       
   256 // --------------------------------------------------------------------------
       
   257 //                  
       
   258 void CFindView::ViewFindError( 
       
   259         MVPbkSimCntView& /*aSimCntView*/, 
       
   260         TInt aError )
       
   261     {
       
   262     ContactViewError( *this, aError, EFalse );
       
   263     }
       
   264 
       
   265 // --------------------------------------------------------------------------
       
   266 // CFindView::UpdateFilterL
       
   267 // --------------------------------------------------------------------------
       
   268 //
       
   269 void CFindView::UpdateFilterL( 
       
   270         const MDesCArray& aFindWords,
       
   271         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts )
       
   272     {
       
   273     SetFindStringsL( aFindWords );
       
   274     SetAlwaysIncludedContactsL( aAlwaysIncludedContacts );
       
   275     ActivateContactMatchL();
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------
       
   279 // CFindView::IsNativeMatchingRequestActive
       
   280 // --------------------------------------------------------------------------
       
   281 //
       
   282 TBool CFindView::IsNativeMatchingRequestActive()
       
   283     {
       
   284     if ( iFindOperation )
       
   285         {
       
   286         return ETrue;
       
   287         }
       
   288     return EFalse;
       
   289     }
       
   290     
       
   291 // --------------------------------------------------------------------------
       
   292 // CFindView::IsContactAlwaysIncluded
       
   293 // --------------------------------------------------------------------------
       
   294 //
       
   295 TBool CFindView::IsContactAlwaysIncluded( const CViewContact& aContact ) const
       
   296     {
       
   297     return iAlwaysIncluded.Find( aContact.SimIndex() ) != KErrNotFound;
       
   298     }
       
   299     
       
   300 // --------------------------------------------------------------------------
       
   301 // CFindView::DoContactAddedToViewL
       
   302 // --------------------------------------------------------------------------
       
   303 //
       
   304 void CFindView::DoContactAddedToViewL(
       
   305         MVPbkContactViewBase& aView, 
       
   306         TInt aIndex, 
       
   307         const MVPbkContactLink& /*aContactLink*/,
       
   308         RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   309     {
       
   310     if ( &iParentView == &aView )
       
   311         {
       
   312         const CViewContact& viewContact = static_cast<const CViewContact&>(
       
   313             iParentView.ContactAtL( aIndex ) );
       
   314         if ( IsMatchL( viewContact ) )
       
   315             {
       
   316             MVPbkSimCntStore& simStore = 
       
   317                 iAllContactsView.Store().NativeStore();
       
   318             const TDesC8* cntBuf = simStore.AtL( viewContact.SimIndex() );
       
   319             if ( cntBuf )
       
   320                 {
       
   321                 CVPbkSimContact* cnt = 
       
   322                     CVPbkSimContact::NewL( *cntBuf, simStore );
       
   323                 CleanupStack::PushL( cnt );
       
   324                 
       
   325                 // We have to insert the new contact to the correct
       
   326                 // location. VPbk sort policy is used for that.
       
   327                 CVPbkContactViewSortPolicy::TParam param(
       
   328                     iAllContactsView.Store().MasterFieldTypeList(), SortOrder() );
       
   329                 CVPbkContactViewSortPolicy* sortPolicy = 
       
   330                     CVPbkContactViewSortPolicy::NewL( param );
       
   331                 CleanupStack::PushL( sortPolicy );
       
   332                 sortPolicy->SortStartL();
       
   333                 CViewContact* current = 
       
   334                     CViewContact::NewL( iAllContactsView, SortOrder() );
       
   335                 CleanupStack::PushL( current );
       
   336                 
       
   337                 const TInt count = aMatchedContacts.Count();
       
   338                 TInt index = KErrNotFound;
       
   339                 for ( TInt i = 0; i < count && index == KErrNotFound; ++i )
       
   340                     {
       
   341                     current->SetSimContactL( *aMatchedContacts[i] );
       
   342                     TInt res = 
       
   343                         sortPolicy->CompareContacts( viewContact, *current );
       
   344                     // CompareContacts returns -1, 0 or 1.
       
   345                     if ( res < 0 )
       
   346                         {
       
   347                         index = i;
       
   348                         }
       
   349                     }
       
   350                 sortPolicy->SortCompleted();
       
   351                 CleanupStack::PopAndDestroy( 2, sortPolicy ); // current
       
   352                 if ( index != KErrNotFound )
       
   353                     {
       
   354                     // Contact is inserted to matched contacts list
       
   355                     aMatchedContacts.InsertL( cnt, index );
       
   356                     }
       
   357                 else
       
   358                     {
       
   359                     // List is empty so append contact to it
       
   360                     aMatchedContacts.AppendL( cnt );
       
   361                     }
       
   362                 CleanupStack::Pop( cnt );
       
   363                 }
       
   364             }
       
   365         }
       
   366     }
       
   367 
       
   368 // --------------------------------------------------------------------------
       
   369 // CFindView::ViewFindCompletedL
       
   370 // --------------------------------------------------------------------------
       
   371 //  
       
   372 void CFindView::ViewFindCompletedL( 
       
   373         MVPbkSimCntView& /*aSimCntView*/,
       
   374         const RVPbkStreamedIntArray& aSimIndexArray )
       
   375     {
       
   376     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   377         "VPbkSimStore:CFindView(%x)::ViewFindCompletedL:found %d contacts"),
       
   378          this, aSimIndexArray.Count() );
       
   379          
       
   380     // Native SIM view find is ready.
       
   381     
       
   382     // Clean the table
       
   383     iSIMMatchedContacts.Reset();
       
   384     
       
   385     // Copy matched indexes
       
   386     const TInt count( aSimIndexArray.Count() );
       
   387     for ( TInt i(0); i < count; ++i )
       
   388         {
       
   389         iSIMMatchedContacts.AppendL( aSimIndexArray[i] );
       
   390         }
       
   391     
       
   392     // The operation is not needed after indexes have been copied
       
   393     delete iFindOperation;
       
   394     iFindOperation = NULL;
       
   395     
       
   396     // Call base class matching function. It will send internal observer
       
   397     // event that this view is ready for filtering.
       
   398     MatchContactsL();
       
   399     // Send view ready event to both internal&external observers.
       
   400     SendViewStateEventToObservers();
       
   401     }
       
   402 
       
   403 // --------------------------------------------------------------------------
       
   404 // CFindView::MatchContactL
       
   405 // --------------------------------------------------------------------------
       
   406 //  
       
   407 void CFindView::MatchContactL( const CViewContact& aViewContact,
       
   408             RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   409     {
       
   410     // aContact matches if it's one of the always included contacts OR
       
   411     // (if it's one of Contacts Model matched contacts AND it also
       
   412     // passes our own match).
       
   413     TInt matchArrayIndex = KErrNotFound;
       
   414     TBool matched = EFalse;
       
   415     if ( IsContactAlwaysIncluded( aViewContact ) )
       
   416         {
       
   417         // Remove from match array to save memory
       
   418         RemoveFromMatchArrayIfFound( aViewContact );
       
   419         matched = ETrue;
       
   420         }
       
   421     else if ( IsSIMMatchL( aViewContact, matchArrayIndex ) )
       
   422         {
       
   423         // Remove from match array to save memory
       
   424         iSIMMatchedContacts.Remove( matchArrayIndex );
       
   425         iSIMMatchedContacts.Compress();
       
   426         
       
   427         if ( IsMatchL( aViewContact ) )
       
   428             {
       
   429             matched = ETrue;
       
   430             }
       
   431         }
       
   432     
       
   433     if ( matched )
       
   434         {
       
   435         // Contact matched.
       
   436         // CFindView owns its contacts so create a copy
       
   437         MVPbkSimCntStore& simStore = iAllContactsView.Store().NativeStore();
       
   438         const TDesC8* cntBuf = simStore.AtL( aViewContact.SimIndex() );
       
   439         if ( cntBuf )
       
   440             {
       
   441             CVPbkSimContact* cnt = 
       
   442                 CVPbkSimContact::NewL( *cntBuf, simStore );
       
   443             CleanupStack::PushL( cnt );
       
   444             aMatchedContacts.AppendL( cnt );
       
   445             CleanupStack::Pop( cnt );
       
   446             }
       
   447         }
       
   448     }
       
   449 
       
   450 // --------------------------------------------------------------------------
       
   451 // CFindView::RemoveFromMatchArrayIfFound
       
   452 // --------------------------------------------------------------------------
       
   453 //  
       
   454 void CFindView::RemoveFromMatchArrayIfFound( const CViewContact& aViewContact )
       
   455     {
       
   456     TInt index = FindFromMatchArray( aViewContact );
       
   457     if ( index != KErrNotFound )
       
   458         {
       
   459         iSIMMatchedContacts.Remove( index );
       
   460         iSIMMatchedContacts.Compress();
       
   461         }
       
   462     }
       
   463 
       
   464 // --------------------------------------------------------------------------
       
   465 // CFindView::IsSIMMatchL
       
   466 // --------------------------------------------------------------------------
       
   467 //  
       
   468 TBool CFindView::IsSIMMatchL( const CViewContact& aContact,
       
   469         TInt& aMatchArrayIndex ) const
       
   470     {
       
   471     aMatchArrayIndex = FindFromMatchArray( aContact );
       
   472     return aMatchArrayIndex != KErrNotFound;
       
   473     }
       
   474 
       
   475 // --------------------------------------------------------------------------
       
   476 // CFindView::FindFromMatchArray
       
   477 // --------------------------------------------------------------------------
       
   478 //  
       
   479 TInt CFindView::FindFromMatchArray( const CViewContact& aContact ) const
       
   480     {
       
   481     return iSIMMatchedContacts.Find( aContact.SimIndex() );
       
   482     }
       
   483 } // namespace VPbkSimStore 
       
   484 // End of File