phonebookengines/VirtualPhonebook/VPbkSimStoreImpl/src/CContactArray.cpp
changeset 0 e686773b3f54
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:  An array of sim contacts
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CContactArray.h"
       
    22 #include "VPbkSimStoreImplError.h"
       
    23 #include "CVPbkSimContactBase.h"
       
    24 #include "CVPbkSimCntField.h"
       
    25 #include <VPbkSimStoreTemplateFunctions.h>
       
    26 #include <VPbkSimStoreCommon.h>
       
    27 
       
    28 namespace VPbkSimStoreImpl {
       
    29 
       
    30 // CONSTANTS
       
    31 
       
    32 // expands memory with this amount if array gets too small
       
    33 const TInt KCntArrayGranularity = 50;
       
    34 
       
    35 // ============================= LOCAL FUNCTIONS ===============================
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CContactArray::CContactArray
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CContactArray::CContactArray() : iCntArray( KCntArrayGranularity )
       
    46     {
       
    47     }
       
    48     
       
    49 // Destructor
       
    50 CContactArray::~CContactArray()
       
    51     {
       
    52     iObservers.Close();
       
    53     iCntArray.ResetAndDestroy();
       
    54     iCntArray.Close();
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CContactArray::AddOrReplaceL
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CContactArray::AddOrReplaceL( MVPbkSimContact* aSimContact )
       
    62     {
       
    63     // SIM contacts must have a valid SIM index because this array is ordered
       
    64     // by the SIM index.
       
    65     __ASSERT_DEBUG( aSimContact && aSimContact->SimIndex() >= 
       
    66         KVPbkSimStoreFirstETelIndex,
       
    67         VPbkSimStoreImpl::Panic( ECContactArrayAddOrReplaceL_NullSimContact ) );
       
    68     if ( !aSimContact || 
       
    69          aSimContact->SimIndex() < KVPbkSimStoreFirstETelIndex )
       
    70         {
       
    71         User::Leave( KErrArgument );
       
    72         }
       
    73     
       
    74     // If array is not big enough, enlarge it.
       
    75     ReserveL( aSimContact->SimIndex() );
       
    76     TInt arrayIndex = aSimContact->SimIndex() - 1;
       
    77 
       
    78     // Take a pointer to the possible old contact. There must not be
       
    79     // any leaving operations after this.
       
    80     MVPbkSimContact* cnt = iCntArray[arrayIndex];
       
    81     // Replace the possible existing contact
       
    82     iCntArray[arrayIndex] = aSimContact;
       
    83     if ( cnt )
       
    84         {
       
    85         SendObserverMessageRR( iObservers,
       
    86             &MContactArrayObserver::ContactChanged, *cnt, *aSimContact );
       
    87         // Delete old contact
       
    88         delete cnt;
       
    89         }
       
    90     else
       
    91         {
       
    92         SendObserverMessageR( iObservers, &MContactArrayObserver::ContactAdded,
       
    93             *aSimContact );
       
    94         }        
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CContactArray::Delete
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CContactArray::Delete( TInt aFromSimIndex, TInt aToSimIndex )
       
   102     {
       
   103     TInt indexFrom = aFromSimIndex - 1;
       
   104     TInt indexTo = aToSimIndex - 1;
       
   105     if ( indexTo < indexFrom )
       
   106         {
       
   107         indexTo = indexFrom;
       
   108         }
       
   109 
       
   110     TInt size = Size();
       
   111     TInt simIndex = aFromSimIndex;
       
   112     for ( TInt i = indexFrom; i < size && i <= indexTo; 
       
   113         ++i, ++simIndex )
       
   114         {
       
   115         if ( iCntArray[i] )
       
   116             {
       
   117             // There must not be any leaving operation after this
       
   118             MVPbkSimContact* cnt = iCntArray[i];
       
   119             iCntArray[i] = NULL;
       
   120             SendObserverMessageR( iObservers, 
       
   121                 &MContactArrayObserver::ContactRemoved, *cnt );
       
   122             delete cnt;
       
   123             }
       
   124         }
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CContactArray::At
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 MVPbkSimContact* CContactArray::At( TInt aSimIndex ) const
       
   132     {
       
   133     // indexes start from 0 in the array and from 1 in the sim
       
   134     TInt arrayIndex = aSimIndex - 1;
       
   135     __ASSERT_DEBUG( arrayIndex < iCntArray.Count(),
       
   136         VPbkSimStoreImpl::Panic( EInvalidSimIndex ) );
       
   137     
       
   138     if ( arrayIndex < iCntArray.Count() )
       
   139         {
       
   140         return iCntArray[arrayIndex];
       
   141         }
       
   142     return NULL;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CContactArray::ResetAndCompress
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CContactArray::DeleteAll()
       
   150     {
       
   151     const TInt firstSimIndex = 1;
       
   152     const TInt lastSimIndex = iCntArray.Count();
       
   153     Delete( firstSimIndex, lastSimIndex );
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CContactArray::AddObserverL
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CContactArray::AddObserverL( MContactArrayObserver& aObserver )
       
   161     {
       
   162     iObservers.AppendL( &aObserver );
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CContactArray::AddObserverL
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CContactArray::RemoveObserver( MContactArrayObserver& aObserver )
       
   170     {
       
   171     TInt pos = iObservers.Find( &aObserver );
       
   172     if ( pos >= 0 )
       
   173         {
       
   174         iObservers.Remove( pos );
       
   175         }
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CContactArray::ReserveL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CContactArray::ReserveL( TInt aSize )
       
   183     {
       
   184     while ( iCntArray.Count() < aSize )
       
   185         {
       
   186         iCntArray.AppendL( NULL );
       
   187         }
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CContactArray::FirstEmptySlot
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 TInt CContactArray::FirstEmptySlot() const
       
   195     {
       
   196     TInt slot = iCntArray.Find(NULL);
       
   197     if ( slot != KErrNotFound )
       
   198         {
       
   199         // SIM indexes start from 1.
       
   200         ++slot;
       
   201         }
       
   202     return slot;
       
   203     }
       
   204 
       
   205 } // namespace VPbkSimStoreImpl
       
   206 //  End of File