filemanager/App/src/CFileManagerStringCache.cpp
changeset 0 6a9f87576119
child 14 efe289f793e7
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0""
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Cache for strings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <AknUtils.h>
       
    21 #include <StringLoader.h>
       
    22 #include <FileManager.rsg>
       
    23 #include <CFileManagerEngine.h>
       
    24 #include <CFileManagerItemProperties.h>
       
    25 #include <CFileManagerCommonDefinitions.h>
       
    26 #include <CFileManagerFeatureManager.h>
       
    27 #include "CFileManagerStringCache.h"
       
    28 #include "CFileManagerIconArray.h"
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KFmgrMaximumSizeOfIconIdString = 3;
       
    32 _LIT( KIconStr, "%d" );
       
    33 const TInt KMaxSizeString = 16;
       
    34 const TInt64 KKileByte = 1024;
       
    35 const TInt64 KMegaByte = 1048576;
       
    36 const TInt64 KGigaByte = 1073741824;
       
    37 const TInt64 KKileByteLowLimit = 100; // 100 bytes
       
    38 const TInt64 KMegaByteLowLimit = 1048576; // 1MB
       
    39 const TInt64 KGigaByteLowLimit = 1073741824; // 1GB
       
    40 const TInt KSizeDecimals = 1;
       
    41 const TInt KSizeTextArrayLen = 2;
       
    42 
       
    43 _LIT( KDateFormat1, "%1" );
       
    44 _LIT( KDateFormat2, "%2" );
       
    45 _LIT( KDateFormat3, "%3" );
       
    46 const TUint KSecondSeparator = 1;
       
    47 const TUint KThirdSeparator = 2;
       
    48 const TInt KDateStrMax = 20;
       
    49 
       
    50 
       
    51 // ============================ LOCAL FUNCTIONS ================================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // GetUnitAndFormatSize
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 static TInt GetUnitAndFormatSize( TDes& aDes, const TInt64& aSize )
       
    58     {
       
    59     TRealFormat sizeFormat( aDes.MaxLength(), KSizeDecimals );
       
    60     sizeFormat.iType |= KDoNotUseTriads;
       
    61     TInt ret( R_QTN_FMGR_UNITS_BYTE );
       
    62     if ( aSize >= KGigaByteLowLimit )
       
    63         {
       
    64         // Format in GBs with decimals
       
    65         TReal size( I64REAL( aSize ) / I64REAL( KGigaByte ) );
       
    66         aDes.AppendNum( size, sizeFormat );
       
    67         ret = R_QTN_FMGR_UNITS_GIGABYTE;
       
    68         }
       
    69     else if ( aSize >= KMegaByteLowLimit )
       
    70         {
       
    71         // Format in MBs with decimals
       
    72         TReal size( I64REAL( aSize ) / I64REAL( KMegaByte ) );
       
    73         aDes.AppendNum( size, sizeFormat );
       
    74         ret = R_QTN_FMGR_UNITS_MEGABYTE;
       
    75         }
       
    76     else if ( aSize >= KKileByteLowLimit )
       
    77         {
       
    78         // Format in kBs with decimals
       
    79         TReal size( I64REAL( aSize ) / I64REAL( KKileByte ) );
       
    80         aDes.AppendNum( size, sizeFormat );
       
    81         ret = R_QTN_FMGR_UNITS_KILOBYTE;
       
    82         }
       
    83     else
       
    84         {
       
    85         // Format in bytes
       
    86         aDes.AppendNum( aSize );
       
    87         }
       
    88     AknTextUtils::LanguageSpecificNumberConversion( aDes );
       
    89     return ret;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // GetFreeSpaceStringL
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 static HBufC* GetFreeSpaceStringL( const TInt64& aSize )
       
    97     {
       
    98     CDesCArray* sizeTexts = new ( ELeave ) CDesCArrayFlat( KSizeTextArrayLen );
       
    99     CleanupStack::PushL( sizeTexts );
       
   100     TBuf< KMaxSizeString > size;
       
   101     HBufC* unit = StringLoader::LoadLC( GetUnitAndFormatSize( size, aSize ) );
       
   102     sizeTexts->AppendL( size );
       
   103     sizeTexts->AppendL( *unit );
       
   104     CleanupStack::PopAndDestroy( unit );
       
   105     HBufC* ret = StringLoader::LoadL( R_QTN_FMGR_FREE_MEMORY_VAR_UNITS,
       
   106         *sizeTexts );
       
   107     CleanupStack::PopAndDestroy( sizeTexts );
       
   108     return ret;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // DateStringL
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 static HBufC* DateStringL( const TTime& aTime )
       
   116     {
       
   117     TBuf< KDateStrMax > dateStr;
       
   118     TBuf< KDateStrMax > dateStrFormat;
       
   119     TLocale local;
       
   120     dateStrFormat.Append( KDateFormat1 );
       
   121     dateStrFormat.Append( local.DateSeparator( KSecondSeparator ) );
       
   122     dateStrFormat.Append( KDateFormat2 );
       
   123     dateStrFormat.Append( local.DateSeparator( KThirdSeparator ) );
       
   124     dateStrFormat.Append( KDateFormat3 );
       
   125     aTime.FormatL( dateStr, dateStrFormat );
       
   126     return dateStr.AllocL();
       
   127     }
       
   128 
       
   129 // ============================ MEMBER FUNCTIONS ===============================
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CFileManagerStringCache::CFileManagerStringCache
       
   133 // C++ default constructor can NOT contain any code, that
       
   134 // might leave.
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 CFileManagerStringCache::CFileManagerStringCache(
       
   138         CFileManagerEngine& aEngine,
       
   139         CFileManagerIconArray& aIconArray ) :
       
   140     iEngine( aEngine ),
       
   141     iIconArray( aIconArray )
       
   142     {
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CFileManagerStringCache::NewL
       
   147 // Two-phased constructor.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 CFileManagerStringCache* CFileManagerStringCache::NewL(
       
   151         CFileManagerEngine& aEngine,
       
   152         CFileManagerIconArray& aIconArray )
       
   153     {
       
   154     CFileManagerStringCache* self = new( ELeave )
       
   155         CFileManagerStringCache( aEngine, aIconArray );
       
   156     
       
   157     CleanupStack::PushL( self );
       
   158     self->ConstructL();
       
   159     CleanupStack::Pop( self );
       
   160 
       
   161     return self;
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CFileManagerStringCache::ConstructL
       
   166 // 
       
   167 // -----------------------------------------------------------------------------
       
   168 // 
       
   169 void CFileManagerStringCache::ConstructL()
       
   170     {
       
   171     Clear();
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CFileManagerStringCache::~CFileManagerStringCache
       
   176 // Destructor
       
   177 // -----------------------------------------------------------------------------
       
   178 // 
       
   179 CFileManagerStringCache::~CFileManagerStringCache()
       
   180     {
       
   181     Clear();
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CFileManagerStringCache::MdcaCount
       
   186 // 
       
   187 // -----------------------------------------------------------------------------
       
   188 // 
       
   189 TInt CFileManagerStringCache::MdcaCount() const
       
   190     {
       
   191     return iEngine.FileList()->MdcaCount();
       
   192     }
       
   193         
       
   194 // -----------------------------------------------------------------------------
       
   195 // CFileManagerStringCache::MdcaPoint
       
   196 // 
       
   197 // -----------------------------------------------------------------------------
       
   198 // 
       
   199 TPtrC CFileManagerStringCache::MdcaPoint( TInt aIndex ) const
       
   200     {
       
   201     TInt err( KErrNone );
       
   202     TInt index( FormattedStringFound( aIndex ) );
       
   203 
       
   204     if ( index >= 0 )
       
   205         {
       
   206         return FormattedString( index );
       
   207         }
       
   208 
       
   209     iFormatString.Zero();
       
   210 
       
   211     // Get icon
       
   212     TInt iconId( EFileManagerOtherFileIcon );
       
   213     TRAP( err, iconId = iEngine.IconIdL( aIndex ) );
       
   214     if ( err != KErrNone )
       
   215         {
       
   216         iconId = EFileManagerOtherFileIcon;
       
   217         SetError( err );
       
   218         }
       
   219     // Get name
       
   220     CFileManagerItemProperties* prop = NULL;
       
   221     TRAP( err, prop = iEngine.GetItemInfoL( aIndex ) );
       
   222     if ( err == KErrNone )
       
   223         {
       
   224         TPtrC ptr( prop->LocalizedName() );
       
   225         if ( iconId != EFileManagerFolderIcon &&
       
   226              iconId != EFileManagerFolderSubIcon &&
       
   227              iconId != EFileManagerFolderEmptyIcon )
       
   228             {
       
   229             // Append file name with conversion
       
   230             HBufC* buffer = NULL;
       
   231             TRAP( err,  buffer = AknTextUtils::ConvertFileNameL( ptr ) );
       
   232             if ( err == KErrNone )
       
   233                 {
       
   234                 iFormatString.Append( *buffer );
       
   235                 }
       
   236             else
       
   237                 {
       
   238                 // Put file name without any convertion, its better than nothing
       
   239                 iFormatString.Append( ptr );
       
   240                 SetError( err );
       
   241                 }
       
   242             delete buffer;
       
   243             }
       
   244         else
       
   245             {
       
   246             // Append folder name
       
   247             iFormatString.Append( ptr );
       
   248             }
       
   249         }
       
   250     else
       
   251         {
       
   252         SetError( err );
       
   253         }
       
   254     // Remove all other possible tabs in string than the icon separator,
       
   255     // so that listbox won't get broken
       
   256     AknTextUtils::StripCharacters( iFormatString, KFmgrTab );
       
   257 
       
   258     AknTextUtils::ReplaceCharacters( iFormatString, KFmgrLineFeed, KFmgrSpace()[0] );
       
   259     AknTextUtils::ReplaceCharacters( iFormatString, KFmgrParagraphSeparator, KFmgrSpace()[0] );
       
   260 
       
   261     TInt iconIndex( iIconArray.FindIcon( iconId ) );
       
   262     if ( iconIndex >= 0 )
       
   263         {
       
   264         iFormatString.Insert( 0, KFmgrTab );
       
   265         TBuf<KFmgrMaximumSizeOfIconIdString> numBuf;
       
   266         numBuf.Copy( KIconStr );
       
   267         numBuf.Format( KIconStr, iconIndex );
       
   268         iFormatString.Insert( 0, numBuf );
       
   269         }
       
   270     else
       
   271         {
       
   272         SetError( iconIndex );
       
   273         }
       
   274 
       
   275     if ( err == KErrNone && prop && prop->IsDrive() )
       
   276         {
       
   277         TFileManagerDriveInfo drvInfo;
       
   278         TRAP( err, iEngine.GetDriveInfoL( prop->DriveId(), drvInfo ) );
       
   279         if ( err == KErrNone )
       
   280             {
       
   281             // Show free space for accessible local drives
       
   282             if ( ( drvInfo.iState & TFileManagerDriveInfo::EDrivePresent ) &&
       
   283                  !( drvInfo.iState & ( TFileManagerDriveInfo::EDriveRemote |
       
   284                                        TFileManagerDriveInfo::EDriveCorrupted |
       
   285                                        TFileManagerDriveInfo::EDriveLocked |
       
   286                                        TFileManagerDriveInfo::EDriveInUse ) ) )
       
   287                 {
       
   288                 HBufC* freeSpace = NULL;
       
   289                 TRAP( err, freeSpace = GetFreeSpaceStringL( drvInfo.iSpaceFree ) );
       
   290                 if ( err == KErrNone )
       
   291                     {
       
   292                     iFormatString.Append( KFmgrTab );
       
   293                     iFormatString.Append( *freeSpace );
       
   294                     }
       
   295                 else
       
   296                     {
       
   297                     SetError( err );
       
   298                     }
       
   299                 delete freeSpace;
       
   300                 }
       
   301             // Show connection icon for connected remote drives
       
   302             else if ( ( drvInfo.iState & TFileManagerDriveInfo::EDriveRemote ) &&
       
   303                       ( drvInfo.iState & TFileManagerDriveInfo::EDriveConnected ) )
       
   304                 {
       
   305                 iconIndex = iIconArray.FindIcon(
       
   306                     EFileManagerRemoteDriveConnectedIcon );
       
   307                 if ( iconIndex >= 0 )
       
   308                     {
       
   309                     iFormatString.Append( KFmgrTab );
       
   310                     iFormatString.Append( KFmgrTab );
       
   311                     iFormatString.AppendNum( iconIndex );
       
   312                     }
       
   313                 else if ( iconIndex != KErrNotFound )
       
   314                     {
       
   315                     SetError( iconIndex );
       
   316                     }
       
   317                 }
       
   318             }
       
   319         }
       
   320     else if ( err == KErrNone && prop && !prop->FullPath().Length() )
       
   321         {
       
   322         // Show the latest backup date
       
   323         TTime time( 0 );
       
   324         if( prop->ModifiedLocalDate( time ) == KErrNone )
       
   325             {
       
   326             HBufC* date = NULL;
       
   327             if ( !(time.DateTime().Year()) )
       
   328             	{
       
   329             	iFormatString.Append( KFmgrTab );
       
   330             	iFormatString.Append( KNullDesC );
       
   331             	}
       
   332             else
       
   333             	{
       
   334 	            
       
   335 	            TRAPD( err2, date = DateStringL( time ) );
       
   336 	            if ( err2 == KErrNone )
       
   337 	                {
       
   338 	                iFormatString.Append( KFmgrTab );
       
   339 	                iFormatString.Append( *date );
       
   340 	                delete date;
       
   341 	                }
       
   342             	}
       
   343             }
       
   344         }
       
   345 
       
   346     delete prop;
       
   347 
       
   348     TRAP( err, StoreFormattedStringL( aIndex ) );
       
   349     if( err != KErrNone )
       
   350         {
       
   351         Clear();
       
   352         SetError( err );
       
   353         }
       
   354 
       
   355     return iFormatString;
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CFileManagerStringCache::Clear
       
   360 // 
       
   361 // -----------------------------------------------------------------------------
       
   362 // 
       
   363 void CFileManagerStringCache::Clear() const
       
   364     {
       
   365     for ( TInt i( 0 ); i < KNumStringCacheItems; i++ )
       
   366         {
       
   367         Clear( i );
       
   368         }
       
   369     iError = KErrNone;
       
   370     }
       
   371 
       
   372 // -----------------------------------------------------------------------------
       
   373 // CFileManagerStringCache::FormattedStringFound
       
   374 // 
       
   375 // -----------------------------------------------------------------------------
       
   376 // 
       
   377 TInt CFileManagerStringCache::FormattedStringFound( TInt aIndex ) const
       
   378     {
       
   379     for ( TInt i( 0 ); i < KNumStringCacheItems; i++ )
       
   380         {
       
   381         if ( iFormattedItems[ i ].iIndex == aIndex )
       
   382             {
       
   383             return i;
       
   384             }
       
   385         }
       
   386     return KErrNotFound;
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CFileManagerStringCache::FormattedString
       
   391 // 
       
   392 // -----------------------------------------------------------------------------
       
   393 // 
       
   394 TPtrC CFileManagerStringCache::FormattedString( TInt aIndex ) const 
       
   395     {
       
   396     TCacheItem& item = iFormattedItems[ aIndex ];
       
   397     if ( item.iString )
       
   398         {
       
   399         return item.iString->Des();
       
   400         }
       
   401     return TPtrC( KNullDesC );
       
   402     }
       
   403  
       
   404 // -----------------------------------------------------------------------------
       
   405 // CFileManagerStringCache::StoreFormattedString
       
   406 // 
       
   407 // -----------------------------------------------------------------------------
       
   408 // 
       
   409 void CFileManagerStringCache::StoreFormattedStringL( TInt aIndex ) const
       
   410     {
       
   411     TInt count( MdcaCount() );
       
   412     TInt replace( 0 );
       
   413     TInt maxDist( 0 );
       
   414 
       
   415     // Find index to be replaced for cyclic list scroll
       
   416     for ( TInt i( 0 ); i < KNumStringCacheItems; i++ )
       
   417         {
       
   418         TCacheItem& item = iFormattedItems[ i ];
       
   419         if ( item.iIndex != KErrNotFound )
       
   420             {
       
   421             TInt dist( Min( Abs( item.iIndex - aIndex ),
       
   422                 ( count - aIndex ) + item.iIndex ) );
       
   423             if ( dist > maxDist )
       
   424                 {
       
   425                 maxDist = dist;
       
   426                 replace = i;
       
   427                 }
       
   428             }
       
   429         else
       
   430             {
       
   431             replace = i;
       
   432             break;
       
   433             }
       
   434         }
       
   435     // Setup new cached item
       
   436     Clear( replace );
       
   437     TCacheItem& item = iFormattedItems[ replace ];
       
   438     item.iString = iFormatString.AllocL();
       
   439     item.iIndex = aIndex;
       
   440     }
       
   441 
       
   442 // -----------------------------------------------------------------------------
       
   443 // CFileManagerStringCache::Clear
       
   444 // 
       
   445 // -----------------------------------------------------------------------------
       
   446 // 
       
   447 void CFileManagerStringCache::Clear( TInt aIndex ) const
       
   448     {
       
   449     TCacheItem& item = iFormattedItems[ aIndex ];
       
   450     delete item.iString;
       
   451     item.iString = NULL;
       
   452     item.iIndex = KErrNotFound;
       
   453     }
       
   454 
       
   455 // -----------------------------------------------------------------------------
       
   456 // CFileManagerStringCache::LastError
       
   457 // 
       
   458 // -----------------------------------------------------------------------------
       
   459 // 
       
   460 TInt CFileManagerStringCache::LastError() const
       
   461     {
       
   462     return iError;
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // CFileManagerStringCache::SetError
       
   467 // 
       
   468 // -----------------------------------------------------------------------------
       
   469 // 
       
   470 void CFileManagerStringCache::SetError( TInt aError ) const
       
   471     {
       
   472     if ( aError == KErrNoMemory ||
       
   473         ( aError != KErrNone && iError == KErrNone ) )
       
   474         {
       
   475         iError = aError;
       
   476         }
       
   477     }
       
   478 
       
   479 // End of File