phoneengine/PhoneCntFinder/ContactService/src/CPhCntContactStores.cpp
changeset 0 5f000ab63145
equal deleted inserted replaced
-1:000000000000 0:5f000ab63145
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Contact stores.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPhCntContactStores.h"
       
    20 #include "MPhCntContactManager.h"
       
    21 #include "MPhCntContactFetchObserver.h"
       
    22 #include "CPhCntContact.h"
       
    23 #include "cphcntcontactfieldsresolver.h"
       
    24 #include <MVPbkContactOperationBase.h>
       
    25 #include <CVPbkContactLinkArray.h>
       
    26 #include <MVPbkStoreContact.h>
       
    27 #include <MVPbkContactLink.h>
       
    28 
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Static constructor
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CPhCntContactStores* CPhCntContactStores::NewL( 
       
    35     MPhCntContactManager& aContactManager ) 
       
    36     {
       
    37     CPhCntContactStores* self = new( ELeave )CPhCntContactStores( aContactManager );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Destructor
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CPhCntContactStores::~CPhCntContactStores() 
       
    49     {
       
    50     CancelRequest();
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // From MPhCntContactStores
       
    55 // Determines if request is active.
       
    56 // ---------------------------------------------------------------------------
       
    57 //     
       
    58 TBool CPhCntContactStores::IsRequestActive() const
       
    59     {
       
    60     TBool isActive( EFalse );
       
    61     if( iRetrieveContactOperation )
       
    62         {
       
    63         isActive = ETrue;
       
    64         }
       
    65     return isActive;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // From MPhCntContactStores
       
    70 // Cancels the request.
       
    71 // ---------------------------------------------------------------------------
       
    72 //       
       
    73 void CPhCntContactStores::CancelRequest()
       
    74     {
       
    75     delete iRetrieveContactOperation;
       
    76     iRetrieveContactOperation = NULL;
       
    77     }
       
    78  
       
    79 // ---------------------------------------------------------------------------
       
    80 // From MPhCntContactStores
       
    81 // Fetches contact from contact store.
       
    82 // ---------------------------------------------------------------------------
       
    83 //   
       
    84 void CPhCntContactStores::FetchContactL( 
       
    85     const MVPbkContactLink& aLink, 
       
    86     MPhCntContactFetchObserver& aObserver ) 
       
    87     {
       
    88     // If we already have somebody fetching contact...
       
    89     if ( iContactFetchObserver )
       
    90         {
       
    91         User::Leave(KErrInUse);
       
    92         }
       
    93             
       
    94     iLink = aLink.CloneLC();
       
    95     CleanupStack::Pop( 1 );
       
    96     iContactFetchObserver = &aObserver;
       
    97     iRetrieveContactOperation = 
       
    98         iContactManager.RetrieveContactL( *iLink, *this ); 
       
    99      
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // From MPhCntContactStores
       
   104 // Fetches first contact pointed by contact links.
       
   105 // ---------------------------------------------------------------------------
       
   106 //    
       
   107 void CPhCntContactStores::FetchFirstContactL( 
       
   108     const TDesC8& aContactLinks,
       
   109     MPhCntContactFetchObserver& aObserver ) 
       
   110     {
       
   111     if ( iContactFetchObserver )
       
   112         {
       
   113         User::Leave(KErrInUse);
       
   114         }
       
   115         
       
   116     CVPbkContactLinkArray* linkArray = 
       
   117         CreateContactLinkArrayL( aContactLinks );
       
   118     CleanupStack::PushL( linkArray );
       
   119     
       
   120     if( linkArray->Count() > 0 ) 
       
   121         {
       
   122         const MVPbkContactLink& link = linkArray->At( 0 );
       
   123         MVPbkContactLink* cloneLink = link.CloneLC();
       
   124         CleanupStack::Pop(); // cloneLink
       
   125         iLink = cloneLink;
       
   126         iContactFetchObserver = &aObserver;
       
   127         iRetrieveContactOperation = 
       
   128             iContactManager.RetrieveContactL( *iLink, *this ); 
       
   129         }
       
   130     else
       
   131         {
       
   132         aObserver.ContactReceived( NULL, KErrNotFound );
       
   133         }
       
   134     CleanupStack::PopAndDestroy( linkArray );
       
   135     }
       
   136     
       
   137 // ---------------------------------------------------------------------------
       
   138 // From MPhCntContactStores
       
   139 // Creates contact link array
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CVPbkContactLinkArray* CPhCntContactStores::CreateContactLinkArrayL( 
       
   143     const TDesC8& aLinks ) 
       
   144     {
       
   145     CVPbkContactLinkArray* linkArray = 
       
   146         CVPbkContactLinkArray::NewLC( 
       
   147             aLinks, iContactManager.ContactStoresL() );
       
   148     CleanupStack::Pop( linkArray );
       
   149     return linkArray;
       
   150     }
       
   151     
       
   152 // ---------------------------------------------------------------------------
       
   153 // From MVPbkSingeContactOperationObserver
       
   154 // Resolves the received contact and notifies the observer about 
       
   155 // contact reception.
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CPhCntContactStores::VPbkSingleContactOperationComplete(
       
   159     MVPbkContactOperationBase& /*aOperation*/,
       
   160     MVPbkStoreContact* aContact ) 
       
   161     {
       
   162     CancelRequest();
       
   163 
       
   164     CPhCntContact* contact = NULL;
       
   165     CPhCntContactFieldsResolver* resolver = NULL;
       
   166     TRAPD( err, 
       
   167         resolver = 
       
   168             CPhCntContactFieldsResolver::NewLC( 
       
   169                 iLink, aContact, iContactManager );
       
   170             
       
   171         contact =  
       
   172             CPhCntContact::NewL( resolver, iContactManager );
       
   173         CleanupStack::Pop( resolver );
       
   174     )
       
   175         
       
   176     iContactFetchObserver->ContactReceived( contact, err );
       
   177     iContactFetchObserver = NULL;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // From MVPbkSingeContactOperationObserver
       
   182 // Notify observer that contact fetch failed.
       
   183 // ---------------------------------------------------------------------------
       
   184 //   
       
   185 void CPhCntContactStores::VPbkSingleContactOperationFailed(
       
   186     MVPbkContactOperationBase& /*aOperation*/, 
       
   187     TInt aError ) 
       
   188     {
       
   189     CancelRequest();
       
   190     
       
   191     delete iLink;
       
   192     iLink = NULL;
       
   193     iContactFetchObserver->ContactReceived( NULL, aError );
       
   194     iContactFetchObserver = NULL;
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // Constructor
       
   199 // ---------------------------------------------------------------------------
       
   200 //    
       
   201 CPhCntContactStores::CPhCntContactStores( 
       
   202     MPhCntContactManager& aContactManager ) :
       
   203     iContactManager( aContactManager )
       
   204     {
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // Second phase constructor.
       
   209 // ---------------------------------------------------------------------------
       
   210 //    
       
   211 void CPhCntContactStores::ConstructL()
       
   212     {
       
   213     }