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