wvsettings20/IMPSSrc/CIMPSSAPKeyValuePairs.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     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:  Container for individual key-value pairs
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include    "CIMPSSAPKeyValuePairs.h"
       
    20 #include    "CIMPSSAPKeyValuePair.h"
       
    21 #include    "CIMPSSAPLookupKeyValuePair.h"
       
    22 #include    "IMPSSAPSettingsStorePanics.h"
       
    23 #include    <e32std.h>
       
    24 
       
    25 //CONSTANTS
       
    26 const TInt KKeyValuePairGranurality = 5;
       
    27 
       
    28 
       
    29 // LOCAL CONSTANTS AND MACROS
       
    30 #define KEYWORD_ORDER TLinearOrder< CIMPSSAPKeyValuePair > ( CompareKeys )
       
    31 
       
    32 
       
    33 // ============================ LOCAL FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CompareKeys()
       
    37 // Comparison function for TLinearOrder
       
    38 //
       
    39 // @param aLhs The left hand side in comparison.
       
    40 // @param aRhs The right hand side in comparison.
       
    41 // @return Result from compare
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TInt CompareKeys( const CIMPSSAPKeyValuePair& aLhs, const CIMPSSAPKeyValuePair& aRhs )
       
    45     {
       
    46     return aLhs.Key().Compare( aRhs.Key() );
       
    47     }
       
    48 
       
    49 
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // SetValueToPairs()
       
    53 // Templated algorithm to set or update new value to list of key values.
       
    54 //
       
    55 // @param aPairs The list of key-value pairs.
       
    56 // @param aLookupKey The lookup key to use in searching.
       
    57 // @param aKey The key for which to assign the value.
       
    58 // @param aValue Templated data type to assign to key.
       
    59 // @return Error code.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 template< typename T >
       
    63 TInt SetValueToPairs( RPointerArray< CIMPSSAPKeyValuePair >& aPairs,
       
    64                       CIMPSSAPLookupKeyValuePair& aLookupKey,
       
    65                       const TDesC& aKey,
       
    66                       const T& aValue )
       
    67     {
       
    68     aLookupKey.SetLookupKey( aKey );
       
    69 
       
    70 
       
    71     TInt err = KErrNone;
       
    72     TInt index = aPairs.FindInOrder( &aLookupKey, KEYWORD_ORDER );
       
    73 
       
    74     if ( index != KErrNotFound )
       
    75         {
       
    76         //Update existing
       
    77         err = aPairs[ index ]->SetValue( aValue );
       
    78         }
       
    79 
       
    80     else
       
    81         {
       
    82         //create new
       
    83         CIMPSSAPKeyValuePair* newPair = CIMPSSAPKeyValuePair::New( aKey );
       
    84         if ( !newPair )
       
    85             {
       
    86             err = KErrNoMemory;
       
    87             }
       
    88         else
       
    89             {
       
    90             //Insert new
       
    91 
       
    92             err = newPair->SetValue( aValue );
       
    93             if ( err == KErrNone )
       
    94                 {
       
    95                 err = aPairs.InsertInOrderAllowRepeats( newPair, KEYWORD_ORDER );
       
    96                 }
       
    97 
       
    98             if ( err != KErrNone )
       
    99                 {
       
   100                 delete newPair;
       
   101                 }
       
   102 
       
   103             }
       
   104         }
       
   105     aLookupKey.ResetLookupKey();
       
   106     return err;
       
   107     }
       
   108 
       
   109 
       
   110 
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // GetValueFromPairs()
       
   114 //
       
   115 // Templated algorithm to get value from list of key values.
       
   116 //
       
   117 // @param aPairs The list of key-value pairs.
       
   118 // @param aLookupKey The lookup key to use in searching.
       
   119 // @param aKey The key for which to get the value.
       
   120 // @param aValue Templated data type to get.
       
   121 // @return Error code.
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 template< typename T >
       
   125 TInt GetValueFromPairs( const RPointerArray< CIMPSSAPKeyValuePair >& aPairs,
       
   126                         CIMPSSAPLookupKeyValuePair& aLookupKey,
       
   127                         const TDesC& aKey,
       
   128                         T& aValue )
       
   129     {
       
   130     aLookupKey.SetLookupKey( aKey );
       
   131 
       
   132 
       
   133     TInt index = aPairs.FindInOrder( &aLookupKey, KEYWORD_ORDER );
       
   134     if ( index != KErrNotFound )
       
   135         {
       
   136         index = aPairs[ index ]->GetValue( aValue );
       
   137         }
       
   138 
       
   139 
       
   140     aLookupKey.ResetLookupKey();
       
   141     return index;
       
   142     }
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 // ================= MEMBER FUNCTIONS =======================
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CIMPSSAPKeyValuePairs::NewL()
       
   151 // Two-phased constructor.
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 CIMPSSAPKeyValuePairs* CIMPSSAPKeyValuePairs::NewL()
       
   155     {
       
   156     CIMPSSAPKeyValuePairs* self = new ( ELeave ) CIMPSSAPKeyValuePairs;
       
   157     CleanupStack::PushL( self );
       
   158     self->ConstructL();
       
   159     CleanupStack::Pop(); //self
       
   160     return self;
       
   161     }
       
   162 
       
   163 
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CIMPSSAPKeyValuePairs::~CIMPSSAPKeyValuePairs()
       
   167 // Destructor
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 CIMPSSAPKeyValuePairs::~CIMPSSAPKeyValuePairs()
       
   171     {
       
   172     iPairs.ResetAndDestroy();
       
   173     delete iLookupKey;
       
   174     }
       
   175 
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CIMPSSAPKeyValuePairs::CIMPSSAPKeyValuePairs()
       
   179 // C++ default constructor can NOT contain any code, that
       
   180 // might leave.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 CIMPSSAPKeyValuePairs::CIMPSSAPKeyValuePairs()
       
   184         : iPairs( KKeyValuePairGranurality )
       
   185     {
       
   186     }
       
   187 
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CIMPSSAPKeyValuePairs::ConstructL)
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CIMPSSAPKeyValuePairs::ConstructL()
       
   194     {
       
   195     iLookupKey = CIMPSSAPLookupKeyValuePair::NewL();
       
   196     }
       
   197 
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CIMPSSAPKeyValuePairs::ObjectID()
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 TIMPSSapObjectID CIMPSSAPKeyValuePairs::ObjectID() const
       
   204     {
       
   205     return EIMPSSapObjKeyValuePairs;
       
   206     }
       
   207 
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CIMPSSAPKeyValuePairs::ObjectVersion()
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 TInt CIMPSSAPKeyValuePairs::ObjectVersion() const
       
   214     {
       
   215     return KIMPSSapObjVersionInitial;
       
   216     }
       
   217 
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CIMPSSAPKeyValuePairs::ExternalizeVersionDataL()
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CIMPSSAPKeyValuePairs::ExternalizeVersionDataL( TInt aVersion,
       
   224                                                      RWriteStream& aStream ) const
       
   225     {
       
   226     switch ( aVersion )
       
   227         {
       
   228         case  KIMPSSapObjVersionInitial:
       
   229             {
       
   230             //Phase 1 key value objects
       
   231 
       
   232             const TInt count = iPairs.Count();
       
   233             aStream.WriteInt32L( count );
       
   234             for ( TInt ii = 0; ii < count; ii++ )
       
   235                 {
       
   236                 iPairs[ ii ]->ExternalizeL( aStream );
       
   237                 }
       
   238             break;
       
   239             }
       
   240 
       
   241         default:
       
   242             {
       
   243             //Unknown version ID in externalization.
       
   244             Panic( EIMPSSetStoreUnknownObjectVersionExternalize );
       
   245             break;
       
   246             }
       
   247         };
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CIMPSSAPKeyValuePairs::InternalizeVersionDataL()
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CIMPSSAPKeyValuePairs::InternalizeVersionDataL( TInt aVersion,
       
   255                                                      RReadStream& aStream )
       
   256     {
       
   257     switch ( aVersion )
       
   258         {
       
   259         case  KIMPSSapObjVersionInitial:
       
   260             {
       
   261             //Phase 1 key value objects
       
   262             const TInt count = aStream.ReadInt32L();
       
   263             for ( TInt ii = 0; ii < count; ii++ )
       
   264                 {
       
   265                 CIMPSSAPKeyValuePair* pair = CIMPSSAPKeyValuePair::NewLC( aStream );
       
   266                 User::LeaveIfError( iPairs.Append( pair ) );
       
   267                 CleanupStack::Pop( pair );
       
   268                 }
       
   269 
       
   270             break;
       
   271             }
       
   272 
       
   273         default:
       
   274             {
       
   275             //Unknown version ID in internalization.
       
   276             Panic( EIMPSSetStoreUnknownObjectVersionInternalize );
       
   277             break;
       
   278             }
       
   279         };
       
   280     }
       
   281 
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CIMPSSAPKeyValuePairs::DataSize()
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 TInt CIMPSSAPKeyValuePairs::DataSize() const
       
   288     {
       
   289     //pairs count == 4 bytes
       
   290     TInt dataSize = 4;
       
   291     const TInt count = iPairs.Count();
       
   292     for ( TInt ii = 0; ii < count; ii++ )
       
   293         {
       
   294         dataSize += iPairs[ ii ]->DataSize();
       
   295         }
       
   296 
       
   297     return dataSize;
       
   298     }
       
   299 
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CIMPSSAPKeyValuePairs::Reset()
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 void CIMPSSAPKeyValuePairs::Reset()
       
   306     {
       
   307     iPairs.ResetAndDestroy();
       
   308     }
       
   309 
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CIMPSSAPKeyValuePairs::SetValueInt()
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 TInt CIMPSSAPKeyValuePairs::SetValueInt( const TDesC& aKey, TInt aValue )
       
   316     {
       
   317     return SetValueToPairs( iPairs, *iLookupKey, aKey, aValue );
       
   318     }
       
   319 
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CIMPSSAPKeyValuePairs::SetValueDesC8()
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 TInt CIMPSSAPKeyValuePairs::SetValueDesC8( const TDesC& aKey, const TDesC8& aValue )
       
   326     {
       
   327     return SetValueToPairs( iPairs, *iLookupKey, aKey, aValue );
       
   328     }
       
   329 
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CIMPSSAPKeyValuePairs::SetValueDesC16()
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 TInt CIMPSSAPKeyValuePairs::SetValueDesC16( const TDesC& aKey, const TDesC16& aValue )
       
   336     {
       
   337     return SetValueToPairs( iPairs, *iLookupKey, aKey, aValue );
       
   338     }
       
   339 
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CIMPSSAPKeyValuePairs::GetValueInt()
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 TInt CIMPSSAPKeyValuePairs::GetValueInt( const TDesC& aKey, TInt& aValue )
       
   346     {
       
   347     return GetValueFromPairs( iPairs, *iLookupKey, aKey, aValue );
       
   348     }
       
   349 
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CIMPSSAPKeyValuePairs::GetValueDesC8()
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 TInt CIMPSSAPKeyValuePairs::GetValueDesC8( const TDesC& aKey, TPtrC8& aValue )
       
   356     {
       
   357     return GetValueFromPairs( iPairs, *iLookupKey, aKey, aValue );
       
   358     }
       
   359 
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CIMPSSAPKeyValuePairs::GetValueDesC16()
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 TInt CIMPSSAPKeyValuePairs::GetValueDesC16( const TDesC& aKey, TPtrC16& aValue )
       
   366     {
       
   367     return GetValueFromPairs( iPairs, *iLookupKey, aKey, aValue );
       
   368     }
       
   369 
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CIMPSSAPKeyValuePairs::DeletePair()
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 TInt CIMPSSAPKeyValuePairs::DeletePair( const TDesC& aKey )
       
   376     {
       
   377 
       
   378     iLookupKey->SetLookupKey( aKey );
       
   379 
       
   380     TInt index = iPairs.FindInOrder( iLookupKey, KEYWORD_ORDER );
       
   381     if ( index != KErrNotFound )
       
   382         {
       
   383         delete iPairs[ index ];
       
   384         iPairs.Remove( index );
       
   385         index = KErrNone;
       
   386         }
       
   387 
       
   388     iLookupKey->ResetLookupKey();
       
   389 
       
   390     return index;
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CIMPSSAPKeyValuePairs::Pairs()
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 const RPointerArray< CIMPSSAPKeyValuePair >& CIMPSSAPKeyValuePairs::Pairs() const
       
   398     {
       
   399     return iPairs;
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CIMPSSAPKeyValuePairs::Pairs()
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 RPointerArray< CIMPSSAPKeyValuePair >& CIMPSSAPKeyValuePairs::Pairs()
       
   407     {
       
   408     return iPairs;
       
   409     }
       
   410 
       
   411 //  End of File
       
   412