phonebookengines/VirtualPhonebook/VPbkCntModel/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:  Contacts Model store filtered contact view implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CFindView.h"
       
    21 
       
    22 // VPbkCntModel
       
    23 #include "CRefineView.h"
       
    24 #include "CViewBase.h"
       
    25 #include "CContactBookmark.h"
       
    26 #include "CContactStore.h"
       
    27 
       
    28 // From Virtual Phonebook
       
    29 #include <CVPbkContactNameConstructionPolicy.h>
       
    30 #include <CVPbkContactFindPolicy.h>
       
    31 #include <MVPbkContactBookmarkCollection.h>
       
    32 
       
    33 // System includes
       
    34 #include <cntdef.h>
       
    35 
       
    36 // Debugging headers
       
    37 #include <VPbkProfile.h>
       
    38 
       
    39 namespace VPbkCntModel {
       
    40 
       
    41 // --------------------------------------------------------------------------
       
    42 // CFindView::CFindView
       
    43 // --------------------------------------------------------------------------
       
    44 //
       
    45 inline CFindView::CFindView( CViewBase& aBaseView )
       
    46     :   CFindViewBase( aBaseView, aBaseView, ETrue )
       
    47     {
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // CFindView::ConstructL
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 void CFindView::ConstructL( const MDesCArray& aFindStrings,
       
    55         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts,
       
    56         MVPbkContactViewObserver& aExternalViewObserver, RFs& aRFs )
       
    57     {
       
    58     CVPbkContactFindPolicy::TParam findPolicyParams(
       
    59         iBaseView.Store().MasterFieldTypeList(), aRFs );
       
    60     iFindPolicy = CVPbkContactFindPolicy::NewL( findPolicyParams );
       
    61     BaseConstructL( aFindStrings, *iFindPolicy,
       
    62         aExternalViewObserver );
       
    63     SetAlwaysIncludedContactsL( aAlwaysIncludedContacts );
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CFindView::NewLC
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 CFindView* CFindView::NewLC(
       
    71         const MDesCArray& aFindStrings,
       
    72         CViewBase& aParentView,
       
    73         MVPbkContactViewObserver& aExternalViewObserver,
       
    74         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts,
       
    75         RFs& aRFs )
       
    76     {
       
    77     CFindView* self =
       
    78         new ( ELeave ) CFindView( aParentView );
       
    79     CleanupStack::PushL( self );
       
    80     self->ConstructL( aFindStrings, aAlwaysIncludedContacts,
       
    81         aExternalViewObserver, aRFs );
       
    82     return self;
       
    83     }
       
    84 
       
    85 // --------------------------------------------------------------------------
       
    86 // CFindView::~CFindView
       
    87 // --------------------------------------------------------------------------
       
    88 //
       
    89 CFindView::~CFindView()
       
    90     {
       
    91     delete iFindPolicy;
       
    92     iAlwaysIncluded.Close();
       
    93     iContactsModelMatchContacts.ResetAndDestroy();
       
    94     }
       
    95 
       
    96 // --------------------------------------------------------------------------
       
    97 // CFindView::SetAlwaysIncludedContactsL
       
    98 // --------------------------------------------------------------------------
       
    99 //
       
   100 void CFindView::SetAlwaysIncludedContactsL(
       
   101         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts )
       
   102     {
       
   103     // Destroy old ids
       
   104     iAlwaysIncluded.Reset();
       
   105 
       
   106     if ( aAlwaysIncludedContacts )
       
   107         {
       
   108         MVPbkContactStore& store = ParentObject().ContactStore();
       
   109         const TInt count = aAlwaysIncludedContacts->Count();
       
   110         for ( TInt i = 0; i < count; ++i )
       
   111             {
       
   112             const CContactBookmark* bookmark =
       
   113                 dynamic_cast<const CContactBookmark*>(
       
   114                     &aAlwaysIncludedContacts->At( i ) );
       
   115             // If bookmark was from VPbkCntModel and if it's from same store
       
   116             // as this view then it's added to array.
       
   117             if ( bookmark &&
       
   118                  &bookmark->ContactStore() == &store )
       
   119                 {
       
   120                 iAlwaysIncluded.AppendL( bookmark->ContactId() );
       
   121                 }
       
   122             }
       
   123         }
       
   124     }
       
   125 
       
   126 // --------------------------------------------------------------------------
       
   127 // CFindView::MatchL
       
   128 // --------------------------------------------------------------------------
       
   129 //
       
   130 void CFindView::MatchL(
       
   131         RPointerArray<CCntModelViewContact>& aMatchedContacts )
       
   132     {
       
   133     iContactsModelMatchContacts.ResetAndDestroy();
       
   134 
       
   135     VPBK_PROFILE_START(VPbkProfile::ECntModelFind);
       
   136     // Get matches from Contacts Model
       
   137     iBaseView.NativeView().ContactsMatchingPrefixL(
       
   138         FindStrings(), iContactsModelMatchContacts );
       
   139     VPBK_PROFILE_END(VPbkProfile::ECntModelFind);
       
   140 
       
   141     if (iAlwaysIncluded.Count() == 0)
       
   142         {
       
   143         // No always included contacts. The match can be done using
       
   144         // only the Contacts Model matched contacts.
       
   145         CViewContact* viewContact = CViewContact::NewL( iBaseView, SortOrder() );
       
   146         CleanupStack::PushL(viewContact);
       
   147         const TInt count = iContactsModelMatchContacts.Count();
       
   148         for (TInt i = 0; i < count; ++i)
       
   149             {
       
   150             viewContact->SetViewContact( *iContactsModelMatchContacts[i] );
       
   151             if ( IsMatchL( *viewContact ) )
       
   152                 {
       
   153                 // Keep the order of the contacts same and move contact
       
   154                 // from iContactsModelMatchContacts to aMatchedContacts
       
   155                 aMatchedContacts.AppendL(iContactsModelMatchContacts[i]);
       
   156                 // Don't remove the contact to keep indexes correct.
       
   157                 // Set to NULL so that ResetAndDestroy doesn't crash in
       
   158                 // destructor
       
   159                 iContactsModelMatchContacts[i] = NULL;
       
   160                 }
       
   161             }
       
   162         CleanupStack::PopAndDestroy( viewContact );
       
   163         }
       
   164     else
       
   165         {
       
   166         // Do it slowly by looping all the parent view contacts.
       
   167         const TInt contactCount = iParentView.ContactCountL();
       
   168         for ( TInt i = 0; i < contactCount; ++i )
       
   169             {
       
   170             // iParentView is always VPbkCntModel view and the contacts type
       
   171             // is CViewContact
       
   172             const CViewContact& candidate = static_cast<const CViewContact&>(
       
   173                 iParentView.ContactAtL( i ) );
       
   174             MatchContactL( candidate, aMatchedContacts );
       
   175             }
       
   176         }
       
   177 
       
   178     iContactsModelMatchContacts.ResetAndDestroy();
       
   179     }
       
   180 
       
   181 // --------------------------------------------------------------------------
       
   182 // CFindView::DoContactAddedToViewL
       
   183 // --------------------------------------------------------------------------
       
   184 //
       
   185 void CFindView::DoContactAddedToViewL( MVPbkContactViewBase& aView,
       
   186         TInt aIndex, const MVPbkContactLink& /*aContactLink*/,
       
   187         RPointerArray<CCntModelViewContact>& aMatchedContacts )
       
   188     {
       
   189     if ( &iParentView == &aView )
       
   190         {
       
   191         const CViewContact& viewContact = static_cast<const CViewContact&>(
       
   192             iParentView.ContactAtL( aIndex ) );
       
   193         if ( IsMatchL( viewContact ) )
       
   194             {
       
   195             CCntModelViewContact* cnt =
       
   196                 CCntModelViewContact::NewL( *viewContact.NativeContact() );
       
   197             CleanupStack::PushL( cnt );
       
   198             aMatchedContacts.InsertInOrderAllowRepeatsL( cnt,
       
   199                 TLinearOrder<CCntModelViewContact>(
       
   200                     CCompareView::CompareFieldsL ) );
       
   201             CleanupStack::Pop( cnt );
       
   202             }
       
   203         }
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CFindView::UpdateFilterL
       
   208 // --------------------------------------------------------------------------
       
   209 //
       
   210 void CFindView::UpdateFilterL(
       
   211         const MDesCArray& aFindWords,
       
   212         const MVPbkContactBookmarkCollection* aAlwaysIncludedContacts )
       
   213     {
       
   214     SetFindStringsL( aFindWords );
       
   215     SetAlwaysIncludedContactsL( aAlwaysIncludedContacts );
       
   216     ActivateContactMatchL();
       
   217     }
       
   218 
       
   219 // --------------------------------------------------------------------------
       
   220 // CFindView::MatchContactL
       
   221 // --------------------------------------------------------------------------
       
   222 //
       
   223 void CFindView::MatchContactL( const CViewContact& aViewContact,
       
   224         RPointerArray<CCntModelViewContact>& aMatchedContacts )
       
   225     {
       
   226     // aContact matches if it's one of the always included contacts OR
       
   227     // if it's one of Contacts Model matched contacts AND it also
       
   228     // passes our own match.
       
   229     TInt matchArrayIndex = KErrNotFound;
       
   230     TBool matched = EFalse;
       
   231     if ( IsContactAlwaysIncluded( aViewContact ) )
       
   232         {
       
   233         // Remove from match array to save memory
       
   234         RemoveFromMatchArrayIfFound( aViewContact );
       
   235         matched = ETrue;
       
   236         }
       
   237     else if ( IsContactsModelMatchL( aViewContact, matchArrayIndex ) )
       
   238         {
       
   239         // Remove from match array to save memory
       
   240         delete iContactsModelMatchContacts[matchArrayIndex];
       
   241         iContactsModelMatchContacts.Remove( matchArrayIndex );
       
   242 
       
   243         if ( IsMatchL( aViewContact ) )
       
   244             {
       
   245             matched = ETrue;
       
   246             }
       
   247         }
       
   248 
       
   249     if ( matched )
       
   250         {
       
   251         // Contact matched.
       
   252         // CFindView owns its contacts so create a copy
       
   253         CCntModelViewContact* cnt =
       
   254             CCntModelViewContact::NewL( *aViewContact.NativeContact() );
       
   255         CleanupStack::PushL( cnt );
       
   256         aMatchedContacts.AppendL( cnt );
       
   257         CleanupStack::Pop( cnt );
       
   258         }
       
   259     }
       
   260 
       
   261 // --------------------------------------------------------------------------
       
   262 // CFindView::IsContactAlwaysIncluded
       
   263 // --------------------------------------------------------------------------
       
   264 //
       
   265 TBool CFindView::IsContactAlwaysIncluded(
       
   266         const CViewContact& aContact ) const
       
   267     {
       
   268     return iAlwaysIncluded.Find( aContact.Id() ) != KErrNotFound;
       
   269     }
       
   270 
       
   271 // --------------------------------------------------------------------------
       
   272 // CFindView::IsContactsModelMatchL
       
   273 // --------------------------------------------------------------------------
       
   274 //
       
   275 TBool CFindView::IsContactsModelMatchL( const CViewContact& aContact,
       
   276         TInt& aMatchArrayIndex ) const
       
   277     {
       
   278     // Contacts are in order in iContactsModelMatchContacts. Use binary
       
   279     // search to check if aContact is can be found from the array.
       
   280     aMatchArrayIndex = FindFromMatchArray( aContact );
       
   281     return aMatchArrayIndex != KErrNotFound;
       
   282     }
       
   283 
       
   284 // --------------------------------------------------------------------------
       
   285 // CFindView::RemoveFromMatchArrayIfFound
       
   286 // --------------------------------------------------------------------------
       
   287 //
       
   288 void CFindView::RemoveFromMatchArrayIfFound( const CViewContact& aContact )
       
   289     {
       
   290     TInt index = FindFromMatchArray( aContact );
       
   291     if ( index != KErrNotFound )
       
   292         {
       
   293         delete iContactsModelMatchContacts[index];
       
   294         iContactsModelMatchContacts.Remove(index);
       
   295         }
       
   296     }
       
   297 
       
   298 // --------------------------------------------------------------------------
       
   299 // CFindView::FindFromMatchArray
       
   300 // --------------------------------------------------------------------------
       
   301 //
       
   302 TInt CFindView::FindFromMatchArray( const CViewContact& aContact ) const
       
   303     {
       
   304     return iContactsModelMatchContacts.FindInOrder(
       
   305         aContact.NativeContact(),
       
   306         TLinearOrder<CCntModelViewContact>( CCompareView::CompareFieldsL ) );
       
   307     }
       
   308 } // namespace VPbkCntModel
       
   309 // End of File