psmservices/psmserver/src/engine/psmsettingshandler.cpp
changeset 0 4e1aa6a622a0
child 59 0f7422b6b602
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 Settings handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ecom/ecom.h>
       
    20 #include <centralrepository.h>
       
    21 #include "psmsettingshandler.h"
       
    22 #include "psmconfigstorage.h"
       
    23 #include "psmbackupstorage.h"
       
    24 #include "psmcenrepstorage.h"
       
    25 #include "psmtrace.h"
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CPsmSettingsHandler::NewL
       
    29 // Two-phased constructor.
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CPsmSettingsHandler* CPsmSettingsHandler::NewL( const TPsmsrvMode aMode )
       
    33     {
       
    34     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::NewL()" ) ) );
       
    35 
       
    36     CPsmSettingsHandler* self = CPsmSettingsHandler::NewLC( aMode );
       
    37     CleanupStack::Pop( self );
       
    38 
       
    39     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::NewL - return 0x%x" ), self ) );
       
    40 
       
    41     return self;
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CPsmSettingsHandler::NewLC
       
    46 // Two-phased constructor.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CPsmSettingsHandler* CPsmSettingsHandler::NewLC( const TPsmsrvMode aMode )
       
    50     {
       
    51     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::NewLC()" ) ) );
       
    52 
       
    53     CPsmSettingsHandler* self = new( ELeave ) CPsmSettingsHandler( aMode );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56 
       
    57     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::NewLC - return 0x%x" ), self ) );
       
    58 
       
    59     return self;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CPsmSettingsHandler::CPsmSettingsHandler
       
    64 // C++ default constructor can NOT contain any code, that
       
    65 // might leave.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CPsmSettingsHandler::CPsmSettingsHandler( const TPsmsrvMode aMode ) :
       
    69     iMode( aMode ),
       
    70     iCurrentMode( aMode )
       
    71     {
       
    72     // Nothing to do
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CPsmSettingsHandler::ConstructL
       
    77 // Symbian 2nd phase constructor can leave.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CPsmSettingsHandler::ConstructL()
       
    81     {
       
    82     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ConstructL()" ) ) );
       
    83     User::LeaveIfError( iFileSession.Connect() );
       
    84     // Create storage files
       
    85     iConfigStorage = CPsmConfigStorage::NewL( iMode, iFileSession );
       
    86     iBackupStorage = CPsmBackupStorage::NewL( iMode, iFileSession );
       
    87     iCenRepStorage = CPsmCenRepStorage::NewL( iMode, iFileSession );
       
    88 
       
    89     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ConstructL - return" ) ) );
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // Destructor
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CPsmSettingsHandler::~CPsmSettingsHandler()
       
    97     {
       
    98     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::~CPsmSettingsHandler()" ) ) );
       
    99 
       
   100     delete iConfigStorage;
       
   101     delete iBackupStorage;
       
   102     delete iCenRepStorage;
       
   103 
       
   104     iFileSession.Close();
       
   105 
       
   106     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::~CPsmSettingsHandler - return") ) );
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CPsmSettingsHandler::BackupAndGetSettingsL
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CPsmSettingsHandler::BackupAndGetSettingsL( 
       
   114     RConfigInfoArray& aPsmConfigArray, TUint32 aRepository )
       
   115     {
       
   116     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::BackupAndGetSettingsL()" ) ) );
       
   117     // Change settings from normal config storage
       
   118     iConfigStorage->InitStorageL( aRepository );
       
   119     ChangeSettingsL( aPsmConfigArray, aRepository, *iConfigStorage );
       
   120     iConfigStorage->CloseStorageL();
       
   121     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::BackupAndGetSettingsL() - return" ) ) );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CPsmSettingsHandler::GetCurrentSettingsL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CPsmSettingsHandler::GetSettingsL( 
       
   129     RConfigInfoArray& aPsmConfigArray, TUint32 aRepository )
       
   130     {
       
   131     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::GetCurrentSettingsL()" ) ) );
       
   132     // Get current settings from normal config storage
       
   133 
       
   134     // Use own storage in case PSM mode is ongoing by some plugin
       
   135     if ( EPsmsrvModeNormal == iMode )
       
   136         {
       
   137         // Normal mode, get current settings from backup storage
       
   138         CPsmBackupStorage* backupStorage = CPsmBackupStorage::NewLC( iMode, iFileSession );
       
   139         backupStorage->InitStorageL( aRepository );
       
   140         backupStorage->GetConfigurationL( aPsmConfigArray );
       
   141         CleanupStack::PopAndDestroy( backupStorage );
       
   142         }
       
   143     else
       
   144         {
       
   145         // PSM mode, get current settings from config storage
       
   146         CPsmConfigStorage* configStorage = CPsmConfigStorage::NewLC( iMode, iFileSession );
       
   147         configStorage->InitStorageL( aRepository );
       
   148         configStorage->GetConfigurationL( aPsmConfigArray );
       
   149         CleanupStack::PopAndDestroy( configStorage );
       
   150         }
       
   151 
       
   152     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::GetCurrentSettingsL() - return" ) ) );
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CPsmSettingsHandler::BackupSettingsL
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CPsmSettingsHandler::BackupSettingsL( 
       
   160     const RConfigInfoArray& aPsmConfigArray, const TUint32 aRepository )
       
   161     {
       
   162     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::BackupSettingsL()" ) ) );
       
   163 
       
   164     if ( EPsmsrvModeNormal == iMode )
       
   165         {
       
   166         ERROR_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::BackupSettingsL() - Not allowed in normal mode - LEAVE" ) ) );
       
   167         // Backup is not allowed in normal mode
       
   168         User::Leave( KErrWrite );
       
   169         }
       
   170 
       
   171     // Use own storage in case PSM mode is ongoing by some plugin
       
   172     CPsmBackupStorage* backupStorage = CPsmBackupStorage::NewLC( iMode, iFileSession );
       
   173     backupStorage->InitStorageL( aRepository );
       
   174     backupStorage->BackupConfigurationL( aPsmConfigArray );
       
   175     backupStorage->CloseStorageL();
       
   176     CleanupStack::PopAndDestroy( backupStorage );
       
   177 
       
   178     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::BackupSettingsL() - return" ) ) );
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CPsmSettingsHandler::Compare
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TBool CPsmSettingsHandler::Compare( 
       
   186     const TPsmsrvConfigInfo& aSource, 
       
   187     const TPsmsrvConfigInfo& aTarget ) const
       
   188     {
       
   189     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::Compare()" ) ) );
       
   190     TBool returnValue( EFalse );
       
   191     if ( ( aSource.iConfigId == aTarget.iConfigId ) &&
       
   192          ( aSource.iConfigType == aTarget.iConfigType ) )
       
   193         {
       
   194         // Check type and copare values
       
   195         switch ( aSource.iConfigType )
       
   196             {
       
   197             case EConfigTypeInt:
       
   198                 returnValue = ( aSource.iIntValue == aTarget.iIntValue );
       
   199                 break;
       
   200             case EConfigTypeReal:
       
   201                 returnValue = ( aSource.iRealValue == aTarget.iRealValue );
       
   202                 break;
       
   203             case EConfigTypeBuf:
       
   204                 returnValue = ( aSource.iDesValue == aTarget.iDesValue );
       
   205                 break;
       
   206             default:
       
   207                 returnValue = EFalse;
       
   208             }
       
   209         }
       
   210     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::Compare() - return" ) ) );
       
   211     return returnValue;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CPsmSettingsHandler::ChangeSettingsL
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CPsmSettingsHandler::ChangeSettingsL( 
       
   219     RConfigInfoArray& aPsmConfigArray, 
       
   220     TUint32 aRepository,
       
   221     CPsmStorage& aConfigStorage )
       
   222     {
       
   223     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeSettingsL()" ) ) );
       
   224 
       
   225     TInt err( KErrNone );
       
   226     // First, get current settings
       
   227     RConfigInfoArray currentSettings;
       
   228     CleanupClosePushL( currentSettings );
       
   229 
       
   230     iBackupStorage->InitStorageL( aRepository );
       
   231     iBackupStorage->ListCurrentSettingSetL( currentSettings );
       
   232 
       
   233     // Check currently owned settings and compare those to the new ones
       
   234     RConfigInfoArray settingsToBackUp;
       
   235     CleanupClosePushL( settingsToBackUp );
       
   236 
       
   237     for ( TInt i = 0; aPsmConfigArray.Count() != i; i++ )
       
   238         {
       
   239         TBool found = EFalse;
       
   240         for ( TInt a = 0; !found && currentSettings.Count() != a; a++ )
       
   241             {
       
   242             found = Compare( currentSettings[ a ], aPsmConfigArray[ i ] );
       
   243             }
       
   244 
       
   245         if ( !found )
       
   246             {
       
   247             // Add new settings to backup
       
   248             settingsToBackUp.Append( aPsmConfigArray[ i ] );
       
   249             currentSettings.Append( aPsmConfigArray[ i ] );
       
   250             }
       
   251         }
       
   252 
       
   253     // Get new values from config
       
   254     if ( EPsmsrvModeNormal == iMode )
       
   255         {
       
   256         // Restore to normal mode, initialize backup storage
       
   257         iBackupStorage->GetConfigurationL( aPsmConfigArray );
       
   258         }
       
   259     else
       
   260         {
       
   261         // Moving from power save mode to another power save mode
       
   262         iBackupStorage->BackupConfigurationL( settingsToBackUp );
       
   263         aConfigStorage.GetConfigurationL( aPsmConfigArray );
       
   264         }
       
   265 
       
   266     if( aPsmConfigArray.Count() == 0 )
       
   267         {
       
   268         err = KErrNotFound;
       
   269         }
       
   270 
       
   271     // Check for missing values and get those from backup
       
   272     RConfigInfoArray settingsFromBackUp;
       
   273     CleanupClosePushL( settingsFromBackUp );
       
   274 
       
   275     for ( TInt i = 0; currentSettings.Count() != i; i++ )
       
   276         {
       
   277         TPsmsrvConfigInfo& currentInfo = currentSettings[ i ];
       
   278         TBool found = EFalse;
       
   279         TPsmsrvConfigInfo newInfo;
       
   280         for ( TInt index = 0; !found && aPsmConfigArray.Count() != index; index++ )
       
   281             {
       
   282             newInfo = aPsmConfigArray[ index ];
       
   283             found = ( newInfo.iConfigId == currentInfo.iConfigId );
       
   284             }
       
   285 
       
   286         // Check was setting found
       
   287         if ( !found )
       
   288             {
       
   289             // not found, get setting from backup
       
   290             settingsFromBackUp.Append( currentInfo );
       
   291             currentSettings.Remove( i );
       
   292             i--;
       
   293             }
       
   294         else
       
   295             {
       
   296             // Setting found, update values
       
   297             currentInfo = newInfo;
       
   298             }
       
   299         }
       
   300 
       
   301     // Finally, get remaining settings from backup
       
   302     if ( settingsFromBackUp.Count() )
       
   303         {
       
   304         iBackupStorage->GetConfigurationL( settingsFromBackUp );
       
   305         }
       
   306 
       
   307     iBackupStorage->UpdateCurrentSettingSetL( currentSettings );
       
   308 
       
   309     // Close backup storage
       
   310     iBackupStorage->CloseStorageL();
       
   311 
       
   312     // add settings from backup to settings
       
   313     for ( TInt i = 0; i < settingsFromBackUp.Count(); i++ )
       
   314         {
       
   315         aPsmConfigArray.Append( settingsFromBackUp[ i ] );
       
   316         }
       
   317 
       
   318     // Cleanup config arrays
       
   319     CleanupStack::PopAndDestroy( &settingsFromBackUp );
       
   320     CleanupStack::PopAndDestroy( &settingsToBackUp );
       
   321     CleanupStack::PopAndDestroy( &currentSettings );
       
   322 
       
   323     User::LeaveIfError( err );
       
   324 
       
   325     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeSettingsL() - return" ) ) );
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CPsmSettingsHandler::ChangeCenRepSettingsL
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 void CPsmSettingsHandler::ChangeCenRepSettingsL()
       
   333     {
       
   334     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL()" ) ) );
       
   335 
       
   336     // First resolve the CenRep config files
       
   337     RPassiveConfigList passiveConfigs;
       
   338     CleanupClosePushL( passiveConfigs );
       
   339 
       
   340     iCenRepStorage->ListPassiveConfigs( passiveConfigs );
       
   341 
       
   342     const TInt configCount( passiveConfigs.Count() );
       
   343 
       
   344     // Loop found passive configs
       
   345     for ( TInt i = 0; i < configCount; i++ )
       
   346         {
       
   347         RConfigInfoArray psmConfigArray;
       
   348         CleanupClosePushL( psmConfigArray );
       
   349 
       
   350         const TUint32 repository = passiveConfigs[i];
       
   351 
       
   352         iCenRepStorage->InitStorageL( repository );
       
   353 
       
   354         if ( EPsmsrvModeNormal == iMode )
       
   355             {
       
   356             // Moving to normal mode, get passive config set from baskup storage
       
   357             iBackupStorage->InitStorageL( repository );
       
   358             iBackupStorage->ListPassiveConfigSetL( psmConfigArray );
       
   359             iBackupStorage->CloseStorageL();
       
   360             }
       
   361         else
       
   362             {
       
   363             // Get configuration set
       
   364             iCenRepStorage->ListPassiveConfigSetL( psmConfigArray );
       
   365             }
       
   366 
       
   367         // Change values
       
   368         ChangeCenRepSettingsL( psmConfigArray, repository );
       
   369 
       
   370         // close storage
       
   371         iCenRepStorage->CloseStorageL();
       
   372         CleanupStack::PopAndDestroy( &psmConfigArray );
       
   373         }
       
   374 
       
   375     CleanupStack::PopAndDestroy( &passiveConfigs );
       
   376 
       
   377     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL() - return" ) ) );
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CPsmSettingsHandler::ChangeCenRepSettingsL
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 void CPsmSettingsHandler::ChangeCenRepSettingsL( 
       
   385     RConfigInfoArray& aPsmConfigArray, TUint32 aRepository )
       
   386     {
       
   387     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL()" ) ) );
       
   388 
       
   389     // First read current values from CenRep
       
   390     TUid cenRepUid = { aRepository };
       
   391     CRepository* cenrep = CRepository::NewLC( cenRepUid );
       
   392 
       
   393     TInt setCount( aPsmConfigArray.Count() );
       
   394 
       
   395     for ( TInt i = 0; i < setCount; i++ )
       
   396         {
       
   397         TPsmsrvConfigInfo& info = aPsmConfigArray[i];
       
   398         switch ( info.iConfigType )
       
   399             {
       
   400             case EConfigTypeInt:
       
   401                 {
       
   402                 User::LeaveIfError( cenrep->Get( info.iConfigId, info.iIntValue ) );
       
   403                 break;
       
   404                 }
       
   405             case EConfigTypeReal:
       
   406                 {
       
   407                 User::LeaveIfError( cenrep->Get( info.iConfigId, info.iRealValue ) );
       
   408                 break;
       
   409                 }
       
   410             case EConfigTypeBuf:
       
   411                 {
       
   412                 User::LeaveIfError( cenrep->Get( info.iConfigId, info.iDesValue ) );
       
   413                 break;
       
   414                 }
       
   415             default:
       
   416                 {
       
   417                 COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL() - Unknown type LEAVE" ) ) );
       
   418                 User::Leave( KErrUnknown );
       
   419                 }
       
   420             }
       
   421         }
       
   422 
       
   423     // Get new values from storage
       
   424     TRAP_IGNORE( ChangeSettingsL( aPsmConfigArray, aRepository, *iCenRepStorage ) );
       
   425 
       
   426     // Get new count
       
   427     setCount = aPsmConfigArray.Count();
       
   428 
       
   429     // Write new values to CenRep
       
   430     for ( TInt j = 0; j < setCount; j++ )
       
   431         {
       
   432         TPsmsrvConfigInfo& info = aPsmConfigArray[j];
       
   433         switch ( info.iConfigType )
       
   434             {
       
   435             case EConfigTypeInt:
       
   436                 {
       
   437                 User::LeaveIfError( cenrep->Set( info.iConfigId, info.iIntValue ) );
       
   438                 break;
       
   439                 }
       
   440             case EConfigTypeReal:
       
   441                 {
       
   442                 User::LeaveIfError( cenrep->Set( info.iConfigId, info.iRealValue ) );
       
   443                 break;
       
   444                 }
       
   445             case EConfigTypeBuf:
       
   446                 {
       
   447                 User::LeaveIfError( cenrep->Set( info.iConfigId, info.iDesValue ) );
       
   448                 break;
       
   449                 }
       
   450             default:
       
   451                 {
       
   452                 COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL() - Unknown type LEAVE" ) ) );
       
   453                 User::Leave( KErrUnknown );
       
   454                 }
       
   455             }
       
   456         }
       
   457 
       
   458     // Cleanup CenRep
       
   459     CleanupStack::PopAndDestroy( cenrep );
       
   460 
       
   461     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::ChangeCenRepSettingsL() - return" ) ) );
       
   462     }
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CPsmSettingsHandler::SetMode
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CPsmSettingsHandler::SetMode( TPsmsrvMode aMode )
       
   469     {
       
   470     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::SetMode()" ) ) );
       
   471 
       
   472     // Store previous mode
       
   473     iCurrentMode = iMode;
       
   474 
       
   475     // Store mode
       
   476     iMode = aMode;
       
   477 
       
   478     // Update mode to storage handlers also
       
   479     iBackupStorage->SetMode( aMode );
       
   480     iConfigStorage->SetMode( aMode );
       
   481     iCenRepStorage->SetMode( aMode );
       
   482 
       
   483     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSettingsHandler::SetMode() - return" ) ) );
       
   484     }
       
   485 
       
   486 // End of file