natfw/natfwsettings/src/cnatfwcenrephandler.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 #include <unsafprotocolscrkeys.h>
       
    23 #include "cnatfwcenrephandler.h"
       
    24 #include "natsettingslogs.h"
       
    25 
       
    26 const TInt KAllowedDomainAndIapKeyCount = 1; 
       
    27 
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CNATFWCenRepHandler::CNATFWCenRepHandler
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CNATFWCenRepHandler::CNATFWCenRepHandler()
       
    36     {
       
    37     }
       
    38 
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CNATFWCenRepHandler::ConstructL
       
    42 // ---------------------------------------------------------------------------
       
    43 //    
       
    44 void CNATFWCenRepHandler::ConstructL( const TUid& aRepositoryUid )
       
    45     {
       
    46     iCenRep = CRepository::NewL( aRepositoryUid );
       
    47     }
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CNATFWCenRepHandler::NewL
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CNATFWCenRepHandler* CNATFWCenRepHandler::NewL( const TUid& aRepositoryUid )
       
    55     {
       
    56     __NATSETTINGS( "CNATFWCenRepHandler::NewL" )
       
    57     
       
    58     CNATFWCenRepHandler* self =
       
    59         new( ELeave ) CNATFWCenRepHandler();
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL( aRepositoryUid );
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CNATFWCenRepHandler::~CNATFWCenRepHandler
       
    69 //----------------------------------------------------------------------------
       
    70 //
       
    71 CNATFWCenRepHandler::~CNATFWCenRepHandler()
       
    72     {
       
    73     __NATSETTINGS( "CNATFWCenRepHandler::~CNATFWNatSettingsImpl" )
       
    74     
       
    75     delete iCenRep;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CNATFWCenRepHandler::ReadDomainSettingsKey
       
    81 // ---------------------------------------------------------------------------
       
    82 //  
       
    83 TInt CNATFWCenRepHandler::ReadDomainSettingsKey(
       
    84     const TDesC8& aDomain, TUint32& aDomainKey ) const
       
    85     {
       
    86     __NATSETTINGS( "CNATFWCenRepHandler::ReadDomainSettingsKeyL" )
       
    87     
       
    88     TInt err( 0 );
       
    89     RArray<TUint32> keys;
       
    90     
       
    91     TRAPD( systemErr, err = iCenRep->FindEqL( KUNSAFProtocolsDomainMask,
       
    92         KUNSAFProtocolsFieldTypeMask, aDomain, keys ) );
       
    93     
       
    94     if ( KErrNone != systemErr )
       
    95         {
       
    96         keys.Close();
       
    97         return systemErr;
       
    98         }
       
    99     
       
   100     TInt count = keys.Count();
       
   101     
       
   102     if ( KErrNone == err && KAllowedDomainAndIapKeyCount == count )
       
   103         {
       
   104         aDomainKey = KUNSAFProtocolsDomainMask ^ keys[0];
       
   105         }
       
   106     if ( count > KAllowedDomainAndIapKeyCount )
       
   107         {
       
   108         err = KErrCorrupt;
       
   109         }
       
   110     keys.Close();
       
   111     return err;
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CNATFWCenRepHandler::ReadIapSettingsKey
       
   117 // ---------------------------------------------------------------------------
       
   118 //  
       
   119 TInt CNATFWCenRepHandler::ReadIapSettingsKey(
       
   120     TInt aIapId, TUint32& aIapKey ) const
       
   121     {
       
   122     __NATSETTINGS( "CNATFWCenRepHandler::ReadIapSettingsKeyL" )
       
   123     
       
   124     TInt err( 0 );
       
   125     RArray<TUint32> keys;
       
   126     
       
   127     TRAPD( systemErr, err = iCenRep->FindEqL( KUNSAFProtocolsIAPIdMask,
       
   128         KUNSAFProtocolsFieldTypeMask, aIapId, keys ) );
       
   129     
       
   130     if ( KErrNone != systemErr )
       
   131         {
       
   132         keys.Close();
       
   133         return systemErr;
       
   134         }
       
   135     
       
   136     TInt count = keys.Count();
       
   137     
       
   138     if ( KErrNone == err && KAllowedDomainAndIapKeyCount == count )
       
   139         {
       
   140         aIapKey = KUNSAFProtocolsIAPIdMask ^ keys[0];
       
   141         }
       
   142     if ( count > KAllowedDomainAndIapKeyCount )
       
   143         {
       
   144         err = KErrCorrupt;
       
   145         }
       
   146     keys.Close();
       
   147     return err;
       
   148     }
       
   149 
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CNATFWCenRepHandler::FindServerKeys
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 TInt CNATFWCenRepHandler::FindServerKeys( TUint32 aPartialId,
       
   156     RArray<TUint32>& aFoundKeys ) const
       
   157     {
       
   158     const TUint32 KMaskForAllServerSettingsIds = 0xff000fff;
       
   159     
       
   160     TInt error( 0 );
       
   161     RArray<TUint32> serverKeys;
       
   162     
       
   163     TRAPD( systemErr, error = iCenRep->FindL( aPartialId,
       
   164         KMaskForAllServerSettingsIds, serverKeys ) );
       
   165     
       
   166     if ( KErrNone != systemErr )    
       
   167         {
       
   168         serverKeys.Close();
       
   169         return systemErr;
       
   170         }
       
   171     
       
   172     if ( KErrNone == error )
       
   173         {
       
   174         TInt count = serverKeys.Count();
       
   175 
       
   176         for ( TInt i( 0 ); i < count; i++ )
       
   177             {
       
   178             error = aFoundKeys.Append( serverKeys[i] & KUNSAFProtocolsSubKeyMask );
       
   179             }
       
   180         
       
   181         for ( TInt j( 0 ); j < aFoundKeys.Count(); j++ )
       
   182             {
       
   183             for ( TInt i( j + 1 ); i < aFoundKeys.Count(); )
       
   184                 {
       
   185                 if ( aFoundKeys[ i ] == aFoundKeys[ j ] )
       
   186                     {
       
   187                     aFoundKeys.Remove( i );
       
   188                     }
       
   189                 else
       
   190                     {
       
   191                     i++;
       
   192                     }
       
   193                 }
       
   194             }
       
   195         }
       
   196     serverKeys.Close();
       
   197     return error;
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CNATFWCenRepHandler::CreateNewKeyL
       
   203 // ---------------------------------------------------------------------------
       
   204 //  
       
   205 TUint32 CNATFWCenRepHandler::CreateNewKeyL(
       
   206     const TUint32 aSettingKeyMask, const TUint32 aField ) const
       
   207     {
       
   208     TUint32 newKey( 0 );
       
   209     RArray<TUint32> keys;
       
   210     CleanupClosePushL( keys );
       
   211     TInt err( iCenRep->FindL( aSettingKeyMask, aField, keys ) );
       
   212     TInt keyCount( keys.Count() );
       
   213     
       
   214     if ( KErrNotFound == err )
       
   215         {
       
   216         newKey = aField + 1;
       
   217         }
       
   218     else
       
   219         {
       
   220         User::LeaveIfError( err );
       
   221         if ( !keyCount )
       
   222             {
       
   223             newKey = aField + 1;
       
   224             }
       
   225         else
       
   226             {
       
   227             // Find the biggest key and increment it by one
       
   228             keys.SortUnsigned();
       
   229             TUint32 maxKey = aSettingKeyMask ^ keys[keyCount - 1];  
       
   230             newKey = aField + 1 + maxKey;
       
   231             }
       
   232         }
       
   233     newKey |= aField; 
       
   234     newKey ^= aField;
       
   235     CleanupStack::PopAndDestroy( &keys );
       
   236     return newKey;
       
   237     }
       
   238 
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // CNATFWCenRepHandler::ReadL
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 HBufC8* CNATFWCenRepHandler::ReadL( const TUint32& aKey ) const
       
   245     {
       
   246     HBufC8* buffer = NULL;
       
   247     TBuf8<1> tmp;
       
   248     TInt actualLength( 0 );
       
   249     TInt status = iCenRep->Get( aKey, tmp, actualLength );
       
   250     
       
   251     if ( ( KErrNone == status || KErrOverflow == status ) )
       
   252         {
       
   253         if( actualLength )
       
   254             {
       
   255             buffer = HBufC8::NewLC( actualLength );
       
   256             TPtr8 ptr( buffer->Des() );
       
   257             User::LeaveIfError( iCenRep->Get( aKey, ptr ) );
       
   258             CleanupStack::Pop( buffer );
       
   259             }
       
   260         else
       
   261             {
       
   262             buffer = KNullDesC8().AllocL();
       
   263             }
       
   264         }
       
   265     return buffer;
       
   266     }
       
   267 
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // CNATFWCenRepHandler::ReadBoolValue
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 TInt CNATFWCenRepHandler::ReadBoolValue(
       
   274     const TUint32& aKey, TBool& aKeyValue ) const
       
   275     {
       
   276     TInt err = KErrNotFound;
       
   277     TInt tmp( 0 );
       
   278     if ( KErrNone == iCenRep->Get( aKey, tmp ) )
       
   279         {
       
   280         aKeyValue = ( tmp != 0 );
       
   281         err = KErrNone;
       
   282         }
       
   283     return err;
       
   284     }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // CNATFWCenRepHandler::Read
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 TInt CNATFWCenRepHandler::Read(
       
   292     const TUint32& aKey, TUint& aKeyValue ) const
       
   293     {
       
   294     TInt err = KErrNotFound;
       
   295     TInt tmp;
       
   296     if ( KErrNone == iCenRep->Get( aKey, tmp ) )
       
   297         {
       
   298         aKeyValue = static_cast<TUint>( tmp );
       
   299         err = KErrNone;
       
   300         }
       
   301     return err;
       
   302     }
       
   303 
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // CNATFWCenRepHandler::Read
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 TInt CNATFWCenRepHandler::Read(
       
   310     const TUint32& aKey, TInt& aKeyValue ) const
       
   311     {
       
   312     TInt readKeyValue( 0 );
       
   313     if ( KErrNone == iCenRep->Get( aKey, readKeyValue ) )
       
   314         {
       
   315         aKeyValue = readKeyValue;
       
   316         }
       
   317     else
       
   318         {
       
   319         return KErrNotFound;
       
   320         }
       
   321     return KErrNone;
       
   322     }