phonebookengines/VirtualPhonebook/VPbkVCardEng/inc/CVPbkImportToContactsMergeOperation.h
author Pat Downey <patd@symbian.org>
Wed, 01 Sep 2010 12:29:52 +0100
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
permissions -rw-r--r--
Revert incorrect RCL_3 drop: Revision: 201033 Kit: 201035

/*
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  An operation for Updating a contact in the store
*
*/


#ifndef CVPbkImportToContactsMergeOperation_H
#define CVPbkImportToContactsMergeOperation_H

// INCLUDE FILES
#include <e32base.h>
#include <MVPbkContactOperationBase.h>
#include <MVPbkSingleContactOperationObserver.h>
#include <MVPbkContactObserver.h>
#include <MVPbkContactOperation.h>
#include <MVPbkContactAttributeManager.h>
#include <VPbkFieldType.hrh>
#include "MVPbkImportOperationImpl.h"
#include "CVPbkGroupCardHandler.h"
#include "MVPbkContactCopyObserver.h"


// FORWARD DECLARATIONS
class MVPbkStoreContact;
class MVPbkContactStore;
class MVPbkSingleContactOperationObserver;
class MVPbkContactLink;
class MVPbkStoreContactField;
class MVPbkFieldType;
class MVPbkSingleContactOperationObserver;
class MVPbkContactCopyObserver;
class CVPbkVCardEng;
class CVPbkVCardImporter;
class CVPbkContactManager;
class MVPbkSetAttributeObserver;
/**
 *  An operation for Updating a contact in the store
 */
