engine/collectionframework/datasource/manager/src/glxstringcache.cpp
changeset 23 74c9f037fd5d
equal deleted inserted replaced
5:f7f0874bfe7d 23:74c9f037fd5d
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:   This class caches resource strings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <glxlog.h>
       
    21 
       
    22 #include <barsc.h>
       
    23 #include <data_caging_path_literals.hrh>
       
    24 #include <bautils.h>
       
    25 #include <glxtracer.h>
       
    26 
       
    27 #include "glxstringcache.h"
       
    28 
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // NewL
       
    32 // ----------------------------------------------------------------------------
       
    33 //
       
    34 CGlxStringCache::CGlxStringItem* CGlxStringCache::CGlxStringItem::NewL(TInt aId, HBufC* aString)
       
    35     {
       
    36     CGlxStringItem* self = new (ELeave) CGlxStringItem(aId);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL(aString);
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41     }
       
    42     
       
    43 // ----------------------------------------------------------------------------
       
    44 // ConstructL
       
    45 // ----------------------------------------------------------------------------
       
    46 //
       
    47 void CGlxStringCache::CGlxStringItem::ConstructL(HBufC* aString)
       
    48     {
       
    49     if (NULL == aString)
       
    50         {
       
    51         User::Leave(KErrArgument);
       
    52         }
       
    53     
       
    54     iString = (*aString).AllocL();
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // Constructor
       
    59 // ----------------------------------------------------------------------------
       
    60 //
       
    61 CGlxStringCache::CGlxStringItem::CGlxStringItem(TInt aId)
       
    62 : iId(aId)
       
    63     {
       
    64     
       
    65     }
       
    66     
       
    67 // ----------------------------------------------------------------------------
       
    68 // Destructor
       
    69 // ----------------------------------------------------------------------------
       
    70 //
       
    71 CGlxStringCache::CGlxStringItem::~CGlxStringItem()
       
    72     {
       
    73     delete iString;
       
    74     }
       
    75     
       
    76 // ----------------------------------------------------------------------------
       
    77 // GetId
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 
       
    81 TInt CGlxStringCache::CGlxStringItem::GetId() const
       
    82     {
       
    83     return iId;
       
    84     }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 // GetString
       
    88 // ----------------------------------------------------------------------------
       
    89 //
       
    90 
       
    91 HBufC* CGlxStringCache::CGlxStringItem::GetString()
       
    92     {
       
    93     return iString;
       
    94     }
       
    95  
       
    96 // ----------------------------------------------------------------------------
       
    97 // NewL
       
    98 // ----------------------------------------------------------------------------
       
    99 //
       
   100  
       
   101 EXPORT_C CGlxStringCache* CGlxStringCache::NewL()
       
   102 	{
       
   103 	CGlxStringCache* self = new(ELeave) CGlxStringCache;
       
   104 	return self;
       
   105 	}
       
   106     
       
   107 // ----------------------------------------------------------------------------
       
   108 // Constructor
       
   109 // ----------------------------------------------------------------------------
       
   110 //
       
   111 CGlxStringCache::CGlxStringCache()
       
   112     {
       
   113     TRACER("CGlxStringCache::CGlxStringCache()");
       
   114     iResourceFileName.Zero();
       
   115     }
       
   116     
       
   117 // ----------------------------------------------------------------------------
       
   118 // Destructor
       
   119 // ----------------------------------------------------------------------------
       
   120 //
       
   121 CGlxStringCache::~CGlxStringCache()
       
   122     {
       
   123     TRACER("CGlxStringCache::~CGlxStringCache()");
       
   124     iStringCache.ResetAndDestroy();
       
   125     }
       
   126     
       
   127 // ----------------------------------------------------------------------------
       
   128 // Count
       
   129 // ----------------------------------------------------------------------------
       
   130 //
       
   131 TInt CGlxStringCache::Count()
       
   132     {
       
   133     TRACER("TInt CGlxStringCache::Count()");
       
   134     return iStringCache.Count();
       
   135     }
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // Find
       
   139 // ----------------------------------------------------------------------------
       
   140 //
       
   141 HBufC* CGlxStringCache::FindL(TInt aId)
       
   142     {
       
   143     TRACER("HBufC* CGlxStringCache::FindL(TInt aId)");
       
   144     // Create a TIdentityRelation to be used in the Find
       
   145     TIdentityRelation<CGlxStringCache::CGlxStringItem> match(&MatchById);
       
   146     
       
   147     // Create a dummy string item with the correct id to use in the find
       
   148     CGlxStringItem* dummyItem = new (ELeave) CGlxStringItem(aId);
       
   149     
       
   150     // Call Find
       
   151     TInt index = iStringCache.Find(dummyItem, match);
       
   152     
       
   153     // delete the dummy item
       
   154     delete dummyItem;
       
   155     
       
   156     
       
   157     if (KErrNotFound != index)
       
   158         {
       
   159         // The string was found so create a copy and pass to caller
       
   160         CGlxStringItem* item = iStringCache[index];
       
   161         return item->GetString()->Des().AllocL();
       
   162         }
       
   163     
       
   164     // String was not found    
       
   165     return NULL;
       
   166     }
       
   167     
       
   168 // ----------------------------------------------------------------------------
       
   169 // InsertL
       
   170 // ----------------------------------------------------------------------------
       
   171 //
       
   172 void CGlxStringCache::InsertL(TInt aId, HBufC* aString)
       
   173     {
       
   174     TRACER("void CGlxStringCache::InsertL(TInt aId, HBufC* aString)");
       
   175     // Create a TLinearOrder to be used in the InsertInOrder
       
   176     TLinearOrder<CGlxStringCache::CGlxStringItem> orderer(&OrderById);
       
   177     
       
   178     // Create the string item
       
   179     CGlxStringItem* item = CGlxStringItem::NewL(aId, aString);
       
   180     
       
   181     // attempt to insert it
       
   182     TInt err = iStringCache.InsertInOrder(item, orderer);
       
   183     
       
   184     switch (err)
       
   185         {
       
   186     case KErrNone:
       
   187         // Do nothing as item has been inserted into cache
       
   188         break;
       
   189         
       
   190     case KErrAlreadyExists:
       
   191         // This is not an error, but we must tidy up
       
   192         delete item;
       
   193         break;
       
   194         
       
   195     default:
       
   196         // We have an error
       
   197         delete item;
       
   198         User::Leave(err);
       
   199         break;
       
   200         }
       
   201     }
       
   202     
       
   203 // ----------------------------------------------------------------------------
       
   204 // Compares two Id's by subtraction
       
   205 // ----------------------------------------------------------------------------
       
   206 //
       
   207 TInt CGlxStringCache::OrderById(const CGlxStringCache::CGlxStringItem& aItem1, const CGlxStringCache::CGlxStringItem& aItem2)
       
   208     {
       
   209     TRACER("TInt CGlxStringCache::OrderById()");
       
   210     return aItem1.GetId() - aItem2.GetId();
       
   211     }
       
   212     
       
   213 // ----------------------------------------------------------------------------
       
   214 // Compares two Id's logical equality
       
   215 // ----------------------------------------------------------------------------
       
   216 //
       
   217 TBool CGlxStringCache::MatchById(const CGlxStringCache::CGlxStringItem& aItem1, const CGlxStringCache::CGlxStringItem& aItem2)
       
   218     {
       
   219     TRACER("TBool CGlxStringCache::MatchById()");
       
   220     return aItem1.GetId() == aItem2.GetId();
       
   221     }
       
   222     
       
   223 // ----------------------------------------------------------------------------
       
   224 // Load the String
       
   225 // ----------------------------------------------------------------------------
       
   226 //    
       
   227 EXPORT_C HBufC* CGlxStringCache::LoadLocalizedStringLC(const TDesC& aResourceFile, const TInt aResourceId)
       
   228     {
       
   229     TRACER("HBufC* CGlxCollectionPluginBase::LoadLocalizedStringLC()");
       
   230     HBufC* string = NULL;
       
   231     
       
   232     TFileName resourceFile;
       
   233     resourceFile.Copy(aResourceFile);
       
   234     
       
   235     TRAPD(err, string = LoadLocalizedStringFromDriveL(resourceFile, aResourceId));
       
   236     
       
   237     switch (err)
       
   238         {
       
   239         case KErrNone:
       
   240             break;
       
   241             
       
   242         case KErrNotFound:
       
   243             {
       
   244             // Could not find resource file on drive z so try on drive c
       
   245             _LIT(KCDrive,"c");
       
   246             resourceFile.Copy(KCDrive);
       
   247             resourceFile.Append(aResourceFile.Mid(1));
       
   248             string = LoadLocalizedStringFromDriveL(resourceFile, aResourceId);
       
   249             }
       
   250             break;
       
   251             
       
   252         default:
       
   253             {
       
   254             GLX_DEBUG2("LEAVING LoadLocalizedStringLC with %d", err);
       
   255             User::Leave(err);
       
   256             }
       
   257             break;
       
   258         }
       
   259             
       
   260     CleanupStack::PushL(string);
       
   261     
       
   262     return string;
       
   263     }
       
   264     
       
   265 // ----------------------------------------------------------------------------
       
   266 // Loads the String from a drive
       
   267 // ----------------------------------------------------------------------------
       
   268 //    
       
   269 HBufC* CGlxStringCache::LoadLocalizedStringFromDriveL(const TDesC& aResourceFile, const TInt aResourceId)
       
   270     {
       
   271     TRACER("HBufC* CGlxCollectionPluginBase::LoadLocalizedStringFromDriveL()");
       
   272     
       
   273     HBufC* string = NULL;
       
   274     
       
   275 	if (NULL != (string = FindL(aResourceId)))
       
   276 		{
       
   277 		// String has been cached to return it
       
   278 		return string;
       
   279 		}
       
   280 			
       
   281     
       
   282     // The string has not been cached to read from resource file
       
   283     RFs fs;
       
   284     User::LeaveIfError(fs.Connect());
       
   285     CleanupClosePushL(fs);
       
   286     
       
   287     // find out if the resource name file has been cached
       
   288     TBool resoureFileNameCached = (0 != iResourceFileName.Length());
       
   289     TFileName resFile;
       
   290     
       
   291     if (resoureFileNameCached)
       
   292         {
       
   293         // Use cached resource file name
       
   294         resFile.Copy(iResourceFileName);
       
   295         }
       
   296     else
       
   297         {
       
   298         // Caluculate the path and language extension
       
   299         TParse parse;
       
   300         parse.Set( aResourceFile, &KDC_ECOM_RESOURCE_DIR, NULL );
       
   301         resFile.Copy(parse.FullName());
       
   302         
       
   303     	TLanguage language = ELangNone;
       
   304     	BaflUtils::NearestLanguageFile( fs, resFile, language);
       
   305     	// Do not cache the file name here as it could be the wrong drive
       
   306     	// causing a leave later
       
   307         }
       
   308 	
       
   309     RResourceFile resourceFile; 
       
   310     resourceFile.OpenL(fs, resFile);
       
   311     CleanupClosePushL(resourceFile);
       
   312     resourceFile.ConfirmSignatureL(0);          // must use BA_RSS_SIGNATURE in resource file (not RSS_SIGNATURE).
       
   313     
       
   314     TResourceReader resReader;
       
   315     
       
   316     HBufC8* buf = resourceFile.AllocReadLC( aResourceId ); 
       
   317     resReader.SetBuffer( buf );
       
   318     string  = resReader.ReadHBufCL();
       
   319     CleanupStack::PopAndDestroy( buf );
       
   320     
       
   321     // We have succesfully read from the resource so 
       
   322     // check to see if we need to cache the file name
       
   323     if (!resoureFileNameCached)
       
   324         {
       
   325         // Cache the resource file name
       
   326         iResourceFileName.Copy(resFile);
       
   327         }
       
   328     
       
   329     CleanupStack::PopAndDestroy( &resourceFile );
       
   330     CleanupStack::PopAndDestroy( &fs );
       
   331     
       
   332     CleanupStack::PushL(string);    
       
   333     
       
   334     InsertL(aResourceId, string); // InsertL won't create duplicates
       
   335     
       
   336     CleanupStack::Pop(string);
       
   337     
       
   338     return string;
       
   339     }