networkprotocolmodules/suplprotocolmodule/HostSettingsApi/src/lbshostsettingsimpl.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     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 // Internal implentation of the LBS Host Settings Interface
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalAll
       
    21  @deprecated 
       
    22 */
       
    23 
       
    24 #include <e32base.h>
       
    25 #include <centralrepository.h>
       
    26 #include <e32debug.h>
       
    27 
       
    28 #include <lbs/lbshostsettingsclasstypes.h>
       
    29 #include <lbs/lbshostsettings.h>
       
    30 #include "lbshostsettingsconsts.h"
       
    31 #include "lbshostsettingsimpl.h"
       
    32 #include "lbshostsettingsnotifier.h"
       
    33 
       
    34 const TInt KFormattedDateSize = 22;
       
    35 _LIT(KDateFormat,"%3%2%1%:1%H%T%S%.%C");
       
    36 
       
    37 /**
       
    38 Provides a seamless interface to the Central Repository.
       
    39 
       
    40 The uniqueness of each key will be achieved by storing a key in the 
       
    41 table, which in effect is a counter that represents the next key to
       
    42 be used and that is incremented each time a group of settings is added
       
    43 in the table. 
       
    44 
       
    45 The group of settings associated with a SUPL account will be stored separately
       
    46 in the Central Repository instead of storing them as one record. The last 24 bits 
       
    47 of the 32-bit key will be used to represent a general key and we will vary the first 
       
    48 8 bits to represent each particular setting. They could then be read back by masking 
       
    49 those bits. The top 8 bits are defined in TLbsHostSettingRecordType.   
       
    50 
       
    51 For example:
       
    52 
       
    53 If the setting id is 4 then the associated record keys will be:
       
    54 CreatorId(0x1):        0x10000004
       
    55 ConnectionId(0x2):     0x20000004
       
    56 ConnectionIdType(0x3): 0x30000004
       
    57 HostAddress(0x4):      0x40000004
       
    58 etc...
       
    59 
       
    60 There is a reserved range of keys used for storing metadata values where the upper 4 bits 
       
    61 are set to record type EMetaData and the bottom 28 bits are defined by 
       
    62 TLbsHostSettingMetaRecordTypes. e.g:
       
    63 NextKey(0x1):     0x00000001
       
    64 DefaultHost(0x2): 0x00000002 
       
    65 
       
    66 This class also offers the ability to update a group of settings associated
       
    67 with an account, to set a particular account as default, to iterate over settings
       
    68 and finally to delete them.
       
    69 
       
    70 @see CRepository
       
    71 @see CLbsHostSettingsStore 
       
    72 */
       
    73 
       
    74 class CLbsHostSettingsStoreImpl : public CBase, public MLbsHostSettingsStoreImpl
       
    75 	{
       
    76 public:
       
    77 	static CLbsHostSettingsStoreImpl* NewL(TLbsHostStoreId aStoreId, MLbsHostSettingsStoreObserver* aObserver);
       
    78 	
       
    79 	TInt CreateHostSettings(const TLbsHostSettingsBase& aSettings, TLbsHostCreatorId aCreatorId, TLbsHostSettingsId& aSettingsId);
       
    80 	TInt UpdateHostSettings(TLbsHostSettingsId aSettingsId, const TLbsHostSettingsBase& aSettings);
       
    81 	TInt GetHostSettings(TLbsHostSettingsId aSettingsId, TLbsHostSettingsBase& aSettings) const;
       
    82 	TInt RewindHostSettings();
       
    83 	TInt GetNextHostSettings(TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId);
       
    84 	TInt GetNextHostSettingsByCreator(TLbsHostCreatorId aCreator, TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId);
       
    85 	TInt SetDefaultHostSettings(TLbsHostSettingsId aSettingsId);
       
    86 	TInt GetDefaultHostSettings(TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId) const;
       
    87 	TInt DeleteHostSettings(TLbsHostSettingsId aSettingsId);
       
    88 	~CLbsHostSettingsStoreImpl();
       
    89 	
       
    90 protected:
       
    91 	CLbsHostSettingsStoreImpl();
       
    92 	void ConstructL(TLbsHostStoreId aStoreId, MLbsHostSettingsStoreObserver* aObserver);
       
    93 
       
    94 private:
       
    95 	TInt GetSettingNumber(const TUint32 aKey) const;
       
    96 	TUint32 CreateKey(const TLbsHostSettingRecordType aRecordType, const TUint32 aSettingNumber) const;
       
    97 	TInt UpdateTimestamp(TLbsHostSettingsId aSettingsId) const;
       
    98 	TInt ValidateHostSettings(const TLbsHostSettingsSupl& aSettings) const;
       
    99 	
       
   100 	CRepository* 					iRepository;
       
   101 	CLbsHostSettingsNotifier*		iStoreNotifier;
       
   102 	
       
   103 	// structure to hold setting addresses and 
       
   104 	// creator ids
       
   105 	struct TIdCreatorPair
       
   106 		{
       
   107 	    TInt iSettingId;
       
   108 	    TUid iCreator;	   	
       
   109 		};
       
   110 	RArray<TIdCreatorPair> iCurrentView;	
       
   111 	TInt iIterator;
       
   112 	};
       
   113 
       
   114 //=============================================================================
       
   115 // CLbsHostSettingsStore::ConstructL()
       
   116 //=============================================================================
       
   117 
       
   118 /**
       
   119 Creates and assigns the internal resources required by the data store
       
   120 library.
       
   121 @param aStoreId Specifies which host settings store to utilise.
       
   122 @param aObserver If non NULL, the observer receives notifications about
       
   123 updates to store specified by aStoreId.
       
   124 @internalComponent
       
   125 */  
       
   126 void CLbsHostSettingsStore::ConstructL(TLbsHostStoreId aStoreId, MLbsHostSettingsStoreObserver* aObserver)
       
   127 	{
       
   128 	iImplementation = CLbsHostSettingsStoreImpl::NewL(aStoreId, aObserver);
       
   129 	iSettingsStore = iImplementation;
       
   130 	}
       
   131 
       
   132 /**
       
   133 Closes the connection to the host settings store and disposes of all open
       
   134 or used resources.
       
   135 @deprecated
       
   136 */  
       
   137 EXPORT_C CLbsHostSettingsStore::~CLbsHostSettingsStore()
       
   138 	{
       
   139 	delete iImplementation;
       
   140 	iImplementation = NULL;
       
   141 	}
       
   142 
       
   143 //=============================================================================
       
   144 // CLbsHostSettingsStoreImpl
       
   145 //=============================================================================
       
   146 
       
   147 CLbsHostSettingsStoreImpl::~CLbsHostSettingsStoreImpl()
       
   148 	{
       
   149 	iCurrentView.Close();
       
   150 	delete iStoreNotifier;
       
   151 	delete iRepository;
       
   152 	}
       
   153 
       
   154 
       
   155 /**
       
   156 Creates a new connection to the host settings stored for the specified protocol
       
   157 */  
       
   158 CLbsHostSettingsStoreImpl* CLbsHostSettingsStoreImpl::NewL(TLbsHostStoreId aStoreId, MLbsHostSettingsStoreObserver* aObserver)
       
   159 	{
       
   160 	CLbsHostSettingsStoreImpl* self = new (ELeave) CLbsHostSettingsStoreImpl();
       
   161 	CleanupStack::PushL(self);
       
   162 	self->ConstructL(aStoreId, aObserver);
       
   163 	CleanupStack::Pop(self);
       
   164 	return self;
       
   165 	}
       
   166 
       
   167 /**
       
   168 Default constructor for the settings store.
       
   169 */  
       
   170 CLbsHostSettingsStoreImpl::CLbsHostSettingsStoreImpl(): iIterator(KErrNotFound)
       
   171 	{
       
   172 	}
       
   173 
       
   174 void CLbsHostSettingsStoreImpl::ConstructL(TLbsHostStoreId aStoreId, MLbsHostSettingsStoreObserver* aObserver)
       
   175 	{
       
   176 	iRepository = CRepository::NewL(aStoreId);
       
   177 
       
   178 	// Create and start notifier if an observer exists
       
   179 	if (aObserver)
       
   180 		{
       
   181 		iStoreNotifier = CLbsHostSettingsNotifier::NewL(aStoreId, *iRepository, aObserver);
       
   182 		iStoreNotifier->RequestNotificationL();
       
   183 		}
       
   184 	}
       
   185 
       
   186 /**
       
   187 Resets the client's view of the data store.
       
   188 @see CLbsHostSettingsStore::RewindHostSettings
       
   189 */  
       
   190 TInt CLbsHostSettingsStoreImpl::RewindHostSettings()
       
   191 	{
       
   192 	// set iterator to -1
       
   193 	// clear structure
       
   194 	
       
   195 	iIterator = KErrNotFound;
       
   196 	iCurrentView.Reset();
       
   197 	
       
   198 	// rebuild our data structure
       
   199 	RArray<TUint32> foundKeys; //method is non-leaving so no need to put this on the CUS
       
   200 	
       
   201 	const TUint32 KSettingNumberMask = 0;
       
   202 	const TUint32 KPartialKey = CreateKey(ECreatorId, KSettingNumberMask); 
       
   203 	
       
   204 	// Find all the Creator id keys in the store so that we can generate a list of setting ids.
       
   205 	TRAPD(err, err = iRepository->FindL(KPartialKey, KRecordTypeMask, foundKeys));
       
   206 	if(err == KErrNone)
       
   207 		{
       
   208 		for(TInt i = 0, x = foundKeys.Count(); i < x; i++)
       
   209 			{		
       
   210 			// retrieve the CreatorId values foreach key	
       
   211 			TInt creatorId;
       
   212 			TIdCreatorPair keyPair;	
       
   213 			err = iRepository->Get(foundKeys[i], creatorId);		
       
   214 			if(err != KErrNone)
       
   215 				{
       
   216 				break;
       
   217 				}
       
   218 			
       
   219 			// now add pair to structure
       
   220 			// convert creatorValue to TUid for convenience
       
   221 			keyPair.iSettingId = GetSettingNumber(foundKeys[i]);
       
   222 			keyPair.iCreator = TUid::Uid(creatorId);
       
   223 			err = iCurrentView.Append(keyPair);
       
   224 			if(err != KErrNone)
       
   225 				{
       
   226 				break;
       
   227 				}
       
   228 			}
       
   229 		}
       
   230 	
       
   231 	foundKeys.Close();
       
   232 	return err;
       
   233 	}
       
   234 
       
   235 
       
   236 /**
       
   237 Retrieves the settings for a particular host settings ID.
       
   238 @see CLbsHostSettingsStore::GetHostSettings
       
   239 */  
       
   240 TInt CLbsHostSettingsStoreImpl::GetHostSettings(TLbsHostSettingsId aSettingsId,	TLbsHostSettingsBase& aSettings) const
       
   241 	{
       
   242 	ASSERT(aSettingsId.iUid >= 0);
       
   243 	if((aSettings.ClassType() & ELbsHostSettingsSuplClass))
       
   244 		{
       
   245 		TLbsHostSettingsSupl& tgt = (TLbsHostSettingsSupl&)aSettings;
       
   246 		
       
   247 		// Set the settings id
       
   248 		tgt.SetHostSettingsId(aSettingsId);
       
   249 		
       
   250 		// Get the creator id
       
   251 		TInt creatorId;
       
   252 		TUint32 creatorIdKey = CreateKey(ECreatorId, aSettingsId.iUid);
       
   253 		TInt err = iRepository->Get(creatorIdKey, creatorId);
       
   254 		if(err != KErrNone)
       
   255 			{
       
   256 			return err;
       
   257 			}
       
   258 		tgt.SetCreatorId(TUid::Uid(creatorId));
       
   259 		
       
   260 		// Get the connection id
       
   261 		TInt connectionId;
       
   262 		TUint32 connectionIdKey = CreateKey(EConnectionId, aSettingsId.iUid);
       
   263 		err = iRepository->Get(connectionIdKey, connectionId);
       
   264 		if(err != KErrNone)
       
   265 			{
       
   266 			return err;
       
   267 			}
       
   268 		
       
   269 		// Get the connection id type
       
   270 		TInt connectionIdType;
       
   271 		TUint32 connectionIdTypeKey = CreateKey(EConnectionIdType, aSettingsId.iUid);
       
   272 		err = iRepository->Get(connectionIdTypeKey, connectionIdType);
       
   273 		if(err != KErrNone)
       
   274 			{
       
   275 			return err;
       
   276 			}
       
   277 		tgt.SetConnectionPoint(connectionId, static_cast<TLbsHostSettingsSupl::TLbsConnectionType>(connectionIdType));
       
   278 		
       
   279 		// Get the host address
       
   280 		TLbsHostNameAddress hostAddr;
       
   281 		TUint32 hostAddrKey = CreateKey(EHostAddress, aSettingsId.iUid);
       
   282 		err = iRepository->Get(hostAddrKey, hostAddr);
       
   283 		if(err != KErrNone)
       
   284 			{
       
   285 			return err;
       
   286 			}
       
   287 		tgt.SetHostNameAddress(hostAddr);
       
   288 		
       
   289 		// Get the read only flag
       
   290 		TBool readOnly;
       
   291 		TUint32 readOnlyKey = CreateKey(EReadOnly, aSettingsId.iUid);
       
   292 		err = iRepository->Get(readOnlyKey, readOnly);
       
   293 		if(err != KErrNone)
       
   294 			{
       
   295 			return err;
       
   296 			}
       
   297 		tgt.SetReadOnly(readOnly);
       
   298 		
       
   299 		// Get the name
       
   300 		TLbsHostName hostName;
       
   301 		TUint32 hostNameKey = CreateKey(EHostName, aSettingsId.iUid);
       
   302 		err = iRepository->Get(hostNameKey, hostName);
       
   303 		if(err != KErrNone)
       
   304 			{
       
   305 			return err;
       
   306 			}
       
   307 		tgt.SetName(hostName);
       
   308 		
       
   309 		// Get the port id
       
   310 		TInt portId;
       
   311 		TUint32 portIdKey = CreateKey(EHostPort, aSettingsId.iUid);
       
   312 		err = iRepository->Get(portIdKey, portId);
       
   313 		if(err != KErrNone)
       
   314 			{
       
   315 			return err;
       
   316 			}
       
   317 		tgt.SetPortId(portId);
       
   318 		
       
   319 		// Get the providerId
       
   320 		TLbsProviderId providerId;
       
   321 		TUint32 providerIdKey = CreateKey(EProviderId, aSettingsId.iUid);
       
   322 		err = iRepository->Get(providerIdKey, providerId);
       
   323 		if(err != KErrNone)
       
   324 			{
       
   325 			return err;
       
   326 			}
       
   327 		tgt.SetProviderId(providerId);
       
   328 		
       
   329 		// Get the MOLR auth modes
       
   330 		TInt authmolr;
       
   331 		TUint32 authmolrKey = CreateKey(EAuthMoLr, aSettingsId.iUid);
       
   332 		err = iRepository->Get(authmolrKey, authmolr);
       
   333 		if(err != KErrNone)
       
   334 			{
       
   335 			return err;
       
   336 			}
       
   337 		tgt.SetAuthModesMOLR(authmolr);
       
   338 
       
   339 		// Get the MTLR auth modes
       
   340 		TInt authmtlr;
       
   341 		TUint32 authmtlrKey = CreateKey(EAuthMtLr, aSettingsId.iUid);
       
   342 		err = iRepository->Get(authmtlrKey, authmtlr);
       
   343 		if(err != KErrNone)
       
   344 			{
       
   345 			return err;
       
   346 			}
       
   347 		tgt.SetAuthModesMTLR(authmtlr);
       
   348 		
       
   349 		//Get the last modified timestamp
       
   350 		TBuf16<KFormattedDateSize> lastMod;
       
   351 		TUint32 lastModKey = CreateKey(ETimestamp, aSettingsId.iUid);
       
   352 		err = iRepository->Get(lastModKey, lastMod);
       
   353 		if(err != KErrNone)
       
   354 			{
       
   355 			return err;
       
   356 			}
       
   357 		TRAP(err, tgt.SetLastModifiedL(lastMod));
       
   358 		if(err != KErrNone)
       
   359 			{
       
   360 			return err;
       
   361 			}
       
   362 		
       
   363 		return KErrNone;
       
   364 		}
       
   365 	return KErrArgument;
       
   366 	}
       
   367 
       
   368 
       
   369 /**
       
   370 Retrieves the next host settings following on from the specified host
       
   371 setttings ID.
       
   372 @see CLbsHostSettingsStore::GetNextHostSettings
       
   373 */  
       
   374 TInt CLbsHostSettingsStoreImpl::GetNextHostSettings(TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId)
       
   375 	{
       
   376 	if((aSettings.ClassType() & ELbsHostSettingsSuplClass))
       
   377 		{
       
   378 		++iIterator;
       
   379 		
       
   380 		// check if we have reached end of iteration
       
   381 		// return KErrNotFound
       
   382 		// this also implies that we need to have called RewindHostSettings
       
   383 		// in order to use this method
       
   384 		if(iIterator >= iCurrentView.Count())
       
   385 			{
       
   386 			return KErrNotFound;
       
   387 			}
       
   388 				
       
   389 		TIdCreatorPair currentPair = iCurrentView[iIterator];
       
   390 		aSettingsId.iUid = currentPair.iSettingId;
       
   391 		return GetHostSettings(aSettingsId, aSettings);
       
   392 		}
       
   393 	return KErrArgument;
       
   394 	}
       
   395 
       
   396 /**
       
   397 Retrieves the next host settings that was provisioned by the specified
       
   398 creator.
       
   399 @see CLbsHostSettingsStore::GetNextHostSettingsByCreator
       
   400 */  
       
   401 TInt CLbsHostSettingsStoreImpl::GetNextHostSettingsByCreator(TLbsHostCreatorId aCreator, TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId)
       
   402 	{
       
   403 	ASSERT(aCreator.iUid >= 0);
       
   404 	if((aSettings.ClassType() & ELbsHostSettingsSuplClass))
       
   405 		{
       
   406 		TIdCreatorPair currentPair;	
       
   407 			
       
   408 		for(;;) 
       
   409 			{							
       
   410 			++iIterator;
       
   411 			
       
   412 			// check if we have reached end of iteration
       
   413 			// return KErrNotFound		
       
   414 			if(iIterator >= iCurrentView.Count())
       
   415 				{
       
   416 				return KErrNotFound;
       
   417 				}
       
   418 			
       
   419 			currentPair = iCurrentView[iIterator];
       
   420 			
       
   421 			// do we have the right creator id?
       
   422 			if(aCreator == currentPair.iCreator)
       
   423 				{
       
   424 				break;
       
   425 				}			
       
   426 			}
       
   427 									
       
   428 		// at this stage we have the right creator
       
   429 		// just return setting
       
   430 		aSettingsId.iUid = currentPair.iSettingId;
       
   431 		return GetHostSettings(aSettingsId, aSettings);
       
   432 		}
       
   433 	return KErrArgument;
       
   434 	}
       
   435 
       
   436 /**
       
   437 Replaces the settings for an existing host settings entry in the data store.
       
   438 @see CLbsHostSettingsStore::UpdateHostSettings
       
   439 */  
       
   440 TInt CLbsHostSettingsStoreImpl::UpdateHostSettings(TLbsHostSettingsId aSettingsId, const TLbsHostSettingsBase& aSettings)
       
   441 	{
       
   442 	ASSERT(aSettingsId.iUid >= 0);
       
   443 	if((aSettings.ClassType() & ELbsHostSettingsSuplClass))
       
   444 		{
       
   445 		//cast aSettings to a type we can use
       
   446 		const TLbsHostSettingsSupl& src = reinterpret_cast<const TLbsHostSettingsSupl&>(aSettings);
       
   447 		
       
   448 		TInt err = ValidateHostSettings(src);
       
   449 		if(err != KErrNone)
       
   450 			{
       
   451 			return err;
       
   452 			}
       
   453 		
       
   454 		// Check the setting exists by getting the creator Id
       
   455 		TInt creatorId;
       
   456 		TUint32 creatorIdKey = CreateKey(ECreatorId, aSettingsId.iUid);
       
   457 		err = iRepository->Get(creatorIdKey, creatorId);
       
   458 		if(err != KErrNone)
       
   459 			{
       
   460 			return err;
       
   461 			}
       
   462 		
       
   463 		//set connectionId & type
       
   464 		TInt connectionId;
       
   465 		TLbsHostSettingsSupl::TLbsConnectionType connectionIdType;
       
   466 		src.GetConnectionPoint(connectionId, connectionIdType);
       
   467 		TUint32 connectionIdKey = CreateKey(EConnectionId, aSettingsId.iUid);
       
   468 		err = iRepository->Set(connectionIdKey, connectionId);
       
   469 		if(err != KErrNone)
       
   470 			{
       
   471 			return err;
       
   472 			}
       
   473 		TUint32 connectionIdTypeKey = CreateKey(EConnectionIdType, aSettingsId.iUid);
       
   474 		err = iRepository->Set(connectionIdTypeKey, connectionIdType);
       
   475 		if(err != KErrNone)
       
   476 			{
       
   477 			return err;
       
   478 			}
       
   479 		
       
   480 		//set hostAddr
       
   481 		TLbsHostNameAddress hostAddr;
       
   482 		TInt hostAddrKey = CreateKey(EHostAddress, aSettingsId.iUid);
       
   483 		src.GetHostNameAddress(hostAddr);
       
   484 		err = iRepository->Set(hostAddrKey, hostAddr);
       
   485 		if(err != KErrNone)
       
   486 			{
       
   487 			return err;
       
   488 			}
       
   489 		
       
   490 		// set read only
       
   491 		TBool readOnly;
       
   492 		TUint32 readOnlyKey = CreateKey(EReadOnly, aSettingsId.iUid);
       
   493 		src.GetReadOnly(readOnly);
       
   494 		err = iRepository->Set(readOnlyKey, readOnly);
       
   495 		if(err != KErrNone)
       
   496 			{
       
   497 			return err;
       
   498 			}
       
   499 		
       
   500 		//set hostName
       
   501 		TLbsHostName hostName;
       
   502 		TInt hostNameKey = CreateKey(EHostName, aSettingsId.iUid);
       
   503 		src.GetName(hostName);
       
   504 		err = iRepository->Set(hostNameKey, hostName);
       
   505 		if(err != KErrNone)
       
   506 			{
       
   507 			return err;
       
   508 			}
       
   509 		
       
   510 		//set port id
       
   511 		TInt portId;
       
   512 		TUint32 portIdKey = CreateKey(EHostPort, aSettingsId.iUid);
       
   513 		src.GetPortId(portId);
       
   514 		err = iRepository->Set(portIdKey, portId);
       
   515 		if(err != KErrNone)
       
   516 			{
       
   517 			return err;
       
   518 			}
       
   519 		
       
   520 		//set providerId setting
       
   521 		TLbsProviderId providerId;
       
   522 		TInt providerIdKey = CreateKey(EProviderId, aSettingsId.iUid);
       
   523 		src.GetProviderId(providerId);
       
   524 		err = iRepository->Set(providerIdKey, providerId);
       
   525 		if(err != KErrNone)
       
   526 			{
       
   527 			return err;
       
   528 			}
       
   529 		
       
   530 		// Get the MOLR auth modes
       
   531 		TLbsHostSettingsSupl::TAuthModes authmolr;
       
   532 		TUint32 authmolrKey = CreateKey(EAuthMoLr, aSettingsId.iUid);
       
   533 		src.GetAuthModesMOLR(authmolr);
       
   534 		err = iRepository->Set(authmolrKey, (TInt)authmolr);
       
   535 		if(err != KErrNone)
       
   536 			{
       
   537 			return err;
       
   538 			}
       
   539 
       
   540 		// Get the MTLR auth modes
       
   541 		TLbsHostSettingsSupl::TAuthModes authmtlr;
       
   542 		TUint32 authmtlrKey = CreateKey(EAuthMtLr, aSettingsId.iUid);
       
   543 		src.GetAuthModesMTLR(authmtlr);
       
   544 		err = iRepository->Set(authmtlrKey, (TInt)authmtlr);
       
   545 		if(err != KErrNone)
       
   546 			{
       
   547 			return err;
       
   548 			}
       
   549 
       
   550 		// Set the last modified timestamp.
       
   551 		// Note that this should be the last setting updated since the
       
   552 		// notifier relies on listening for changes to ETimestamp.
       
   553 		return UpdateTimestamp(aSettingsId);
       
   554 		}
       
   555 	return KErrArgument;
       
   556 	}
       
   557 
       
   558 /**
       
   559 Removes the host information with the specified ID from the data store.
       
   560 @see CLbsHostSettingsStore::DeleteHostSettings
       
   561 */  
       
   562 TInt CLbsHostSettingsStoreImpl::DeleteHostSettings(TLbsHostSettingsId aSettingsId)
       
   563 	{
       
   564 	ASSERT(aSettingsId.iUid >= 0);
       
   565 	// We want to carry on with all of the deletes even if individual deletes fail in
       
   566 	// order to clean up after ourselves as best we can. We return the first error that occurs.
       
   567 	TInt returnErr = KErrNone;
       
   568 	
       
   569 	//delete creatorId
       
   570 	TUint32 creatorIdKey = CreateKey(ECreatorId, aSettingsId.iUid);
       
   571 	TInt err = iRepository->Delete(creatorIdKey);
       
   572 	returnErr = err;
       
   573 	
       
   574 	//delete connection setting 
       
   575 	//(no need to check it exists first as Delete will return KErrorNotFound)
       
   576 	TUint32 connectionIdKey = CreateKey(EConnectionId, aSettingsId.iUid);
       
   577 	err = iRepository->Delete(connectionIdKey);
       
   578 	if(returnErr == KErrNone)
       
   579 		{
       
   580 		returnErr = err;
       
   581 		}
       
   582 	TUint32 connectionIdTypeKey = CreateKey(EConnectionIdType, aSettingsId.iUid);
       
   583 	err = iRepository->Delete(connectionIdTypeKey);
       
   584 	if(returnErr == KErrNone)
       
   585 		{
       
   586 		returnErr = err;
       
   587 		}
       
   588 	
       
   589 	//delete hostAddr setting
       
   590 	TUint32 hostAddrKey = CreateKey(EHostAddress, aSettingsId.iUid);
       
   591 	err = iRepository->Delete(hostAddrKey);
       
   592 	if(returnErr == KErrNone)
       
   593 		{
       
   594 		returnErr = err;
       
   595 		}
       
   596 
       
   597 	//delete read only setting
       
   598 	TUint32 readOnlyKey = CreateKey(EReadOnly, aSettingsId.iUid);
       
   599 	err = iRepository->Delete(readOnlyKey);
       
   600 	if(returnErr == KErrNone)
       
   601 		{
       
   602 		returnErr = err;
       
   603 		}
       
   604 
       
   605 	//delete name setting
       
   606 	TUint32 hostNameKey = CreateKey(EHostName, aSettingsId.iUid);
       
   607 	err = iRepository->Delete(hostNameKey);
       
   608 	if(returnErr == KErrNone)
       
   609 		{
       
   610 		returnErr = err;
       
   611 		}
       
   612 	
       
   613 	// delete port id setting
       
   614 	TUint32 portIdKey = CreateKey(EHostPort, aSettingsId.iUid);
       
   615 	err = iRepository->Delete(portIdKey);
       
   616 	if(returnErr == KErrNone)
       
   617 		{
       
   618 		returnErr = err;
       
   619 		}
       
   620 	
       
   621 	//delete providerId setting
       
   622 	TUint32 providerIdKey = CreateKey(EProviderId, aSettingsId.iUid);
       
   623 	err = iRepository->Delete(providerIdKey);
       
   624 	if(returnErr == KErrNone)
       
   625 		{
       
   626 		returnErr = err;
       
   627 		}
       
   628 
       
   629 	//delete authmolr setting
       
   630 	TUint32 authMoLr = CreateKey(EAuthMoLr, aSettingsId.iUid);
       
   631 	err = iRepository->Delete(authMoLr);
       
   632 	if(returnErr == KErrNone)
       
   633 		{
       
   634 		returnErr = err;
       
   635 		}
       
   636 
       
   637 	//delete authmtlr setting
       
   638 	TUint32 authMtLr = CreateKey(EAuthMtLr, aSettingsId.iUid);
       
   639 	err = iRepository->Delete(authMtLr);
       
   640 	if(returnErr == KErrNone)
       
   641 		{
       
   642 		returnErr = err;
       
   643 		}
       
   644 	
       
   645 	
       
   646 	// delete the last modified timestamp
       
   647 	TUint32 lastModKey = CreateKey(ETimestamp, aSettingsId.iUid);
       
   648 	err = iRepository->Delete(lastModKey);
       
   649 	if (iStoreNotifier && err == KErrNone)
       
   650 		{
       
   651 		// Suppress notification to this client session
       
   652 		iStoreNotifier->SuppressNotification(aSettingsId);
       
   653 		}
       
   654 
       
   655 	if(returnErr == KErrNone)
       
   656 		{
       
   657 		returnErr = err;
       
   658 		}
       
   659 
       
   660 	// reset the nextID to 0 when no records in cenrep
       
   661 	RArray<TUint32> foundKeys;
       
   662 	const TUint32 KSettingNumberMask = 0;
       
   663 	const TUint32 KPartialKey = CreateKey(ECreatorId, KSettingNumberMask); 
       
   664 	TRAP(err, err = iRepository->FindL(KPartialKey, KRecordTypeMask, foundKeys));
       
   665 	if (err == KErrNotFound)
       
   666 		{
       
   667 		TInt nextIdKey = CreateKey(EMetaData, ENextKey);
       
   668 		err = iRepository->Set(nextIdKey, 0);
       
   669 		}
       
   670 	foundKeys.Close();
       
   671 	
       
   672 	if(returnErr == KErrNone)
       
   673 		{
       
   674 		returnErr = err;
       
   675 		}
       
   676 
       
   677 	//reset the default host if the default host is being deleted.
       
   678 	TInt defaultHost;
       
   679 	TInt defaultHostKey = CreateKey(EMetaData, EDefaultHost);
       
   680 	err = iRepository->Get(defaultHostKey, defaultHost);
       
   681 	if (err == KErrNone)
       
   682 		{
       
   683 		if (defaultHost == aSettingsId.iUid) 
       
   684 			{
       
   685 			err = iRepository->Set(defaultHostKey, KMaxTInt);
       
   686 			}
       
   687 		}
       
   688 	
       
   689 	if(returnErr == KErrNone)
       
   690 		{
       
   691 		returnErr = err;
       
   692 		}
       
   693 
       
   694 	return returnErr;
       
   695 	}
       
   696 
       
   697 /**
       
   698 Retrieves the settings for the host that is currently specified as the default host.
       
   699 @see CLbsHostSettingsStore::GetDefaultHostSettings
       
   700 */  
       
   701 TInt CLbsHostSettingsStoreImpl::GetDefaultHostSettings(TLbsHostSettingsBase& aSettings, TLbsHostSettingsId& aSettingsId) const
       
   702 	{
       
   703 	TInt defaultHost;
       
   704 	TInt defaultHostKey = CreateKey(EMetaData, EDefaultHost);
       
   705 	TInt err = iRepository->Get(defaultHostKey, defaultHost);
       
   706 	if(err != KErrNone)
       
   707 		{
       
   708 		return err;
       
   709 		}
       
   710 	//now we have the key for the setting, we can read it if it exists
       
   711 	aSettingsId = TUid::Uid(defaultHost);
       
   712 	return GetHostSettings(TUid::Uid(defaultHost), aSettings);
       
   713 	}
       
   714 
       
   715 /**
       
   716 Changes the default host to that specified by the supplied index.
       
   717 @internalComponent
       
   718 @see CLbsHostSettingsStore::SetDefaultHostSettings
       
   719 */  
       
   720 TInt CLbsHostSettingsStoreImpl::SetDefaultHostSettings(TLbsHostSettingsId aSettingsId)
       
   721 	{
       
   722 	ASSERT(aSettingsId.iUid >= 0);
       
   723 	TInt defaultHostKey = CreateKey(EMetaData, EDefaultHost);
       
   724 	return iRepository->Set(defaultHostKey, static_cast<TInt>(aSettingsId.iUid));
       
   725 	}
       
   726 
       
   727 
       
   728 /**
       
   729 Creates a new host settings entry in the data store.
       
   730 @see CLbsHostSettingsStore::CreateHostSettings
       
   731 */  
       
   732 TInt CLbsHostSettingsStoreImpl::CreateHostSettings(const TLbsHostSettingsBase& aSettings,	TLbsHostCreatorId  aCreator, TLbsHostSettingsId& aSettingsId)
       
   733 	{
       
   734 	ASSERT(aCreator.iUid >= 0);
       
   735 	if((aSettings.ClassType() & ELbsHostSettingsSuplClass))
       
   736 		{						
       
   737 		const TLbsHostSettingsSupl& src = reinterpret_cast<const TLbsHostSettingsSupl&>(aSettings);										
       
   738 		
       
   739 		TInt err = ValidateHostSettings(src);
       
   740 		if(err != KErrNone)
       
   741 			{
       
   742 			return err;
       
   743 			}
       
   744 		
       
   745 		//get the next row id from the cenrep
       
   746 		TInt nextId;
       
   747 		TInt nextIdKey = CreateKey(EMetaData, ENextKey);
       
   748 		err = iRepository->Get(nextIdKey, nextId);
       
   749 		if(err != KErrNone)
       
   750 			{
       
   751 			return err;
       
   752 			}
       
   753 					
       
   754 		//create keys for storing settings in the CenRep store
       
   755 		TUint32 creatorIdKey = CreateKey(ECreatorId, nextId);				
       
   756 		err = iRepository->Create(creatorIdKey, TInt(aCreator.iUid));
       
   757 		if(err != KErrNone)
       
   758 			{
       
   759 			return err;
       
   760 			}
       
   761 		
       
   762 		// Create the connection id & connection type settings
       
   763 		TInt connectionId;
       
   764 		TLbsHostSettingsSupl::TLbsConnectionType connectionIdType;
       
   765 		src.GetConnectionPoint(connectionId, connectionIdType);
       
   766 		
       
   767 		TUint32 connectionIdKey = CreateKey(EConnectionId, nextId);
       
   768 		err = iRepository->Create(connectionIdKey, connectionId);
       
   769 		if(err != KErrNone)
       
   770 			{
       
   771 			// Cleanup
       
   772 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   773 			return err;
       
   774 			}
       
   775 		TUint32 connectionIdTypeKey = CreateKey(EConnectionIdType, nextId);
       
   776 		err = iRepository->Create(connectionIdTypeKey, connectionIdType);
       
   777 		if(err != KErrNone)
       
   778 			{
       
   779 			// Cleanup
       
   780 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   781 			return err;
       
   782 			}
       
   783 		
       
   784 		// Create the host address setting
       
   785 		TLbsHostNameAddress hostAddr;			
       
   786 		src.GetHostNameAddress(hostAddr);
       
   787 		
       
   788 		TUint32 hostAddrKey = CreateKey(EHostAddress, nextId);
       
   789 		err = iRepository->Create(hostAddrKey, hostAddr);
       
   790 		if(err != KErrNone)
       
   791 			{
       
   792 			// Cleanup
       
   793 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   794 			return err;
       
   795 			}
       
   796 		
       
   797 		// Create the read only setting
       
   798 		TBool readOnly;
       
   799 		src.GetReadOnly(readOnly);
       
   800 		TUint32 readOnlyKey = CreateKey(EReadOnly, nextId);
       
   801 		err = iRepository->Create(readOnlyKey, readOnly);
       
   802 		if(err != KErrNone)
       
   803 			{
       
   804 			// Cleanup
       
   805 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   806 			return err;
       
   807 			}
       
   808 		
       
   809 		// create the name setting
       
   810 		TLbsHostName hostName;
       
   811 		src.GetName(hostName);	
       
   812 		TUint32 hostNameKey = CreateKey(EHostName, nextId);
       
   813 		err = iRepository->Create(hostNameKey, hostName);
       
   814 		if(err != KErrNone)
       
   815 			{
       
   816 			// Cleanup
       
   817 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   818 			return err;
       
   819 			}
       
   820 		
       
   821 		//set port id
       
   822 		TInt portId;
       
   823 		TUint32 portIdKey = CreateKey(EHostPort, nextId);
       
   824 		src.GetPortId(portId);
       
   825 		err = iRepository->Create(portIdKey, portId);
       
   826 		if(err != KErrNone)
       
   827 			{
       
   828 			// Cleanup
       
   829 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   830 			return err;
       
   831 			}
       
   832 		
       
   833 		// create the providerId setting
       
   834 		TLbsProviderId providerId;
       
   835 		src.GetProviderId(providerId);	
       
   836 		TUint32 providerIdKey = CreateKey(EProviderId, nextId);
       
   837 		err = iRepository->Create(providerIdKey, providerId);
       
   838 		if(err != KErrNone)
       
   839 			{
       
   840 			// Cleanup
       
   841 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   842 			return err;
       
   843 			}
       
   844 		
       
   845 		// Get the MOLR auth modes
       
   846 		TLbsHostSettingsSupl::TAuthModes authmolr;
       
   847 		TUint32 authmolrKey = CreateKey(EAuthMoLr, nextId);
       
   848 		src.GetAuthModesMOLR(authmolr);
       
   849 		err = iRepository->Create(authmolrKey, (TInt)authmolr);
       
   850 		if(err != KErrNone)
       
   851 			{
       
   852 			return err;
       
   853 			}
       
   854 
       
   855 		// Get the MTLR auth modes
       
   856 		TLbsHostSettingsSupl::TAuthModes authmtlr;
       
   857 		TUint32 authmtlrKey = CreateKey(EAuthMtLr, nextId);
       
   858 		src.GetAuthModesMTLR(authmtlr);
       
   859 		err = iRepository->Create(authmtlrKey, (TInt)authmtlr);
       
   860 		if(err != KErrNone)
       
   861 			{
       
   862 			return err;
       
   863 			}		
       
   864 				
       
   865 		// Create the last modified timestamp.
       
   866 		// Note that this should be the last setting created since the
       
   867 		// notifier relies on listening for changes to ETimestamp.
       
   868 		err = UpdateTimestamp(TLbsHostSettingsId::Uid(nextId));
       
   869 		if(err != KErrNone)
       
   870 			{
       
   871 			// Cleanup
       
   872 			DeleteHostSettings(TLbsHostSettingsId::Uid(nextId));
       
   873 			return err;
       
   874 			}
       
   875 		
       
   876 		aSettingsId.iUid = nextId;
       
   877 	
       
   878 		//increment row id & store it
       
   879 		err = iRepository->Set(nextIdKey, ++nextId);
       
   880 		return err;
       
   881 		}
       
   882 	return KErrArgument;
       
   883 	}
       
   884 
       
   885 TInt CLbsHostSettingsStoreImpl::GetSettingNumber(const TUint32 aKey) const
       
   886 	{
       
   887 	return (aKey & KSettingIdMask); 
       
   888 	}
       
   889 
       
   890 TUint32 CLbsHostSettingsStoreImpl::CreateKey(const TLbsHostSettingRecordType aRecordType, const TUint32 aSettingNumber) const
       
   891 	{
       
   892 	return ((aRecordType << KRecordTypeShift) | (aSettingNumber & KSettingIdMask));
       
   893 	}
       
   894 
       
   895 TInt CLbsHostSettingsStoreImpl::UpdateTimestamp(TLbsHostSettingsId aSettingsId) const
       
   896 	{
       
   897 	//set the timestamp
       
   898 	TTime currentTime;
       
   899 	currentTime.UniversalTime();
       
   900 	TUint32 lastModKey = CreateKey(ETimestamp, aSettingsId.iUid);
       
   901 	TBuf16<KFormattedDateSize> formattedDate;
       
   902   	TLocale locale;
       
   903   	// Make sure that the timestamp will always be recorded in YYYYMMDD format
       
   904   	locale.SetDateFormat(EDateEuropean);
       
   905   	TRAPD(err, currentTime.FormatL(formattedDate, KDateFormat, locale));
       
   906 
       
   907 	if(err != KErrNone)
       
   908 		{
       
   909 		return err;
       
   910 		}
       
   911 	err = iRepository->Set(lastModKey, formattedDate);
       
   912 	if (iStoreNotifier && err == KErrNone)
       
   913 		{
       
   914 		// Suppress notification to this client session
       
   915 		iStoreNotifier->SuppressNotification(aSettingsId);
       
   916 		}
       
   917 
       
   918 	return err;
       
   919 	}
       
   920 
       
   921 TInt CLbsHostSettingsStoreImpl::ValidateHostSettings(const TLbsHostSettingsSupl& aSettings) const
       
   922 	{
       
   923 	//Validate Connection Type
       
   924 	TInt connectionId;
       
   925 	TLbsHostSettingsSupl::TLbsConnectionType connectionIdType;
       
   926 	aSettings.GetConnectionPoint(connectionId, connectionIdType);
       
   927 	if(connectionIdType > TLbsHostSettingsSupl::ELbsConnectionTypeProxy || 
       
   928 			connectionIdType < TLbsHostSettingsSupl::ELbsConnectionTypeInvalid)
       
   929 		{
       
   930 		return KErrArgument;
       
   931 		}
       
   932 	
       
   933 	//Validate port, cannot be negative
       
   934 	TInt portId;
       
   935 	aSettings.GetPortId(portId);
       
   936 	if((portId < 0) || (portId > 0xFFFF))
       
   937 		{
       
   938 		return KErrArgument;
       
   939 		}
       
   940 	
       
   941 	TUint32 maxAuth = TLbsHostSettingsSupl::EAuthNone + 
       
   942 						TLbsHostSettingsSupl::EAuthTls + 
       
   943 						TLbsHostSettingsSupl::EAuthAcaTls + 
       
   944 						TLbsHostSettingsSupl::EAuthPskTls;
       
   945 	// Validate molr host settings cannot be larger than:
       
   946 	// EAuthNone(0x1) + EAuthTls(0x2) + EAuthAcaTls(0x4) + EAuthPskTls(0x8) = (0xF)
       
   947 	TLbsHostSettingsSupl::TAuthModes authmolr;
       
   948 	aSettings.GetAuthModesMOLR(authmolr);
       
   949 	if(authmolr > maxAuth)
       
   950 		{
       
   951 		return KErrArgument;
       
   952 		}
       
   953 	
       
   954 	// PSK/TLS is currently not supported by SUPL implementation.
       
   955 	// Return KErrNotSupported if PskTls is the only specified authmode.
       
   956 	if(authmolr == TLbsHostSettingsSupl::EAuthPskTls)
       
   957 		{
       
   958 		return KErrNotSupported;
       
   959 		}
       
   960 
       
   961 	// Validate mtlr host settings cannot be larger than:
       
   962 	// EAuthNone(0x1) + EAuthTls(0x2) + EAuthAcaTls(0x4) + EAuthPskTls(0x8) = (0xF)
       
   963 	TLbsHostSettingsSupl::TAuthModes authmtlr;
       
   964 	aSettings.GetAuthModesMTLR(authmtlr);
       
   965 	if(authmtlr > maxAuth)
       
   966 		{
       
   967 		return KErrArgument;
       
   968 		}
       
   969 	
       
   970 	// PSK/TLS is currently not supported by SUPL implementation.
       
   971 	// Return KErrNotSupported if PskTls is the only specified authmode.
       
   972 	if(authmtlr == TLbsHostSettingsSupl::EAuthPskTls)
       
   973 		{
       
   974 		return KErrNotSupported;
       
   975 		}
       
   976 		
       
   977 	// Passed validation
       
   978 	return KErrNone;
       
   979 	}
       
   980