ncdengine/provider/storage/src/ncdstoragemanagerimpl.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 
       
    21 #include "ncdstoragemanagerimpl.h"
       
    22 #include "ncdstorageimpl.h"
       
    23 #include "ncdstorageclientimpl.h"
       
    24 #include "ncdproviderdefines.h"
       
    25 
       
    26 #include "catalogsconstants.h"
       
    27 #include "catalogsdebug.h"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // ConstructL
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 void CNcdStorageManager::ConstructL( const TDesC& aRootDirectory )
       
    37     {
       
    38     DLTRACEIN( ("") );    
       
    39     iRootDir.CreateL( aRootDirectory );    
       
    40     iFileMan = CFileMan::NewL( iFs );    
       
    41     DLTRACEOUT( ("" ) );
       
    42     }
       
    43 
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // NewL
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CNcdStorageManager* CNcdStorageManager::NewL( 
       
    50     RFs& aFs,
       
    51     const TDesC& aRootDirectory )
       
    52     {
       
    53     CNcdStorageManager* self = new (ELeave ) CNcdStorageManager( aFs );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL( aRootDirectory );
       
    56     CleanupStack::Pop( self );
       
    57     return self;
       
    58     }
       
    59 
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Destructor
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CNcdStorageManager::~CNcdStorageManager()
       
    67     {
       
    68     DLTRACEIN( ( "" ) );
       
    69     
       
    70     iClients.ResetAndDestroy();    
       
    71     
       
    72     delete iFileMan;   
       
    73     iRootDir.Close();
       
    74     }
       
    75 
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // CreateOrGetStorageL
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 MNcdStorage& CNcdStorageManager::CreateOrGetStorageL( const TDesC& aClientUid, 
       
    82     const TDesC& aNamespace )
       
    83     {
       
    84     DLTRACEIN((""));
       
    85     MNcdStorage* storage = NULL;
       
    86     
       
    87     TRAPD( err,        
       
    88            storage =
       
    89            &StorageL( aClientUid, aNamespace ) );
       
    90     
       
    91     if ( err == KErrNotFound ) 
       
    92         {
       
    93         DLINFO(("Creating storage for the client"));
       
    94         storage = 
       
    95             &CreateStorageL( aClientUid, 
       
    96                              aNamespace );
       
    97         }
       
    98     else if ( err != KErrNone )
       
    99         {
       
   100         DLERROR(("Leaving: %i", err));
       
   101                 
       
   102         User::Leave( err );   
       
   103         }
       
   104             
       
   105 
       
   106     DLTRACEOUT((""));
       
   107     return *storage;    
       
   108     }
       
   109 
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CreateStorageL
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 MNcdStorage& CNcdStorageManager::CreateStorageL( const TDesC& aClientUid, 
       
   116     const TDesC& aNamespace )
       
   117     {
       
   118     DLTRACEIN( ("") );
       
   119     TInt index = FindClientByUid( aClientUid );
       
   120     
       
   121     CNcdStorageClient* client = NULL;
       
   122     
       
   123     if ( index == KErrNotFound ) 
       
   124         {
       
   125         DLTRACE( ("Create a new client") );
       
   126         // Create a new client
       
   127         client = CNcdStorageClient::NewLC( *this, aClientUid );
       
   128         iClients.AppendL( client );
       
   129         CleanupStack::Pop( client );
       
   130         }
       
   131     else 
       
   132         {
       
   133         DLTRACE( ("Use an existing client") );
       
   134         // Use existing client
       
   135         client = iClients[ index ];
       
   136         DASSERT( client );
       
   137         }
       
   138 
       
   139     // Create the storage
       
   140     MNcdStorage& storage = client->CreateStorageL( aNamespace );
       
   141     DLTRACEOUT( ("") );
       
   142     return storage;                
       
   143     }
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // Provider storage getter
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 MNcdStorage& CNcdStorageManager::ProviderStorageL( const TDesC& aClientUid )
       
   151     {
       
   152     return CreateOrGetStorageL( 
       
   153         aClientUid, 
       
   154         NcdProviderDefines::KProviderStorageNamespace );
       
   155     }
       
   156     
       
   157     
       
   158 // ---------------------------------------------------------------------------
       
   159 // Storage getter
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 MNcdStorage& CNcdStorageManager::StorageL( const TDesC& aClientUid, 
       
   163     const TDesC& aNamespace )
       
   164     {
       
   165     DLTRACEIN( ("") );
       
   166     TInt index = FindClientByUidL( aClientUid );
       
   167             
       
   168     return iClients[index]->StorageL( aNamespace );
       
   169     }
       
   170 
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // Storage client namespace list getter
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 MDesCArray* CNcdStorageManager::StorageNamespacesLC( 
       
   177     const TDesC& aClientUid ) const
       
   178     {
       
   179     TInt index = FindClientByUidL( aClientUid );
       
   180     return iClients[index]->NamespacesLC();
       
   181     }
       
   182     
       
   183 // ---------------------------------------------------------------------------
       
   184 // Storage client getter
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 MNcdStorageClient& CNcdStorageManager::StorageClientL( 
       
   188     const TDesC& aClientUid )
       
   189     {
       
   190     TInt index = FindClientByUid( aClientUid );
       
   191     if ( index == KErrNotFound ) 
       
   192         {
       
   193         // Create a new client
       
   194         CNcdStorageClient* client = CNcdStorageClient::NewLC( *this, aClientUid );
       
   195         iClients.AppendL( client );
       
   196         CleanupStack::Pop( client );
       
   197         return *client;
       
   198         }
       
   199     return *iClients[ index ];
       
   200     }
       
   201 
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // Client remover
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 /*
       
   208 void CNcdStorageManager::RemoveClientL( const TDesC& aClientUid )
       
   209     {    
       
   210     DLTRACEIN( ("") );
       
   211     TInt index = KErrNone;
       
   212     
       
   213     TRAPD( err, index = FindClientByUidL( aClientUid ) );
       
   214     if ( err == KErrNotFound ) 
       
   215         {
       
   216         // Create storage client temporarily.
       
   217         CNcdStorageClient* client = CNcdStorageClient::NewLC( *this, aClientUid );
       
   218         iClients.AppendL( client );
       
   219         CleanupStack::Pop( client );
       
   220         index = iClients.Count() - 1;
       
   221         }
       
   222     
       
   223     RBuf path;
       
   224     CleanupClosePushL( path );
       
   225     path.CreateL( KMaxPath );
       
   226     
       
   227     iClients[index]->AppendRoot( path );
       
   228         
       
   229     DLINFO( ( _L("Removing client: %S"), &path ) );
       
   230     
       
   231     delete iClients[index];
       
   232     iClients.Remove( index );
       
   233     
       
   234     // Remove client's directory
       
   235     RemoveDirectoryL( iFs, path );
       
   236     
       
   237     CleanupStack::PopAndDestroy( &path );
       
   238     DLTRACEOUT((""));
       
   239     }
       
   240 */    
       
   241     
       
   242 // ---------------------------------------------------------------------------
       
   243 // Storage remover
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CNcdStorageManager::RemoveStorageL( const TDesC& aClientUid, 
       
   247     const TDesC& aNamespace)
       
   248     {
       
   249     DLTRACEIN( ("") );
       
   250     TInt index = KErrNone;
       
   251     TRAPD( err, index = FindClientByUidL( aClientUid ) );
       
   252     if ( err == KErrNotFound ) 
       
   253         {
       
   254         // Create storage client.
       
   255         CNcdStorageClient* client = CNcdStorageClient::NewLC( *this, aClientUid );
       
   256         iClients.AppendL( client );
       
   257         CleanupStack::Pop( client );
       
   258         index = iClients.Count() - 1;
       
   259         }
       
   260     
       
   261     // Removes the actual storage directory
       
   262     TRAP( err, iClients[ index ]->RemoveStorageL( aNamespace ) );
       
   263     if ( err != KErrNotFound ) 
       
   264         {
       
   265         User::LeaveIfError( err );
       
   266         }
       
   267         
       
   268     DLTRACEOUT( ( "" ) );
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // Close
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CNcdStorageManager::CloseClient( const TDesC& aClientUid )
       
   276     {
       
   277     DLTRACEIN((""));
       
   278     TInt index = FindClientByUid( aClientUid );
       
   279     if ( index != KErrNotFound )
       
   280         {
       
   281         delete iClients[ index ];
       
   282         iClients.Remove( index );
       
   283         }
       
   284     }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // File session getter
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 RFs& CNcdStorageManager::FileSession()
       
   292     {
       
   293     DLTRACEIN((""));
       
   294 
       
   295     return iFs;
       
   296     }
       
   297     
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // Prepends root dir to the des
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CNcdStorageManager::AppendRoot( TDes& aPath ) const
       
   304     {
       
   305     DLTRACEIN((""));
       
   306 
       
   307     aPath.Append( iRootDir );
       
   308     
       
   309     DLTRACEOUT((""));
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // File manager getter
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 CFileMan& CNcdStorageManager::FileManager()
       
   317     {
       
   318     DASSERT( iFileMan );
       
   319     return *iFileMan;
       
   320     }
       
   321 
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // Utility function for removing a directory
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 void CNcdStorageManager::RemoveDirectoryL( RFs& aFileSession, 
       
   328     const TDesC& aDirectory )
       
   329     {
       
   330     CFileMan* fileman = CFileMan::NewL( aFileSession );
       
   331     CleanupStack::PushL( fileman );
       
   332     User::LeaveIfError( fileman->RmDir( aDirectory ) );    
       
   333     CleanupStack::PopAndDestroy( fileman );    
       
   334     }
       
   335 
       
   336 
       
   337 // ---------------------------------------------------------------------------
       
   338 // Constructor
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 CNcdStorageManager::CNcdStorageManager( RFs& aFs ) : iFs( aFs )
       
   342     {
       
   343     }
       
   344     
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 //  Searches for the storage client by uid
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 TInt CNcdStorageManager::FindClientByUidL( const TDesC& aUid ) const
       
   351     {
       
   352     DLTRACEIN(( _L("Uid: %S"), &aUid ));
       
   353     TInt index = FindClientByUid( aUid );    
       
   354     User::LeaveIfError( index );
       
   355     return index;
       
   356     }
       
   357     
       
   358     
       
   359 // ---------------------------------------------------------------------------
       
   360 //  Searches for the storage client by uid
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 TInt CNcdStorageManager::FindClientByUid( const TDesC& aUid ) const
       
   364     {
       
   365     DLTRACEIN(( _L("Uid: %S"), &aUid ));
       
   366     TInt count = iClients.Count();
       
   367     for ( TInt i = 0; i < count; ++i ) 
       
   368         {
       
   369         if ( iClients[ i ]->ClientUid().Compare( aUid ) == 0 ) 
       
   370             {
       
   371             DLTRACEOUT(("index: %d", i));
       
   372             return i;
       
   373             }
       
   374         }
       
   375     DLTRACEOUT(("Err: -1"));    
       
   376     return KErrNotFound;
       
   377     }
       
   378     
       
   379     
       
   380