psmservices/psmserver/src/engine/psmbackupstorage.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 "psmbackupstorage.h"
       
    20 #include "psmstoragedefines.h"
       
    21 #include "psmtrace.h"
       
    22 
       
    23 // -----------------------------------------------------------------------------
       
    24 // CPsmBackupStorage::NewL
       
    25 // Two-phased constructor.
       
    26 // -----------------------------------------------------------------------------
       
    27 //
       
    28 CPsmBackupStorage* CPsmBackupStorage::NewL( TPsmsrvMode aMode, RFs& aFile )
       
    29     {
       
    30     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::NewL()" ) ) );
       
    31 
       
    32     CPsmBackupStorage* self = CPsmBackupStorage::NewLC( aMode, aFile );
       
    33     CleanupStack::Pop( self );
       
    34 
       
    35     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::NewL - return 0x%x" ), self ) );
       
    36 
       
    37     return self;
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CPsmBackupStorage::NewLC
       
    42 // Two-phased constructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CPsmBackupStorage* CPsmBackupStorage::NewLC( TPsmsrvMode aMode, RFs& aFile )
       
    46     {
       
    47     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::NewLC()" ) ) );
       
    48 
       
    49     CPsmBackupStorage* self = new( ELeave ) CPsmBackupStorage( aMode, aFile );
       
    50 
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53 
       
    54     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::NewLC - return 0x%x" ), self ) );
       
    55 
       
    56     return self;
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CPsmBackupStorage::CPsmBackupStorage
       
    61 // C++ default constructor can NOT contain any code, that
       
    62 // might leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CPsmBackupStorage::CPsmBackupStorage( TPsmsrvMode aMode, RFs& aFile ) :
       
    66     CPsmStorage( aMode, aFile )
       
    67     {
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CPsmBackupStorage::ConstructL
       
    72 // Symbian 2nd phase constructor can leave.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void CPsmBackupStorage::ConstructL()
       
    76     {
       
    77     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ConstructL()" ) ) );
       
    78     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ConstructL - return" ) ) );
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // Destructor
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CPsmBackupStorage::~CPsmBackupStorage()
       
    86     {
       
    87     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::~CPsmBackupStorage()" ) ) );
       
    88 
       
    89     // No need to make new backup file at this point
       
    90     iChangesMade = EFalse;
       
    91 
       
    92     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::~CPsmBackupStorage - return") ) );
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CPsmBackupStorage::InitRootElementsL
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 void CPsmBackupStorage::InitRootElementsL()
       
   100     {
       
   101     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::InitRootElementsL()" ) ) );
       
   102    
       
   103     // First get document element
       
   104     if ( iStorageFile.NotNull() )
       
   105         {
       
   106         iStorageRoot = iStorageFile.DocumentElement();
       
   107 
       
   108         // Check is root found. If not, create new
       
   109         if ( iStorageRoot.IsNull() )
       
   110             {            
       
   111             iStorageRoot = iStorageFile.CreateDocumentElementL( KPsmBackupRootTag() );
       
   112             }
       
   113         }
       
   114 
       
   115     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::InitRootElementsL() - return" ) ) );
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CPsmBackupStorage::InitStorageL
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CPsmBackupStorage::InitStorageL( const TUint32 aStorageUid )
       
   123     {
       
   124     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::InitStorageL()" ) ) );
       
   125 
       
   126     iStorageUid = aStorageUid;
       
   127 
       
   128     // Check backup folder
       
   129     CheckBackupLocation();
       
   130 
       
   131     // Create storage file parser
       
   132     iDOMImpl.OpenL( );
       
   133     User::LeaveIfError( iStorageParser.Open( iDOMImpl ) );
       
   134 
       
   135     // Generate file folder+name+extension
       
   136     TBuf<KPsmMaxPathLength> storageFile( KNullDesC );
       
   137     BackupFileLocation( storageFile );
       
   138 
       
   139     TRAPD(err, iStorageFile = iStorageParser.ParseFileL( iFileSession, storageFile ));
       
   140 
       
   141     // Initialize PSM backup file
       
   142     if ( KErrNotFound == err )
       
   143         {
       
   144         // Backup file does not exist, create new
       
   145         iStorageFile.OpenL( iDOMImpl );        
       
   146         }
       
   147     else
       
   148         {
       
   149         // Leave if error
       
   150         User::LeaveIfError( err );
       
   151         }
       
   152     InitRootElementsL();
       
   153     iChangesMade = EFalse;
       
   154 
       
   155     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::InitStorageL() - return" ) ) );
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CPsmBackupStorage::CloseStorageL
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CPsmBackupStorage::CloseStorageL()
       
   163     {
       
   164     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::CloseStorageL()" ) ) );
       
   165 
       
   166     // Use file composer to store changes to XML file, if any changes has been done
       
   167     if ( iChangesMade )
       
   168         {
       
   169         TBuf<KPsmMaxPathLength> storageFile( KNullDesC );
       
   170         BackupFileLocation( storageFile );
       
   171 
       
   172         iStorageFile.SaveL( iFileSession,
       
   173                             storageFile,
       
   174                             iStorageRoot,
       
   175                             TXmlEngSerializationOptions::KOptionOmitXMLDeclaration );
       
   176 
       
   177         iChangesMade = EFalse;
       
   178         }
       
   179 
       
   180     // Tell base class to finalize closing the storage
       
   181     CPsmStorage::CloseStorageL();
       
   182 
       
   183     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::CloseStorageL() - return" ) ) );
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPsmBackupStorage::FindSetItemL
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 TXmlEngElement CPsmBackupStorage::FindSetItemL( const TXmlEngElement& aConfigSet, const TInt aKey )
       
   191     {
       
   192     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::FindSetItemL()" ) ) );
       
   193 
       
   194     // Search correct set item for aKey
       
   195     TBool setFound( EFalse );
       
   196     RXmlEngNodeList<TXmlEngElement> nodeList;
       
   197     aConfigSet.GetElementsByTagNameL(nodeList, KPsmSetItem);
       
   198     TXmlEngElement setItem = nodeList.Next().AsElement();       
       
   199 
       
   200     // Loop until correct set item is found
       
   201     while ( setItem.NotNull() && !setFound )    
       
   202         {
       
   203         // Read key from storage
       
   204         TInt setItemKey = GetAttributeIntValueL( setItem, KPsmSetItemKey );
       
   205 
       
   206         if ( aKey == setItemKey )
       
   207             {
       
   208             // Set found, no need to loop anumore
       
   209             setFound = ETrue;
       
   210             }
       
   211         else
       
   212             {
       
   213             // Set not found, continue loop with next config set
       
   214             setItem = nodeList.Next().AsElement();            
       
   215             }
       
   216         }
       
   217 
       
   218     if ( !setFound )
       
   219         {
       
   220         COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::FindSetItemL() - Not found id:%i, LEAVE" ), aKey ) );
       
   221         User::Leave( KErrNotFound );
       
   222         }
       
   223 
       
   224     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::FindSetItemL() - return" ) ) );
       
   225     return setItem;
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CPsmBackupStorage::BackupConfigurationL
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CPsmBackupStorage::BackupConfigurationL( const RConfigInfoArray& aConfigArray )
       
   233     {
       
   234     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupConfigurationL()" ) ) );
       
   235     UpdateBackupL( aConfigArray, EPsmsrvModeNormal );
       
   236     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupConfigurationL() - return" ) ) );
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CPsmBackupStorage::UpdateBackupL
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CPsmBackupStorage::UpdateBackupL( const RConfigInfoArray& aConfigArray, TInt aMode )
       
   244     {
       
   245     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - Mode: %i" ), aMode ) );
       
   246 
       
   247     if ( iStorageRoot.IsNull() )    
       
   248         {
       
   249         CloseStorageL();
       
   250         User::Leave( KErrNotFound );
       
   251         }
       
   252 
       
   253     // This is used only to backup original settings
       
   254     // Search correct settings for aUid
       
   255     TInt trapErr( KErrNone );
       
   256     
       
   257     TXmlEngElement settings;    
       
   258     TRAP( trapErr, settings = FindConfigurationSetL( aMode ) );
       
   259 
       
   260     // If not found, create new set of backup items
       
   261     if ( KErrNotFound == trapErr && settings.IsNull() )
       
   262         {
       
   263         // Set for the UID not found, create new set to storage
       
   264         
       
   265         // First param indicates that can have childs, second is the owner document and last is tag
       
   266         settings = iStorageFile.CreateElementL( KPsmPluginGroupTag );
       
   267         // Add attribute to group, Set mode to 'NormalMode', we don't backup PSM values
       
   268         SetAttributeValueL( settings, KPsmPluginGroupAttribute, aMode );
       
   269         COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - Append set to storage root" ) ) );
       
   270         // Add element to correct place
       
   271         iStorageRoot.AppendChildL( settings );
       
   272         // reset error value
       
   273         trapErr = KErrNone;
       
   274         }
       
   275 
       
   276     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - Config count: %i" ), aConfigArray.Count() ) );
       
   277 
       
   278     // Loop config array and update storage
       
   279     for ( TInt i = 0; i < aConfigArray.Count(); i++ )
       
   280         {
       
   281         COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - Loop configs: %i" ), i ) );
       
   282 
       
   283         const TPsmsrvConfigInfo& configInfo = aConfigArray[i];
       
   284 
       
   285         // Search set element from config and create new if not found
       
   286         TXmlEngElement setItem;
       
   287         TRAP( trapErr, setItem = FindSetItemL( settings, configInfo.iConfigId ) );
       
   288 
       
   289         // If not found, create new
       
   290         if ( KErrNotFound == trapErr && setItem.IsNull() )
       
   291             {
       
   292             // First param indicates that cannot have childs, 
       
   293             // second is the owner document and last is tag
       
   294             setItem = iStorageFile.CreateElementL( KPsmSetItem );
       
   295             // Add key and type attributes as they cannot be updated later
       
   296             SetAttributeValueL( setItem, KPsmSetItemKey, configInfo.iConfigId );
       
   297             SetAttributeValueL( setItem, KPsmSetItemType, configInfo.iConfigType );
       
   298             // Add element to set
       
   299             settings.AppendChildL( setItem );
       
   300             }
       
   301         else
       
   302             {
       
   303             // remove existing attribute value
       
   304             setItem.RemoveAttributeL( KPsmSetItemValue );
       
   305             }
       
   306 
       
   307         // Finally, change Value attribute
       
   308         switch ( configInfo.iConfigType )
       
   309             {
       
   310             case EConfigTypeInt:
       
   311                 {
       
   312                 SetAttributeValueL( setItem, KPsmSetItemValue, configInfo.iIntValue );
       
   313                 break;
       
   314                 }
       
   315             case EConfigTypeReal:
       
   316                 {
       
   317                 SetAttributeValueL( setItem, KPsmSetItemValue, configInfo.iRealValue );
       
   318                 break;
       
   319                 }
       
   320             case EConfigTypeBuf:
       
   321                 {
       
   322                 TBuf8<KPsmMaxAttribLength> buffer( KNullDesC8 );
       
   323                 buffer.Copy( configInfo.iDesValue );
       
   324                 SetAttributeValueL( setItem, KPsmSetItemValue, buffer );
       
   325                 break;
       
   326                 }
       
   327             default:
       
   328                 {
       
   329                 COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - Unknown type LEAVE" ) ) );
       
   330                 User::Leave( KErrUnknown );
       
   331                 }
       
   332             }
       
   333         }
       
   334 
       
   335     // Indicate that storage needs to be composed when closing
       
   336     iChangesMade = ETrue;
       
   337     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::UpdateBackupL() - return" ) ) );
       
   338     }
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CPsmBackupStorage::BackupFileLocation
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 void CPsmBackupStorage::BackupFileLocation( TDes& aLocation )
       
   345     {
       
   346     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupFileLocation()" ) ) );
       
   347 
       
   348     // Generate file folder+name+extension
       
   349     TBuf<KPsmMaxPathLength> privatePath( KNullDesC );
       
   350     iFileSession.PrivatePath( privatePath );
       
   351 
       
   352     TBuf<KPsmMaxPathLength> storageFile( KNullDesC );
       
   353     storageFile.Append( KPsmBackupDrive );
       
   354     storageFile.Append( privatePath );
       
   355     storageFile.Append( KPsmBackupFolder );
       
   356     storageFile.AppendNumUC( iStorageUid, EHex );
       
   357     storageFile.Append( KPsmStorageExtension );
       
   358 
       
   359     aLocation.Copy( storageFile );
       
   360 
       
   361     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupFileLocation - return") ) );
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CPsmBackupStorage::CheckBackupLocation
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CPsmBackupStorage::CheckBackupLocation()
       
   369     {
       
   370     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::CheckBackupLocation()" ) ) );
       
   371 
       
   372     TInt err = iFileSession.CreatePrivatePath( EDriveC );
       
   373 
       
   374     if ( KErrNone == err || KErrAlreadyExists == err )
       
   375         {
       
   376         TBuf<KPsmMaxPathLength> storageFile( KNullDesC );
       
   377         TBuf<KPsmMaxPathLength> privatePath( KNullDesC );
       
   378         iFileSession.PrivatePath( privatePath );
       
   379 
       
   380         storageFile.Append( KPsmBackupDrive );
       
   381         storageFile.Append( privatePath );
       
   382         storageFile.Append( KPsmBackupFolder );
       
   383 
       
   384         // Create dir 
       
   385         iFileSession.MkDir( storageFile );
       
   386         }
       
   387 
       
   388     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::CheckBackupLocation - return") ) );
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CPsmBackupStorage::ListPassiveConfigSetL
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 void CPsmBackupStorage::ListPassiveConfigSetL( RConfigInfoArray& aPsmConfigArray )
       
   396     {
       
   397     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ListPassiveConfigSetL()" ) ) );
       
   398 
       
   399     // Read configuration information from config files and generate config array
       
   400     CPsmStorage::CreateConfigArrayL( aPsmConfigArray, EPsmsrvModeNormal );
       
   401 
       
   402     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ListPassiveConfigSetL() - return" ) ) );
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CPsmBackupStorage::ListCurrentSettingSetL
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 void CPsmBackupStorage::ListCurrentSettingSetL( RConfigInfoArray& aPsmConfigArray )
       
   410     {
       
   411     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ListCurrentSettingSetL()" ) ) );
       
   412 
       
   413     // Read configuration information from config files and generate config array
       
   414     TRAP_IGNORE( CPsmStorage::CreateConfigArrayL( aPsmConfigArray, KPsmCurrentSetMode ) );
       
   415 
       
   416     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::ListCurrentSettingSetL() - return" ) ) );
       
   417     }
       
   418 
       
   419 // -----------------------------------------------------------------------------
       
   420 // CPsmBackupStorage::UpdateCurrentSettingSetL
       
   421 // -----------------------------------------------------------------------------
       
   422 //
       
   423 void CPsmBackupStorage::UpdateCurrentSettingSetL( const RConfigInfoArray& aConfigArray )
       
   424     {
       
   425     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupConfigurationL()" ) ) );
       
   426     UpdateBackupL( aConfigArray, KPsmCurrentSetMode );
       
   427     COMPONENT_TRACE( ( _L( "PSM Server - CPsmBackupStorage::BackupConfigurationL() - return" ) ) );
       
   428     }
       
   429 
       
   430 // End of file