omads/omadsextensions/adapters/contactsgroup/src/snapshotitem.cpp
branchRCL_3
changeset 52 4f0867e42d62
parent 51 8e7494275d3a
child 56 3e6957da2ff8
equal deleted inserted replaced
51:8e7494275d3a 52:4f0867e42d62
     1 /*
       
     2 * Copyright (c) 2010 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:  Part of SyncML Data Synchronization Plug In Adapter
       
    15 *
       
    16 */
       
    17 
       
    18 #include <hash.h>
       
    19 #include <utf.h>
       
    20 #include <cntitem.h>
       
    21 
       
    22 #include "snapshotitem.h"
       
    23 #include "logger.h"
       
    24 
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // TSnapshotItem::TSnapshotItem
       
    28 // C++ default constructor can NOT contain any code, that might leave
       
    29 // -----------------------------------------------------------------------------
       
    30 TSnapshotItem::TSnapshotItem() : TNSmlSnapshotItem()
       
    31     {
       
    32     }
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // TSnapshotItem::TSnapshotItem
       
    36 // Constructor, takes item id as a parameter
       
    37 // -----------------------------------------------------------------------------
       
    38 TSnapshotItem::TSnapshotItem( const TSmlDbItemUid& aItemId,
       
    39     const TSmlDbItemUid& aParent )
       
    40 : TNSmlSnapshotItem( aItemId )
       
    41     {
       
    42     SetParentId( aParent );
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // TSnapshotItem::ExternalizeL
       
    47 // Writes the contents of this class and it's base to stream
       
    48 // -----------------------------------------------------------------------------
       
    49 void TSnapshotItem::ExternalizeL( RWriteStream& aStream ) const
       
    50     {
       
    51     TNSmlSnapshotItem::ExternalizeL( aStream );
       
    52     TPckgBuf<THashValue> nameBuf(iHash);
       
    53 	aStream << nameBuf;
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // TSnapshotItem::InternalizeL
       
    58 // Reads the contents of this class and it's base from stream
       
    59 // -----------------------------------------------------------------------------
       
    60 void TSnapshotItem::InternalizeL( RReadStream& aStream )
       
    61     {
       
    62     TNSmlSnapshotItem::InternalizeL( aStream );
       
    63     TPckgBuf<THashValue> nameBuf;
       
    64 	aStream >> nameBuf;
       
    65 	iHash = nameBuf();
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // TSnapshotItem::CreateHashL
       
    70 // Create hash value from group content
       
    71 // -----------------------------------------------------------------------------
       
    72 void TSnapshotItem::CreateHashL( CContactGroup& aGroup )
       
    73     {
       
    74     CMessageDigest* hash = CMessageDigestFactory::NewDigestLC( CMessageDigest::EMD5 );
       
    75     
       
    76     if ( aGroup.HasItemLabelField() )
       
    77         {
       
    78         TPtrC label = aGroup.GetGroupLabelL();
       
    79         HBufC8* tempBuf = CnvUtfConverter::ConvertFromUnicodeToUtf8L( label );
       
    80         CleanupStack::PushL( tempBuf );
       
    81         hash->Update( tempBuf->Des() );
       
    82         CleanupStack::PopAndDestroy( tempBuf );
       
    83         }
       
    84     
       
    85     // Create text field from items on group
       
    86     CContactIdArray* contactsIdArray = aGroup.ItemsContainedLC();
       
    87     const TInt idBufferMaxSize( 400 );
       
    88     HBufC8* tempIdBuf = HBufC8::NewLC( idBufferMaxSize );
       
    89     TPtr8 tempId = tempIdBuf->Des();
       
    90     const TInt KMaxNumLength(5);
       
    91     if ( contactsIdArray ) // this is NULL if there are no objects
       
    92         {
       
    93         tempId.AppendNum( contactsIdArray->Count(), EHex );
       
    94         for (TInt i=0; i< contactsIdArray->Count(); i++ )
       
    95             {
       
    96             if ( tempId.Length()+KMaxNumLength > tempId.MaxLength() )
       
    97                 {
       
    98                 // buffer is almost full, don't add any new items
       
    99                 LOGGER_WRITE("buffer full");
       
   100                 break;
       
   101                 }
       
   102             // add item id
       
   103             tempId.AppendNum( (*contactsIdArray)[i] , EHex );
       
   104             }
       
   105         }
       
   106     else
       
   107         {
       
   108         tempId.AppendNum( 0 );
       
   109         }
       
   110     // update hash
       
   111     hash->Update( tempId );
       
   112     
       
   113     iHash.Copy( hash->Final() );
       
   114     
       
   115     CleanupStack::PopAndDestroy( tempIdBuf );
       
   116     CleanupStack::PopAndDestroy( contactsIdArray );
       
   117     CleanupStack::PopAndDestroy( hash );
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // TSnapshotItem::Hash
       
   122 // Gets Hash
       
   123 // -----------------------------------------------------------------------------
       
   124 const TDesC8& TSnapshotItem::Hash() const
       
   125     {
       
   126     return iHash;
       
   127     }