os/persistentdata/persistentstorage/centralrepository/common/inc/operations.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#ifndef OPERATIONS_H
sl@0
    17
#define OPERATIONS_H
sl@0
    18
sl@0
    19
#include <e32debug.h>
sl@0
    20
#include <e32std.h>
sl@0
    21
#include "datatype.h"
sl@0
    22
sl@0
    23
/**
sl@0
    24
This class encapsulates the operation logic used in both the client-server and the PC side library. The MOperationLogic
sl@0
    25
defines a set of pure virtual functions to be implemented. Currently the two classes responsible for performing operations
sl@0
    26
respectively are the CServerRepository and the CPcRepImpl
sl@0
    27
@internalTechnology
sl@0
    28
*/
sl@0
    29
class MOperationLogic
sl@0
    30
	{
sl@0
    31
	public:
sl@0
    32
sl@0
    33
/** DELETE RANGE
sl@0
    34
Delete a settings from a source list. Settings are marked as deleted rather than deleted immediately
sl@0
    35
@param aSourceList the source array of settings's pointer matching the partial key and mask
sl@0
    36
@param aPartialKey the partialKey of the settings to be delted
sl@0
    37
@param aErrorKey to hold the error encountered during the Delete operation
sl@0
    38
*/
sl@0
    39
void DeleteSettingsRangeL(RSettingPointerArray& aSourceList,TUint32 aPartialKey,TUint32& aErrorKey)
sl@0
    40
	{
sl@0
    41
	if (aSourceList.Count()==0)	
sl@0
    42
		{
sl@0
    43
		aErrorKey=aPartialKey;		
sl@0
    44
		User::Leave(KErrNotFound);
sl@0
    45
		}
sl@0
    46
		
sl@0
    47
	aErrorKey = KUnspecifiedKey;
sl@0
    48
	TInt numSettings = aSourceList.Count();
sl@0
    49
	TInt error=KErrNone;		
sl@0
    50
	for (TInt i = 0; (i < numSettings) && (error == KErrNone); i++)
sl@0
    51
		{
sl@0
    52
		ASSERT(aSourceList[i]);
sl@0
    53
		TServerSetting& settingToDelete = *(aSourceList[i]);
sl@0
    54
		TUint32 key = settingToDelete.Key();
sl@0
    55
		// delete it Ensure there is a delete placeholder at the location
sl@0
    56
		if (GetWritableSettingList().Find(key) == &settingToDelete)
sl@0
    57
			{
sl@0
    58
			// we are deleting a setting that is already in the transaction list: Flag it as deleted
sl@0
    59
			settingToDelete.Reset();
sl@0
    60
			settingToDelete.SetDeleted();
sl@0
    61
			}
sl@0
    62
		else
sl@0
    63
			{
sl@0
    64
			// create a new placeholder and set as deleted
sl@0
    65
			TServerSetting newSetting(key);
sl@0
    66
			newSetting.SetMeta(settingToDelete.Meta());
sl@0
    67
			newSetting.SetDeleted();
sl@0
    68
			GetWritableSettingList().OrderedInsertL(newSetting);
sl@0
    69
			}
sl@0
    70
		}	
sl@0
    71
	}
sl@0
    72
sl@0
    73
/** DELETE
sl@0
    74
Mark a setting in the source list as deleted if found
sl@0
    75
@param aId the setting Id to be deleted
sl@0
    76
@param aSettingList the list of source setting to look for
sl@0
    77
*/
sl@0
    78
void DeleteSettingL(TUint32 aId)
sl@0
    79
	{
sl@0
    80
	TServerSetting* ts=GetWritableSettingList().Find(aId);
sl@0
    81
	if (ts)
sl@0
    82
		{
sl@0
    83
		if (ts->IsDeleted())
sl@0
    84
			User::Leave(KErrNotFound);
sl@0
    85
		else
sl@0
    86
			{
sl@0
    87
			ts->Reset();
sl@0
    88
			ts->SetDeleted();	
sl@0
    89
			}
sl@0
    90
		}
sl@0
    91
	else
sl@0
    92
		{
sl@0
    93
		TServerSetting* s=GetSetting(aId);
sl@0
    94
		if (!s)	
sl@0
    95
			User::Leave(KErrNotFound);
sl@0
    96
		else
sl@0
    97
			{
sl@0
    98
			TServerSetting newSetting(aId);
sl@0
    99
			newSetting.SetMeta(s->Meta());
sl@0
   100
			newSetting.SetDeleted();
sl@0
   101
			GetWritableSettingList().OrderedInsertL(newSetting);
sl@0
   102
			}
sl@0
   103
		}
sl@0
   104
	}
sl@0
   105
sl@0
   106
/** SET
sl@0
   107
Set a setting to a new value, create the setting if it does not exist yet
sl@0
   108
@param aKey the id of the setting
sl@0
   109
@param aVal the new value of the setting
sl@0
   110
*/
sl@0
   111
template <class T>
sl@0
   112
void SetSettingL(TUint32 aKey,const T& aVal)
sl@0
   113
	{
sl@0
   114
	TServerSetting* s = GetWritableSettingList().Find(aKey);
sl@0
   115
	if (s)
sl@0
   116
		{
sl@0
   117
		if (s->IsDeleted())
sl@0
   118
			{
sl@0
   119
	 		// replace the deleted entry with the new values
sl@0
   120
			s->CopyValueL(aVal);
sl@0
   121
			s->SetAccessPolicy(GetFallbackAccessPolicy(aKey)); 			
sl@0
   122
			}
sl@0
   123
		else
sl@0
   124
			{
sl@0
   125
			User::LeaveIfError(s->AssignValueFrom(aVal));	
sl@0
   126
			s->SetMeta(s->Meta() & (~KMetaDefaultValue));	
sl@0
   127
			}		
sl@0
   128
		}
sl@0
   129
	else	
sl@0
   130
		{
sl@0
   131
		TServerSetting* ns=GetSetting(aKey);
sl@0
   132
		TServerSetting newSetting(aKey);
sl@0
   133
		newSetting.CopyValueL(aVal);
sl@0
   134
		newSetting.SetAccessPolicy(GetFallbackAccessPolicy(aKey));
sl@0
   135
		TUint32 metadata;		
sl@0
   136
		if (!ns)
sl@0
   137
			{
sl@0
   138
			GetSingleMeta(aKey,metadata);
sl@0
   139
			}
sl@0
   140
		else
sl@0
   141
			{
sl@0
   142
			if (!ns->IsType(aVal))
sl@0
   143
				{
sl@0
   144
				User::Leave(KErrArgument);
sl@0
   145
				}			
sl@0
   146
			metadata = ~KMetaDefaultValue & ns->Meta();
sl@0
   147
			}
sl@0
   148
		newSetting.SetMeta(metadata);			
sl@0
   149
		newSetting.PushL(); // only needed for strings
sl@0
   150
		GetWritableSettingList().OrderedInsertL(newSetting);	
sl@0
   151
		newSetting.Pop();			
sl@0
   152
		}
sl@0
   153
	}
sl@0
   154
sl@0
   155
/** CREATE
sl@0
   156
Create a setting with a new value with a meta value.If meta value
sl@0
   157
not specified it will attempt to look for the meta in order of single
sl@0
   158
,range, and finally default meta
sl@0
   159
@param aKey the id of the setting
sl@0
   160
@param aVal the new value of the setting
sl@0
   161
@param aMeta the meta value of the setting
sl@0
   162
@leave KErrAlreadyExists if setting with that id already exist
sl@0
   163
*/
sl@0
   164
template <class T>
sl@0
   165
void CreateSettingL(TUint32 aKey, const T& aVal, TUint32* aMeta)
sl@0
   166
	{
sl@0
   167
	TServerSetting* s = GetSetting(aKey);
sl@0
   168
	if (s)
sl@0
   169
		{
sl@0
   170
		if (!s->IsDeleted())
sl@0
   171
			User::Leave(KErrAlreadyExists);
sl@0
   172
		else
sl@0
   173
			{
sl@0
   174
			//previously deleted settings			
sl@0
   175
			s->CopyValueL(aVal);
sl@0
   176
			s->SetAccessPolicy(GetFallbackAccessPolicy(aKey));		
sl@0
   177
			}
sl@0
   178
		}
sl@0
   179
	else
sl@0
   180
		{
sl@0
   181
		//brand new settings, create this
sl@0
   182
		TServerSetting newSetting(aKey);
sl@0
   183
		newSetting.CopyValueL(aVal);
sl@0
   184
		if (aMeta)		
sl@0
   185
			{
sl@0
   186
			newSetting.SetMeta(*aMeta);
sl@0
   187
			}
sl@0
   188
		else
sl@0
   189
			{
sl@0
   190
			TUint32 singleMeta;
sl@0
   191
			GetSingleMeta(aKey,singleMeta);
sl@0
   192
			newSetting.SetMeta(singleMeta);	
sl@0
   193
			}
sl@0
   194
		newSetting.SetAccessPolicy(GetFallbackAccessPolicy(aKey));
sl@0
   195
		newSetting.PushL(); // only needed for strings
sl@0
   196
		GetWritableSettingList().OrderedInsertL(newSetting);	
sl@0
   197
		newSetting.Pop();		
sl@0
   198
		}
sl@0
   199
	}
sl@0
   200
sl@0
   201
/** GETMETA
sl@0
   202
Retrieve the meta associated with a setting
sl@0
   203
@param aId the id of the setting
sl@0
   204
@param aMeta return value for the setting's meta
sl@0
   205
@return KErrNotFound if setting does not exist
sl@0
   206
*/
sl@0
   207
TInt GetMeta(TUint32 aId, TUint32& aMeta)
sl@0
   208
	{
sl@0
   209
	const TServerSetting* s = GetSetting(aId);
sl@0
   210
	//if is deleted or cannot be found
sl@0
   211
	if (s && s->IsDeleted() || !s)
sl@0
   212
		{
sl@0
   213
		return KErrNotFound;
sl@0
   214
		}
sl@0
   215
	aMeta = ~KMetaDefaultValue & s->Meta();
sl@0
   216
	return KErrNone;
sl@0
   217
	}
sl@0
   218
sl@0
   219
/** GET
sl@0
   220
Retrieve the value of a setting
sl@0
   221
@param aId the id of the setting
sl@0
   222
@param aVal return value for the setting's value
sl@0
   223
@return KErrNotFound if setting does not exist
sl@0
   224
		KErrArgument if specified setting type does not match	
sl@0
   225
*/
sl@0
   226
template <class T>
sl@0
   227
TInt Get(TUint32 aId, T& aVal)
sl@0
   228
	{
sl@0
   229
	const TServerSetting* s = GetSetting(aId);
sl@0
   230
	//if is deleted or cannot be found
sl@0
   231
	if (s && s->IsDeleted() || !s)
sl@0
   232
		{
sl@0
   233
		return KErrNotFound;
sl@0
   234
		}
sl@0
   235
	return s->AssignValueTo(aVal);
sl@0
   236
	}
sl@0
   237
sl@0
   238
/** FIND COMPARE
sl@0
   239
Retrieve a list of settings' id that match the value based on either the equal or not equal comparison
sl@0
   240
@param aInputArray the source array of pointer to the settings
sl@0
   241
@param aVal the value to be compared for the setting
sl@0
   242
@param aEqual the comparison rule to be applied
sl@0
   243
@param aFoundIds the passed in ID array to hold the matching settings
sl@0
   244
*/
sl@0
   245
template <class T>
sl@0
   246
void FindCompareL(const RSettingPointerArray& aInputArray,const T& aVal,TComparison aEqual,RArray<TUint32>& aFoundIds) const
sl@0
   247
	{
sl@0
   248
	aFoundIds.Reset();
sl@0
   249
	TInt numSettings=aInputArray.Count();
sl@0
   250
	for (TInt i=0;i< numSettings;i++)
sl@0
   251
		{
sl@0
   252
		ASSERT(aInputArray[i]);
sl@0
   253
		const TServerSetting& setting = *(aInputArray[i]);
sl@0
   254
		ASSERT(!setting.IsDeleted());
sl@0
   255
		TInt error=KErrNone;		
sl@0
   256
		if(aEqual && setting==aVal || !aEqual && setting!=aVal)
sl@0
   257
			{
sl@0
   258
			error = aFoundIds.Append(setting.Key());
sl@0
   259
			if (error != KErrNone)
sl@0
   260
				{
sl@0
   261
				aFoundIds.Reset();
sl@0
   262
				User::Leave(error);
sl@0
   263
				}
sl@0
   264
			}
sl@0
   265
		}
sl@0
   266
	if (aFoundIds.Count() == 0)
sl@0
   267
		{
sl@0
   268
		User::Leave(KErrNotFound);
sl@0
   269
		}
sl@0
   270
	}
sl@0
   271
		
sl@0
   272
/** FINDL
sl@0
   273
Retrieve a list of settings that match the partial id and mask
sl@0
   274
@param aSourcePartialKey the partial key to match
sl@0
   275
@param aMask the mask to be used with the partial key for matching
sl@0
   276
@param aFoundIds, the array to hold the found settings id
sl@0
   277
@param aFoundIdsMaxLimit, specify the max id to fit in the aFoundIds
sl@0
   278
@param aExcessIds, the array to hold the remaining settings id
sl@0
   279
*/		
sl@0
   280
void FindL(TUint32 aSourcePartialKey,TUint32 aMask,RArray<TUint32>& aFoundIds,TUint aFoundIdsMaxLimit,RArray<TUint32>& aExcessIds)
sl@0
   281
	{
sl@0
   282
	RSettingPointerArray settings;
sl@0
   283
	CleanupClosePushL(settings);
sl@0
   284
	TInt error = FindSettings(aSourcePartialKey,aMask, settings);
sl@0
   285
	if (error == KErrNone)
sl@0
   286
		{
sl@0
   287
		const TUint numSettings = settings.Count();
sl@0
   288
		if (numSettings==0)
sl@0
   289
			{
sl@0
   290
			User::Leave(KErrNotFound);
sl@0
   291
			}
sl@0
   292
		aFoundIds.Reset();
sl@0
   293
		
sl@0
   294
		const TUint numInitial = numSettings > aFoundIdsMaxLimit ? aFoundIdsMaxLimit : numSettings;
sl@0
   295
		const TUint numFinal = numSettings > aFoundIdsMaxLimit ? numSettings - aFoundIdsMaxLimit : 0;
sl@0
   296
		
sl@0
   297
		//reserve memory for everything that needs to be added to the array
sl@0
   298
		aFoundIds.ReserveL(numSettings);
sl@0
   299
		
sl@0
   300
		//now append up to aFoundIdsMaxLimit settings
sl@0
   301
		for(TUint i = 0; i < numInitial; i++)
sl@0
   302
			{
sl@0
   303
			ASSERT(settings[i]);
sl@0
   304
			// all settings flagged as deleted should have been removed by now, but just to be safe:
sl@0
   305
			ASSERT(!settings[i]->IsDeleted());
sl@0
   306
			aFoundIds.AppendL(settings[i]->Key());
sl@0
   307
			}
sl@0
   308
			
sl@0
   309
		//fill the aExcessIds array with any remaining settings
sl@0
   310
		if(numFinal)
sl@0
   311
			{
sl@0
   312
			aExcessIds.Reset();
sl@0
   313
			aExcessIds.ReserveL(numFinal);
sl@0
   314
			for(TUint i = numInitial; i < numSettings; i++)
sl@0
   315
				{
sl@0
   316
				ASSERT(settings[i]);
sl@0
   317
				// all settings flagged as deleted should have been removed by now, but just to be safe:
sl@0
   318
				ASSERT(!settings[i]->IsDeleted());
sl@0
   319
				aExcessIds.AppendL(settings[i]->Key());
sl@0
   320
				}
sl@0
   321
			}
sl@0
   322
		}
sl@0
   323
	CleanupStack::PopAndDestroy();
sl@0
   324
	User::LeaveIfError(error);
sl@0
   325
	}
sl@0
   326
sl@0
   327
/** MOVE
sl@0
   328
Move settings that match a given partial key to another target partial key given the mask
sl@0
   329
@param aSourcePartialKey, the source partialKey
sl@0
   330
@param aTargetPartialKey the target partialKey
sl@0
   331
@param aMask the mask to be used with the partial keys
sl@0
   332
@param aErrorKey to hold the error encountered during the move operation
sl@0
   333
@param aSourcePointerArray the array containing the source settings' pointer
sl@0
   334
*/
sl@0
   335
TInt MoveL(TUint32 aSourcePartialKey,TUint32 aTargetPartialKey,TUint32 aMask, TUint32& aErrorKey,
sl@0
   336
			const RSettingPointerArray& aSourcePointerArray)
sl@0
   337
	{
sl@0
   338
	// all write operations now done in a transaction
sl@0
   339
	TInt error = KErrNone;
sl@0
   340
	aErrorKey = KUnspecifiedKey;
sl@0
   341
	
sl@0
   342
	TUint32 maskedSourcePartialKey = aSourcePartialKey & aMask;
sl@0
   343
	TUint32 maskedTargetPartialKey = aTargetPartialKey & aMask;
sl@0
   344
	TUint32 sourceToTarget = maskedSourcePartialKey ^ maskedTargetPartialKey;
sl@0
   345
	if(!sourceToTarget)
sl@0
   346
		{
sl@0
   347
		// not moving anywhere: must return now as this trivial case fails with later logic
sl@0
   348
		return KErrNone;
sl@0
   349
		}
sl@0
   350
sl@0
   351
	//Validation of the SourcePointerArray whether any item exist
sl@0
   352
	if (aSourcePointerArray.Count() == 0)
sl@0
   353
		{
sl@0
   354
		aErrorKey=aSourcePartialKey;
sl@0
   355
		return KErrNotFound;
sl@0
   356
		}
sl@0
   357
sl@0
   358
	// create a local copy of settings as the settings pointed to by RSettingPointerArray 
sl@0
   359
	// could move and cause CServerRepository to point to the wrong key, added as fix for DEF080104
sl@0
   360
	RSettingsArray settingsCopy;
sl@0
   361
	CleanupClosePushL(settingsCopy);
sl@0
   362
	settingsCopy.CopyFromPointerArrayL(aSourcePointerArray);
sl@0
   363
			
sl@0
   364
	for (TInt i = 0; (i < settingsCopy.Count()) && (error == KErrNone); i++)
sl@0
   365
		{
sl@0
   366
		ASSERT(&settingsCopy[i]);
sl@0
   367
		TServerSetting& sourceSetting = settingsCopy[i];
sl@0
   368
		TUint32 sourceKey = sourceSetting.Key();
sl@0
   369
		TUint32 targetKey = sourceKey ^ sourceToTarget;
sl@0
   370
		
sl@0
   371
		TServerSetting* targetSetting = GetSetting(targetKey);		
sl@0
   372
		if (targetSetting)
sl@0
   373
			{
sl@0
   374
			// must be set as deleted only(for PC side this can never be reached, all setting either exist or not and if exist
sl@0
   375
			// this will return KErrAlreadyExists and this error already captured earlier
sl@0
   376
			ASSERT(targetSetting->IsDeleted());
sl@0
   377
			error = targetSetting->Replace(sourceSetting);
sl@0
   378
			if (error == KErrNone)
sl@0
   379
				{
sl@0
   380
				targetSetting->SetKey(targetKey);
sl@0
   381
				// setting takes the access policy of the target key
sl@0
   382
				targetSetting->SetAccessPolicy(GetFallbackAccessPolicy(targetKey));
sl@0
   383
				}
sl@0
   384
			}
sl@0
   385
		else
sl@0
   386
			{
sl@0
   387
			TServerSetting newSetting;
sl@0
   388
			error = newSetting.Replace(sourceSetting);
sl@0
   389
			if (error == KErrNone)
sl@0
   390
				{
sl@0
   391
				TUint32 metaFromIni;
sl@0
   392
				newSetting.SetKey(targetKey);
sl@0
   393
				GetSingleMeta(targetKey,metaFromIni);
sl@0
   394
				newSetting.SetMeta(metaFromIni);
sl@0
   395
			
sl@0
   396
				// setting takes the access policy of the target key
sl@0
   397
				newSetting.SetAccessPolicy(GetFallbackAccessPolicy(targetKey));
sl@0
   398
				newSetting.PushL(); // only needed for strings
sl@0
   399
				GetWritableSettingList().OrderedInsertL(newSetting);
sl@0
   400
				newSetting.Pop();	// only needed for strings
sl@0
   401
				}
sl@0
   402
			}
sl@0
   403
			
sl@0
   404
		// ensure there is a delete placeholder at the old location
sl@0
   405
		TServerSetting* oldSetting=GetWritableSettingList().Find(sourceKey);
sl@0
   406
		if (oldSetting)
sl@0
   407
			{
sl@0
   408
			oldSetting->Reset();
sl@0
   409
			oldSetting->SetDeleted();
sl@0
   410
			}
sl@0
   411
		else
sl@0
   412
			{
sl@0
   413
			//this part cannot happen for PC side as the search is on the persistent directly
sl@0
   414
			TServerSetting newSetting(sourceKey);
sl@0
   415
			newSetting.SetDeleted();
sl@0
   416
			GetWritableSettingList().OrderedInsertL(newSetting);			
sl@0
   417
			}
sl@0
   418
		}
sl@0
   419
	// destroy local copy of settings
sl@0
   420
	settingsCopy.Reset();
sl@0
   421
	CleanupStack::PopAndDestroy(&settingsCopy);
sl@0
   422
	return error;		
sl@0
   423
	}
sl@0
   424
sl@0
   425
sl@0
   426
	//pure virtual functions to be implemented by CServerRepository and CPcRepImpl
sl@0
   427
	
sl@0
   428
	/**
sl@0
   429
	Retrieve the meta for a setting, look for the meta in the order of individual setting meta
sl@0
   430
	range meta and then default meta.
sl@0
   431
	*/
sl@0
   432
	virtual void GetSingleMeta(TUint aKey,TUint32& aMeta)=0;
sl@0
   433
	
sl@0
   434
	/**
sl@0
   435
	Retrieve the fall back policy associated with a setting
sl@0
   436
	*/
sl@0
   437
	virtual TSettingsAccessPolicy* GetFallbackAccessPolicy(TUint32 aId) const =0;
sl@0
   438
	
sl@0
   439
	/**
sl@0
   440
	Retrieve a pointer to a setting having the key specified in aKey
sl@0
   441
	*/
sl@0
   442
	virtual TServerSetting* GetSetting(TUint aKey)=0;
sl@0
   443
	
sl@0
   444
	/**
sl@0
   445
	Retrieve the settings that match the partial key and mask and add it into the settings pointer array
sl@0
   446
	*/
sl@0
   447
	virtual TInt FindSettings(TUint32 aSourcePartialKey,TUint32 aMask,RSettingPointerArray& aOutputArray) const=0;
sl@0
   448
	
sl@0
   449
	/**
sl@0
   450
	Get the list of settings array where modification should be made
sl@0
   451
	*/
sl@0
   452
	virtual RSettingsArray& GetWritableSettingList() =0;
sl@0
   453
};
sl@0
   454
#endif // OPERATIONS_H
sl@0
   455