ncdengine/provider/storage/src/ncdstorageimpl.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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 #include <bautils.h>
       
    21 
       
    22 #include "ncdstorageimpl.h"
       
    23 #include "ncddbstoragemanager.h"
       
    24 #include "ncdfilestorageimpl.h"
       
    25 #include "catalogsdebug.h"
       
    26 #include "catalogsutils.h"
       
    27 #include "catalogsconstants.h"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // ConstructL
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 void CNcdStorage::ConstructL( const TDesC& aNamespace )
       
    37     {
       
    38     DLTRACEIN( ("") );
       
    39     iNamespace.CreateL( aNamespace ); 
       
    40     iDirectory.Assign( EncodeFilenameLC( aNamespace, iOwner.FileSession() ) );
       
    41     CleanupStack::Pop(); // iDirectory
       
    42 
       
    43     HBufC* path = CurrentPathLC();
       
    44     
       
    45     DLTRACE(( _L("Creating the directory: %S"), path));
       
    46     // Create the directory
       
    47     BaflUtils::EnsurePathExistsL( iOwner.FileSession(), 
       
    48         *path );    
       
    49     
       
    50     CleanupStack::PopAndDestroy( path );
       
    51     DLTRACEOUT( ("") );   
       
    52     }
       
    53 
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // ConstructL
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 void CNcdStorage::ConstructL( HBufC* aNamespace, HBufC* aDirectory )
       
    60     {
       
    61     DLTRACEIN( ("") );
       
    62     iNamespace.Assign( aNamespace ); 
       
    63     iDirectory.Assign( aDirectory );    
       
    64 
       
    65     HBufC* path = CurrentPathLC();
       
    66     
       
    67     DLTRACE(( _L("Creating the directory: %S"), path));
       
    68     // Create the directory
       
    69     BaflUtils::EnsurePathExistsL( iOwner.FileSession(), 
       
    70         *path );    
       
    71     
       
    72     CleanupStack::PopAndDestroy( path );
       
    73     DLTRACEOUT( ("") );   
       
    74     }
       
    75 
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // NewL
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CNcdStorage* CNcdStorage::NewL( MNcdStorageOwner& aOwner, 
       
    82     const TDesC& aNamespace )
       
    83     {
       
    84     CNcdStorage* self = CNcdStorage::NewLC( aOwner, aNamespace );
       
    85     CleanupStack::Pop( self );
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // NewLC
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CNcdStorage* CNcdStorage::NewLC( MNcdStorageOwner& aOwner, 
       
    95     const TDesC& aNamespace )
       
    96     {
       
    97     CNcdStorage* self = new( ELeave ) CNcdStorage( aOwner );
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL( aNamespace );
       
   100     return self;
       
   101     }
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // NewL
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 CNcdStorage* CNcdStorage::NewL( MNcdStorageOwner& aOwner, 
       
   109     HBufC* aNamespace, HBufC* aDirectory )
       
   110     {
       
   111     CNcdStorage* self = new( ELeave ) CNcdStorage( aOwner );
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL( aNamespace, aDirectory );
       
   114     CleanupStack::Pop( self );
       
   115     return self;
       
   116     }
       
   117 
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // Destructor
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 CNcdStorage::~CNcdStorage()
       
   124     {
       
   125     DLTRACEIN((""));
       
   126     iNamespace.Close();        
       
   127     iDirectory.Close();
       
   128     iDbStorages.ResetAndDestroy();    
       
   129     iFileStorages.ResetAndDestroy();
       
   130     }
       
   131        
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // Gets an existing database storage or creates a new one
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 MNcdDatabaseStorage& CNcdStorage::DatabaseStorageL( const TDesC& aUid )
       
   138     {
       
   139     DLTRACEIN( ( _L("Uid: %S"), &aUid ) );
       
   140     TInt index = FindDbStorageByUid( aUid );
       
   141     
       
   142     CNcdDbStorageManager* db = NULL;
       
   143     if ( index == KErrNotFound ) 
       
   144         {
       
   145         RBuf path;
       
   146         CleanupClosePushL( path );
       
   147         path.CreateL( KMaxPath );
       
   148 
       
   149         
       
   150         DLTRACE( ("DB not found, creating new object" ) );
       
   151 
       
   152         // Creates the path
       
   153         CreateCurrentPathL( path, aUid );      
       
   154             
       
   155         DLTRACE(( _L("Checking if the directory exists: %S"),
       
   156             &path ));
       
   157             
       
   158         db = CNcdDbStorageManager::NewLC( FileSession(), aUid, path, 
       
   159             iDirectory );
       
   160         
       
   161         
       
   162         iDbStorages.AppendL( db );
       
   163         CleanupStack::Pop( db );
       
   164         CleanupStack::PopAndDestroy( &path );
       
   165         }
       
   166     else 
       
   167         {
       
   168         db = iDbStorages[index];
       
   169         }
       
   170         
       
   171     DASSERT( db );
       
   172     DLTRACEOUT( ( "db: %x", db ) );
       
   173     return *db;
       
   174     }
       
   175 
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Gets an existing file storage or creates a new one
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 MNcdFileStorage& CNcdStorage::FileStorageL( const TDesC& aUid )
       
   182     {
       
   183     DLTRACEIN( ( _L("Uid: %S"), &aUid ) );
       
   184     TInt index = FindFileStorageByUid( aUid );
       
   185     
       
   186     CNcdFileStorage* fs = NULL;
       
   187     if ( index == KErrNotFound ) 
       
   188         {
       
   189         RBuf path;
       
   190         CleanupClosePushL( path );
       
   191         path.CreateL( KMaxPath );
       
   192         
       
   193         CreateCurrentPathL( path, aUid );
       
   194                     
       
   195         // Note: giving the owner of this object instead of giving *this
       
   196         fs = CNcdFileStorage::NewLC( iOwner, aUid, path );
       
   197         iFileStorages.AppendL( fs );
       
   198         CleanupStack::Pop( fs );        
       
   199         CleanupStack::PopAndDestroy( &path );
       
   200         }
       
   201     else 
       
   202         {
       
   203         fs = iFileStorages[index];
       
   204         }
       
   205         
       
   206     DASSERT( fs );
       
   207     DLTRACEOUT( ( _L("fs: %x"), fs ) );
       
   208     return *fs;    
       
   209     }
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Namespace getter 
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 const TDesC& CNcdStorage::Namespace() const
       
   217     {
       
   218     return iNamespace;
       
   219     }
       
   220 
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Directory getter 
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 const TDesC& CNcdStorage::Directory() const
       
   227     {
       
   228     return iDirectory;
       
   229     }
       
   230 
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // File session getter
       
   234 // ---------------------------------------------------------------------------
       
   235 //    
       
   236 RFs& CNcdStorage::FileSession()
       
   237     {
       
   238     return iOwner.FileSession();
       
   239     }
       
   240 
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // Size getter
       
   244 // ---------------------------------------------------------------------------
       
   245 //    
       
   246 TInt CNcdStorage::SizeL()
       
   247     {
       
   248     DLTRACEIN((""));
       
   249     HBufC* path = CurrentPathLC();
       
   250     TInt size = FileSystemAllocationL( FileSession(), *path );
       
   251     DLTRACEOUT(( _L("Size for path %S is %d bytes"), path, size ));
       
   252     CleanupStack::PopAndDestroy( path );
       
   253     return size;
       
   254     }
       
   255     
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // Constructor
       
   259 // ---------------------------------------------------------------------------
       
   260 //    
       
   261 CNcdStorage::CNcdStorage( MNcdStorageOwner& aOwner ) : iOwner( aOwner )
       
   262     {
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // Searches for a database by name
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 TInt CNcdStorage::FindDbStorageByUid( const TDesC& aUid )
       
   270     {
       
   271     for ( TInt i = 0; i < iDbStorages.Count(); ++i )
       
   272         {
       
   273         if ( iDbStorages[i]->Uid().Compare( aUid ) == 0 ) 
       
   274             {
       
   275             return i;
       
   276             }
       
   277         }
       
   278     return KErrNotFound;
       
   279     }
       
   280     
       
   281     
       
   282 // ---------------------------------------------------------------------------
       
   283 // Searches for a file storage by name
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 TInt CNcdStorage::FindFileStorageByUid( const TDesC& aUid )
       
   287     {
       
   288     for ( TInt i = 0; i < iFileStorages.Count(); ++i )
       
   289         {
       
   290         if ( iFileStorages[i]->Uid().Compare( aUid ) == 0 ) 
       
   291             {
       
   292             return i;
       
   293             }
       
   294         }
       
   295     return KErrNotFound;
       
   296     }
       
   297     
       
   298     
       
   299 // ---------------------------------------------------------------------------
       
   300 // Generates the current path
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CNcdStorage::CreateCurrentPathL( TDes& aPath, const TDesC& aUid ) const
       
   304     {
       
   305     iOwner.AppendRoot( aPath );
       
   306        
       
   307     DLTRACE(( _L("Adding namespace %S"), &iNamespace ));
       
   308     aPath.Append( iDirectory );
       
   309     aPath.Append( KDirectorySeparator );    
       
   310     DLTRACE(( _L("Adding uid %S"), &aUid ));
       
   311     aPath.Append( aUid );                
       
   312     aPath.Append( KDirectorySeparator );    
       
   313     
       
   314     // Create the directory
       
   315     BaflUtils::EnsurePathExistsL( iOwner.FileSession(), aPath );    
       
   316     }
       
   317 
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // Storage path getter
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 HBufC* CNcdStorage::CurrentPathLC() const
       
   324     {
       
   325     HBufC* path = HBufC::NewLC( KMaxPath );
       
   326     TPtr ptr( path->Des() );
       
   327     iOwner.AppendRoot( ptr );
       
   328     ptr.Append( iDirectory );
       
   329     ptr.Append( KDirectorySeparator );    
       
   330     return path;
       
   331     }