presencecache/presencecacheutils/src/presencecachebuddyinfo.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2008 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bamdesca.h>
       
    20 #include <s32strm.h>
       
    21 #include "presencecachebuddyinfo.h"
       
    22 #include <utf.h>
       
    23 
       
    24 #define STRCNST(x)  (const_cast<TDesC*>( &x ))
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 CPresenceCacheBuddyInfo::CPresenceCacheBuddyInfo()
       
    28     {
       
    29     // No implementation required
       
    30     }
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 CPresenceCacheBuddyInfo::~CPresenceCacheBuddyInfo()
       
    34     {      
       
    35     iHashMap.Close();  
       
    36     delete iAvailabilityTextBuf;
       
    37     delete iStatusTextBuf;
       
    38     delete iBuddyId;
       
    39     
       
    40     iIds.ResetAndDestroy();
       
    41     iIds.Close();
       
    42     iValues.ResetAndDestroy();
       
    43     iValues.Close();    
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // MPresenceBuddyInfo2::NewL()
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 EXPORT_C MPresenceBuddyInfo2* MPresenceBuddyInfo2::NewL( )
       
    51     {
       
    52     return CPresenceCacheBuddyInfo::NewL( );
       
    53     }
       
    54     
       
    55 EXPORT_C MPresenceBuddyInfo2* MPresenceBuddyInfo2::NewLC( )
       
    56     {
       
    57     CPresenceCacheBuddyInfo* info = CPresenceCacheBuddyInfo::NewLC( );
       
    58     CleanupStack::Pop(info);
       
    59     MPresenceBuddyInfo2* minfo = info;  
       
    60     CleanupDeletePushL(minfo);
       
    61     return minfo; 
       
    62     }
       
    63     
       
    64 // -----------------------------------------------------------------------------
       
    65 CPresenceCacheBuddyInfo* CPresenceCacheBuddyInfo::NewL()
       
    66     {
       
    67     CPresenceCacheBuddyInfo* self = NewLC();
       
    68     CleanupStack::Pop(); // self;    
       
    69     return self;
       
    70     }
       
    71     
       
    72 // -----------------------------------------------------------------------------
       
    73 CPresenceCacheBuddyInfo* CPresenceCacheBuddyInfo::NewLC()
       
    74     {
       
    75     CPresenceCacheBuddyInfo* self = new (ELeave)CPresenceCacheBuddyInfo();
       
    76     CleanupStack::PushL(self);
       
    77     self->ConstructL();  
       
    78     return self;
       
    79     }    
       
    80     
       
    81 // -----------------------------------------------------------------------------
       
    82 static TUint32 StringPtrHash( TDesC* const& aValue )
       
    83     {
       
    84     return DefaultHash::Des16( *aValue );
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 static TBool StringIdentity( TDesC* const& aA, TDesC* const& aB )
       
    89     {
       
    90     return !aA->CompareF( *aB );
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 void CPresenceCacheBuddyInfo::ConstructL()
       
    95     {
       
    96     iHashMap.Close();
       
    97     iHashMap = RHashMap<TDesC*, TInt>(    
       
    98         THashFunction32<TDesC*>( StringPtrHash ),
       
    99         TIdentityRelation<TDesC*>( StringIdentity ) );
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 void CPresenceCacheBuddyInfo::SetIdentityL( 
       
   104     const TDesC& aBuddyId )
       
   105     {
       
   106     delete iBuddyId;
       
   107     iBuddyId = NULL;
       
   108     iBuddyId = aBuddyId.AllocL( );        
       
   109     }
       
   110     
       
   111 // -----------------------------------------------------------------------------
       
   112 TPtrC CPresenceCacheBuddyInfo::BuddyId( ) const
       
   113     {
       
   114     return iBuddyId ? iBuddyId->Des() : TPtrC();
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 void CPresenceCacheBuddyInfo::SetAvailabilityL( 
       
   119     TAvailabilityValues aAvailability,
       
   120     const TDesC& aAvailabilityText )
       
   121     {  
       
   122     iAvailability = aAvailability;    
       
   123     DoSet16BitValueL( NPresenceCacheFieldName::KAvailability, aAvailabilityText );
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 MPresenceBuddyInfo2::TAvailabilityValues CPresenceCacheBuddyInfo::Availability( )
       
   128     {
       
   129     return iAvailability;    
       
   130     }
       
   131     
       
   132 // -----------------------------------------------------------------------------
       
   133 TPtrC CPresenceCacheBuddyInfo::AvailabilityText( )
       
   134     {
       
   135     return DoGet16BitValue( NPresenceCacheFieldName::KAvailability );
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 void CPresenceCacheBuddyInfo::DoSet16BitValueL( 
       
   140     const TDesC& aKey,    
       
   141     const TDesC& aValue )
       
   142     {   
       
   143       
       
   144     // Remove old values first
       
   145     RemoveField( aKey );
       
   146     
       
   147     //Convert Unicode to Utf-8
       
   148     HBufC8* convertBuffer =  CnvUtfConverter::ConvertFromUnicodeToUtf8L( aValue );
       
   149     CleanupStack::PushL( convertBuffer );
       
   150     HBufC* keyBuffer = aKey.AllocLC();    
       
   151     
       
   152     TInt insertPos = iIds.Find(0);
       
   153     if ( insertPos < 0 )
       
   154         {
       
   155         insertPos = iIds.Count();
       
   156         iIds.Append( keyBuffer );
       
   157         iValues.Append( convertBuffer );
       
   158         }
       
   159     else
       
   160         {
       
   161         iIds[insertPos] = keyBuffer;
       
   162         iValues[insertPos] = convertBuffer;
       
   163         }
       
   164     
       
   165     iHashMap.InsertL( keyBuffer, insertPos  );
       
   166         
       
   167     CleanupStack::Pop( keyBuffer );
       
   168     CleanupStack::Pop( convertBuffer );       
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 void CPresenceCacheBuddyInfo::RemoveField(const TDesC& aKey )
       
   173     {
       
   174     TInt* pos = iHashMap.Find( STRCNST( aKey ) );    
       
   175     if ( pos )
       
   176         {
       
   177         TInt mypos = *pos;
       
   178         iHashMap.Remove( iIds[mypos ]);
       
   179         
       
   180         delete iIds[mypos];
       
   181         iIds[mypos] = NULL;
       
   182         
       
   183         delete iValues[mypos];
       
   184         iValues[mypos] = NULL;               
       
   185         }    
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 TPtrC CPresenceCacheBuddyInfo::DoGet16BitValue(const TDesC& aKey )
       
   190     {
       
   191     HBufC8* val = NULL;        
       
   192     TInt* pos = iHashMap.Find( STRCNST( aKey ) );
       
   193     TPtrC result( KNullDesC() );
       
   194     
       
   195     if ( pos )
       
   196         {       
       
   197         val = iValues[*pos];
       
   198         if ( val )
       
   199             {
       
   200             HBufC* temp = NULL;
       
   201             // Convert  Utf-8 to Unicode
       
   202             TRAPD( errx,  temp = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *val ) );
       
   203             if ( !errx && temp )
       
   204                 { 
       
   205                 // Avalibility text handled here
       
   206                 if ( !aKey.CompareF( NPresenceCacheFieldName::KAvailability ) )
       
   207                 	{
       
   208                     delete iAvailabilityTextBuf;
       
   209                     iAvailabilityTextBuf = NULL;                 	
       
   210                     iAvailabilityTextBuf = temp; 
       
   211                     result.Set( iAvailabilityTextBuf->Des() );
       
   212                 	}
       
   213                 // Status message handled here
       
   214                 else if( !aKey.CompareF( NPresenceCacheFieldName::KStatusMessage ) )
       
   215                 	{
       
   216                     delete iStatusTextBuf;
       
   217                     iStatusTextBuf = NULL;                 	
       
   218                     iStatusTextBuf = temp;  
       
   219                     result.Set( iStatusTextBuf->Des() );
       
   220                 	}
       
   221                 }
       
   222             }
       
   223         }
       
   224     return result;
       
   225     }
       
   226     
       
   227 // -----------------------------------------------------------------------------
       
   228 void CPresenceCacheBuddyInfo::SetAvatarL( 
       
   229     const TDesC8& aAvatar )
       
   230     {
       
   231     SetAnyFieldL( NPresenceCacheFieldName::KAvatar, aAvatar);
       
   232     }
       
   233     
       
   234 // -----------------------------------------------------------------------------
       
   235 TPtrC8 CPresenceCacheBuddyInfo::Avatar( )
       
   236     {
       
   237     return GetAnyField( NPresenceCacheFieldName::KAvatar ); 
       
   238     }
       
   239     
       
   240 // -----------------------------------------------------------------------------
       
   241 void CPresenceCacheBuddyInfo::SetStatusMessageL( 
       
   242     const TDesC& aStatusMessage )
       
   243     {
       
   244     DoSet16BitValueL( NPresenceCacheFieldName::KStatusMessage, aStatusMessage );
       
   245     }
       
   246 
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 TPtrC CPresenceCacheBuddyInfo::StatusMessage(  )
       
   250     {
       
   251     return DoGet16BitValue( NPresenceCacheFieldName::KStatusMessage );
       
   252     }
       
   253     
       
   254 // -----------------------------------------------------------------------------
       
   255 void CPresenceCacheBuddyInfo::SetAnyFieldL( 
       
   256     const TDesC16& aKey,
       
   257     const TDesC8& aValue )
       
   258     {
       
   259     if( NPresenceCacheFieldName::KExpiry().CompareF( aKey ) == 0 )
       
   260         {
       
   261         TPckg<TInt64> expiryPck( iExpiryTime );
       
   262         expiryPck.Copy( aValue );
       
   263         // write also to key-value map
       
   264         }
       
   265     
       
   266     // Remove old values first
       
   267     RemoveField( aKey );    
       
   268     
       
   269     HBufC8* valueBuffer = aValue.AllocLC();      
       
   270     HBufC* keyBuffer = aKey.AllocLC();  
       
   271     
       
   272     TInt insertPos = iIds.Find(0);
       
   273     if ( insertPos < 0 )
       
   274         {
       
   275         insertPos = iIds.Count();
       
   276         iIds.Append( keyBuffer );
       
   277         iValues.Append( valueBuffer );
       
   278         }
       
   279     else
       
   280         {
       
   281         iIds[insertPos] = keyBuffer;
       
   282         iValues[insertPos] = valueBuffer;
       
   283         }
       
   284     
       
   285     iHashMap.InsertL( keyBuffer, insertPos  ); 
       
   286     
       
   287     CleanupStack::Pop( keyBuffer );
       
   288     CleanupStack::Pop( valueBuffer );    
       
   289     }
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 void CPresenceCacheBuddyInfo::SetAnyFieldPtrL( 
       
   293     HBufC16* aKey,
       
   294     HBufC8* aValue )
       
   295     {  
       
   296     if ( !aKey )
       
   297         {
       
   298         User::Leave( KErrArgument );
       
   299         }
       
   300     // Remove old values first
       
   301     RemoveField( aKey->Des() );
       
   302          
       
   303     TInt insertPos = iIds.Find(0);
       
   304     if ( insertPos < 0 )
       
   305         {
       
   306         insertPos = iIds.Count();
       
   307         iIds.Append( aKey );
       
   308         iValues.Append( aValue );
       
   309         }
       
   310     else
       
   311         {
       
   312         iIds[insertPos] = aKey;
       
   313         iValues[insertPos] = aValue;
       
   314         }
       
   315     iHashMap.InsertL( aKey, insertPos  );       
       
   316     }
       
   317     
       
   318 // -----------------------------------------------------------------------------
       
   319 TPtrC8 CPresenceCacheBuddyInfo::GetAnyField( 
       
   320     const TDesC16& aKey )
       
   321     {
       
   322     HBufC8* val = NULL;    
       
   323     TInt* pos = iHashMap.Find( STRCNST( aKey ) );
       
   324     if ( pos )
       
   325         {       
       
   326         val = iValues[*pos];        
       
   327         } 
       
   328     return val ? val->Des() : TPtrC8();    
       
   329     }
       
   330     
       
   331 // -----------------------------------------------------------------------------
       
   332 void CPresenceCacheBuddyInfo::GetFieldKeysL( 
       
   333     CDesCArrayFlat& aKeys )
       
   334     {
       
   335     aKeys.Reset();
       
   336     THashMapIter<TDesC*, TInt> iter(iHashMap);    
       
   337     TDesC* const* nextKey = iter.NextKey();
       
   338     while (nextKey)
       
   339         {
       
   340         aKeys.AppendL( **nextKey );
       
   341         nextKey = iter.NextKey();
       
   342         }    
       
   343     }
       
   344                                                                           
       
   345 // -----------------------------------------------------------------------------
       
   346 TBool CPresenceCacheBuddyInfo::EqualsIdentity(
       
   347     const MPresenceBuddyInfo2& aOtherInstance ) const
       
   348     {
       
   349     TBool same = ETrue;
       
   350 
       
   351     TBool x;
       
   352     x = 0 == BuddyId().CompareF( aOtherInstance.BuddyId() );
       
   353     same &= x;
       
   354 
       
   355     return same;
       
   356     }  
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 void CPresenceCacheBuddyInfo::ExternalizeL( RWriteStream& aStream ) const
       
   360     {
       
   361     aStream.WriteInt32L( iAvailability ); 
       
   362     ExternalizeFieldL( BuddyId(), aStream );
       
   363            
       
   364     // externalize expiry time
       
   365     TUint32 time_high = I64HIGH( iExpiryTime );
       
   366     TUint32 time_low = I64LOW( iExpiryTime );
       
   367     aStream.WriteUint32L(time_high);
       
   368     aStream.WriteUint32L(time_low);
       
   369     
       
   370     TInt count = iIds.Count();
       
   371     aStream.WriteInt32L( count );     
       
   372     for ( TInt i=0; i < count; i++ )
       
   373         {
       
   374         if ( iIds[i] )
       
   375             {
       
   376             TPtrC key = iIds[i]->Des();
       
   377             TPtrC8 value = iValues[i] ? iValues[i]->Des() : TPtrC8();        
       
   378             ExternalizeFieldL( key, aStream );        
       
   379             ExternalizeFieldL( value, aStream );             
       
   380             }
       
   381         else
       
   382             {
       
   383             // nothing to insert, special case if replacement has been failed earlier
       
   384             }
       
   385         }
       
   386     } 
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 void CPresenceCacheBuddyInfo::InternalizeL( RReadStream& aStream )
       
   390     {
       
   391     iAvailability = (MPresenceBuddyInfo2::TAvailabilityValues)aStream.ReadInt32L( ); 
       
   392     
       
   393     HBufC* temp  = InternalizeFieldL( aStream );
       
   394     iBuddyId = temp;
       
   395        
       
   396     iIds.ResetAndDestroy();
       
   397     iValues.ResetAndDestroy();
       
   398     
       
   399     HBufC* key = NULL;
       
   400     HBufC8* value = NULL;
       
   401 
       
   402     // internalize expiry time
       
   403     TUint32 time_high = aStream.ReadUint32L();
       
   404     TUint32 time_low = aStream.ReadUint32L();
       
   405     iExpiryTime = MAKE_TINT64( time_high, time_low );
       
   406     
       
   407     TInt count = aStream.ReadInt32L( );    
       
   408     for ( TInt i=0; i < count; i++ )
       
   409         {
       
   410         key = InternalizeFieldL( aStream ); 
       
   411         
       
   412         CleanupStack::PushL( key );
       
   413         value = InternalizeField8L( aStream );
       
   414         CleanupStack::Pop( key );          
       
   415         // This takes ownership anyway.
       
   416         SetAnyFieldPtrL( key, value );                       
       
   417         }      
       
   418     } 
       
   419 
       
   420 // -----------------------------------------------------------------------------
       
   421 void CPresenceCacheBuddyInfo::ExternalizeFieldL( const TDesC& aData, RWriteStream& aStream ) const
       
   422     {
       
   423     TInt32 length = aData.Length();    
       
   424     aStream.WriteInt32L( length  );
       
   425     aStream.WriteL( aData, length );
       
   426     }
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 void CPresenceCacheBuddyInfo::ExternalizeFieldL( const TDesC8& aData, RWriteStream& aStream ) const
       
   430     {
       
   431     TInt32 length = aData.Length();    
       
   432     aStream.WriteInt32L( length  );
       
   433     aStream.WriteL( aData, length );
       
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 HBufC* CPresenceCacheBuddyInfo::InternalizeFieldL( RReadStream& aStream )
       
   438     {
       
   439     HBufC16* temp = NULL;    
       
   440     TInt32 length = aStream.ReadInt32L();
       
   441     if ( length > 0 )
       
   442         { 
       
   443         temp = HBufC::NewLC( length);
       
   444         TPtr stringPtr = temp->Des(); 
       
   445         aStream.ReadL( stringPtr, length );
       
   446         CleanupStack::Pop( temp );
       
   447         } 
       
   448     else
       
   449         {
       
   450         }
       
   451     return temp;
       
   452     }
       
   453 
       
   454 // -----------------------------------------------------------------------------
       
   455 HBufC8* CPresenceCacheBuddyInfo::InternalizeField8L( RReadStream& aStream )
       
   456     {
       
   457     HBufC8* temp = NULL;    
       
   458     TInt32 length = aStream.ReadInt32L();
       
   459     if ( length > 0 )
       
   460         { 
       
   461         temp = HBufC8::NewLC( length);
       
   462         TPtr8 stringPtr = temp->Des(); 
       
   463         aStream.ReadL( stringPtr, length );
       
   464         CleanupStack::Pop( temp );
       
   465         } 
       
   466     else
       
   467         {
       
   468         }
       
   469     return temp;
       
   470     }
       
   471 
       
   472