ncdengine/provider/server/src/ncdproviderimpl.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:   Implements CNcdProvider class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // For streams
       
    20 #include <s32mem.h>
       
    21 
       
    22 // RFs
       
    23 #include <f32file.h>
       
    24 #include <bautils.h>
       
    25 
       
    26 #include <es_sock.h> // KErrConnectionTerminated
       
    27 #include <hash.h>
       
    28 
       
    29 #include "ncdproviderimpl.h"
       
    30 #include "catalogsbasemessage.h"
       
    31 #include "catalogssession.h"
       
    32 #include "ncdnodemanager.h"
       
    33 #include "ncdfavoritemanagerimpl.h"
       
    34 #include "ncdpurchasehistorydbimpl.h"
       
    35 #include "ncdoperationmanager.h"
       
    36 #include "ncdnodeimpl.h"
       
    37 #include "ncdstoragemanagerimpl.h"
       
    38 #include "ncdprotocolimpl.h"
       
    39 #include "catalogsdebug.h"
       
    40 #include "catalogsutils.h"
       
    41 #include "catalogstransportimpl.h"
       
    42 #include "catalogstransporttypes.h"
       
    43 #include "ncdkeyvaluepair.h"
       
    44 #include "ncdkeyvaluemap.h"
       
    45 #include "ncdproviderdefines.h"
       
    46 #include "ncdconfigurationmanagerimpl.h"
       
    47 #include "ncdconfigurationkeys.h"
       
    48 #include "catalogsuids.h"
       
    49 #include "ncdutils.h"
       
    50 #include "catalogscontext.h"
       
    51 #include "catalogshttpsession.h"
       
    52 #include "catalogssmssession.h"
       
    53 #include "catalogshttpconfig.h"
       
    54 #include "catalogsaccesspointmanagerimpl.h"
       
    55 #include "ncdsubscriptionmanagerimpl.h"
       
    56 #include "ncdpreviewmanager.h"
       
    57 #include "ncdproviderutils.h"
       
    58 #include "catalogsuids.h"
       
    59 #include "ncdengineconfiguration.h"
       
    60 #include "catalogsconstants.h"
       
    61 #include "ncddatabaseversions.h"
       
    62 #include "ncdsessionhandler.h"
       
    63 #include "ncderrors.h"
       
    64 #include "ncdreportmanager.h"
       
    65 #include "ncdnodeseeninfo.h"
       
    66 #include "ncdconnectionmethod.h"
       
    67 #include "catalogsconnectionmethod.h"
       
    68 #include "ncdhttputils.h"
       
    69 #include "ncdserverreportmanagerimpl.h"
       
    70 #include "ncdnodecachecleanermanager.h"
       
    71 #include "ncdnodecachecleaner.h"
       
    72 #include "ncdprovideroptions.h"
       
    73 #include "ncdprovidercloseobserver.h"
       
    74 #include "ncdgeneralmanager.h"
       
    75 
       
    76 #ifdef __WINS__
       
    77 // Fake SSID for emulator
       
    78 _LIT8( KEmulatorFakeSsid, "emulatorssid" );
       
    79 #endif 
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // NewLC
       
    84 // ---------------------------------------------------------------------------
       
    85 //   
       
    86 CNcdProvider* CNcdProvider::NewLC( 
       
    87     const TUid& aFamilyId,
       
    88     MNcdProviderCloseObserver& aCloseObserver,
       
    89     const TDesC& aEngineRoot,
       
    90     CCatalogsTransport& aTransport,
       
    91     CNcdStorageManager& aStorageManager )
       
    92     {
       
    93     CNcdProvider* self = new( ELeave ) CNcdProvider( 
       
    94         aFamilyId, 
       
    95         aCloseObserver,
       
    96         aEngineRoot,
       
    97         aTransport,
       
    98         aStorageManager );
       
    99     CleanupClosePushL( *self );
       
   100     self->ConstructL();
       
   101     return self;        
       
   102     }
       
   103 
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Destructor
       
   107 // ---------------------------------------------------------------------------
       
   108 //   
       
   109 CNcdProvider::~CNcdProvider()
       
   110     {
       
   111     DLTRACEIN((""));
       
   112     iCloseObserver.ProviderClosed( *this );    
       
   113     
       
   114     if ( iServerReportManager )
       
   115         {
       
   116         iServerReportManager->Close();        
       
   117         }
       
   118         
       
   119     if ( iNodeManager ) 
       
   120         {
       
   121         iNodeManager->Close();
       
   122         }    
       
   123     
       
   124     if ( iPurchaseHistory )
       
   125         {
       
   126         iPurchaseHistory->Close();
       
   127         }
       
   128     
       
   129     if ( iOperationManager ) 
       
   130         {        
       
   131         iOperationManager->Close();
       
   132         }
       
   133 
       
   134     if ( iSubscriptionManager ) 
       
   135         {        
       
   136         iSubscriptionManager->Close();
       
   137         }
       
   138         
       
   139     if ( iFavoriteManager ) 
       
   140         {
       
   141         iFavoriteManager->Close();
       
   142         }
       
   143         
       
   144     delete iProtocolHandler;        
       
   145     delete iConfigurationManager;
       
   146     
       
   147     delete iHttpUtils;
       
   148     delete iAccessPointManager;    	
       
   149 
       
   150 	TRAP_IGNORE( HandleShutdownFileL() );
       
   151 	delete iGeneralManager;
       
   152 	
       
   153 	// Close provider's storage resources
       
   154 	iStorageManager.CloseClient( FamilyName() );
       
   155     DLTRACEOUT((""));
       
   156     }
       
   157         
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // ReceiveMessage
       
   161 // ---------------------------------------------------------------------------
       
   162 //       
       
   163 void CNcdProvider::ReceiveMessage( MCatalogsBaseMessage* aMessage,
       
   164                                    TInt aFunctionNumber )
       
   165     {
       
   166     DLTRACEIN(("Function: %d", aFunctionNumber ));
       
   167     DASSERT( aMessage );
       
   168             
       
   169     TRAPD( trapError, ReceiveMessageL( *aMessage, aFunctionNumber) );    
       
   170     
       
   171     if ( trapError != KErrNone )
       
   172         {
       
   173         // Because something went wrong the complete has not been
       
   174         // yet called for the message.
       
   175         // So, inform the client about the error.
       
   176         aMessage->CompleteAndRelease( trapError );
       
   177         }
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // ReceiveMessageL
       
   183 // ---------------------------------------------------------------------------
       
   184 //       
       
   185 void CNcdProvider::ReceiveMessageL( 
       
   186     MCatalogsBaseMessage& aMessage,
       
   187     TInt aFunctionNumber  )
       
   188     {
       
   189     switch( aFunctionNumber )
       
   190         {
       
   191         case ENcdProviderNodeManagerHandle:
       
   192             NodeManagerHandleRequestL( aMessage );
       
   193             break;
       
   194             
       
   195         case ENcdProviderPurchaseHistoryHandle:
       
   196             PurchaseHistoryHandleRequestL( aMessage );
       
   197             break;
       
   198             
       
   199         case ENcdProviderOperationManagerHandle:
       
   200             OperationManagerHandleRequestL( aMessage );
       
   201             break;
       
   202 
       
   203         case ENcdProviderSubscriptionManagerHandle:
       
   204             SubscriptionManagerHandleRequestL( aMessage );
       
   205             break;
       
   206             
       
   207         case ENcdProviderFavoriteManagerHandle:
       
   208             FavoriteManagerHandleRequestL( aMessage );
       
   209             break;
       
   210 
       
   211         case ENcdProviderServerReportManagerHandle:
       
   212             ServerReportManagerHandleRequestL( aMessage );
       
   213             break;
       
   214 
       
   215         case ENcdProviderRelease:
       
   216             ReleaseRequestL( aMessage );
       
   217             break;
       
   218             
       
   219         case ENcdProviderAddConfiguration:
       
   220             AddConfigurationRequestL( aMessage );
       
   221             break;
       
   222             
       
   223         case ENcdProviderRemoveConfiguration:
       
   224             RemoveConfigurationRequestL( aMessage );
       
   225             break;
       
   226 
       
   227         case ENcdProviderRetrieveConfigurations:
       
   228             RetrieveConfigurationsRequestL( aMessage );
       
   229             break;
       
   230             
       
   231         case ENcdProviderSetDefaultAccessPoint:
       
   232             SetDefaultAccessPointRequestL( aMessage );
       
   233             break;
       
   234                        
       
   235         case ENcdProviderClientId:
       
   236             CreateClientIdRequestL( aMessage );
       
   237             break;
       
   238 
       
   239         case ENcdProviderClearCache:
       
   240             ClearCacheRequestL( aMessage );
       
   241             break;
       
   242         
       
   243         case ENcdGetProviderInfo:
       
   244             GetInfoRequestL( aMessage );
       
   245             break;
       
   246            
       
   247         case ENcdProviderIsSimChanged:
       
   248             IsSimChangedRequestL( aMessage );
       
   249             break;
       
   250             
       
   251         case ENcdProviderIsFixedAp:
       
   252             IsFixedApRequestL( aMessage );
       
   253             break;
       
   254             
       
   255         case ENcdProviderSyncSeenInfo:
       
   256             SyncSeenInfoRequestL( aMessage );
       
   257             break;
       
   258             
       
   259         default:
       
   260             User::Leave( KErrNotSupported );
       
   261             break;
       
   262         }
       
   263     
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // CounterPartLost
       
   268 // ---------------------------------------------------------------------------
       
   269 //       
       
   270 void CNcdProvider::CounterPartLost( const MCatalogsSession& /* aSession */ )
       
   271     {
       
   272     /**
       
   273      * @ Implement this
       
   274      */
       
   275     }
       
   276 
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // Handles connection confirmation requests
       
   281 // ---------------------------------------------------------------------------
       
   282 //           
       
   283 TCatalogsHttpConnectionConfirmationState 
       
   284     CNcdProvider::HandleConnectionConfirmationRequestL( 
       
   285         MCatalogsHttpSession& /* aSession */, 
       
   286         const TCatalogsConnectionMethod& /* aMethod */  )
       
   287     {
       
   288     DLTRACEIN((""));
       
   289     /**
       
   290      * @ Handle connection confirmation requests
       
   291      */
       
   292     return ECatalogsHttpConnectionConfirmed;
       
   293     }
       
   294 
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // Handles some errors in HTTP connections
       
   298 // ---------------------------------------------------------------------------
       
   299 //           
       
   300 void CNcdProvider::HandleConnectionErrorL( 
       
   301     MCatalogsHttpSession& aSession,
       
   302     const TCatalogsConnectionMethod& /*aMethod*/, 
       
   303     TInt aError )
       
   304     {
       
   305     DLTRACEIN(("aError: %d", aError));
       
   306     // Access point selection was cancelled so cancel all HTTP operations
       
   307     // Also connection termination by pressing the red phone button is handled
       
   308     // here
       
   309     if ( aError == KErrCancel ||
       
   310          aError == KErrConnectionTerminated )
       
   311         {
       
   312         aSession.NotifyCancelAllOperations();
       
   313         }
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // Constructor
       
   318 // ---------------------------------------------------------------------------
       
   319 //           
       
   320 CNcdProvider::CNcdProvider( 
       
   321     const TUid& aFamilyId,
       
   322     MNcdProviderCloseObserver& aCloseObserver,
       
   323     const TDesC& aEngineRoot,
       
   324     CCatalogsTransport& aTransport,
       
   325     CNcdStorageManager& aStorageManager ) : 
       
   326         CCatalogsCommunicable(), 
       
   327         iFamilyId( aFamilyId ),
       
   328         iFamilyName( aFamilyId.Name() ),
       
   329         iCloseObserver( aCloseObserver ),
       
   330         iEngineRoot( aEngineRoot ),
       
   331         iStorageManager( aStorageManager ),
       
   332         iTransport( aTransport )
       
   333         
       
   334     {
       
   335     }
       
   336 
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // ConstructL
       
   340 // ---------------------------------------------------------------------------
       
   341 //       
       
   342 void CNcdProvider::ConstructL()
       
   343     {
       
   344     DLTRACEIN((""));
       
   345     RBuf familyPath;
       
   346     FamilyPathLC( familyPath );
       
   347     
       
   348     BaflUtils::EnsurePathExistsL( CNcdProviderUtils::FileSession(), familyPath );
       
   349     // Begin startup procedure. If the previous startup failed, all of 
       
   350     // provider's data files are deleted    
       
   351     // StartupEndL is called in PrepareSessionL
       
   352     TBool startupFailed = StartupBeginL( familyPath );    
       
   353     CleanupStack::PopAndDestroy( &familyPath );
       
   354     
       
   355     RBuf path;
       
   356     PurchaseHistoryPathLC( path );
       
   357     iPurchaseHistory = CNcdPurchaseHistoryDb::NewL( path );
       
   358     CleanupStack::PopAndDestroy( &path );
       
   359                 
       
   360     DLTRACE(("iPurchaseHistory ok"));
       
   361         
       
   362     iGeneralManager = new( ELeave ) CNcdGeneralManager( 
       
   363         iFamilyId, 
       
   364         FamilyName() );
       
   365     
       
   366     iGeneralManager->SetStorageManager( iStorageManager );
       
   367     iGeneralManager->SetPurchaseHistory( *iPurchaseHistory );
       
   368         
       
   369     // Create configuration manager for the provider    
       
   370     iConfigurationManager = 
       
   371         CNcdConfigurationManager::NewL( *iGeneralManager );
       
   372     
       
   373     iGeneralManager->SetConfigurationManager( *iConfigurationManager );
       
   374     
       
   375     DLTRACE(("iConfigurationManager ok"));
       
   376 
       
   377     iNodeManager = CNcdNodeManager::NewL( *iGeneralManager );
       
   378     iGeneralManager->SetNodeManager( *iNodeManager );
       
   379     DLTRACE(("iNodeManager ok"));
       
   380 
       
   381 
       
   382     iAccessPointManager = 
       
   383         CCatalogsAccessPointManager::NewL( *iGeneralManager );
       
   384     
       
   385     iGeneralManager->SetAccessPointManager( *iAccessPointManager );
       
   386     DLTRACE(("iAccessPointManager ok"));    
       
   387 
       
   388 
       
   389     iSubscriptionManager = CNcdSubscriptionManager::NewL( 
       
   390         iStorageManager, *iNodeManager );
       
   391 
       
   392     iProtocolHandler = CNcdProtocol::NewL(
       
   393         *iConfigurationManager, *iSubscriptionManager );
       
   394 
       
   395     iGeneralManager->SetProtocolManager( *iProtocolHandler );
       
   396     
       
   397     DLTRACE(("iProtocolHandler ok"));
       
   398 
       
   399     iHttpUtils = new( ELeave ) CNcdHttpUtils( *iAccessPointManager );
       
   400     iGeneralManager->SetHttpUtils( *iHttpUtils );    
       
   401 
       
   402     iOperationManager = CNcdOperationManager::NewL( 
       
   403         *this, 
       
   404         *iGeneralManager,        
       
   405         *iSubscriptionManager );
       
   406 
       
   407     DLTRACE(("iOperationManager ok"));
       
   408 
       
   409     iSubscriptionManager->SetOperationManager( iOperationManager );
       
   410     DLTRACE(("iSubscriptionManager ok"));
       
   411     
       
   412     iFavoriteManager = CNcdFavoriteManager::NewL( *iGeneralManager );
       
   413     DLTRACE(("iFavoriteManager ok"));
       
   414     
       
   415     iNodeManager->SetFavoriteManager( *iFavoriteManager );
       
   416 
       
   417     iServerReportManager = CNcdServerReportManager::NewL( *this );
       
   418     DLTRACE(("iServerReportManager ok"));
       
   419 
       
   420     // If fixed AP must be used, set it to AP manager.    
       
   421     MNcdEngineConfiguration& engineConfig = CNcdProviderUtils::EngineConfig();
       
   422     if ( engineConfig.UseFixedAp() ) 
       
   423         {
       
   424         const RPointerArray<CNcdKeyValuePair>& apDetails = 
       
   425             engineConfig.FixedApDetails();
       
   426         TRAPD( err, iAccessPointManager->SetFixedApL( apDetails ) );
       
   427         if ( err != KErrNone ) 
       
   428             {
       
   429             User::Leave( KNcdErrorApCreationFailed );
       
   430             }
       
   431         }
       
   432         
       
   433     DLTRACEOUT((""));
       
   434     }
       
   435 
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // NodeManagerHandleRequestL
       
   439 // ---------------------------------------------------------------------------
       
   440 //       
       
   441 void CNcdProvider::NodeManagerHandleRequestL( 
       
   442     MCatalogsBaseMessage& aMessage ) const
       
   443     {
       
   444     // Get the session that will contain the handle
       
   445     MCatalogsSession& requestSession( aMessage.Session() );
       
   446 
       
   447     // Add the manager to the session and get the handle.
       
   448     TInt handle( requestSession.AddObjectL( iNodeManager ) );
       
   449 
       
   450     // Sent the information to the client side            
       
   451     aMessage.CompleteAndReleaseL( handle, KErrNone );    
       
   452     }
       
   453 
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // PurchaseHistoryHandleRequestL
       
   457 // ---------------------------------------------------------------------------
       
   458 //       
       
   459 void CNcdProvider::PurchaseHistoryHandleRequestL(
       
   460     MCatalogsBaseMessage& aMessage ) const
       
   461     {
       
   462     // Get the session that will contain the handle
       
   463     MCatalogsSession& requestSession( aMessage.Session() );
       
   464 
       
   465     // Add the purchase history to the session and get the handle.
       
   466     TInt handle( requestSession.AddObjectL( iPurchaseHistory ) );
       
   467 
       
   468     // Sent the information to the client side            
       
   469     aMessage.CompleteAndReleaseL( handle, KErrNone );        
       
   470     }
       
   471 
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // OperationManagerHandleRequestL
       
   475 // ---------------------------------------------------------------------------
       
   476 //       
       
   477 void CNcdProvider::OperationManagerHandleRequestL( 
       
   478     MCatalogsBaseMessage& aMessage ) const
       
   479     {
       
   480     // Get the session that will contain the handle
       
   481     MCatalogsSession& requestSession( aMessage.Session() );
       
   482 
       
   483     // Add the manager to the session and get the handle.
       
   484     TInt handle( requestSession.AddObjectL( iOperationManager ) );
       
   485 
       
   486     // Sent the information to the client side            
       
   487     aMessage.CompleteAndReleaseL( handle, KErrNone );        
       
   488     }
       
   489 
       
   490 // ---------------------------------------------------------------------------
       
   491 // SubscriptionManagerHandleRequestL
       
   492 // ---------------------------------------------------------------------------
       
   493 //       
       
   494 void CNcdProvider::SubscriptionManagerHandleRequestL( 
       
   495     MCatalogsBaseMessage& aMessage ) const
       
   496     {
       
   497     // Get the session that will contain the handle
       
   498     MCatalogsSession& requestSession( aMessage.Session() );
       
   499 
       
   500     // Add the manager to the session and get the handle.
       
   501     TInt handle( requestSession.AddObjectL( iSubscriptionManager ) );
       
   502 
       
   503     // Sent the information to the client side            
       
   504     aMessage.CompleteAndReleaseL( handle, KErrNone );        
       
   505     }
       
   506 
       
   507 // ---------------------------------------------------------------------------
       
   508 // FavoriteManagerHandleRequestL
       
   509 // ---------------------------------------------------------------------------
       
   510 //       
       
   511 void CNcdProvider::FavoriteManagerHandleRequestL( 
       
   512     MCatalogsBaseMessage& aMessage ) const
       
   513     {
       
   514     // Get the session that will contain the handle
       
   515     MCatalogsSession& requestSession( aMessage.Session() );
       
   516 
       
   517     // Add the manager to the session and get the handle.
       
   518     TInt handle( requestSession.AddObjectL( iFavoriteManager ) );
       
   519 
       
   520     // Sent the information to the client side            
       
   521     aMessage.CompleteAndReleaseL( handle, KErrNone );        
       
   522     }
       
   523 
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // ServerReportHandleRequestL
       
   527 // ---------------------------------------------------------------------------
       
   528 //       
       
   529 void CNcdProvider::ServerReportManagerHandleRequestL(
       
   530         MCatalogsBaseMessage& aMessage ) const
       
   531     {
       
   532     // Get the session that will contain the handle
       
   533     MCatalogsSession& requestSession( aMessage.Session() );
       
   534 
       
   535     // Add the manager to the session and get the handle.
       
   536     TInt handle( requestSession.AddObjectL( iServerReportManager ) );
       
   537 
       
   538     // Sent the information to the client side            
       
   539     aMessage.CompleteAndReleaseL( handle, KErrNone );        
       
   540     }
       
   541 
       
   542 
       
   543 // ---------------------------------------------------------------------------
       
   544 // ReleaseRequestL
       
   545 // ---------------------------------------------------------------------------
       
   546 //       
       
   547 void CNcdProvider::ReleaseRequestL( MCatalogsBaseMessage& aMessage ) const
       
   548     {
       
   549     DLTRACEIN((""));
       
   550     // Decrease the reference count for this object.
       
   551     // When the reference count reaches zero, this object will be destroyed
       
   552     // and removed from the session.    
       
   553     MCatalogsSession& requestSession( aMessage.Session() );
       
   554     TInt handle( aMessage.Handle() );
       
   555     aMessage.CompleteAndRelease( KErrNone );
       
   556     requestSession.RemoveObject( handle );    
       
   557     }
       
   558 
       
   559 
       
   560 // ---------------------------------------------------------------------------
       
   561 // AddConfigurationRequestL
       
   562 // ---------------------------------------------------------------------------
       
   563 //       
       
   564 void CNcdProvider::AddConfigurationRequestL( MCatalogsBaseMessage& aMessage )
       
   565     {
       
   566     DLTRACEIN((""));
       
   567     
       
   568     // Read input from the message
       
   569     RBuf8 data;
       
   570     data.CreateL( aMessage.InputLength() );
       
   571     CleanupClosePushL( data );
       
   572     
       
   573     User::LeaveIfError( aMessage.ReadInput( data ) );
       
   574     DLTRACE(("Msg len: %d", aMessage.InputLength() ));
       
   575     
       
   576     // open stream to the data
       
   577     RDesReadStream stream( data );
       
   578     CleanupClosePushL( stream );
       
   579     
       
   580     // internalize key-value pair from the stream
       
   581     CNcdKeyValuePair* pair = CNcdKeyValuePair::NewL( stream );
       
   582     
       
   583     CleanupStack::PopAndDestroy( &stream );    
       
   584     CleanupStack::PopAndDestroy( &data );
       
   585     
       
   586     DLINFO((_L("Key: %S, value: %S"), &pair->Key(), &pair->Value()));
       
   587 
       
   588     // Node manager has to be informed about the db size restrictions.
       
   589     // So, proper db cleaning methods may be started when required.
       
   590     if ( pair->Key() == NcdConfigurationKeys::KMaxStorageSize )
       
   591         {
       
   592         // The configuration value gives the max db size for the client.
       
   593         TInt maxDbSize( 0 );
       
   594         TLex lexPair( pair->Value() );
       
   595         
       
   596         DLINFO((_L("Convert max storage size string: %S"), &pair->Value()));
       
   597         User::LeaveIfError( lexPair.Val( maxDbSize ) );
       
   598         
       
   599         DLINFO(("Max storage size string converted correctly: %d", maxDbSize));
       
   600         // The value could be parsed correctly.
       
   601         // Set max db size value.
       
   602         // So, inform node manger about this.
       
   603         iNodeManager->DbSetMaxSizeL( aMessage.Session().Context().FamilyId(), 
       
   604                                     maxDbSize );            
       
   605         }    
       
   606         
       
   607     // AddConfigurationL deletes pair if a leave occurs
       
   608     iConfigurationManager->AddConfigurationL( aMessage.Session().Context(),
       
   609                                               pair );
       
   610 
       
   611     // Complete the message
       
   612     aMessage.CompleteAndRelease( KErrNone );
       
   613     DLTRACEOUT((""));
       
   614     }
       
   615 
       
   616 
       
   617 // ---------------------------------------------------------------------------
       
   618 // RemoveConfigurationsRequestL
       
   619 // ---------------------------------------------------------------------------
       
   620 //           
       
   621 void CNcdProvider::RemoveConfigurationRequestL( 
       
   622     MCatalogsBaseMessage& aMessage )
       
   623     {
       
   624     DLTRACEIN((""));
       
   625     // Read input from the message
       
   626     RBuf8 data;
       
   627     data.CreateL( aMessage.InputLength() );
       
   628     CleanupClosePushL( data );
       
   629     
       
   630     User::LeaveIfError( aMessage.ReadInput( data ) );
       
   631 
       
   632     RDesReadStream stream( data );
       
   633     CleanupClosePushL( stream );
       
   634     HBufC* key = NULL;
       
   635     
       
   636     // Read key from the data
       
   637     if ( InternalizeDesL( key, stream ) == 0 ) 
       
   638         {
       
   639         delete key;
       
   640         User::Leave( KErrArgument );
       
   641         }
       
   642         
       
   643     CleanupStack::PopAndDestroy( 2 ); // stream, data    
       
   644     CleanupStack::PushL( key );
       
   645     
       
   646     // Remove configuration
       
   647     TInt err = iConfigurationManager->RemoveConfigurationL( 
       
   648         aMessage.Session().Context(), *key );
       
   649     CleanupStack::PopAndDestroy( key );
       
   650     
       
   651     if ( err > KErrNone ) 
       
   652         {
       
   653         err = KErrNone;
       
   654         }
       
   655     
       
   656     TBuf8<1> errBuf;
       
   657     errBuf.SetLength( 1 );
       
   658     
       
   659     errBuf[0] = 0;
       
   660 
       
   661     aMessage.CompleteAndReleaseL( errBuf, err );
       
   662     DLTRACEOUT(("err: %d", err));
       
   663     }
       
   664     
       
   665     
       
   666 // ---------------------------------------------------------------------------
       
   667 // RetrieveConfigurationsRequestL
       
   668 // ---------------------------------------------------------------------------
       
   669 //           
       
   670 void CNcdProvider::RetrieveConfigurationsRequestL( 
       
   671     MCatalogsBaseMessage& aMessage )
       
   672     {
       
   673     DLTRACEIN((""));
       
   674     
       
   675     // Get the configuration
       
   676     CNcdKeyValueMap* configuration = iConfigurationManager->ConfigurationsLC( 
       
   677         aMessage.Session().Context() );    
       
   678     
       
   679     if ( !configuration->Pairs().Count() ) 
       
   680         {
       
   681         DLERROR(("No configurations, leaving"));
       
   682         User::Leave( KErrNotFound );
       
   683         }
       
   684         
       
   685     // Externalize the configuration
       
   686     RBuf8 data;
       
   687     CleanupClosePushL( data );
       
   688     configuration->ExternalizeL( data );
       
   689     
       
   690     // Complete the message
       
   691     aMessage.CompleteAndReleaseL( data, KErrNone );
       
   692     
       
   693     CleanupStack::PopAndDestroy( 2, configuration ); // data
       
   694     DLTRACEOUT((""));
       
   695     }
       
   696 
       
   697 
       
   698 // ---------------------------------------------------------------------------
       
   699 // ---------------------------------------------------------------------------
       
   700 // 
       
   701 void CNcdProvider::GetProviderContextL( 
       
   702     MCatalogsContext& aContext, 
       
   703     TNcdProviderContext& aProviderContext )
       
   704     {
       
   705     DLTRACEIN((""));
       
   706     RBuf8& providerContextBuf = aContext.ProviderDataL( iProviderIndex );
       
   707     
       
   708     // If the provider creation has failed it is not guaranteed that provider 
       
   709     // context has been created
       
   710     if ( providerContextBuf.Size() != sizeof( TNcdProviderContext ) ) 
       
   711         {
       
   712         DLERROR(("Size of the provider context is wrong, leaving with KErrCorrupt. Size is %d, should be %d",
       
   713             providerContextBuf.Size(), sizeof( TNcdProviderContext )));
       
   714         User::Leave( KErrCorrupt );
       
   715         }
       
   716         
       
   717     // Read the provider specific context data.
       
   718     TPtr8 des( (TUint8*)&aProviderContext, 
       
   719         sizeof( TNcdProviderContext ), 
       
   720         sizeof( TNcdProviderContext ) );
       
   721     des = providerContextBuf;
       
   722     }
       
   723 
       
   724 
       
   725 // ---------------------------------------------------------------------------
       
   726 // ---------------------------------------------------------------------------
       
   727 //           
       
   728 CNcdNodeManager& CNcdProvider::NodeManager()
       
   729     {
       
   730     return *iNodeManager;
       
   731     }
       
   732 
       
   733 
       
   734 // ---------------------------------------------------------------------------
       
   735 // ---------------------------------------------------------------------------
       
   736 //           
       
   737 MCatalogsAccessPointManager& CNcdProvider::AccessPointManager()
       
   738     {
       
   739     return *iAccessPointManager;    
       
   740     }
       
   741 
       
   742 
       
   743 // ---------------------------------------------------------------------------
       
   744 // ---------------------------------------------------------------------------
       
   745 //           
       
   746 const TUid& CNcdProvider::FamilyId() const
       
   747     {
       
   748     return iFamilyId;
       
   749     }
       
   750 
       
   751 
       
   752 // ---------------------------------------------------------------------------
       
   753 // ---------------------------------------------------------------------------
       
   754 //           
       
   755 const TDesC& CNcdProvider::FamilyName() const
       
   756     {
       
   757     return iFamilyName;
       
   758     }
       
   759 
       
   760 
       
   761 // ---------------------------------------------------------------------------
       
   762 // ---------------------------------------------------------------------------
       
   763 //           
       
   764 TInt CNcdProvider::DatabaseClearingStatus() const
       
   765     {
       
   766     return iDatabaseClearingStatus;
       
   767     }
       
   768 
       
   769 // ---------------------------------------------------------------------------
       
   770 // ---------------------------------------------------------------------------
       
   771 //           
       
   772 void CNcdProvider::PrepareSessionL( 
       
   773     MCatalogsSession& aSession, 
       
   774     TUint32 aOptions )
       
   775     {
       
   776     DLTRACEIN(("aOptions: %u", aOptions ));
       
   777     // Prepare provider specific context structure.
       
   778     TNcdProviderContext context;
       
   779     
       
   780     // Create own HTTP session for the client. Use secure id -> different sessions for clients
       
   781     // in the same family.
       
   782     // DatabaseClearingStatus() is used to determine wheter download manager
       
   783     // should be cleared or not
       
   784     context.iHttpSession = static_cast<MCatalogsHttpSession*>( 
       
   785         iTransport.QueryInterfaceL( aSession.Context().SecureId().iId, 
       
   786                                     KCatalogsTransportHttpInterface,
       
   787                                     DatabaseClearingStatus() != 0 ) );    
       
   788 
       
   789     context.iSmsSession = static_cast<MCatalogsSmsSession*>(     
       
   790         iTransport.QueryInterfaceL( aSession.Context().SecureId().iId,
       
   791                                     KCatalogsTransportSmsInterface ) );
       
   792 
       
   793     context.iHttpSession->ConnectionManager().SetConnectionConfirmationObserver( this );
       
   794     context.iHttpSession->ConnectionManager().SetConnectionErrorObserver( this );
       
   795 
       
   796     context.iReportManager = 
       
   797         CNcdReportManager::NewL( 
       
   798             aSession.Context(),
       
   799             *iGeneralManager,            
       
   800             *context.iHttpSession,
       
   801             iShutdownFailed );
       
   802 
       
   803     // Save the session interface pointer in provider's context slot.
       
   804     TPtrC8 contextDes( (const TUint8*)&context, sizeof( TNcdProviderContext ) );
       
   805     aSession.Context().ProviderDataL( iProviderIndex ).CreateL( contextDes );
       
   806     
       
   807     HandleProviderOptionsL( 
       
   808         aSession.Context(), 
       
   809         aOptions, 
       
   810         *context.iHttpSession );
       
   811         
       
   812     // Startup sequence ends.
       
   813     StartupEndL();
       
   814     }
       
   815 
       
   816 // ---------------------------------------------------------------------------
       
   817 // ---------------------------------------------------------------------------
       
   818 //           
       
   819 void CNcdProvider::HandleSessionRemoval( MCatalogsSession& aSession )
       
   820     {
       
   821     DLTRACEIN((""));
       
   822     TRAP_IGNORE( 
       
   823         {
       
   824         TNcdProviderContext context;        
       
   825         GetProviderContextL( aSession.Context(), context );
       
   826 
       
   827         // Report manager must be deleted before HTTP session is released
       
   828         DLTRACE(("Deleting report manager"));
       
   829         delete context.iReportManager;
       
   830 
       
   831         // Release the HTTP and SMS sessions and clear the context data.
       
   832         ReleasePtr( context.iHttpSession );
       
   833         ReleasePtr( context.iSmsSession );
       
   834         
       
   835         aSession.Context().ProviderDataL( iProviderIndex ).Close();        
       
   836         }); // TRAP_IGNORE
       
   837     }
       
   838 
       
   839 // ---------------------------------------------------------------------------
       
   840 // SetDefaultAccessPointRequestL
       
   841 // ---------------------------------------------------------------------------
       
   842 //       
       
   843 void CNcdProvider::SetDefaultAccessPointRequestL( MCatalogsBaseMessage& aMessage )
       
   844     {
       
   845     DLTRACEIN((""));
       
   846     
       
   847     // Read input from the message
       
   848     DASSERT( aMessage.InputLength() == sizeof( TNcdConnectionMethod ) );
       
   849     
       
   850     RBuf8 data;
       
   851     data.CreateL( aMessage.InputLength() );
       
   852     CleanupClosePushL( data );
       
   853     
       
   854     User::LeaveIfError( aMessage.ReadInput( data ) );
       
   855     DLINFO(("Msg len: %d", aMessage.InputLength() ));
       
   856     
       
   857     // Read the TUint32 inside the input data.
       
   858     const TNcdConnectionMethod* accessPointId = 
       
   859         reinterpret_cast< const TNcdConnectionMethod* >( data.Ptr() );
       
   860     DLINFO(( "Access point type: %d, id %u", 
       
   861         accessPointId->iType, 
       
   862         accessPointId->iId ));
       
   863 
       
   864     // We have the HTTP session interface pointer in context data.
       
   865     TNcdProviderContext context;
       
   866     GetProviderContextL( aMessage.Session().Context(), context );
       
   867     DASSERT( context.iHttpSession != NULL );
       
   868 
       
   869     TCatalogsConnectionMethod method;
       
   870     
       
   871     iHttpUtils->ConvertConnectionMethod(
       
   872         *accessPointId,
       
   873         method );
       
   874              
       
   875     // Set the default access points to the HTTP session.
       
   876     context.iHttpSession->SetDefaultConnectionMethod( method );    
       
   877 
       
   878     CleanupStack::PopAndDestroy();  // data
       
   879 
       
   880     // Complete the message
       
   881     aMessage.CompleteAndRelease( KErrNone );
       
   882     DLTRACEOUT((""));
       
   883     }
       
   884 
       
   885 
       
   886 // ---------------------------------------------------------------------------
       
   887 // CreateClientIdRequestL
       
   888 // ---------------------------------------------------------------------------
       
   889 //       
       
   890 void CNcdProvider::CreateClientIdRequestL( MCatalogsBaseMessage& aMessage )
       
   891     {
       
   892     DLTRACEIN((""));
       
   893     
       
   894     const TDesC& clientId = iConfigurationManager->ClientIdL(
       
   895         aMessage.Session().Context() );
       
   896     aMessage.CompleteAndReleaseL( clientId, KErrNone );
       
   897     }
       
   898 
       
   899 
       
   900 // ---------------------------------------------------------------------------
       
   901 // ClearCacheRequestL
       
   902 // ---------------------------------------------------------------------------
       
   903 //       
       
   904 void CNcdProvider::ClearCacheRequestL(
       
   905     MCatalogsBaseMessage& aMessage )
       
   906     {
       
   907     DLTRACEIN((""));
       
   908     MCatalogsContext& context( aMessage.Session().Context() );
       
   909     
       
   910     CNcdReportManager& reportManager = 
       
   911         iOperationManager->ReportManagerL( context );
       
   912     
       
   913     reportManager.CloseStorage();
       
   914     
       
   915     // Clears cache and deletes incomplete downloads 
       
   916     iNodeManager->ClearClientCacheL( context, ETrue );
       
   917   
       
   918     CNcdProviderUtils::EngineConfig().ClearClientDataL( FamilyName(), EFalse );
       
   919     
       
   920     DLTRACE(( "Cache cleared" ));
       
   921     iConfigurationManager->ClearServerCapabilitiesL( context );
       
   922     
       
   923     DLTRACE(( "Server capabilities cleared" ));
       
   924     
       
   925     // Because capabilities are removed, we need to also remove server sessions
       
   926     // so that we get the capabilities again on next response.
       
   927     TRAPD( err,
       
   928         {        
       
   929         iProtocolHandler->SessionHandlerL( aMessage.Session().Context() )
       
   930             .RemoveAllSessions();
       
   931         });
       
   932     LeaveIfNotErrorL( err, KErrNotFound );
       
   933     
       
   934     DLTRACE(( "Server sessions removed" ));
       
   935     
       
   936     aMessage.CompleteAndReleaseL( KNullDesC8(), KErrNone );
       
   937     DLTRACEOUT(( "Message completed" ));    
       
   938     }
       
   939 
       
   940 
       
   941 // ---------------------------------------------------------------------------
       
   942 // AllowCacheCleaningRequestL
       
   943 // ---------------------------------------------------------------------------
       
   944 //       
       
   945 void CNcdProvider::AllowCacheCleaningL( 
       
   946     const MCatalogsContext& aContext,
       
   947     TBool aAllow )
       
   948     {
       
   949     DLTRACEIN(("aAllow: %d", aAllow));
       
   950     NodeManager().
       
   951         NodeCacheCleanerManager().
       
   952             CacheCleanerL( aContext.FamilyId() ).
       
   953                 SetAllowCleaning( aAllow );
       
   954 
       
   955     }
       
   956 
       
   957 
       
   958 // ---------------------------------------------------------------------------
       
   959 // GetInfoRequestL
       
   960 // ---------------------------------------------------------------------------
       
   961 //       
       
   962 void CNcdProvider::GetInfoRequestL(
       
   963     MCatalogsBaseMessage& aMessage )
       
   964     {
       
   965     DLTRACEIN((""));
       
   966     DASSERT( aMessage.InputLength() == 1 );
       
   967     TBuf<1> data;
       
   968     
       
   969     aMessage.ReadInput( data );
       
   970     TNcdProviderInfo info( static_cast<TNcdProviderInfo>( data[0] ) );
       
   971     
       
   972     const MNcdEngineConfiguration& config( 
       
   973         CNcdProviderUtils::EngineConfig() );
       
   974     
       
   975     HBufC* response = NULL;
       
   976     switch( info )
       
   977         {
       
   978         case ENcdProviderInfoType: 
       
   979             {
       
   980             DLTRACE(("Engine type"));
       
   981             response = config.EngineType().AllocLC();
       
   982             break;
       
   983             }
       
   984             
       
   985         case ENcdProviderInfoVersion:
       
   986             {
       
   987             DLTRACE(("Engine version"));
       
   988             response = config.EngineVersion().AllocLC();
       
   989             break;
       
   990             }
       
   991         
       
   992         case ENcdProviderInfoUid:
       
   993             {
       
   994             DLTRACE(("Engine uid"));
       
   995             response = config.EngineUid().AllocLC();
       
   996             break;
       
   997             }
       
   998         
       
   999         case ENcdProviderInfoProvisioning:
       
  1000             {
       
  1001             DLTRACE(("Engine provisioning"));
       
  1002             response = config.EngineProvisioning().AllocLC();
       
  1003             break;
       
  1004             }
       
  1005 
       
  1006         default:
       
  1007             {
       
  1008             DASSERT( 0 );
       
  1009             }
       
  1010 
       
  1011         }
       
  1012     if (response)
       
  1013         {
       
  1014              aMessage.CompleteAndReleaseL( *response, KErrNone );
       
  1015              CleanupStack::PopAndDestroy( response );
       
  1016         }
       
  1017     
       
  1018     DLTRACEOUT(("Response sent"));
       
  1019     }
       
  1020     
       
  1021 
       
  1022 void CNcdProvider::IsSimChangedRequestL( MCatalogsBaseMessage& aMessage ) 
       
  1023     {
       
  1024     DLTRACEIN((""));    
       
  1025     aMessage.CompleteAndReleaseL( iSimChanged, KErrNone );
       
  1026     DLTRACEOUT(("Response sent"));
       
  1027     } 
       
  1028     
       
  1029     
       
  1030 void CNcdProvider::IsFixedApRequestL( MCatalogsBaseMessage& aMessage )
       
  1031     {
       
  1032     DLTRACEIN((""));
       
  1033     MNcdEngineConfiguration& engineConfig = CNcdProviderUtils::EngineConfig();
       
  1034     aMessage.CompleteAndReleaseL( engineConfig.UseFixedAp(), KErrNone );
       
  1035     }
       
  1036     
       
  1037 
       
  1038 void CNcdProvider::SyncSeenInfoRequestL( MCatalogsBaseMessage& aMessage )
       
  1039     {
       
  1040     DLTRACEIN((""));
       
  1041     DASSERT( iNodeManager );
       
  1042     
       
  1043     // Read the syncronization depth
       
  1044     RCatalogsMessageReader reader;
       
  1045     reader.OpenLC( aMessage );
       
  1046     TInt depth = reader().ReadInt32L();
       
  1047     CleanupStack::PopAndDestroy( &reader );
       
  1048     
       
  1049     // Sync seen info.
       
  1050     // Remove syncseen if not needed
       
  1051     /*iNodeManager->SeenInfo().SyncSeenInfoL(
       
  1052         aMessage.Session().Context().FamilyId(), depth, *iNodeManager );*/
       
  1053         
       
  1054     aMessage.CompleteAndRelease( KErrNone );
       
  1055     }
       
  1056 
       
  1057 
       
  1058 // ---------------------------------------------------------------------------
       
  1059 // 
       
  1060 // ---------------------------------------------------------------------------
       
  1061 //       
       
  1062 TBool CNcdProvider::StartupBeginL( const TDesC& aEnginePath )
       
  1063     {
       
  1064     DLTRACEIN((""));
       
  1065     iProviderStartingUp = ETrue;
       
  1066     
       
  1067     // Clears family's temp files 
       
  1068     ClearTempFilesL();
       
  1069     
       
  1070     RBuf path;
       
  1071     AppendPathsLC( 
       
  1072         path, 
       
  1073         aEnginePath, 
       
  1074         NcdProviderDefines::KNcdProviderStartupFile() );
       
  1075     
       
  1076     // Check if "ncdstartup" exists. If it does, databases are deleted.
       
  1077     // Also database versions are checked. If they don't match, db's are deleted
       
  1078     
       
  1079     TBool startupFailed = EFalse;
       
  1080     DLTRACE(("Checking if the startup file exists and db versions match"));
       
  1081     
       
  1082     TInt failedShutdowns = 0;
       
  1083     TRAPD( err, failedShutdowns = 
       
  1084         CNcdProviderUtils::UpdateShutdownFileL( aEnginePath ) );
       
  1085     
       
  1086     iShutdownFailed = failedShutdowns > 0;        
       
  1087     
       
  1088     if ( err != KErrNone || 
       
  1089          failedShutdowns > NcdProviderDefines::KNcdMaxFailedShutdowns || 
       
  1090          BaflUtils::FileExists( CNcdProviderUtils::FileSession(), path ) )
       
  1091         {
       
  1092         DLERROR(("Last startup failed. Cleaning files"));
       
  1093         iDatabaseClearingStatus = KNcdDatabasesClearedAfterCrash;
       
  1094         ClearProviderFilesL( aEnginePath );
       
  1095         
       
  1096         // Delete the shutdown file because updating it failed
       
  1097         if ( err != KErrNone ) 
       
  1098             {
       
  1099             DLTRACE(("Deleting shutdown file because it wasn't updated successfully"));
       
  1100             TRAP_IGNORE( CNcdProviderUtils::RemoveShutdownFileL( 
       
  1101                 aEnginePath ) );
       
  1102             }
       
  1103         startupFailed = ETrue;
       
  1104         }
       
  1105     else 
       
  1106         {
       
  1107         CheckDatabaseVersionsL( aEnginePath );
       
  1108         }
       
  1109 
       
  1110     // Startup file is created 
       
  1111     DLTRACE(( _L("Creating/replacing startup file %S"), &path ));
       
  1112     RFile file;
       
  1113     CleanupClosePushL( file );
       
  1114     User::LeaveIfError( file.Replace( 
       
  1115         CNcdProviderUtils::FileSession(), path, EFileWrite ) );
       
  1116     CleanupStack::PopAndDestroy( &file );
       
  1117 
       
  1118     CleanupStack::PopAndDestroy( &path );
       
  1119     DLTRACEOUT(("Startup begun"));  
       
  1120     return startupFailed;  
       
  1121     }
       
  1122 
       
  1123 
       
  1124 // ---------------------------------------------------------------------------
       
  1125 // Checks that the database version numbers match with the supported versions
       
  1126 // 
       
  1127 // ---------------------------------------------------------------------------
       
  1128 //       
       
  1129 void CNcdProvider::CheckDatabaseVersionsL(
       
  1130     const TDesC& aEnginePath )
       
  1131     {
       
  1132     DLTRACEIN(( _L("Engine path: %S"), &aEnginePath ));
       
  1133     
       
  1134     TUint32 generalVersion = 0;
       
  1135     TUint32 purchaseVersion = 0;
       
  1136     TRAPD( err, CNcdProviderUtils::ReadDatabaseVersionsL( 
       
  1137         aEnginePath, generalVersion, purchaseVersion ) );
       
  1138     
       
  1139     DLINFO(("Supported versions, general: %d, purchase history: %d",
       
  1140         KNcdGeneralDatabaseVersion, KNcdPurchaseHistoryVersion ));
       
  1141     
       
  1142     if ( err == KErrNone ) 
       
  1143         {
       
  1144         if ( generalVersion != KNcdGeneralDatabaseVersion )
       
  1145             {
       
  1146             DLERROR(("General database version mismatch. Deleting existing dbs"));
       
  1147             iDatabaseClearingStatus = KNcdGeneralDatabaseVersionMismatch;
       
  1148             ClearDatabasesL( aEnginePath );
       
  1149             }
       
  1150             
       
  1151         if ( purchaseVersion != KNcdPurchaseHistoryVersion )
       
  1152             {
       
  1153             DLERROR(("Purchase history version mismatch. Deleting purchase history"));
       
  1154             iDatabaseClearingStatus |= KNcdPurchaseHistoryVersionMismatch;
       
  1155             ClearPurchaseHistoryL();            
       
  1156             }
       
  1157         }
       
  1158     else
       
  1159         {
       
  1160         DLERROR(("Couldn't read database versions. Deleting dbs just to be sure."));
       
  1161         iDatabaseClearingStatus = 
       
  1162             KNcdPurchaseHistoryVersionMismatch |
       
  1163             KNcdGeneralDatabaseVersionMismatch;
       
  1164         ClearProviderFilesL( aEnginePath );
       
  1165         }
       
  1166     
       
  1167     DLTRACEOUT(("Database versions checked, clearing status: %d", 
       
  1168         iDatabaseClearingStatus ));        
       
  1169     }
       
  1170 
       
  1171 
       
  1172 // ---------------------------------------------------------------------------
       
  1173 // 
       
  1174 // ---------------------------------------------------------------------------
       
  1175 //       
       
  1176 void CNcdProvider::StartupEndL()
       
  1177     {
       
  1178     DLTRACEIN((""));
       
  1179     
       
  1180     if ( !iProviderStartingUp ) 
       
  1181         {
       
  1182         DLTRACEOUT(("Provider already up"));
       
  1183         return;
       
  1184         }
       
  1185     
       
  1186     RBuf path;
       
  1187     FamilyPathLC( path );
       
  1188     
       
  1189     // Ensure that database versions file contains the correct versions
       
  1190     CNcdProviderUtils::WriteDatabaseVersionsL( 
       
  1191         path, 
       
  1192         KNcdGeneralDatabaseVersion,
       
  1193         KNcdPurchaseHistoryVersion );
       
  1194     
       
  1195     path.Append( NcdProviderDefines::KNcdProviderStartupFile );
       
  1196     
       
  1197     DLTRACE(( _L("Deleting the startup file after successful startup, path: %S"), 
       
  1198         &path ));
       
  1199     CNcdProviderUtils::FileSession().Delete( path );
       
  1200     
       
  1201     CleanupStack::PopAndDestroy( &path );
       
  1202         
       
  1203     iProviderStartingUp = EFalse;
       
  1204     DLTRACEOUT(("Startup file deleted"));
       
  1205     }
       
  1206 
       
  1207 
       
  1208 // ---------------------------------------------------------------------------
       
  1209 //
       
  1210 // ---------------------------------------------------------------------------
       
  1211 //       
       
  1212 void CNcdProvider::ClearProviderFilesL( const TDesC& aEnginePath )
       
  1213     {
       
  1214     DLTRACEIN((""));
       
  1215     ClearDatabasesL( aEnginePath );
       
  1216     ClearPurchaseHistoryL();
       
  1217     // Clear downloaded files
       
  1218     CNcdProviderUtils::EngineConfig().ClearClientDataL( FamilyName(), EFalse );
       
  1219     }
       
  1220 
       
  1221 
       
  1222 // ---------------------------------------------------------------------------
       
  1223 // Clears all databases except purchase history
       
  1224 // Also doesn't delete startupfile nor shutdownfile
       
  1225 // ---------------------------------------------------------------------------
       
  1226 //       
       
  1227 void CNcdProvider::ClearDatabasesL( const TDesC& aEnginePath )
       
  1228     {
       
  1229     DLTRACEIN((""));
       
  1230     RFs& fs( CNcdProviderUtils::FileSession() );    
       
  1231         
       
  1232     CFileMan* fileman = CFileMan::NewL( fs );
       
  1233     CleanupStack::PushL( fileman );
       
  1234     
       
  1235     CDir* fileList;
       
  1236     CDir* dirList;
       
  1237     
       
  1238     User::LeaveIfError( fs.GetDir( aEnginePath,
       
  1239         KEntryAttDir, ESortNone, fileList, dirList) );
       
  1240     
       
  1241     delete fileList;
       
  1242     fileList = NULL;        
       
  1243     
       
  1244     // Minimize stack usage by creating TParse on heap
       
  1245     TParse* parse = NULL;
       
  1246     parse = new (ELeave) TParse;
       
  1247     CleanupDeletePushL( parse );
       
  1248     parse->Set( aEnginePath, NULL, NULL );
       
  1249     
       
  1250     // Delete directories
       
  1251     for ( TInt i = 0; i < dirList->Count(); i++ )
       
  1252         {        
       
  1253         DLTRACE(( _L("Deleting %S"), &(*dirList)[i].iName ));
       
  1254         parse->AddDir( (*dirList)[i].iName );
       
  1255         fileman->RmDir( parse->DriveAndPath() );
       
  1256         parse->PopDir();
       
  1257         }
       
  1258         
       
  1259     delete dirList;
       
  1260     dirList = NULL;        
       
  1261     
       
  1262     CleanupStack::PopAndDestroy( 2, fileman ); // parse, fileman
       
  1263     
       
  1264     DLTRACEOUT(("Databases successfully deleted"));    
       
  1265     }
       
  1266 
       
  1267 // ---------------------------------------------------------------------------
       
  1268 // Deletes the purchase history database
       
  1269 // ---------------------------------------------------------------------------
       
  1270 //       
       
  1271 void CNcdProvider::ClearPurchaseHistoryL()
       
  1272     {
       
  1273     DLTRACEIN((""));
       
  1274 
       
  1275     RBuf path;
       
  1276     PurchaseHistoryPathLC( path );
       
  1277     
       
  1278     DLTRACE(( _L("Deleting purchase history, %S"), &path ));
       
  1279     TInt err = CNcdProviderUtils::FileSession().Delete( path );
       
  1280     CleanupStack::PopAndDestroy( &path );
       
  1281     
       
  1282     DLTRACEOUT(( "Purchase history deleted, err = %d", err ));
       
  1283     LeaveIfNotErrorL( err, KErrNotFound, KErrPathNotFound );
       
  1284     }
       
  1285 
       
  1286 
       
  1287 // ---------------------------------------------------------------------------
       
  1288 // Clears family's temporary files
       
  1289 // ---------------------------------------------------------------------------
       
  1290 //       
       
  1291 void CNcdProvider::ClearTempFilesL()
       
  1292     {
       
  1293     DLTRACEIN((""));
       
  1294     CNcdProviderUtils::EngineConfig().ClearClientDataL( FamilyName(), ETrue );
       
  1295     }
       
  1296 
       
  1297 
       
  1298 // ---------------------------------------------------------------------------
       
  1299 // Handles the shutdown file
       
  1300 // ---------------------------------------------------------------------------
       
  1301 //       
       
  1302 void CNcdProvider::HandleShutdownFileL()
       
  1303     {
       
  1304     DLTRACEIN((""));
       
  1305     RBuf path;
       
  1306     FamilyPathLC( path );
       
  1307     CNcdProviderUtils::RemoveShutdownFileL( path );
       
  1308     CleanupStack::PopAndDestroy( &path );
       
  1309     DLTRACEOUT(("Shutdown file removed"));
       
  1310     }
       
  1311 
       
  1312 // ---------------------------------------------------------------------------
       
  1313 // Manages the storages if SIM card is changed
       
  1314 // ---------------------------------------------------------------------------
       
  1315 //
       
  1316 void CNcdProvider::ManageStoragesL( 
       
  1317     const MCatalogsContext& aContext, 
       
  1318     TBool aClearStorages ) 
       
  1319     {
       
  1320     DLTRACEIN(("aClearStorages: %d", aClearStorages ));
       
  1321     const TDesC8& previousSsid = iConfigurationManager->SsidL( aContext );
       
  1322     HBufC8* currentSsid = HashImsiLC();
       
  1323     
       
  1324     if ( previousSsid != *currentSsid ) 
       
  1325         {
       
  1326         // SIM card was changed or removed, manage the storages
       
  1327         iConfigurationManager->SetSsidL( aContext, currentSsid );
       
  1328         CleanupStack::Pop( currentSsid );
       
  1329         
       
  1330         if ( aClearStorages ) 
       
  1331             {
       
  1332             DLTRACE(("Clearing storages"));
       
  1333             // Clear node cache (NOTE! FAVORITE NODES MUST NOT BE CLEARED!!).
       
  1334             iNodeManager->ClearClientCacheL( aContext, EFalse );
       
  1335             
       
  1336             // Clear subscriptions
       
  1337             iSubscriptionManager->ClearSubscriptionDbL( aContext );
       
  1338             
       
  1339             // Clear server capabilities
       
  1340             iConfigurationManager->ClearServerCapabilitiesL( aContext );
       
  1341             }
       
  1342         
       
  1343         iSimChanged = ETrue;
       
  1344         }
       
  1345     else 
       
  1346         {
       
  1347         CleanupStack::PopAndDestroy( currentSsid );
       
  1348         }
       
  1349     }
       
  1350     
       
  1351 // ---------------------------------------------------------------------------
       
  1352 // Hash IMSI
       
  1353 // ---------------------------------------------------------------------------
       
  1354 //        
       
  1355 HBufC8* CNcdProvider::HashImsiLC()
       
  1356     {    
       
  1357     DLTRACEIN((""));
       
  1358     CSHA1* sha1 = CSHA1::NewL(); 
       
  1359     CleanupStack::PushL( sha1 );
       
  1360     
       
  1361     #ifdef __WINS__
       
  1362 
       
  1363     // emulator uses a fake ssid
       
  1364     CleanupStack::PopAndDestroy( sha1 );  // sha1
       
  1365     DLINFO(("Emulator, fake SSID used"));
       
  1366     return KEmulatorFakeSsid().AllocLC();
       
  1367 
       
  1368     #else
       
  1369 
       
  1370     MNcdDeviceService& deviceService = CNcdProviderUtils::DeviceService();
       
  1371 
       
  1372     HBufC8* temp = Des16ToDes8L( deviceService.ImsiL() );
       
  1373     CleanupStack::PushL( temp );
       
  1374 
       
  1375     // No SIM card causes null IMSI
       
  1376     if ( *temp == KNullDesC8 ) 
       
  1377         {
       
  1378         // null IMSI is KNullDesC in device 
       
  1379         CleanupStack::PopAndDestroy( 2, sha1 );  // temp, sha1
       
  1380         DLINFO(("Null IMSI!"));
       
  1381         return KNullDesC8().AllocLC();
       
  1382         }
       
  1383     
       
  1384     // Finalize hash
       
  1385     HBufC8* hash = sha1->Final( *temp ).AllocL();
       
  1386     
       
  1387     CleanupStack::PopAndDestroy( 2, sha1 );  // temp, sha1
       
  1388     CleanupStack::PushL( hash );    
       
  1389     
       
  1390     HBufC8* final = ConvertDataToTextL( *hash );
       
  1391     CleanupStack::PopAndDestroy( hash );
       
  1392     CleanupStack::PushL( final );
       
  1393     
       
  1394     DLTRACEOUT(("Hash: %S, length: %d", final, final->Length() ));
       
  1395     return final;
       
  1396 
       
  1397     #endif
       
  1398     }
       
  1399     
       
  1400 // ---------------------------------------------------------------------------
       
  1401 // Converts binary data to text
       
  1402 // ---------------------------------------------------------------------------
       
  1403 //    
       
  1404 HBufC8* CNcdProvider::ConvertDataToTextL( const TDesC8& aData ) const
       
  1405     {
       
  1406     DLTRACEIN(("length: %i", aData.Length() ));
       
  1407     _LIT8( KFormatString, "%02x" );       
       
  1408     
       
  1409     HBufC8* target = HBufC8::NewL( 2 * aData.Length() );
       
  1410     TPtr8 targetPtr( target->Des() );
       
  1411     TBuf8<2> buf;
       
  1412     
       
  1413     for ( TInt i = 0; i < aData.Length(); i++ ) 
       
  1414         {        
       
  1415         buf.Format( KFormatString, aData[i] );
       
  1416         targetPtr.Append( buf );
       
  1417         }
       
  1418      
       
  1419     DLTRACEOUT(("target: %S", target));
       
  1420     return target;
       
  1421     }
       
  1422 
       
  1423 
       
  1424 // ---------------------------------------------------------------------------
       
  1425 // 
       
  1426 // ---------------------------------------------------------------------------
       
  1427 //    
       
  1428 void CNcdProvider::PurchaseHistoryPathLC( RBuf& aPath ) const
       
  1429     {    
       
  1430     CleanupClosePushL( aPath );
       
  1431     
       
  1432     aPath.CreateL( 
       
  1433         iEngineRoot.Length() + 
       
  1434         NcdProviderDefines::KPurchaseHistoryDirectory().Length() +
       
  1435         FamilyName().Length() + 
       
  1436         NcdProviderDefines::KDatabaseExtension().Length() );
       
  1437     aPath.Append( iEngineRoot );
       
  1438     aPath.Append( NcdProviderDefines::KPurchaseHistoryDirectory() );
       
  1439     aPath.Append( FamilyName() );
       
  1440     aPath.Append( NcdProviderDefines::KDatabaseExtension );
       
  1441     }
       
  1442 
       
  1443 
       
  1444 // ---------------------------------------------------------------------------
       
  1445 // 
       
  1446 // ---------------------------------------------------------------------------
       
  1447 //    
       
  1448 void CNcdProvider::FamilyPathLC( RBuf& aPath ) const
       
  1449     {
       
  1450     CleanupClosePushL( aPath );
       
  1451     aPath.CreateL( KMaxPath );    
       
  1452     aPath.Append( iEngineRoot );
       
  1453     aPath.Append( FamilyName() );
       
  1454     aPath.Append( KDirectorySeparator );
       
  1455     }
       
  1456 
       
  1457 
       
  1458 // ---------------------------------------------------------------------------
       
  1459 // 
       
  1460 // ---------------------------------------------------------------------------
       
  1461 //    
       
  1462 void CNcdProvider::HandleProviderOptionsL( 
       
  1463     const MCatalogsContext& aContext, 
       
  1464     TUint32 aOptions,
       
  1465     MCatalogsHttpSession& aHttpSession )
       
  1466     {
       
  1467     DLTRACEIN(("Options: %d", aOptions ));
       
  1468     
       
  1469     // Manage the client's storages if SIM card is changed
       
  1470     ManageStoragesL( 
       
  1471         aContext, 
       
  1472         aOptions & ENcdProviderEnableSimChangeCacheCleaning );
       
  1473     
       
  1474     AllowCacheCleaningL( 
       
  1475         aContext,
       
  1476         // We actually enable cache cleaning so we have to invert the flag
       
  1477         !( aOptions & ENcdProviderDisableNodeCacheCleaner ) );
       
  1478 
       
  1479     TUint32 protocolOptions = 0;
       
  1480     if ( aOptions & ENcdProviderSendImei ) 
       
  1481         {
       
  1482         protocolOptions = CNcdProtocol::ESendImei;
       
  1483         }
       
  1484     iProtocolHandler->SetProtocolOptions( protocolOptions );
       
  1485     
       
  1486     TUint32 httpOptions = 0;
       
  1487     if ( aOptions & ENcdProviderDisableHttpHeadRequest ) 
       
  1488         {
       
  1489         httpOptions = ECatalogsHttpDisableHeadRequest;
       
  1490         }
       
  1491     aHttpSession.SetOptions( httpOptions );
       
  1492     }