cbs/CbsServer/ServerSrc/CCbsDbImpSettings.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2003 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:  This module contains the implementation of CCbsDbImpSettings class 
       
    15 *                member functions.
       
    16 *    
       
    17 *                This class represents CBS settings stored in the database.
       
    18 *                A functionality is provided here to access, modify, store and restore
       
    19 *                settings.
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 //  INCLUDE FILES
       
    25 #include <centralrepository.h>
       
    26 #include "CbsServerPrivateCRKeys.h"
       
    27 #include "MCbsDbSettingsObserver.H"
       
    28 #include "CbsServerPanic.h"
       
    29 #include "CCbsDbImpSettings.H"
       
    30 #include "CbsDbConstants.h"
       
    31 #include "CbsStreamHelper.h"
       
    32 #include "CbsUtils.h"
       
    33 #include "CbsLogger.h"
       
    34 
       
    35 // CONSTANTS
       
    36 // Used if languages cannot be read from Shared Data.
       
    37 const TInt8 KDefaultsLanguageSubscribedValue = '1';
       
    38 
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CCbsDbImpSettings::CCbsDbImpSettings
       
    44 // C++ default constructor can NOT contain any code, that
       
    45 // might leave.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CCbsDbImpSettings::CCbsDbImpSettings(  
       
    49     CCbsDbImp& aDatabase )
       
    50     : iDatabase( aDatabase )
       
    51     {
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CCbsDbImp::ConstructL
       
    56 // Symbian 2nd phase constructor can leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void CCbsDbImpSettings::ConstructL()
       
    60     {
       
    61     // Create an array for settings event observers.
       
    62     iObservers = new ( ELeave ) CArrayFixFlat< MCbsDbSettingsObserver* >( 
       
    63         KCbsDbObserverArraySize );
       
    64 
       
    65     TRAPD( result, LoadSettingsL() );
       
    66 	if ( result != KErrNone )   
       
    67 		{
       
    68 		User::Leave( result );
       
    69 		}
       
    70 		
       
    71     __TEST_INVARIANT;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CCbsDbImpSettings::NewL
       
    76 // Two-phased constructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CCbsDbImpSettings* CCbsDbImpSettings::NewL( CCbsDbImp& aDatabase )
       
    80     {
       
    81     CCbsDbImpSettings* self = 
       
    82         new ( ELeave ) CCbsDbImpSettings( aDatabase );
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop();
       
    86     return self;
       
    87     }
       
    88     
       
    89 // Destructor
       
    90 CCbsDbImpSettings::~CCbsDbImpSettings()
       
    91     {
       
    92     CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpSettings::~CCbsDbImpSettings()");
       
    93     delete iObservers;
       
    94     CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpSettings::~CCbsDbImpSettings()");
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CCbsDbImpSettings::SetTopicDetectionStatusL
       
    99 // Changes the topic detection status.
       
   100 // (other items were commented in a header).
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CCbsDbImpSettings::SetTopicDetectionStatusL( 
       
   104     TBool aStatus )
       
   105     {
       
   106 
       
   107     // If the value does not change we do nothing but return
       
   108     if ( aStatus != iSettings.iTopicDetectionStatus ) 
       
   109         {
       
   110         // First we change the status value in cache.
       
   111         iSettings.iTopicDetectionStatus = aStatus;
       
   112 
       
   113         // Now we try to save the modified settings to the store
       
   114         if ( !SaveSettings() ) 
       
   115             {        
       
   116             // If an error occured, we leave the status unchanged.
       
   117             if ( iSettings.iTopicDetectionStatus ) 
       
   118                 {
       
   119                 iSettings.iTopicDetectionStatus = EFalse;
       
   120                 }
       
   121             else 
       
   122                 {
       
   123                 iSettings.iTopicDetectionStatus = ETrue;
       
   124                 }
       
   125 
       
   126             // and then leave again
       
   127             User::Leave( KErrWrite );
       
   128             }
       
   129 
       
   130         // Inform observers about the changed status
       
   131         TInt amountOfObservers( iObservers->Count() );
       
   132         for ( TInt i( 0 ); i < amountOfObservers; i++ )
       
   133             {
       
   134             iObservers->At( i )->TopicDetectionStatusChangedIndL();
       
   135             }                
       
   136         }    
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CCbsDbImpSettings::SaveSettings
       
   141 // Saves the settings to CenRep
       
   142 // (other items were commented in a header).
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 TBool CCbsDbImpSettings::SaveSettings()
       
   146     {
       
   147     __TEST_INVARIANT;
       
   148     TBool result( ETrue );
       
   149 
       
   150     // Try to save the settings.
       
   151     TRAPD( error, DoSaveSettingsL() );
       
   152     if ( error != KErrNone )
       
   153         {
       
   154         // Some kind of failure occurred.
       
   155         __TEST_INVARIANT;        
       
   156         result = EFalse;
       
   157         }
       
   158 
       
   159     __TEST_INVARIANT;
       
   160     return result;
       
   161     }    
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CCbsDbImpSettings::DoSaveSettingsL
       
   165 // Saves the settings to CenRep
       
   166 // The method leaves in case the writing to the does not succeed.
       
   167 // (other items were commented in a header).
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CCbsDbImpSettings::DoSaveSettingsL()
       
   171     {
       
   172     
       
   173     TInt reception( iSettings.iReceptionStatus );
       
   174     TInt topicDetection( iSettings.iTopicDetectionStatus );
       
   175     
       
   176     TBuf< ECbsCount > languages; 
       
   177    
       
   178     for ( TInt i( 0 ); i < ECbsCount; i++ )
       
   179         {
       
   180         languages.AppendNum( iSettings.iLanguageStatus.iLanguages[i] );	
       
   181         }
       
   182    
       
   183     // Connecting and initialization
       
   184     CRepository* repository = CRepository::NewL( KCRUidCellBroadcast );
       
   185  
       
   186     // Get the values from Central Repository
       
   187     repository->Set( KCbsReception, reception );
       
   188     repository->Set( KCbsTopicDetection, topicDetection );
       
   189     repository->Set( KCbsLanguages, languages );
       
   190     
       
   191     // Closing the connection
       
   192     delete repository;
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CCbsDbImpSettings::GetTopicDetectionStatus
       
   197 // Returns the current value of the topic detection status.
       
   198 // (other items were commented in a header).
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CCbsDbImpSettings::GetTopicDetectionStatus( 
       
   202     TBool& aStatus ) const
       
   203     {
       
   204     // We just give the asked status value from cache to aStatus
       
   205     aStatus = iSettings.iTopicDetectionStatus;
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CCbsDbImpSettings::SetReceptionStatusL
       
   210 // Changes the reception status.
       
   211 // (other items were commented in a header).
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CCbsDbImpSettings::SetReceptionStatusL( 
       
   215     TBool aStatus )    
       
   216     {
       
   217 
       
   218     // If the value does not change we do nothing but return
       
   219     if ( aStatus != iSettings.iReceptionStatus ) 
       
   220         {
       
   221         // First we change the status value in iSettings
       
   222         iSettings.iReceptionStatus = aStatus;
       
   223 
       
   224         // Now we try to save the modified settings to the store
       
   225         if ( !SaveSettings() ) 
       
   226             {
       
   227             // If an error occured, we leave the status unchanged.
       
   228             if ( iSettings.iReceptionStatus ) 
       
   229                 {
       
   230                 iSettings.iReceptionStatus = EFalse;
       
   231                 }
       
   232             else 
       
   233                 {
       
   234                 iSettings.iReceptionStatus = ETrue;
       
   235                 }
       
   236 
       
   237             // and then leave.
       
   238             User::Leave( KErrWrite );
       
   239             }
       
   240 
       
   241         // Inform observers about the changed status
       
   242         TInt amountOfObservers( iObservers->Count() );
       
   243         for ( TInt i( 0 ); i < amountOfObservers; i++ )
       
   244             {
       
   245             iObservers->At( i )->ReceptionStatusChangedIndL();
       
   246             }                
       
   247         }    
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CCbsDbImpSettings::GetReceptionStatus
       
   252 // Returns the current value of the topic reception status.
       
   253 // (other items were commented in a header).
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CCbsDbImpSettings::GetReceptionStatus( 
       
   257     TBool& aStatus ) const
       
   258     {
       
   259     // We just give the asked status value from cache to aStatus
       
   260     aStatus = iSettings.iReceptionStatus;
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CCbsDbImpSettings::SetLanguagesL
       
   265 // Changes the preferred languages.
       
   266 // (other items were commented in a header).
       
   267 // -----------------------------------------------------------------------------
       
   268 // 
       
   269 void CCbsDbImpSettings::SetLanguagesL( 
       
   270     const TCbsDbLanguages& aLanguages )
       
   271     {
       
   272     __TEST_INVARIANT;
       
   273 
       
   274     // Compare language settings.
       
   275     if ( !IsLanguagesEqual( aLanguages, iSettings.iLanguageStatus ) ) 
       
   276         {
       
   277         // We take a backup of the language status before changing the value.
       
   278         TCbsDbLanguages oldLanguageStatus = iSettings.iLanguageStatus;
       
   279         
       
   280         // We change the language status value
       
   281         iSettings.iLanguageStatus = aLanguages;
       
   282 
       
   283         // Now we try to save the modified settings struct to the store
       
   284         if ( !SaveSettings() ) 
       
   285             {
       
   286             // If an error occured, we leave the settings unchanged.
       
   287             iSettings.iLanguageStatus = oldLanguageStatus;
       
   288             User::Leave( KErrWrite );
       
   289             }
       
   290 
       
   291         // Inform observers about the changed status
       
   292         TInt amountOfObservers( iObservers->Count() );
       
   293         for ( TInt i( 0 ); i < amountOfObservers; i++ )
       
   294             {
       
   295             iObservers->At( i )->LanguagesChangedIndL();
       
   296             }    
       
   297         }
       
   298     
       
   299     __TEST_INVARIANT;
       
   300     }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CCbsDbImpSettings::GetLanguages
       
   304 // Returns the preferred languages.
       
   305 // (other items were commented in a header).
       
   306 // -----------------------------------------------------------------------------
       
   307 // 
       
   308 void CCbsDbImpSettings::GetLanguages( 
       
   309     TCbsDbLanguages& aLanguages ) const
       
   310     {
       
   311     // We just give the asked status value from cache to aLanguages
       
   312     aLanguages = iSettings.iLanguageStatus;
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CCbsDbImpSettings::AddObserverL
       
   317 // Adds an observer to the settings.
       
   318 // Observers are notified when an event occurs on the settings.    
       
   319 // Panics on debug mode if aObserver is null.  
       
   320 // (other items were commented in a header).
       
   321 // -----------------------------------------------------------------------------
       
   322 // 
       
   323 void CCbsDbImpSettings::AddObserverL( 
       
   324     MCbsDbSettingsObserver* aObserver )
       
   325     {
       
   326     __TEST_INVARIANT;
       
   327     __ASSERT_DEBUG( aObserver!=0, CbsServerPanic( ECbsObserverNull ) );
       
   328     iObservers->AppendL( aObserver );
       
   329     __TEST_INVARIANT;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CCbsDbImpSettings::RemoveObserver
       
   334 // Removes database observer.
       
   335 // The method will panic, if there is no such observer added
       
   336 // or the given observer is null.
       
   337 // (other items were commented in a header).
       
   338 // -----------------------------------------------------------------------------
       
   339 // 
       
   340 void CCbsDbImpSettings::RemoveObserver( 
       
   341     const MCbsDbSettingsObserver* aObserver )
       
   342     {
       
   343     __ASSERT_DEBUG( aObserver != 0, CbsServerPanic( ECbsObserverNull ) );
       
   344 
       
   345     // Find the observer to remove
       
   346     TInt amountOfObservers( iObservers->Count() );
       
   347     TBool found( EFalse );
       
   348 
       
   349     for ( TInt index( 0 ); ( index < amountOfObservers ) && !found; index++ )
       
   350         {
       
   351         if ( aObserver == iObservers->At( index ) )
       
   352             {
       
   353             iObservers->Delete( index );
       
   354             iObservers->Compress();  
       
   355             found = ETrue;            
       
   356             }
       
   357         }
       
   358 
       
   359     // Panic if the observer was not found
       
   360     if ( !found )
       
   361         {
       
   362         CbsServerPanic( ECbsObserverNotFound );
       
   363         }    
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CCbsDbImpSettings::LoadSettingsL
       
   368 // Loads the settings, i.e. values for reception status, topic detection
       
   369 // and language subscriptions.
       
   370 // These values are retrieved from Central Repository, associated with
       
   371 // the UID of CbsServer.
       
   372 // (other items were commented in a header).
       
   373 // -----------------------------------------------------------------------------
       
   374 // 
       
   375 void CCbsDbImpSettings::LoadSettingsL()
       
   376     {
       
   377     CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpSettings::LoadSettingsL()");
       
   378     
       
   379     // Connecting and initialization
       
   380     CRepository* repository = CRepository::NewL( KCRUidCellBroadcast );
       
   381     
       
   382     TBuf< ECbsCount > languages;
       
   383     languages.Zero();
       
   384 
       
   385     TInt reception( 0 );
       
   386     TInt topicDetection( 0 );
       
   387     
       
   388     // Get the values from Central Repository
       
   389     repository->Get( KCbsReception, reception );
       
   390     repository->Get( KCbsTopicDetection, topicDetection );
       
   391     repository->Get( KCbsLanguages, languages );
       
   392     
       
   393     // Closing the connection
       
   394     delete repository;
       
   395     
       
   396     CBSLOGSTRING("CBSSERVER: CCbsDbImpSettings::LoadSettingsL(): Repository reading finished.");
       
   397 
       
   398     iSettings.iReceptionStatus = reception == 0 ? EFalse : ETrue;
       
   399     iSettings.iTopicDetectionStatus = topicDetection == 0 ? EFalse : ETrue;
       
   400 
       
   401     if ( languages.Length() != ECbsCount )
       
   402         {
       
   403         User::Leave( KErrCorrupt );
       
   404         }
       
   405     
       
   406     for ( TInt i( 0 ); i < ECbsCount; i++ )
       
   407         {
       
   408         iSettings.iLanguageStatus.iLanguages[ i ] = 
       
   409             ( languages[ i ] == KDefaultsLanguageSubscribedValue );
       
   410         }
       
   411     CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpSettings::LoadSettingsL()");
       
   412     }
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // CCbsDbImpSettings::IsLanguagesEqual
       
   416 // Determines whether the language settings are equal. 
       
   417 // (other items were commented in a header).
       
   418 // -----------------------------------------------------------------------------
       
   419 // 
       
   420 TBool CCbsDbImpSettings::IsLanguagesEqual( 
       
   421     const TCbsDbLanguages& aLanguage1, 
       
   422     const TCbsDbLanguages& aLanguage2 ) const
       
   423     {
       
   424     // Go through each language settings.
       
   425     for ( TInt index( 0 ); index < ECbsCount; index++ )
       
   426         {
       
   427         if ( aLanguage1.iLanguages[ index ] != aLanguage2.iLanguages[ index ] )
       
   428             {
       
   429             return EFalse;
       
   430             }
       
   431         }
       
   432 
       
   433     // Equal.
       
   434     return ETrue;
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CCbsDbImpSettings::SetDefaultLanguageSettings
       
   439 // Sets default language settings.
       
   440 // (other items were commented in a header).
       
   441 // -----------------------------------------------------------------------------
       
   442 // 
       
   443 void CCbsDbImpSettings::SetDefaultLanguageSettings( 
       
   444     TCbsDbLanguages& aLanguage ) const
       
   445     {
       
   446     // First, set all languages to false.
       
   447     for ( TInt index( 0 ); index < ECbsAll; index++ )
       
   448         {
       
   449         aLanguage.iLanguages[ index ] = EFalse;
       
   450         }
       
   451 
       
   452     // And then set the "All"-choice to true.
       
   453     aLanguage.iLanguages[ ECbsAll ] = ETrue;
       
   454     }
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // CCbsDbImpSettings::__DbgTestInvariant
       
   458 // Checks that the object is in a valid state, and panics if it is not.
       
   459 // (other items were commented in a header).
       
   460 // -----------------------------------------------------------------------------
       
   461 // 
       
   462 void CCbsDbImpSettings::__DbgTestInvariant() const
       
   463     {
       
   464 #if defined(_DEBUG)
       
   465     if ( iSettings.iReceptionStatus < 0 || iSettings.iReceptionStatus > 1 ||
       
   466         iSettings.iTopicDetectionStatus < 0 || 
       
   467         iSettings.iTopicDetectionStatus > 1 ||
       
   468         iObservers == NULL )
       
   469         {
       
   470         User::Invariant();
       
   471         }
       
   472 #endif
       
   473     }
       
   474 
       
   475 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   476 //  End of File