phonebookui/Phonebook2/Presentation/src/CPbk2ImageManagerImpl.cpp
changeset 0 e686773b3f54
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 *       Provides methods for CPbkImageManager implementation classes.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbk2ImageManagerImpl.h"
       
    22 
       
    23 // From Phonebook2
       
    24 #include "CPbk2EmbeddedImage.h"
       
    25 
       
    26 // From Virtual Phonebook
       
    27 #include <MVPbkBaseContact.h>
       
    28 #include <MVPbkStoreContact.h>
       
    29 #include <MVPbkBaseContactField.h>
       
    30 #include <MVPbkContactFieldData.h>
       
    31 #include <MVPbkFieldType.h>
       
    32 #include <CVPbkContactManager.h>
       
    33 
       
    34 // Unnamed namespace for local definitions
       
    35 namespace
       
    36 	{
       
    37 	/**
       
    38 	 * Finds field index from contact.
       
    39 	 *
       
    40 	 * @param aContactManager   Reference to contact manager.
       
    41 	 * @param aContact          Reference to contact,
       
    42 	 * @param aFieldType        Reference to field type.
       
    43 	 * @return Pointer to found field. NULL if field cannot be found.
       
    44 	 */
       
    45 	template <class VPbkContactField, class VPbkContact>
       
    46     	VPbkContactField* FindFieldFromContactL(
       
    47     	CVPbkContactManager& aContactManager,
       
    48     	VPbkContact& aContact, 
       
    49     	const MVPbkFieldType& aFieldType)
       
    50     		{
       
    51     		const MVPbkFieldTypeList& supportedFieldTypes = 
       
    52 				aContactManager.FieldTypes();			
       
    53 			VPbkContactField* field = NULL;
       
    54 			
       
    55 		    for ( TInt n = 0; n < aContact.Fields().FieldCount(); ++n )
       
    56 		        {
       
    57 		        field = &aContact.Fields().FieldAt( n );
       
    58 
       
    59 		        const TInt maxMatchPriority = 
       
    60 		            supportedFieldTypes.MaxMatchPriority();
       
    61 		        for (TInt matchPriority = 0; 
       
    62 		             matchPriority <= maxMatchPriority; 
       
    63 		             ++matchPriority)
       
    64 		            {
       
    65 		            const MVPbkFieldType* sourceFieldType = 
       
    66 		                field->MatchFieldType( matchPriority );
       
    67 		            if ( sourceFieldType && 
       
    68 		                 aFieldType.IsSame( *sourceFieldType ) )
       
    69 		                {	                
       
    70 		                return field;
       
    71 		                }
       
    72 		            }
       
    73 		        }
       
    74 		    		 
       
    75 			return NULL;
       
    76     		}
       
    77     		
       
    78 	/**
       
    79 	 * Finds field index from contact.
       
    80 	 *
       
    81 	 * @param aContactManager   Reference to contact manager.
       
    82 	 * @param aContact          Reference to contact,
       
    83 	 * @param aFieldType        Reference to field type.
       
    84 	 * @return Found field index. KErrNotFound if field cannot be found.
       
    85 	 */
       
    86    	TInt FindFieldIndex(
       
    87 		CVPbkContactManager& aContactManager,
       
    88 		const MVPbkBaseContact& aContact,
       
    89 		const MVPbkFieldType& aFieldType)
       
    90 		{
       
    91 		const MVPbkBaseContactFieldCollection& fields = aContact.Fields();	
       
    92 		const TInt fieldCount = fields.FieldCount();
       
    93 	    
       
    94 	    const TInt maxMatchPriority = 
       
    95 	    	aContactManager.FieldTypes().MaxMatchPriority();
       
    96 	    for (TInt matchPriority = 0; 
       
    97 	    	matchPriority <= maxMatchPriority; 
       
    98 	    	++matchPriority)
       
    99 	        {            
       
   100 	        for (TInt i = 0; i < fieldCount; ++i)
       
   101 	            {
       
   102 	            const MVPbkFieldType* fieldType = 
       
   103 	                    fields.FieldAt(i).MatchFieldType(matchPriority);
       
   104 	            if (fieldType && fieldType->IsSame(aFieldType))
       
   105 	                {
       
   106 	                return i;
       
   107 	                }
       
   108 	            }
       
   109 	        }    
       
   110 	    return KErrNotFound;
       
   111 		}		
       
   112 	} // unnamed namespace
       
   113 // ================= MEMBER FUNCTIONS =======================
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // Pbk2ImageOperationFactory::CreateReaderLC
       
   117 // --------------------------------------------------------------------------
       
   118 //
       
   119 MPbk2ImageReader* Pbk2ImageOperationFactory::CreateReaderLC
       
   120         (CVPbkContactManager& aContactManager,
       
   121         const MVPbkBaseContact& aContact,
       
   122         MPbk2ImageGetObserver& aObserver,
       
   123         const MVPbkFieldType& aFieldType)        
       
   124     {
       
   125     const MVPbkBaseContactField* field = 
       
   126     	FindFieldFromContactL<const MVPbkBaseContactField, const MVPbkBaseContact>(
       
   127     		aContactManager, aContact, aFieldType);
       
   128     
       
   129     MPbk2ImageReader* reader = NULL;
       
   130     if (field && SupportedStorageType(field))
       
   131         {
       
   132         reader = CPbk2EmbeddedImageReader::NewLC(*field,aObserver);
       
   133         }
       
   134     return reader;
       
   135     }
       
   136 
       
   137 // --------------------------------------------------------------------------
       
   138 // Pbk2ImageOperationFactory::CreateWriterLC
       
   139 // --------------------------------------------------------------------------
       
   140 //
       
   141 MPbk2ImageWriter* Pbk2ImageOperationFactory::CreateWriterLC
       
   142         (CVPbkContactManager& aContactManager,
       
   143         MVPbkStoreContact& aContact, 
       
   144         MPbk2ImageSetObserver& aObserver,
       
   145         const MVPbkFieldType& aFieldType)
       
   146     {
       
   147     MPbk2ImageWriter* writer = NULL;
       
   148 
       
   149 	// Find existing field from contact
       
   150     MVPbkStoreContactField* field = NULL;
       
   151     
       
   152     field =	FindFieldFromContactL<MVPbkStoreContactField, MVPbkStoreContact>(
       
   153     		aContactManager, aContact, aFieldType);
       
   154     	
       
   155     if (field && !SupportedStorageType(field))
       
   156         {
       
   157         // Existing field's storage type is not supported -> remove field
       
   158         TInt index(FindFieldIndex(
       
   159         	aContactManager, 
       
   160         	static_cast<const MVPbkBaseContact&>(aContact), 
       
   161         	aFieldType));
       
   162         User::LeaveIfError(index);
       
   163         aContact.RemoveField(index);
       
   164         field = NULL;
       
   165         }
       
   166 
       
   167     if (!field)
       
   168         {
       
   169         // No thumbnail field -> create one
       
   170         field = aContact.CreateFieldLC(aFieldType);
       
   171         TInt fieldIndex = aContact.AddFieldL(field); // takes ownership
       
   172         CleanupStack::Pop(); // field
       
   173 		field = &aContact.Fields().FieldAt(fieldIndex);			
       
   174         }
       
   175 	
       
   176 	if (SupportedStorageType(field))
       
   177 		{
       
   178 		writer = 
       
   179 		    CPbk2EmbeddedImageWriter::NewLC(
       
   180 		        aContactManager, 
       
   181 		        *field, 
       
   182 		        aObserver);
       
   183 		}
       
   184 	else
       
   185 		{
       
   186 		User::Leave(KErrNotSupported);
       
   187 		}
       
   188 		
       
   189     return writer;
       
   190     }
       
   191 
       
   192 // --------------------------------------------------------------------------
       
   193 // Pbk2ImageOperationFactory::SupportedStorageType
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 TBool Pbk2ImageOperationFactory::SupportedStorageType
       
   197         (const MVPbkBaseContactField* aField)
       
   198     {
       
   199     TVPbkFieldStorageType storageType = aField->FieldData().DataType();
       
   200     return EVPbkFieldStorageTypeBinary==storageType ? ETrue: EFalse;
       
   201     }
       
   202 
       
   203 //  End of File