phonebookengines/VirtualPhonebook/VPbkSimStoreImpl/src/CMultipleReadCmd.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:  A class that can read multiple contacts from ETel store
       
    15 *                Uses ETel RMobilePhoneBookStore Read
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CMultipleReadCmd.h"
       
    23 
       
    24 #include "CVPbkETelCntConverter.h"
       
    25 #include "CVPbkSimContactBuf.h"
       
    26 #include "CVPbkSimCntField.h"
       
    27 #include "VPbkSimStoreImplError.h"
       
    28 #include "CBasicStore.h"
       
    29 #include "CContactArray.h"
       
    30 #include "VPbkDebug.h"
       
    31 
       
    32 namespace VPbkSimStoreImpl {
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // Define some default size of one contact for buffer creation
       
    37 const TInt KDefaultSizeForOneContact = 256;
       
    38 // Define a maximum size for the read buffer. 20 contacts at one request is
       
    39 // a pretty good base for maximum.
       
    40 const TInt KMaxSizeForBuffer = 20 * KDefaultSizeForOneContact;
       
    41 
       
    42 // ============================= LOCAL FUNCTIONS ===============================
       
    43 
       
    44 namespace {
       
    45 // -----------------------------------------------------------------------------
       
    46 // Return the number of indexes
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 inline TInt AmountOfIndexes( TInt aLastIndex, TInt aFirstIndex )
       
    50     {
       
    51     return aLastIndex - aFirstIndex + 1;
       
    52     }
       
    53 } // unnamed namespace
       
    54 
       
    55 // ============================ MEMBER FUNCTIONS ===============================
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CMultipleReadCmd::CMultipleReadCmd
       
    59 // C++ default constructor can NOT contain any code, that
       
    60 // might leave.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CMultipleReadCmd::CMultipleReadCmd( CBasicStore& aBasicStore, 
       
    64         TInt aFirstIndexToRead, TInt aLastIndexToRead )
       
    65         :   CActive( EPriorityStandard ),
       
    66             iBasicStore( aBasicStore ),
       
    67             iFirstIndexToRead( aFirstIndexToRead ),
       
    68             iLastIndexToRead( aLastIndexToRead ),
       
    69             iIndexToRead( KVPbkSimStoreInvalidETelIndex ),
       
    70             iBufferPtr( 0, NULL )
       
    71     {
       
    72     CActiveScheduler::Add( this );
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CMultipleReadCmd::CMultipleReadCmd
       
    77 // C++ default constructor can NOT contain any code, that
       
    78 // might leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CMultipleReadCmd::CMultipleReadCmd( CBasicStore& aBasicStore, 
       
    82         TInt& aIndexToRead )
       
    83         :   CActive( EPriorityStandard ),
       
    84             iBasicStore( aBasicStore ),
       
    85             iFirstIndexToRead( KVPbkSimStoreInvalidETelIndex ),
       
    86             iLastIndexToRead( KVPbkSimStoreInvalidETelIndex ),
       
    87             iIndexToRead( aIndexToRead ),
       
    88             iBufferPtr( 0, NULL )
       
    89     {
       
    90     CActiveScheduler::Add( this );
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CMultipleReadCmd::ConstructL
       
    95 // Symbian 2nd phase constructor can leave.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CMultipleReadCmd::ConstructL()
       
    99     {
       
   100     iCntConverter = CVPbkETelCntConverter::NewL();
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CMultipleReadCmd::NewLC
       
   105 // Two-phased constructor.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CMultipleReadCmd* CMultipleReadCmd::NewLC( CBasicStore& aBasicStore )
       
   109     {
       
   110     CMultipleReadCmd* self = new( ELeave ) CMultipleReadCmd( aBasicStore, 
       
   111         KVPbkSimStoreFirstETelIndex, KVPbkSimStoreInvalidETelIndex );
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL();
       
   114     return self;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CMultipleReadCmd::NewLC
       
   119 // Two-phased constructor.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 CMultipleReadCmd* CMultipleReadCmd::NewLC( CBasicStore& aBasicStore,    
       
   123         TInt aFirstIndexToRead, TInt aLastIndexToRead )
       
   124     {
       
   125     CMultipleReadCmd* self = new( ELeave ) CMultipleReadCmd( aBasicStore, 
       
   126             aFirstIndexToRead, aLastIndexToRead );
       
   127     CleanupStack::PushL( self );
       
   128     self->ConstructL();
       
   129     return self;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CMultipleReadCmd::NewLC
       
   134 // Two-phased constructor.
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 CMultipleReadCmd* CMultipleReadCmd::NewLC( CBasicStore& aBasicStore, 
       
   138         TInt& aIndexToRead )
       
   139     {
       
   140     CMultipleReadCmd* self = new( ELeave ) CMultipleReadCmd( aBasicStore, 
       
   141         aIndexToRead );
       
   142     CleanupStack::PushL( self );
       
   143     self->ConstructL();
       
   144     return self;
       
   145     }
       
   146     
       
   147 // Destructor
       
   148 CMultipleReadCmd::~CMultipleReadCmd()
       
   149     {
       
   150     CancelCmd();
       
   151     delete iCntConverter;
       
   152     delete iBuffer;
       
   153     iCntArray.ResetAndDestroy();
       
   154     iCntArray.Close();
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CMultipleReadCmd::Execute
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CMultipleReadCmd::Execute()
       
   162     {
       
   163     __ASSERT_DEBUG( iObserver,
       
   164         VPbkSimStoreImpl::Panic( EPreCondCMultipleReadCmdExecute ) );
       
   165         
       
   166     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   167         "VPbkSimStoreImpl: CMultipleReadCmd::Execute Request") );        
       
   168         
       
   169     if ( iIndexToRead != KVPbkSimStoreInvalidETelIndex )
       
   170         {
       
   171         iFirstIndexToRead = iIndexToRead;
       
   172         iLastIndexToRead = iIndexToRead;
       
   173         }
       
   174     else if ( iLastIndexToRead == KVPbkSimStoreInvalidETelIndex )
       
   175         {
       
   176         iLastIndexToRead = iBasicStore.ETelStoreInfo().iTotalEntries;
       
   177         }
       
   178 
       
   179     // Define the size of the buffer. ETel iTotalEntries can not be trusted.
       
   180     TInt amountOfIndexes = 
       
   181         AmountOfIndexes( iLastIndexToRead, iFirstIndexToRead );
       
   182     if ( amountOfIndexes > 0 )
       
   183         {
       
   184         // Create a buffer according to amount of possible contacts to read.
       
   185         // There must be a limit for the size.
       
   186         TInt size = Min( KMaxSizeForBuffer, 
       
   187             amountOfIndexes * KDefaultSizeForOneContact );
       
   188         TRAPD( result, iBuffer = HBufC8::NewL( size ) );
       
   189         if ( result != KErrNone )
       
   190             {
       
   191             DoDummyRequest( result );
       
   192             }
       
   193         else
       
   194             {
       
   195             iBufferPtr.Set( iBuffer->Des() );
       
   196             DoRequest();
       
   197             }
       
   198         }
       
   199     else
       
   200         {
       
   201         // There was probably a problem with ETel iTotalEntries.
       
   202         // Behave same as iTotalEntries would be zero. Completing with
       
   203         // KErrNotFound from ETel means that there were no contacts
       
   204         // in given index range.
       
   205         DoDummyRequest( KErrNotFound );
       
   206         }
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CMultipleReadCmd::AddObserverL
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CMultipleReadCmd::AddObserverL( MVPbkSimCommandObserver& aObserver )
       
   214     {
       
   215     __ASSERT_DEBUG( !iObserver,
       
   216         VPbkSimStoreImpl::Panic( EPreCondCMultipleReadCmdAddObserverL ) );
       
   217     iObserver = &aObserver;
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CMultipleReadCmd::CancelCmd
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CMultipleReadCmd::CancelCmd()
       
   225     {
       
   226     CActive::Cancel();
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CMultipleReadCmd::RunL
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CMultipleReadCmd::RunL()
       
   234     {
       
   235     TInt result = iStatus.Int();
       
   236 
       
   237     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   238         "VPbkSimStoreImpl: RMobilePhoneBooStore::Read h%d complete %d"),
       
   239         iBasicStore.ETelStore().SubSessionHandle(), result);
       
   240 
       
   241     switch ( result )
       
   242         {
       
   243         case KErrNone:
       
   244             {
       
   245             // Convert from ETel to own contacts
       
   246             ConvertContactsL();
       
   247             // Check that was the buffer big enough
       
   248             if ( WasBufferBigEnough() )
       
   249                 {
       
   250                 // Contacts were found -> update contact array
       
   251                 UpdateContactArrayL();
       
   252                 }
       
   253             // Buffer wasn't big enough -> Check that it's not growing
       
   254             // for ever.
       
   255             else if ( !IsBufferSizeInSane() )
       
   256                 {
       
   257                 // Make buffer bigger and read again
       
   258                 EnlargeBufferL();
       
   259                 }
       
   260             else
       
   261                 {
       
   262                 // Stop trying -> there is something wrong in ETel 
       
   263                 result = KErrOverflow;
       
   264                 }
       
   265             
       
   266             // Stop if error
       
   267             if ( result != KErrNone )
       
   268                 {
       
   269                 iObserver->CommandError( *this, result );
       
   270                 }
       
   271             // If all the indexes have been read and we are done.
       
   272             else if ( AmountOfIndexes( 
       
   273                         iLastIndexToRead, iFirstIndexToRead ) <= 0 )
       
   274                 {
       
   275                 iObserver->CommandDone( *this );
       
   276                 }
       
   277             // There are still contacts to read
       
   278             else
       
   279                 {
       
   280                 DoRequest();
       
   281                 }
       
   282             break;
       
   283             }
       
   284         case KErrNotFound:
       
   285             {
       
   286             // If nothing found update the array and complete command.
       
   287             iBasicStore.Contacts().Delete( iFirstIndexToRead, iLastIndexToRead );
       
   288             iObserver->CommandDone( *this );
       
   289             break;
       
   290             }
       
   291         default:
       
   292             {
       
   293             iObserver->CommandError( *this, result );
       
   294             break;
       
   295             }
       
   296         }
       
   297     }
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CMultipleReadCmd::DoCancel
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 void CMultipleReadCmd::DoCancel()
       
   304     {
       
   305     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   306         "VPbkSimStoreImpl: RMobilePhoneBooStore::Read h%d Cancel"),
       
   307         iBasicStore.ETelStore().SubSessionHandle());
       
   308     iBasicStore.ETelStore().CancelAsyncRequest( EMobilePhoneStoreRead );
       
   309     }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CMultipleReadCmd::RunError
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 TInt CMultipleReadCmd::RunError( TInt aError )
       
   316     {
       
   317     iObserver->CommandError( *this, aError );
       
   318     return KErrNone;
       
   319     }
       
   320     
       
   321 // -----------------------------------------------------------------------------
       
   322 // CMultipleReadCmd::DoDummyRequest
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CMultipleReadCmd::DoDummyRequest( TInt aResult )
       
   326     {
       
   327     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   328         "VPbkSimStoreImpl: CMultipleReadCmd dummy request"));
       
   329     TRequestStatus* status = &iStatus;
       
   330     User::RequestComplete( status, aResult );
       
   331     SetActive();
       
   332     }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CMultipleReadCmd::DoRequest
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CMultipleReadCmd::DoRequest()
       
   339     {
       
   340     ResetBuffer();
       
   341 
       
   342     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   343         "VPbkSimStoreImpl: RMobilePhoneBooStore::Read h%d request\
       
   344         firstslot=%d,lastslot=%d"),
       
   345         iBasicStore.ETelStore().SubSessionHandle(),
       
   346         iFirstIndexToRead, iLastIndexToRead);
       
   347 
       
   348     iBasicStore.ETelStore().Read( iStatus, iFirstIndexToRead, 
       
   349         AmountOfIndexes( iLastIndexToRead, iFirstIndexToRead ), iBufferPtr );
       
   350     SetActive();
       
   351     }
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CMultipleReadCmd::IsBufferSizeInSane
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 TBool CMultipleReadCmd::IsBufferSizeInSane()
       
   358     {
       
   359     __ASSERT_DEBUG( iBuffer, 
       
   360         Panic( EPreCond_CMultipleReadCmd_IsBufferSizeInSane ) );
       
   361         
       
   362     if ( iBuffer->Size() >= KMaxSizeForBuffer )
       
   363         {
       
   364         return ETrue;
       
   365         }
       
   366     return EFalse;
       
   367     }
       
   368     
       
   369 // -----------------------------------------------------------------------------
       
   370 // CMultipleReadCmd::EnlargeBufferL
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CMultipleReadCmd::EnlargeBufferL()
       
   374     {
       
   375     __ASSERT_DEBUG( iBuffer, 
       
   376         Panic( EPreCond_CMultipleReadCmd_EnlargeBufferL ) );
       
   377         
       
   378     const TInt doubleFactor = 2;
       
   379     TInt newSize = iBufferPtr.MaxSize() * doubleFactor;
       
   380     
       
   381     HBufC8* buffer = HBufC8::NewL( newSize );
       
   382     delete iBuffer;
       
   383     iBuffer = buffer;
       
   384     iBufferPtr.Set( iBuffer->Des() );
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CMultipleReadCmd::ConvertContactsL
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CMultipleReadCmd::ConvertContactsL()
       
   392     {    
       
   393     // Make sure that there are no old contacts
       
   394     iCntArray.ResetAndDestroy();
       
   395     
       
   396     if ( iBufferPtr.Length() > 0 )
       
   397         {
       
   398         iCntConverter->ConvertFromETelToVPbkSimContactsL( 
       
   399             iCntArray, iBufferPtr, iBasicStore );
       
   400         }
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // CMultipleReadCmd::WasBufferBigEnough
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 TBool CMultipleReadCmd::WasBufferBigEnough()
       
   408     {
       
   409     // ConvertContactsL must be called before this. The logic is that if
       
   410     // ETel retuned KErrNone from Read request but there are not contacts
       
   411     // in iBuffer then the buffer was too small.
       
   412     return iCntArray.Count() > 0;
       
   413     }
       
   414     
       
   415 // -----------------------------------------------------------------------------
       
   416 // CMultipleReadCmd::UpdateContactArrayL
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 void CMultipleReadCmd::UpdateContactArrayL()
       
   420     {
       
   421     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   422         "VPbkSimStoreImpl: CMultipleReadCmd::UpdateContactArrayL "));
       
   423     
       
   424     __ASSERT_DEBUG( iCntArray.Count() > 0, Panic( EReadingFromETelFailed ) );
       
   425 
       
   426     // Get amount of contacts
       
   427     TInt cntCount = iCntArray.Count();
       
   428     for ( TInt i = cntCount - 1; i >= 0; --i )
       
   429         {
       
   430         // Check the biggest sim index that was read
       
   431         TInt simIndex = iCntArray[i]->SimIndex();
       
   432         if ( simIndex >= iFirstIndexToRead )
       
   433             {
       
   434             // iFirstIndexToRead must be the first sim index that was not read.
       
   435             iFirstIndexToRead = simIndex + 1; 
       
   436             }
       
   437 
       
   438         // Add contacts to contact array. Ownership changes.
       
   439         iBasicStore.Contacts().AddOrReplaceL( iCntArray[i] );
       
   440         iCntArray.Remove( i );
       
   441         }
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CMultipleReadCmd::ResetBuffer
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 void CMultipleReadCmd::ResetBuffer()
       
   449     {
       
   450     iBufferPtr.FillZ( iBufferPtr.MaxLength() );
       
   451     iBufferPtr.Zero();
       
   452     }
       
   453 } // namespace VPbkSimStoreImpl
       
   454 //  End of File