syncmlfw/dm/settings/src/NSmlDMSettings.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
child 44 39aa16f3fdc2
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-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:  DM-settings 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <NSmlDMProfileRes.rsg>
       
    20 #include <SyncMLDef.h>
       
    21 #include <utf.h>
       
    22 
       
    23 #include <nsmlconstants.h>
       
    24 #include <nsmldebug.h>
       
    25 #include <nsmldmconstants.h>
       
    26 #include <nsmldmtreedbclient.h>
       
    27 #include "nsmldmsettings.h"
       
    28 #include "barsc.h"
       
    29 #include "bautils.h"
       
    30 #include "NSmlDMResourceProfile.h"
       
    31 #include "DMprofileContentHandler.h"
       
    32 #include "RXMLReader.h"
       
    33 #include <centralrepository.h>
       
    34 #include "DevManInternalCRKeys.h"
       
    35 
       
    36 //------------------------------------------------------------------------------
       
    37 //
       
    38 //		CNSmlDMSettings
       
    39 //
       
    40 //------------------------------------------------------------------------------
       
    41 
       
    42 //------------------------------------------------------------------------------
       
    43 //		CNSmlDMSettings::NewL()
       
    44 //		Creates a new instance of CNSmlDMSettings object.
       
    45 //------------------------------------------------------------------------------
       
    46 
       
    47 EXPORT_C CNSmlDMSettings* CNSmlDMSettings::NewL()
       
    48 	{
       
    49 	CNSmlDMSettings* self = CNSmlDMSettings::NewLC();	
       
    50 	CleanupStack::Pop();
       
    51 	return self;
       
    52 	}	
       
    53 
       
    54 //------------------------------------------------------------------------------
       
    55 //		CNSmlDMSettings::NewLC()
       
    56 //		Creates a new instance of CNSmlDMSettings object. 
       
    57 //		Pushes and leaves new instance into CleanupStack.
       
    58 //		Opens/creates settings database.
       
    59 //------------------------------------------------------------------------------
       
    60 
       
    61 EXPORT_C CNSmlDMSettings* CNSmlDMSettings::NewLC()
       
    62 	{
       
    63 
       
    64 	CNSmlDMSettings* self = new( ELeave ) CNSmlDMSettings;
       
    65 	CleanupStack::PushL( self );
       
    66 
       
    67 	User::LeaveIfError( self->iFsSession.Connect() );
       
    68 	User::LeaveIfError( self->iRdbSession.Connect() );	
       
    69 	
       
    70     TParse name;
       
    71     
       
    72 #ifdef SYMBIAN_SECURE_DBMS
       
    73 	name.Set( KNSmlDMSettingsDbName(), NULL, NULL );
       
    74 #else
       
    75     name.Set( KNSmlDMSettingsDbName(), KNSmlDatabasesNonSecurePath, NULL );
       
    76 #endif
       
    77 
       
    78 	TInt err = self->iDatabase.Open( self->iRdbSession,
       
    79 	                                 name.FullName(),
       
    80 	                                 KNSmlDBMSSecureSOSServerID );
       
    81 	if ( err == KErrNotFound )
       
    82 		{
       
    83 		self->CreateDatabaseL( name.FullName() );
       
    84 		self->iDatabase.Open( self->iRdbSession,
       
    85 		                      name.FullName(),
       
    86 		                      KNSmlDBMSSecureSOSServerID );
       
    87 		}
       
    88 	else
       
    89 		{
       
    90 		if ( (err == KErrEof) || (err == KErrCorrupt) ||
       
    91 		     (err == KErrArgument) || (err == KErrNotSupported))
       
    92 			{
       
    93 			self->iRdbSession.DeleteDatabase( name.FullName(),
       
    94                                               KNSmlSOSServerPolicyUID );
       
    95 
       
    96 			}
       
    97 		User::LeaveIfError( err );
       
    98 		}
       
    99 	User::LeaveIfError( self->iTableProfiles.Open( self->iDatabase, KNSmlDMTableProfiles ) );
       
   100 	self->iColSet = self->iTableProfiles.ColSetL();
       
   101 	//Upgrade from older releases 
       
   102 	if( self->iColSet->Count() != KNSmlDMProfileTableColCount )
       
   103 	{		
       
   104 		self->ScanAndRepairRestoredProfilesL();
       
   105 	}
       
   106 
       
   107 	return self;
       
   108 	}
       
   109 
       
   110 //------------------------------------------------------------------------------
       
   111 //		CNSmlDMSettings::~CNSmlDMSettings()
       
   112 //		Destructor.
       
   113 //------------------------------------------------------------------------------
       
   114 
       
   115 EXPORT_C CNSmlDMSettings::~CNSmlDMSettings()
       
   116 	{
       
   117 	iView.Close();
       
   118 	delete iColSet;
       
   119 	iTableProfiles.Close();
       
   120 	iDatabase.Close();
       
   121 	iFsSession.Close();
       
   122 	iRdbSession.Close();
       
   123 	}
       
   124 
       
   125 //------------------------------------------------------------------------------
       
   126 //		CNSmlDMSettings::ScanAndRepairRestoredProfilesL()
       
   127 //		Scan And Repair RestoredProfiles in database.
       
   128 //------------------------------------------------------------------------------
       
   129 EXPORT_C void CNSmlDMSettings::ScanAndRepairRestoredProfilesL()
       
   130 {
       
   131 	if( iColSet->Count() != KNSmlDMProfileTableColCount )
       
   132 	{
       
   133         TBool hiddenMissing = EFalse;
       
   134         TBool lastSyncMissing = EFalse;
       
   135 		TBool profilelockMissing = EFalse;		
       
   136         if(iColSet->ColNo( KNSmlDMProfileHidden ) == KDbNullColNo)
       
   137         {
       
   138         	hiddenMissing = ETrue;
       
   139             iMissingCols.AppendL(TPtrC (KNSmlDMProfileHidden)) ;
       
   140         }
       
   141         if(iColSet->ColNo( KNSmlDMLastSync ) == KDbNullColNo)
       
   142         {
       
   143         	lastSyncMissing= ETrue;
       
   144             iMissingCols.AppendL(TPtrC(KNSmlDMLastSync));
       
   145         }
       
   146         
       
   147         if(iColSet->ColNo( KNSmlDMProfileLock ) == KDbNullColNo)
       
   148                 {
       
   149                     profilelockMissing= ETrue;
       
   150                     iMissingCols.AppendL(TPtrC(KNSmlDMProfileLock));
       
   151                 }
       
   152             //Check for missing column 
       
   153         if ( hiddenMissing || lastSyncMissing || profilelockMissing  )
       
   154         {                   
       
   155             CNSmlDMProfileList* oldProfiles = new( ELeave ) CArrayPtrFlat<CNSmlDMProfileListItem>(1);
       
   156             CleanupStack::PushL( PtrArrCleanupItem( CNSmlDMProfileListItem, oldProfiles ) );
       
   157             GetProfileListL(oldProfiles);
       
   158             for ( TInt i = 0; i < oldProfiles->Count(); i++)
       
   159             {	
       
   160                 TInt profileID = (*oldProfiles)[i]->IntValue( TNSmlDMProfileData( EDMProfileId ) );
       
   161                 CNSmlDMProfile* profile = OldProfileL( profileID );
       
   162                 profile->CloseDbHandles();
       
   163                 iOldProfiles.AppendL(profile);
       
   164                 HBufC* sqlStatement = HBufC::NewLC( KDMSQLDeleteProfile().Length() + KNSmlDmMaxIntegerLength );
       
   165                 TPtr sqlStatementPtr = sqlStatement->Des();
       
   166     		    TInt err( KErrNone );
       
   167                 iDatabase.Begin();
       
   168                 sqlStatementPtr.Format( KDMSQLDeleteProfile, profileID );
       
   169                 err = iDatabase.Execute( sqlStatementPtr );
       
   170                 CleanupStack::PopAndDestroy();  // sqlStatement
       
   171                     if ( err < KErrNone )
       
   172         		    {   
       
   173         		    iDatabase.Rollback();
       
   174         	      	}
       
   175                 iDatabase.Commit();
       
   176                 iDatabase.Compact();
       
   177     	    }
       
   178                     
       
   179             iView.Close();
       
   180             delete iColSet;
       
   181             iColSet = NULL;
       
   182             iTableProfiles.Close();
       
   183             iDatabase.Begin();
       
   184             iDatabase.DropTable(KNSmlDMTableProfiles);
       
   185             iDatabase.DropTable(KDMCreateCommSetTable);
       
   186             iDatabase.DropTable(KNSmlTableVersion);
       
   187             iDatabase.Commit();
       
   188             iDatabase.Compact();
       
   189             CreateDMTablesL();
       
   190             User::LeaveIfError( iTableProfiles.Open( iDatabase, KNSmlDMTableProfiles ) );
       
   191             iColSet = iTableProfiles.ColSetL();
       
   192             CreateHiddenProfilesL();
       
   193             CreateDMProfilesFromXmlL();
       
   194             // Get the server ID if found in resource do not save it
       
   195             RArray<TPtrC> serverIDList;
       
   196             CNSmlDMProfileList* newProfiles = new( ELeave ) CArrayPtrFlat<CNSmlDMProfileListItem>(1);
       
   197             CleanupStack::PushL( PtrArrCleanupItem( CNSmlDMProfileListItem, newProfiles ) );
       
   198             GetProfileListL(newProfiles);
       
   199             for ( TInt i = 0; i < newProfiles->Count(); i++)
       
   200                 {	
       
   201           //      (*newProfiles)[i]->StrValue( TNSmlDMProfileData( EDMProfileServerId );
       
   202                 TInt profileID = (*newProfiles)[i]->IntValue( TNSmlDMProfileData( EDMProfileId ) );
       
   203                 CNSmlDMProfile* profile = ProfileL( profileID );
       
   204 			    CleanupStack::PushL( profile );
       
   205 			
       
   206                 serverIDList.AppendL(profile->StrValue( EDMProfileServerId));
       
   207                 CleanupStack::PopAndDestroy(profile);
       
   208                 }
       
   209         
       
   210              newProfiles->ResetAndDestroy();
       
   211             CleanupStack::PopAndDestroy(); //newProfiles                
       
   212            // now save the profiles . 
       
   213             for( TInt i = 0 ; i<iOldProfiles.Count(); i++ ) 
       
   214             {
       
   215                 CNSmlDMProfile *profile = iOldProfiles[i];
       
   216                 profile->ResetID();
       
   217                 profile->ResetDatabaseL(&(iDatabase));
       
   218                 if( serverIDList.Find(profile->StrValue( TNSmlDMProfileData( EDMProfileServerId ))) == KErrNotFound)
       
   219                     {
       
   220                     profile->SaveL();
       
   221                     }
       
   222             }
       
   223             oldProfiles->ResetAndDestroy();
       
   224             CleanupStack::PopAndDestroy(); //oldProfiles
       
   225          }
       
   226          else // Corrupt case 
       
   227          
       
   228             {
       
   229             iTableProfiles.Close();
       
   230             iDatabase.Close();
       
   231             TParse name;
       
   232     
       
   233 #ifdef SYMBIAN_SECURE_DBMS
       
   234 	name.Set( KNSmlDMSettingsDbName(), NULL, NULL );
       
   235 #else
       
   236     name.Set( KNSmlDMSettingsDbName(), KNSmlDatabasesNonSecurePath, NULL );
       
   237 #endif
       
   238             TInt err = iRdbSession.DeleteDatabase( name.FullName(),
       
   239                                               KNSmlSOSServerPolicyUID );
       
   240             CreateDatabaseL( name.FullName() );
       
   241             err = iDatabase.Open( iRdbSession,
       
   242 		                      name.FullName(),
       
   243 		                      KNSmlDBMSSecureSOSServerID );
       
   244              }
       
   245            User::LeaveIfError( iTableProfiles.Open( iDatabase, KNSmlDMTableProfiles ) );
       
   246 	       iColSet = iTableProfiles.ColSetL();
       
   247 		        
       
   248 	}
       
   249 	}
       
   250 
       
   251 //------------------------------------------------------------------------------
       
   252 //		CNSmlDMSettings::CreateDMTablesL()
       
   253 //		Creates settings database.
       
   254 //------------------------------------------------------------------------------
       
   255 
       
   256 void CNSmlDMSettings::CreateDMTablesL()
       
   257     {
       
   258     
       
   259     HBufC* createProfileTable = HBufC::NewLC( KDMCreateDMProfilesTable().Length() + KNSmlDmCreateDB); 
       
   260 	TPtr profileTablePtr = createProfileTable->Des();
       
   261 
       
   262 	profileTablePtr.Format(KDMCreateDMProfilesTable,	KNSmlDmMaxProfileNameLength,
       
   263 													KNSmlDmMaxUsernameLength,
       
   264 													KNSmlDmMaxPasswordLength,
       
   265 													KNSmlDmSharedSecretMaxLength,
       
   266 													KNSmlDmMaxURLLength,
       
   267 													KNSmlDmServerIdMaxLength,
       
   268 													KNSmlDmMaxHttpAuthUsernameLength,		
       
   269 													KNSmlDmMaxHttpAuthPasswordLength);
       
   270 													
       
   271 	iDatabase.Begin();
       
   272 
       
   273 	iDatabase.Execute( *createProfileTable );
       
   274 	iDatabase.Execute( KDMCreateCommSetTable );
       
   275 
       
   276 
       
   277 	CDbColSet* colSet = CDbColSet::NewLC();
       
   278 	colSet->AddL(TDbCol(KNSmlVersionColumnMajor(), EDbColUint16));
       
   279 	colSet->AddL(TDbCol(KNSmlVersionColumnMinor(), EDbColUint16));
       
   280 	User::LeaveIfError(iDatabase.CreateTable(KNSmlTableVersion(), *colSet));
       
   281 	CleanupStack::PopAndDestroy(  ); //colset
       
   282 
       
   283 	RDbTable table;
       
   284 	User::LeaveIfError(table.Open(iDatabase, KNSmlTableVersion()));
       
   285 	CleanupClosePushL(table);
       
   286 	colSet = table.ColSetL();
       
   287 	CleanupStack::PushL(colSet);
       
   288 	table.InsertL();
       
   289 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMajor), KNSmlSettingsCurrentVersionMajor);
       
   290 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMinor), KNSmlSettingsCurrentVersionMinor);
       
   291 	table.PutL();
       
   292 	
       
   293 	
       
   294 	iDatabase.Commit();
       
   295 		
       
   296 //	iDatabase.Close();
       
   297 	CleanupStack::PopAndDestroy( 3 ); // createProfileTable, colSet, table													
       
   298 													
       
   299     }
       
   300 //------------------------------------------------------------------------------
       
   301 //		CNSmlDMSettings::CreateDatabaseL()
       
   302 //		Creates settings database.
       
   303 //------------------------------------------------------------------------------
       
   304 void CNSmlDMSettings::CreateDatabaseL(const TDesC& aFullName)
       
   305 	{				
       
   306 	HBufC* createProfileTable = HBufC::NewLC( KDMCreateDMProfilesTable().Length() + KNSmlDmCreateDB); 
       
   307 	TPtr profileTablePtr = createProfileTable->Des();
       
   308 
       
   309 	profileTablePtr.Format(KDMCreateDMProfilesTable,	KNSmlDmMaxProfileNameLength,
       
   310 													KNSmlDmMaxUsernameLength,
       
   311 													KNSmlDmMaxPasswordLength,
       
   312 													KNSmlDmSharedSecretMaxLength,
       
   313 													KNSmlDmMaxURLLength,
       
   314 													KNSmlDmServerIdMaxLength,
       
   315 													KNSmlDmMaxHttpAuthUsernameLength,		
       
   316 													KNSmlDmMaxHttpAuthPasswordLength);
       
   317 
       
   318     User::LeaveIfError( iDatabase.Create( this->iRdbSession,
       
   319                                           aFullName,
       
   320                                           KNSmlDBMSSecureSOSServerID ) );  
       
   321 	
       
   322 	iDatabase.Begin();
       
   323 
       
   324 	iDatabase.Execute( *createProfileTable );
       
   325 	iDatabase.Execute( KDMCreateCommSetTable );
       
   326 
       
   327 	CDbColSet* colSet = CDbColSet::NewLC();
       
   328 	colSet->AddL(TDbCol(KNSmlVersionColumnMajor(), EDbColUint16));
       
   329 	colSet->AddL(TDbCol(KNSmlVersionColumnMinor(), EDbColUint16));
       
   330 	User::LeaveIfError(iDatabase.CreateTable(KNSmlTableVersion(), *colSet));
       
   331 	CleanupStack::PopAndDestroy(  ); //colset
       
   332 
       
   333 	RDbTable table;
       
   334 	User::LeaveIfError(table.Open(iDatabase, KNSmlTableVersion()));
       
   335 	CleanupClosePushL(table);
       
   336 	colSet = table.ColSetL();
       
   337 	CleanupStack::PushL(colSet);
       
   338 	table.InsertL();
       
   339 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMajor), KNSmlSettingsCurrentVersionMajor);
       
   340 	table.SetColL(colSet->ColNo(KNSmlVersionColumnMinor), KNSmlSettingsCurrentVersionMinor);
       
   341 	table.PutL();
       
   342 	
       
   343 	iDatabase.Commit();
       
   344 	CreateHiddenProfilesL();
       
   345 	CreateDMProfilesFromXmlL();
       
   346 	iDatabase.Close();
       
   347 	CleanupStack::PopAndDestroy( 3 ); // createProfileTable, colSet, table
       
   348 	}
       
   349 	
       
   350 //=============================================
       
   351 //		CNSmlDMSettings::CreateHiddenProfilesL()
       
   352 //		Create new profiles reading data from
       
   353 //		resource file
       
   354 //=============================================
       
   355 
       
   356 void CNSmlDMSettings::CreateHiddenProfilesL()
       
   357 	{
       
   358 	TFileName fileName;
       
   359 	Dll::FileName( fileName );
       
   360 	TParse parse;
       
   361 
       
   362 	parse.Set( KNSmlDMProfilesRsc, &fileName, NULL );
       
   363 	fileName = parse.FullName();
       
   364 
       
   365 	RResourceFile resourceFile; 
       
   366 	BaflUtils::NearestLanguageFile( iFsSession, fileName );
       
   367 
       
   368 	TRAPD(leavecode,resourceFile.OpenL( iFsSession,fileName));
       
   369 	if(leavecode != KErrNone )
       
   370 		{
       
   371 		return;
       
   372 		}
       
   373 	CleanupClosePushL(resourceFile);
       
   374 		
       
   375 	HBufC8* profileRes = resourceFile.AllocReadLC( NSML_DM_PROFILES );
       
   376 	TResourceReader reader;
       
   377 	reader.SetBuffer( profileRes );
       
   378 		
       
   379 	CNSmlDMResourceProfiles* profileResReader = CNSmlDMResourceProfiles::NewLC( reader, this );
       
   380 	profileResReader->SaveProfilesL();
       
   381 	CleanupStack::PopAndDestroy(3); // profileResReader, profileRes, resourceFile
       
   382 	}
       
   383 
       
   384 //=============================================
       
   385 //		CNSmlDMSettings::CreateDMProfilesFromXmlL()
       
   386 //		Create new profiles reading data from
       
   387 //		XML file
       
   388 //=============================================
       
   389 
       
   390 void CNSmlDMSettings::CreateDMProfilesFromXmlL()
       
   391 	{
       
   392 	_DBG_FILE("CNSmlDMSettings::CreateDMProfilesFromXmlL(): begin");
       
   393 	
       
   394 	TBuf<KMaxXmlPathLength> xmlPath;
       
   395 	
       
   396     CRepository* cenrep = NULL;
       
   397     cenrep = CRepository::NewL( KCRUidDeviceManagementInternalKeys );
       
   398     TInt result = cenrep->Get( KDevManProfilesXMLFile, xmlPath );
       
   399     delete cenrep;
       
   400     cenrep = NULL;
       
   401 	
       
   402 	if((xmlPath.Length()) && (result == KErrNone))
       
   403 	{
       
   404 		TInt error = KErrNone;
       
   405 		RFile wFile;	
       
   406 		if(wFile.Open(iFsSession, xmlPath, EFileRead | EFileShareReadersOnly) != KErrNone)		
       
   407 		{		
       
   408 		return;
       
   409 		}
       
   410 		CleanupClosePushL(wFile);
       
   411 		RXMLReader DMProfileParser;
       
   412 		DMProfileParser.CreateL();		
       
   413 		CleanupClosePushL(DMProfileParser);
       
   414 		
       
   415 		CDMProfileContentHandler* contentHandler = CDMProfileContentHandler::NewL(this);
       
   416 		CleanupStack::PushL(contentHandler);
       
   417 		DMProfileParser.SetContentHandler(contentHandler);
       
   418 
       
   419 		//parse xml file
       
   420 		TRAP(error, DMProfileParser.ParseL(wFile));
       
   421 		if(error == KErrNone )
       
   422 		{
       
   423 			contentHandler->SaveProfilesL();			
       
   424 		} 
       
   425 		else
       
   426 		{
       
   427 			DBG_FILE_CODE(error, _S8("CNSmlDMSettings::CreateDMProfilesFromXmlL() : XML parser status"));
       
   428 		}   
       
   429     
       
   430 		CleanupStack::PopAndDestroy(3); // contentHandler, DMProfileParser, wFile			
       
   431 	}		
       
   432 	_DBG_FILE("CNSmlDMSettings::CreateDMProfilesFromXmlL(): end");	
       
   433 	}
       
   434 
       
   435 //------------------------------------------------------------------------------
       
   436 //		CNSmlDMSettings::CreateProfileL()
       
   437 //		Returns a pointer to newly created CNSmlDMProfile object.
       
   438 //------------------------------------------------------------------------------
       
   439 
       
   440 EXPORT_C CNSmlDMProfile* CNSmlDMSettings::CreateProfileL()
       
   441 	{
       
   442 	CNSmlDMProfile* profile = CNSmlDMProfile::NewL( this->Database() );
       
   443 
       
   444 	// Set default values for a profile
       
   445 	profile->SetIntValue( EDMProfileId,							KNSmlNewObject );
       
   446 	profile->SetIntValue( EDMProfileTransportId,				KUidNSmlMediumTypeInternet.iUid );
       
   447 
       
   448 	profile->SetIntValue( EDMProfileIAPId,						-1 );
       
   449 	profile->SetIntValue( EDMProfileServerAlertAction,			ESmlConfirmSync );
       
   450 	profile->SetIntValue( EDMProfileAuthenticationRequired,		EFalse );
       
   451 	profile->SetIntValue( EDMProfileDeleteAllowed,				ETrue );
       
   452 	profile->SetIntValue( EDMProfileHttpAuthUsed,				EFalse );
       
   453 	profile->SetIntValue( EDMProfileLock,				        ETrue );
       
   454 							
       
   455 	return profile;
       
   456 	}
       
   457 
       
   458 //------------------------------------------------------------------------------
       
   459 //		CNSmlDMSettings::CreateProfileL()
       
   460 //		Copies all profile data with given Id to
       
   461 //		a newly created CNSmlDMProfile object.
       
   462 //------------------------------------------------------------------------------
       
   463 
       
   464 EXPORT_C CNSmlDMProfile* CNSmlDMSettings::CreateProfileL( const TInt aId  )
       
   465 	{
       
   466 	CNSmlDMProfile* profile = ProfileL( aId);
       
   467 
       
   468 	if ( profile )
       
   469 		{
       
   470 		profile->SetIntValue( EDMProfileId, KNSmlNewObject );
       
   471 		}
       
   472 	
       
   473 	return profile;
       
   474 	}
       
   475 
       
   476 //------------------------------------------------------------------------------
       
   477 //		CNSmlDMSettings::Database()
       
   478 //		Returns a reference to settings database object.
       
   479 //------------------------------------------------------------------------------
       
   480 	
       
   481 EXPORT_C RDbNamedDatabase* CNSmlDMSettings::Database()
       
   482 	{
       
   483 	return &iDatabase;
       
   484 	}
       
   485 
       
   486 //------------------------------------------------------------------------------
       
   487 //		CNSmlDMSettings::ProfileL()
       
   488 //		Returns a pointer to CNSmlDMProfile object with given Id.
       
   489 //		If there's no corresponding Id in the database, returns NULL.
       
   490 //------------------------------------------------------------------------------
       
   491 
       
   492 EXPORT_C CNSmlDMProfile* CNSmlDMSettings::ProfileL( TInt aId)
       
   493 	{
       
   494 	_DBG_FILE("CNSmlDMSettings::ProfileL(): begin");
       
   495 
       
   496 	HBufC* sqlStatement = HBufC::NewLC( KDMSQLGetProfile().Length() + KNSmlDmMaxIntegerLength );
       
   497 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   498 
       
   499 	sqlStatementPtr.Format( KDMSQLGetProfile, aId );
       
   500 
       
   501 	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   502 	
       
   503 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   504 
       
   505 	CNSmlDMProfile* profile = CNSmlDMProfile::NewL( Database() );
       
   506 
       
   507 	if( iView.FirstL() )
       
   508 		{
       
   509 		CleanupStack::PushL( profile );
       
   510 		CNSmlDMCrypt* crypt = new(ELeave) CNSmlDMCrypt;
       
   511 		CleanupStack::PushL(crypt);
       
   512 		iView.GetL();
       
   513 
       
   514 		profile->SetIntValue( EDMProfileId,						ViewColUint( KNSmlDMProfileId ) );
       
   515 		profile->SetStrValue( EDMProfileDisplayName,			ViewColDes( KNSmlDMProfileDisplayName ) );
       
   516 		profile->SetIntValue( EDMProfileIAPId,					ViewColInt( KNSmlDMProfileIAPId ) );
       
   517 		profile->SetIntValue( EDMProfileTransportId,			ViewColInt( KNSmlDMProfileTransportId ) );
       
   518 
       
   519 		profile->SetStrValue( EDMProfileServerUsername, 		ViewColDes( KNSmlDMProfileServerUsername ) );
       
   520 		profile->SetStrValue( EDMProfileServerPassword, 		crypt->DecryptedL(ViewColDes( KNSmlDMProfileServerPassword ) ) );
       
   521 		profile->SetStrValue( EDMProfileClientPassword,         crypt->DecryptedL(ViewColDes( KNSmlDMProfileClientPassword ) ) );
       
   522 		profile->SetStrValue( EDMProfileServerURL,              ViewColDes( KNSmlDMProfileServerURL ) );
       
   523 		
       
   524 		profile->SetStrValue( EDMProfileServerId,				ViewColDes( KNSmlDMProfileServerId ) );
       
   525 		profile->SetIntValue( EDMProfileSessionId,				ViewColInt( KNSmlDMProfileSessionId ) );
       
   526 
       
   527 		profile->SetIntValue( EDMProfileServerAlertAction, 		ViewColInt( KNSmlDMProfileServerAlertedAction ) );
       
   528 		profile->SetIntValue( EDMProfileAuthenticationRequired,	ViewColInt( KNSmlDMProfileAuthenticationRequired ) );
       
   529 		profile->SetIntValue( EDMProfileCreatorId,				ViewColInt( KNSmlDMProfileCreatorId ) );
       
   530 		profile->SetIntValue( EDMProfileDeleteAllowed,				ViewColInt( KNSmlDMProfileDeleteAllowed) );
       
   531 		profile->SetIntValue( EDMProfileHidden,		    		ViewColInt( KNSmlDMProfileHidden) );
       
   532 
       
   533 		profile->SetIntValue( EDMProfileHttpAuthUsed, 			ViewColInt( KNSmlDMProfileHttpAuthUsed) );
       
   534 		profile->SetStrValue( EDMProfileHttpAuthUsername, 		ViewColDes( KNSmlDMProfileHttpAuthUsername ) );
       
   535 		profile->SetStrValue( EDMProfileHttpAuthPassword, 		crypt->DecryptedL(ViewColDes( KNSmlDMProfileHttpAuthPassword ) ) );
       
   536 			profile->SetIntValue( EDMProfileLock,				ViewColInt( KNSmlDMProfileLock) );
       
   537 		
       
   538 		CleanupStack::PopAndDestroy(); // crypt
       
   539 		CleanupStack::Pop(); // profile
       
   540 		}
       
   541 	else
       
   542 		{
       
   543 		delete profile;
       
   544 		//couldn't find
       
   545 		profile = NULL;
       
   546 		}
       
   547 
       
   548 	_DBG_FILE("CNSmlDMSettings::ProfileL(): end");
       
   549 	return profile;
       
   550 	}
       
   551 
       
   552 //------------------------------------------------------------------------------
       
   553 //		CNSmlDMSettings::GetProfileListL()
       
   554 //		Appends CNSmlDMProfileListItem objects to aProfileList.
       
   555 //------------------------------------------------------------------------------
       
   556 
       
   557 EXPORT_C void CNSmlDMSettings::GetProfileListL( CNSmlDMProfileList* aProfileList )
       
   558 	{
       
   559 	_DBG_FILE("CNSmlDMSettings::GetProfileListL(): begin");
       
   560 
       
   561 	aProfileList->ResetAndDestroy();
       
   562 
       
   563 	PrepareViewL( KDMSQLGetAllProfiles, iView.EUpdatable );
       
   564 
       
   565 	if( iView.Unevaluated() )
       
   566 		{
       
   567 		iView.EvaluateAll(); 	
       
   568 		}
       
   569 	
       
   570 
       
   571 	while ( iView.NextL() )
       
   572 		{
       
   573 		AddToProfileListL( aProfileList );
       
   574 		}
       
   575 
       
   576 	_DBG_FILE("CNSmlDMSettings::GetProfileListL(): end");
       
   577 	}
       
   578 
       
   579 //------------------------------------------------------------------------------
       
   580 //		CNSmlDMSettings::DeleteProfileL()
       
   581 //		Permanently deletes profile with given Id.
       
   582 //		If delete is succesfull returns KErrNone,
       
   583 //		otherwise returns negative integer 
       
   584 //      (defined in EPOC error codes).
       
   585 //------------------------------------------------------------------------------
       
   586 
       
   587 EXPORT_C TInt CNSmlDMSettings::DeleteProfileL( TInt aId )
       
   588 	{
       
   589 	_DBG_FILE("CNSmlDMSettings::DeleteProfileL(): begin");
       
   590 	if ( iDatabase.InTransaction() )
       
   591 		{
       
   592 		return ( KErrAccessDenied );
       
   593 		}
       
   594 	
       
   595 	CNSmlDMProfile* profile = ProfileL(aId);
       
   596 	if(profile)
       
   597 		{
       
   598 		CleanupStack::PushL(profile);
       
   599 		RNSmlDMCallbackSession cbSession;
       
   600 		User::LeaveIfError( cbSession.Connect() );
       
   601 		CleanupClosePushL(cbSession);
       
   602 		HBufC8* serverId = CnvUtfConverter::ConvertFromUnicodeToUtf8L(
       
   603 			profile->StrValue(EDMProfileServerId));
       
   604 		CleanupStack::PushL(serverId);
       
   605 		
       
   606 		cbSession.EraseServerIdL(*serverId);
       
   607 		CleanupStack::PopAndDestroy(3);  // profile,cbSession,serverId
       
   608 		}
       
   609 
       
   610 	HBufC* sqlStatement = HBufC::NewLC( KDMSQLDeleteProfile().Length() + KNSmlDmMaxIntegerLength );
       
   611 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   612 	
       
   613 	TInt err( KErrNone );
       
   614 
       
   615 	iDatabase.Begin();
       
   616 
       
   617 	sqlStatementPtr.Format( KDMSQLDeleteProfile, aId );
       
   618 	err = iDatabase.Execute( sqlStatementPtr );
       
   619 
       
   620 	CleanupStack::PopAndDestroy();  // sqlStatement
       
   621 	
       
   622 	if ( err < KErrNone )
       
   623 		{
       
   624 		iDatabase.Rollback();
       
   625 		return err;
       
   626 		}
       
   627 
       
   628 	CommitAndCompact();
       
   629 
       
   630 	_DBG_FILE("CNSmlDMSettings::DeleteProfileL(): end");
       
   631 
       
   632 	return KErrNone;
       
   633 	}
       
   634 
       
   635 //------------------------------------------------------------------------------
       
   636 //		CNSmlDMSettings::PrepareViewL()
       
   637 //		Closes and prepares the view
       
   638 //		
       
   639 //------------------------------------------------------------------------------
       
   640 void CNSmlDMSettings::PrepareViewL( const TDesC& aSql, RDbRowSet::TAccess aAccess )
       
   641 	{
       
   642 	iView.Close();
       
   643 	User::LeaveIfError( iView.Prepare( iDatabase, TDbQuery( aSql ), aAccess ) );
       
   644 	}
       
   645 
       
   646 //------------------------------------------------------------------------------
       
   647 //		CNSmlDMSettings::CommitAndCompact
       
   648 //		Commits update and compacts the database
       
   649 //		
       
   650 //------------------------------------------------------------------------------
       
   651 void CNSmlDMSettings::CommitAndCompact() 
       
   652 	{
       
   653 	iDatabase.Commit();
       
   654 	iDatabase.Compact();
       
   655 	}
       
   656 
       
   657 //------------------------------------------------------------------------------
       
   658 //		CNSmlDMSettings::AddToProfileListL
       
   659 //		Adds database field to profile list
       
   660 //		
       
   661 //------------------------------------------------------------------------------
       
   662 void CNSmlDMSettings::AddToProfileListL( CNSmlDMProfileList* aProfileList )
       
   663 	{
       
   664 	iView.GetL();
       
   665 
       
   666 	aProfileList->AppendL( CNSmlDMProfileListItem::NewLC( iView.ColUint( iColSet->ColNo( KNSmlDMProfileId ) ) ) );
       
   667 	
       
   668 	CleanupStack::Pop();
       
   669 	}
       
   670 
       
   671 //------------------------------------------------------------------------------
       
   672 //		CNSmlDMSettings::ColDes
       
   673 //		
       
   674 //------------------------------------------------------------------------------
       
   675 TPtrC CNSmlDMSettings::ViewColDes( const TDesC& aFieldName )
       
   676 	{
       
   677 	return iView.ColDes( iColSet->ColNo( aFieldName ) );
       
   678 	}
       
   679 
       
   680 //------------------------------------------------------------------------------
       
   681 //		CNSmlDMSettings::ColInt
       
   682 //		
       
   683 //------------------------------------------------------------------------------
       
   684 TInt CNSmlDMSettings::ViewColInt( const TDesC& aFieldName )
       
   685 	{
       
   686 	return iView.ColInt( iColSet->ColNo( aFieldName ) );
       
   687 	}
       
   688 
       
   689 //------------------------------------------------------------------------------
       
   690 //		CNSmlDMSettings::ColUint
       
   691 //	
       
   692 //		
       
   693 //------------------------------------------------------------------------------
       
   694 TUint CNSmlDMSettings::ViewColUint( const TDesC& aFieldName )
       
   695 	{
       
   696 	return iView.ColUint( iColSet->ColNo( aFieldName ) );
       
   697 	}
       
   698 
       
   699 //------------------------------------------------------------------------------
       
   700 //		CNSmlDMSettings::IsDMAllowedL
       
   701 //	
       
   702 //		
       
   703 //------------------------------------------------------------------------------
       
   704 EXPORT_C TBool CNSmlDMSettings::IsDMAllowedL()
       
   705 {
       
   706 	TBuf<30> sqlStatement;
       
   707 	sqlStatement.Format( KDMSQLGetAllCommonSettings );
       
   708 
       
   709 	RDbTable table;
       
   710 	CleanupClosePushL( table );
       
   711 
       
   712 	User::LeaveIfError( table.Open( iDatabase, KNSmlTableCommonSettings ) );
       
   713 	
       
   714 	CDbColSet* colSet = table.ColSetL();
       
   715 	CleanupStack::PushL( colSet );
       
   716 	
       
   717 	RDbView lView;
       
   718 	CleanupClosePushL( lView );
       
   719 	User::LeaveIfError( lView.Prepare( iDatabase, TDbQuery( sqlStatement ), lView.EReadOnly ) );
       
   720 
       
   721 	TBuf<10> columnName;
       
   722 	columnName.Format( KLogColumn);
       
   723 
       
   724 	iAllowed = ETrue;
       
   725 
       
   726 	iDatabase.Begin();	
       
   727 	if ( lView.FirstL() )
       
   728 		{
       
   729 		lView.GetL();
       
   730 
       
   731 		iAllowed = lView.ColInt( colSet->ColNo( columnName ));
       
   732 
       
   733 		}	
       
   734 
       
   735 	iDatabase.Commit();
       
   736 
       
   737 	CleanupStack::PopAndDestroy(3);  //table, colSet, lView
       
   738 	return (iAllowed);
       
   739 }
       
   740 
       
   741 //------------------------------------------------------------------------------
       
   742 //		CNSmlDMSettings::ServerIdFoundL
       
   743 //	
       
   744 //		
       
   745 //------------------------------------------------------------------------------
       
   746 EXPORT_C TBool CNSmlDMSettings::ServerIdFoundL( const TDesC& aServerId )
       
   747 {
       
   748 	_DBG_FILE("CNSmlDMSettings::ServerIdFoundL(): begin");
       
   749 
       
   750 	TBool ret = EFalse;
       
   751 	if(aServerId.Length()>0)
       
   752 	    {
       
   753 	    HBufC* sqlStatement = HBufC::NewLC( KDMSQLSelectMatchingServerIdOnly().Length() + aServerId.Length());
       
   754         TPtr sqlStatementPtr = sqlStatement->Des();
       
   755         sqlStatementPtr.Format(KDMSQLSelectMatchingServerIdOnly, &aServerId);
       
   756     	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   757 
       
   758     	CleanupStack::PopAndDestroy();  // sqlStatement
       
   759     	
       
   760     	if ( iView.FirstL() )
       
   761     		{
       
   762     		ret = ETrue;
       
   763     		}
       
   764 	    }
       
   765 	_DBG_FILE("CNSmlDMSettings::ServerIdFoundL(): end");
       
   766     return ret;
       
   767 }
       
   768 //------------------------------------------------------------------------------
       
   769 //		CNSmlDMSettings::ServerIdFoundL
       
   770 //	
       
   771 //		
       
   772 //------------------------------------------------------------------------------
       
   773 EXPORT_C TBool CNSmlDMSettings::ServerIdFoundL( const TDesC& aServerId, TInt aProfileId )
       
   774 {
       
   775 	_DBG_FILE("CNSmlDMSettings::ServerIdFoundL(): begin");
       
   776 
       
   777 	TBool ret = EFalse;
       
   778 	if(aServerId.Length()>0)
       
   779 	    {
       
   780 	    HBufC* sqlStatement = HBufC::NewLC( KDMSQLSelectMatchingServerId().Length() + aServerId.Length() + KNSmlDmMaxIntegerLength );
       
   781         TPtr sqlStatementPtr = sqlStatement->Des();
       
   782         sqlStatementPtr.Format(KDMSQLSelectMatchingServerId, &aServerId, aProfileId );
       
   783     	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   784 
       
   785     	CleanupStack::PopAndDestroy();  // sqlStatement
       
   786     	
       
   787     	if ( iView.FirstL() )
       
   788     		{
       
   789     		ret = ETrue;
       
   790     		}
       
   791 	    }
       
   792 	_DBG_FILE("CNSmlDMSettings::ServerIdFoundL(): end");
       
   793     return ret;
       
   794 }
       
   795 
       
   796 //------------------------------------------------------------------------------
       
   797 //		CNSmlDMSettings::SetDMAllowedFlagL
       
   798 //	
       
   799 //		
       
   800 //------------------------------------------------------------------------------
       
   801 EXPORT_C void CNSmlDMSettings::SetDMAllowedFlagL(TBool aAllowed)
       
   802 {
       
   803 	_DBG_FILE("CNSmlDMSettings::SetDMAllowedFlagL(): begin");
       
   804 
       
   805 	TBuf<30> sqlStatement;
       
   806 	sqlStatement.Format( KDMSQLGetAllCommonSettings);
       
   807 
       
   808 	RDbTable table;
       
   809 	CleanupClosePushL( table );
       
   810 
       
   811 	User::LeaveIfError( table.Open( iDatabase, KNSmlTableCommonSettings ) );
       
   812 	
       
   813 	CDbColSet* colSet = table.ColSetL();
       
   814 	CleanupStack::PushL( colSet );
       
   815 	
       
   816 	RDbView lView;
       
   817 	CleanupClosePushL( lView );
       
   818 	User::LeaveIfError( lView.Prepare( iDatabase, TDbQuery( sqlStatement ), lView.EUpdatable ) );
       
   819 
       
   820 	TBuf<10> columnName;
       
   821 	columnName.Format( KLogColumn);
       
   822 
       
   823 	iDatabase.Begin();	
       
   824 	if ( lView.FirstL() )
       
   825 		{
       
   826 		lView.GetL();
       
   827 		lView.UpdateL();
       
   828 
       
   829 		lView.SetColL( colSet->ColNo( columnName ), aAllowed);
       
   830 
       
   831 		}
       
   832 	else
       
   833 		{
       
   834 		lView.InsertL();
       
   835 		lView.SetColL( colSet->ColNo( columnName ), aAllowed );
       
   836 		}
       
   837 	lView.PutL();
       
   838 
       
   839 	CleanupStack::PopAndDestroy(3);  //table, colSet, lView
       
   840 
       
   841 	iDatabase.Commit();
       
   842 	iDatabase.Compact();
       
   843 
       
   844 	_DBG_FILE("CNSmlDMSettings::SetDMAllowedFlagL(): end");
       
   845 }
       
   846 
       
   847 //------------------------------------------------------------------------------
       
   848 //		CNSmlDMSettings::OldProfileL
       
   849 //	
       
   850 //		
       
   851 //------------------------------------------------------------------------------
       
   852 
       
   853 CNSmlDMProfile* CNSmlDMSettings::OldProfileL( TInt aId)
       
   854 	{
       
   855 	_DBG_FILE("CNSmlDMSettings::OldProfileL(): begin");
       
   856 
       
   857 	HBufC* sqlStatement = HBufC::NewLC( KDMSQLGetProfile().Length() + KNSmlDmMaxIntegerLength );
       
   858 	TPtr sqlStatementPtr = sqlStatement->Des();
       
   859 
       
   860 	sqlStatementPtr.Format( KDMSQLGetProfile, aId );
       
   861 
       
   862 	PrepareViewL( sqlStatementPtr, iView.EReadOnly );
       
   863 	
       
   864 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   865 
       
   866 	CNSmlDMProfile* profile = CNSmlDMProfile::NewL( Database() );
       
   867 
       
   868 	if( iView.FirstL() )
       
   869 		{
       
   870 		CleanupStack::PushL( profile );
       
   871 		CNSmlDMCrypt* crypt = new(ELeave) CNSmlDMCrypt;
       
   872 		CleanupStack::PushL(crypt);
       
   873 		iView.GetL();
       
   874 
       
   875 		profile->SetIntValue( EDMProfileId,						ViewColUint( KNSmlDMProfileId ) );
       
   876 		profile->SetStrValue( EDMProfileDisplayName,			ViewColDes( KNSmlDMProfileDisplayName ) );
       
   877 		profile->SetIntValue( EDMProfileIAPId,					ViewColInt( KNSmlDMProfileIAPId ) );
       
   878 		profile->SetIntValue( EDMProfileTransportId,			ViewColInt( KNSmlDMProfileTransportId ) );
       
   879 
       
   880 		profile->SetStrValue( EDMProfileServerUsername, 		ViewColDes( KNSmlDMProfileServerUsername ) );
       
   881 		profile->SetStrValue( EDMProfileServerPassword, 		crypt->DecryptedL(ViewColDes( KNSmlDMProfileServerPassword ) ) );
       
   882 		profile->SetStrValue( EDMProfileClientPassword,         crypt->DecryptedL(ViewColDes( KNSmlDMProfileClientPassword ) ) );
       
   883 		profile->SetStrValue( EDMProfileServerURL,              ViewColDes( KNSmlDMProfileServerURL ) );
       
   884 		
       
   885 		profile->SetStrValue( EDMProfileServerId,				ViewColDes( KNSmlDMProfileServerId ) );
       
   886 		profile->SetIntValue( EDMProfileSessionId,				ViewColInt( KNSmlDMProfileSessionId ) );
       
   887 
       
   888 		profile->SetIntValue( EDMProfileServerAlertAction, 		ViewColInt( KNSmlDMProfileServerAlertedAction ) );
       
   889 		profile->SetIntValue( EDMProfileAuthenticationRequired,	ViewColInt( KNSmlDMProfileAuthenticationRequired ) );
       
   890 		profile->SetIntValue( EDMProfileCreatorId,				ViewColInt( KNSmlDMProfileCreatorId ) );
       
   891 		profile->SetIntValue( EDMProfileDeleteAllowed,				ViewColInt( KNSmlDMProfileDeleteAllowed) );
       
   892 		
       
   893 		if(iMissingCols.Find(TPtrC(KNSmlDMProfileHidden))==KErrNotFound)
       
   894 		    profile->SetIntValue( EDMProfileHidden,		    		ViewColInt( KNSmlDMProfileHidden) );
       
   895 
       
   896 		profile->SetIntValue( EDMProfileHttpAuthUsed, 			ViewColInt( KNSmlDMProfileHttpAuthUsed) );
       
   897 		profile->SetStrValue( EDMProfileHttpAuthUsername, 		ViewColDes( KNSmlDMProfileHttpAuthUsername ) );
       
   898 		profile->SetStrValue( EDMProfileHttpAuthPassword, 		crypt->DecryptedL(ViewColDes( KNSmlDMProfileHttpAuthPassword ) ) );
       
   899 		if(iMissingCols.Find(TPtrC(KNSmlDMProfileLock))==KErrNotFound)		
       
   900 		  profile->SetIntValue( EDMProfileLock,				ViewColInt( KNSmlDMProfileLock) );
       
   901 		
       
   902 		CleanupStack::PopAndDestroy(); // crypt
       
   903 		CleanupStack::Pop(); // profile
       
   904 		}
       
   905 	else
       
   906 		{
       
   907 		delete profile;
       
   908 		//couldn't find
       
   909 		profile = NULL;
       
   910 		}
       
   911 
       
   912 	_DBG_FILE("CNSmlDMSettings::OldProfileL(): end");
       
   913 	return profile;
       
   914 	}
       
   915 	
       
   916 //  End of File
       
   917