phonebookengines/VirtualPhonebook/VPbkEng/src/VPbkUtils.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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:  Utils for virtual phonebook
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "VPbkUtils.h"
       
    20 
       
    21 #include <barsread.h>
       
    22 
       
    23 #include <VPbkError.h>
       
    24 #include <MVPbkFieldType.h>
       
    25 #include <MVPbkBaseContact.h>
       
    26 #include <MVPbkBaseContactField.h>
       
    27 #include <CVPbkContactManager.h>
       
    28 #include <CVPbkFieldTypeSelector.h>
       
    29 #include <MVPbkBaseContactFieldCollection.h>
       
    30 #include <CVPbkContactFieldIterator.h>
       
    31 #include <MVPbkStoreContact.h>
       
    32 #include <VPbkEng.rsg>
       
    33 #include <RLocalizedResourceFile.h>
       
    34 #include <VPbkDataCaging.hrh>
       
    35 #include <TVPbkFieldVersitProperty.h>
       
    36 #include <MVPbkContactFieldData.h>
       
    37 #include <MVPbkContactFieldTextData.h>
       
    38 #include <VPbkSyncConstants.h>
       
    39 
       
    40 
       
    41 namespace VPbkUtils {
       
    42 
       
    43 EXPORT_C const MVPbkFieldType* MatchFieldType
       
    44     (const MVPbkFieldTypeList& aFieldTypeList,
       
    45     const MVPbkBaseContactField& aField)
       
    46     {
       
    47     const MVPbkFieldType* result = NULL;
       
    48 
       
    49     for (TInt matchPriority = 0; 
       
    50         matchPriority <= aFieldTypeList.MaxMatchPriority() && !result;
       
    51         ++matchPriority)
       
    52         {
       
    53         result = aField.MatchFieldType(matchPriority);
       
    54         }
       
    55 
       
    56     return result;
       
    57     }
       
    58 
       
    59 EXPORT_C TBool IsFieldTypeIncludedL(
       
    60         const MVPbkFieldTypeList& aFieldTypeList,
       
    61         const MVPbkBaseContactField& aField,
       
    62         TResourceReader& aResourceReader)
       
    63     {
       
    64     TBool ret = EFalse;
       
    65     // Get aField's field type
       
    66     const MVPbkFieldType* fieldType = MatchFieldType(aFieldTypeList, 
       
    67             aField);
       
    68     
       
    69     if ( fieldType )
       
    70         {
       
    71         CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL(
       
    72             aResourceReader, aFieldTypeList );
       
    73         // Check if the fields type is the one needed
       
    74         ret = selector->IsFieldTypeIncluded( *fieldType );
       
    75         delete selector;
       
    76         }
       
    77     return ret;
       
    78     }
       
    79 
       
    80 EXPORT_C TBool IsFieldTypeIncludedInContactL(
       
    81         const MVPbkFieldTypeList& aFieldTypeList,
       
    82         const MVPbkBaseContact& aContact,
       
    83         TResourceReader& aResourceReader)
       
    84     {
       
    85     // create the field type selector
       
    86     CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL(
       
    87             aResourceReader, aFieldTypeList);
       
    88     CleanupStack::PushL(selector);
       
    89     
       
    90     TBool ret = EFalse;
       
    91     // loop though all the fields in the contact
       
    92     const MVPbkBaseContactFieldCollection& fieldCol = aContact.Fields();
       
    93     for (TInt i = 0; i < fieldCol.FieldCount(); ++i)
       
    94         {
       
    95         const MVPbkBaseContactField& field = fieldCol.FieldAt(i);
       
    96         // get the field type
       
    97         const MVPbkFieldType* fieldType = MatchFieldType(aFieldTypeList, 
       
    98                 field);
       
    99         ret = selector->IsFieldTypeIncluded(*fieldType);
       
   100         if (ret)
       
   101             {
       
   102             // break and return if a field matching the criteria is found
       
   103             break;
       
   104             }
       
   105         }
       
   106     CleanupStack::PopAndDestroy(selector);
       
   107     return ret;
       
   108     }
       
   109 
       
   110 void DoValidateFieldContentL(
       
   111         MVPbkStoreContactField& aSyncField)
       
   112     {
       
   113     // get the field content
       
   114     MVPbkContactFieldData& data = aSyncField.FieldData();
       
   115     if (data.DataType() == EVPbkFieldStorageTypeText)
       
   116         {
       
   117         // check that a valid text is in the field
       
   118         MVPbkContactFieldTextData& textData = MVPbkContactFieldTextData::Cast(data);
       
   119         if (!(!textData.Text().CompareF(KVPbkContactSyncPrivate) || 
       
   120             !textData.Text().CompareF(KVPbkContactSyncPublic) ||
       
   121             !textData.Text().CompareF(KVPbkContactSyncNoSync)))
       
   122             {
       
   123             // set default sync setting to field
       
   124             textData.SetTextL(KVPbkContactSyncPrivate);
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129 EXPORT_C TBool VerifySyncronizationFieldL(
       
   130         RFs& aFs,
       
   131         const MVPbkFieldTypeList& aSupportedFieldTypeList,
       
   132         MVPbkStoreContact& aContact)
       
   133     {
       
   134     TBool ret = EFalse;
       
   135     // check whether the contact store supports the sync field
       
   136     VPbkEngUtils::RLocalizedResourceFile resFile;
       
   137     resFile.OpenLC(aFs, KVPbkRomFileDrive, 
       
   138         KDC_RESOURCE_FILES_DIR, KVPbkDefResFileName);
       
   139     HBufC8* resourceBuf = resFile.AllocReadLC(R_VPBK_SYNCRONIZATION_FIELD_PROPERTY);
       
   140 
       
   141     TResourceReader resReader;
       
   142     resReader.SetBuffer(resourceBuf);
       
   143     TVPbkFieldVersitProperty syncProperty(resReader);
       
   144     // find field type from aSupportedFieldTypeList
       
   145     const MVPbkFieldType* syncFieldType = 
       
   146         aSupportedFieldTypeList.FindMatch(syncProperty, 0);
       
   147     CleanupStack::PopAndDestroy(2); // resFile, resourceBuf
       
   148     if (syncFieldType)
       
   149         {
       
   150         // syncronization field is supported by store
       
   151         // check whether the contact has the syncronization field
       
   152         CVPbkContactFieldTypeIterator* iter = CVPbkContactFieldTypeIterator::NewLC(
       
   153                 *syncFieldType, aContact.Fields());
       
   154         if(iter->HasNext())
       
   155             {
       
   156             // syncronization field is in contact
       
   157             MVPbkStoreContactField* syncField = iter->Next();
       
   158             // check that the contact contains one of the required texts, 
       
   159             // else replace it with default
       
   160             DoValidateFieldContentL(*syncField);
       
   161             }
       
   162         else
       
   163             {
       
   164             // No sync field in contact
       
   165             // create field to contact and set content to default
       
   166             MVPbkStoreContactField* syncField = aContact.CreateFieldLC(*syncFieldType);
       
   167             DoValidateFieldContentL(*syncField);
       
   168             // add created field to contact
       
   169             aContact.AddFieldL(syncField);
       
   170             CleanupStack::Pop(syncField);
       
   171             }
       
   172         CleanupStack::PopAndDestroy(iter);
       
   173         }
       
   174     return ret;
       
   175     }
       
   176 
       
   177 } // VPbkUtils
       
   178 
       
   179 // end of file