uiservicetabsettings/src/cvimpstsettingskeyvaluepair.cpp
branchRCL_3
changeset 22 3104fc151679
parent 21 2b7283837edb
child 23 9a48e301e94b
equal deleted inserted replaced
21:2b7283837edb 22:3104fc151679
     1 /*
       
     2 * Copyright (c) 2004 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:  Key - value container.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include	<utf.h> // for unicode character conversions
       
    20 #include	<centralrepository.h>
       
    21 #include    <e32std.h>
       
    22 #include    "cvimpstsettingskeyvaluepair.h"
       
    23 #include    "vimpstsettingsstoredefinitions.h"
       
    24 #include    "vimpstsettingsstorecenrepuids.h"
       
    25 //MACROS 
       
    26 
       
    27 //Helper macro to check value type in data set
       
    28 #define REQUIRE_SET_VALUE_TYPE( aType ) \
       
    29     { \
       
    30     TBool _settable = ( iValueType == EValueTypeNone ) || \
       
    31                       ( iValueType == aType ); \
       
    32     if( !_settable )\
       
    33         {\
       
    34         return KErrGeneral;\
       
    35         }\
       
    36     } \
       
    37 
       
    38 //Helper macro to check value type in data get
       
    39 #define REQUIRE_GET_VALUE_TYPE( aType ) \
       
    40     { \
       
    41     if( iValueType == EValueTypeNone ) \
       
    42         { \
       
    43         return KErrNotFound; \
       
    44         } \
       
    45     TBool _readable = ( iValueType == aType ); \
       
    46     if( !_readable ) \
       
    47         { \
       
    48         return KErrGeneral; \
       
    49         } \
       
    50     } \
       
    51 
       
    52 
       
    53 // ================= MEMBER FUNCTIONS =======================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CVIMPSTSettingsKeyValuePair::New()
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CVIMPSTSettingsKeyValuePair* CVIMPSTSettingsKeyValuePair::New( const TDesC16& aKey )
       
    60     {
       
    61     //Non leaving two phased constructor!!!
       
    62     				 
       
    63     CVIMPSTSettingsKeyValuePair* self = new CVIMPSTSettingsKeyValuePair;	// CSI: 62 #
       
    64     if( self )
       
    65         {
       
    66         self->iKey = aKey.Alloc();
       
    67         if( !self->iKey )
       
    68             {
       
    69             delete self;
       
    70             self = NULL;
       
    71             }
       
    72         }
       
    73     
       
    74     return self;    
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CVIMPSTSettingsKeyValuePair::NewLC()
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CVIMPSTSettingsKeyValuePair* CVIMPSTSettingsKeyValuePair::NewLC( const TDesC& aKeyValuePairFlat )
       
    82     {
       
    83     CVIMPSTSettingsKeyValuePair* self = new (ELeave) CVIMPSTSettingsKeyValuePair;
       
    84     CleanupStack::PushL( self );
       
    85     self->ParseFlatKeyValuePairL( aKeyValuePairFlat );
       
    86     return self;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CVIMPSTSettingsKeyValuePair::~CVIMPSTSettingsKeyValuePair()
       
    91 // Destructor
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 CVIMPSTSettingsKeyValuePair::~CVIMPSTSettingsKeyValuePair()
       
    95     {
       
    96     Reset();
       
    97     }
       
    98 
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CVIMPSTSettingsKeyValuePair::CVIMPSTSettingsKeyValuePair()
       
   102 // C++ default constructor can NOT contain any code, that
       
   103 // might leave.
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 CVIMPSTSettingsKeyValuePair::CVIMPSTSettingsKeyValuePair()
       
   107     : iValueType( EValueTypeNone )
       
   108     {
       
   109     }
       
   110 
       
   111 
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CVIMPSTSettingsKeyValuePair::ConstructL()
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CVIMPSTSettingsKeyValuePair::ConstructL( RReadStream& aStream )
       
   118     {
       
   119     
       
   120     //Default data    
       
   121     TInt keyLength = aStream.ReadInt32L();
       
   122     iKey = HBufC::NewL( keyLength );
       
   123     TPtr keyPtr( iKey->Des() );
       
   124     aStream.ReadL( keyPtr, keyLength );
       
   125     
       
   126     iValueType = static_cast< TValueType > ( aStream.ReadInt8L() );
       
   127     switch( iValueType )
       
   128         {        
       
   129         case EValueTypeDesC16: 
       
   130             {
       
   131             TInt descLength = aStream.ReadInt32L();
       
   132             iBuf16 = HBufC16::NewL( descLength );
       
   133             TPtr16 descPtr16( iBuf16->Des() );
       
   134             aStream.ReadL( descPtr16, descLength );
       
   135             break;
       
   136             }
       
   137 
       
   138         //FLOW THROUGH
       
   139         case EValueTypeNone:
       
   140         default:
       
   141             {
       
   142             break;
       
   143             }
       
   144         };
       
   145     }
       
   146 
       
   147 
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CVIMPSTSettingsKeyValuePair::Reset()
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CVIMPSTSettingsKeyValuePair::Reset()
       
   154     {
       
   155     switch( iValueType )
       
   156         {
       
   157         case EValueTypeDesC16: 
       
   158             {
       
   159             delete iBuf16;
       
   160             iBuf16 = NULL;
       
   161             break;
       
   162             }
       
   163         
       
   164         //FLOW THROUGH       
       
   165         case EValueTypeNone:
       
   166         default:
       
   167             {
       
   168             break;
       
   169             }
       
   170         };
       
   171     
       
   172     iValueType = EValueTypeNone;
       
   173 
       
   174     delete iKey;
       
   175     iKey = NULL;
       
   176     }
       
   177 
       
   178 
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CVIMPSTUiSAPKeyValuePair::Key()
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 const TDesC& CVIMPSTSettingsKeyValuePair::Key() const
       
   185     {
       
   186     return *iKey;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CVIMPSTSettingsKeyValuePair::SetValue()
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 TInt CVIMPSTSettingsKeyValuePair::SetValue( const TDesC16& aValue )
       
   194     {
       
   195     REQUIRE_SET_VALUE_TYPE( EValueTypeDesC16 )
       
   196 
       
   197 	// Allocate memory for the new value
       
   198     HBufC16* tmp = aValue.Alloc();
       
   199     if( !tmp )
       
   200         {
       
   201         return KErrNoMemory;
       
   202         }   
       
   203 
       
   204  	// Swap pointers
       
   205     HBufC16* tmp2( NULL );
       
   206    	tmp2 = iBuf16;
       
   207     iBuf16 = tmp;
       
   208     iValueType = EValueTypeDesC16;
       
   209 	
       
   210 	TBool dataTooBig( IsDataTooBig() );
       
   211 	TInt err( KErrNone );
       
   212 
       
   213 	// Rollback if data too bog
       
   214 	if( dataTooBig )
       
   215 		{
       
   216 		iBuf16 = tmp2;
       
   217 		delete tmp;
       
   218 		err = KErrTooBig;
       
   219 		}
       
   220 	// Otherwise delete the old value
       
   221 	else
       
   222 		{
       
   223 		delete tmp2;
       
   224 		}	 	    
       
   225     return err;
       
   226     }
       
   227 
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CVIMPSTSettingsKeyValuePair::GetValue()
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt CVIMPSTSettingsKeyValuePair::GetValue( TPtrC16& aValue ) const
       
   234     {
       
   235     REQUIRE_GET_VALUE_TYPE( EValueTypeDesC16 )
       
   236 
       
   237     aValue.Set( *iBuf16 );
       
   238     return KErrNone;
       
   239     }
       
   240 	
       
   241 // -----------------------------------------------------------------------------
       
   242 // CVIMPSTSettingsKeyValuePair::KeyValuePairFlatLC()
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 
       
   246 HBufC* CVIMPSTSettingsKeyValuePair::KeyValuePairFlatLC()
       
   247 	{
       
   248 	
       
   249 	// reserve memory for 2 field separators
       
   250 	HBufC* flatBuf = HBufC::NewLC( NCentralRepositoryConstants::KMaxUnicodeStringLength );  
       
   251 	
       
   252 	TPtr flatBufPtr( flatBuf->Des() );
       
   253    	
       
   254     flatBufPtr.Append( *iKey );
       
   255     flatBufPtr.Append( KKeyValuePairFieldSeparator );
       
   256 	flatBufPtr.AppendNum( iValueType, EDecimal );
       
   257    	flatBufPtr.Append( KKeyValuePairFieldSeparator );
       
   258 		
       
   259 	switch( iValueType )
       
   260         {
       
   261         case EValueTypeDesC16: 
       
   262             {
       
   263           	flatBufPtr.Append( *iBuf16 );
       
   264             break;
       
   265             }
       
   266 
       
   267         //FLOW THROUGH
       
   268         case EValueTypeNone:
       
   269         default:
       
   270             {
       
   271             break;
       
   272             }
       
   273         };
       
   274 	return flatBuf;
       
   275 	}
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CVIMPSTSettingsKeyValuePair::IsDataTooBig()
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 TBool CVIMPSTSettingsKeyValuePair::IsDataTooBig() const
       
   282     {
       
   283     return ( DataSizeDec() > ( NCentralRepositoryConstants::KMaxUnicodeStringLength - 
       
   284                     2*KKeyValuePairFieldSeparator().Length() - 1) );
       
   285     }
       
   286 
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CVIMPSTSettingsKeyValuePair::DataSizeDec()
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 TInt CVIMPSTSettingsKeyValuePair::DataSizeDec() const
       
   293     {    
       
   294     TInt dataSize( iKey->Length() );
       
   295     
       
   296     switch( iValueType )
       
   297         {    
       
   298         case EValueTypeDesC16: 
       
   299             {
       
   300             dataSize += iBuf16->Length();
       
   301             break;
       
   302             }
       
   303 
       
   304         //FLOW THROUGH
       
   305         case EValueTypeNone:
       
   306         default:
       
   307             {
       
   308             break;
       
   309             }
       
   310         };
       
   311 
       
   312     return dataSize;
       
   313     }
       
   314 
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CVIMPSTSettingsKeyValuePair::ParseFlatKeyValuePairL()
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 
       
   321 void CVIMPSTSettingsKeyValuePair::ParseFlatKeyValuePairL( const TDesC& aKeyValuePairFlat )
       
   322     {
       
   323     TInt offset( aKeyValuePairFlat.Find( KKeyValuePairFieldSeparator ) );
       
   324     TInt offset2( aKeyValuePairFlat.Mid( offset + 
       
   325             KKeyValuePairFieldSeparator().Length() ).Find(
       
   326                     KKeyValuePairFieldSeparator ) 
       
   327     );
       
   328 
       
   329     if( ( offset == KErrNotFound ) || ( offset2 == KErrNotFound ) )
       
   330         {
       
   331         //Incorrect format, leave
       
   332         User::Leave( KErrNotFound );
       
   333         }
       
   334 
       
   335     TPtrC key( aKeyValuePairFlat.Left( offset ) );
       
   336 
       
   337     iKey = key.AllocL();
       
   338 
       
   339     TPtrC valueType( aKeyValuePairFlat.Mid( offset + 
       
   340             KKeyValuePairFieldSeparator().Length(), offset2 ) );
       
   341     TLex lexer( valueType );
       
   342     TInt valueTypeInt( 0 );
       
   343     User::LeaveIfError( lexer.Val( valueTypeInt ) ); 
       
   344     TPtrC value( aKeyValuePairFlat.Mid(offset + offset2 + 
       
   345             2*KKeyValuePairFieldSeparator().Length() ) ); // skip 2 separators
       
   346             switch( valueTypeInt )
       
   347                 {               
       
   348                 case EValueTypeDesC16:
       
   349                     {
       
   350                     User::LeaveIfError( SetValue( value ) );
       
   351                     break;
       
   352                     }
       
   353 
       
   354                     //FLOW THROUGH
       
   355                 case EValueTypeNone:
       
   356                 default:
       
   357                     {
       
   358                     break;
       
   359                     }
       
   360                 }
       
   361     }
       
   362 //  End of File
       
   363