syncmlfw/ds/settings/src/NSmlDSSettings.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  DS-settings 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <NSmlDSProfileRes.rsg>
       
    20 
       
    21 #include <nsmlconstants.h>
       
    22 #include <nsmldsconstants.h>
       
    23 #include "nsmldssettings.h"
       
    24 #include "NSmlDSResourceProfile.h"
       
    25 #include "barsc.h"
       
    26 #include "bautils.h"
       
    27 #include "nsmlroam.h"
       
    28 //XML profiles
       
    29 #include "RXMLReader.h"
       
    30 #include "NSmlProfileContentHandler.h"
       
    31 #include <centralrepository.h> //CRepository
       
    32 
       
    33 _LIT(Kinfile,"z:\\Private\\101F99FB\\VariantData.xml");
       
    34 
       
    35 
       
    36 //=============================================
       
    37 //
       
    38 //		CNSmlDSSettings
       
    39 //
       
    40 //=============================================
       
    41 
       
    42 //=============================================
       
    43 //		CNSmlDSSettings::NewL()
       
    44 //		Creates a new instance of CNSmlDSSettings object.
       
    45 //=============================================
       
    46 
       
    47 EXPORT_C CNSmlDSSettings* CNSmlDSSettings::NewL()
       
    48 	{
       
    49 	CNSmlDSSettings* self = CNSmlDSSettings::NewLC();
       
    50 	CleanupStack::Pop();
       
    51 	return self;
       
    52 	}	
       
    53 
       
    54 //=============================================
       
    55 //		CNSmlDSSettings::NewLC()
       
    56 //		Creates a new instance of CNSmlDSSettings object. 
       
    57 //		Pushes and leaves new instance into CleanupStack.
       
    58 //		Opens/creates settings database.
       
    59 //=============================================
       
    60 
       
    61 EXPORT_C CNSmlDSSettings* CNSmlDSSettings::NewLC()
       
    62 	{
       
    63 
       
    64 	CNSmlDSSettings* self = new( ELeave ) CNSmlDSSettings;
       
    65 	CleanupStack::PushL( self );
       
    66 	self->ConstructL();
       
    67 
       
    68 	User::LeaveIfError( self->iFsSession.Connect() );
       
    69 	User::LeaveIfError( self->iRdbSession.Connect() );	
       
    70 	
       
    71    TParse name;
       
    72     
       
    73 #ifdef SYMBIAN_SECURE_DBMS
       
    74 	name.Set( KNSmlSettingsDbName(), NULL, NULL );
       
    75 #else
       
    76     name.Set( KNSmlSettingsDbName(), KNSmlDatabasesNonSecurePath, NULL );
       
    77 #endif
       
    78 
       
    79 	TInt err = self->iDatabase.Open( self->iRdbSession,
       
    80 	                                 name.FullName(),
       
    81 	                                 KNSmlDBMSSecureSOSServerID );
       
    82 	
       
    83 	if ( err == KErrNotFound )
       
    84 		{
       
    85 		self->CreateDatabaseL( name.FullName() );
       
    86 		self->iDatabase.Open( self->iRdbSession,
       
    87 		                      name.FullName(),
       
    88 		                      KNSmlDBMSSecureSOSServerID );
       
    89 		User::LeaveIfError( self->iTableProfiles.Open( self->iDatabase, KNSmlTableProfiles ) );
       
    90 		self->iColSet = self->iTableProfiles.ColSetL();
       
    91 		}
       
    92 	else
       
    93 		{
       
    94 		if ( (err == KErrEof) || (err == KErrCorrupt) ||
       
    95 		     (err == KErrArgument) || (err == KErrNotSupported) )
       
    96 			{
       
    97 			self->iRdbSession.DeleteDatabase( name.FullName(),
       
    98 			                                  KNSmlSOSServerPolicyUID );
       
    99 			}
       
   100 		User::LeaveIfError( err );
       
   101 	User::LeaveIfError( self->iTableProfiles.Open( self->iDatabase, KNSmlTableProfiles ) );
       
   102 	self->iColSet = self->iTableProfiles.ColSetL();
       
   103 		}
       
   104 	return self;
       
   105 	}
       
   106 
       
   107 //=============================================
       
   108 //		CNSmlDSSettings::ConstructL()
       
   109 //	    2nd phase constructor
       
   110 //=============================================
       
   111 void CNSmlDSSettings::ConstructL()
       
   112 	{
       
   113 	iResourceProfileArray = new (ELeave) CArrayPtrFlat<CNSmlDSProfile> (5);
       
   114 	}
       
   115 
       
   116 
       
   117 //=============================================
       
   118 //		CNSmlDSSettings::~CNSmlDSSettings()
       
   119 //		Destructor.
       
   120 //=============================================
       
   121 
       
   122 CNSmlDSSettings::~CNSmlDSSettings()
       
   123 	{
       
   124 	if(iResourceProfileArray)
       
   125 		{
       
   126 		iResourceProfileArray->ResetAndDestroy();
       
   127 		}
       
   128 	delete iResourceProfileArray;
       
   129 	
       
   130 	iView.Close();
       
   131 	delete iColSet;
       
   132 	iTableProfiles.Close();
       
   133 	iDatabase.Close();
       
   134 	iFsSession.Close();
       
   135 	iRdbSession.Close();
       
   136 	}
       
   137 
       
   138 //=============================================
       
   139 //		CNSmlDSSettings::CreateDatabaseL()
       
   140 //		Creates settings database.
       
   141 //=============================================
       
   142 
       
   143 void CNSmlDSSettings::CreateDatabaseL(const TDesC& aFullName)
       
   144 	{
       
   145 	// 50 is the extra length neede for integer lengths
       
   146 	HBufC* createProfileTable = HBufC::NewLC( KDSCreateProfilesTable().Length() + 50);
       
   147 	TPtr profileTablePtr = createProfileTable->Des();
       
   148 
       
   149 	profileTablePtr.Format(KDSCreateProfilesTable,KNSmlMaxProfileNameLength,KNSmlMaxUsernameLength,KNSmlMaxPasswordLength,KNSmlMaxURLLength,KNSmlMaxServerIdLength,KNSmlMaxHttpAuthUsernameLength,KNSmlMaxHttpAuthPasswordLength, KNSmlDSVisibilityArraySize );
       
   150 
       
   151 	// 25 is the extra length neede for integer lengths
       
   152 	HBufC* createAdaptersTable = HBufC::NewLC( KDSCreateAdaptersTable().Length() + 25);
       
   153 	TPtr adaptersTablePtr = createAdaptersTable->Des();
       
   154 	adaptersTablePtr.Format(KDSCreateAdaptersTable,KNSmlMaxAdapterDisplayNameLength,KNSmlMaxRemoteNameLength, KNSmlMaxLocalNameLength );
       
   155 
       
   156     User::LeaveIfError( iDatabase.Create( this->iRdbSession, aFullName, KNSmlDBMSSecureSOSServerID ) );  
       
   157 	
       
   158 	iDatabase.Begin();
       
   159 
       
   160 	iDatabase.Execute( *createProfileTable );
       
   161 	iDatabase.Execute( *createAdaptersTable );
       
   162 
       
   163 	CDbColSet* colSet = CDbColSet::NewLC();
       
   164 	colSet->AddL(TDbCol(KNSmlVersionColumnMajor(), EDbColUint16));
       
   165 	colSet->AddL(TDbCol(KNSmlVersionColumnMinor(), EDbColUint16));
       
   166 	User::LeaveIfError(iDatabase.CreateTable(KNSmlTableVersion(), *colSet));
       
   167 	CleanupStack::PopAndDestroy(  ); //colset
       
   168 
       
   169 	RDbTable table;
       
   170 	User::LeaveIfError(table.Open(iDatabase, KNSmlTableVersion()));
       
   171 	CleanupClosePushL(table);
       
   172 	colSet = table.ColSetL();
       
   173 	CleanupStack::PushL(colSet);
       
   174 	table.InsertL();
       
   175 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMajor), KNSmlSettingsCurrentVersionMajor);
       
   176 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMinor), KNSmlSettingsCurrentVersionMinor);
       
   177 	table.PutL();
       
   178 	
       
   179 	iDatabase.Commit();
       
   180 	
       
   181 	CreateHiddenProfilesL();
       
   182 
       
   183 	TInt keyVal;
       
   184 	TRAPD (err ,ReadRepositoryL(KNsmlDsCustomProfiles, keyVal));
       
   185 	if (err == KErrNone && keyVal)
       
   186 	{
       
   187 		TBool aRestore = EFalse;
       
   188 		CreateXMLProfilesL(aRestore);
       
   189 	}
       
   190 	iDatabase.Close();
       
   191 	CleanupStack::PopAndDestroy( 4 ); // createAdaptersTable, createProfileTable, colSet, table
       
   192 	}
       
   193 	
       
   194 //=============================================
       
   195 //		CNSmlDSSettings::CreateHiddenProfilesL()
       
   196 //		Create new profiles reading data from
       
   197 //		resource file
       
   198 //=============================================
       
   199 
       
   200 void CNSmlDSSettings::CreateHiddenProfilesL()
       
   201 	{
       
   202 	TFileName fileName;
       
   203 	Dll::FileName( fileName );
       
   204 	TParse parse;
       
   205 
       
   206 	parse.Set( KNSmlDSProfilesRsc, &fileName, NULL );
       
   207 	fileName = parse.FullName();
       
   208 
       
   209 	RResourceFile resourceFile; 
       
   210 	BaflUtils::NearestLanguageFile( iFsSession, fileName );
       
   211 
       
   212 	TRAPD(leavecode,resourceFile.OpenL( iFsSession,fileName));
       
   213 	if(leavecode != 0)
       
   214 		{
       
   215 		return;
       
   216 		}
       
   217 	CleanupClosePushL(resourceFile);
       
   218 	
       
   219 	HBufC8* profileRes = resourceFile.AllocReadLC( NSML_DS_PROFILES );
       
   220 	TResourceReader reader;
       
   221 	reader.SetBuffer( profileRes );
       
   222 
       
   223 	CNSmlDSResourceProfiles* profileResReader = CNSmlDSResourceProfiles::NewLC( reader, this );
       
   224 	profileResReader->SaveProfilesL(iResourceProfileArray);
       
   225 	CleanupStack::PopAndDestroy(3); // profileResReader, profileRes, resourceFile
       
   226 	}
       
   227 	
       
   228 	
       
   229 //===============================================
       
   230 //		CNSmlDSSettings::CreateXMLProfiles()
       
   231 //		
       
   232 //		
       
   233 //===============================================	
       
   234 void CNSmlDSSettings::CreateXMLProfilesL(TBool aRestore)
       
   235 	{
       
   236 	
       
   237 	TBool status = TRUE;
       
   238 	TInt error = KErrNone;
       
   239 		
       
   240 	//file server
       
   241 	RFs wSession;
       
   242 	error = wSession.Connect();
       
   243 	if (error != KErrNone)
       
   244 	{
       
   245 	return;
       
   246 	}
       
   247 	
       
   248 	RXMLReader DSProfileParser;
       
   249 	DSProfileParser.CreateL();
       
   250 	CArrayPtrFlat<CNSmlDSProfile>* customProfileArray = new (ELeave) CArrayPtrFlat<CNSmlDSProfile> (5);
       
   251 	CleanupStack::PushL(customProfileArray);
       
   252 	
       
   253 	CNSmlProfileContentHandler* cb = CNSmlProfileContentHandler::NewL(this ,customProfileArray);
       
   254 	CleanupStack::PushL(cb);
       
   255 
       
   256 	DSProfileParser.SetContentHandler(cb);
       
   257 
       
   258 	DSProfileParser.SetFeature(EXMLValidation, ETrue);
       
   259 	DSProfileParser.SetFeature(EXMLValidation, EFalse);
       
   260 	DSProfileParser.GetFeature(EXMLBinary, status);
       
   261 	DSProfileParser.GetFeature(EXMLValidation, status);
       
   262 
       
   263 	status = TRUE;
       
   264 
       
   265 	RFile wFile;
       
   266 	TInt err =wFile.Open(wSession, Kinfile, EFileRead | EFileShareReadersOnly); 
       
   267 	if (err != KErrNone)
       
   268 	{
       
   269 		CleanupStack::PopAndDestroy(2);	
       
   270 		return;	
       
   271 	}
       
   272 	
       
   273 	CleanupClosePushL(wFile);
       
   274 	//parse file
       
   275 	TRAP(error, DSProfileParser.ParseL(wFile));
       
   276 	if (error != KErrNone)
       
   277 	{
       
   278 	CleanupStack::PopAndDestroy(3);	
       
   279 	return;
       
   280 	}
       
   281 	TInt index;
       
   282 	TBuf<150> buf;
       
   283     
       
   284     if(aRestore)
       
   285     {
       
   286     //handling back up-restore for custom profiles
       
   287     CNSmlDSProfileList* profList = new (ELeave) CArrayPtrFlat<CNSmlDSProfileListItem>(1);
       
   288 	CleanupStack::PushL(profList);
       
   289     GetAllProfileListL( profList );
       
   290     for( TInt i = 0 ; i < profList->Count() ; i++ )
       
   291      {
       
   292      	TInt id = profList->At(i)->IntValue( EDSProfileId );
       
   293      	CNSmlDSProfile* prof = ProfileL(id);
       
   294      	CleanupStack::PushL( prof );
       
   295      	buf = prof->StrValue(EDSProfileServerId);
       
   296      	
       
   297      	if (buf.Compare(KEmpty) != 0)
       
   298      	{
       
   299      		for (index = 0; index < customProfileArray->Count(); index++ )
       
   300      		{
       
   301      		if (buf.Compare(customProfileArray->At(index)->StrValue(EDSProfileServerId)) == 0)
       
   302      			{
       
   303      			DeleteProfileL(id);
       
   304      			}
       
   305      		}	
       
   306      	}
       
   307      	
       
   308      	else
       
   309      	{
       
   310      		buf = prof->StrValue(EDSProfileServerURL);
       
   311      		for (index = 0; index < customProfileArray->Count(); index++ )
       
   312      		{
       
   313      		if (buf.Compare(customProfileArray->At(index)->StrValue(EDSProfileServerURL)) == 0)
       
   314      			{
       
   315      			DeleteProfileL(id);
       
   316      			}
       
   317      		}	
       
   318      	}
       
   319        	CleanupStack::PopAndDestroy(); // prof
       
   320      }
       
   321     
       
   322     profList->ResetAndDestroy();
       
   323     CleanupStack::PopAndDestroy();
       
   324     }
       
   325     
       
   326     //save profiles
       
   327     TBool defaultprofilefound = EFalse;
       
   328     //Set the ProfileID to default
       
   329     WriteRepositoryL( KCRUidDSDefaultProfileInternalKeys, KNsmlDsDefaultProfile, -1 );
       
   330 	for ( index = 0; index < customProfileArray->Count(); index++ )
       
   331 		{
       
   332 		if (CheckXMLProfileSettings(customProfileArray ,index))
       
   333 			{
       
   334 			customProfileArray->At(index)->SaveL();
       
   335 			if(!defaultprofilefound && customProfileArray->At(index)->IntValue(EDSProfileDefaultProfile))
       
   336 			    {
       
   337 			    //Set the ProfileID to be used as a Default Profile
       
   338 			    WriteRepositoryL( KCRUidDSDefaultProfileInternalKeys, KNsmlDsDefaultProfile, 
       
   339 			                      customProfileArray->At(index)->IntValue(EDSProfileId) );
       
   340 			    defaultprofilefound = ETrue;
       
   341 			    }		
       
   342 			}
       
   343 		}
       
   344 		
       
   345 	CleanupStack::PopAndDestroy(); // wFile
       
   346 	CleanupStack::PopAndDestroy(); // cb
       
   347 	
       
   348 	customProfileArray->ResetAndDestroy();
       
   349 	CleanupStack::PopAndDestroy(); //customProfileArray
       
   350 	
       
   351 	
       
   352 	}
       
   353 
       
   354 //===============================================
       
   355 //		CNSmlDSSettings::CheckXMLProfileSettings()
       
   356 //		
       
   357 //		
       
   358 //===============================================	
       
   359 TBool CNSmlDSSettings::CheckXMLProfileSettings(CNSmlProfileArray* aProfileArray,TInt aIndex)
       
   360 	{
       
   361 	
       
   362 	const TInt KMaxXMLProfileCount = 5;
       
   363 	TBuf<160> buf;
       
   364 	TInt count = aProfileArray->Count();
       
   365 	//max count
       
   366 	if (count > KMaxXMLProfileCount)
       
   367 		{
       
   368 		return EFalse;
       
   369 		}
       
   370 	
       
   371 	//mandatory settings
       
   372 	if (aProfileArray->At(aIndex)->StrValue(EDSProfileDisplayName).Compare(KEmpty) == 0
       
   373 		||aProfileArray->At(aIndex)->StrValue(EDSProfileServerURL).Compare(KEmpty) == 0
       
   374 		)
       
   375 		{
       
   376 		return EFalse;
       
   377 		}
       
   378 	
       
   379 	//Unique Server Id	
       
   380 	buf = aProfileArray->At(aIndex)->StrValue(EDSProfileServerId);
       
   381 	if (buf.Compare(KEmpty) != 0)
       
   382 		{
       
   383 	   	for(TInt i = 0 ; i < iResourceProfileArray->Count() ; i++ )
       
   384 	   	    {
       
   385 	   		if (iResourceProfileArray->At(i)->StrValue(EDSProfileServerId)
       
   386      						.Compare(buf) == 0)
       
   387      			{
       
   388      	 		return EFalse;	
       
   389      			}
       
   390 	   	    }
       
   391      	}
       
   392     
       
   393     if(buf.Compare(KEmpty) != 0)
       
   394 		{
       
   395 		for (TInt i = 0; i < count ;i++)
       
   396 			{
       
   397 			if (buf.Compare(aProfileArray->At(i)->StrValue(EDSProfileServerId)) == 0
       
   398 					&& i < aIndex)
       
   399 					{
       
   400 					return EFalse;
       
   401 					}
       
   402 			}
       
   403 		}
       
   404     
       
   405 	return ETrue; 
       
   406 	}
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CNSmlDSSettings::ReadRepositoryL
       
   410 //
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CNSmlDSSettings::ReadRepositoryL(TInt aKey, TInt& aValue)
       
   414 	{
       
   415 	const TUid KRepositoryId = KCRUidDataSyncInternalKeys;
       
   416 	
       
   417     CRepository* rep = CRepository::NewLC(KRepositoryId);
       
   418     TInt err = rep->Get(aKey, aValue);
       
   419 	User::LeaveIfError(err);
       
   420 	CleanupStack::PopAndDestroy(rep);
       
   421     
       
   422 	}	
       
   423 // -----------------------------------------------------------------------------
       
   424 // CNSmlDSSettings::WriteRepositoryL
       
   425 //
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 void CNSmlDSSettings::WriteRepositoryL(TUid aUid, TInt aKey, TInt aValue)
       
   429     {
       
   430     const TUid KRepositoryId = aUid;
       
   431     
       
   432     CRepository* rep = CRepository::NewLC(KRepositoryId);
       
   433     TInt err = rep->Set(aKey, aValue);
       
   434     User::LeaveIfError(err);
       
   435     CleanupStack::PopAndDestroy(rep);
       
   436     
       
   437     }
       
   438 //===============================================
       
   439 //		CNSmlDSSettings::UpdateHiddenProfilesL()
       
   440 //		Updates the profiles with data from 
       
   441 //		resource file
       
   442 //===============================================
       
   443 EXPORT_C void CNSmlDSSettings::UpdateHiddenProfilesL()
       
   444 {
       
   445 	 
       
   446 	CNSmlDSProfileList* profList = new (ELeave) CArrayPtrFlat<CNSmlDSProfileListItem>(1);
       
   447 	CleanupStack::PushL(profList);
       
   448     GetAllProfileListL( profList );
       
   449     for( TInt i = 0 ; i < profList->Count() ; i++ )
       
   450     {
       
   451        	TInt id = profList->At(i)->IntValue( EDSProfileId );
       
   452      	CNSmlDSProfile* prof = ProfileL(id);
       
   453      	CleanupStack::PushL( prof );
       
   454      	if(!prof->IntValue(EDSProfileDeleteAllowed))
       
   455      	{
       
   456      		
       
   457      		DeleteProfileL(id);
       
   458      	}
       
   459      	CleanupStack::PopAndDestroy(); // prof
       
   460     }
       
   461      
       
   462     profList->ResetAndDestroy();
       
   463     CleanupStack::PopAndDestroy();
       
   464      
       
   465     CreateHiddenProfilesL();
       
   466      
       
   467     TInt keyVal;
       
   468 	TRAPD (err ,ReadRepositoryL(KNsmlDsCustomProfiles, keyVal));
       
   469 	if (err == KErrNone && keyVal)
       
   470 	{
       
   471 	    TInt aRestore = ETrue;
       
   472     	CreateXMLProfilesL(aRestore);
       
   473 	}
       
   474 }
       
   475 //=============================================
       
   476 //		CNSmlDSSettings::CreateProfileL()
       
   477 //		Returns a pointer to newly created CNSmlDSProfile object.
       
   478 //=============================================
       
   479 
       
   480 EXPORT_C CNSmlDSProfile* CNSmlDSSettings::CreateProfileL()
       
   481 	{
       
   482 	CNSmlDSProfile* profile = CNSmlDSProfile::NewL( this->Database() );
       
   483 
       
   484 	profile->SetIntValue( EDSProfileId,					KNSmlNewObject );
       
   485 	profile->SetIntValue( EDSProfileTransportId,		KUidNSmlMediumTypeInternet.iUid );
       
   486 	profile->SetIntValue( EDSProfileHidden,				EFalse);
       
   487 	profile->SetIntValue( EDSProfileDeleteAllowed,		ETrue);
       
   488 
       
   489 	profile->SetIntValue( EDSProfileServerAlertedAction,ESmlConfirmSync);
       
   490 
       
   491 	profile->SetIntValue( EDSProfileIAPId,				-2 );
       
   492 
       
   493 	profile->SetIntValue( EDSProfileHttpAuthUsed,		EFalse);
       
   494 	profile->SetIntValue( EDSProfileAutoChangeIAP,		EFalse);
       
   495 	
       
   496 	profile->SetIntValue( EDSProfileProtocolVersion, 	ESmlVersion1_2 );
       
   497 
       
   498 	profile->InitVisibilityArray(KNSmlDefaultVisibility);
       
   499 
       
   500     return profile;
       
   501 	}
       
   502 
       
   503 //=============================================
       
   504 //		CNSmlDSSettings::CreateProfileL()
       
   505 //		Copies all profile data with given Id to
       
   506 //		a newly created CNSmlDSProfile object.
       
   507 //=============================================
       
   508 
       
   509 EXPORT_C CNSmlDSProfile* CNSmlDSSettings::CreateProfileL( const TInt aId  )
       
   510 	{
       
   511 	CNSmlDSProfile* profile = ProfileOnlyL( aId);
       
   512 	CleanupStack::PushL( profile );
       
   513 	if ( profile )
       
   514 		{
       
   515 		profile->SetIntValue( EDSProfileId,		KNSmlNewObject );
       
   516 
       
   517 		CNSmlDSProfile* copyProfile = ProfileL( aId);
       
   518 		CleanupStack::PushL(copyProfile);
       
   519 		if(!copyProfile)
       
   520 			{
       
   521 			profile = 0;
       
   522 			return profile;
       
   523 			}
       
   524 
       
   525 		for ( TInt i = 0; i < copyProfile->iContentTypes->Count(); i++ )
       
   526 			{
       
   527 			TInt uid = copyProfile->iContentTypes->At( i )->IntValue(EDSAdapterImplementationId);
       
   528 			TDesC clientDataSource = copyProfile->iContentTypes->At( i )->StrValue( EDSAdapterClientDataSource );
       
   529 			TDesC serverDataSource = copyProfile->iContentTypes->At( i )->StrValue( EDSAdapterServerDataSource );
       
   530 			profile->AddContentTypeL( uid, clientDataSource, serverDataSource );
       
   531 			CNSmlDSContentType* content = profile->ContentType( uid );
       
   532 			content->SetIntValue( EDSAdapterEnabled, copyProfile->iContentTypes->At( i )->IntValue(EDSAdapterEnabled) );
       
   533 			content->SetStrValue( EDSAdapterDisplayName, copyProfile->iContentTypes->At( i )->StrValue(EDSAdapterDisplayName) );
       
   534 			content->SetIntValue( EDSAdapterSyncType, copyProfile->iContentTypes->At( i )->IntValue(EDSAdapterSyncType) );
       
   535 			content->SetIntValue( EDSAdapterFilterMatchType, copyProfile->iContentTypes->At( i )->IntValue(EDSAdapterFilterMatchType) );
       
   536 			}
       
   537 
       
   538 		CleanupStack::PopAndDestroy(); // copyProfile
       
   539 		}
       
   540 	CleanupStack::Pop(); //profile
       
   541 	return profile;
       
   542 	}
       
   543 
       
   544 //=============================================
       
   545 //		CNSmlDSSettings::Database()
       
   546 //		Returns a reference to settings database object.
       
   547 //=============================================
       
   548 	
       
   549 EXPORT_C RDbNamedDatabase* CNSmlDSSettings::Database()
       
   550 	{
       
   551 	return &iDatabase;
       
   552 	}
       
   553 
       
   554 //=============================================
       
   555 //		CNSmlDSSettings::ProfileIdL()
       
   556 //		Returns Profile ID with given AdapterId.
       
   557 //		If there's no corresponding Id returns 0.
       
   558 //=============================================
       
   559 
       
   560 EXPORT_C TInt CNSmlDSSettings::ProfileIdL( TInt aId)
       
   561 	{
       
   562 	TInt profileId = 0;
       
   563 
       
   564 	HBufC* sqlStatement = HBufC::NewLC( KDSSQLGetProfileId().Length() + KNSmlDsSettingsMaxIntegerLength);
       
   565 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   566 
       
   567 	sqlStatementPtr.Format( KDSSQLGetProfileId, aId );
       
   568 
       
   569 	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   570 	
       
   571 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   572 
       
   573 	if( iView.FirstL() )
       
   574 		{
       
   575 		iView.GetL();
       
   576 
       
   577 		profileId =  ViewColUint( KNSmlDSProfileId );
       
   578 		}
       
   579 	else
       
   580 		{
       
   581 		//couldn't find
       
   582 		profileId = KErrNotFound;
       
   583 		}
       
   584 
       
   585 	return profileId;
       
   586 	}
       
   587 
       
   588 //=============================================
       
   589 //		CNSmlDSSettings::ProfileL()
       
   590 //		Returns a pointer to CNSmlDSProfile object with given Id.
       
   591 //		If there's no corresponding Id in the database, returns NULL.
       
   592 //=============================================
       
   593 
       
   594 EXPORT_C CNSmlDSProfile* CNSmlDSSettings::ProfileL( TInt aId, const TNSmlDSContentTypeGetMode aMode)
       
   595 	{
       
   596 	HBufC* sqlStatement = HBufC::NewLC( KDSSQLGetProfile().Length() + KNSmlDsSettingsMaxIntegerLength);
       
   597 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   598 
       
   599 	sqlStatementPtr.Format( KDSSQLGetProfile, aId );
       
   600 
       
   601 	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   602 	
       
   603 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   604 	
       
   605 	CNSmlDSProfile* profile = CNSmlDSProfile::NewLC( Database() );
       
   606 
       
   607 	if( iView.FirstL() )
       
   608 		{
       
   609 		CNSmlDSCrypt* crypt = new(ELeave) CNSmlDSCrypt;
       
   610 		CleanupStack::PushL(crypt);
       
   611 		iView.GetL();
       
   612 
       
   613 		profile->SetIntValue( EDSProfileId, ViewColUint( KNSmlDSProfileId ) );
       
   614 		profile->SetStrValue( EDSProfileDisplayName, ViewColDes( KNSmlDSProfileDisplayName ) );
       
   615 		profile->SetIntValue( EDSProfileIAPId, ViewColInt( KNSmlDSProfileIAPId ) );
       
   616 		profile->SetIntValue( EDSProfileTransportId, ViewColInt( ( KNSmlDSProfileTransportId ) ) );
       
   617 		profile->SetStrValue( EDSProfileServerURL, ViewColDes( KNSmlDSProfileServerURL ) );
       
   618 		profile->SetStrValue( EDSProfileServerId, ViewColDes( KNSmlDSProfileServerId ) );
       
   619 
       
   620 		profile->SetStrValue( EDSProfileSyncServerUsername, ViewColDes( KNSmlDSProfileSyncServerUsername ) );
       
   621 		profile->SetStrValue( EDSProfileSyncServerPassword,  crypt->DecryptedL(ViewColDes( KNSmlDSProfileSyncServerPassword ) ) );
       
   622 		
       
   623 		profile->SetIntValue( EDSProfileServerAlertedAction, ViewColInt( KNSmlDSProfileServerAlertedAction ) );
       
   624 		profile->SetIntValue( EDSProfileDeleteAllowed, ViewColInt( KNSmlDSProfileDeleteAllowed ) );
       
   625 		profile->SetIntValue( EDSProfileHidden, ViewColInt( KNSmlDSProfileHidden ) );
       
   626 
       
   627 		profile->SetIntValue( EDSProfileHttpAuthUsed, ViewColInt( KNSmlDSProfileHttpAuthUsed ) );
       
   628 		profile->SetStrValue( EDSProfileHttpAuthUsername, ViewColDes( KNSmlDSProfileHttpAuthUsername ) );
       
   629 		profile->SetStrValue( EDSProfileHttpAuthPassword,  crypt->DecryptedL(ViewColDes( KNSmlDSProfileHttpAuthPassword ) ) );
       
   630 		profile->SetIntValue( EDSProfileAutoChangeIAP, ViewColInt( KNSmlDSProfileAutoChangeIAP ) );
       
   631 		profile->SetIntValue( EDSProfileCreatorId, ViewColInt( KNSmlDSProfileCreatorID ) );
       
   632 		
       
   633 		profile->InitVisibilityArray( ViewColDes( KNSmlDSProfileVisibilityStr ) );
       
   634 
       
   635 		profile->SetIntValue( EDSProfileProtocolVersion, ViewColInt( KNSmlDSProfileProtocolVersion ) );
       
   636 
       
   637 		CleanupStack::PopAndDestroy(); // crypt
       
   638 		}
       
   639 	else
       
   640 		{
       
   641 		delete profile;
       
   642 		//couldn't find
       
   643 		profile = NULL;
       
   644 		}
       
   645 //
       
   646 //  Get adapters in profile
       
   647 //
       
   648 	if ( profile )
       
   649 		{
       
   650 		profile->GetContentTypesL( aMode );
       
   651 		}
       
   652 
       
   653 	CleanupStack::Pop(); // profile
       
   654 
       
   655 	return profile;
       
   656 	}
       
   657 
       
   658 //=============================================
       
   659 //		CNSmlDSSettings::ProfileOnlyL()
       
   660 //		Returns a pointer to CNSmlDSProfile object with given Id.
       
   661 //		If there's no corresponding Id in the database, returns NULL.
       
   662 //=============================================
       
   663 
       
   664 CNSmlDSProfile* CNSmlDSSettings::ProfileOnlyL( TInt aId )
       
   665 	{
       
   666 	HBufC* sqlStatement = HBufC::NewLC( KDSSQLGetProfile().Length() + KNSmlDsSettingsMaxIntegerLength);
       
   667 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   668 
       
   669 	sqlStatementPtr.Format( KDSSQLGetProfile, aId );
       
   670 
       
   671 	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   672 	
       
   673 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   674 	
       
   675 	CNSmlDSProfile* profile = CNSmlDSProfile::NewLC( Database() );
       
   676 
       
   677 	if( iView.FirstL() )
       
   678 		{
       
   679 		CNSmlDSCrypt* crypt = new(ELeave) CNSmlDSCrypt;
       
   680 		CleanupStack::PushL(crypt);
       
   681 		iView.GetL();
       
   682 
       
   683 		profile->SetIntValue( EDSProfileId, ViewColUint( KNSmlDSProfileId ) );
       
   684 		profile->SetStrValue( EDSProfileDisplayName, ViewColDes( KNSmlDSProfileDisplayName ) );
       
   685 		profile->SetIntValue( EDSProfileIAPId, ViewColInt( KNSmlDSProfileIAPId ) );
       
   686 		profile->SetIntValue( EDSProfileTransportId, ViewColInt( KNSmlDSProfileTransportId ) );
       
   687 		profile->SetStrValue( EDSProfileServerURL, ViewColDes( KNSmlDSProfileServerURL ) );
       
   688 		profile->SetStrValue( EDSProfileServerId, ViewColDes( KNSmlDSProfileServerId ) );
       
   689 
       
   690 		profile->SetStrValue( EDSProfileSyncServerUsername, ViewColDes( KNSmlDSProfileSyncServerUsername ) );
       
   691 		profile->SetStrValue( EDSProfileSyncServerPassword,  crypt->DecryptedL(ViewColDes( KNSmlDSProfileSyncServerPassword ) ) );
       
   692 		
       
   693 		profile->SetIntValue( EDSProfileServerAlertedAction, ViewColInt( KNSmlDSProfileServerAlertedAction ) );
       
   694 		profile->SetIntValue( EDSProfileDeleteAllowed,  ViewColInt( KNSmlDSProfileDeleteAllowed ) );
       
   695 		profile->SetIntValue( EDSProfileHidden,  ViewColInt( KNSmlDSProfileHidden ) );
       
   696 
       
   697 		profile->SetIntValue( EDSProfileHttpAuthUsed, ViewColInt( KNSmlDSProfileHttpAuthUsed ) );
       
   698 		profile->SetStrValue( EDSProfileHttpAuthUsername, ViewColDes( KNSmlDSProfileHttpAuthUsername ) );
       
   699 		profile->SetStrValue( EDSProfileHttpAuthPassword,  crypt->DecryptedL(ViewColDes( KNSmlDSProfileHttpAuthPassword ) ) );
       
   700 		profile->SetIntValue( EDSProfileAutoChangeIAP, ViewColInt( KNSmlDSProfileAutoChangeIAP ) );
       
   701 		profile->SetIntValue( EDSProfileCreatorId, ViewColInt( KNSmlDSProfileCreatorID ) );
       
   702 
       
   703 		profile->InitVisibilityArray( ViewColDes( KNSmlDSProfileVisibilityStr ) );
       
   704 
       
   705 		profile->SetIntValue( EDSProfileProtocolVersion, ViewColInt( KNSmlDSProfileProtocolVersion ) );
       
   706 
       
   707 		CleanupStack::PopAndDestroy(); // crypt
       
   708 		CleanupStack::Pop(); // profile
       
   709 		}
       
   710 	else
       
   711 		{
       
   712 		CleanupStack::PopAndDestroy(profile);
       
   713 		//couldn't find
       
   714 		profile = NULL;
       
   715 		}
       
   716 
       
   717 	return profile;
       
   718 	}
       
   719 //=============================================
       
   720 //		CNSmlDSSettings::GetProfileListL()
       
   721 //		Appends CNSmlDSProfileListItem objects to aProfileList.
       
   722 //=============================================
       
   723 
       
   724 EXPORT_C void CNSmlDSSettings::GetProfileListL( CNSmlDSProfileList* aProfileList )
       
   725 	{
       
   726 	aProfileList->ResetAndDestroy();
       
   727 
       
   728 	PrepareViewL( KDSSQLGetProfiles, iView.EUpdatable );
       
   729 	
       
   730 	if(iView.Unevaluated())
       
   731 		{
       
   732 		iView.EvaluateAll(); // Needed for order by
       
   733 		}	
       
   734 	
       
   735 	while ( iView.NextL() )
       
   736 		{
       
   737 		AddToProfileListL( aProfileList );
       
   738 		}
       
   739 
       
   740 	}
       
   741 
       
   742 //=============================================
       
   743 //		CNSmlDSSettings::GetaAllProfileListL()
       
   744 //		Appends CNSmlDSProfileListItem objects to aProfileList.
       
   745 //=============================================
       
   746 
       
   747 EXPORT_C void CNSmlDSSettings::GetAllProfileListL( CNSmlDSProfileList* aProfileList )
       
   748 	{
       
   749 	aProfileList->ResetAndDestroy();
       
   750 
       
   751 	PrepareViewL( KDSSQLGetAllProfiles, iView.EUpdatable );
       
   752 	
       
   753 	if(iView.Unevaluated())
       
   754 		{
       
   755 		iView.EvaluateAll(); // Needed for order by
       
   756 		}
       
   757 		
       
   758 	while ( iView.NextL() )
       
   759 		{
       
   760 		AddToProfileListL( aProfileList );
       
   761 		}
       
   762 	}
       
   763 
       
   764 //=============================================
       
   765 //		CNSmlDSSettings::DeleteProfileL()
       
   766 //		Permanently deletes profile with given Id.
       
   767 //		If delete is successful returns KErrNone,
       
   768 //		otherwise returns negative integer 
       
   769 //      (defined in EPOC error codes).
       
   770 //=============================================
       
   771 
       
   772 EXPORT_C TInt CNSmlDSSettings::DeleteProfileL( TInt aId )
       
   773 	{
       
   774 	if ( iDatabase.InTransaction() )
       
   775 		{
       
   776 		return ( KErrAccessDenied );
       
   777 		}
       
   778 
       
   779 	TBuf<64> sqlStatement;
       
   780 	
       
   781 	TInt err( KErrNone );
       
   782 
       
   783 	iDatabase.Begin();
       
   784 
       
   785 	sqlStatement.Format( KDSSQLDeleteProfile, aId );
       
   786 	err = iDatabase.Execute( sqlStatement );
       
   787 	if ( err < KErrNone )
       
   788 		{
       
   789 		iDatabase.Rollback();
       
   790 		return err;
       
   791 		}
       
   792 
       
   793 	sqlStatement.Format( KDSSQLDeleteAdapter, aId );
       
   794 	err = iDatabase.Execute( sqlStatement );
       
   795 	if ( err < KErrNone )
       
   796 		{
       
   797 		iDatabase.Rollback();
       
   798 		return err;
       
   799 		}
       
   800 
       
   801 	// Delete IAP Id's from RoamTable
       
   802 	CNSmlRoamTable* roamTable = CNSmlRoamTable::NewL( iDatabase );
       
   803 	CleanupStack::PushL( roamTable );
       
   804 	TRAPD( errRoam, roamTable->RemoveAllByIdL( aId ) );
       
   805 	if ( errRoam < KErrNone ) 
       
   806 		{
       
   807 		CleanupStack::PopAndDestroy(); // roamTable
       
   808 		iDatabase.Rollback();
       
   809 		return errRoam;
       
   810 		}
       
   811 	CleanupStack::PopAndDestroy(); // roamTable
       
   812 
       
   813 	CommitAndCompact();
       
   814 
       
   815 	return KErrNone;
       
   816 	}
       
   817 
       
   818 //=============================================
       
   819 //		CNSmlDSSettings::PrepareViewL()
       
   820 //		Closes and prepares the view
       
   821 //		
       
   822 //=============================================
       
   823 void CNSmlDSSettings::PrepareViewL( const TDesC& aSql, RDbRowSet::TAccess aAccess )
       
   824 	{
       
   825 	iView.Close();
       
   826 	User::LeaveIfError( iView.Prepare( iDatabase, TDbQuery( aSql ), aAccess ) );
       
   827 	}
       
   828 
       
   829 //=============================================
       
   830 //		CNSmlDSSettings::CommitAndCompact
       
   831 //		Commits update and compacts the database
       
   832 //		
       
   833 //=============================================
       
   834 void CNSmlDSSettings::CommitAndCompact() 
       
   835 	{
       
   836 	iDatabase.Commit();
       
   837 	iDatabase.Compact();
       
   838 	}
       
   839 
       
   840 //=============================================
       
   841 //		CNSmlDSSettings::AddToProfileListL
       
   842 //		Adds database field to profile list
       
   843 //		
       
   844 //=============================================
       
   845 void CNSmlDSSettings::AddToProfileListL( CNSmlDSProfileList* aProfileList )
       
   846 	{
       
   847 	iView.GetL();
       
   848 
       
   849 	aProfileList->AppendL( CNSmlDSProfileListItem::NewLC( iView.ColUint( iColSet->ColNo( KNSmlDSProfileId ) ) ) );
       
   850 	CleanupStack::Pop();
       
   851 	}
       
   852 
       
   853 //=============================================
       
   854 //		CNSmlDSSettings::ViewColDes
       
   855 //	
       
   856 //		
       
   857 //=============================================
       
   858 TPtrC CNSmlDSSettings::ViewColDes( const TDesC& aFieldName )
       
   859 	{
       
   860 	return iView.ColDes( iColSet->ColNo( aFieldName ) );
       
   861 	}
       
   862 
       
   863 //=============================================
       
   864 //		CNSmlDSSettings::ViewColInt
       
   865 //	
       
   866 //		
       
   867 //=============================================
       
   868 TInt CNSmlDSSettings::ViewColInt( const TDesC& aFieldName )
       
   869 	{
       
   870 	return iView.ColInt( iColSet->ColNo( aFieldName ) );
       
   871 	}
       
   872 
       
   873 //=============================================
       
   874 //		CNSmlDSSettings::ViewColUint
       
   875 //	
       
   876 //		
       
   877 //=============================================
       
   878 TUint CNSmlDSSettings::ViewColUint( const TDesC& aFieldName )
       
   879 	{
       
   880 	return iView.ColUint( iColSet->ColNo( aFieldName ) );
       
   881 	}
       
   882 
       
   883 
       
   884 //  End of File