phonebookengines/VirtualPhonebook/VPbkSimStore/src/CRefineView.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 "CRefineView.h"
       
    20 
       
    21 // From VPbkSimStore
       
    22 #include "CContactStore.h"
       
    23 #include "CViewContact.h"
       
    24 #include "CContactView.h"
       
    25 
       
    26 // From Virtual Phonebook
       
    27 #include <MVPbkSimContact.h>
       
    28 #include <CVPbkSimCntField.h>
       
    29 #include <CVPbkContactFindPolicy.h>
       
    30 #include <MVPbkContactLink.h>
       
    31 #include <CVPbkContactViewSortPolicy.h>
       
    32 
       
    33 // System includes
       
    34 #include <cntviewbase.h>
       
    35 #include <featmgr.h>
       
    36 
       
    37 // Debugging headers
       
    38 #include <VPbkProfile.h>
       
    39 
       
    40 namespace VPbkSimStore {
       
    41 
       
    42 inline CRefineView::CRefineView( MParentViewForFiltering& aParentView,
       
    43         CContactView& aAllContactsView,
       
    44         MAlwaysIncludedContacts& aAlwaysIncluded ) 
       
    45         :   CFindViewBase( aParentView, aAllContactsView, EFalse ),
       
    46             iAlwaysIncluded( aAlwaysIncluded )
       
    47     {
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // CRefineView::~CRefineView
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 CRefineView::~CRefineView()
       
    55     {
       
    56     delete iIdleUpdate;
       
    57     }
       
    58 
       
    59 // --------------------------------------------------------------------------
       
    60 // CRefineView::NewLC
       
    61 // --------------------------------------------------------------------------
       
    62 //
       
    63 CRefineView* CRefineView::NewLC(
       
    64         const MDesCArray& aFindStrings,
       
    65         MParentViewForFiltering& aParentView,
       
    66         CContactView& aAllContactsView,
       
    67         MVPbkContactViewObserver& aExternalViewObserver,
       
    68         MVPbkContactFindPolicy& aContactFindPolicy,
       
    69         MAlwaysIncludedContacts& aAlwaysIncluded )
       
    70     {
       
    71     CRefineView* self =
       
    72         new ( ELeave ) CRefineView( aParentView, aAllContactsView, 
       
    73             aAlwaysIncluded );
       
    74     CleanupStack::PushL( self );
       
    75     self->BaseConstructL( aExternalViewObserver, aFindStrings, 
       
    76         aContactFindPolicy  );
       
    77     return self;
       
    78     }
       
    79 
       
    80 // --------------------------------------------------------------------------
       
    81 // CRefineView::ContactViewReady
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 void CRefineView::ContactViewReady( MVPbkContactViewBase& /*aView*/ )
       
    85     {
       
    86     // Send view ready event to all observers
       
    87     SendViewStateEventToObservers();
       
    88     }
       
    89     
       
    90 // --------------------------------------------------------------------------
       
    91 // CRefineView::ContactViewReadyForFiltering
       
    92 // --------------------------------------------------------------------------
       
    93 //
       
    94 void CRefineView::ContactViewReadyForFiltering( 
       
    95         MParentViewForFiltering& /*aView*/ )
       
    96     {
       
    97     // Cancel update so that there won't be double matching in case
       
    98     // the parent view is updating itself and also this view is updating
       
    99     // itself.
       
   100     if ( iIdleUpdate )
       
   101         {
       
   102         iIdleUpdate->Cancel();
       
   103         }
       
   104         
       
   105     // Parent view is ready -> Call base class function to start
       
   106     // matching. MatchContactsL will also call internal observer interface
       
   107     // to send internal view events to higher level filtered views. This
       
   108     // way all the CRefineViews are updated before any external observer
       
   109     // gets the view event. 
       
   110     // External observers will get view event from this view when the
       
   111     // CFindView  (CFindView::ViewFindCompletedL) calls 
       
   112     // SendViewStateEventToObservers.
       
   113     TRAPD( res, MatchContactsL() );
       
   114     if ( res != KErrNone )
       
   115         {
       
   116         ContactViewError( *this, res, EFalse );
       
   117         }
       
   118     }
       
   119     
       
   120 // --------------------------------------------------------------------------
       
   121 // CRefineView::MatchL
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 void CRefineView::MatchL( RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   125     {
       
   126     const TInt contactCount = iParentView.ContactCountL();
       
   127     for ( TInt i = 0; i < contactCount; ++i )
       
   128         {
       
   129         // iParentView is always VPbkSimStore view and the contacts type
       
   130         // is CViewContact
       
   131         const CViewContact& candidate = static_cast<const CViewContact&>(
       
   132             iParentView.ContactAtL( i ) );
       
   133         if ( IsContactAlwaysIncluded( candidate ) || IsMatchL( candidate ) )
       
   134             {
       
   135             // iParentView is always CFindView or CRefineView. The contacts type
       
   136             // is CViewContact. It can be safely casted. Const cast be done
       
   137             // because aContact is actually owned by CFindView.
       
   138             MVPbkSimContact* nativeContact = 
       
   139                 const_cast<MVPbkSimContact*>( candidate.NativeContact() );
       
   140             aMatchedContacts.AppendL( nativeContact );
       
   141             }
       
   142         }    
       
   143     }
       
   144 
       
   145 // --------------------------------------------------------------------------
       
   146 // CRefineView::DoContactAddedToViewL
       
   147 // --------------------------------------------------------------------------
       
   148 //
       
   149 void CRefineView::DoContactAddedToViewL( MVPbkContactViewBase& aView,
       
   150         TInt aIndex, const MVPbkContactLink& /*aContactLink*/,
       
   151         RPointerArray<MVPbkSimContact>& aMatchedContacts )
       
   152     {
       
   153     if ( &iParentView == &aView && 
       
   154          IsMatchL( iParentView.ContactAtL( aIndex )))
       
   155         {
       
   156         // iParentView is always CFindView or CRefineView the contacts type
       
   157         // is CViewContact. It can be safely casted.
       
   158         const CViewContact& contact = static_cast<const CViewContact&>(
       
   159             iParentView.ContactAtL( aIndex ) );
       
   160         
       
   161         // We have to insert the new contact to the correct
       
   162         // location. VPbk sort policy is used for that.
       
   163         CVPbkContactViewSortPolicy::TParam param(
       
   164             iAllContactsView.Store().MasterFieldTypeList(), SortOrder() );
       
   165         CVPbkContactViewSortPolicy* sortPolicy = 
       
   166             CVPbkContactViewSortPolicy::NewL( param );
       
   167         CleanupStack::PushL( sortPolicy );
       
   168         sortPolicy->SortStartL();
       
   169         // Create a view contact instance for comparing contacts.
       
   170         CViewContact* current = 
       
   171             CViewContact::NewL( iAllContactsView, SortOrder() );
       
   172         CleanupStack::PushL( current );
       
   173         
       
   174         const TInt count = aMatchedContacts.Count();
       
   175         TInt index = KErrNotFound;
       
   176         for ( TInt i = 0; i < count && index == KErrNotFound; ++i )
       
   177             {
       
   178             current->SetSimContactL( *aMatchedContacts[i] );
       
   179             TInt res = 
       
   180                 sortPolicy->CompareContacts( contact, *current );
       
   181             if ( res < 0 )
       
   182                 {
       
   183                 index = i;
       
   184                 }
       
   185             }
       
   186         sortPolicy->SortCompleted();
       
   187         CleanupStack::PopAndDestroy( 2, sortPolicy );
       
   188         
       
   189         // Const cast be done
       
   190         // because nativeContact is actually owned by CFindView.
       
   191         MVPbkSimContact* nativeContact = 
       
   192             const_cast<MVPbkSimContact*>( contact.NativeContact() );
       
   193         if ( index != KErrNotFound )
       
   194             {
       
   195             aMatchedContacts.InsertL( nativeContact, index );
       
   196             }
       
   197         else
       
   198             {
       
   199             aMatchedContacts.AppendL( nativeContact );
       
   200             }
       
   201         }    
       
   202     }
       
   203 
       
   204 // --------------------------------------------------------------------------
       
   205 // CRefineView::UpdateFilterL
       
   206 // --------------------------------------------------------------------------
       
   207 //
       
   208 void CRefineView::UpdateFilterL( 
       
   209         const MDesCArray& aFindWords,
       
   210         const MVPbkContactBookmarkCollection* /*aAlwaysIncludedContacts*/ )
       
   211     {
       
   212     if ( !iIdleUpdate )
       
   213         {
       
   214         iIdleUpdate = CIdle::NewL( CActive::EPriorityStandard );
       
   215         }
       
   216         
       
   217     // NOTE:
       
   218     // aAlwaysIncludedContacts is not used by CRefineView. It uses 
       
   219     // MAlwaysIncludedContacts interface for that purpose.
       
   220     SetFindStringsL( aFindWords );
       
   221     // Start idle update.
       
   222     if ( !iIdleUpdate->IsActive() )
       
   223         {
       
   224         iIdleUpdate->Start( TCallBack( StartUpdateCallback, this ) ); 
       
   225         }
       
   226     }
       
   227 
       
   228 // --------------------------------------------------------------------------
       
   229 // CRefineView::IsNativeMatchingRequestActive
       
   230 // --------------------------------------------------------------------------
       
   231 //
       
   232 TBool CRefineView::IsNativeMatchingRequestActive()
       
   233     {
       
   234     // Forward to CFindView
       
   235     return iParentView.IsNativeMatchingRequestActive();
       
   236     }
       
   237     
       
   238 // --------------------------------------------------------------------------
       
   239 // CRefineView::IsContactAlwaysIncluded
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 TBool CRefineView::IsContactAlwaysIncluded( 
       
   243         const CViewContact& aContact ) const
       
   244     {
       
   245     return iAlwaysIncluded.IsContactAlwaysIncluded( aContact );
       
   246     }
       
   247 
       
   248 // --------------------------------------------------------------------------
       
   249 // CRefineView::StartUpdateCallback
       
   250 // --------------------------------------------------------------------------
       
   251 //
       
   252 TInt CRefineView::StartUpdateCallback( TAny* aThis )
       
   253     {
       
   254     CRefineView* view = static_cast<CRefineView*>( aThis );
       
   255     // If the CFindView is doing async match from SIM server then
       
   256     // do nothing here and wait that parent view notifies this view.
       
   257     if ( !view->IsNativeMatchingRequestActive() )
       
   258         {
       
   259         TRAPD( res, view->ActivateContactMatchL() )
       
   260         if ( res != KErrNone )
       
   261             {
       
   262             view->ContactViewError( *view, res, EFalse );
       
   263             }
       
   264         }
       
   265     // Do not continue idle
       
   266     return EFalse;
       
   267     }
       
   268 } // namespace VPbkSimStore
       
   269 
       
   270 // End of File