phonebookui/Phonebook2/UIControls/src/cpbk2filteredviewstackelement.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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:  Phonebook 2 view stack element.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cpbk2filteredviewstackelement.h"
       
    20 
       
    21 // Virtual Phonebook
       
    22 #include <CVPbkContactFindPolicy.h>
       
    23 #include <MVPbkContactViewFiltering.h>
       
    24 #include <MVPbkContactViewBase.h>
       
    25 
       
    26 // Debugging headers
       
    27 #include <Pbk2Debug.h>
       
    28 
       
    29 /// Unnamed namespace for local definitions
       
    30 namespace {
       
    31 
       
    32 #ifdef _DEBUG
       
    33 enum TPanicCode
       
    34     {
       
    35     EPreCond_CreateViewL = 1,
       
    36     EBaseViewHasNoFilter,
       
    37     EViewAlreadyExists,
       
    38     EPreCond_UpdateFilterL,
       
    39     EPreCond_SetUnderDestruction,
       
    40     EPreCond_FindMustBeDigraphicL
       
    41     };
       
    42 
       
    43 void Panic(TPanicCode aReason)
       
    44     {
       
    45     _LIT(KPanicText, "CPbk2FilteredViewStackElement");
       
    46     User::Panic(KPanicText, aReason);
       
    47     }
       
    48 
       
    49 #endif // _DEBUG
       
    50 
       
    51 } /// namespace
       
    52 
       
    53 // --------------------------------------------------------------------------
       
    54 // CPbk2FilteredViewStackBaseElement::CPbk2FilteredViewStackBaseElement
       
    55 // --------------------------------------------------------------------------
       
    56 //
       
    57 inline CPbk2FilteredViewStackBaseElement::CPbk2FilteredViewStackBaseElement( 
       
    58         MVPbkContactViewBase& aBaseView ) :
       
    59         iBaseView( aBaseView )
       
    60     {
       
    61     }
       
    62 
       
    63 // --------------------------------------------------------------------------
       
    64 // CPbk2FilteredViewStackBaseElement::NewL
       
    65 // --------------------------------------------------------------------------
       
    66 //
       
    67 CPbk2FilteredViewStackBaseElement* CPbk2FilteredViewStackBaseElement::NewL( 
       
    68         MVPbkContactViewBase& aBaseView )
       
    69     {
       
    70     CPbk2FilteredViewStackBaseElement* self =
       
    71         new ( ELeave ) CPbk2FilteredViewStackBaseElement( aBaseView );
       
    72     return self;
       
    73     }
       
    74 
       
    75 // --------------------------------------------------------------------------
       
    76 // CPbk2FilteredViewStackBaseElement::CreateViewL
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 void CPbk2FilteredViewStackBaseElement::CreateViewL( 
       
    80         MVPbkContactViewBase& /*aBaseView*/,
       
    81         MVPbkContactViewBase& /*aParentView*/,
       
    82         MVPbkContactViewObserver& /*aObserver*/ )
       
    83     {
       
    84     // Base element gets the view in constructor
       
    85     __ASSERT_DEBUG( EFalse, Panic( EViewAlreadyExists ) );
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CPbk2FilteredViewStackBaseElement::View
       
    90 // --------------------------------------------------------------------------
       
    91 //  
       
    92 MVPbkContactViewBase* CPbk2FilteredViewStackBaseElement::View() const
       
    93     {
       
    94     return &iBaseView;
       
    95     }
       
    96 
       
    97 // --------------------------------------------------------------------------
       
    98 // CPbk2FilteredViewStackBaseElement::FindArray
       
    99 // --------------------------------------------------------------------------
       
   100 //  
       
   101 const MDesCArray& CPbk2FilteredViewStackBaseElement::FindArray() const
       
   102     {
       
   103     CDesCArray* nil = NULL;
       
   104     return *nil;
       
   105     }
       
   106     
       
   107 // --------------------------------------------------------------------------
       
   108 // CPbk2FilteredViewStackBaseElement::Level
       
   109 // --------------------------------------------------------------------------
       
   110 //  
       
   111 TInt CPbk2FilteredViewStackBaseElement::Level() const
       
   112     {
       
   113     // Level is always zero for base element
       
   114     return 0;
       
   115     }
       
   116     
       
   117 // --------------------------------------------------------------------------
       
   118 // CPbk2ViewStackBaseElement::AlwaysIncluded
       
   119 // --------------------------------------------------------------------------
       
   120 //  
       
   121 const MVPbkContactBookmarkCollection* 
       
   122         CPbk2FilteredViewStackBaseElement::AlwaysIncluded() const
       
   123     {
       
   124     return NULL;
       
   125     }
       
   126 
       
   127 // --------------------------------------------------------------------------
       
   128 // CPbk2FilteredViewStackBaseElement::IsSame
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 TBool CPbk2FilteredViewStackBaseElement::IsSame( 
       
   132         const MPbk2FilteredViewStackElement& aOther ) const
       
   133     {
       
   134     // For all contacts view element it enough to compare levels
       
   135     return aOther.Level() == Level();  
       
   136     }
       
   137 
       
   138 // --------------------------------------------------------------------------
       
   139 // CPbk2FilteredViewStackBaseElement::UpdateFilterL
       
   140 // --------------------------------------------------------------------------
       
   141 //
       
   142 void CPbk2FilteredViewStackBaseElement::UpdateFilterL( 
       
   143         MVPbkContactViewBase& /*aBaseView*/,
       
   144         MVPbkContactViewObserver& /*aObserver*/,
       
   145         const MDesCArray& /*aSearchLevelArray*/,
       
   146         const MVPbkContactBookmarkCollection* /*aAlwaysIncluded*/ )
       
   147     {
       
   148     __ASSERT_DEBUG( EFalse, Panic( EBaseViewHasNoFilter ) );
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2FilteredViewStackBaseElement::ViewState
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 MPbk2FilteredViewStackElement::TViewState 
       
   156         CPbk2FilteredViewStackBaseElement::ViewState() const
       
   157     {
       
   158     return iState;    
       
   159     }
       
   160 
       
   161 // --------------------------------------------------------------------------
       
   162 // CPbk2FilteredViewStackBaseElement::SetViewState
       
   163 // --------------------------------------------------------------------------
       
   164 //
       
   165 void CPbk2FilteredViewStackBaseElement::SetViewState( TViewState aState )
       
   166     {
       
   167     iState = aState;
       
   168     }
       
   169 
       
   170 // --------------------------------------------------------------------------
       
   171 // CPbk2FilteredViewStackBaseElement::UnderDestruction
       
   172 // --------------------------------------------------------------------------
       
   173 //
       
   174 TBool CPbk2FilteredViewStackBaseElement::UnderDestruction() const
       
   175     {
       
   176     // Base element can not be under destruction
       
   177     return EFalse;
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CPbk2FilteredViewStackBaseElement::SetUnderDestruction
       
   182 // --------------------------------------------------------------------------
       
   183 //
       
   184 void CPbk2FilteredViewStackBaseElement::SetUnderDestruction()
       
   185     {
       
   186     __ASSERT_DEBUG( EFalse, Panic( EPreCond_SetUnderDestruction ) );
       
   187     }
       
   188     
       
   189 // --------------------------------------------------------------------------
       
   190 // CPbk2FilteredViewStackElement::CPbk2FilteredViewStackElement
       
   191 // --------------------------------------------------------------------------
       
   192 //
       
   193 inline CPbk2FilteredViewStackElement::CPbk2FilteredViewStackElement( 
       
   194         MVPbkContactFindPolicy& aFindPolicy,
       
   195         const MVPbkContactBookmarkCollection* aAlwaysIncluded,
       
   196         const MDesCArray* aSearchLevelArray )
       
   197         :   iFindPolicy( aFindPolicy ),
       
   198             iAlwaysIncluded( aAlwaysIncluded ),
       
   199             iSearchLevelArray( aSearchLevelArray )
       
   200     {
       
   201     }
       
   202 
       
   203 // --------------------------------------------------------------------------
       
   204 // CPbk2FilteredViewStackElement::ConstructL
       
   205 // --------------------------------------------------------------------------
       
   206 //
       
   207 void CPbk2FilteredViewStackElement::ConstructL()
       
   208     {
       
   209     iLengthOfSearchArray = CountLengthOfStrings( *iSearchLevelArray );
       
   210     }
       
   211 
       
   212 // --------------------------------------------------------------------------
       
   213 // CPbk2FilteredViewStackElement::NewL
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 CPbk2FilteredViewStackElement* CPbk2FilteredViewStackElement::NewL( 
       
   217         MVPbkContactFindPolicy& aFindPolicy,
       
   218         const MDesCArray* aSearchLevelArray,
       
   219         const MVPbkContactBookmarkCollection* aAlwaysIncluded )
       
   220     {
       
   221     CPbk2FilteredViewStackElement* self = 
       
   222             new ( ELeave ) CPbk2FilteredViewStackElement( aFindPolicy, 
       
   223                 aAlwaysIncluded, aSearchLevelArray );
       
   224     CleanupStack::PushL( self );
       
   225     self->ConstructL();
       
   226     CleanupStack::Pop( self );
       
   227     return self;
       
   228     }
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CPbk2FilteredViewStackElement::~CPbk2FilteredViewStackElement
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 CPbk2FilteredViewStackElement::~CPbk2FilteredViewStackElement()
       
   235     {
       
   236     delete iView;
       
   237     delete iSearchLevelArray; //from FilteredViewStack
       
   238     }
       
   239 
       
   240 // --------------------------------------------------------------------------
       
   241 // CPbk2FilteredViewStackElement::CreateViewL
       
   242 // --------------------------------------------------------------------------
       
   243 //
       
   244 void CPbk2FilteredViewStackElement::CreateViewL( 
       
   245         MVPbkContactViewBase& aBaseView,
       
   246         MVPbkContactViewBase& aParentView,
       
   247         MVPbkContactViewObserver& aObserver )
       
   248     {
       
   249     __ASSERT_DEBUG( !iView && aParentView.ViewFiltering(), 
       
   250             Panic ( EPreCond_CreateViewL ) );
       
   251 
       
   252     if( FindMustBeDigraphicL() )
       
   253         {
       
   254         PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   255                 ("FilteredViewStackElement::CreateViewL: Ortographic") );
       
   256         // Use base view as the filtering view.
       
   257         // This is needed to prevent accidentally filtering out contacts
       
   258         // that include diraphic characters. For example with Croatian
       
   259         // locale a contact name beginning with "nj" character would
       
   260         // be filtered out with the (parent) find filter "n" and it would
       
   261         // not be shown with find filter "nj".
       
   262         iView = aBaseView.ViewFiltering()->CreateFilteredViewLC( 
       
   263             aObserver, *iSearchLevelArray, iAlwaysIncluded );
       
   264         }
       
   265     else
       
   266         {
       
   267         PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   268                 ("FilteredViewStackElement::CreateViewL: No ortographic") );
       
   269         iView = aParentView.ViewFiltering()->CreateFilteredViewLC( 
       
   270             aObserver, *iSearchLevelArray, iAlwaysIncluded );  
       
   271         }
       
   272 
       
   273     CleanupStack::Pop(); // iView
       
   274     }
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CPbk2FilteredViewStackElement::View
       
   278 // --------------------------------------------------------------------------
       
   279 //
       
   280 MVPbkContactViewBase* CPbk2FilteredViewStackElement::View() const
       
   281     {
       
   282     return iView;
       
   283     }
       
   284 
       
   285 // --------------------------------------------------------------------------
       
   286 // CPbk2FilteredViewStackElement::FindArray
       
   287 // --------------------------------------------------------------------------
       
   288 //
       
   289 const MDesCArray& CPbk2FilteredViewStackElement::FindArray() const
       
   290     {
       
   291     return *iSearchLevelArray;
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CPbk2FilteredViewStackElement::FindMustBeDigraphicL
       
   296 // --------------------------------------------------------------------------
       
   297 //
       
   298 TBool CPbk2FilteredViewStackElement::FindMustBeDigraphicL() const
       
   299     {
       
   300     __ASSERT_DEBUG( iSearchLevelArray, Panic( EPreCond_FindMustBeDigraphicL ) );
       
   301 
       
   302     TBool isDigraphic( EFalse );
       
   303 
       
   304     // Go through the find string tokens one-by-one and check if they
       
   305     // include digraphs
       
   306     for ( TInt i(0); i < iSearchLevelArray->MdcaCount(); i++ )
       
   307         {
       
   308         TPtrC findStringToken = iSearchLevelArray->MdcaPoint( i );
       
   309         const TInt KDigraphLength(2);
       
   310         const TInt tokenLength( findStringToken.Length() );
       
   311         // No need to check single character find strings
       
   312         if( tokenLength >= KDigraphLength )
       
   313             {
       
   314             TPtrC substring = findStringToken.Left( tokenLength - 1 );
       
   315             if( !iFindPolicy.MatchRefineL( findStringToken, substring ) )
       
   316                 {
       
   317                 // The substring did not match the characters of the find
       
   318                 // string token -> the find string token must include digraphs
       
   319                 // For example with Croatian locale the find string "nj" does
       
   320                 // not include the substring "n" because "nj" is a digraph
       
   321                 isDigraphic = ETrue;
       
   322                 break;
       
   323                 }
       
   324             }
       
   325         }
       
   326 
       
   327     return isDigraphic;
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CPbk2FilteredViewStackElement::Level
       
   332 // --------------------------------------------------------------------------
       
   333 //
       
   334 TInt CPbk2FilteredViewStackElement::Level() const
       
   335     {
       
   336     return iLengthOfSearchArray;
       
   337     }
       
   338 
       
   339 // --------------------------------------------------------------------------
       
   340 // CPbk2FilteredViewStackElement::AlwaysIncluded
       
   341 // --------------------------------------------------------------------------
       
   342 //
       
   343 const MVPbkContactBookmarkCollection* 
       
   344         CPbk2FilteredViewStackElement::AlwaysIncluded() const
       
   345     {
       
   346     return iAlwaysIncluded;
       
   347     }
       
   348 
       
   349 // --------------------------------------------------------------------------
       
   350 // CPbk2FilteredViewStackElement::IsSame
       
   351 // --------------------------------------------------------------------------
       
   352 //
       
   353 TBool CPbk2FilteredViewStackElement::IsSame( 
       
   354         const MPbk2FilteredViewStackElement& aOther ) const
       
   355     {
       
   356     // Elements are same if:
       
   357     // 1) Filter level is same
       
   358     // 2) Find texts match
       
   359     return ( aOther.Level() == Level() && 
       
   360              Match( aOther.FindArray(), FindArray() ) );
       
   361     }
       
   362 
       
   363 // --------------------------------------------------------------------------
       
   364 // CPbk2FilteredViewStackElement::UpdateFilterL
       
   365 // --------------------------------------------------------------------------
       
   366 //
       
   367 void CPbk2FilteredViewStackElement::UpdateFilterL(
       
   368         MVPbkContactViewBase& aBaseView,
       
   369         MVPbkContactViewObserver& aObserver,
       
   370         const MDesCArray& aSearchLevelArray,
       
   371         const MVPbkContactBookmarkCollection* aAlwaysIncluded )
       
   372     {
       
   373     __ASSERT_DEBUG( iView, Panic( EPreCond_UpdateFilterL ) );
       
   374 
       
   375     // update find text, first copy it to new array
       
   376     TInt count = aSearchLevelArray.MdcaCount();
       
   377     CDesCArrayFlat* temp = new ( ELeave ) CDesCArrayFlat( count );
       
   378     
       
   379     for ( TInt i = 0; i < count; ++i )
       
   380         {
       
   381         temp->AppendL( aSearchLevelArray.MdcaPoint( i ) );
       
   382         }
       
   383 
       
   384     delete iSearchLevelArray;
       
   385     iSearchLevelArray = temp;
       
   386 
       
   387     if( FindMustBeDigraphicL() )
       
   388         {
       
   389         delete iView;
       
   390         iView = 0;
       
   391         iView = aBaseView.ViewFiltering()->CreateFilteredViewLC( 
       
   392             aObserver, *iSearchLevelArray, iAlwaysIncluded );
       
   393         CleanupStack::Pop(); // iView
       
   394         }
       
   395     else
       
   396         {
       
   397         iView->ViewFiltering()->UpdateFilterL( 
       
   398             *iSearchLevelArray, aAlwaysIncluded );
       
   399         iState = EUpdating;
       
   400         }
       
   401     }
       
   402 
       
   403 
       
   404 // --------------------------------------------------------------------------
       
   405 // CPbk2FilteredViewStackElement::ViewState
       
   406 // --------------------------------------------------------------------------
       
   407 //
       
   408 MPbk2FilteredViewStackElement::TViewState 
       
   409         CPbk2FilteredViewStackElement::ViewState() const
       
   410     {
       
   411     return iState;    
       
   412     }
       
   413 
       
   414 // --------------------------------------------------------------------------
       
   415 // CPbk2FilteredViewStackElement::SetViewState
       
   416 // --------------------------------------------------------------------------
       
   417 //
       
   418 void CPbk2FilteredViewStackElement::SetViewState( TViewState aState )
       
   419     {
       
   420     iState = aState;
       
   421     }
       
   422 
       
   423 // --------------------------------------------------------------------------
       
   424 // CPbk2FilteredViewStackElement::UnderDestruction
       
   425 // --------------------------------------------------------------------------
       
   426 //
       
   427 TBool CPbk2FilteredViewStackElement::UnderDestruction() const
       
   428     {
       
   429     return iUnderDestruction;
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------
       
   433 // CPbk2FilteredViewStackElement::SetUnderDestruction
       
   434 // --------------------------------------------------------------------------
       
   435 //
       
   436 void CPbk2FilteredViewStackElement::SetUnderDestruction()
       
   437     {
       
   438     iUnderDestruction = ETrue;
       
   439     }
       
   440 
       
   441 // --------------------------------------------------------------------------
       
   442 // CPbk2FilteredViewStackElement::Match
       
   443 // --------------------------------------------------------------------------
       
   444 //
       
   445 TBool CPbk2FilteredViewStackElement::Match( const MDesCArray& aArray1, 
       
   446         const MDesCArray& aArray2 ) const
       
   447     {
       
   448     TBool isMatch = EFalse;    
       
   449     TInt count = aArray1.MdcaCount();
       
   450     if ( count == aArray2.MdcaCount() )
       
   451         {
       
   452         for ( TInt i = 0; i < count; ++i )
       
   453             {
       
   454             if ( aArray1.MdcaPoint( i ).CompareC( 
       
   455                  aArray2.MdcaPoint( i ) ) == 0 )
       
   456                 {
       
   457                 isMatch = ETrue;
       
   458                 }
       
   459             else
       
   460                 {
       
   461                 isMatch = EFalse;
       
   462                 break;
       
   463                 }
       
   464             }
       
   465         }
       
   466     return isMatch;
       
   467     }
       
   468     
       
   469 // --------------------------------------------------------------------------
       
   470 // CPbk2FilteredViewStackElement::CountLengthOfStrings
       
   471 // Counts characters of array of strings
       
   472 // --------------------------------------------------------------------------
       
   473 //
       
   474 TInt CPbk2FilteredViewStackElement::CountLengthOfStrings( const MDesCArray& 
       
   475     aStringArray ) const
       
   476     {
       
   477     TInt stringCount = aStringArray.MdcaCount();
       
   478     TInt totalLength = 0;
       
   479     for ( TInt i = 0; i < stringCount; ++i )
       
   480         {
       
   481         totalLength += aStringArray.MdcaPoint( i ).Length();
       
   482         }
       
   483         
       
   484     return totalLength;
       
   485     }
       
   486     
       
   487 // End of File