phonebookui/Phonebook2/CommandsExtension/src/CPbk2ImageCmdBase.cpp
changeset 0 e686773b3f54
child 68 9da50d567e3c
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: 
       
    15 *           Base class for image commands set, view and remove.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbk2ImageCmdBase.h"
       
    22 #include "CPbk2FieldFocusHelper.h"
       
    23 
       
    24 // Phonebook 2
       
    25 #include <CPbk2ImageManager.h>
       
    26 #include <CPbk2ContactRelocator.h>
       
    27 #include <MPbk2CommandObserver.h>
       
    28 #include <RPbk2LocalizedResourceFile.h>
       
    29 #include <Pbk2DataCaging.hrh>
       
    30 #include <CPbk2FieldPropertyArray.h>
       
    31 #include <CPbk2ApplicationServices.h>
       
    32 #include <MPbk2AppUi.h>
       
    33 
       
    34 // Virtual Phonebook
       
    35 #include <MVPbkStoreContact.h>
       
    36 #include <MPbk2ContactUiControl.h>
       
    37 #include <MVPbkContactLink.h>
       
    38 #include <MVPbkFieldType.h>
       
    39 #include <CVPbkContactManager.h>
       
    40 #include <MVPbkContactOperationBase.h>
       
    41 #include <VPbkEng.rsg>
       
    42 
       
    43 // System headers
       
    44 #include <coemain.h>
       
    45 
       
    46 // Debugging headers
       
    47 #include <Pbk2Debug.h>
       
    48 
       
    49 
       
    50 /// Unnamed namespace for local definitons
       
    51 namespace {
       
    52 
       
    53 #ifdef _DEBUG
       
    54     enum TPanicCode
       
    55         {
       
    56         EPanicInvalidOpCode,
       
    57         EPanicLogic_RelocateContactL
       
    58         };
       
    59 
       
    60     void Panic(TInt aReason)
       
    61         {
       
    62         _LIT(KPanicText, "CPbk2ImageCmdBase");
       
    63         User::Panic(KPanicText, aReason);
       
    64         }
       
    65 #endif  // _DEBUG
       
    66 
       
    67 } // namespace
       
    68 
       
    69 
       
    70 CPbk2ImageCmdBase::CPbk2ImageCmdBase
       
    71         (MPbk2ContactUiControl& aUiControl) :
       
    72     iUiControl(&aUiControl)
       
    73     {
       
    74     }
       
    75 
       
    76 CPbk2ImageCmdBase::~CPbk2ImageCmdBase()
       
    77     {
       
    78     if( iUiControl )
       
    79         {
       
    80         iUiControl->RegisterCommand( NULL );
       
    81         }
       
    82 
       
    83     delete iFieldFocusHelper;
       
    84     delete iImageManager;
       
    85     delete iContactLink;
       
    86     delete iRetrieveOperation;
       
    87     delete iStoreContact;
       
    88     delete iContactRelocator;
       
    89     }
       
    90 
       
    91 void CPbk2ImageCmdBase::BaseConstructL()
       
    92     {
       
    93     CPbk2ApplicationServices* appServices = CPbk2ApplicationServices::InstanceLC();
       
    94 
       
    95     iContactManager =
       
    96         &(appServices->ContactManager());
       
    97 
       
    98     iContactLink = iUiControl->FocusedContactL()->CreateLinkLC();
       
    99     CleanupStack::Pop(); // iContactLink
       
   100 
       
   101     iImageManager = CPbk2ImageManager::NewL(*iContactManager);
       
   102     iFieldFocusHelper = CPbk2FieldFocusHelper::NewL( *iUiControl, 
       
   103         appServices->FieldProperties() );
       
   104 
       
   105     CleanupStack::PopAndDestroy(); // appServices
       
   106 
       
   107     iUiControl->RegisterCommand( this );        
       
   108     }
       
   109 
       
   110 void CPbk2ImageCmdBase::ProcessDismissed(TInt aError)
       
   111     {
       
   112     if ( aError != KErrNone && 
       
   113          aError != KLeaveExit )
       
   114         {
       
   115         // Report the failure to the user
       
   116         CCoeEnv::Static()->HandleError( aError );
       
   117         }
       
   118 
       
   119     if (iUiControl)
       
   120         {
       
   121         // Not matter if saved field can not focused
       
   122         TRAP_IGNORE( iFieldFocusHelper->RestoreSavedFieldL() );
       
   123         iUiControl->UpdateAfterCommandExecution();
       
   124         }
       
   125 
       
   126     if (iCommandObserver)
       
   127         {
       
   128         // Notify command owner that the command has finished
       
   129         iCommandObserver->CommandFinished(*this);
       
   130         }
       
   131     }
       
   132 
       
   133 void CPbk2ImageCmdBase::ExecuteLD()
       
   134     {
       
   135     CleanupStack::PushL(this);
       
   136     ReadFieldTypesL();
       
   137     iRetrieveOperation = iContactManager->RetrieveContactL(
       
   138         *iContactLink, *this);
       
   139     CleanupStack::Pop(this);
       
   140     }
       
   141 
       
   142 void CPbk2ImageCmdBase::AddObserver( MPbk2CommandObserver& aObserver )
       
   143     {
       
   144     iCommandObserver = &aObserver;
       
   145     }
       
   146 
       
   147 void CPbk2ImageCmdBase::ResetUiControl(
       
   148         MPbk2ContactUiControl& aUiControl)
       
   149     {
       
   150     if (iUiControl == &aUiControl)
       
   151         {
       
   152         iUiControl = NULL;
       
   153         }
       
   154     }
       
   155 
       
   156 void CPbk2ImageCmdBase::ContactOperationCompleted
       
   157         ( TContactOpResult aResult )
       
   158     {
       
   159     __ASSERT_DEBUG((aResult.iOpCode == EContactCommit ||
       
   160             aResult.iOpCode == EContactLock),
       
   161             Panic(EPanicInvalidOpCode));
       
   162 
       
   163     if (aResult.iOpCode == EContactLock)
       
   164         {
       
   165         // Not matter, if focusing not success
       
   166         TRAP_IGNORE( iFieldFocusHelper->SaveInitiallyFocusedFieldL( *iStoreContact ) );
       
   167         TBool result (EFalse);
       
   168         TRAPD(err, result = ExecuteCommandL());
       
   169         if (err != KErrNone || !result)
       
   170             {
       
   171             ProcessDismissed(err);
       
   172             }
       
   173         }
       
   174     else
       
   175         {
       
   176         ProcessDismissed(KErrNone);
       
   177         }
       
   178     }
       
   179 
       
   180 void CPbk2ImageCmdBase::ContactOperationFailed
       
   181     ( TContactOp PBK2_DEBUG_ONLY( aOpCode ), TInt aErrorCode,
       
   182       TBool /*aErrorNotified*/ )
       
   183     {
       
   184     __ASSERT_DEBUG( ( aOpCode == EContactCommit || aOpCode == EContactLock ),
       
   185         Panic( EPanicInvalidOpCode ) );
       
   186 
       
   187     ProcessDismissed( aErrorCode );
       
   188     }
       
   189 
       
   190 /**
       
   191  * Called when retrieve operation completes.
       
   192  */
       
   193 void CPbk2ImageCmdBase::VPbkSingleContactOperationComplete(
       
   194     MVPbkContactOperationBase& aOperation, MVPbkStoreContact* aContact)
       
   195     {
       
   196     if (&aOperation == iRetrieveOperation)
       
   197         {
       
   198         delete iRetrieveOperation;
       
   199         iRetrieveOperation = NULL;
       
   200 
       
   201         if (iStoreContact)
       
   202             {
       
   203             delete iStoreContact;
       
   204             iStoreContact = NULL;
       
   205             }
       
   206         // We now have a store contact
       
   207         iStoreContact = aContact;
       
   208         }
       
   209 
       
   210     TBool contactNeedsRelocation = EFalse;
       
   211     TRAPD( err, contactNeedsRelocation = RelocateContactL());
       
   212     if (err == KErrNone && !contactNeedsRelocation)
       
   213         {
       
   214         TRAP(err, iStoreContact->LockL(*this));
       
   215         }
       
   216     if (err != KErrNone)
       
   217         {
       
   218         ProcessDismissed(err);
       
   219         }
       
   220     }
       
   221 
       
   222 /**
       
   223  * Called when retrieve operation fails.
       
   224  */
       
   225 void CPbk2ImageCmdBase::VPbkSingleContactOperationFailed(
       
   226         MVPbkContactOperationBase& aOperation, TInt aError)
       
   227     {
       
   228     if (&aOperation == iRetrieveOperation)
       
   229         {
       
   230         delete iRetrieveOperation;
       
   231         iRetrieveOperation = NULL;
       
   232 
       
   233         // We cannot get the contact, so we have to
       
   234         // fail. We cannot continue, since this operation
       
   235         // was executed only in case of one contact.
       
   236         ProcessDismissed(aError);
       
   237         }
       
   238     }
       
   239 
       
   240 inline TBool CPbk2ImageCmdBase::RelocateContactL()
       
   241     {
       
   242     __ASSERT_DEBUG( iStoreContact, Panic( EPanicLogic_RelocateContactL ) );
       
   243     TBool ret = EFalse;
       
   244 
       
   245     delete iContactRelocator;
       
   246     iContactRelocator = NULL;
       
   247     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   248 
       
   249     if ( !iContactRelocator->IsPhoneMemoryContact( *iStoreContact ) &&
       
   250         iContactRelocator->IsPhoneMemoryInConfigurationL() )
       
   251         {
       
   252         // Asynchronously relocate contact
       
   253         iContactRelocator->RelocateContactL( iStoreContact, *this,
       
   254             Pbk2ContactRelocator::EPbk2DisplayStoreDoesNotSupportQuery,
       
   255             CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   256         iStoreContact = NULL;    // Ownership was taken
       
   257         ret = ETrue;        // Indicate that contact needs relocating
       
   258         }
       
   259 
       
   260     return ret;
       
   261     }
       
   262 
       
   263 void CPbk2ImageCmdBase::ContactRelocatedL
       
   264         (MVPbkStoreContact* aRelocatedContact)
       
   265     {
       
   266     iStoreContact = aRelocatedContact; // take ownership
       
   267     }
       
   268 
       
   269 void CPbk2ImageCmdBase::ContactRelocationFailed
       
   270         (TInt aReason, MVPbkStoreContact* aContact)
       
   271     {
       
   272     // Take the contact back
       
   273     iStoreContact = aContact;
       
   274     if ( aReason == KErrCancel )
       
   275         {
       
   276         // No error note is to be shown to the user when she
       
   277         // manually cancels the relocation process, therefore
       
   278         // the error code must be converted
       
   279         aReason = KErrNone;
       
   280         }
       
   281     ProcessDismissed(aReason);
       
   282     }
       
   283 
       
   284 void CPbk2ImageCmdBase::ContactsRelocationFailed
       
   285         ( TInt /*aReason*/, CVPbkContactLinkArray* /*aContacts*/ )
       
   286     {
       
   287     // Do nothing
       
   288     }
       
   289 
       
   290 void CPbk2ImageCmdBase::RelocationProcessComplete()
       
   291     {
       
   292     // now lock the contact to start the actual process
       
   293     TRAPD( res, iStoreContact->LockL( *this ) );
       
   294     if ( res != KErrNone )
       
   295         {
       
   296         ProcessDismissed( res );
       
   297         }
       
   298     }
       
   299 
       
   300 void CPbk2ImageCmdBase::ReadFieldTypesL()
       
   301     {
       
   302     iThumbnailFieldType =
       
   303         iContactManager->FieldTypes().Find(R_VPBK_FIELD_TYPE_THUMBNAILPIC);
       
   304     iImageFieldType =
       
   305         iContactManager->FieldTypes().Find(R_VPBK_FIELD_TYPE_CALLEROBJIMG);
       
   306         
       
   307     if (!iThumbnailFieldType || !iImageFieldType)
       
   308         {
       
   309         User::Leave(KErrNotFound);
       
   310         }
       
   311     }
       
   312     
       
   313 TInt CPbk2ImageCmdBase::FindFieldIndex(const MVPbkFieldType& aFieldType)
       
   314 	{
       
   315 	if( iStoreContact )
       
   316 	    {
       
   317     	const MVPbkBaseContactFieldCollection& fields = iStoreContact->Fields();	
       
   318     	const TInt fieldCount = fields.FieldCount();
       
   319         
       
   320         for (TInt i = 0; i < fieldCount; ++i)
       
   321             {
       
   322             const MVPbkFieldType* fieldType = 
       
   323                 fields.FieldAt(i).BestMatchingFieldType();
       
   324             if (fieldType && fieldType->IsSame(aFieldType))
       
   325                 {
       
   326                 return i;
       
   327                 }
       
   328             }
       
   329 	    }
       
   330     return KErrNotFound;
       
   331 	}    
       
   332 
       
   333 //  End of File