uiutils/Localizer/src/CDirectoryLocalizer.cpp
changeset 0 2f259fa3e83a
child 7 08e69e956a8c
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Source file
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <barsread.h>
       
    21 #include <bautils.h>
       
    22 #include <coemain.h>
       
    23 #include <DIRECTORYLOCALIZER.RSG>
       
    24 #ifdef RD_MULTIPLE_DRIVE
       
    25  #include <pathinfo.h>
       
    26 #endif // RD_MULTIPLE_DRIVE
       
    27 
       
    28 #include "CDirectoryLocalizer.h"
       
    29 
       
    30 //  LOCAL CONSTANTS AND MACROS
       
    31 
       
    32 // A resource file that contains the default directories and their localized names
       
    33 // Note for code inspector: make sure the path is correct
       
    34 _LIT( KDLDefaultResourceFile, "z:\\resource\\directorylocalizer.rsc" );
       
    35 
       
    36 // Total number of default localized directories is 13.
       
    37 // 8 * 2 = 16, leaves some room for app specific additions without reallocation
       
    38 const TInt KDLEntryGranularity = 8;
       
    39 
       
    40 // Entry index of an entry that was never found
       
    41 const TInt KDLEntryNotFound = -1;
       
    42 
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CDirectoryLocalizer::CDirectoryLocalizer
       
    48 // C++ default constructor can NOT contain any code, that might leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CDirectoryLocalizer::CDirectoryLocalizer()
       
    52     :   iCurrentEntry( -1 ),
       
    53         iIconIndices( KDLEntryGranularity )
       
    54     {
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CDirectoryLocalizer::ConstructL
       
    59 // Symbian 2nd phase constructor can leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CDirectoryLocalizer::ConstructL()
       
    63     {
       
    64     // Initialize the arrays to hold the data
       
    65     iPaths = new( ELeave ) CArrayPtrFlat< TDesC >( KDLEntryGranularity );
       
    66     iLocalizedNames = new( ELeave ) CArrayPtrFlat< TDesC >( KDLEntryGranularity );
       
    67     iExtraData = new( ELeave ) CArrayPtrFlat< TDesC >( KDLEntryGranularity );
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CDirectoryLocalizer::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C CDirectoryLocalizer* CDirectoryLocalizer::NewL()
       
    76     {
       
    77     CDirectoryLocalizer* self = new( ELeave ) CDirectoryLocalizer();
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     
       
    81     // Will be set to NULL if CCoeEnv does not exist
       
    82     CCoeEnv* cone = CCoeEnv::Static();
       
    83     RFs fsSession;
       
    84     if( cone )
       
    85         {
       
    86         // Get a fs session from CCoeEnv
       
    87         fsSession = cone->FsSession();
       
    88         }
       
    89     else
       
    90         {
       
    91         // Create own fs session (take care for also closing it)
       
    92         User::LeaveIfError( fsSession.Connect() ) ;
       
    93         CleanupClosePushL( fsSession );
       
    94         }
       
    95         
       
    96     // Get the correct language version
       
    97     TFileName resourceFileName( KDLDefaultResourceFile );
       
    98     BaflUtils::NearestLanguageFile( fsSession, resourceFileName );
       
    99 
       
   100     RResourceFile resourceFile;
       
   101     resourceFile.OpenL( fsSession, resourceFileName );  // Open the resource file
       
   102     CleanupClosePushL( resourceFile );
       
   103 
       
   104     // The argument of ConfirmSignatureL has no meaning.
       
   105     // Declaration of the method is ConfirmSignatureL(TInt /*aSignature*/);
       
   106     resourceFile.ConfirmSignatureL( 0 );
       
   107 
       
   108     // Read the resource containing the default entries.
       
   109     HBufC8* defaultEntries = resourceFile.AllocReadLC( R_DIRECTORYLOCALIZER_DEFAULT_ENTRIES );
       
   110     TResourceReader reader;
       
   111     reader.SetBuffer( defaultEntries );
       
   112 
       
   113     // Add the default entries to the localizer
       
   114     self->AddFromResourceL( reader );
       
   115 
       
   116     CleanupStack::PopAndDestroy();  // defaultEntries
       
   117     CleanupStack::PopAndDestroy();  // resourceFile.Close()
       
   118     if( !cone )
       
   119         {
       
   120         // If created new fs session - close it:
       
   121         CleanupStack::PopAndDestroy();  // fsSession.Close()
       
   122         }
       
   123     CleanupStack::Pop();            // self
       
   124     return self;
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CDirectoryLocalizer::NewL
       
   129 // Two-phased constructor.
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 EXPORT_C CDirectoryLocalizer* CDirectoryLocalizer::NewL( TResourceReader& aReader )
       
   133     {
       
   134     CDirectoryLocalizer* self = new( ELeave ) CDirectoryLocalizer();
       
   135     CleanupStack::PushL( self );
       
   136     self->ConstructL();
       
   137     self->AddFromResourceL( aReader );
       
   138     CleanupStack::Pop();
       
   139     return self;
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CDirectoryLocalizer::NewL
       
   144 // Two-phased constructor.
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C CDirectoryLocalizer* CDirectoryLocalizer::NewL( TInt aResourceId )
       
   148     {
       
   149     TResourceReader reader;
       
   150     CCoeEnv::Static()->CreateResourceReaderLC( reader, aResourceId );
       
   151     CDirectoryLocalizer* self = NewL( reader );
       
   152     CleanupStack::PopAndDestroy();      // reader
       
   153     return self;
       
   154     }
       
   155 
       
   156 // Destructor
       
   157 EXPORT_C CDirectoryLocalizer::~CDirectoryLocalizer()
       
   158     {
       
   159     if( iPaths )
       
   160         {
       
   161         iPaths->ResetAndDestroy();
       
   162         }
       
   163     if( iLocalizedNames )
       
   164         {
       
   165         iLocalizedNames->ResetAndDestroy();
       
   166         }
       
   167     if( iExtraData )
       
   168         {
       
   169         iExtraData->ResetAndDestroy();
       
   170         }
       
   171     delete iPaths;
       
   172     delete iLocalizedNames;
       
   173     delete iExtraData;
       
   174     iIconIndices.Close();       // RArray going out of scope. Must Close().
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CDirectoryLocalizer::AddFromResourceL
       
   179 // ?implementation_description
       
   180 // (other items were commented in a header).
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C void CDirectoryLocalizer::AddFromResourceL( TResourceReader& aReader )
       
   184     {
       
   185     // Read the amount of entries in the resource
       
   186     TInt directoryCount( aReader.ReadInt16() );
       
   187 
       
   188     // read directory data from the resource
       
   189     for ( TInt i( 0 ); i < directoryCount; i++ )
       
   190         {
       
   191         // Read path
       
   192         HBufC* desc = aReader.ReadHBufCL();     // returns NULL if the resource string is empty
       
   193         // an entry must always have a path
       
   194         __ASSERT_ALWAYS( desc, User::Panic( _L("DirLocalizer"), 0 ) );
       
   195         CleanupStack::PushL( desc );
       
   196         iPaths->AppendL( desc );
       
   197         CleanupStack::Pop();
       
   198 
       
   199         // Read logical name
       
   200         desc = aReader.ReadHBufCL();
       
   201         // an entry must always have a logical name
       
   202         __ASSERT_ALWAYS( desc, User::Panic( _L("DirLocalizer"), 0 ) );
       
   203         CleanupStack::PushL( desc );
       
   204         iLocalizedNames->AppendL( desc );
       
   205         CleanupStack::Pop();
       
   206 
       
   207         // Read extra data
       
   208         desc = aReader.ReadHBufCL();    // Append also null pointer to the array
       
   209         CleanupStack::PushL( desc );    // Nulls will be handled in ExtraData()
       
   210         iExtraData->AppendL( desc );
       
   211         CleanupStack::Pop();
       
   212 
       
   213         // Read icon index
       
   214         iIconIndices.Append( aReader.ReadInt16() );
       
   215         }
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CDirectoryLocalizer::AddFromResourceL
       
   220 // ?implementation_description
       
   221 // (other items were commented in a header).
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 EXPORT_C void CDirectoryLocalizer::AddFromResourceL( TInt aResourceId )
       
   225     {
       
   226     TResourceReader reader;
       
   227     CCoeEnv::Static()->CreateResourceReaderLC( reader, aResourceId );
       
   228     this->AddFromResourceL( reader );
       
   229     CleanupStack::PopAndDestroy();      // reader
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CDirectoryLocalizer::SetFullPath
       
   234 // ?implementation_description
       
   235 // (other items were commented in a header).
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 #ifdef RD_MULTIPLE_DRIVE
       
   239 EXPORT_C void CDirectoryLocalizer::SetFullPath( const TDesC& aFullPath )
       
   240     {
       
   241     TBool localizationNeeded( EFalse );
       
   242     // Will be set to NULL if CCoeEnv does not exist
       
   243     CCoeEnv* cone = CCoeEnv::Static();
       
   244     RFs fs;
       
   245     if( cone )
       
   246         {
       
   247         // Get a fs session from CCoeEnv
       
   248         fs = cone->FsSession();
       
   249         }
       
   250     else
       
   251         {
       
   252         // Create own fs session (take care for also closing it)
       
   253         User::LeaveIfError( fs.Connect() ) ;
       
   254         CleanupClosePushL( fs );
       
   255         }  
       
   256     if ( aFullPath.Length() > 0 )
       
   257         {
       
   258         TInt drive = TDriveUnit( aFullPath );
       
   259         TDriveInfo driveInfo;        
       
   260         if ( fs.Drive( driveInfo, drive ) == KErrNone )
       
   261             {
       
   262             // No localization for remote drives
       
   263             localizationNeeded = !( driveInfo.iDriveAtt & KDriveAttRemote );
       
   264             }
       
   265         }
       
   266     
       
   267     if( !cone )
       
   268         {
       
   269         // If created new fs session - close it:
       
   270         CleanupStack::PopAndDestroy();  // fsSession.Close()
       
   271         }
       
   272     if ( localizationNeeded )
       
   273         {
       
   274         TInt pathType( PathInfo::PathType( aFullPath ) );
       
   275         if ( pathType != PathInfo::ENotSystemPath )
       
   276             {
       
   277             TInt count ( iPaths->Count() );
       
   278             for ( TInt i( 0 ); i < count; i++ )
       
   279                 {                
       
   280                 if ( PathInfo::PathType( *( iPaths->At( i ) ) ) == pathType )
       
   281                     {
       
   282                     iCurrentEntry = i;
       
   283                     return;
       
   284                     }
       
   285                 }
       
   286             }
       
   287         }
       
   288     iCurrentEntry = KDLEntryNotFound;   // Didn't find a match
       
   289     }
       
   290  
       
   291 #else // RD_MULTIPLE_DRIVE 
       
   292 EXPORT_C void CDirectoryLocalizer::SetFullPath( const TDesC& aFullPath )
       
   293     {
       
   294     // Looks up aFullPath in the entry list. The operation is case-insensitive
       
   295     TInt count ( iPaths->Count() );
       
   296 
       
   297     for ( TInt i( 0 ); i < count; i++ )
       
   298         {
       
   299         if ( ( iPaths->At( i )->CompareF( aFullPath ) ) == 0 )  // if the descriptors are equal after folding
       
   300             {
       
   301             iCurrentEntry = i;
       
   302             return;
       
   303             }
       
   304         }
       
   305     iCurrentEntry = KDLEntryNotFound;   // Didn't find a match
       
   306     } 
       
   307 #endif // RD_MULTIPLE_DRIVE
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CDirectoryLocalizer::IsLocalized
       
   311 // ?implementation_description
       
   312 // (other items were commented in a header).
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 EXPORT_C TBool CDirectoryLocalizer::IsLocalized() const
       
   316     {
       
   317     if ( iCurrentEntry == KDLEntryNotFound )    // SetFullPath() hadn't found the path from the list
       
   318         {
       
   319         return EFalse;
       
   320         }
       
   321     else
       
   322         {
       
   323         return ETrue;
       
   324         }
       
   325     }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CDirectoryLocalizer::LocalizedName
       
   329 // ?implementation_description
       
   330 // (other items were commented in a header).
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 EXPORT_C const TDesC& CDirectoryLocalizer::LocalizedName() const
       
   334     {
       
   335     if ( iCurrentEntry == KDLEntryNotFound )    // SetFullPath() hadn't found the path from the list
       
   336         {
       
   337         return KNullDesC;
       
   338         }
       
   339     else
       
   340         {
       
   341         return *iLocalizedNames->At( iCurrentEntry );
       
   342         }
       
   343     }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CDirectoryLocalizer::ExtraData
       
   347 // ?implementation_description
       
   348 // (other items were commented in a header).
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 EXPORT_C const TDesC& CDirectoryLocalizer::ExtraData() const
       
   352     {
       
   353     if ( iCurrentEntry == KDLEntryNotFound )    // SetFullPath() hadn't found the path from the list
       
   354         {
       
   355         return KNullDesC;
       
   356         }
       
   357     else
       
   358         {
       
   359         TDesC* desc = iExtraData->At( iCurrentEntry );
       
   360         if ( desc == NULL )     // ExtraData might be a NULL ( see AddFromResourceL() )
       
   361             {
       
   362             return KNullDesC;
       
   363             }
       
   364         else
       
   365             {
       
   366             return *desc;
       
   367             }
       
   368         }
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CDirectoryLocalizer::Icon
       
   373 // ?implementation_description
       
   374 // (other items were commented in a header).
       
   375 // -----------------------------------------------------------------------------
       
   376 //
       
   377 EXPORT_C TInt CDirectoryLocalizer::Icon() const
       
   378     {
       
   379     if ( iCurrentEntry == KDLEntryNotFound )    // SetFullPath() hadn't found the path from the list
       
   380         {
       
   381         return KErrGeneral;
       
   382         }
       
   383     else
       
   384         {
       
   385         return iIconIndices[ iCurrentEntry ];
       
   386         }
       
   387     }
       
   388 
       
   389 //  End of File