uiservicetab/vimpstui/src/cvimpstuilistboxmodel.cpp
branchRCL_3
changeset 28 3104fc151679
parent 27 2b7283837edb
child 29 9a48e301e94b
equal deleted inserted replaced
27:2b7283837edb 28:3104fc151679
     1 /*
       
     2 * Copyright (c) 2008 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:  Custom listbox model for the contact list
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cvimpstuilistboxmodel.h"
       
    20 #include "mvimpstprocessarray.h"
       
    21 #include "uiservicetabtracer.h"
       
    22 
       
    23 // ======== MEMBER FUNCTIONS ========
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // CVIMPSTUiListBoxModel::CVIMPSTUiListBoxModel
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CVIMPSTUiListBoxModel::CVIMPSTUiListBoxModel()
       
    30     {
       
    31     }
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CVIMPSTUiListBoxModel::~CVIMPSTUiListBoxModel
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CVIMPSTUiListBoxModel::~CVIMPSTUiListBoxModel()
       
    38     {
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CVIMPSTUiListBoxModel::NumberOfItems
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 TInt CVIMPSTUiListBoxModel::NumberOfItems() const
       
    46     {
       
    47 	TRACER_AUTO;
       
    48     if( IsFilterActive() )
       
    49         {
       
    50         // Something is filtered
       
    51         return CountItems();
       
    52         }
       
    53     if( iItemTextArray )
       
    54         {
       
    55         // Fast
       
    56         return iItemTextArray->MdcaCount();
       
    57         }
       
    58 
       
    59     return 0;
       
    60     }
       
    61     
       
    62 // ---------------------------------------------------------------------------
       
    63 // CVIMPSTUiListBoxModel::ItemText
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 TPtrC CVIMPSTUiListBoxModel::ItemText( TInt aItemIndex ) const
       
    67     {
       
    68     if( IsFilterActive() )
       
    69         {
       
    70         // Slow
       
    71         return ItemAt( aItemIndex );
       
    72         }
       
    73 
       
    74     // Fast
       
    75     return iItemTextArray->MdcaPoint( aItemIndex );
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // CVIMPSTUiListBoxModel::FilteredItemIndex
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 TInt CVIMPSTUiListBoxModel::FilteredItemIndex( TInt aVisibleItemIndex ) const
       
    83     {
       
    84 	TRACER_AUTO;
       
    85     if( !IsFilterActive() )
       
    86         {
       
    87         // Nothing is filtered
       
    88         return aVisibleItemIndex;
       
    89         }
       
    90       if( Filter() )
       
    91         {
       
    92         // Get positions of lists
       
    93         RArray<TInt> pos;
       
    94        // iItemModel->MapContactListPositions( pos );
       
    95                
       
    96         // Count visible items until we reach the wanted one
       
    97         TInt itemcount = -1;
       
    98         TInt count = iItemTextArray->MdcaCount();
       
    99         for( TInt i = 0; i < count; ++i )
       
   100             {
       
   101             if( ( pos.Find( i ) != KErrNotFound && 
       
   102                 IsContactList( i, pos ) ) ||
       
   103                 Filter()->VisibleItemIndex( i ) != KErrNotFound )
       
   104                 {                
       
   105                 // this item is either visible by filter or
       
   106                 // it's our dynamically added contact list
       
   107                 itemcount++;
       
   108                 if( itemcount == aVisibleItemIndex )
       
   109                     {
       
   110                     pos.Close();
       
   111                     return i;
       
   112                     }
       
   113                }
       
   114             }
       
   115         pos.Close();
       
   116         } 
       
   117     return 0;
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CVIMPSTUiListBoxModel::VisibleItemIndex
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 TInt CVIMPSTUiListBoxModel::VisibleItemIndex( TInt aOriginalIndex ) const
       
   125     {
       
   126 	TRACER_AUTO;
       
   127     if( !IsFilterActive() )
       
   128         {
       
   129         // Nothing is filtered
       
   130         return aOriginalIndex;
       
   131         }
       
   132     
       
   133     // Get positions of contact lists
       
   134     RArray<TInt> pos;
       
   135     //iItemModel->MapContactListPositions( pos );
       
   136     
       
   137     // Check if it's even visible
       
   138     TInt index = Filter()->VisibleItemIndex( aOriginalIndex );
       
   139     TBool visibleList = IsContactList( aOriginalIndex, pos );
       
   140     if( index == KErrNotFound && !visibleList )
       
   141         {
       
   142         pos.Close();
       
   143         return KErrNotFound;
       
   144         }
       
   145     
       
   146     // Count up to original index to find out the visible position
       
   147     TInt visibleIndex = -1;
       
   148     for( TInt i = 0; i <= aOriginalIndex; ++i )
       
   149         {
       
   150         if( ( pos.Find( i ) != KErrNotFound && 
       
   151             IsContactList( i, pos ) ) ||
       
   152             Filter()->VisibleItemIndex( i ) != KErrNotFound )
       
   153             {
       
   154             // this item is either visible by filter or
       
   155             // it's our dynamically added contact list
       
   156             visibleIndex++;
       
   157             }
       
   158         }
       
   159     pos.Close();
       
   160     return visibleIndex;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // CVIMPSTUiListBoxModel::CountItems
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 TInt CVIMPSTUiListBoxModel::CountItems() const
       
   168     {
       
   169 	TRACER_AUTO;
       
   170     TInt itemcount = 0;
       
   171     if( Filter() )
       
   172         {
       
   173         // Count of visible items by filter
       
   174         itemcount = Filter()->FilteredNumberOfItems();
       
   175         if( itemcount == 0 )
       
   176             {
       
   177             return itemcount;
       
   178             }
       
   179 
       
   180         // Get positions of contact lists
       
   181         RArray<TInt> pos;
       
   182         //iItemModel->MapContactListPositions( pos );
       
   183                        
       
   184         // Scan through list positions and count the additional visible items.
       
   185         TInt count = pos.Count();
       
   186         for( TInt i = 0; i < count; ++i )
       
   187             {
       
   188             if( IsContactList( pos[i], pos ) && 
       
   189                 Filter()->VisibleItemIndex( pos[i] ) == KErrNotFound )
       
   190                 {
       
   191                 // List should be visible because it has found contacts in it
       
   192                 // but it's filtered out by searchfield. Add it dynamically.
       
   193                 itemcount++;
       
   194                 }
       
   195             }
       
   196         pos.Close();
       
   197         }
       
   198     return itemcount;
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CVIMPSTUiListBoxModel::ItemAt
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 TPtrC CVIMPSTUiListBoxModel::ItemAt( TInt aIndex ) const
       
   206     {
       
   207 	TRACER_AUTO;
       
   208     TInt itemcount = -1;
       
   209     if( Filter() )
       
   210         {
       
   211         // Get positions of contact lists
       
   212         RArray<TInt> pos;       
       
   213         
       
   214         // Scan through visible items until we reach the wanted one
       
   215         TInt count = iItemTextArray->MdcaCount();
       
   216         for( TInt i = 0; i < count; ++i )
       
   217             {
       
   218             if( ( pos.Find( i ) != KErrNotFound && 
       
   219                 IsContactList( i, pos ) ) ||
       
   220                 Filter()->VisibleItemIndex( i ) != KErrNotFound )
       
   221                 {
       
   222                 // this item is either visible by filter or
       
   223                 // it's our dynamically added contact list
       
   224                 itemcount++;
       
   225                 if( itemcount == aIndex )
       
   226                     {
       
   227                     // Found it
       
   228                     pos.Close();
       
   229                     return iItemTextArray->MdcaPoint( i );
       
   230                     }
       
   231                 }
       
   232             }
       
   233         pos.Close();
       
   234         }
       
   235     return KNullDesC();
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // CVIMPSTUiListBoxModel::IsContactList
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 TBool CVIMPSTUiListBoxModel::IsContactList( TInt aIndex, RArray<TInt>& aPosArray ) const
       
   243     {
       
   244 	TRACER_AUTO;
       
   245     TInt startInd = aPosArray.Find( aIndex );
       
   246     if( KErrNotFound == startInd )
       
   247         {
       
   248         // aIndex not found in array -> not even list
       
   249         return EFalse;
       
   250         }
       
   251 
       
   252     TInt count = aPosArray.Count();
       
   253     TInt end = 0;
       
   254     if( startInd >= count - 1 )
       
   255         {
       
   256         // aIndex is the last list:
       
   257         // Search from aIndex to last item
       
   258         end = iItemTextArray->MdcaCount();
       
   259         }
       
   260     else
       
   261         {
       
   262         // Search from aInded to next item in array
       
   263         end = aPosArray[ startInd + 1 ];
       
   264         }
       
   265     
       
   266     // Try to find visible contacts for this list
       
   267     for ( TInt i = aIndex + 1; i < end; ++i )
       
   268         {
       
   269         if( Filter()->VisibleItemIndex( i ) != KErrNotFound )
       
   270             {
       
   271             // found one
       
   272             return ETrue;
       
   273             }
       
   274         }
       
   275     // not found
       
   276     return EFalse;
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CVIMPSTUiListBoxModel::IsFilterActive
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 TBool CVIMPSTUiListBoxModel::IsFilterActive() const
       
   284     {
       
   285     if( Filter() )
       
   286         {
       
   287         return iItemTextArray->MdcaCount() != 
       
   288                Filter()->FilteredNumberOfItems();
       
   289         }
       
   290     return EFalse;
       
   291     }
       
   292 // ---------------------------------------------------------------------------
       
   293 // CVIMPSTUiListBoxModel::MatchableTextArray
       
   294 // ---------------------------------------------------------------------------
       
   295 //
       
   296 const MDesCArray* CVIMPSTUiListBoxModel::MatchableTextArray() const
       
   297     {
       
   298     return this;
       
   299     }
       
   300     
       
   301 // ---------------------------------------------------------------------------
       
   302 // CVIMPSTUiListBoxModel::MdcaCount
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TInt CVIMPSTUiListBoxModel::MdcaCount() const
       
   306     {
       
   307     return iItemModel->Count();
       
   308     }
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CVIMPSTUiListBoxModel::MdcaPoint
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 TPtrC CVIMPSTUiListBoxModel::MdcaPoint( TInt aIndex ) const
       
   315     {
       
   316     return iItemModel->GetItemNameText( aIndex );
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CVIMPSTUiListBoxModel::SetContactListModel
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CVIMPSTUiListBoxModel::SetContactListModel( MVIMPSTProcessArray& 
       
   324                                                   aContactListModel )
       
   325     {
       
   326     iItemModel = &aContactListModel;
       
   327     }
       
   328 
       
   329 // End of file