phonebookui/Phonebook2/CommandsExtension/src/Pbk2SendCmdUtils.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:  Send command's utility functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "Pbk2SendCmdUtils.h"
       
    20 
       
    21 #include <barsread.h> // TResourceReader
       
    22 #include <coemain.h> // CCoeEnv
       
    23 #include <MVPbkBaseContact.h>
       
    24 #include <MVPbkBaseContactField.h>
       
    25 #include <MVPbkContactFieldData.h>
       
    26 #include <MVPbkBaseContactFieldCollection.h>
       
    27 #include <TVPbkFieldTypeMapping.h>
       
    28 #include <MVPbkFieldType.h>
       
    29 
       
    30 TBool Pbk2SendCmdUtils::IsContactEmpty(const MVPbkBaseContact* aContact)
       
    31 	{
       
    32 	TInt fieldCount = aContact->Fields().FieldCount();
       
    33 	const MVPbkBaseContactFieldCollection& fieldSet = aContact->Fields();
       
    34 	for (TInt i=0; i < fieldCount; ++i)
       
    35 		{
       
    36 		const MVPbkBaseContactField& field = fieldSet.FieldAt(i);
       
    37 		if (!field.FieldData().IsEmpty())
       
    38 			{
       
    39 			// There was a field, which was not empty (or all spaces),
       
    40 			// that's enough for us, the contact gets sent
       
    41 			return EFalse;
       
    42 			}
       
    43 		}
       
    44 		
       
    45 	return ETrue;			
       
    46 	}
       
    47 
       
    48 const MVPbkBaseContactField* Pbk2SendCmdUtils::FindFieldL(
       
    49 	const MVPbkBaseContact& aContact,
       
    50 	TInt aResIdForFieldType,
       
    51 	const MVPbkFieldTypeList& aMasterFieldTypeList)
       
    52 	{
       
    53 	const MVPbkFieldType& fieldtype = 
       
    54 		ReadFieldTypeFromResL(aResIdForFieldType, aMasterFieldTypeList);
       
    55 	return FindField(aContact, fieldtype, aMasterFieldTypeList);
       
    56 	}
       
    57 	
       
    58 const MVPbkBaseContactField* Pbk2SendCmdUtils::FindField(
       
    59 	const MVPbkBaseContact& aContact,
       
    60 	const MVPbkFieldType& aFieldType,
       
    61 	const MVPbkFieldTypeList& aMasterFieldTypeList)
       
    62 	{
       
    63 	const MVPbkBaseContactFieldCollection& fields = aContact.Fields();
       
    64 	TInt fieldCount(fields.FieldCount());
       
    65 	for (TInt i = 0; i < fieldCount; ++i)
       
    66 		{
       
    67 		const MVPbkBaseContactField& field = fields.FieldAt(i);
       
    68 		if (IsFieldMatching(field, aFieldType, aMasterFieldTypeList))
       
    69 			{
       
    70 			return &field;
       
    71 			}
       
    72 		}
       
    73 	return NULL;		
       
    74 	}
       
    75 	
       
    76 const MVPbkFieldType& Pbk2SendCmdUtils::ReadFieldTypeFromResL(
       
    77 	TInt aResId,
       
    78 	const MVPbkFieldTypeList& aMasterFieldTypeList)
       
    79 	{
       
    80     TResourceReader reader;
       
    81     CCoeEnv::Static()->CreateResourceReaderLC(reader, aResId);
       
    82     const TInt count = reader.ReadInt16();
       
    83     
       
    84     TVPbkFieldTypeMapping mapping(reader);
       
    85     const MVPbkFieldType* fieldType = mapping.FindMatch(aMasterFieldTypeList);
       
    86     if (!fieldType)
       
    87         {
       
    88         User::Leave(KErrNotFound);
       
    89         }
       
    90     CleanupStack::PopAndDestroy(); // CreateResourceReaderLC
       
    91     return *fieldType;
       
    92 	}	
       
    93 	
       
    94 const MVPbkFieldType* Pbk2SendCmdUtils::FieldType(
       
    95 	const MVPbkBaseContactField& aField,
       
    96 	const MVPbkFieldTypeList& aMasterFieldTypeList)
       
    97 	{
       
    98     for (TInt matchPriority = 0; 
       
    99         matchPriority <= aMasterFieldTypeList.MaxMatchPriority();
       
   100         ++matchPriority)
       
   101         {
       
   102         return aField.MatchFieldType(matchPriority);
       
   103         }	
       
   104 	return NULL;
       
   105 	}
       
   106 		
       
   107 TBool Pbk2SendCmdUtils::IsFieldMatching(
       
   108 	const MVPbkBaseContactField& aField,
       
   109 	const MVPbkFieldType& aFieldType,
       
   110 	const MVPbkFieldTypeList& aMasterFieldTypeList)
       
   111 	{
       
   112     const MVPbkFieldType* result = NULL;
       
   113 
       
   114     for (TInt matchPriority = 0; 
       
   115         matchPriority <= aMasterFieldTypeList.MaxMatchPriority();
       
   116         ++matchPriority)
       
   117         {
       
   118         result = aField.MatchFieldType(matchPriority);
       
   119         if (result && result->IsSame(aFieldType))
       
   120         	{
       
   121         	return ETrue;
       
   122         	}
       
   123         }
       
   124 
       
   125     return EFalse;	
       
   126 	}  		
       
   127 		
       
   128 // End of file