NONSHARABLE_CLASS(CVPbkImportToContactsMergeOperation) 
    :   public CActive,
        public MVPbkContactOperation,
        public MVPbkImportOperationObserver,
        public MVPbkSingleContactOperationObserver,
        public MVPbkContactObserver,
        public MVPbkSetAttributeObserver
    {
public:
     static CVPbkImportToContactsMergeOperation *NewL( 
             TVPbkImportCardType aType,
             CVPbkVCardData& aData, 
             const MVPbkContactLink& aReplaceContact, 
             MVPbkContactStore& aTargetStore, 
             RReadStream& aSourceStream, 
             MVPbkSingleContactOperationObserver& aObserver );

    /**
    * Destructor.
    */
    ~CVPbkImportToContactsMergeOperation();
        
protected: // CActive
    /**
    * From CActive. Called when asynchronous operation completes.
    */
    void RunL();
    /**
    * From CActive. Called when RunL leaves.
    */    

    TInt RunError( TInt aError );
    /**
    * From CActive. Cancels operation.
    */

    void DoCancel();        

private:

    CVPbkImportToContactsMergeOperation( 
            const MVPbkContactLink& aReplaceContact,
            MVPbkSingleContactOperationObserver& aObserver,
            MVPbkContactStore& aTargetStore,
            CVPbkVCardData & aData
            );
    /**
    * Second phase constructor.
    */
    void ConstructL( TVPbkImportCardType aType, RReadStream& aSourceStream);

public:
    // from base class MVPbkImportOperationObserver
      /**
        * From MVPbkImportOperationObserver  
        * Called when a contact import operation has Completed.
        */
     void ContactsImported();
      /**
        * From MVPbkImportOperationObserver  
        * Called when a contact import operation has failed.
        */
     void ContactImportingFailed( TInt aError );
      /**
        * From MVPbkImportOperationObserver  
        * Called when a contact import operation has Completed Fully.
        */
     void ContactsImportingCompleted();
        
public:
    // from base class MVPbkContactOperation
     /**
        * From MVPbkContactOperation  
        * Called when a contact operation has to be started
        */
     void StartL();
      /**
        * From MVPbkContactOperation  
        * Called when a contact operation has to be cancled
        */
     void Cancel();
     
public: // from MVPbkSingleContactOperationObserver

    /**
        * From MVPbkSingleContactOperationObserver  
        * Called when the contact operation is complete
        * Client takes Ownership in deleting returned aContact.
        */
    void VPbkSingleContactOperationComplete(
                    MVPbkContactOperationBase& aOperation,
                    MVPbkStoreContact* aContact );
    /**
        * From MVPbkSingleContactOperationObserver  
        * Called when the contact operation fails
        */                
    void VPbkSingleContactOperationFailed(
                    MVPbkContactOperationBase& aOperation, 
                    TInt aError );
public:

    // from MVPbkContactObserver
    /**
        * From MVPbkContactObserver  
        * Called when a contact operation has succesfully completed.
        * Client takes Ownership in deleting returned aResult.
        */
    void ContactOperationCompleted(TContactOpResult aResult);
    /**
        * From MVPbkContactObserver  
        * Called when a contact operation has failed.
        */
    void ContactOperationFailed
                (TContactOp aOpCode, TInt aErrorCode, TBool aErrorNotified);
    

private:

    // from MVPbkSetAttributeObserver
    /**
        * From MVPbkSetAttributeObserver  
        * Called when a set attribute operation has succesfully completed.
        * Client takes Ownership in deleting returned aResult.
        */
    void AttributeOperationComplete( MVPbkContactOperationBase& aOperation );
    /**
        * From MVPbkSetAttributeObserver  
        * Called when a set attribute operation has failed.
        */
    void AttributeOperationFailed(
            MVPbkContactOperationBase& aOperation,
            TInt aError );
    
private: // types
    enum TState
        {
        EImport,
        ERetrieve,
        ELock,
        EUpdateContact,
        EReplaceFields,
        ESetAttributes,
        ECommit,
        EReRetrieve, //Reload contact to have valid lastmodified datetime
        EComplete
        };

private:    
    void NextState( TState aNextState );
    /**
    * Retrive the contact to be updated
    */
    void RetrieveContactL();
    /**
    * Lock the contact for editing
    */
    void LockContactL();
    /**
    * Commit the updated contact
    */
    void CommitContactL();
    /**
    * Update imported contacts
    */
    void UpdateContactL();
    /**
    * Retrieve the contact after updation
    */
    void ReRetrieveContactL();
    
    void HandleError(TInt aError);
    void HandleContactReplacing(MVPbkStoreContact* aContact);
    /**
    * Replace the fields of target contact
    */
    void ReplaceFieldL( const MVPbkStoreContact& aSrc, MVPbkStoreContact& aTarget );
    /**
    * Copy the fields of source contact to target contact
    */
    void CopyFieldL( const MVPbkStoreContactField& aSourceField,
            const MVPbkFieldType& aType,
            MVPbkStoreContact& aTargetContact );
    /**
    * Gets the attributes from aField
    */
    void GetDefaultAttributsL( const MVPbkStoreContactField& aField );
    /**
    * Return the attribute which is to be set
    */
    TVPbkDefaultType NextAttribute();
    /**
    * Set the attribute to the target contect field
    */
    void SetNextL();

private: // data
    /// Own: state
    TState iState;
    /// Own: the imported contacts
    RPointerArray<MVPbkStoreContact> iImportedContacts;
    /// Own: the implementation of the import operation
    MVPbkImportOperationImpl* iOperationImpl;
    /// Ref: the target store of the operation
    MVPbkContactStore& iTargetStore;
    /// Ref: Vcard Data
    CVPbkVCardData& iData;
    /// Ref: contact to replace
    const MVPbkContactLink& iReplaceContact;
    /// Own: current contact
    MVPbkStoreContact* iContact;
    /// Ref: client that waits importing
    MVPbkSingleContactOperationObserver& iObserver;
    /// Own: the operation for import and retrieve
    MVPbkContactOperationBase* iVPbkOperation;
    /// Own: Group Card Handler
    CVPbkGroupCardHandler* iGroupcardHandler;
    /// Own: the operation for set attribute
    MVPbkContactOperationBase* iSetAttributeOperation;
    /// Own: the array of one field's attributes
    CArrayFixFlat<TVPbkDefaultType>* iDefaultAttributes;
    /// The index of the current operated field 
    TInt iCurrentFieldIndex;
    /// The field count of the source contact
    TInt iFieldCount;
    };

#endif