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