phonebookengines/VirtualPhonebook/VPbkEng/src/VPbkUtils.cpp
changeset 0 e686773b3f54
child 3 04ab22b956c2
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:  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     // Get aField's field type
       
    65     const MVPbkFieldType* fieldType = MatchFieldType(aFieldTypeList, 
       
    66             aField);
       
    67 
       
    68     CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL(
       
    69             aResourceReader, aFieldTypeList);
       
    70     // Check if the fields type is the one needed
       
    71     TBool ret = selector->IsFieldTypeIncluded(*fieldType);
       
    72     delete selector;
       
    73     return ret;
       
    74     }
       
    75 
       
    76 EXPORT_C TBool IsFieldTypeIncludedInContactL(
       
    77         const MVPbkFieldTypeList& aFieldTypeList,
       
    78         const MVPbkBaseContact& aContact,
       
    79         TResourceReader& aResourceReader)
       
    80     {
       
    81     // create the field type selector
       
    82     CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL(
       
    83             aResourceReader, aFieldTypeList);
       
    84     CleanupStack::PushL(selector);
       
    85     
       
    86     TBool ret = EFalse;
       
    87     // loop though all the fields in the contact
       
    88     const MVPbkBaseContactFieldCollection& fieldCol = aContact.Fields();
       
    89     for (TInt i = 0; i < fieldCol.FieldCount(); ++i)
       
    90         {
       
    91         const MVPbkBaseContactField& field = fieldCol.FieldAt(i);
       
    92         // get the field type
       
    93         const MVPbkFieldType* fieldType = MatchFieldType(aFieldTypeList, 
       
    94                 field);
       
    95         ret = selector->IsFieldTypeIncluded(*fieldType);
       
    96         if (ret)
       
    97             {
       
    98             // break and return if a field matching the criteria is found
       
    99             break;
       
   100             }
       
   101         }
       
   102     CleanupStack::PopAndDestroy(selector);
       
   103     return ret;
       
   104     }
       
   105 
       
   106 void DoValidateFieldContentL(
       
   107         MVPbkStoreContactField& aSyncField)
       
   108     {
       
   109     // get the field content
       
   110     MVPbkContactFieldData& data = aSyncField.FieldData();
       
   111     if (data.DataType() == EVPbkFieldStorageTypeText)
       
   112         {
       
   113         // check that a valid text is in the field
       
   114         MVPbkContactFieldTextData& textData = MVPbkContactFieldTextData::Cast(data);
       
   115         if (!(!textData.Text().CompareF(KVPbkContactSyncPrivate) || 
       
   116             !textData.Text().CompareF(KVPbkContactSyncPublic) ||
       
   117             !textData.Text().CompareF(KVPbkContactSyncNoSync)))
       
   118             {
       
   119             // set default sync setting to field
       
   120             textData.SetTextL(KVPbkContactSyncPrivate);
       
   121             }
       
   122         }
       
   123     }
       
   124 
       
   125 EXPORT_C TBool VerifySyncronizationFieldL(
       
   126         RFs& aFs,
       
   127         const MVPbkFieldTypeList& aSupportedFieldTypeList,
       
   128         MVPbkStoreContact& aContact)
       
   129     {
       
   130     TBool ret = EFalse;
       
   131     // check whether the contact store supports the sync field
       
   132     VPbkEngUtils::RLocalizedResourceFile resFile;
       
   133     resFile.OpenLC(aFs, KVPbkRomFileDrive, 
       
   134         KDC_RESOURCE_FILES_DIR, KVPbkDefResFileName);
       
   135     HBufC8* resourceBuf = resFile.AllocReadLC(R_VPBK_SYNCRONIZATION_FIELD_PROPERTY);
       
   136 
       
   137     TResourceReader resReader;
       
   138     resReader.SetBuffer(resourceBuf);
       
   139     TVPbkFieldVersitProperty syncProperty(resReader);
       
   140     // find field type from aSupportedFieldTypeList
       
   141     const MVPbkFieldType* syncFieldType = 
       
   142         aSupportedFieldTypeList.FindMatch(syncProperty, 0);
       
   143     CleanupStack::PopAndDestroy(2); // resFile, resourceBuf
       
   144     if (syncFieldType)
       
   145         {
       
   146         // syncronization field is supported by store
       
   147         // check whether the contact has the syncronization field
       
   148         CVPbkContactFieldTypeIterator* iter = CVPbkContactFieldTypeIterator::NewLC(
       
   149                 *syncFieldType, aContact.Fields());
       
   150         if(iter->HasNext())
       
   151             {
       
   152             // syncronization field is in contact
       
   153             MVPbkStoreContactField* syncField = iter->Next();
       
   154             // check that the contact contains one of the required texts, 
       
   155             // else replace it with default
       
   156             DoValidateFieldContentL(*syncField);
       
   157             }
       
   158         else
       
   159             {
       
   160             // No sync field in contact
       
   161             // create field to contact and set content to default
       
   162             MVPbkStoreContactField* syncField = aContact.CreateFieldLC(*syncFieldType);
       
   163             DoValidateFieldContentL(*syncField);
       
   164             // add created field to contact
       
   165             aContact.AddFieldL(syncField);
       
   166             CleanupStack::Pop(syncField);
       
   167             }
       
   168         CleanupStack::PopAndDestroy(iter);
       
   169         }
       
   170     return ret;
       
   171     }
       
   172 
       
   173 } // VPbkUtils
       
   174 
       
   175 // end of file