phonebookengines/VirtualPhonebook/VPbkCntModel/src/CAsyncContactOperation.cpp
changeset 0 e686773b3f54
child 15 e8e3147d53eb
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:  The virtual phonebook asynchronous contact operation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include "CAsyncContactOperation.h"
       
    22 #include <cntdb.h>
       
    23 #include <cntitem.h>
       
    24 #include "CContactStore.h"
       
    25 #include "CContact.h"
       
    26 #include <CVPbkDiskSpaceCheck.h>
       
    27 #include <MVPbkContactStoreProperties.h>
       
    28 
       
    29 
       
    30 namespace VPbkCntModel {
       
    31 
       
    32 const TInt KDiskSpaceNeedUnknown = 0;
       
    33 
       
    34 inline CAsyncContactOperation::CAsyncContactOperation
       
    35         ( CContactStore& aContactStore ) :
       
    36     CAsyncOneShot( CActive::EPriorityIdle ),
       
    37     iContactStore( aContactStore ),
       
    38     iOpCode( MVPbkContactObserver::EContactOperationUnknown )
       
    39     {
       
    40     }
       
    41 
       
    42 CAsyncContactOperation* CAsyncContactOperation::NewL
       
    43 		( CContactStore& aContactStore )
       
    44 	{
       
    45 	CAsyncContactOperation* self = 
       
    46 	    new(ELeave) CAsyncContactOperation( aContactStore );
       
    47 	CleanupStack::PushL( self );
       
    48 	self->ConstructL();
       
    49 	CleanupStack::Pop( self );
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 CAsyncContactOperation::~CAsyncContactOperation()
       
    54     {
       
    55     CActive::Cancel();
       
    56     delete iDiskSpaceChecker;
       
    57     iFs.Close();
       
    58     }
       
    59     
       
    60 void CAsyncContactOperation::ConstructL()
       
    61     {
       
    62     User::LeaveIfError( iFs.Connect() );
       
    63     // Get contact store location drive for disk space check
       
    64     const TPtrC ptr( iContactStore.StoreProperties().Uri().Component
       
    65         ( TVPbkContactStoreUriPtr::EContactStoreUriStoreDrive ) );
       
    66     TInt drive( EDriveA ); // c is usually the default location
       
    67     User::LeaveIfError( iFs.CharToDrive
       
    68         ( ptr[0], drive) );
       
    69     iDiskSpaceChecker = VPbkEngUtils::CVPbkDiskSpaceCheck::NewL( iFs, drive );
       
    70     }    
       
    71 
       
    72 void CAsyncContactOperation::PrepareL
       
    73         ( MVPbkContactObserver::TContactOp aOpCode, 
       
    74         TContactItemId aContactId,
       
    75         MVPbkContactObserver& aObserver )
       
    76     {
       
    77     if ( IsActive() )
       
    78         {
       
    79         User::Leave( KErrInUse );
       
    80         }
       
    81     iTargetContactId = aContactId;
       
    82     iOpCode = aOpCode;
       
    83     iObserver = &aObserver;
       
    84     iClientContact = NULL;
       
    85     }
       
    86 
       
    87 void CAsyncContactOperation::PrepareL
       
    88         ( MVPbkContactObserver::TContactOp aOpCode, 
       
    89         const CContact& aContact,
       
    90         MVPbkContactObserver& aObserver )
       
    91     {
       
    92     if ( IsActive() )
       
    93         {
       
    94         User::Leave( KErrInUse );
       
    95         }
       
    96     iOpCode = aOpCode;
       
    97     iObserver = &aObserver;
       
    98     iClientContact = &aContact;
       
    99     }
       
   100 
       
   101 void CAsyncContactOperation::Cancel( CContactItem* aContact )
       
   102 	{
       
   103 	if ( iClientContact &&
       
   104 			aContact == iClientContact->NativeContact() )
       
   105 		{
       
   106 		// Do not call CActive::Cancel(), it is not desired since not all
       
   107 		// operations are affected. The client contact is just not
       
   108 		// valid anymore, so let's reset it.
       
   109 		iClientContact = NULL;
       
   110 		}
       
   111     }
       
   112 
       
   113 void CAsyncContactOperation::DoCancel()
       
   114     {
       
   115     iOpCode = MVPbkContactObserver::EContactOperationUnknown;
       
   116     }
       
   117 
       
   118 void CAsyncContactOperation::RunL()
       
   119     {
       
   120     MVPbkContactObserver::TContactOpResult result;
       
   121     result.iOpCode = iOpCode;
       
   122     result.iStoreContact = NULL;
       
   123     result.iExtension = NULL;
       
   124 
       
   125     switch( iOpCode )
       
   126         {
       
   127         case MVPbkContactObserver::EContactRead:
       
   128             {
       
   129             CContactItem* ci = 
       
   130                 iContactStore.NativeDatabase().ReadContactLC( iTargetContactId );
       
   131             result.iStoreContact = CContact::NewL( iContactStore, ci );
       
   132             CleanupStack::Pop( ci );
       
   133             break;
       
   134             }
       
   135 
       
   136         case MVPbkContactObserver::EContactReadAndLock:
       
   137             {
       
   138             CContactItem* ci = 
       
   139                 iContactStore.NativeDatabase().OpenContactLX( iTargetContactId );
       
   140             CleanupStack::PushL( ci );
       
   141             CContact* contact = CContact::NewL( iContactStore, ci );
       
   142             result.iStoreContact = contact;
       
   143             contact->SetModified();
       
   144             CleanupStack::Pop( 2 );  // ci, lock
       
   145             break;
       
   146             }
       
   147 
       
   148         case MVPbkContactObserver::EContactDelete:
       
   149             {
       
   150             iContactStore.NativeDatabase().DeleteContactL( iTargetContactId );
       
   151             break;
       
   152             }
       
   153 
       
   154         case MVPbkContactObserver::EContactLock:
       
   155             {
       
   156             if( iClientContact )
       
   157             	{
       
   158             	CContactItem* ci = iContactStore.NativeDatabase().OpenContactLX(
       
   159             			iClientContact->NativeContact()->Id() );
       
   160 	            CleanupStack::PushL( ci );
       
   161 	            const_cast<CContact*>( iClientContact )->SetContact( ci );
       
   162 	            const_cast<CContact*>( iClientContact )->SetModified();
       
   163 	            CleanupStack::Pop( 2 );  // ci, lock
       
   164             	}
       
   165             break;
       
   166             }
       
   167             
       
   168         case MVPbkContactObserver::EContactCommit:
       
   169             {
       
   170             if( iClientContact )
       
   171             	{	
       
   172             	iDiskSpaceChecker->DiskSpaceCheckL( KDiskSpaceNeedUnknown );
       
   173 	            if (iClientContact->IsNewContact())
       
   174 	                {
       
   175 	                iContactStore.NativeDatabase().AddNewContactL( 
       
   176 	                                    *iClientContact->NativeContact() );
       
   177 	                }
       
   178 	            else
       
   179 	                {
       
   180 	                iContactStore.NativeDatabase().CommitContactL(
       
   181 	                                    *iClientContact->NativeContact() );
       
   182 	                }
       
   183             	}	
       
   184             break;
       
   185             }
       
   186         case MVPbkContactObserver::EContactSetOwn:
       
   187         	{
       
   188         	iContactStore.NativeDatabase().SetOwnCardL( *iClientContact->NativeContact() );
       
   189         	break;
       
   190         	}
       
   191         default:
       
   192             {
       
   193             // Operation was canceled
       
   194             return;
       
   195             }
       
   196         }
       
   197 
       
   198     iObserver->ContactOperationCompleted( result );
       
   199     }
       
   200 
       
   201 TInt CAsyncContactOperation::RunError( TInt aError )
       
   202     {
       
   203     iObserver->ContactOperationFailed( iOpCode, aError, EFalse );
       
   204     return KErrNone;
       
   205     }
       
   206 
       
   207 } // namespace VPbkCntModel
       
   208 
       
   209 //End of file