phonebookengines/VirtualPhonebook/VPbkEng/src/CVPbkContactEasyManager.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Easy management of contacts.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 
       
    21 #include "CVPbkContactEasyManager.h"
       
    22 
       
    23 #include <CVPbkContactManager.h>
       
    24 #include <CVPbkContactLinkArray.h>
       
    25 #include <MVPbkContactLink.h>
       
    26 #include <MVPbkContactOperationBase.h>
       
    27 #include <MVPbkOperationObserver.h>
       
    28 
       
    29 CVPbkContactEasyManager* CVPbkContactEasyManager::NewL(
       
    30     CVPbkContactManager& aContactManager )
       
    31     {
       
    32     CVPbkContactEasyManager* self =
       
    33         new (ELeave) CVPbkContactEasyManager(aContactManager);
       
    34     return self;
       
    35     }
       
    36 
       
    37 CVPbkContactEasyManager::~CVPbkContactEasyManager()
       
    38     {
       
    39     delete iOperation;
       
    40     delete iContactLinks;
       
    41     }
       
    42 
       
    43 void CVPbkContactEasyManager::RetrieveAndLockContactsL(
       
    44     const MVPbkContactLinkArray& aContactLinks,            
       
    45     RPointerArray<MVPbkStoreContact>& aRetrievedContacts,
       
    46     MVPbkOperationObserver& aObserver,
       
    47     MVPbkOperationErrorObserver& aErrorObserver )
       
    48     {
       
    49     iCancelled = EFalse;
       
    50     iObserver = & aObserver;
       
    51     iErrorObserver = &aErrorObserver;
       
    52     iContacts = &aRetrievedContacts; // owned externally
       
    53     delete iContactLinks;
       
    54     iContactLinks = NULL;
       
    55     iContactLinks = CloneLinkArrayL( aContactLinks );
       
    56     delete iOperation;
       
    57     iOperation = NULL;
       
    58     // Retrieve the first contact.
       
    59     // The next contacts are retrieved when the previous one completes.     
       
    60     if ( iContactLinks->Count() > 0 )
       
    61         {
       
    62         iOperation = iContactManager.RetrieveContactL( iContactLinks->At( 0 ), *this );
       
    63         iContactLinks->Delete( 0 );
       
    64         // Now waiting for callback.
       
    65         }
       
    66     }
       
    67 
       
    68 void CVPbkContactEasyManager::Cancel()
       
    69     {
       
    70     iCancelled = ETrue;
       
    71     delete iOperation;
       
    72     iOperation = NULL;
       
    73     delete iContactLinks;
       
    74     iContactLinks = NULL;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------
       
    78 // These are from MVPbkSingleContactOperationObserver
       
    79 // They are related to the contact retrieving.
       
    80 
       
    81 void CVPbkContactEasyManager::VPbkSingleContactOperationComplete(
       
    82     MVPbkContactOperationBase& /*aOperation*/,
       
    83     MVPbkStoreContact* aContact )
       
    84     {
       
    85     TRAPD( error, DoSingleContactOperationCompleteL( aContact ) );
       
    86     if ( error != KErrNone )
       
    87         {
       
    88         delete iOperation;
       
    89         iOperation = NULL;
       
    90         iErrorObserver->VPbkOperationFailed( this, error );
       
    91         }
       
    92     }
       
    93 
       
    94 void CVPbkContactEasyManager::VPbkSingleContactOperationFailed(
       
    95     MVPbkContactOperationBase& /*aOperation*/, 
       
    96     TInt aError )
       
    97     {
       
    98     iErrorObserver->VPbkOperationFailed( this, aError );
       
    99     delete iOperation;
       
   100     iOperation = NULL;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------
       
   104 // These are from from MVPbkContactObserver
       
   105 // They are related to the contact locking.
       
   106 
       
   107 void CVPbkContactEasyManager::ContactOperationCompleted(
       
   108     MVPbkContactObserver::TContactOpResult /*aResult*/ )
       
   109     {
       
   110     // Locking of a contact has completed.
       
   111     TRAPD( error, DoContactOperationCompletedL() );
       
   112     if ( error != KErrNone )
       
   113         {
       
   114         iErrorObserver->VPbkOperationFailed( this, error );
       
   115         }
       
   116     }
       
   117 
       
   118 void CVPbkContactEasyManager::ContactOperationFailed(
       
   119     TContactOp /*aOpCode*/, TInt aErrorCode, TBool /*aErrorNotified*/)
       
   120     {
       
   121     if ( !iCancelled )
       
   122         {
       
   123         iErrorObserver->VPbkOperationFailed( this, aErrorCode );
       
   124         // No more processing after this.
       
   125         }
       
   126     }
       
   127 
       
   128 // ------------------------------------------------------------------
       
   129 
       
   130 
       
   131 /// Implementation which is called from VPbkSingleContactOperationComplete().
       
   132 void CVPbkContactEasyManager::DoSingleContactOperationCompleteL(
       
   133     MVPbkStoreContact* aContact )
       
   134     {
       
   135     // Retrieving of a contact has completed.
       
   136     
       
   137     iContacts->AppendL( aContact ); // Ownership is given to array.
       
   138     delete iOperation;
       
   139     iOperation = NULL;
       
   140 
       
   141     if ( iContactLinks->Count() > 0 )
       
   142         {
       
   143         // Start to retrieve contacts one by one. The rest of the contact
       
   144         // retrievals are started from the callback.
       
   145 
       
   146         iOperation = iContactManager.RetrieveContactL(
       
   147             iContactLinks->At( 0 ), *this );
       
   148         iContactLinks->Delete( 0 );
       
   149         // Now waiting for callback.
       
   150         }
       
   151     else
       
   152         {
       
   153         // No more contacts to retrieve.
       
   154         // Now lock the retrieved contacts one by one.
       
   155         iNextContactToLock = 0;
       
   156         (*iContacts)[ iNextContactToLock ]->LockL( *this );        
       
   157         }
       
   158     }
       
   159 
       
   160 /// Implementation which is called from ContactOperationCompleted().
       
   161 void CVPbkContactEasyManager::DoContactOperationCompletedL()
       
   162     {
       
   163     // Locking of a contact has completed.
       
   164     if ( !iCancelled )
       
   165         {
       
   166         ++iNextContactToLock;
       
   167         if ( iNextContactToLock < iContacts->Count() )
       
   168             {
       
   169             // Still some contacts that need to be locked.
       
   170             (*iContacts)[ iNextContactToLock ]->LockL( *this );
       
   171             }
       
   172         else
       
   173             {
       
   174             // All contacts have been retrieved & locked.
       
   175             delete iContactLinks;
       
   176             iContactLinks = NULL;
       
   177             iObserver->VPbkOperationCompleted( this );
       
   178             }
       
   179         }
       
   180     }
       
   181 
       
   182 CVPbkContactLinkArray* CVPbkContactEasyManager::CloneLinkArrayL(
       
   183     const MVPbkContactLinkArray& aArray )
       
   184     {
       
   185     CVPbkContactLinkArray* newArray = CVPbkContactLinkArray::NewLC();
       
   186     const TInt count = aArray.Count();
       
   187     for ( TInt n = 0; n < count; ++n )
       
   188         {
       
   189         MVPbkContactLink* link = aArray.At(n).CloneLC();
       
   190         newArray->AppendL( link );
       
   191         CleanupStack::Pop(); // link
       
   192         }
       
   193     CleanupStack::Pop( newArray );
       
   194     return newArray;
       
   195     }
       
   196 
       
   197 CVPbkContactEasyManager::CVPbkContactEasyManager(
       
   198     CVPbkContactManager& aContactManager )
       
   199     : iContactManager( aContactManager )
       
   200     {
       
   201     }
       
   202 
       
   203 // end of file