phonebookengines/VirtualPhonebook/VPbkCntModel/src/TContactFieldCollection.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Maps a Contact Model field collection to a virtual Phonebook 
       
    15                  field collection.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include "TContactFieldCollection.h"
       
    22 #include <cntitem.h>
       
    23 #include <VPbkError.h>
       
    24 #include "TContactField.h"
       
    25 #include "CContact.h"
       
    26 #include "CContactLink.h"
       
    27 
       
    28 namespace VPbkCntModel {
       
    29 
       
    30 TContactFieldCollection::TContactFieldCollection
       
    31         (CContact& aParentContact, CContactItemFieldSet& aFieldSet) :
       
    32     iFieldSet(&aFieldSet),
       
    33     iCurrentField(aParentContact),
       
    34     iParentContact(&aParentContact)
       
    35     {
       
    36     }
       
    37 
       
    38 TContactFieldCollection::~TContactFieldCollection()
       
    39     {
       
    40     }
       
    41 
       
    42 void TContactFieldCollection::SetContact
       
    43         (CContact& aParentContact, CContactItemFieldSet& aFieldSet)
       
    44     {
       
    45     iFieldSet = &aFieldSet;
       
    46     iCurrentField.SetParentContact(aParentContact);
       
    47     iParentContact = &aParentContact;
       
    48     }
       
    49 
       
    50 MVPbkBaseContact& TContactFieldCollection::ParentContact() const
       
    51     {
       
    52     return *iParentContact;
       
    53     }
       
    54 
       
    55 TInt TContactFieldCollection::FieldCount() const
       
    56     {
       
    57     return iFieldSet->Count();
       
    58     }
       
    59 
       
    60 const MVPbkStoreContactField& TContactFieldCollection::FieldAt(TInt aIndex) const
       
    61     {
       
    62     __ASSERT_ALWAYS(aIndex >= 0 && aIndex < iFieldSet->Count(), 
       
    63         VPbkError::Panic(VPbkError::EInvalidFieldIndex));
       
    64     iCurrentField.SetField((*iFieldSet)[aIndex]);
       
    65     return iCurrentField;
       
    66     }
       
    67 
       
    68 MVPbkStoreContactField& TContactFieldCollection::FieldAt(TInt aIndex)
       
    69     {
       
    70     __ASSERT_ALWAYS(aIndex >= 0 && aIndex < iFieldSet->Count(), 
       
    71         VPbkError::Panic(VPbkError::EInvalidFieldIndex));
       
    72     iCurrentField.SetField((*iFieldSet)[aIndex]);
       
    73     return iCurrentField;
       
    74     }
       
    75 
       
    76 MVPbkStoreContactField* TContactFieldCollection::FieldAtLC(TInt aIndex) const
       
    77     {
       
    78     __ASSERT_ALWAYS(aIndex >= 0 && aIndex < iFieldSet->Count(), 
       
    79         VPbkError::Panic(VPbkError::EInvalidFieldIndex));
       
    80     TContactField* clone =
       
    81         new (ELeave) TContactField(*iParentContact);
       
    82     clone->SetField((*iFieldSet)[aIndex]);
       
    83     CleanupDeletePushL(clone);
       
    84     return clone;
       
    85     }
       
    86 
       
    87 MVPbkStoreContact& TContactFieldCollection::ParentStoreContact() const
       
    88     {
       
    89     return *iParentContact;
       
    90     }
       
    91     
       
    92 MVPbkStoreContactField* TContactFieldCollection::RetrieveField(
       
    93         const MVPbkContactLink& aContactLink) const
       
    94     {
       
    95     __ASSERT_ALWAYS(aContactLink.RefersTo(*iParentContact), 
       
    96                     VPbkError::Panic(VPbkError::EInvalidContactField));
       
    97 
       
    98     MVPbkStoreContactField* result = NULL;
       
    99 
       
   100     if (&aContactLink.ContactStore() == &iParentContact->ContactStore())
       
   101         {
       
   102         const CContactLink& link = static_cast<const CContactLink&>(aContactLink);
       
   103         const TInt fieldCount = FieldCount();
       
   104         for (TInt i = 0; i < fieldCount; ++i)
       
   105             {
       
   106             if ((*iFieldSet)[i].Id() == link.ContactFieldId())
       
   107                 {
       
   108                 iCurrentField.SetField((*iFieldSet)[i]);
       
   109                 result = &iCurrentField;
       
   110                 }
       
   111             }
       
   112         }
       
   113 
       
   114     return result;
       
   115     }
       
   116 } // namespace VPbkCntModel
       
   117