ncdengine/provider/server/src/ncdconfigurationmanagerimpl.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-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:   Implementation of CNcdConfigurationManager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32math.h>
       
    20 
       
    21 #include "ncderrors.h"
       
    22 #include "ncdconfigurationmanagerimpl.h"
       
    23 #include "catalogsdebug.h"
       
    24 #include "catalogscontext.h"
       
    25 #include "catalogsutils.h"
       
    26 #include "ncdstoragemanager.h"
       
    27 #include "ncddatabasestorage.h"
       
    28 #include "ncdstorage.h"
       
    29 #include "ncdkeyvaluemap.h"
       
    30 #include "ncdkeyvaluepair.h"
       
    31 #include "ncdproviderdefines.h"
       
    32 #include "ncdstorageitem.h"
       
    33 #include "ncdserveraddress.h"
       
    34 #include "ncdconfigurationkeys.h"
       
    35 #include "ncdutils.h"
       
    36 #include "catalogsconstants.h"
       
    37 #include "ncdconfigurationobserver.h"
       
    38 #include "ncdserverdetailsimpl.h"
       
    39 #include "ncdproviderutils.h"
       
    40 #include "ncdgeneralmanager.h"
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CContextConfigurationId
       
    45 // ---------------------------------------------------------------------------
       
    46 //    
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // NewLC
       
    50 // ---------------------------------------------------------------------------
       
    51 //         
       
    52 CNcdConfigurationManager::CContextConfiguration* 
       
    53     CNcdConfigurationManager::CContextConfiguration::NewLC( 
       
    54         const MCatalogsContext& aContext, 
       
    55         const TDesC& aId )
       
    56     {
       
    57     CContextConfiguration* self = new( ELeave ) CContextConfiguration(
       
    58         aContext );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL( aId );
       
    61     return self;
       
    62     }
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // FindNewL
       
    67 // ---------------------------------------------------------------------------
       
    68 //         
       
    69 CNcdConfigurationManager::CContextConfiguration* 
       
    70     CNcdConfigurationManager::CContextConfiguration::FindNewL( 
       
    71         const MCatalogsContext& aContext )
       
    72     {
       
    73     CContextConfiguration* self = new( ELeave ) CContextConfiguration(
       
    74         aContext );
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Constructor
       
    81 // ---------------------------------------------------------------------------
       
    82 //         
       
    83 CNcdConfigurationManager::CContextConfiguration::CContextConfiguration( 
       
    84     const MCatalogsContext& aContext ) : 
       
    85     iFamilyId( aContext.FamilyId() ), 
       
    86     iId( NULL ),
       
    87     iConfigurationGeneral( NULL ), 
       
    88     iConfigurationClientInfo( NULL ),
       
    89     iSsid( NULL ),
       
    90     iMasterServer( NULL )    
       
    91     {
       
    92     }
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // ConstructL
       
    97 // ---------------------------------------------------------------------------
       
    98 //         
       
    99 void CNcdConfigurationManager::CContextConfiguration::ConstructL(
       
   100     const TDesC& aId )
       
   101     {
       
   102     DLTRACEIN((""));
       
   103     iId = aId.AllocL();
       
   104     iSsid = KNullDesC8().AllocL();
       
   105     iConfigurationGeneral = CNcdKeyValueMap::NewL();
       
   106     iConfigurationClientInfo = CNcdKeyValueMap::NewL();        
       
   107     
       
   108     DLTRACEOUT((""));
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Destructor
       
   113 // ---------------------------------------------------------------------------
       
   114 //         
       
   115 CNcdConfigurationManager::CContextConfiguration::~CContextConfiguration()
       
   116     {
       
   117     delete iId;
       
   118     delete iConfigurationGeneral;
       
   119     delete iConfigurationClientInfo;
       
   120     delete iSsid;
       
   121     delete iMasterServer;
       
   122     iServerDetails.ResetAndDestroy();
       
   123     iObservers.Close();
       
   124     }
       
   125 
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // ServerDetailsL
       
   129 // ---------------------------------------------------------------------------
       
   130 //         
       
   131 MNcdServerDetails& 
       
   132     CNcdConfigurationManager::CContextConfiguration::ServerDetailsL( 
       
   133     const TDesC& aServerUri,
       
   134     const TDesC& aNamespace )
       
   135     {
       
   136     DLTRACEIN((""));
       
   137     CNcdServerDetails* details = ServerDetails( aServerUri, aNamespace );
       
   138     
       
   139     if ( !details ) 
       
   140         {        
       
   141         DLTRACE(("Creating new details"));
       
   142         details = CNcdServerDetails::NewLC( aServerUri,
       
   143             aNamespace );
       
   144         iServerDetails.AppendL( details );
       
   145         CleanupStack::Pop( details );
       
   146         }
       
   147     return *details;
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // ServerDetailsL
       
   152 // ---------------------------------------------------------------------------
       
   153 //         
       
   154 CNcdServerDetails*
       
   155     CNcdConfigurationManager::CContextConfiguration::ServerDetails( 
       
   156     const TDesC& aServerUri,
       
   157     const TDesC& aNamespace )
       
   158     {
       
   159     DLTRACEIN((""));
       
   160     for ( TInt i = 0; i < iServerDetails.Count(); ++i )
       
   161         {
       
   162         if ( iServerDetails[i]->ServerUri() == aServerUri &&
       
   163              iServerDetails[i]->Namespace() == aNamespace ) 
       
   164             {
       
   165             DLTRACEOUT(("Found details"));
       
   166             return iServerDetails[i];
       
   167             }
       
   168         }
       
   169     DLTRACEOUT(("Details not found"));
       
   170     return NULL;
       
   171     }
       
   172     
       
   173 // ---------------------------------------------------------------------------
       
   174 // ClearServerDetails
       
   175 // ---------------------------------------------------------------------------
       
   176 void CNcdConfigurationManager::CContextConfiguration::ClearServerDetails() 
       
   177     {
       
   178     DLTRACEIN((""));
       
   179     iServerDetails.ResetAndDestroy();
       
   180     }
       
   181     
       
   182 // ---------------------------------------------------------------------------
       
   183 // Notifies the observers.
       
   184 // ---------------------------------------------------------------------------
       
   185 void CNcdConfigurationManager::CContextConfiguration::NotifyObserversL() const 
       
   186     {
       
   187     DLTRACEIN((""));
       
   188     for ( TInt i = 0; i < iObservers.Count(); i++ ) 
       
   189         {
       
   190         iObservers[i]->ConfigurationChangedL();
       
   191         }
       
   192     }
       
   193     
       
   194 // ---------------------------------------------------------------------------
       
   195 // Adds an observer.
       
   196 // ---------------------------------------------------------------------------
       
   197 void CNcdConfigurationManager::CContextConfiguration::AddObserverL(
       
   198     MNcdConfigurationObserver& aObserver ) 
       
   199     {
       
   200     DLTRACEIN((""));
       
   201     if ( iObservers.Find( &aObserver ) == KErrNotFound ) 
       
   202         {
       
   203         iObservers.AppendL( &aObserver );
       
   204         }
       
   205     }
       
   206     
       
   207 // ---------------------------------------------------------------------------
       
   208 // Removes an observer.
       
   209 // ---------------------------------------------------------------------------
       
   210 void CNcdConfigurationManager::CContextConfiguration::RemoveObserver(
       
   211     MNcdConfigurationObserver& aObserver ) 
       
   212     {
       
   213     DLTRACEIN((""));
       
   214     TInt index = iObservers.Find( &aObserver );
       
   215     if ( index != KErrNotFound ) 
       
   216         {
       
   217         iObservers.Remove( index );
       
   218         }
       
   219     }
       
   220 
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // ExternalizeL
       
   224 // ---------------------------------------------------------------------------
       
   225 //         
       
   226 void CNcdConfigurationManager::CContextConfiguration::ExternalizeL( 
       
   227     RWriteStream& aStream )
       
   228     {
       
   229     DLTRACEIN((""));    
       
   230     ExternalizeDesL( *iId, aStream );
       
   231     ExternalizeDesL( *iSsid, aStream );
       
   232     if ( iMasterServer ) 
       
   233         {
       
   234         DLTRACE(("Externalizing master server address"));
       
   235         aStream.WriteInt32L( 1 );
       
   236         iMasterServer->ExternalizeL( aStream );
       
   237         }
       
   238     else 
       
   239         {
       
   240         aStream.WriteInt32L( 0 );
       
   241         }
       
   242     
       
   243     DLTRACE(("Externalizing %d server details", iServerDetails.Count() ));    
       
   244     // Externalize server capabilities.
       
   245     aStream.WriteInt32L( iServerDetails.Count() );
       
   246     for ( TInt i = 0; i < iServerDetails.Count(); i++ ) 
       
   247         {
       
   248         iServerDetails[i]->ExternalizeL( aStream );
       
   249         }            
       
   250             
       
   251     DLTRACEOUT((""));
       
   252     }
       
   253      
       
   254      
       
   255 // ---------------------------------------------------------------------------
       
   256 // InternalizeL
       
   257 // ---------------------------------------------------------------------------
       
   258 //         
       
   259 void CNcdConfigurationManager::CContextConfiguration::InternalizeL( 
       
   260     RReadStream& aStream )
       
   261     {
       
   262     DLTRACEIN((""));
       
   263     InternalizeDesL( iId, aStream );
       
   264     InternalizeDesL( iSsid, aStream );
       
   265     TInt32 masterServerExists = aStream.ReadInt32L();
       
   266     if ( masterServerExists ) 
       
   267         {
       
   268         DLTRACE(("Internalizing master server address"));
       
   269         CNcdServerAddress* newServer = CNcdServerAddress::NewL( aStream );
       
   270         delete iMasterServer;
       
   271         iMasterServer = newServer;
       
   272         }
       
   273     else 
       
   274         {
       
   275         delete iMasterServer;
       
   276         iMasterServer = NULL;
       
   277         }
       
   278     
       
   279     
       
   280     // Internalize server capabilities.
       
   281     iServerDetails.ResetAndDestroy();
       
   282     TInt32 serverDetailCount = aStream.ReadInt32L();
       
   283     
       
   284     DLTRACE(("Internalizing %d server details", serverDetailCount ));
       
   285     
       
   286     iServerDetails.ReserveL( serverDetailCount );
       
   287     for ( TInt i = 0; i < serverDetailCount; i++ ) 
       
   288         {
       
   289         CNcdServerDetails* details = CNcdServerDetails::NewL( aStream );
       
   290         TInt err = iServerDetails.Append( details );
       
   291         DASSERT( err == KErrNone );
       
   292         }        
       
   293     
       
   294     DLTRACEOUT(("Internalization successful"));
       
   295     }
       
   296             
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // CNcdConfigurationManager
       
   300 // ---------------------------------------------------------------------------
       
   301 //    
       
   302 
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // Creator
       
   306 // ---------------------------------------------------------------------------
       
   307 //    
       
   308 CNcdConfigurationManager* CNcdConfigurationManager::NewL( 
       
   309     CNcdGeneralManager& aGeneralManager )
       
   310     {
       
   311     CNcdConfigurationManager* self = new( ELeave ) CNcdConfigurationManager(
       
   312         aGeneralManager );
       
   313     CleanupStack::PushL( self );
       
   314     self->ConstructL();
       
   315     CleanupStack::Pop();
       
   316     return self;
       
   317     }
       
   318     
       
   319     
       
   320 // ---------------------------------------------------------------------------
       
   321 // Destructor
       
   322 // ---------------------------------------------------------------------------
       
   323 //        
       
   324 CNcdConfigurationManager::~CNcdConfigurationManager()
       
   325     {
       
   326     DLTRACEIN((""));
       
   327     iConfigurations.ResetAndDestroy();
       
   328     DLTRACEOUT((""));
       
   329     }
       
   330 
       
   331 // ---------------------------------------------------------------------------
       
   332 // Adds a new observer
       
   333 // ---------------------------------------------------------------------------
       
   334 void CNcdConfigurationManager::AddObserverL(
       
   335     MNcdConfigurationObserver& aObserver,
       
   336     const MCatalogsContext& aContext ) 
       
   337     {
       
   338     DLTRACEIN((""));
       
   339     TInt index = CreateContextConfigurationL( aContext );
       
   340     CContextConfiguration* conf = iConfigurations[index];
       
   341     conf->AddObserverL( aObserver );
       
   342     }
       
   343     
       
   344 // ---------------------------------------------------------------------------
       
   345 // Removes a observer
       
   346 // ---------------------------------------------------------------------------
       
   347 void CNcdConfigurationManager::RemoveObserver(
       
   348     MNcdConfigurationObserver& aObserver )
       
   349     {
       
   350     DLTRACEIN((""));
       
   351     for ( TInt i = 0; i < iConfigurations.Count(); i++ ) 
       
   352         {
       
   353         iConfigurations[i]->RemoveObserver( aObserver );
       
   354         }
       
   355     }
       
   356  
       
   357 // ---------------------------------------------------------------------------
       
   358 // Master server address getter
       
   359 // ---------------------------------------------------------------------------
       
   360 //        
       
   361 const TDesC& CNcdConfigurationManager::MasterServerAddressL( 
       
   362     const MCatalogsContext& aContext ) const
       
   363     {
       
   364     DLTRACEIN((""));
       
   365     
       
   366     TInt index = FindConfigurationL( aContext );
       
   367     
       
   368     if ( index != KErrNotFound )
       
   369         {
       
   370         // Context has a configuration        
       
   371         const CContextConfiguration& config( *iConfigurations[index] );
       
   372     
       
   373         // See if MSA exists and is valid
       
   374         if ( config.iMasterServer && config.iMasterServer->IsValid() ) 
       
   375             {
       
   376             DLINFO(( _L("Using master server address: %S"), 
       
   377                 &config.iMasterServer->Address() ));
       
   378             return config.iMasterServer->Address();
       
   379             }
       
   380         
       
   381         
       
   382         // Check if the client has overridden master server address
       
   383         CNcdKeyValueMap::KeyValueIndex keyIndex = 
       
   384             config.iConfigurationGeneral->KeyExists( 
       
   385                 NcdConfigurationKeys::KMasterServer );
       
   386         
       
   387         if ( keyIndex != KErrNotFound && !config.iUseHardcodedMasterServer ) 
       
   388             {
       
   389             DLINFO(( _L("Using Master Server Address from client: %S"), 
       
   390                 &config.iConfigurationGeneral->ValueByIndex( keyIndex ) ));
       
   391             return config.iConfigurationGeneral->ValueByIndex( keyIndex );
       
   392             }
       
   393         }
       
   394     
       
   395     if ( !NcdProviderDefines::KMasterServerUri().Length() ) 
       
   396         {
       
   397         DLERROR(("No master server URI. Leaving with KNcdErrorNoMasterServerUri"));
       
   398         User::Leave( KNcdErrorNoMasterServerUri );
       
   399         }
       
   400         
       
   401     // Use hardcoded MSA     
       
   402     DLTRACEOUT(( _L("Using hardcoded: %S"), 
       
   403         &NcdProviderDefines::KMasterServerUri() ));
       
   404     return NcdProviderDefines::KMasterServerUri();    
       
   405     }
       
   406 
       
   407     
       
   408 // ---------------------------------------------------------------------------
       
   409 // Master server address validity checker
       
   410 // ---------------------------------------------------------------------------
       
   411 //            
       
   412 TBool CNcdConfigurationManager::IsMasterServerAddressValidL( 
       
   413     const MCatalogsContext& aContext ) const
       
   414     {
       
   415     DLTRACEIN((""));
       
   416 
       
   417     TInt index = FindConfigurationL( aContext );
       
   418     
       
   419     if ( index != KErrNotFound )
       
   420         {
       
   421         
       
   422         // Context has a configuration        
       
   423         const CContextConfiguration& config( *iConfigurations[index] );
       
   424     
       
   425         // See if MSA exists
       
   426         if ( config.iMasterServer )
       
   427             {
       
   428             DLTRACE(("Context has set a Master server address"));            
       
   429             return config.iMasterServer->IsValid();
       
   430             }
       
   431         }
       
   432 
       
   433     // Other MSA's are always valid
       
   434     return ETrue;
       
   435     }
       
   436     
       
   437 // ---------------------------------------------------------------------------
       
   438 // Master server address & validity setter
       
   439 // ---------------------------------------------------------------------------
       
   440 //        
       
   441 void CNcdConfigurationManager::SetMasterServerAddressL(
       
   442     const MCatalogsContext& aContext,
       
   443     const TDesC& aAddress, 
       
   444     const TInt64& aValidity )
       
   445     {
       
   446     DLTRACEIN(( _L("address: %S, validity: %d"), &aAddress, aValidity ));
       
   447     if ( !aAddress.Length() ) 
       
   448         {
       
   449         User::Leave( KErrArgument );
       
   450         }
       
   451 
       
   452     
       
   453     CNcdServerAddress* address = CNcdServerAddress::NewLC(
       
   454         aAddress, aValidity );
       
   455         
       
   456     SetMasterServerAddressL( aContext, address );
       
   457     CleanupStack::Pop( address );
       
   458         
       
   459     DLTRACEOUT((""));
       
   460     }
       
   461 
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // Master server address & validity setter
       
   465 // ---------------------------------------------------------------------------
       
   466 //        
       
   467 void CNcdConfigurationManager::SetMasterServerAddressL(
       
   468     const MCatalogsContext& aContext,
       
   469     CNcdServerAddress* aAddress )
       
   470     {
       
   471     DLTRACEIN((""));
       
   472     TInt index = CreateContextConfigurationL( aContext );    
       
   473     CContextConfiguration& config( *iConfigurations[index] );    
       
   474     
       
   475     delete config.iMasterServer;
       
   476     config.iMasterServer = aAddress;
       
   477 
       
   478     config.iUseHardcodedMasterServer = EFalse;
       
   479 
       
   480     // Save updated master server address
       
   481     SaveDataL( aContext.FamilyId().Name(), 
       
   482         NcdProviderDefines::ENcdContextData, config );
       
   483         
       
   484     config.NotifyObserversL();    
       
   485     }
       
   486 
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // Resets the master server address
       
   490 // ---------------------------------------------------------------------------
       
   491 //        
       
   492 void CNcdConfigurationManager::ResetMasterServerAddressL( 
       
   493     const MCatalogsContext& aContext )
       
   494     {
       
   495     DLTRACEIN((""));
       
   496     // Sets the current overriding master server address as invalid
       
   497     TInt index = FindConfigurationL( aContext );
       
   498     if ( index != KErrNotFound ) 
       
   499         {
       
   500         if ( iConfigurations[index]->iMasterServer ) 
       
   501             {
       
   502             DLINFO(("Reverting to client-set/hardcoded MasterServer"));
       
   503             delete iConfigurations[index]->iMasterServer;
       
   504             iConfigurations[index]->iMasterServer = NULL;
       
   505             }
       
   506         else 
       
   507             {             
       
   508             // Check if the client has overridden master server address
       
   509             CNcdKeyValueMap::KeyValueIndex keyIndex = 
       
   510                 iConfigurations[index]->iConfigurationGeneral->KeyExists( 
       
   511                     NcdConfigurationKeys::KMasterServer );
       
   512 
       
   513             if ( keyIndex != KErrNotFound ) 
       
   514                 {
       
   515                 // Use hardcoded instead of the one set by the client
       
   516                 iConfigurations[index]->iUseHardcodedMasterServer = ETrue;                
       
   517                 }        
       
   518             }
       
   519         iConfigurations[index]->NotifyObserversL();
       
   520         }
       
   521     DLTRACEOUT((""));
       
   522     }
       
   523 
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // Client ID getter
       
   527 // ---------------------------------------------------------------------------
       
   528 //        
       
   529 const TDesC& CNcdConfigurationManager::ClientIdL(
       
   530     const MCatalogsContext& aContext )
       
   531     {
       
   532     DLTRACEIN((""));
       
   533     
       
   534     TInt index = CreateContextConfigurationL( aContext );
       
   535     CContextConfiguration& data( *iConfigurations[index] );
       
   536     if ( !data.iId->Length() ) 
       
   537         {
       
   538         DLTRACE(("Trying to load id from db"));
       
   539         TRAPD( err,
       
   540         LoadDataL( aContext.FamilyId().Name(), 
       
   541             NcdProviderDefines::ENcdContextData,
       
   542             data ) );
       
   543         
       
   544         if ( err != KErrNone && err != KErrNotFound ) 
       
   545             {
       
   546             // serious error
       
   547             User::Leave( err );
       
   548             }
       
   549         
       
   550         if ( !data.iId->Length() )
       
   551             {
       
   552             // Generate a new client id
       
   553             HBufC* id = GenerateClientIdLC();
       
   554             delete data.iId;
       
   555             data.iId = id;
       
   556             CleanupStack::Pop( id );
       
   557             
       
   558             // Save the client id
       
   559             SaveDataL( aContext.FamilyId().Name(), 
       
   560                 NcdProviderDefines::ENcdContextData, data );
       
   561             }
       
   562         }
       
   563     DLTRACEOUT(( _L("Id: %S"), data.iId ));
       
   564     return *data.iId;
       
   565     
       
   566     }
       
   567 
       
   568 
       
   569 // ---------------------------------------------------------------------------
       
   570 // SSID setter
       
   571 // ---------------------------------------------------------------------------
       
   572 //
       
   573 void CNcdConfigurationManager::SetSsidL( const MCatalogsContext& aContext, 
       
   574     HBufC8* aSsid )
       
   575     {
       
   576     DLTRACEIN(( "" ));    
       
   577     TInt index = CreateContextConfigurationL( aContext );
       
   578     if ( iConfigurations[index]->iSsid ) 
       
   579         {
       
   580         DLTRACE(( "Deleting old SSID" ));
       
   581         delete iConfigurations[index]->iSsid;
       
   582         }
       
   583         
       
   584     iConfigurations[index]->iSsid = aSsid;
       
   585 
       
   586     // Save updated Ssid
       
   587     SaveDataL( aContext.FamilyId().Name(), 
       
   588         NcdProviderDefines::ENcdContextData, *iConfigurations[index] );
       
   589 
       
   590     DLTRACEOUT(( "" ));
       
   591             
       
   592     }
       
   593      
       
   594        
       
   595 // ---------------------------------------------------------------------------
       
   596 // SSID getter
       
   597 // ---------------------------------------------------------------------------
       
   598 //
       
   599 const TDesC8& CNcdConfigurationManager::SsidL( 
       
   600     const MCatalogsContext& aContext )
       
   601     {
       
   602     // Try to find the configuration from both memory and db
       
   603     TInt index = CreateContextConfigurationL( aContext );
       
   604     if ( index != KErrNotFound && iConfigurations[index]->iSsid ) 
       
   605         {
       
   606         return *iConfigurations[index]->iSsid;
       
   607         }
       
   608     DLERROR(("No SSID set!"));    
       
   609     return KNullDesC8();
       
   610     }
       
   611     
       
   612 
       
   613 // ---------------------------------------------------------------------------
       
   614 // Server details getter
       
   615 // ---------------------------------------------------------------------------
       
   616 //
       
   617 MNcdServerDetails& CNcdConfigurationManager::ServerDetailsL( 
       
   618     const MCatalogsContext& aContext, 
       
   619     const TDesC& aServerUri,
       
   620     const TDesC& aNamespace )
       
   621     {
       
   622     DLTRACEIN(( _L("ServerURI: %S, Namespace: %S"), &aServerUri, &aNamespace ));
       
   623     // Get index for the config. A new configuration is created if necessary    
       
   624     TInt index = CreateContextConfigurationL( aContext );    
       
   625     
       
   626     // Get server details. New details are created if necessary
       
   627     return iConfigurations[index]->ServerDetailsL( aServerUri, aNamespace );
       
   628     }
       
   629     
       
   630 
       
   631 // ---------------------------------------------------------------------------
       
   632 // Server details getter
       
   633 // ---------------------------------------------------------------------------
       
   634 //
       
   635 const MNcdServerDetails* CNcdConfigurationManager::ServerDetails( 
       
   636     const MCatalogsContext& aContext, 
       
   637     const TDesC& aServerUri,
       
   638     const TDesC& aNamespace ) const
       
   639     {
       
   640     DLTRACEIN(( _L("ServerURI: %S, Namespace: %S"), &aServerUri, &aNamespace ));
       
   641     // Get index for the config. 
       
   642     TInt index = KErrNotFound;
       
   643     
       
   644     TRAPD( err, index = FindConfigurationL( aContext ) );    
       
   645     if ( err == KErrNone && index != KErrNotFound ) 
       
   646         {
       
   647         // Get server details. New details are created if necessary
       
   648         return iConfigurations[index]->ServerDetails( aServerUri, aNamespace );
       
   649         }
       
   650     DLTRACEOUT(("Details not found or error occured"));
       
   651     return NULL;
       
   652     }
       
   653     
       
   654     
       
   655 // ---------------------------------------------------------------------------
       
   656 // Saves context configuration details to database.
       
   657 // ---------------------------------------------------------------------------
       
   658 void CNcdConfigurationManager::SaveConfigurationToDbL(
       
   659     const MCatalogsContext& aContext ) 
       
   660     {
       
   661     DLTRACEIN((""));
       
   662     
       
   663     // Get index for the config.
       
   664     TInt index = FindConfigurationL( aContext );    
       
   665     if ( index == KErrNotFound ) 
       
   666         {
       
   667         DLTRACEOUT(("Configuration not found"));
       
   668         return;
       
   669         }
       
   670     
       
   671     // Get server details.
       
   672     CContextConfiguration* config = iConfigurations[index];
       
   673     
       
   674     // Save the configuration.
       
   675     SaveDataL( aContext.FamilyId().Name(), 
       
   676         NcdProviderDefines::ENcdContextData, *config );
       
   677     }
       
   678     
       
   679 // ---------------------------------------------------------------------------
       
   680 // Clears the server capabilities.
       
   681 // ---------------------------------------------------------------------------
       
   682 void CNcdConfigurationManager::ClearServerCapabilitiesL(
       
   683     const MCatalogsContext& aContext )
       
   684     {
       
   685     DLTRACEIN((""));
       
   686     
       
   687     // Get index for the config.
       
   688     TInt index = FindConfigurationL( aContext );
       
   689     if ( index == KErrNotFound ) 
       
   690         {
       
   691         return;
       
   692         }
       
   693         
       
   694     // Get the config and clear server details.
       
   695     CContextConfiguration* config = iConfigurations[index];
       
   696     config->ClearServerDetails();
       
   697     
       
   698     // Save the cleared config.
       
   699     SaveConfigurationToDbL( aContext );        
       
   700     }
       
   701     
       
   702 
       
   703 // ---------------------------------------------------------------------------
       
   704 // AddConfigurationL
       
   705 // ---------------------------------------------------------------------------
       
   706 //
       
   707 void CNcdConfigurationManager::AddConfigurationL( 
       
   708     const MCatalogsContext& aContext, CNcdKeyValuePair* aConfig )
       
   709     {    
       
   710     DLTRACEIN(( "" ));
       
   711     
       
   712     // Ensure aConfig is deleted if a leave occurs
       
   713     CleanupStack::PushL( aConfig );
       
   714 
       
   715     // Get index for the config. A new configuration is created if necessary    
       
   716     TInt index = CreateContextConfigurationL( aContext );    
       
   717     
       
   718     CNcdKeyValueMap& map( ConfigurationByCategory( index, aConfig->Key() ) );
       
   719     
       
   720     if ( aConfig->Key() == NcdConfigurationKeys::KCapability ) 
       
   721         {
       
   722         DLTRACE(("Adding a capability"));
       
   723         if ( map.PairExists( *aConfig ) == KErrNotFound ) 
       
   724             {            
       
   725             map.AddL( aConfig );
       
   726             }
       
   727         else 
       
   728             {
       
   729             DLTRACE(("Capability already exists"));
       
   730             CleanupStack::PopAndDestroy( aConfig );
       
   731             return;
       
   732             }
       
   733         }
       
   734     else 
       
   735         {
       
   736         DLTRACE(("Adding something that is not a capability"));
       
   737         // Replaces the old key if necessary
       
   738         map.ReplaceL( aConfig );
       
   739 
       
   740         if ( aConfig->Key() == NcdConfigurationKeys::KMasterServer ) 
       
   741             {
       
   742             // Ensure that the new master server is used instead of hardcoded one
       
   743             iConfigurations[index]->iUseHardcodedMasterServer = EFalse;
       
   744             }
       
   745         }
       
   746                 
       
   747     CleanupStack::Pop( aConfig );
       
   748     
       
   749     DLTRACEOUT(("" ));
       
   750     }
       
   751 
       
   752 
       
   753 // ---------------------------------------------------------------------------
       
   754 // RemoveConfiguration
       
   755 // ---------------------------------------------------------------------------
       
   756 //
       
   757 TInt CNcdConfigurationManager::RemoveConfigurationL( 
       
   758     const MCatalogsContext& aContext, const TDesC& aKey )
       
   759     {
       
   760     DLTRACEIN((""));
       
   761     TInt index = FindConfigurationL( aContext );
       
   762     if ( index != KErrNotFound ) 
       
   763         {
       
   764         return ConfigurationByCategory( index, aKey ).Remove( aKey );        
       
   765         }
       
   766     return index;    
       
   767     }
       
   768     
       
   769     
       
   770 // ---------------------------------------------------------------------------
       
   771 // ConfigurationsL
       
   772 // ---------------------------------------------------------------------------
       
   773 //
       
   774 CNcdKeyValueMap* CNcdConfigurationManager::ConfigurationsLC( 
       
   775     const MCatalogsContext& aContext )
       
   776     {
       
   777     DLTRACEIN((""));
       
   778     TInt index = FindConfigurationL( aContext );
       
   779     if ( index == KErrNotFound ) 
       
   780         {
       
   781         User::Leave( KErrNotFound );
       
   782         }
       
   783     
       
   784     DASSERT( iConfigurations[index]->iConfigurationGeneral && 
       
   785         iConfigurations[index]->iConfigurationClientInfo );
       
   786         
       
   787     // Create a new map and append all different configuration categories to
       
   788     // it
       
   789     CNcdKeyValueMap* newMap = CNcdKeyValueMap::NewL( 
       
   790         *iConfigurations[index]->iConfigurationGeneral );
       
   791     CleanupStack::PushL( newMap );
       
   792     
       
   793     newMap->AppendL( *iConfigurations[index]->iConfigurationClientInfo );
       
   794         
       
   795     DLTRACEOUT((""));
       
   796     return newMap;
       
   797     }
       
   798 
       
   799 
       
   800 // ---------------------------------------------------------------------------
       
   801 // ConfigurationsL
       
   802 // ---------------------------------------------------------------------------
       
   803 //        
       
   804 CNcdKeyValueMap& CNcdConfigurationManager::ConfigurationsL(
       
   805     const MCatalogsContext& aContext, 
       
   806     TNcdConfigurationCategory aCategory )
       
   807     {
       
   808     TInt index = FindConfigurationL( aContext );
       
   809     if ( index == KErrNotFound ) 
       
   810         {
       
   811         User::Leave( KErrNotFound );
       
   812         }
       
   813     
       
   814     return ConfigurationByCategory( index, aCategory );  
       
   815     }
       
   816 
       
   817     
       
   818 // ---------------------------------------------------------------------------
       
   819 // Constructor
       
   820 // ---------------------------------------------------------------------------
       
   821 //        
       
   822 CNcdConfigurationManager::CNcdConfigurationManager( 
       
   823     CNcdGeneralManager& aGeneralManager ) : 
       
   824     iGeneralManager( aGeneralManager),
       
   825     iStorageManager( aGeneralManager.StorageManager() )
       
   826     {
       
   827     }
       
   828     
       
   829     
       
   830 // ---------------------------------------------------------------------------
       
   831 // ConstructL
       
   832 // ---------------------------------------------------------------------------
       
   833 //        
       
   834 void CNcdConfigurationManager::ConstructL()
       
   835     {
       
   836     }
       
   837     
       
   838 
       
   839 // ---------------------------------------------------------------------------
       
   840 // Generates a new client id
       
   841 // ---------------------------------------------------------------------------
       
   842 //        
       
   843 HBufC* CNcdConfigurationManager::GenerateClientIdLC() const
       
   844     {
       
   845     DLTRACEIN((""));
       
   846 #ifdef USE_OLD_CLIENT_ID_GENERATOR    
       
   847     // Enough for timestamp (64bit) + rand (32bit)
       
   848     HBufC* id = HBufC::NewLC( NcdProviderDefines::KClientIdMaxLength );  
       
   849 
       
   850     TTime now;
       
   851     now.HomeTime();
       
   852     TInt64 int64 = now.Int64();
       
   853 
       
   854     TInt rand = Math::Rand( int64 );
       
   855 
       
   856     TPtr ptr( id->Des() );
       
   857 
       
   858 
       
   859     ptr.NumFixedWidth( int64 >> 32, EHex, 8 );
       
   860     ptr.AppendNumFixedWidth( int64 & 0xffffffff, EHex, 8 );
       
   861     ptr.AppendNumFixedWidth( rand, EHex, 8 );
       
   862     DLTRACEOUT(( _L("Client ID: %S"), id ));
       
   863 #else
       
   864 
       
   865 
       
   866     // Enough for timestamp (64bit) + rand (32bit)
       
   867     HBufC* id = HBufC::NewLC( NcdProviderDefines::KClientIdMaxLength );  
       
   868 
       
   869     TTime now;
       
   870     now.HomeTime();
       
   871     TInt64 int64 = now.Int64();
       
   872     
       
   873 
       
   874     TInt64 freeSpace = FreeDiskSpaceL( CNcdProviderUtils::FileSession(),
       
   875         EDriveC ) + int64;            
       
   876 
       
   877     // Use both timestamp and free disk space to get a unique seed
       
   878     TInt rand = Math::Rand( freeSpace );
       
   879 
       
   880     TPtr ptr( id->Des() );
       
   881 
       
   882 
       
   883     ptr.NumFixedWidth( int64 >> 32, EHex, 8 );
       
   884     ptr.AppendNumFixedWidth( int64 & 0xffffffff, EHex, 8 );
       
   885     ptr.AppendNumFixedWidth( rand, EHex, 8 );
       
   886     DLTRACEOUT(( _L("Client ID: %S"), id ));
       
   887 
       
   888 
       
   889 #endif    
       
   890     return id;
       
   891     }
       
   892     
       
   893     
       
   894 // ---------------------------------------------------------------------------
       
   895 // MatchContexts
       
   896 // ---------------------------------------------------------------------------
       
   897 //    
       
   898 TBool CNcdConfigurationManager::MatchContexts( 
       
   899     const CContextConfiguration& aFirst,
       
   900     const CContextConfiguration& aSecond )
       
   901     {
       
   902     return ( aFirst.iFamilyId == aSecond.iFamilyId );
       
   903     }
       
   904     
       
   905 
       
   906 // ---------------------------------------------------------------------------
       
   907 // FindConfiguration
       
   908 // ---------------------------------------------------------------------------
       
   909 //        
       
   910 TInt CNcdConfigurationManager::FindConfigurationL( 
       
   911     const MCatalogsContext& aContext ) const
       
   912     {
       
   913     TIdentityRelation<CContextConfiguration> match( 
       
   914         CNcdConfigurationManager::MatchContexts );
       
   915 
       
   916     CContextConfiguration* config = CContextConfiguration::FindNewL( aContext );
       
   917 
       
   918     TInt index = iConfigurations.Find( config, match );
       
   919     delete config;
       
   920     return index;
       
   921     }
       
   922 
       
   923 
       
   924 // ---------------------------------------------------------------------------
       
   925 // DetermineCategory
       
   926 // ---------------------------------------------------------------------------
       
   927 //        
       
   928 MNcdUserConfiguration::TNcdConfigurationCategory 
       
   929     CNcdConfigurationManager::DetermineCategory(
       
   930     const TDesC& aKey ) const
       
   931     {
       
   932     if ( aKey == NcdConfigurationKeys::KMasterServer 
       
   933          || aKey == NcdConfigurationKeys::KMaxStorageSize )
       
   934         {
       
   935         return ENcdConfigurationGeneral;
       
   936         }
       
   937     return ENcdConfigurationClientInfo;
       
   938     }
       
   939 
       
   940 
       
   941 // ---------------------------------------------------------------------------
       
   942 // ConfigurationByCategory
       
   943 // ---------------------------------------------------------------------------
       
   944 //        
       
   945 CNcdKeyValueMap& CNcdConfigurationManager::ConfigurationByCategory( 
       
   946     TInt aIndex, const TDesC& aKey )
       
   947     {
       
   948     TNcdConfigurationCategory category = DetermineCategory( aKey );
       
   949     return ConfigurationByCategory( aIndex, category );
       
   950     }
       
   951     
       
   952 // ---------------------------------------------------------------------------
       
   953 // ConfigurationByCategory
       
   954 // ---------------------------------------------------------------------------
       
   955 //        
       
   956 CNcdKeyValueMap& CNcdConfigurationManager::ConfigurationByCategory( TInt aIndex, 
       
   957     TNcdConfigurationCategory aCategory )
       
   958     {
       
   959     
       
   960     switch( aCategory )
       
   961         {
       
   962         case ENcdConfigurationGeneral:
       
   963             {
       
   964             return *iConfigurations[aIndex]->iConfigurationGeneral;
       
   965             }
       
   966         
       
   967         case ENcdConfigurationClientInfo:
       
   968             {
       
   969             return *iConfigurations[aIndex]->iConfigurationClientInfo;
       
   970             }
       
   971         default:
       
   972             {
       
   973             DASSERT( 0 );
       
   974             }
       
   975         }
       
   976     
       
   977     // For the compiler
       
   978     CNcdKeyValueMap* ignore = NULL;    
       
   979     return *ignore;
       
   980     }
       
   981     
       
   982 
       
   983 
       
   984 // ---------------------------------------------------------------------------
       
   985 // Create context configuration
       
   986 // ---------------------------------------------------------------------------
       
   987 //        
       
   988 TInt CNcdConfigurationManager::CreateContextConfigurationL( 
       
   989     const MCatalogsContext& aContext )
       
   990     {    
       
   991     DLTRACEIN((""));
       
   992     
       
   993     TInt index = FindConfigurationL( aContext );
       
   994     if ( index == KErrNotFound ) 
       
   995         {     
       
   996                    
       
   997         DLTRACE(( "Creating a new configuration for context" ));
       
   998         
       
   999         // Create new config
       
  1000         CContextConfiguration* newConfig = 
       
  1001             CContextConfiguration::NewLC( aContext, KNullDesC() );
       
  1002         
       
  1003         iConfigurations.AppendL( newConfig );    
       
  1004         CleanupStack::Pop( newConfig );        
       
  1005         
       
  1006         // Try to load data from database
       
  1007         TRAPD( err, LoadDataL( aContext.FamilyId().Name(), 
       
  1008             NcdProviderDefines::ENcdContextData, *newConfig ) );
       
  1009             
       
  1010         if ( err != KErrNone && err != KErrNotFound ) 
       
  1011             {
       
  1012             DLERROR(( "Context configuration loading failed with: %d", err ));
       
  1013             User::Leave( err );
       
  1014             }
       
  1015         
       
  1016         // update the index
       
  1017         index = iConfigurations.Count() - 1;
       
  1018         }
       
  1019     
       
  1020     DLTRACEOUT(( "index: %d", index ));    
       
  1021     return index;    
       
  1022     }
       
  1023     
       
  1024     
       
  1025    
       
  1026 // ---------------------------------------------------------------------------
       
  1027 // Saves data
       
  1028 // ---------------------------------------------------------------------------
       
  1029 //        
       
  1030 void CNcdConfigurationManager::SaveDataL( const TDesC& aId,
       
  1031     NcdProviderDefines::TNcdDatabaseDataType aType, 
       
  1032     MNcdStorageDataItem& aDataItem )
       
  1033     {
       
  1034     DLTRACEIN(( _L("Id: %S, type: %d"), &aId, aType ));
       
  1035     MNcdStorage& storage = iStorageManager.ProviderStorageL(
       
  1036         iGeneralManager.FamilyName() );
       
  1037     
       
  1038     // NOTE: this creates the database if it does not already exist
       
  1039     MNcdDatabaseStorage& database = 
       
  1040         storage.DatabaseStorageL( 
       
  1041             NcdProviderDefines::KDefaultDatabaseUid );
       
  1042             
       
  1043     // Get/create the storage item where the data is saved
       
  1044     // Note: database has the ownership of the item
       
  1045     MNcdStorageItem* item = 
       
  1046         database.StorageItemL( aId, 
       
  1047             aType );    
       
  1048     
       
  1049     
       
  1050     // Save new item to database
       
  1051     item->SetDataItem( &aDataItem );
       
  1052     item->OpenL();
       
  1053     
       
  1054     // Calls ExternalizeL for the node
       
  1055     item->WriteDataL();
       
  1056     
       
  1057     // Save the node to the database.
       
  1058     // The node object implements MNcdStorageDataItem interface.
       
  1059     // So, the externalize function will insert the data to the stream
       
  1060     // that the database handler will save to the database.
       
  1061     item->SaveL();      
       
  1062     DLTRACEOUT((""));      
       
  1063     }
       
  1064 
       
  1065 
       
  1066 // ---------------------------------------------------------------------------
       
  1067 // Loads data
       
  1068 // ---------------------------------------------------------------------------
       
  1069 //        
       
  1070 void CNcdConfigurationManager::LoadDataL( const TDesC& aId,
       
  1071     NcdProviderDefines::TNcdDatabaseDataType aType, 
       
  1072     MNcdStorageDataItem& aDataItem )
       
  1073     {
       
  1074     DLTRACEIN(( _L("Id: %S, type: %d"), &aId, aType ));
       
  1075     MNcdStorage& storage = iStorageManager.ProviderStorageL( 
       
  1076         iGeneralManager.FamilyName() );
       
  1077     
       
  1078     // NOTE: this creates the database if it does not already exist
       
  1079     MNcdDatabaseStorage& database = 
       
  1080         storage.DatabaseStorageL( 
       
  1081             NcdProviderDefines::KDefaultDatabaseUid );
       
  1082 
       
  1083 
       
  1084    if( !database.ItemExistsInStorageL( aId, aType ) )
       
  1085         {
       
  1086         DLINFO(("Item not found"));
       
  1087         User::Leave( KErrNotFound );
       
  1088         }
       
  1089             
       
  1090     // Get/create the storage item where the data is saved
       
  1091     // Note: database has the ownership of the item
       
  1092     MNcdStorageItem* item = 
       
  1093         database.StorageItemL( aId, 
       
  1094             aType );    
       
  1095                
       
  1096     item->SetDataItem( &aDataItem );
       
  1097     item->ReadDataL();    
       
  1098     
       
  1099     DLTRACEOUT((""));      
       
  1100     }