phonebookengines/VirtualPhonebook/VPbkSimStore/src/CCommitContactsOperation.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 contact operation for commtting multiple contacts
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CCommitContactsOperation.h"
       
    21 
       
    22 #include <MVPbkBatchOperationObserver.h>
       
    23 #include <MVPbkStoreContact.h>
       
    24 #include "VPbkSimStoreError.h"
       
    25 
       
    26 namespace VPbkSimStore {
       
    27 
       
    28 // CONSTANTS
       
    29 
       
    30 // Contacts are commited 1 contact at a time
       
    31 const TInt KStepSize = 1;
       
    32 
       
    33 // ============================= LOCAL FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // SendStepFailed
       
    37 // Send step failed event to given observer.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 inline TBool SendStepFailed( CCommitContactsOperation& aOperation, TInt aError,
       
    41     MVPbkBatchOperationObserver& aObserver )
       
    42     { 
       
    43     return aObserver.StepFailed( aOperation, KStepSize, aError );
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // SendStepComplete
       
    48 // Send step completed event to given observer.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 inline void SendStepComplete( CCommitContactsOperation& aOperation,
       
    52     MVPbkBatchOperationObserver& aObserver )
       
    53     {
       
    54     aObserver.StepComplete( aOperation, KStepSize );
       
    55     }
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CCommitContactsOperation::CCommitContactsOperation
       
    61 // C++ default constructor can NOT contain any code, that
       
    62 // might leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CCommitContactsOperation::CCommitContactsOperation( CContactStore& aStore,
       
    66     MVPbkBatchOperationObserver& aObserver )
       
    67     :   iStore( aStore ),
       
    68         iObserver( aObserver )
       
    69     {
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CCommitContactsOperation::ConstructL
       
    74 // Symbian 2nd phase constructor can leave.
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CCommitContactsOperation::ConstructL( 
       
    78     const TArray<MVPbkStoreContact*>& aContacts )
       
    79     {
       
    80     const TInt count = aContacts.Count();
       
    81     for ( TInt i = 0; i < count; ++i )
       
    82         {
       
    83         iStoreContacts.AppendL( aContacts[i] );
       
    84         }
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CCommitContactsOperation::NewL
       
    89 // Two-phased constructor.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CCommitContactsOperation* CCommitContactsOperation::NewL( 
       
    93     CContactStore& aStore, 
       
    94     MVPbkBatchOperationObserver& aObserver,
       
    95     const TArray<MVPbkStoreContact*>& aContacts )
       
    96     {
       
    97     CCommitContactsOperation* self = 
       
    98         new( ELeave ) CCommitContactsOperation( aStore, aObserver );
       
    99     CleanupStack::PushL( self );
       
   100     self->ConstructL( aContacts );
       
   101     CleanupStack::Pop( self );
       
   102     return self;
       
   103     }
       
   104 
       
   105     
       
   106 // Destructor
       
   107 CCommitContactsOperation::~CCommitContactsOperation()
       
   108     {
       
   109     iStoreContacts.Close();
       
   110     }
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CCommitContactsOperation::StartL
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CCommitContactsOperation::StartL()
       
   117     {
       
   118     __ASSERT_DEBUG( iStoreContacts.Count() > 0,
       
   119         VPbkSimStore::Panic( VPbkSimStore::EZeroCommitContacts ) );
       
   120     iCommitCounter = 0;
       
   121     User::LeaveIfError( NextCycle() );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CCommitContactsOperation::Cancel
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CCommitContactsOperation::Cancel()
       
   129     {
       
   130     iCommitCounter = iStoreContacts.Count();
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CCommitContactsOperation::ContactOperationCompleted
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CCommitContactsOperation::ContactOperationCompleted( 
       
   138     TContactOpResult /*aResult*/ )
       
   139     {
       
   140     if ( iCommitCounter >= iStoreContacts.Count() )
       
   141         {
       
   142         iObserver.OperationComplete( *this );
       
   143         }
       
   144     else
       
   145         {
       
   146         SendStepComplete( *this, iObserver );
       
   147         TryCommitNext();
       
   148         }
       
   149     }
       
   150   
       
   151 // -----------------------------------------------------------------------------
       
   152 // CCommitContactsOperation::ContactOperationFailed
       
   153 // -----------------------------------------------------------------------------
       
   154 //   
       
   155 void CCommitContactsOperation::ContactOperationFailed( TContactOp /*aOpCode*/, 
       
   156     TInt aErrorCode, TBool /*aErrorNotified*/ )
       
   157     {
       
   158     if ( SendStepFailed( *this, aErrorCode, iObserver ) )
       
   159         {
       
   160         // Continue only if ETrue is returned. Otherwise this operation might
       
   161         // be already deleted.
       
   162         TryCommitNext();        
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CCommitContactsOperation::NextCycle
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TInt CCommitContactsOperation::NextCycle()
       
   171     {
       
   172     TRAPD( result, iStoreContacts[iCommitCounter]->CommitL( *this ) );
       
   173     ++iCommitCounter;
       
   174     return result;
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CCommitContactsOperation::TryCommitNext
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 void CCommitContactsOperation::TryCommitNext()
       
   182     {
       
   183     const TInt totalCount = iStoreContacts.Count(); 
       
   184     TInt result = KErrNone;
       
   185     TBool abortedByClient = EFalse;
       
   186     do
       
   187         {
       
   188         result = NextCycle();
       
   189         if ( result != KErrNone )
       
   190             {
       
   191             if ( !SendStepFailed( *this, result, iObserver ) )
       
   192                 {
       
   193                 // Set aborted, we can't continue because this operation might
       
   194                 // be already deleted.
       
   195                 abortedByClient = ETrue;
       
   196                 }
       
   197             }
       
   198         } while ( !abortedByClient &&           // Client is not aborted this operation
       
   199                   result != KErrNone && 
       
   200                   iCommitCounter < totalCount );
       
   201                   
       
   202     if ( !abortedByClient && result != KErrNone && iCommitCounter >= totalCount )
       
   203         {
       
   204         iObserver.OperationComplete( *this );
       
   205         }
       
   206     }
       
   207 } // namespace VPbkSimStore
       
   208 //  End of File  
       
   209