phonebookengines/VirtualPhonebook/VPbkCntModel/src/CContactLink.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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:  The virtual phonebook contact link
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CContactLink.h"
       
    21 #include "CContactStore.h"
       
    22 #include "CContact.h"
       
    23 #include "CViewContact.h"
       
    24 #include "CViewBase.h"
       
    25 
       
    26 #include <cntitem.h>
       
    27 #include <CVPbkContactLinkArray.h>
       
    28 #include <MVPbkStoreContact.h>
       
    29 #include <MVPbkViewContact.h>
       
    30 #include <MVPbkContactStoreProperties.h>
       
    31 
       
    32 namespace {
       
    33 
       
    34 /**
       
    35  * Link version supported by this version.
       
    36  */
       
    37 const TUint8 KContactLinkVersion = 1;
       
    38 
       
    39 } // namespace
       
    40 
       
    41 namespace VPbkCntModel {
       
    42 
       
    43 // CContactLink implementation
       
    44 CContactLink::CContactLink(CContactStore& aContactStore) :
       
    45     iContactStore(aContactStore),
       
    46     iFieldId(KErrNotFound)
       
    47     {
       
    48     }
       
    49 
       
    50 void CContactLink::CommonConstructL()
       
    51     {
       
    52     iLinkArray = CVPbkContactLinkArray::NewL();
       
    53     iLinkArray->AppendL(this);
       
    54     }
       
    55     
       
    56 /**
       
    57  * The Contact model link internal specification.
       
    58  * Specifies link content and order.
       
    59  * 1st item: version numer (byte)
       
    60  * 2nd item: contact item id (word)
       
    61  * 3rd item: contact item field id (word)
       
    62  */
       
    63 inline void CContactLink::ConstructL(
       
    64         RReadStream& aStream)
       
    65     {
       
    66     CommonConstructL();
       
    67     /*TUint8 version = */aStream.ReadUint8L();
       
    68     iId = aStream.ReadInt32L(); // TContactItemId
       
    69     iFieldId = aStream.ReadInt32L(); // TInt
       
    70     
       
    71     }
       
    72 
       
    73 inline void CContactLink::ConstructL(
       
    74         TContactItemId aId,
       
    75         TInt aFieldId)
       
    76     {
       
    77     CommonConstructL();
       
    78     iId = aId;
       
    79     iFieldId = aFieldId;
       
    80     }
       
    81 
       
    82 CContactLink* CContactLink::NewLC
       
    83         (CContactStore& aContactStore,
       
    84         RReadStream& aStream)
       
    85     {
       
    86     CContactLink* self = new(ELeave) CContactLink(aContactStore);
       
    87     CleanupStack::PushL(self);
       
    88     self->ConstructL(aStream);
       
    89     return self;
       
    90     }
       
    91 
       
    92 CContactLink* CContactLink::NewLC
       
    93         (CContactStore& aContactStore, 
       
    94         TContactItemId aId,
       
    95         TInt aFieldId /* = KErrNotFound */)
       
    96     {
       
    97     CContactLink* self = new(ELeave) CContactLink(aContactStore);
       
    98     CleanupStack::PushL(self);
       
    99     self->ConstructL(aId, aFieldId);
       
   100     return self;
       
   101     }
       
   102 
       
   103 CContactLink::~CContactLink()
       
   104     {
       
   105     if (iLinkArray)
       
   106         {
       
   107         iLinkArray->Reset();
       
   108         delete iLinkArray;
       
   109         }
       
   110     }
       
   111 
       
   112 TBool CContactLink::Matches(
       
   113         const TDesC& aStoreUri, 
       
   114         const MVPbkContactLink& aLink,
       
   115         TVPbkContactStoreUriPtr::TVPbkContactStoreUriComponent aComponent)
       
   116     {
       
   117     if (aLink.ContactStore().StoreProperties().Uri().Compare(aStoreUri, aComponent) == 0) 
       
   118         {
       
   119         return ETrue;
       
   120         }
       
   121     return EFalse;
       
   122     }
       
   123 
       
   124 TBool CContactLink::Matches(
       
   125         const TVPbkContactStoreUriPtr& aStoreUri,
       
   126         const MVPbkContactLink& aLink)
       
   127     {
       
   128     if (aLink.ContactStore().StoreProperties().Uri().Compare(aStoreUri, 
       
   129             TVPbkContactStoreUriPtr::EContactStoreUriAllComponents) == 0) 
       
   130         {
       
   131         return ETrue;
       
   132         }
       
   133     return EFalse;
       
   134     }
       
   135 
       
   136 TContactItemId CContactLink::ContactId() const
       
   137     {
       
   138     return iId;
       
   139     }
       
   140 
       
   141 TInt CContactLink::ContactFieldId() const
       
   142     {
       
   143     return iFieldId;
       
   144     }
       
   145 
       
   146 void CContactLink::SetContactId(TContactItemId aContactId)
       
   147     {
       
   148     iId = aContactId;
       
   149     }
       
   150 
       
   151 void CContactLink::SetContactFieldId(TInt aContactFieldId)
       
   152     {
       
   153     iFieldId = aContactFieldId;
       
   154     }
       
   155 
       
   156 const TPtrC CContactLink::ContactDbFileName() const
       
   157     {
       
   158     return iContactStore.StoreProperties().Uri().Component(
       
   159             TVPbkContactStoreUriPtr::EContactStoreUriStoreLocation);
       
   160     }
       
   161 
       
   162 MVPbkContactStore& CContactLink::ContactStore() const
       
   163     {
       
   164     return iContactStore;
       
   165     }
       
   166 
       
   167 TBool CContactLink::IsSame(const MVPbkContactLink& aOther) const
       
   168     {
       
   169     TBool result = EFalse;
       
   170     if (&ContactStore() == &aOther.ContactStore())
       
   171         {
       
   172         const CContactLink& other = 
       
   173             static_cast<const CContactLink&>(aOther);
       
   174         result = (other.ContactId() == ContactId() &&
       
   175                   other.ContactFieldId() == ContactFieldId());
       
   176         }
       
   177     return result;
       
   178     }
       
   179     
       
   180 TBool CContactLink::RefersTo(const MVPbkBaseContact& aContact) const
       
   181     {
       
   182     TBool result = EFalse;
       
   183     if (const CContact* contact = dynamic_cast<const CContact*>(&aContact))
       
   184         {
       
   185         if (&ContactStore() == &contact->ParentStore())
       
   186             {
       
   187             result = (contact->NativeContact()->Id() == ContactId());
       
   188             }
       
   189         }
       
   190     else if(const CViewContact* contact = dynamic_cast<const CViewContact*>(&aContact))
       
   191         {
       
   192         if (&ContactStore() == &contact->View().ContactStore())
       
   193             {
       
   194             result = (contact->Id() == ContactId());
       
   195             }
       
   196         }
       
   197     return result;
       
   198     }
       
   199 
       
   200 const MVPbkStreamable* CContactLink::Streamable() const
       
   201     {
       
   202     return this;
       
   203     }
       
   204 
       
   205 const MVPbkContactLinkPacking& CContactLink::Packing() const
       
   206     {
       
   207     return *this;
       
   208     }
       
   209     
       
   210 MVPbkContactLink* CContactLink::CloneLC() const
       
   211     {
       
   212     return CContactLink::NewLC(iContactStore, iId, iFieldId);
       
   213     }
       
   214 
       
   215 void CContactLink::ExternalizeL(
       
   216         RWriteStream& aStream) const
       
   217     {
       
   218     HBufC8* buf = iLinkArray->PackLC();
       
   219     aStream.WriteL(*buf);
       
   220     CleanupStack::PopAndDestroy(buf);
       
   221     }
       
   222 
       
   223 TInt CContactLink::ExternalizedSize() const
       
   224     {
       
   225     return iLinkArray->PackedBufferSize();
       
   226     }
       
   227 
       
   228 TInt CContactLink::InternalPackedSize() const
       
   229     {
       
   230     return (1 /*Link version number */ +
       
   231             4 /* Contact item id */ +
       
   232             4 /* Contact field id */);
       
   233     }
       
   234 
       
   235 void CContactLink::PackInternalsL(RWriteStream& aStream) const
       
   236     {
       
   237     aStream.WriteUint8L(KContactLinkVersion); // version number
       
   238     aStream.WriteInt32L(iId); // contact item id
       
   239     aStream.WriteInt32L(iFieldId);
       
   240     }
       
   241 
       
   242 } // namespace VPbkCntModel
       
   243 
       
   244 // end of file