natfw/tsrc/natfwtestconsoles/natfwtestconsole/src/datadepository.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <centralrepository.h>
       
    22 
       
    23 #include "datadepository.h"
       
    24 
       
    25 
       
    26 CDataDepository::CDataDepository( CRepository* aRepository )
       
    27     {
       
    28     iRepository = aRepository;
       
    29     }
       
    30 
       
    31 CDataDepository::~CDataDepository( )
       
    32     {
       
    33     }
       
    34 
       
    35 CDataDepository* CDataDepository::NewL( CRepository* aRepository )
       
    36     {
       
    37     CDataDepository* self = CDataDepository::NewLC( aRepository );
       
    38     CleanupStack::Pop(self);
       
    39     return self;
       
    40     }
       
    41 
       
    42 CDataDepository* CDataDepository::NewLC( CRepository* aRepository )
       
    43     {
       
    44     CDataDepository* self = new( ELeave ) CDataDepository( aRepository );
       
    45     CleanupStack::PushL( self );
       
    46     //self->ConstructL( );
       
    47     return self;
       
    48     }
       
    49 
       
    50 
       
    51 TUint32 CDataDepository::CreateNewTableKeyL( TUint aTableMask, TUint32 aFieldMask ) const
       
    52     {
       
    53     const TInt KReserved = 1;
       
    54     TUint32 newKey = 0;
       
    55     RArray< TUint32 > keys;
       
    56     CleanupClosePushL( keys );
       
    57     TInt err = iRepository->FindL( aTableMask,
       
    58                                    aFieldMask,
       
    59                                    keys );
       
    60     TInt keyCount = keys.Count( );
       
    61     
       
    62     if ( err == KErrNotFound )
       
    63         {
       
    64         newKey = aFieldMask + 1;
       
    65         }
       
    66     else
       
    67         {
       
    68         User::LeaveIfError( err );
       
    69         if ( keyCount == 0 )
       
    70             {
       
    71             newKey = aFieldMask + 1;
       
    72             }
       
    73         else
       
    74             {
       
    75             // Find the biggest key and increment it by one
       
    76             keys.SortUnsigned( );
       
    77             TUint32 maxKey = aTableMask ^ keys[ keyCount - 1 ];  
       
    78             newKey = aFieldMask + 1 + maxKey;
       
    79             }
       
    80         }
       
    81     newKey |= aFieldMask; 
       
    82     newKey ^= aFieldMask;
       
    83     
       
    84     User::LeaveIfError( iRepository->Create( aTableMask | newKey, KReserved ) );
       
    85     CleanupStack::PopAndDestroy( &keys );
       
    86     return newKey;
       
    87     }
       
    88     
       
    89 void CDataDepository::StoreL( const TUint32 aKey, const TInt aData )
       
    90     {
       
    91     User::LeaveIfError(
       
    92         iRepository->StartTransaction(
       
    93             CRepository::EConcurrentReadWriteTransaction ) );
       
    94     iRepository->CleanupCancelTransactionPushL( );
       
    95 
       
    96     TInt tmp = 0;
       
    97     if ( iRepository->Get( aKey,tmp ) == KErrNone )
       
    98         {
       
    99         // Update existing value
       
   100         User::LeaveIfError( iRepository->Set( aKey, aData ) );
       
   101         }
       
   102     else
       
   103         {
       
   104         // Create new value
       
   105         User::LeaveIfError( iRepository->Create( aKey, aData ) );
       
   106         } 
       
   107     TUint32 dummy;
       
   108     User::LeaveIfError( iRepository->CommitTransaction( dummy ) );
       
   109     CleanupStack::Pop( ); // transaction
       
   110     }   
       
   111         
       
   112 void CDataDepository::StoreL( const TUint32 aKey, const TDesC8& aData )
       
   113     {
       
   114     User::LeaveIfError(
       
   115             iRepository->StartTransaction(
       
   116                 CRepository::EConcurrentReadWriteTransaction ) );
       
   117             
       
   118     iRepository->CleanupCancelTransactionPushL( );
       
   119     
       
   120     TBuf8< 1 > tmp;
       
   121     TInt err = iRepository->Get( aKey,tmp );
       
   122     if ( err == KErrNone || err == KErrOverflow )
       
   123         {
       
   124         // Update existing value
       
   125         User::LeaveIfError( iRepository->Set( aKey, aData ) );
       
   126         }
       
   127     else
       
   128         {
       
   129         // Create new value
       
   130         User::LeaveIfError( iRepository->Create( aKey, aData ) );
       
   131         }
       
   132     TUint32 dummy;
       
   133     User::LeaveIfError( iRepository->CommitTransaction( dummy ) );
       
   134     CleanupStack::Pop( ); // transaction
       
   135     }
       
   136             
       
   137 TInt CDataDepository::Read( const TUint32 aKey, TInt& aData )
       
   138     {
       
   139     TInt err = KErrNotFound;
       
   140     if ( iRepository->Get( aKey, aData ) == KErrNone )
       
   141         {
       
   142         err = KErrNone;
       
   143         }
       
   144     return err;
       
   145     }
       
   146     
       
   147 TInt CDataDepository::ReadL( const TUint32 aKey, HBufC8** aData )
       
   148     {
       
   149     TInt err = KErrNotFound;
       
   150     TBuf8< 1 > tmp;
       
   151     TInt actualLength = 0;
       
   152     TInt status = iRepository->Get( aKey,tmp,actualLength );
       
   153                 
       
   154     if ( ( status == KErrNone || status == KErrOverflow) && ( actualLength > 0 ) )
       
   155         {
       
   156         err = KErrNone;
       
   157         HBufC8* buf = HBufC8::NewL( actualLength );
       
   158         CleanupStack::PushL( buf );
       
   159         TPtr8 ptr( buf->Des( ) );
       
   160         User::LeaveIfError( iRepository->Get( aKey,ptr ) );
       
   161         *aData = buf; 
       
   162         CleanupStack::Pop( buf );
       
   163         }
       
   164     else
       
   165         {
       
   166         *aData = KNullDesC8( ).AllocL( );
       
   167         }
       
   168     return err;    
       
   169     }       
       
   170 
       
   171 TInt CDataDepository::EraseL( TUint32 aTableMask, TUint32 aFieldTypeMask )
       
   172     {
       
   173     //find KDomain names
       
   174     RArray< TUint32 > keys;
       
   175     CleanupClosePushL( keys );
       
   176     		
       
   177     TInt err = iRepository->FindL( aTableMask,
       
   178                                    aFieldTypeMask,
       
   179                                    keys );
       
   180     if( err == KErrNone ) 
       
   181         {
       
   182         TInt count = keys.Count( );
       
   183         for ( TInt i = 0; i < count; i++ )
       
   184             {
       
   185             //delete setting
       
   186             iRepository->Delete( keys[ i ] );	
       
   187             }
       
   188         }
       
   189     CleanupStack::PopAndDestroy( &keys );
       
   190     return err;
       
   191     }