psmservices/psmserver/src/engine/psmstorage.cpp
changeset 0 4e1aa6a622a0
child 7 1a73e8f1b64d
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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:  PSM storage for backups and configs
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "psmstorage.h"
       
    20 #include "psmstoragedefines.h"
       
    21 #include "psmtrace.h"
       
    22 
       
    23 // -----------------------------------------------------------------------------
       
    24 // CPsmStorage::CPsmStorage
       
    25 // C++ default constructor can NOT contain any code, that
       
    26 // might leave.
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 CPsmStorage::CPsmStorage( TPsmsrvMode aMode, RFs& aFile ) :
       
    30     iFileSession( aFile ),
       
    31     iMode( aMode )
       
    32     {
       
    33     }
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CPsmStorage::ConstructL
       
    37 // Symbian 2nd phase constructor can leave.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 void CPsmStorage::ConstructL()
       
    41     {
       
    42     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::ConstructL()" ) ) );
       
    43     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::ConstructL - return" ) ) );
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // Destructor
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CPsmStorage::~CPsmStorage()
       
    51     {
       
    52     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::~CPsmStorage()" ) ) );
       
    53 
       
    54     if( iStorageFile.NotNull() )
       
    55         {
       
    56         iStorageFile.Close( );
       
    57         }
       
    58     iStorageParser.Close( );
       
    59     iDOMImpl.Close( );    
       
    60 
       
    61     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::~CPsmStorage - return") ) );
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CPsmBackupStorage::InitRootElementsL
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CPsmStorage::InitRootElementsL()
       
    69     {
       
    70     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::InitRootElementsL()" ) ) );
       
    71     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::InitRootElementsL() - return" ) ) );
       
    72     }
       
    73 
       
    74 void CPsmStorage::BackupConfigurationL( const RConfigInfoArray& /*aConfigArray*/ )
       
    75     {
       
    76     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::BackupConfigurationL()" ) ) );
       
    77     User::Leave( KErrNotSupported );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CPsmStorage::CloseStorageL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CPsmStorage::CloseStorageL()
       
    85     {
       
    86     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::CloseStorageL()" ) ) );
       
    87 
       
    88     // Cleanup storage elements
       
    89     if( iStorageFile.NotNull() )
       
    90         {
       
    91         iStorageFile.Close( );
       
    92         }
       
    93 
       
    94     iStorageParser.Close( );
       
    95     iDOMImpl.Close( );
       
    96         
       
    97     // Zero storage UID
       
    98     iStorageUid = 0;
       
    99 
       
   100     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::CloseStorageL() - return" ) ) );
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CPsmStorage::FindConfigurationSet
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 TXmlEngElement CPsmStorage::FindConfigurationSetL( const TInt aMode )
       
   108     {
       
   109     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::FindConfigurationSet()" ) ) );
       
   110 
       
   111     // Search correct settings for aUid
       
   112     TBool setFound( EFalse );
       
   113     TXmlEngElement settings;
       
   114     RXmlEngNodeList<TXmlEngElement> nodeList;   
       
   115 
       
   116     if ( iStorageRoot.NotNull() )
       
   117         {
       
   118         iStorageRoot.GetElementsByTagNameL(nodeList, KPsmPluginGroupTag);
       
   119         settings = nodeList.Next().AsElement();
       
   120         }
       
   121     else
       
   122         {
       
   123         COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::FindConfigurationSet() - Storage not initialized! LEAVE" ) ) );
       
   124         User::Leave( KErrNotFound );
       
   125         }
       
   126     
       
   127     // Loop until correct set is found
       
   128     while ( settings.NotNull() && !setFound )
       
   129         {
       
   130         // Read mode from storage
       
   131         TInt mode = GetAttributeIntValueL( settings, KPsmPluginGroupAttribute );
       
   132 
       
   133         if ( aMode == mode )
       
   134             {
       
   135             // Set found, no need to loop anumore
       
   136             setFound = ETrue;
       
   137             }
       
   138         else
       
   139             {
       
   140             // Set not found, continue loop with next config set
       
   141             settings = nodeList.Next().AsElement();            
       
   142             }
       
   143         }
       
   144 
       
   145     if ( !setFound )
       
   146         {
       
   147         COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::FindConfigurationSet() - Mode not found: %i, LEAVE" ), aMode ) );
       
   148         User::Leave( KErrNotFound );
       
   149         }
       
   150 
       
   151     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::FindConfigurationSet() - return" ) ) );
       
   152     return settings;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CPsmStorage::CreateConfigArrayL
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CPsmStorage::CreateConfigArrayL( RConfigInfoArray& aPsmConfigArray, const TInt aMode )
       
   160     {
       
   161     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::CreateConfigArrayL()" ) ) );
       
   162     // Read configuration information from config files and generate config array    
       
   163 
       
   164     // Search correct settings for current mode
       
   165     TXmlEngElement settings = FindConfigurationSetL( aMode );
       
   166 
       
   167     // Get first setElemens
       
   168     RXmlEngNodeList<TXmlEngElement> nodeList;
       
   169     settings.GetElementsByTagNameL(nodeList, KPsmSetItem);
       
   170     TXmlEngElement setElement = nodeList.Next().AsElement();
       
   171 
       
   172     // Loop through all set elements and update values to array
       
   173     while( setElement.NotNull() )
       
   174         {
       
   175         TPsmsrvConfigInfo configInfo;
       
   176         configInfo.iConfigId = GetAttributeIntValueL( setElement, KPsmSetItemKey );
       
   177         configInfo.iConfigType = (TPsmsrvConfigTypes)GetAttributeIntValueL( setElement,
       
   178                                                                            KPsmSetItemType );
       
   179         switch ( configInfo.iConfigType )
       
   180             {
       
   181             case EConfigTypeInt:
       
   182                 {
       
   183                 configInfo.iIntValue = 
       
   184                     GetAttributeIntValueL( setElement, KPsmSetItemValue );
       
   185                 break;
       
   186                 }
       
   187             case EConfigTypeReal:
       
   188                 {
       
   189                 configInfo.iRealValue = 
       
   190                     GetAttributeRealValueL( setElement, KPsmSetItemValue );
       
   191                 break;
       
   192                 }
       
   193             case EConfigTypeBuf:
       
   194                 {
       
   195                 GetAttributeStrValueL( setElement,
       
   196                                        KPsmSetItemValue,
       
   197                                        configInfo.iDesValue );
       
   198                 break;
       
   199                 }
       
   200             default:
       
   201                 COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::CreateConfigArrayL() - Unknown type" ) ) );
       
   202             }
       
   203 
       
   204         // Add info to list
       
   205         aPsmConfigArray.Append( configInfo );
       
   206 
       
   207         // handle next element set
       
   208         setElement = nodeList.Next().AsElement();
       
   209         }
       
   210 
       
   211     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::CreateConfigArrayL() - return" ) ) );
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CPsmStorage::GetConfigurationL
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CPsmStorage::GetConfigurationL( RConfigInfoArray& aConfigArray )
       
   219     {
       
   220     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetConfigurationL()" ) ) );
       
   221 
       
   222     // This is used for getting PSM configuration and for restoring original settings
       
   223     const TInt configCount( aConfigArray.Count() );
       
   224 
       
   225     // Search correct settings for current mode
       
   226     TXmlEngElement settings = FindConfigurationSetL( iMode );    
       
   227 
       
   228     // Get first setElemens
       
   229     RXmlEngNodeList<TXmlEngElement> nodeList;
       
   230     settings.GetElementsByTagNameL(nodeList, KPsmSetItem);
       
   231     TXmlEngElement setElement = nodeList.Next().AsElement();
       
   232 
       
   233     // Loop through all set elements and update values to array
       
   234     while( setElement.NotNull() )
       
   235         {
       
   236         const TInt setKey = GetAttributeIntValueL( setElement, KPsmSetItemKey );
       
   237         const TInt setType = GetAttributeIntValueL( setElement, KPsmSetItemType );
       
   238         const TInt blocked = GetAttributeIntValueL( setElement, KPsmSetItemBlock );
       
   239 
       
   240         TBool configUpdated( EFalse );
       
   241 
       
   242         // Loop configurations and update values to aConfigArray (do not replace, just update value)
       
   243         // If some values are not found, they remains the same
       
   244         for ( TInt i = 0; i < configCount && !configUpdated; i++ )
       
   245             {
       
   246             TPsmsrvConfigInfo& configInfo = aConfigArray[i];
       
   247 
       
   248             // check keys
       
   249             if ( setKey == configInfo.iConfigId )
       
   250                 {
       
   251                 // Add blocked information
       
   252                 configInfo.iBlocked = blocked;
       
   253 
       
   254                 // correct config found, update value
       
   255                 switch ( setType )
       
   256                     {
       
   257                     case EConfigTypeInt:
       
   258                         {
       
   259                         configInfo.iIntValue = 
       
   260                             GetAttributeIntValueL( setElement, KPsmSetItemValue );                        
       
   261                         break;
       
   262                         }
       
   263                     case EConfigTypeReal:
       
   264                         {
       
   265                         configInfo.iRealValue = 
       
   266                             GetAttributeRealValueL( setElement, KPsmSetItemValue );
       
   267                         break;
       
   268                         }
       
   269                     case EConfigTypeBuf:
       
   270                         {
       
   271                         GetAttributeStrValueL( setElement,
       
   272                                                KPsmSetItemValue,
       
   273                                                configInfo.iDesValue );
       
   274                         break;
       
   275                         }
       
   276                     default:
       
   277                         {
       
   278                         COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetConfigurationL() - Unknown type LEAVE" ) ) );
       
   279                         User::Leave( KErrUnknown );
       
   280                         }
       
   281                     }
       
   282                 // No need to loop config array anymore with this set element
       
   283                 configUpdated = ETrue;
       
   284                 }
       
   285             }
       
   286         // handle next element set
       
   287         setElement = nodeList.Next().AsElement();
       
   288         }
       
   289 
       
   290     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetConfigurationL() - return" ) ) );
       
   291     }
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // CPsmStorage::GetAttributeIntValueL
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 TInt CPsmStorage::GetAttributeIntValueL( const TXmlEngElement& aElement, const TDesC8& aAttrib ) const
       
   298     {
       
   299     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeIntValueL()" ) ) );
       
   300     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   301 
       
   302     TInt intValue( 0 );
       
   303 
       
   304     // Check availability
       
   305     if ( aElement.AttributeNodeL( aAttrib ).NotNull() )
       
   306         {
       
   307         // Read attribute value
       
   308         TPtrC8 ptr = aElement.AttributeValueL( aAttrib );
       
   309 
       
   310         // Cast literal value into TInt
       
   311         TLex8 lexValue( ptr );
       
   312         lexValue.Val( intValue );
       
   313         }
       
   314 
       
   315     COMPONENT_TRACE( ( _L( "  IntValue: %i" ), intValue ) );
       
   316     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeIntValueL() - return" ) ) );
       
   317     return intValue;
       
   318     }
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // CPsmStorage::GetAttributeStrValueL
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CPsmStorage::GetAttributeStrValueL(
       
   325     const TXmlEngElement& aElement, const TDesC8& aAttrib, TDes8& aTarget ) const
       
   326     {
       
   327     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeStrValueL()" ) ) );
       
   328     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   329 
       
   330     // Check availability
       
   331     if ( aElement.AttributeNodeL( aAttrib ).NotNull() )
       
   332         {
       
   333         aTarget = aElement.AttributeValueL( aAttrib );
       
   334         }
       
   335 
       
   336     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeStrValueL() - return" ) ) );
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // CPsmStorage::GetAttributeHexValueL
       
   341 // ---------------------------------------------------------------------------
       
   342 //
       
   343 TUint CPsmStorage::GetAttributeHexValueL( const TXmlEngElement& aElement, const TDesC8& aAttrib ) const
       
   344     {
       
   345     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeHexValueL()" ) ) );
       
   346     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   347 
       
   348     TUint32 hexValue( 0 );
       
   349 
       
   350     // Check availability
       
   351     if ( aElement.AttributeNodeL( aAttrib ).NotNull() )
       
   352         {
       
   353         // Read attribute value
       
   354         TPtrC8 ptr = aElement.AttributeValueL( aAttrib );
       
   355 
       
   356         // Get bounded value and cast it into TUint32 (hex)
       
   357         TRadix radix( EHex );
       
   358         TUint limit( 0xFFFFFFFF );
       
   359 
       
   360         // Append string into Lex and skip first two characters, 0x
       
   361         TLex8 lexValue( ptr );
       
   362         lexValue.Inc( 2 );
       
   363 
       
   364         // Read value
       
   365         lexValue.BoundedVal( hexValue, radix, limit );
       
   366         }
       
   367 
       
   368     COMPONENT_TRACE( ( _L( "  HexValue: %x" ), hexValue ) );
       
   369     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeHexValueL() - return" ) ) );
       
   370     return hexValue;
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // CPsmStorage::GetAttributeRealValueL
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 TReal CPsmStorage::GetAttributeRealValueL( const TXmlEngElement& aElement, const TDesC8& aAttrib ) const
       
   378     {
       
   379     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeRealValueL()" ) ) );
       
   380     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   381 
       
   382     TReal realValue( 0 );
       
   383 
       
   384     // Check availability
       
   385     if ( aElement.AttributeNodeL( aAttrib ).NotNull() )
       
   386         {
       
   387         // Read attribute value
       
   388         TPtrC8 ptr = aElement.AttributeValueL( aAttrib );
       
   389 
       
   390         // Cast literal value into TReal
       
   391         TLex8 lexValue( ptr );
       
   392         lexValue.Val( realValue );
       
   393         }
       
   394 
       
   395     COMPONENT_TRACE( ( _L( "  IntValue: %f" ), realValue ) );
       
   396     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::GetAttributeRealValueL() - return" ) ) );
       
   397     return realValue;
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CPsmStorage::SetAttributeIntValue
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CPsmStorage::SetAttributeValueL( 
       
   405     TXmlEngElement& aElement, const TDesC8& aAttrib, const TInt aValue ) const
       
   406     {
       
   407     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - Integer" ) ) );
       
   408     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );   
       
   409     COMPONENT_TRACE( ( _L( "  Value: %i" ), aValue ) );
       
   410 
       
   411     TBuf8<KPsmMaxAttribLength> attributeValue;
       
   412     attributeValue.Num( aValue, EDecimal );
       
   413     aElement.SetAttributeL( aAttrib, attributeValue );
       
   414 
       
   415     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - return" ) ) );
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------------------------
       
   419 // CPsmStorage::SetAttributeStrValue
       
   420 // ---------------------------------------------------------------------------
       
   421 //
       
   422 void CPsmStorage::SetAttributeValueL( 
       
   423     TXmlEngElement& aElement, const TDesC8& aAttrib, const TDesC8& aValue ) const
       
   424     {
       
   425     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - String" ) ) );
       
   426     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   427     COMPONENT_TRACE( ( _L( "  Value: %S" ), &aValue ) );
       
   428 
       
   429     aElement.SetAttributeL( aAttrib, aValue );
       
   430 
       
   431     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - return" ) ) );
       
   432     }
       
   433 
       
   434 // ---------------------------------------------------------------------------
       
   435 // CPsmStorage::SetAttributeRealValue
       
   436 // ---------------------------------------------------------------------------
       
   437 //
       
   438 void CPsmStorage::SetAttributeValueL( 
       
   439     TXmlEngElement& aElement, const TDesC8& aAttrib, const TReal aValue ) const
       
   440     {
       
   441     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - Real" ) ) );
       
   442     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );
       
   443     COMPONENT_TRACE( ( _L( "  Value: %f" ), aValue ) );
       
   444 
       
   445     TBuf8<KPsmMaxAttribLength> attributeValue;
       
   446     TRealFormat* realFormat = new ( ELeave ) TRealFormat();
       
   447     attributeValue.Num( aValue, *realFormat );
       
   448     delete realFormat;
       
   449     aElement.SetAttributeL( aAttrib, attributeValue );
       
   450 
       
   451     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeValueL() - return" ) ) );
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CPsmStorage::SetAttributeHexValue
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void CPsmStorage::SetAttributeHexValueL( 
       
   459     TXmlEngElement& aElement, const TDesC8& aAttrib, const TUint32 aValue ) const
       
   460     {
       
   461     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeHexValueL() - Hex" ) ) );
       
   462     COMPONENT_TRACE( ( _L( "  Attribute: %S" ), &aAttrib ) );   
       
   463     COMPONENT_TRACE( ( _L( "  Value: 0x%x" ), aValue ) );
       
   464 
       
   465     TBuf8<KPsmMaxAttribLength> attributeValue;
       
   466     attributeValue.Append( _L("0x") );
       
   467     attributeValue.AppendNumUC( aValue, EHex );
       
   468     aElement.SetAttributeL( aAttrib, attributeValue );
       
   469 
       
   470     COMPONENT_TRACE( ( _L( "PSM Server - CPsmStorage::SetAttributeHexValueL() - return" ) ) );
       
   471     }
       
   472 
       
   473 // End of file