predictivesearch/PcsAlgorithm/Algorithm1/src/CPcsAdaptiveGrid.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Utility class to hold a single item for Adaptive Grid.
       
    15 *               Used to provide quickly to client applications (Phonebook)
       
    16 *               The Adaptive Grid for the full list of contacts, which is
       
    17 *               the one that is displayed when the Find Box is empty.
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CPcsDebug.h"
       
    23 #include "CPcsAdaptiveGridItem.h"
       
    24 #include "CPcsAdaptiveGrid.h"
       
    25 
       
    26 
       
    27 
       
    28 // ============================== MEMBER FUNCTIONS ============================
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // CPcsAdaptiveGrid::NewL
       
    32 // Two Phase Construction
       
    33 // ----------------------------------------------------------------------------
       
    34 CPcsAdaptiveGrid* CPcsAdaptiveGrid::NewL( )
       
    35 {
       
    36     return new ( ELeave ) CPcsAdaptiveGrid();
       
    37 } 
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // CPcsAdaptiveGrid::CPcsAdaptiveGrid
       
    41 // Constructor
       
    42 // ----------------------------------------------------------------------------
       
    43 CPcsAdaptiveGrid::CPcsAdaptiveGrid( )
       
    44 {
       
    45     iAdaptiveGrid.ResetAndDestroy();
       
    46 }
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // CPcsAdaptiveGrid::~CPcsAdaptiveGrid
       
    50 // Destructor
       
    51 // ----------------------------------------------------------------------------
       
    52 CPcsAdaptiveGrid::~CPcsAdaptiveGrid( )
       
    53 {
       
    54     iAdaptiveGrid.ResetAndDestroy();
       
    55 }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // CPcsAdaptiveGrid::Count
       
    59 // 
       
    60 // ----------------------------------------------------------------------------
       
    61 TInt CPcsAdaptiveGrid::Count( )
       
    62 {
       
    63     return iAdaptiveGrid.Count();
       
    64 }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // CPcsAdaptiveGrid::GetAdaptiveGridItemIndex
       
    68 // 
       
    69 // ----------------------------------------------------------------------------
       
    70 TInt CPcsAdaptiveGrid::GetAdaptiveGridItemIndex( const TChar& aChar )
       
    71 {
       
    72     TChar aUpperChar = User::UpperCase( aChar );
       
    73 
       
    74     TInt retValue = KErrNotFound;
       
    75 
       
    76     // The Grid items are sorted alphabetically, anyway the number of items in the
       
    77     // array is small so we do linear search
       
    78     for ( TInt i=0; i < iAdaptiveGrid.Count(); i++ )
       
    79         {
       
    80         if ( iAdaptiveGrid[i]->Character( ) == aUpperChar )
       
    81             {
       
    82             retValue = i;
       
    83             break;
       
    84             }
       
    85         }
       
    86 
       
    87     return retValue;
       
    88 }
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // CPcsAdaptiveGrid::GetAdaptiveGridItemPtr
       
    92 // 
       
    93 // ----------------------------------------------------------------------------
       
    94 CPcsAdaptiveGridItem* CPcsAdaptiveGrid::GetAdaptiveGridItemPtr( const TChar& aChar )
       
    95 {
       
    96     CPcsAdaptiveGridItem* retValue = NULL;
       
    97 
       
    98     TInt gridItemIndex = GetAdaptiveGridItemIndex( aChar );
       
    99     
       
   100     if ( gridItemIndex != KErrNotFound )
       
   101         {
       
   102         retValue = iAdaptiveGrid[gridItemIndex];
       
   103         }
       
   104 
       
   105     return retValue;
       
   106 }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 // CPcsAdaptiveGrid::IncrementRefCounter
       
   110 // 
       
   111 // ----------------------------------------------------------------------------
       
   112 void CPcsAdaptiveGrid::IncrementRefCounterL( const TChar& aChar, const TUint aSelector )
       
   113 {
       
   114     CPcsAdaptiveGridItem* gridItem = GetAdaptiveGridItemPtr( aChar );
       
   115     
       
   116     if ( gridItem == NULL )
       
   117         {
       
   118         TLinearOrder<CPcsAdaptiveGridItem> rule( CPcsAdaptiveGridItem::CompareByCharacter );
       
   119 
       
   120         CPcsAdaptiveGridItem* newGridItem = CPcsAdaptiveGridItem::NewL( aChar );
       
   121         newGridItem->IncrementRefCounter( aSelector );
       
   122         iAdaptiveGrid.InsertInOrder(newGridItem, rule);
       
   123         }
       
   124     else
       
   125         {
       
   126         gridItem->IncrementRefCounter( aSelector );
       
   127         }
       
   128 
       
   129     PRINT2 ( _L("CPcsAdaptiveGrid::IncrementRefCounterL: Char='%c', Selector=%d"),
       
   130              (TUint) aChar, aSelector );
       
   131     // PrintAdatptiveGrid( );
       
   132 }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // CPcsAdaptiveGrid::DecrementRefCounter
       
   136 // 
       
   137 // ----------------------------------------------------------------------------
       
   138 void CPcsAdaptiveGrid::DecrementRefCounter( const TChar& aChar, const TUint aSelector )
       
   139 {
       
   140     TInt gridItemIndex = GetAdaptiveGridItemIndex( aChar );
       
   141 
       
   142     if ( gridItemIndex != KErrNotFound )
       
   143         {
       
   144         CPcsAdaptiveGridItem* gridItem = iAdaptiveGrid[gridItemIndex];
       
   145  
       
   146         gridItem->DecrementRefCounter( aSelector );
       
   147         
       
   148         // Remove the Grid item, if there is no reference to it anymore
       
   149         if ( gridItem->AreRefCountersZero( ) )
       
   150             {
       
   151             iAdaptiveGrid.Remove( gridItemIndex );
       
   152             delete gridItem;
       
   153             }
       
   154         }   
       
   155 
       
   156     PRINT2 ( _L("CPcsAdaptiveGrid::DecrementRefCounter: Char='%c', Selector=%d"),
       
   157              (TUint) aChar, aSelector );
       
   158     // PrintAdatptiveGrid( );
       
   159 }
       
   160 
       
   161 // ----------------------------------------------------------------------------
       
   162 // CPcsAdaptiveGrid::GetAdaptiveGrid
       
   163 // 
       
   164 // ----------------------------------------------------------------------------
       
   165 void CPcsAdaptiveGrid::GetAdaptiveGrid( const TBool aCompanyName, TDes& aGrid )
       
   166 {
       
   167     aGrid.Zero();
       
   168     TInt gridCharCount = 0;
       
   169     for ( TInt i=0; i < iAdaptiveGrid.Count(); i++ )
       
   170         {
       
   171         if ( iAdaptiveGrid[i]->IsRefCounterNonZero( aCompanyName ) )
       
   172             {
       
   173             aGrid.Append( iAdaptiveGrid[i]->Character() );
       
   174             gridCharCount++;
       
   175             if ( gridCharCount == KPsAdaptiveGridStringMaxLen )
       
   176                 {
       
   177                 break;
       
   178                 }
       
   179             }
       
   180         }
       
   181 }
       
   182 
       
   183 // ----------------------------------------------------------------------------
       
   184 // CPcsAdaptiveGrid::PrintAdatptiveGrid
       
   185 // 
       
   186 // ----------------------------------------------------------------------------
       
   187 void CPcsAdaptiveGrid::PrintAdatptiveGrid( )
       
   188 {
       
   189     PRINT ( _L("CPcsAdaptiveGrid::PrintAdatptiveGrid: ----------------------------------------") );
       
   190     PRINT1 ( _L("CPcsAdaptiveGrid::PrintAdatptiveGrid: Adaptive Grid (%d items)"), iAdaptiveGrid.Count() );
       
   191     PRINT ( _L("CPcsAdaptiveGrid::PrintAdatptiveGrid: ----------------------------------------") );
       
   192 
       
   193     for ( TInt i=0; i < iAdaptiveGrid.Count(); i++ )
       
   194         {
       
   195         CPcsAdaptiveGridItem* gridItem = iAdaptiveGrid[i];
       
   196 
       
   197         PRINT5 ( _L("CPcsAdaptiveGrid::PrintAdatptiveGrid: Grid[%d].Character=%c, RefCounters[%d,%d,%d]"),
       
   198                  i, (TUint) iAdaptiveGrid[i]->Character(),
       
   199                  iAdaptiveGrid[i]->GetRefCounter(CPcsAdaptiveGridItem::EFirstNameLastName),
       
   200                  iAdaptiveGrid[i]->GetRefCounter(CPcsAdaptiveGridItem::EUnnamedCompanyName),
       
   201                  iAdaptiveGrid[i]->GetRefCounter(CPcsAdaptiveGridItem::ECompanyName) );
       
   202         }
       
   203 
       
   204     PRINT ( _L("CPcsAdaptiveGrid::PrintAdatptiveGrid: ----------------------------------------") );
       
   205 }
       
   206 
       
   207 // ----------------------------------------------------------------------------
       
   208 // CPcsAdaptiveGrid::Reset
       
   209 // 
       
   210 // ----------------------------------------------------------------------------
       
   211 void CPcsAdaptiveGrid::Reset()
       
   212     {
       
   213     iAdaptiveGrid.ResetAndDestroy();
       
   214     }
       
   215 
       
   216 // End of file