appfw/apparchitecture/aplist/apsiconcaptionoverride.cpp
changeset 0 2e3d3ce01487
child 81 676b6116ca93
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2008-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 // apsiconcaptionoverride.cpp
       
    15 //
       
    16 
       
    17 #include "apsiconcaptionoverride.h"
       
    18 #include <centralrepository.h>
       
    19 #include <apadef.h>
       
    20 
       
    21 //constants defined
       
    22 const TUid KUidIconCaptionRepository = { 0x1028583d }; // Central Repository UID
       
    23 const TUint32 KAppBits = 0xFF000000; // App mask bits
       
    24 const TUint32 KFieldBits = 0x00FF0000; // Field mask bits
       
    25 const TUint32 KUidKey = 0x00FFFFFF; // UID mask bits
       
    26 
       
    27 // Field id values for short caption, caption, icon count, icon file name
       
    28 // of an application.
       
    29 enum TFieldId
       
    30 	{
       
    31 	EOverrideFieldShortCaption,
       
    32 	EOverrideFieldCaption,
       
    33 	EOverrideFieldNumIcons,
       
    34 	EOverrideFieldIconFileName
       
    35 	};
       
    36 
       
    37 /** 
       
    38 Stores the Central Repository configuration details into a Hash table.
       
    39 Two Hash tables defined to store integer and string type data separately.
       
    40 
       
    41 @internalComponent
       
    42 */
       
    43 NONSHARABLE_CLASS(CApaIconCaptionOverrideStore) : public CBase
       
    44 	{
       
    45 public:
       
    46 	CApaIconCaptionOverrideStore();
       
    47 	~CApaIconCaptionOverrideStore();
       
    48 	
       
    49 	const TDesC* String(TUint32 aKey) const;
       
    50 	void SetStringL(TUint32 aKey, const TDesC& aString);
       
    51 	const TInt* Int(TUint32 aKey) const;
       
    52 	void SetIntL(TUint32 aKey, TInt aInt);
       
    53 	
       
    54 private:
       
    55 	typedef RHashMap<TUint32, TInt> RIntMap;
       
    56 	RIntMap iIntMap;
       
    57 	typedef RHashMap<TUint32, HBufC*> RStringMap;
       
    58 	RStringMap iStringMap;
       
    59 	};
       
    60 
       
    61 /**
       
    62 A utility class used to write information into a store (CApaIconCaptionOverrideStore).
       
    63 
       
    64 @internalComponent
       
    65 */
       
    66 NONSHARABLE_CLASS(TApaIconCaptionOverrideWriter)
       
    67 	{
       
    68 public:
       
    69 	TApaIconCaptionOverrideWriter(CApaIconCaptionOverrideStore& aStore);
       
    70 	void LoadFieldFromCenRepL(CRepository* aRepository, TUint32 aFullKey);
       
    71 
       
    72 private:
       
    73 	CApaIconCaptionOverrideStore& iStore;
       
    74 	};
       
    75 
       
    76 // Constructor
       
    77 CApaIconCaptionOverrideStore::CApaIconCaptionOverrideStore()
       
    78 	{
       
    79 	}
       
    80 
       
    81 // Destructor
       
    82 CApaIconCaptionOverrideStore::~CApaIconCaptionOverrideStore()
       
    83 	{
       
    84 	RStringMap::TIter pString(iStringMap);
       
    85 	for (HBufC* const* str = pString.NextValue();
       
    86 		 str;
       
    87 		 str = pString.NextValue())
       
    88 		delete *str;
       
    89 	iStringMap.Close();
       
    90 	iIntMap.Close();
       
    91 	}
       
    92 
       
    93 /** 
       
    94 Looks up a specified key in the associative array and returns a pointer to the corresponding value.
       
    95 
       
    96 @param aKey The key object of type integer to look up.
       
    97 @return A pointer to the corresponding string value.  
       
    98 */
       
    99 const TDesC* CApaIconCaptionOverrideStore::String(TUint32 aKey) const
       
   100 	{
       
   101 	HBufC* const* pStr = iStringMap.Find(aKey);
       
   102 	if (pStr)
       
   103 		return *pStr;
       
   104 	else
       
   105 		return NULL;
       
   106 	}
       
   107 
       
   108 /**
       
   109 Inserts the key-value pair into the array (string Hash table).
       
   110 
       
   111 @param aKey The key object of type integer to add to the array.
       
   112 @param aString The value object of type string to associate with aKey.
       
   113 */
       
   114 void CApaIconCaptionOverrideStore::SetStringL(TUint32 aKey, const TDesC& aString)
       
   115 	{
       
   116 	HBufC* newStr = aString.AllocL();
       
   117 	CleanupStack::PushL(newStr);
       
   118 	HBufC* const* pStr = iStringMap.Find(aKey);
       
   119 	if (pStr)
       
   120 		{
       
   121 		delete *pStr;
       
   122 		}
       
   123 	iStringMap.InsertL(aKey, newStr);
       
   124 	CleanupStack::Pop(newStr);
       
   125 	}
       
   126 
       
   127 /**
       
   128 Looks up a specified key in the associative array and returns a pointer to the corresponding value.
       
   129 
       
   130 @param aKey The key object of type integer to look up
       
   131 @return A pointer to the corresponding integer value.
       
   132 */
       
   133 const TInt* CApaIconCaptionOverrideStore::Int(TUint32 aKey) const
       
   134 	{
       
   135 	const TInt* pInt = iIntMap.Find(aKey);
       
   136 	return pInt;
       
   137 	}
       
   138 
       
   139 /**
       
   140 Inserts the key-value pair into the array (integer Hash table)
       
   141 
       
   142 @param aKey The key object of type integer to add to the array.
       
   143 @param aInt The value object of type integer to associate with aKey.
       
   144 */
       
   145 void CApaIconCaptionOverrideStore::SetIntL(TUint32 aKey, TInt aInt)
       
   146 	{
       
   147 	iIntMap.InsertL(aKey, aInt);
       
   148 	}
       
   149 
       
   150 // Constructor
       
   151 TApaIconCaptionOverrideWriter::TApaIconCaptionOverrideWriter(CApaIconCaptionOverrideStore& aStore)
       
   152 : iStore(aStore)
       
   153 	{
       
   154 	}
       
   155 
       
   156 /**
       
   157 Reads the Central Repository integer, string settings and inserts the key-value pair into Store.
       
   158 
       
   159 Each setting is enumerated by a 32-bit key. The top 8 bits of the key is for app identification,
       
   160 the next 8 bits is for field (short caption (00), caption (01),total number of icons (02), 
       
   161 icon filename (03)) identification and next 16 bits for language identification.
       
   162 
       
   163 It is computed in the following way
       
   164 
       
   165 	00XXXXXX - App UID field
       
   166 	XX00XXXX - Short Caption field
       
   167 	XX01XXXX - Caption field
       
   168 	XX02XXXX - Icon count field
       
   169 	XX03XXXX - Icon Filename field
       
   170 	XXXX0000 - Language field
       
   171 
       
   172 First it performs a bitwise NOT operation on application mask key (KAppBits) and its value is bitwise
       
   173 AND with the 32 bit key of the field.
       
   174 
       
   175 It performs a bitwise AND with the field mask key (KFieldBits) and does a bitwise right shift to 16 bits
       
   176 to extract the the field bit value from the 32 bit key.
       
   177 
       
   178 It reads each field's (short caption, caption, total number of icons, icon file name) value from the
       
   179 Central Repository and inserts into a Store (Hash table).
       
   180 
       
   181 @param aRepository The object provides access to Central Repository
       
   182 @param aFullKey The 32-bit field key
       
   183 */
       
   184 void TApaIconCaptionOverrideWriter::LoadFieldFromCenRepL(CRepository* aRepository, TUint32 aFullKey)
       
   185 	{
       
   186 	TUint32 key = aFullKey & ~KAppBits;
       
   187 	TUint32 fieldId = (key & KFieldBits) >> 16;
       
   188 	switch (fieldId)
       
   189 		{
       
   190 		case EOverrideFieldShortCaption:
       
   191 			{
       
   192 			TApaAppCaption shortCaption;
       
   193 			if (aRepository->Get(aFullKey, shortCaption) == KErrNone)
       
   194 				iStore.SetStringL(key, shortCaption);
       
   195 			break;
       
   196 			}
       
   197 		case EOverrideFieldCaption:
       
   198 			{
       
   199 			TApaAppCaption caption;
       
   200 			if (aRepository->Get(aFullKey, caption) == KErrNone)
       
   201 				iStore.SetStringL(key, caption);
       
   202 			break;
       
   203 			}
       
   204 		case EOverrideFieldNumIcons:
       
   205 			int numIcons;
       
   206 			if (aRepository->Get(aFullKey, numIcons) == KErrNone)
       
   207 				iStore.SetIntL(key, numIcons);
       
   208 			break;
       
   209 		case EOverrideFieldIconFileName:
       
   210 			{
       
   211 			TFileName fileName;
       
   212 			if (aRepository->Get(aFullKey, fileName) == KErrNone)
       
   213 				iStore.SetStringL(key, fileName);
       
   214 			break;
       
   215 			}
       
   216 		default:
       
   217 			break;
       
   218 		}
       
   219 	}
       
   220 
       
   221 // Constructor
       
   222 TApaIconCaptionOverrideReader::TApaIconCaptionOverrideReader(const CApaIconCaptionOverrideStore& aStore, const RArray<TLanguage>& aLanguageDowngradePath)
       
   223 : iStore(aStore), iLanguageDowngradePath(aLanguageDowngradePath)
       
   224 	{
       
   225 	}
       
   226 
       
   227 /**
       
   228 Gets the short caption value of the corresponding setting from store.
       
   229 */
       
   230 const TDesC* TApaIconCaptionOverrideReader::ShortCaption() const
       
   231 	{
       
   232 	return GetString(EOverrideFieldShortCaption);
       
   233 	}
       
   234 
       
   235 /**
       
   236 Gets the caption value of the corresponding setting from store.
       
   237 */
       
   238 const TDesC* TApaIconCaptionOverrideReader::Caption() const
       
   239 	{
       
   240 	return GetString(EOverrideFieldCaption);
       
   241 	}
       
   242 
       
   243 /**
       
   244 Gets the total number of icons value of the corresponding setting from the
       
   245 store and checks whether it is NULL.
       
   246 */
       
   247 TBool TApaIconCaptionOverrideReader::NumIconsSet() const
       
   248 	{
       
   249 	return GetInt(EOverrideFieldNumIcons) != NULL;
       
   250 	}
       
   251 
       
   252 /**
       
   253 Gets the total number of icons of the corresponding setting from store.
       
   254 */
       
   255 TInt TApaIconCaptionOverrideReader::NumIcons() const
       
   256 	{
       
   257 	const TInt* pInt = GetInt(EOverrideFieldNumIcons);
       
   258 	if (pInt)
       
   259 		return *pInt;
       
   260 	else
       
   261 		return 0;
       
   262 	}
       
   263 
       
   264 /**
       
   265 Gets the icon file name of the corresponding setting from store.
       
   266 */
       
   267 const TDesC* TApaIconCaptionOverrideReader::IconFileName() const
       
   268 	{
       
   269 	return GetString(EOverrideFieldIconFileName);
       
   270 	}
       
   271 
       
   272 /**
       
   273 Reads store and retrieves the corresponding string value of the passed field Id.
       
   274 
       
   275 Gets the configuration information with reference to the language downgrade path, with
       
   276 ELangNone as default language, in case there is no match is found.
       
   277 
       
   278 @param aFieldId Field Id values for short caption, caption, icon count, icon file name.
       
   279 @return A pointer to the corresponding string value.
       
   280 */
       
   281 const TDesC* TApaIconCaptionOverrideReader::GetString(TUint32 aFieldId) const
       
   282 	{
       
   283 	TInt count = iLanguageDowngradePath.Count();
       
   284 	for (TInt ii=0; ii<count; ii++)
       
   285 		{
       
   286 		TLanguage language = iLanguageDowngradePath[ii];
       
   287 		TUint32 key = (aFieldId << 16) | language;
       
   288 		const TDesC* pString = iStore.String(key);
       
   289 		if (pString)
       
   290 			return pString;
       
   291 		}
       
   292 	// default fallback language is ELangNone
       
   293 	TUint32 key = (aFieldId << 16) | ELangNone;
       
   294 	const TDesC* pString = iStore.String(key);
       
   295 	if (pString)
       
   296 		return pString;
       
   297 	return NULL;
       
   298 	}
       
   299 
       
   300 /**
       
   301 Reads store and retrieves the corresponding integer value of the passed field Id.
       
   302 
       
   303 Gets the configuration information with reference to the language downgrade path, with
       
   304 ELangNone as default language, in case there is no match is found.
       
   305 
       
   306 @param aFieldId Field Id values for short caption, caption, icon count, icon file name.
       
   307 @return A pointer to the corresponding integer value.
       
   308 */
       
   309 const TInt* TApaIconCaptionOverrideReader::GetInt(TUint32 aFieldId) const
       
   310 	{
       
   311 	TInt count = iLanguageDowngradePath.Count();
       
   312 	for (TInt ii=0; ii<count; ii++)
       
   313 		{
       
   314 		TLanguage language = iLanguageDowngradePath[ii];
       
   315 		TUint32 key = (aFieldId << 16) | language;
       
   316 		const TInt* pInt = iStore.Int(key);
       
   317 		if (pInt)
       
   318 			return pInt;
       
   319 		}
       
   320 	// default fallback language is ELangNone
       
   321 	TUint32 key = (aFieldId << 16) | ELangNone;
       
   322 	const TInt* pInt = iStore.Int(key);
       
   323 	if (pInt)
       
   324 		return pInt;
       
   325 	return NULL;
       
   326 	}
       
   327 
       
   328 // Constructor
       
   329 CApaIconCaptionOverridesForApp::CApaIconCaptionOverridesForApp()
       
   330 	{
       
   331 	}
       
   332 
       
   333 // Destructor
       
   334 CApaIconCaptionOverridesForApp::~CApaIconCaptionOverridesForApp()
       
   335 	{
       
   336 	delete iStore;
       
   337 	}
       
   338 
       
   339 CApaIconCaptionOverridesForApp* CApaIconCaptionOverridesForApp::NewL()
       
   340 	{
       
   341 	CApaIconCaptionOverridesForApp* self = new (ELeave) CApaIconCaptionOverridesForApp;
       
   342 	CleanupStack::PushL(self);
       
   343 	self->ConstructL();
       
   344 	CleanupStack::Pop(); // self
       
   345 	return self;
       
   346 	}
       
   347 
       
   348 /** 2nd phase constructor that creates an object to store the Central Repository configuration
       
   349 details (integer & string values) into separate Hash tables.
       
   350 */
       
   351 void CApaIconCaptionOverridesForApp::ConstructL()
       
   352 	{
       
   353 	iStore = new(ELeave) CApaIconCaptionOverrideStore;
       
   354 	}
       
   355 
       
   356 /**
       
   357 Finds all the settings that exist and match the specification given by partial key (aAppKey) and
       
   358 application mask key (KAppBits) from the Central Repository.
       
   359 
       
   360 All the 32-bit field keys have been retrieved from the Central Repository and stored into an array.
       
   361 It performs a bitwise NOT operation on application mask key (KAppBits) and its value is bitwise AND
       
   362 with each of the value retrieved from the array. If the new value does not match with the UID mask key,
       
   363 it tries to read each field setting value and stores the information into a store (Hash tables).
       
   364 
       
   365 @param aRepository The object provides access to Central Repository.
       
   366 @param aAppKey partial key.
       
   367 */
       
   368 void CApaIconCaptionOverridesForApp::LoadFromCenRepL(CRepository* aRepository, TUint32 aAppKey)
       
   369 	{
       
   370 	TApaIconCaptionOverrideWriter writer(*iStore);
       
   371 	RArray<TUint32> appKeys;
       
   372 	CleanupClosePushL(appKeys);
       
   373 	User::LeaveIfError(aRepository->FindL(aAppKey, KAppBits, appKeys));
       
   374 	TInt count = appKeys.Count();
       
   375 	for (TInt ii=0; ii<count; ii++)
       
   376 		{
       
   377 		TUint32 key = appKeys[ii];
       
   378 		if ((key & ~KAppBits) == KUidKey)
       
   379 			continue;
       
   380 		writer.LoadFieldFromCenRepL(aRepository, key);
       
   381 		}
       
   382 	CleanupStack::PopAndDestroy(&appKeys);	
       
   383 	}
       
   384 
       
   385 /**
       
   386 Creates an object to read the configuration information with reference to the language downgrade path,
       
   387 with ELangNone used as the default language, if no better match is found.
       
   388 */
       
   389 TApaIconCaptionOverrideReader CApaIconCaptionOverridesForApp::Reader(const RArray<TLanguage>& aLanguageDowngradePath) const
       
   390 	{
       
   391 	TApaIconCaptionOverrideReader reader(*iStore, aLanguageDowngradePath);
       
   392 	return reader;
       
   393 	}
       
   394 
       
   395 // Constructor
       
   396 CApaIconCaptionOverrides::CApaIconCaptionOverrides()
       
   397 	{
       
   398 	}
       
   399 
       
   400 // Destructor
       
   401 CApaIconCaptionOverrides::~CApaIconCaptionOverrides()
       
   402 	{
       
   403 	DeleteOverrides();
       
   404 	}
       
   405 
       
   406 /**
       
   407 Finds all the settings that exist and match the specification given by partial and mask keys, from the
       
   408 Central Repository. If the settings are found, it reads the values and stores icon, caption details
       
   409 into the store.
       
   410 */
       
   411 void CApaIconCaptionOverrides::LoadFromCenRepL(CRepository* aRepository)
       
   412 	{
       
   413 	DeleteOverrides();
       
   414 
       
   415 	RArray<TUint32> appKeys;
       
   416 	CleanupClosePushL(appKeys);
       
   417 	aRepository->FindL(KUidKey, ~KAppBits, appKeys);	// ignore return. if no overrides found, array will be empty
       
   418 	TInt count = appKeys.Count();
       
   419 	for (TInt ii=0; ii<count; ii++)
       
   420 		{
       
   421 		TUint32 key = appKeys[ii];
       
   422 		TInt appUidVal;
       
   423 		User::LeaveIfError(aRepository->Get(key, appUidVal));
       
   424 		TUid appUid = { appUidVal };
       
   425 		CApaIconCaptionOverridesForApp* app = CApaIconCaptionOverridesForApp::NewL();
       
   426 		CleanupStack::PushL(app);
       
   427 		app->LoadFromCenRepL(aRepository, key);
       
   428 		SetOverrideForAppL(appUid, app);
       
   429 		CleanupStack::Pop(app);
       
   430 		}
       
   431 	CleanupStack::PopAndDestroy(&appKeys);
       
   432 	}
       
   433 
       
   434 /**
       
   435 Searches Hash table that keeps a map of app UIDs to configuration information for supplied application
       
   436 and returns a pointer to an object that keeps information of icon caption overrides for an application, if
       
   437 any suitable entry is found; otherwise NULL.
       
   438 */
       
   439 CApaIconCaptionOverridesForApp* CApaIconCaptionOverrides::OverrideForApp(TUid aAppUid) const
       
   440 	{
       
   441 	CApaIconCaptionOverridesForApp* const* override = iMap.Find(aAppUid.iUid);
       
   442 	if (override)
       
   443 		return *override;
       
   444 	else
       
   445 		return NULL;
       
   446 	}
       
   447 
       
   448 /**
       
   449 Inserts key-value pair into an array (Hash table), if found, updates it.
       
   450 */
       
   451 void CApaIconCaptionOverrides::SetOverrideForAppL(TUid aAppUid, CApaIconCaptionOverridesForApp* aOverrides)
       
   452 	{
       
   453 	//Searches the Hash table with the app UID, if it's found, it returns the corresponding entry;
       
   454 	CApaIconCaptionOverridesForApp* prev = OverrideForApp(aAppUid);
       
   455 	if (aOverrides)
       
   456 		{
       
   457 		iMap.InsertL(aAppUid.iUid, aOverrides);
       
   458 		}
       
   459 	else
       
   460 		{
       
   461 		iMap.Remove(aAppUid.iUid);
       
   462 		}
       
   463 	delete prev; // delete the found entry as InsertL() replaces it.
       
   464 	}
       
   465 
       
   466 /**
       
   467 Removes all the mapping done with app UIDs in Hash table for the icon caption overrides.
       
   468 */
       
   469 void CApaIconCaptionOverrides::DeleteOverrides()
       
   470 	{
       
   471 	RAppOverrideMap::TIter pOverride(iMap);
       
   472 	for (CApaIconCaptionOverridesForApp* const* override = pOverride.NextValue();
       
   473 		 override;
       
   474 		 override = pOverride.NextValue())
       
   475 		delete *override;
       
   476 	iMap.Close();
       
   477 	}
       
   478 
       
   479 CApaIconCaptionCenrepObserver::CApaIconCaptionCenrepObserver(CApaIconCaptionOverrides& aOverrides)
       
   480 : CActive(EPriorityStandard), iOverrides(aOverrides)
       
   481 	{
       
   482 	CActiveScheduler::Add(this);
       
   483 	}
       
   484 	
       
   485 // Creates a Central Repository observer
       
   486 CApaIconCaptionCenrepObserver* CApaIconCaptionCenrepObserver::NewL(CApaIconCaptionOverrides& aOverrides)
       
   487 	{
       
   488 	CApaIconCaptionCenrepObserver* self = new (ELeave) CApaIconCaptionCenrepObserver(aOverrides);
       
   489 	CleanupStack::PushL(self);
       
   490 	self->ConstructL();
       
   491 	CleanupStack::Pop();
       
   492 	return self;
       
   493 	}
       
   494 
       
   495 // Destructor
       
   496 CApaIconCaptionCenrepObserver::~CApaIconCaptionCenrepObserver()
       
   497 	{
       
   498 	Cancel();
       
   499 	delete iRepository;
       
   500 	}
       
   501 
       
   502 /** 2nd phase constructor that creates a Central Repository object to access the repository with the
       
   503 specified UID and starts notification.
       
   504 */
       
   505 void CApaIconCaptionCenrepObserver::ConstructL()
       
   506 	{
       
   507 	TRAP_IGNORE(iRepository = CRepository::NewL(KUidIconCaptionRepository));
       
   508 	Start();
       
   509 	}
       
   510 
       
   511 /**
       
   512 Finds all the settings that exist and match the specification given by partial and mask keys, from the
       
   513 Central Repository, if it exists.
       
   514 */
       
   515 void CApaIconCaptionCenrepObserver::LoadOverridesL()
       
   516 	{
       
   517 	if (iRepository)
       
   518 		{
       
   519 		iOverrides.LoadFromCenRepL(iRepository);
       
   520 		}
       
   521 	}
       
   522 
       
   523 /**
       
   524 Requests a notification on any change in the Central Repository. Only one notification can be
       
   525 received per call to NotifyRequest().
       
   526 */
       
   527 void CApaIconCaptionCenrepObserver::Start()
       
   528 	{
       
   529 	if (iRepository)
       
   530 		{
       
   531 		iRepository->NotifyRequest(0, 0, iStatus);	// notify on all key changes
       
   532 		SetActive();
       
   533 		}
       
   534 	}
       
   535 
       
   536 /**
       
   537 Handles an active object's request completion event.
       
   538 */
       
   539 void CApaIconCaptionCenrepObserver::RunL()
       
   540 	{
       
   541 		Start();
       
   542 		LoadOverridesL();
       
   543 	}
       
   544 
       
   545 /**
       
   546 Cancels a notification previously requested from NotifyRequest
       
   547 */
       
   548 void CApaIconCaptionCenrepObserver::DoCancel()
       
   549 	{
       
   550 	iRepository->NotifyCancel(0, 0);
       
   551 	}