lafagnosticuifoundation/cone/src/CoeDataStorage.cpp
changeset 0 2f259fa3e83a
child 30 56e9a0aaad89
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include <s32mem.h>
       
    18 #include <centralrepository.h>
       
    19 #include <coemain.h>
       
    20 #include "coedatastorage.h"
       
    21 #include <coedef.h>
       
    22 #include <gulcolor.h>
       
    23 #include <barsread.h>
       
    24 #include "coepanic.h"
       
    25 #include <bautils.h>
       
    26 #include "coedefkeys.h"
       
    27 
       
    28 const TUint KUidSystemColorRepository=0x10272619;
       
    29 const TUint KSystemColorRepositoryKey=0;
       
    30 const TInt KColorListBufferGranularity=1024;
       
    31 const TUint KUidFepSpecificSettingsRepository=0x10009793; // also defined in fepbase\source\Fepbase.cpp
       
    32 
       
    33 // CCoeDataStorage
       
    34 
       
    35 EXPORT_C CCoeDataStorage& CCoeDataStorage::GetL(CCoeEnv& aCoeEnv)
       
    36 	{ // static
       
    37 	COwner* owner=STATIC_CAST(COwner*, aCoeEnv.FindStatic(TUid::Uid(COwner::ECoeStaticUid)));
       
    38 	if (owner==NULL)
       
    39 		{
       
    40 		CCoeDataStorage* const dataStorage=NewL();
       
    41 		CleanupStack::PushL(dataStorage);
       
    42 		owner=new(ELeave) COwner(dataStorage);
       
    43 		CleanupStack::Pop(dataStorage);
       
    44 		}
       
    45 	return *owner->DataStorage();
       
    46 	}
       
    47 
       
    48 EXPORT_C CCoeDataStorage* CCoeDataStorage::NewL()
       
    49 	{ // static
       
    50 	return new(ELeave) CCoeDataStorage;
       
    51 	}
       
    52 
       
    53 EXPORT_C CCoeDataStorage::~CCoeDataStorage()
       
    54 	{
       
    55 	delete iFepFrameworkRepository;
       
    56 	delete iFepSpecificSettingsRepository;
       
    57 	delete iSystemColorListRepository;
       
    58 	}
       
    59 
       
    60 EXPORT_C void CCoeDataStorage::GetInstalledFepIdL(TDes& aFepId)
       
    61 	{
       
    62 	CRepository& repository=FepFrameworkRepositoryL();
       
    63 	if (repository.Get(ERepositoryKey_DynamicFepId, aFepId)!=KErrNone)
       
    64 		{
       
    65 		User::LeaveIfError(repository.Get(ERepositoryKey_DefaultFepId, aFepId));
       
    66 		}
       
    67 	}
       
    68 
       
    69 EXPORT_C void CCoeDataStorage::SetInstalledFepIdL(const TDesC& aFepId)
       
    70 	{
       
    71 	User::LeaveIfError(FepFrameworkRepositoryL().Set(ERepositoryKey_DynamicFepId, aFepId));
       
    72 	}
       
    73 
       
    74 EXPORT_C void CCoeDataStorage::GetFepAttributeL(TUid aAttributeUid, TDes8& aAttributeData)
       
    75 	{
       
    76 	CRepository& repository=FepSpecificSettingsRepositoryL();
       
    77 	User::LeaveIfError(repository.Get(aAttributeUid.iUid, aAttributeData));
       
    78 	}
       
    79 
       
    80 EXPORT_C void CCoeDataStorage::SetFepAttributeL(TUid aAttributeUid, const TDesC8& aAttributeData)
       
    81 	{
       
    82 	User::LeaveIfError(FepSpecificSettingsRepositoryL().Set(aAttributeUid.iUid, aAttributeData));
       
    83 	}
       
    84 
       
    85 /** This method will return a new CColorList object poplulated with the color information
       
    86 read from a configuration ini-file. If the ini-file cannot be found, this method will return NULL, 
       
    87 in which case the color list should be read from resource file. This is the action taken by PopulateColorArrayL(). */
       
    88 EXPORT_C CColorList* CCoeDataStorage::GetSystemColorListL()
       
    89 	{
       
    90 	TBool doesExist = EFalse;
       
    91 	CBufFlat* const buffer = GetSystemColorListBufferLC(doesExist);
       
    92 	CColorList* colorList = NULL;
       
    93 	if (doesExist)
       
    94 		{
       
    95 		colorList=CColorList::NewLC();
       
    96 		RBufReadStream readStream(*buffer);
       
    97 		TRAPD(err,colorList->InternalizeL(readStream));
       
    98 		CleanupStack::Pop(colorList);
       
    99 		if (err != KErrNone)
       
   100 			{
       
   101 			delete colorList;
       
   102 			colorList=NULL;
       
   103 			}
       
   104 		}
       
   105 	CleanupStack::PopAndDestroy(buffer);
       
   106 	return colorList;
       
   107 	}
       
   108 
       
   109 EXPORT_C CBufBase* CCoeDataStorage::GetSystemColorListBufferL()
       
   110 	{
       
   111 	TBool exists=EFalse;
       
   112 	CBufFlat* const buffer=GetSystemColorListBufferLC(exists);
       
   113 	if (!exists)
       
   114 		{
       
   115 		buffer->Reset();
       
   116 		}
       
   117 	CleanupStack::Pop(buffer);
       
   118 	return buffer;
       
   119 	}
       
   120 
       
   121 EXPORT_C void CCoeDataStorage::SetSystemColorListL(const CColorList& aColorList)
       
   122 	{
       
   123 	CBufFlat* const buffer=CBufFlat::NewL(KColorListBufferGranularity);
       
   124 	CleanupStack::PushL(buffer);
       
   125 	RBufWriteStream writeStream(*buffer);
       
   126 	aColorList.ExternalizeL(writeStream);
       
   127 	writeStream.CommitL();
       
   128 	const TPtrC8 data(buffer->Ptr(0));
       
   129 	SetSystemColorListFromBufferL(data);
       
   130 	CleanupStack::PopAndDestroy(buffer);
       
   131 	}
       
   132 
       
   133 EXPORT_C void CCoeDataStorage::SetSystemColorListFromBufferL(const TDesC8& aBuffer)
       
   134 	{
       
   135 	User::LeaveIfError(SystemColorListRepositoryL().Set(KSystemColorRepositoryKey, aBuffer));
       
   136 	}
       
   137 
       
   138 /**
       
   139 Returns a new color list object. The list will be populated from a configuration ini-file.
       
   140 If the ini-file cannot be found, the list will be populated with the default colors from eikpriv.rsc.
       
   141 */
       
   142 EXPORT_C CColorList* CCoeDataStorage::PopulateColorArrayL()
       
   143 	{
       
   144 	CColorList* colorList =GetSystemColorListL();
       
   145 	if (colorList==NULL)
       
   146 		{
       
   147 		TInt numGrays=0;
       
   148 		TInt numColors=0;
       
   149 		RFs fs;
       
   150 		CleanupClosePushL(fs);
       
   151 		User::LeaveIfError(fs.Connect());
       
   152 		RWsSession ws;
       
   153 		CleanupClosePushL(ws);
       
   154 		User::LeaveIfError(ws.Connect(fs));
       
   155 		TDisplayMode defaultMode=ws.GetDefModeMaxNumColors(numColors,numGrays);
       
   156 		const TInt resourceId=(defaultMode>=EColor16? KCoeColorColor16:KCoeColorGray4);
       
   157 		_LIT(KResourceFileName,"z:\\Resource\\Uiklaf\\eikpriv.rsc");
       
   158 		TFileName resourceFileName(KResourceFileName);
       
   159 		BaflUtils::NearestLanguageFile(fs,resourceFileName);
       
   160 		if (!(BaflUtils::FileExists(fs, resourceFileName)))
       
   161 			{
       
   162 			_LIT(KAppResourceFileName,"z:\\Resource\\Apps\\eikpriv.rsc");
       
   163 			resourceFileName = KAppResourceFileName;
       
   164 			BaflUtils::NearestLanguageFile(fs,resourceFileName);
       
   165 			}
       
   166 		RResourceFile resourceFile;
       
   167 		resourceFile.OpenL(fs, resourceFileName);
       
   168 		CleanupClosePushL(resourceFile);
       
   169 		resourceFile.ConfirmSignatureL(ECoeResourceSignatureValue);
       
   170 		resourceFile.Offset();
       
   171 		
       
   172 		HBufC8* resource= resourceFile.AllocReadLC(resourceId);
       
   173 		TResourceReader reader;
       
   174 		reader.SetBuffer(resource);
       
   175 		const TInt listId=reader.ReadInt32();
       
   176 		const TInt extrasId=reader.ReadInt32();
       
   177 		HBufC8* resourceList =resourceFile.AllocReadLC(listId);
       
   178 		reader.SetBuffer(resourceList);			
       
   179 		CColorArray* colorArray=CColorArray::NewLC();
       
   180 		ResourceUtils::PopulateColorArrayL(*colorArray,reader);
       
   181 		__ASSERT_ALWAYS(colorArray->Count()==EColorLabelHighlightFullEmphasis+1,Panic(ECoePanicInvalidColorRange));
       
   182 		CArrayFix<TRgb>* colors=new(ELeave) CArrayFixFlat<TRgb>(8);
       
   183 		CleanupStack::PushL(colors);
       
   184 		for (TInt ii=EColorWindowBackground;ii<=EColorLabelHighlightFullEmphasis;ii++)
       
   185 			{
       
   186 			__ASSERT_ALWAYS(colorArray->Contains(ii),Panic(ECoePanicLogicalColorNotFound));
       
   187 			colors->InsertL(STATIC_CAST(TLogicalColor,ii),colorArray->Color(ii));
       
   188 			}
       
   189 		colorList=CColorList::NewL(colors);
       
   190 		CleanupStack::Pop(colors); // colors
       
   191 		CleanupStack::PushL(colorList);
       
   192 		CColorArray* extrasArray=CColorArray::NewLC();
       
   193 		HBufC8* resourceExtras=resourceFile.AllocReadLC(extrasId);
       
   194 		reader.SetBuffer(resourceExtras);		
       
   195 		ResourceUtils::PopulateColorArrayL(*extrasArray,reader);
       
   196 		colorList->AddColorArrayL(TUid::Uid(KCoeCustomColorsArrayValue),extrasArray);
       
   197 		CleanupStack::PopAndDestroy(resourceExtras);
       
   198 		CleanupStack::Pop(extrasArray); // extrasArray
       
   199 		TRAP_IGNORE(SetSystemColorListL(*colorList) );
       
   200 		CleanupStack::Pop(colorList);
       
   201 		CleanupStack::PopAndDestroy(colorArray); // colorArray
       
   202 		CleanupStack::PopAndDestroy(resourceList);
       
   203 		CleanupStack::PopAndDestroy(resource);
       
   204 		CleanupStack::PopAndDestroy(3); // resourceFile,ws, fs
       
   205 		}
       
   206 	return colorList;
       
   207 	}
       
   208 
       
   209 
       
   210 CRepository& CCoeDataStorage::FepFrameworkRepositoryL()
       
   211 	{
       
   212 	if (iFepFrameworkRepository==NULL)
       
   213 		{
       
   214 		iFepFrameworkRepository=CRepository::NewL(TUid::Uid(KUidFepFrameworkRepository));
       
   215 		}
       
   216 	return *iFepFrameworkRepository;
       
   217 	}
       
   218 
       
   219 CRepository& CCoeDataStorage::FepSpecificSettingsRepositoryL()
       
   220 	{
       
   221 	if (iFepSpecificSettingsRepository==NULL)
       
   222 		{
       
   223 		iFepSpecificSettingsRepository=CRepository::NewL(TUid::Uid(KUidFepSpecificSettingsRepository));
       
   224 		}
       
   225 	return *iFepSpecificSettingsRepository;
       
   226 	}
       
   227 
       
   228 CRepository& CCoeDataStorage::SystemColorListRepositoryL()
       
   229 	{
       
   230 	if (iSystemColorListRepository==NULL)
       
   231 		{
       
   232 		iSystemColorListRepository=CRepository::NewL(TUid::Uid(KUidSystemColorRepository));
       
   233 		}
       
   234 	return *iSystemColorListRepository;
       
   235 	}
       
   236 
       
   237 CBufFlat* CCoeDataStorage::GetSystemColorListBufferLC(TBool& aExists)
       
   238 	{
       
   239 	aExists=ETrue;
       
   240 	CRepository& repository=SystemColorListRepositoryL();
       
   241 	CBufFlat* const buffer=CBufFlat::NewL(2048);
       
   242 	CleanupStack::PushL(buffer);
       
   243 	FOREVER
       
   244 		{
       
   245 		TInt sizeOfBufferToAllocate=0;
       
   246 		TPtr8 bufferAsDescriptor(buffer->Ptr(0));
       
   247 		const TInt error=repository.Get(KSystemColorRepositoryKey, bufferAsDescriptor, sizeOfBufferToAllocate);
       
   248 		if (error==KErrOverflow)
       
   249 			{
       
   250 			buffer->ResizeL(sizeOfBufferToAllocate);
       
   251 			}
       
   252 		else
       
   253 			{
       
   254 			if (error==KErrNotFound)
       
   255 				{
       
   256 				aExists=EFalse;
       
   257 				}
       
   258 			else
       
   259 				{
       
   260 				User::LeaveIfError(error);
       
   261 				}
       
   262 			break;
       
   263 			}
       
   264 		}
       
   265 	return buffer;
       
   266 	}
       
   267 
       
   268 // CCoeDataStorage::COwner
       
   269 
       
   270 CCoeDataStorage::COwner::COwner(CCoeDataStorage* aDataStorage)
       
   271 	:CCoeStatic(TUid::Uid(ECoeStaticUid), KMinTInt), // the second parameter (aDestructionPriority) must be less than zero so that this CCoeStatic object still exists when the CCoeEnv's iExtra->iFepTracker is destroyed, as CCoeFepTracker's destructor uses this CCoeStatic object (via CCoeFepTracker's iRepository reference)
       
   272 	 iDataStorage(aDataStorage)
       
   273 	{
       
   274 	}
       
   275 
       
   276 CCoeDataStorage::COwner::~COwner()
       
   277 	{
       
   278 	delete iDataStorage;
       
   279 	}
       
   280