syncmlfw/dm/treedbhandler/src/nsmldmdbhandler.cpp
changeset 0 b497e44ab2fc
child 66 08c8318ec9cb
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2005 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:  source file for dm tree db handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <utf.h>
       
    20 #include <s32file.h>
       
    21 #include <sysutil.h>
       
    22 #include <bautils.h>
       
    23 #include <utf.h>
       
    24 #include <data_caging_path_literals.hrh>
       
    25 
       
    26 #include <nsmlconstants.h>
       
    27 #include <nsmldebug.h>
       
    28 #include <nsmldmdbhandler.h>
       
    29 #include <nsmldmuri.h>
       
    30 //For P&S keys
       
    31 #include <e32property.h>
       
    32 #include "nsmldminternalpskeys.h"
       
    33 #include "nsmldmaclparser.h"
       
    34 
       
    35 
       
    36 
       
    37 // ===========================================================================
       
    38 // CNSmlDmDbHandler
       
    39 // ===========================================================================
       
    40 
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CNSmlDmDbHandler::~CNSmlDmDbHandler()
       
    44 // ---------------------------------------------------------------------------
       
    45 EXPORT_C CNSmlDmDbHandler::~CNSmlDmDbHandler()
       
    46 	{
       
    47 	iAdapters.ResetAndDestroy();
       
    48 	iView.Close();
       
    49 	iDatabase.Close();
       
    50 	if ( iFileStore )
       
    51 		{
       
    52 		delete iFileStore;
       
    53 		}
       
    54 	iFsSession.Close();
       
    55 	delete iServer;
       
    56 	iAclURIs.ResetAndDestroy();
       
    57 	iAcls.ResetAndDestroy();
       
    58 
       
    59 	}
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CNSmlDmDbHandler* CNSmlDmDbHandler::NewLC()
       
    63 // ---------------------------------------------------------------------------
       
    64 EXPORT_C CNSmlDmDbHandler* CNSmlDmDbHandler::NewLC()
       
    65 	{
       
    66 	CNSmlDmDbHandler* self = new (ELeave) CNSmlDmDbHandler();
       
    67 	CleanupStack::PushL( self );
       
    68 	self->ConstructL();
       
    69 	return self;
       
    70 	}
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CNSmlDmDbHandler* CNSmlDmDbHandler::NewL()
       
    74 // ---------------------------------------------------------------------------
       
    75 EXPORT_C CNSmlDmDbHandler* CNSmlDmDbHandler::NewL()
       
    76 	{
       
    77 	CNSmlDmDbHandler* self = CNSmlDmDbHandler::NewLC();
       
    78 	CleanupStack::Pop();
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // void CNSmlDmDbHandler::ConstructL()
       
    85 // ---------------------------------------------------------------------------
       
    86 void CNSmlDmDbHandler::ConstructL()
       
    87 	{
       
    88 	_DBG_FILE("CNSmlDmDbHandler::ConstructL() : begin");
       
    89 	User::LeaveIfError( iFsSession.Connect() );
       
    90 	
       
    91 	HBufC* treeDbPath = HBufC::NewLC( KMaxFileName );
       
    92 	TPtr treeDbPtr = treeDbPath->Des();
       
    93 	User::LeaveIfError(iFsSession.PrivatePath( treeDbPtr ) );
       
    94 	treeDbPtr.Append(KSmlDmModuleDbName);
       
    95 	
       
    96 	if ( !BaflUtils::FileExists ( iFsSession, *treeDbPath ) )
       
    97 		{
       
    98 		CreateDatabaseL( *treeDbPath );		
       
    99 		}
       
   100 	else
       
   101 		{
       
   102 		
       
   103  	   	iFileStore = CPermanentFileStore::OpenL ( iFsSession,
       
   104 	   		*treeDbPath, EFileRead|EFileWrite );
       
   105  	   		
       
   106     	iFileStore->SetTypeL ( iFileStore->Layout() );
       
   107 		}
       
   108 	CleanupStack::PopAndDestroy(); //treeDbPath	
       
   109 	_DBG_FILE("CNSmlDmDbHandler::ConstructL() : end");
       
   110 	}
       
   111 
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // void CNSmlDmDbHandler::CreateDatabaseL( const TDesC& aFullName )
       
   115 // Creates database and tables
       
   116 // ---------------------------------------------------------------------------
       
   117 void CNSmlDmDbHandler::CreateDatabaseL( const TDesC& aFullName )
       
   118 	{
       
   119 	_DBG_FILE("CNSmlDmDbHandler::CreateDatabaseL() : begin");
       
   120 	
       
   121     // Create empty database file.
       
   122   	iFsSession.CreatePrivatePath( EDriveC );
       
   123 
       
   124     iFileStore = CPermanentFileStore::ReplaceL ( iFsSession,
       
   125     	aFullName, EFileRead|EFileWrite );
       
   126     	
       
   127     iFileStore->SetTypeL ( iFileStore->Layout() );
       
   128     TStreamId id = iDatabase.CreateL ( iFileStore );
       
   129     iFileStore->SetRootL ( id );
       
   130     iFileStore->CommitL();
       
   131 	
       
   132 	iDatabase.Begin();
       
   133 
       
   134 	CDbColSet* luidMappingColumns = CDbColSet::NewLC();
       
   135 	luidMappingColumns->AddL(TDbCol ( KNSmlDmAdapterId, EDbColUint32 ) ); 		//adapter impluid
       
   136 	luidMappingColumns->AddL(TDbCol ( KNSmlDmMappingURI, EDbColLongText8 ) ); 	//mapping uries in string separated with '&'
       
   137 	luidMappingColumns->AddL(TDbCol ( KNSmlDmMappingLuid, EDbColLongText8 ) ); 	//table of mapping luids
       
   138 	
       
   139 	//Create LuidMapping table
       
   140 	User::LeaveIfError ( iDatabase.CreateTable ( KNSmlDmLuidMappingTable,
       
   141 		*luidMappingColumns ) );
       
   142 
       
   143 	CDbColSet* aclColumns = CDbColSet::NewLC();
       
   144 
       
   145 	//adapter impluid
       
   146 	aclColumns->AddL ( TDbCol ( KNSmlDmAdapterId, EDbColUint32 ) ); 
       
   147 	//uries in string separated with '&'
       
   148 	aclColumns->AddL ( TDbCol ( KNSmlDmAclURI, EDbColLongText8 ) );
       
   149 	//table of acls
       
   150 	aclColumns->AddL ( TDbCol ( KNSmlDmAcl, EDbColLongText8 ) );
       
   151 	
       
   152 	//Create AclTable table
       
   153 	User::LeaveIfError ( iDatabase.CreateTable ( KNSmlDmAclTable,
       
   154 		*aclColumns ) );
       
   155 
       
   156 	iDatabase.Commit();
       
   157 	iDatabase.Close();
       
   158 	CleanupStack::PopAndDestroy( 2 ); // luidMappingColumns, aclColumns
       
   159 	_DBG_FILE("CNSmlDmDbHandler::CreateDatabaseL() : end");
       
   160 	}
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // void CNSmlDmDbHandler::SetServerL(const TDesC& aServer)
       
   164 // Set the iServer member variable, which is used for ACL handling
       
   165 // ---------------------------------------------------------------------------
       
   166 EXPORT_C void CNSmlDmDbHandler::SetServerL(const TDesC8& aServer)
       
   167 	{
       
   168 	_DBG_FILE("CNSmlDmDbHandler::SetServerL() : begin");
       
   169 	if(iServer)
       
   170 		{
       
   171 		delete iServer;
       
   172 		iServer = 0;
       
   173 		}
       
   174 	iServer = aServer.AllocL();
       
   175 	_DBG_FILE("CNSmlDmDbHandler::SetServerL() : end");
       
   176 	}
       
   177 
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // void CNSmlDmDbHandler::ReadAllURIMappingsFromDbL()
       
   181 // Get the URI mapping info from database to member variables.
       
   182 // ---------------------------------------------------------------------------
       
   183 
       
   184 void CNSmlDmDbHandler::ReadAllURIMappingsFromDbL()
       
   185 	{
       
   186 	iAdapters.ResetAndDestroy();
       
   187 	iDatabase.OpenL ( iFileStore, iFileStore->Root() );
       
   188 
       
   189 	_LIT ( KNSmlDmSQLStatement, "SELECT * FROM LuidMappingTable");
       
   190 	TPtrC sql ( KNSmlDmSQLStatement() );
       
   191 
       
   192 	User::LeaveIfError ( iView.Prepare ( iDatabase, sql,
       
   193 		TDbWindow( TDbWindow::EUnlimited ) ) );
       
   194 		
       
   195 	User::LeaveIfError ( iView.EvaluateAll() );
       
   196 
       
   197 	if ( iView.FirstL() )
       
   198 		{
       
   199 		do 
       
   200 			{
       
   201 			iView.GetL();
       
   202 			ReadOneLineFromDbL();
       
   203 			} while ( iView.NextL() );	
       
   204 		}
       
   205 	iView.Close();
       
   206 	iDatabase.Close();
       
   207 	}
       
   208 
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // void CNSmlDmDbHandler::ReadOneLineFromDbL()
       
   212 // Reads one line at the time from db.
       
   213 // Cursor is kept at valid line outside of this function.
       
   214 // ---------------------------------------------------------------------------
       
   215 
       
   216 void CNSmlDmDbHandler::ReadOneLineFromDbL()
       
   217 	{
       
   218 	TUint32 adapterId(0);			
       
   219 	CDbColSet* colSet = iView.ColSetL();
       
   220 	CleanupStack::PushL ( colSet );
       
   221 	
       
   222 	RDbColReadStream colReadStream;
       
   223 		
       
   224 	adapterId = iView.ColUint32 ( colSet->ColNo ( KNSmlDmAdapterId ) );
       
   225 	TDbColNo uri = colSet->ColNo ( KNSmlDmMappingURI );
       
   226 	TDbColNo luid = colSet->ColNo ( KNSmlDmMappingLuid );
       
   227 
       
   228 	colReadStream.OpenLC ( iView, uri );
       
   229 	HBufC8* uri8 = HBufC8::NewLC ( iView.ColLength( uri ) );
       
   230 	TPtr8 uriPtr = uri8->Des();
       
   231 	colReadStream.ReadL ( uriPtr, iView.ColLength ( uri ) );
       
   232 	CleanupStack::Pop();  // uriPtr
       
   233 	CleanupStack::PopAndDestroy();  // colReadStream
       
   234 	CleanupStack::PushL( uri8 ); 
       
   235 
       
   236 	colReadStream.OpenLC ( iView, luid );
       
   237 	HBufC8* luid8 = HBufC8::NewLC ( iView.ColLength( luid ) );
       
   238 	TPtr8 luidPtr = luid8->Des();
       
   239 	colReadStream.ReadL ( luidPtr, iView.ColLength ( luid ) );
       
   240 	CleanupStack::Pop();  // luidPtr
       
   241 	CleanupStack::PopAndDestroy();  // colReadStream
       
   242 	CleanupStack::PushL ( luid8 ); 
       
   243 		
       
   244 	UpdateInternalArrayL (
       
   245 		adapterId, uriPtr, luidPtr, EMapNoAction, ENormalSearch );
       
   246 		
       
   247 	CleanupStack::PopAndDestroy(3); // luidPtr, uriPtr, colSet				
       
   248 	}
       
   249 	
       
   250 // ---------------------------------------------------------------------------
       
   251 // void CNSmlDmDbHandler::UpdateInternalArrayL()
       
   252 // Adds (or marks) entries to member variable array.
       
   253 // Given values are either read from db or function call parameters.
       
   254 // ---------------------------------------------------------------------------
       
   255 void CNSmlDmDbHandler::UpdateInternalArrayL(TUint32 aAdapterId,
       
   256 	const TDesC8& aURI,
       
   257 	const TDesC8& aLUID,
       
   258 	TNSmlDmMapOperation aOperation,
       
   259 	TNSmlDmSearchMethod aMethod,
       
   260 	const TDesC8& aNewUri )
       
   261 	{
       
   262 	CNSmlDmURIMapping* mapElement =
       
   263 		CNSmlDmURIMapping::NewLC( aURI, aLUID, aOperation );
       
   264 		
       
   265 	TBool newAdapter(ETrue);
       
   266 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   267 		{
       
   268 		if ( iAdapters[i]->iAdapterId == aAdapterId )
       
   269 			{
       
   270 			newAdapter = EFalse;
       
   271 			if ( aOperation == EMapNoAction || aMethod == ENoSearch )
       
   272 				{
       
   273 				iAdapters[i]->iMapArray.AppendL ( mapElement );
       
   274 				CleanupStack::Pop();  // mapElement			
       
   275 				}
       
   276 			else
       
   277 				{
       
   278 				TInt adIndex(0);
       
   279 				RArray<TInt> uriIndex;
       
   280 				SearchUriL( aAdapterId, aURI, aMethod, adIndex, uriIndex );
       
   281 				if ( aOperation == EMapDelete )
       
   282 					{
       
   283 					for ( TInt u=0; u<uriIndex.Count(); u++ )
       
   284 						{
       
   285 						TInt mapIndex ( uriIndex[u] );
       
   286 						iAdapters[adIndex]->iMapArray[mapIndex]->iOperation
       
   287 							= aOperation;
       
   288 						}
       
   289 					CleanupStack::PopAndDestroy();  // mapElement
       
   290 					}
       
   291 				else if ( aOperation == EMapInsert )
       
   292 					{
       
   293 					if ( uriIndex.Count() > 0 )
       
   294 						{
       
   295 						CleanupStack::PopAndDestroy();  // mapElement
       
   296 						for ( TInt u=0; u<uriIndex.Count(); u++ )
       
   297 							{
       
   298 							if ( aLUID.Compare (*iAdapters[adIndex]->
       
   299 								iMapArray[uriIndex[u]]->iLUID ) != 0 ||
       
   300 							    iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   301 							    iOperation == EMapDelete ) 
       
   302 								{
       
   303 								iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   304 									iOperation = EMapUpdate;
       
   305 									
       
   306 								delete iAdapters[adIndex]->
       
   307 									iMapArray[uriIndex[u]]->iLUID;
       
   308 									
       
   309 								iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   310 									iLUID = NULL;							
       
   311 									
       
   312 								iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   313 									iLUID = aLUID.AllocL();
       
   314 								}
       
   315 							}
       
   316 						}
       
   317 					else
       
   318 						{
       
   319 						iAdapters[i]->iMapArray.AppendL ( mapElement );
       
   320 						CleanupStack::Pop();  // mapElement
       
   321 						}
       
   322 					}
       
   323 				else if ( aOperation == EMapRename )
       
   324 					{
       
   325 					for ( TInt u=0; u<uriIndex.Count(); u++ )
       
   326 						{
       
   327 						iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   328 							iOperation = EMapDelete;
       
   329 							
       
   330 						HBufC8* oldLUIDSaved = iAdapters[adIndex]->
       
   331 							iMapArray[uriIndex[u]]->iLUID;
       
   332 							
       
   333 						TPtr8 newURI = HBufC8::NewLC( aNewUri.Length() + ( iAdapters[adIndex]->iMapArray[uriIndex[u]]->iURI->Length() - aURI.Length() ) )->Des();
       
   334 						newURI.Copy ( aNewUri );
       
   335 						
       
   336 						if (  iAdapters[adIndex]->iMapArray[uriIndex[u]]->
       
   337 							iURI->Length() - aURI.Length() > 0 )
       
   338 							{
       
   339 							newURI.Append ( iAdapters[adIndex]->
       
   340 								iMapArray[uriIndex[u]]->iURI->
       
   341 								Mid( aURI.Length() ) );
       
   342 							}
       
   343 							
       
   344 						UpdateInternalArrayL ( aAdapterId,
       
   345 							newURI,
       
   346 							*oldLUIDSaved,
       
   347 							EMapInsert,
       
   348 							ENoSearch );
       
   349 							
       
   350 						CleanupStack::PopAndDestroy();  // newURI
       
   351 						}
       
   352 					CleanupStack::PopAndDestroy();  // mapElement
       
   353 					}
       
   354 				else 
       
   355 					{
       
   356 					CleanupStack::PopAndDestroy(); // mapElement
       
   357 					}
       
   358 				uriIndex.Reset();
       
   359 				}
       
   360 			break;
       
   361 			}
       
   362 		}
       
   363 	if ( newAdapter && aOperation != EMapDelete )
       
   364 		{
       
   365 		CNSmlDmAdapterElement* adapterElement =
       
   366 			CNSmlDmAdapterElement::NewLC ( aAdapterId );
       
   367 			
       
   368 		iAdapters.AppendL ( adapterElement );
       
   369 		iAdapters[iAdapters.Count()-1]->iMapArray.AppendL ( mapElement );
       
   370 		CleanupStack::Pop(2);  // adapterElement, mapElement
       
   371 		}	
       
   372 //ADD-S for DMS00652653
       
   373 	else if ( newAdapter)
       
   374 		{
       
   375 		CleanupStack::PopAndDestroy(); // mapElement
       
   376 		}
       
   377 //ADD-E for DMS00652653
       
   378 	}
       
   379 	
       
   380 	
       
   381 // ---------------------------------------------------------------------------
       
   382 // void CNSmlDmDbHandler::UpdateRowInDbL(
       
   383 //	TUint32 aAdapterId,
       
   384 // 	const TDesC8& aURI, 
       
   385 //	const TDesC8& aLUID,
       
   386 //	TNSmlDmMapOperation aOperation,
       
   387 //	TBool aPrepareView )
       
   388 // ---------------------------------------------------------------------------
       
   389 void CNSmlDmDbHandler::UpdateRowInDbL(TUint32 aAdapterId,
       
   390 	const TDesC8& aURI,
       
   391 	const TDesC8& aLUID,
       
   392 	TNSmlDmMapOperation aOperation )
       
   393 	{
       
   394 	iDatabase.Begin();
       
   395 	TBool insertNewEntry(EFalse);
       
   396 	if ( aOperation == EMapDelete )
       
   397 		{
       
   398 		if ( SearchRowInDbL ( aAdapterId, aURI ) == KErrNone )
       
   399 			{
       
   400 			iView.GetL();
       
   401 			iView.DeleteL();
       
   402 			iPrepareView = ETrue;
       
   403 
       
   404 			}
       
   405 		}
       
   406 	else if ( aOperation == EMapUpdate )
       
   407 		{
       
   408 		if ( SearchRowInDbL ( aAdapterId, aURI ) == KErrNone )
       
   409 			{
       
   410 			CDbColSet* colSet = iView.ColSetL();
       
   411 			CleanupStack::PushL ( colSet );
       
   412 			iView.GetL();
       
   413 			iView.UpdateL();
       
   414 			RDbColWriteStream writeStream;
       
   415 			writeStream.OpenLC ( iView, colSet->ColNo( KNSmlDmMappingLuid ));
       
   416 			writeStream.WriteL ( aLUID );
       
   417 			writeStream.CommitL();
       
   418 			CleanupStack::PopAndDestroy(); //writeStream
       
   419 			iView.PutL();
       
   420 			CleanupStack::PopAndDestroy(); //colSet
       
   421 			iPrepareView = EFalse;
       
   422 			}
       
   423 		else
       
   424 			{
       
   425 			insertNewEntry = ETrue;
       
   426 			}
       
   427 		}		
       
   428 	if ( aOperation == EMapInsert  || insertNewEntry )
       
   429 		{
       
   430 		RDbTable uriTable;
       
   431 		User::LeaveIfError ( uriTable.Open ( iDatabase,
       
   432 			KNSmlDmLuidMappingTable ) );
       
   433 			
       
   434 		CDbColSet* columns = uriTable.ColSetL();
       
   435 		CleanupStack::PushL ( columns );
       
   436 
       
   437 		uriTable.Reset();
       
   438 		uriTable.InsertL();
       
   439 		uriTable.SetColL ( columns->ColNo ( KNSmlDmAdapterId ), aAdapterId );
       
   440 		RDbColWriteStream writeStream;
       
   441 		writeStream.OpenLC ( uriTable, columns->ColNo( KNSmlDmMappingLuid ) );
       
   442 		writeStream.WriteL ( aLUID );
       
   443 		writeStream.CommitL();
       
   444 		CleanupStack::PopAndDestroy();  // writestream for Luid
       
   445 		writeStream.OpenLC ( uriTable, columns->ColNo( KNSmlDmMappingURI ) );
       
   446 		writeStream.WriteL ( aURI );
       
   447 		writeStream.CommitL();
       
   448 		CleanupStack::PopAndDestroy();  // writestream for Uri		
       
   449 		uriTable.PutL();
       
   450 		CleanupStack::PopAndDestroy(); // columns
       
   451 		uriTable.Close();
       
   452 		iPrepareView = EFalse;
       
   453 		}
       
   454 	iView.Close();	
       
   455 	iDatabase.Commit();
       
   456 	}
       
   457 
       
   458 // ---------------------------------------------------------------------------
       
   459 // TInt CNSmlDmDbHandler::SearchRowInDbL()
       
   460 // ---------------------------------------------------------------------------
       
   461 TInt CNSmlDmDbHandler::SearchRowInDbL(TUint32 aAdapterId,
       
   462 	const TDesC8& aURI )
       
   463 	{
       
   464 	TInt ret(KErrNotFound);
       
   465 	
       
   466 	if ( iPrepareView )
       
   467 		{
       
   468 		_LIT( KNSmlDmUpdateRow,
       
   469 			"SELECT * FROM LuidMappingTable WHERE AdapterId=%u" );
       
   470 			
       
   471 		TPtr sqlPtr = HBufC::NewLC( KNSmlDmUpdateRow().Length() +
       
   472 			KNSmlDmIdLength )->Des(); 
       
   473 			
       
   474 		sqlPtr.Format ( KNSmlDmUpdateRow(), aAdapterId );
       
   475 		
       
   476 		User::LeaveIfError ( iView.Prepare ( iDatabase, sqlPtr,
       
   477 			TDbWindow( TDbWindow::EUnlimited ) ) );
       
   478 			
       
   479 		User::LeaveIfError ( iView.EvaluateAll() );
       
   480 
       
   481 		CleanupStack::PopAndDestroy();  // sqlPtr
       
   482 		}
       
   483 
       
   484 	if ( iView.FirstL() )
       
   485 		{
       
   486 		do 
       
   487 			{
       
   488 			CDbColSet* colSet = iView.ColSetL();
       
   489 			CleanupStack::PushL ( colSet );
       
   490 			TDbColNo uri = colSet->ColNo ( KNSmlDmMappingURI );
       
   491 
       
   492 			iView.GetL();
       
   493 			RDbColReadStream colReadStream;
       
   494 			colReadStream.OpenLC ( iView, uri );
       
   495 			HBufC8* uri8 = HBufC8::NewLC ( iView.ColLength( uri ) );
       
   496 			TPtr8 uriPtr = uri8->Des();
       
   497 			colReadStream.ReadL ( uriPtr, iView.ColLength ( uri ) );
       
   498 			
       
   499 			if ( aURI.Compare ( uriPtr ) == 0 )
       
   500 				{
       
   501 				CleanupStack::PopAndDestroy(3);//uri8, colReadStream, colSet
       
   502 				ret = KErrNone;
       
   503 				break;
       
   504 				}
       
   505 			CleanupStack::PopAndDestroy(3);//uri8, colReadStream, colSet			
       
   506 			} while ( iView.NextL() );	
       
   507 		}
       
   508 	return ret;
       
   509 	}
       
   510 	
       
   511 
       
   512 // ---------------------------------------------------------------------------
       
   513 // TInt CNSmlDmDbHandler::SearchUriL()
       
   514 // Searches the given URI from the internal array.
       
   515 // There are four different search methods:
       
   516 // - ENormalSearch: lengths and contents must match
       
   517 // - EUsingParents: e.g. given Email/NN/XXX finds and stops to Email/NN
       
   518 // - EAmongChidren: e.g. given Email/NN finds: Email/NN/AA,
       
   519 //		Email/NN/3 and Email/NN
       
   520 // - EAmongParents: e.g. given Email/NN finds: Email/NN/AA and Email/NN/3
       
   521 //		but NOT Email/NN
       
   522 // ---------------------------------------------------------------------------
       
   523 
       
   524 TInt CNSmlDmDbHandler::SearchUriL( TUint32 aAdapterId,
       
   525 	const TDesC8& aURI,
       
   526 	TNSmlDmSearchMethod aSearchMethod,
       
   527 	TInt& aAdIndex,
       
   528 	RArray<TInt>& aUriIndex)
       
   529 	{
       
   530 	TInt found(KErrNotFound);
       
   531 	TInt i;	
       
   532 	for ( i=0; i<iAdapters.Count(); i++ )
       
   533 		{
       
   534 		if ( iAdapters[i]->iAdapterId == aAdapterId )
       
   535 			{
       
   536 			if ( aSearchMethod == ENormalSearch )
       
   537 				{
       
   538 				for ( TInt u=0; u<iAdapters[i]->iMapArray.Count(); u++ )
       
   539 					{
       
   540 					if ( aURI.Compare
       
   541 						( *iAdapters[i]->iMapArray[u]->iURI ) == 0 ) 
       
   542 						{
       
   543 						found = KErrNone;
       
   544 						aAdIndex = i;
       
   545 						aUriIndex.Append ( u );
       
   546 						break;
       
   547 						}
       
   548 					}
       
   549 				}
       
   550 			else if ( aSearchMethod == EUsingParents )
       
   551 				{
       
   552 				TPtr8 uriPtr = aURI.AllocLC()->Des();
       
   553 				while ( uriPtr.Length() > 0 && found != KErrNone )
       
   554 					{
       
   555 					for ( TInt u=0; u<iAdapters[i]->iMapArray.Count(); u++ )
       
   556 						{
       
   557 						if ( uriPtr.Compare
       
   558 							( *iAdapters[i]->iMapArray[u]->iURI ) == 0 ) 
       
   559 							{
       
   560 							found = KErrNone;
       
   561 							aAdIndex = i;
       
   562 							aUriIndex.Append ( u );
       
   563                             if( uriPtr.Compare(aURI) == 0 )
       
   564                                 {
       
   565                                 //Checks the cmd is Add or not
       
   566                                 TInt val = KErrNotFound;
       
   567                                 TInt err = RProperty::Get( KPSUidNSmlDMSyncAgent, 
       
   568                                         KNSmlDMCmdAddOnExistingNodeorLeafKey,
       
   569                                         val );      
       
   570                                 if( err == KErrNone && EAddCmd == val )//means Add cmd
       
   571                                     {           
       
   572                                     //Node exists in DB, set P&S key to EAddOnExistingNode 
       
   573                                     RProperty::Set( KPSUidNSmlDMSyncAgent,
       
   574                                             KNSmlDMCmdAddOnExistingNodeorLeafKey,
       
   575                                             EAddOnExistingNode );
       
   576                                     }
       
   577                                 }
       
   578 							}
       
   579 						}
       
   580 					uriPtr = NSmlDmURI::RemoveLastSeg ( uriPtr );
       
   581 					}
       
   582 				CleanupStack::PopAndDestroy(); //uriPtr
       
   583 				}
       
   584 			else if ( aSearchMethod == EAmongChildren )
       
   585 				{
       
   586 				for ( TInt u=0; u<iAdapters[i]->iMapArray.Count(); u++ )
       
   587 					{
       
   588 					if ( iAdapters[i]->iMapArray[u]->iURI->Find( aURI ) == 0 )
       
   589 						{
       
   590 						found = KErrNone;
       
   591 						aAdIndex = i;
       
   592 						aUriIndex.Append ( u );
       
   593 						}
       
   594 					}				
       
   595 				}
       
   596 			else if ( aSearchMethod == EAmongParents )
       
   597 				{
       
   598 				for ( TInt u=0; u<iAdapters[i]->iMapArray.Count(); u++ )
       
   599 					{
       
   600 					if ( aURI.Compare (NSmlDmURI::ParentURI(
       
   601 						*iAdapters[i]->iMapArray[u]->iURI )  ) == 0 )
       
   602 						{
       
   603 						found = KErrNone;
       
   604 						aAdIndex = i;
       
   605 						aUriIndex.Append ( u );
       
   606 						}
       
   607 					}				
       
   608 				}
       
   609 			break;
       
   610 			}	
       
   611 		}
       
   612 	return found;
       
   613 	}
       
   614 
       
   615 // ---------------------------------------------------------------------------
       
   616 // TInt CNSmlDmDbHandler::GetMappingInfoL()
       
   617 // Get the mapping info either by reading first mappings from db or 
       
   618 // directly from member variables.
       
   619 // ---------------------------------------------------------------------------
       
   620 EXPORT_C TInt CNSmlDmDbHandler::GetMappingInfoLC(TUint32 aAdapterId,
       
   621 	const TDesC8& aURI,	HBufC8*& aLuid )
       
   622 	{
       
   623 	_DBG_FILE("CNSmlDmDbHandler::GetMappingInfoL() : begin");
       
   624 	if ( !iURIMappingsReadFromDb )
       
   625 		{
       
   626 		ReadAllURIMappingsFromDbL();
       
   627 		iURIMappingsReadFromDb = ETrue;
       
   628 		}
       
   629 		
       
   630 	TInt adIndex(0);
       
   631 	RArray<TInt> uriIndex;
       
   632 	TInt found = SearchUriL( aAdapterId, aURI, EUsingParents,
       
   633 		adIndex, uriIndex );
       
   634 		
       
   635 	if ( found == KErrNone )
       
   636 		{
       
   637 		aLuid = iAdapters[adIndex]->iMapArray[uriIndex[0]]->iLUID->AllocLC();
       
   638 		}
       
   639 	else
       
   640 		{
       
   641 		aLuid = KNullDesC8().AllocLC();		
       
   642 		}
       
   643 	uriIndex.Reset();
       
   644 	_DBG_FILE("CNSmlDmDbHandler::GetMappingInfoL() : end");
       
   645 	return found;
       
   646 	}
       
   647 
       
   648 // ---------------------------------------------------------------------------
       
   649 // TInt CNSmlDmDbHandler::AddMappingInfoL ( TUint32 aAdapterId,
       
   650 // const TDesC8& aURI, const TDesC8& aLuid )
       
   651 // Adds the  mapping info to member variables, if it doesn't already exist.
       
   652 // ---------------------------------------------------------------------------
       
   653 EXPORT_C TInt CNSmlDmDbHandler::AddMappingInfoL ( TUint32 aAdapterId,
       
   654 	const TDesC8& aURI, const TDesC8& aLuid )
       
   655 	{
       
   656 	_DBG_FILE("CNSmlDmDbHandler::AddMappingInfoL() : begin");
       
   657 	if (SysUtil::FFSSpaceBelowCriticalLevelL ( &iFsSession,
       
   658 		aURI.Size() + aLuid.Size() ) ) 
       
   659 		{
       
   660 		User::Leave(KErrDiskFull);
       
   661 		}
       
   662 
       
   663 	if ( !iURIMappingsReadFromDb )
       
   664 		{
       
   665 		ReadAllURIMappingsFromDbL();
       
   666 		iURIMappingsReadFromDb = ETrue;
       
   667 		}
       
   668 
       
   669 	UpdateInternalArrayL ( aAdapterId, aURI, aLuid,
       
   670 		EMapInsert, ENormalSearch );
       
   671 	
       
   672 	_DBG_FILE("CNSmlDmDbHandler::AddMappingInfoL() : end");
       
   673 	return KErrNone;
       
   674 	}
       
   675 
       
   676 
       
   677 // ---------------------------------------------------------------------------
       
   678 // TInt CNSmlDmDbHandler::RemoveMappingInfoL ( TUint32 aAdapterId,
       
   679 //		const TDesC8& aURI, TBool aChildAlso )
       
   680 // Removes mapping info from member variables
       
   681 // ---------------------------------------------------------------------------
       
   682 EXPORT_C TInt CNSmlDmDbHandler::RemoveMappingInfoL ( TUint32 aAdapterId,
       
   683 	const TDesC8& aURI,	TBool aChildAlso )
       
   684 	{
       
   685 	_DBG_FILE("CNSmlDmDbHandler::RemoveMappingInfoL() : begin");
       
   686 	if ( !iURIMappingsReadFromDb )
       
   687 		{
       
   688 		ReadAllURIMappingsFromDbL();
       
   689 		iURIMappingsReadFromDb = ETrue;
       
   690 		}
       
   691 
       
   692 	//delete mapping items if found
       
   693 	TNSmlDmSearchMethod method =
       
   694 		( aChildAlso ) ? EAmongChildren : ENormalSearch;
       
   695 		
       
   696 	HBufC8* luid = HBufC8::NewLC(0);
       
   697 	UpdateInternalArrayL ( aAdapterId, aURI, *luid, EMapDelete, method );
       
   698 	CleanupStack::PopAndDestroy();  // luid
       
   699 
       
   700 	_DBG_FILE("CNSmlDmDbHandler::RemoveMappingInfoL() : end");
       
   701 	return KErrNone;
       
   702 	}
       
   703 
       
   704 
       
   705 // ---------------------------------------------------------------------------
       
   706 // TInt CNSmlDmDbHandler::RenameMappingL ( TUint32 aAdapterId,
       
   707 //		const TDesC8& aURI, const TDesC8& aObject )
       
   708 // ---------------------------------------------------------------------------
       
   709 EXPORT_C TInt CNSmlDmDbHandler::RenameMappingL ( TUint32 aAdapterId,
       
   710 	const TDesC8& aURI, const TDesC8& aObject )
       
   711 	{
       
   712 	_DBG_FILE("CNSmlDmDbHandler::RenameMappingL() : begin");
       
   713 	TInt ret = KErrNotFound;
       
   714 
       
   715 	if ( !iURIMappingsReadFromDb )
       
   716 		{
       
   717 		ReadAllURIMappingsFromDbL();
       
   718 		iURIMappingsReadFromDb = ETrue;
       
   719 		}
       
   720 
       
   721 	if ( aObject.Compare ( NSmlDmURI::LastURISeg ( aURI ) ) == 0 )
       
   722 		{
       
   723 		return KErrNone;
       
   724 		}
       
   725 
       
   726 	TPtr8 newUriPtr = HBufC8::NewLC( NSmlDmURI::RemoveLastSeg(aURI).Length() +
       
   727 		aObject.Length() + 1 )->Des();
       
   728 		
       
   729 	newUriPtr.Copy ( NSmlDmURI::RemoveLastSeg(aURI) );
       
   730 	newUriPtr.Append ( KNSmlDmLitSeparator );
       
   731 	newUriPtr.Append ( aObject );
       
   732 	
       
   733 	//Does the renamed uri already exist..
       
   734 	TInt adIndex(0);
       
   735 	RArray<TInt> uriIndex;
       
   736 	if ( SearchUriL( aAdapterId, newUriPtr, EAmongChildren, adIndex,
       
   737 		uriIndex ) == KErrNone )
       
   738 		{
       
   739 		ret = KErrAlreadyExists;
       
   740 		}
       
   741 	else 
       
   742 		{
       
   743 		adIndex = 0;
       
   744 		uriIndex.Reset();		
       
   745 		if ( SearchUriL( aAdapterId, aURI, ENormalSearch, adIndex,
       
   746 			uriIndex ) == KErrNone )
       
   747 			{
       
   748 			HBufC8* luid = HBufC8::NewLC(0);
       
   749 			
       
   750 			UpdateInternalArrayL ( aAdapterId, aURI, *luid, EMapRename,
       
   751 				EAmongChildren, newUriPtr );
       
   752 				
       
   753 			CleanupStack::PopAndDestroy();  // luid
       
   754 			RenameAclL ( aURI, newUriPtr );
       
   755 			ret = KErrNone;
       
   756 			}		
       
   757 		}
       
   758 	uriIndex.Reset();
       
   759 	CleanupStack::PopAndDestroy();  // newUriPtr
       
   760 	_DBG_FILE("CNSmlDmDbHandler::RenameMappingL() : end");
       
   761 	return ret;
       
   762 	}
       
   763 
       
   764 
       
   765 // ---------------------------------------------------------------------------
       
   766 // TInt CNSmlDmDbHandler::GetURISegmentListL ( TUint32 aAdapterId,
       
   767 //	const TDesC8& aURI,
       
   768 //	CArrayFix<TNSmlDmMappingInfo>& aPreviousURISegmentList)
       
   769 // Gets child uri list of aURI.
       
   770 // ---------------------------------------------------------------------------
       
   771 EXPORT_C TInt CNSmlDmDbHandler::GetURISegmentListL ( TUint32 aAdapterId,
       
   772 	const TDesC8& aURI, CArrayFix<TSmlDmMappingInfo>& aURISegList)
       
   773 	{
       
   774 	_DBG_FILE("CNSmlDmDbHandler::GetURISegmentListL() : begin");
       
   775 	TInt found(KErrNone);
       
   776 	if ( !iURIMappingsReadFromDb )
       
   777 		{
       
   778 		ReadAllURIMappingsFromDbL();
       
   779 		iURIMappingsReadFromDb = ETrue;
       
   780 		}
       
   781 	TInt adIndex;
       
   782 	RArray<TInt> uriIndex;
       
   783 	found = SearchUriL ( aAdapterId, aURI, EAmongParents, adIndex, uriIndex );
       
   784 	TSmlDmMappingInfo mappingInfo;
       
   785 	for ( TInt i=0; i<uriIndex.Count(); i++ )
       
   786 		{
       
   787 		mappingInfo.iURISeg = NSmlDmURI::LastURISeg( *iAdapters[adIndex]->
       
   788 			iMapArray[uriIndex[i]]->iURI );
       
   789 			
       
   790 		mappingInfo.iURISegLUID.Set( *iAdapters[adIndex]->
       
   791 			iMapArray[uriIndex[i]]->iLUID );
       
   792 			
       
   793 		aURISegList.AppendL ( mappingInfo );
       
   794 		}
       
   795 	uriIndex.Reset();
       
   796 	_DBG_FILE("CNSmlDmDbHandler::GetURISegmentListL() : end");
       
   797 //	return KErrNone;
       
   798 	return found;
       
   799 	}
       
   800 
       
   801 
       
   802 // ---------------------------------------------------------------------------
       
   803 // TInt CNSmlDmDbHandler::WriteMappingInfoToDbL()
       
   804 // Write mapping info from member variables to db.
       
   805 // ---------------------------------------------------------------------------
       
   806 EXPORT_C TInt CNSmlDmDbHandler::WriteMappingInfoToDbL()
       
   807 	{
       
   808 	_DBG_FILE("CNSmlDmDbHandler::WriteMappingInfoToDbL() : begin");	
       
   809 	TBool dbUpdated(EFalse);
       
   810 	iDatabase.OpenL ( iFileStore, iFileStore->Root() );
       
   811 	
       
   812 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   813 		{
       
   814 		iPrepareView = ETrue;
       
   815 		for ( TInt u=0; u<iAdapters[i]->iMapArray.Count(); u++ )
       
   816 			{
       
   817 			if ( iAdapters[i]->iMapArray[u]->iOperation != EMapNoAction )
       
   818 				{
       
   819 				UpdateRowInDbL ( iAdapters[i]->iAdapterId,
       
   820 					*iAdapters[i]->iMapArray[u]->iURI,
       
   821 					*iAdapters[i]->iMapArray[u]->iLUID,
       
   822 					iAdapters[i]->iMapArray[u]->iOperation );
       
   823 					
       
   824 				if ( iAdapters[i]->iMapArray[u]->iOperation == EMapDelete )
       
   825 					{
       
   826 					delete iAdapters[i]->iMapArray[u];
       
   827 					iAdapters[i]->iMapArray.Remove(u);
       
   828 					u--;				
       
   829 					}
       
   830 				else
       
   831 					{
       
   832 					iAdapters[i]->iMapArray[u]->iOperation = EMapNoAction;					
       
   833 					}
       
   834 				dbUpdated = ETrue;								
       
   835 				}
       
   836 			}
       
   837 		}
       
   838 	if ( dbUpdated )
       
   839 		{
       
   840 		iView.Close();
       
   841 		iDatabase.Compact();
       
   842 		}
       
   843 	iDatabase.Close();
       
   844 	_DBG_FILE("CNSmlDmDbHandler::WriteMappingInfoToDbL() : end");		
       
   845 	return KErrNone;		
       
   846 	}
       
   847 
       
   848 
       
   849 // ---------------------------------------------------------------------------
       
   850 // TInt CNSmlDmDbHandler::UpdateMappingInfoL ( TUint32 aAdapterId,
       
   851 //		const TDesC8& aURI, CBufBase &aCurrentList )
       
   852 // Updates mapping info. 
       
   853 // ---------------------------------------------------------------------------
       
   854 EXPORT_C void CNSmlDmDbHandler::UpdateMappingInfoL(TUint32 aAdapterId,
       
   855 	const TDesC8& aURI,
       
   856 	CBufBase &aCurrentList)
       
   857 	{
       
   858 	_DBG_FILE("CNSmlDmDbHandler::UpdateMappingInfoL() : begin");
       
   859 
       
   860 	if ( !iURIMappingsReadFromDb )
       
   861 		{
       
   862 		ReadAllURIMappingsFromDbL();
       
   863 		iURIMappingsReadFromDb = ETrue;
       
   864 		}
       
   865 	
       
   866 	TInt startPos(0);
       
   867 	TInt segEnds(0);
       
   868 	RPointerArray<HBufC8> currentUris;
       
   869 	CleanupStack::PushL ( PtrArrCleanupItemRArr ( HBufC8, &currentUris ) );
       
   870 	
       
   871 	while ( startPos < ( aCurrentList.Size()  ) )
       
   872 		{
       
   873 		TPtrC8 tmpDesc = aCurrentList.Ptr(startPos);
       
   874 		segEnds = tmpDesc.Find ( KNSmlDmLitSeparator );
       
   875 		if ( segEnds == KErrNotFound )
       
   876 		    {
       
   877 			segEnds = tmpDesc.Length();
       
   878 		    }
       
   879 		
       
   880 		HBufC8* childUri = HBufC8::NewL ( aURI.Length() +
       
   881 			KNSmlDmLitSeparator().Length() + segEnds );
       
   882 			
       
   883 		TPtr8 childUriPtr = childUri->Des();
       
   884 		childUriPtr.Append ( aURI );
       
   885 		childUriPtr.Append ( KNSmlDmLitSeparator );
       
   886 		childUriPtr.Append ( tmpDesc.Left ( segEnds ) );	
       
   887 		currentUris.Append ( childUri );
       
   888 		startPos += segEnds + 1;
       
   889 		}
       
   890 	
       
   891 	// Search all the uris which have the parent uri matching with the given aURI 
       
   892 	TBool found(EFalse);
       
   893 	TInt adIndex(0);
       
   894 	RArray<TInt> uriIndex;
       
   895 	SearchUriL ( aAdapterId, aURI, EAmongChildren, adIndex, uriIndex );
       
   896 	
       
   897 	for ( TInt i(0); i<uriIndex.Count(); i++ )
       
   898 		{
       
   899 		for ( TInt u(0); u<currentUris.Count(); u++ )
       
   900 			{
       
   901 			if ( iAdapters[adIndex]->iMapArray[uriIndex[i]]->iURI->
       
   902 				Compare ( *currentUris[u] ) >= 0  ||
       
   903 				iAdapters[adIndex]->iMapArray[uriIndex[i]]->iURI->Length() <=
       
   904 				( aURI.Length() + 1 ) )
       
   905 				{
       
   906 				found = ETrue;
       
   907 				break;
       
   908 				}
       
   909 			}
       
   910 		if ( !found )
       
   911 			{
       
   912 			// Mark deleted all the uris which have different child uri
       
   913 			iAdapters[adIndex]->iMapArray[uriIndex[i]]->iOperation =
       
   914 				EMapDelete;
       
   915 				
       
   916 			DeleteAclL ( *iAdapters[adIndex]->iMapArray[uriIndex[i]]->iURI );			
       
   917 			}
       
   918 		found = EFalse;
       
   919 		}
       
   920 	
       
   921 	uriIndex.Reset();
       
   922 	CleanupStack::PopAndDestroy();  // currentUris
       
   923 	
       
   924 	_DBG_FILE("CNSmlDmDbHandler::UpdateMappingInfoL() : end");
       
   925 	}
       
   926 
       
   927 // ---------------------------------------------------------------------------
       
   928 // CNSmlDmDbHandler::DeleteAclL()
       
   929 // Delete Acl information belongin to the aURI
       
   930 // ---------------------------------------------------------------------------
       
   931 EXPORT_C TInt CNSmlDmDbHandler::DeleteAclL(const TDesC8& aURI)
       
   932 	{
       
   933 	GetAclDataFromDbL();
       
   934 
       
   935 	for(TInt i=0;i<iAclURIs.Count();i++)
       
   936 		{
       
   937 		if(iAclURIs[i]->Find(aURI)==0)
       
   938 			{
       
   939 			delete iAclURIs[i];
       
   940 			iAclURIs.Remove(i);
       
   941 			delete iAcls[i];
       
   942 			iAcls.Remove(i);
       
   943 			i--;
       
   944 			}
       
   945 		}
       
   946 	WriteAclInfoToDbL();
       
   947 
       
   948 	return 0;
       
   949 	}
       
   950 
       
   951 // ---------------------------------------------------------------------------
       
   952 // CNSmlDmDbHandler::UpdateAclL()
       
   953 // Update Acl information
       
   954 // ---------------------------------------------------------------------------
       
   955 EXPORT_C TInt CNSmlDmDbHandler::UpdateAclL(const TDesC8& aURI,
       
   956 	const TDesC8& aACL)
       
   957 	{
       
   958 	if (SysUtil::FFSSpaceBelowCriticalLevelL(&iFsSession,
       
   959 		aURI.Size()+aACL.Size()))
       
   960 		{
       
   961 		User::Leave(KErrDiskFull);
       
   962 		}
       
   963 
       
   964 	if(aURI.Length()==0||aURI.Compare(KNSmlDmRootUri)==0)
       
   965 		{
       
   966 		if(!(aACL.Find(KNSmlDmAclAddForAll)==KErrNone||
       
   967 			aACL.Compare(KNSmlDmAclAll)==0))
       
   968 			{
       
   969 			return KErrAccessDenied;
       
   970 			}
       
   971 		}
       
   972 	if(aACL.Locate(TChar(KNSmlDMAclUriSeparator))>=0)
       
   973 		{
       
   974 		return KErrCorrupt;
       
   975 		}
       
   976 	GetAclDataFromDbL();
       
   977 
       
   978 	HBufC8* uri;
       
   979 	if(aURI.Length()==0)
       
   980 		{
       
   981 		uri = HBufC8::NewLC( 1 );
       
   982 		uri->Des().Format(KNSmlDmRootUri);
       
   983 		}
       
   984 	else
       
   985 		{
       
   986 		uri = aURI.AllocLC();
       
   987 		}
       
   988 	TPtr8 uriPtr = uri->Des();
       
   989 
       
   990 
       
   991 	TInt uriIndex = KErrNotFound;
       
   992 
       
   993 	//check if aURI allready exists
       
   994 	for(TInt i=0;i<iAclURIs.Count();i++)
       
   995 		{
       
   996 		if(iAclURIs[i]->Compare(uriPtr)==0)
       
   997 			{
       
   998 			uriIndex=i;
       
   999 			break;
       
  1000 			}
       
  1001 		}
       
  1002 
       
  1003 	if(uriIndex!=KErrNotFound)
       
  1004 		{
       
  1005 		delete iAclURIs[uriIndex];
       
  1006 		iAclURIs.Remove(uriIndex);
       
  1007 		delete iAcls[uriIndex];
       
  1008 		iAcls.Remove(uriIndex);
       
  1009 		}
       
  1010 
       
  1011 
       
  1012 	if(aACL.Length()>0&&aACL.Compare(KNSmlDmLitAclNull)!=0)
       
  1013 		{
       
  1014 		HBufC8* acl = HBufC8::NewLC(aACL.Length() );
       
  1015 		TPtr8 aclPtr = acl->Des();
       
  1016 		aclPtr.Format(aACL);
       
  1017 		
       
  1018 		CleanupStack::Pop(); // acl
       
  1019 		iAcls.AppendL(acl);
       
  1020 		CleanupStack::Pop(); // uri
       
  1021 		iAclURIs.AppendL(uri);
       
  1022 		}
       
  1023 	else
       
  1024 		{
       
  1025 		CleanupStack::PopAndDestroy(); //uri
       
  1026 		}
       
  1027 
       
  1028 	WriteAclInfoToDbL();
       
  1029 	return KErrNone;
       
  1030 	}
       
  1031 	
       
  1032 // ---------------------------------------------------------------------------
       
  1033 // CNSmlDmDbHandler::GetAclL()
       
  1034 // Get the acl belonging to aURI
       
  1035 // ---------------------------------------------------------------------------
       
  1036 EXPORT_C TInt CNSmlDmDbHandler::GetAclL(const TDesC8& aURI,
       
  1037 	CBufBase& aACL,
       
  1038 	TBool aInherited)
       
  1039 	{
       
  1040 	_DBG_FILE("CNSmlDmDbHandler::GetAclL() : begin");
       
  1041 
       
  1042 	GetAclDataFromDbL();
       
  1043 
       
  1044 	TInt ret(KErrNone);
       
  1045 	HBufC8* uri;
       
  1046 	if(aURI.Length()==0)
       
  1047 		{
       
  1048 		uri = HBufC8::NewLC( 1 );
       
  1049 		uri->Des().Format(KNSmlDmRootUri);
       
  1050 		}
       
  1051 	else
       
  1052 		{
       
  1053 		uri = aURI.AllocLC();
       
  1054 		}
       
  1055 	TPtr8 uriPtr = uri->Des();
       
  1056 
       
  1057 	if(iAcls.Count()==0||iAclURIs.Count()==0)
       
  1058 		{
       
  1059 		if(aInherited||uriPtr.Compare(KNSmlDmRootUri)==0)
       
  1060 			{
       
  1061 			//no ACL info in database, return default ACL
       
  1062 			aACL.InsertL(0,KNSmlDmAclDefaultRoot);
       
  1063 			ret = KErrNone;
       
  1064 			}
       
  1065 		}
       
  1066 	else
       
  1067 		{
       
  1068 		TBool found = EFalse;
       
  1069 		ret = KErrNotFound;
       
  1070 		//acl is inherited to childs also, in this loop all the parents
       
  1071 		//are checked
       
  1072 		while(uriPtr.Length()!=0 && !found)
       
  1073 			{
       
  1074 			for(TInt i=0;i<iAclURIs.Count();i++)
       
  1075 				{
       
  1076 				if(uriPtr.Compare(iAclURIs[i]->Des())==0)
       
  1077 					{
       
  1078 					//acl belonging to aURI or to any parent is found and
       
  1079 					//returned
       
  1080 					found = ETrue;
       
  1081 					aACL.InsertL(0,iAcls[i]->Des());
       
  1082 					ret = KErrNone;
       
  1083 					break;
       
  1084 					}
       
  1085 				}
       
  1086 			if(found||!aInherited)
       
  1087 				{
       
  1088 				break;
       
  1089 				}
       
  1090 			uriPtr.Format(NSmlDmURI::RemoveLastSeg(uriPtr));
       
  1091 			} //end while
       
  1092 
       
  1093 		if(!found&&(aInherited||uriPtr.Compare(KNSmlDmRootUri)==0))
       
  1094 			{
       
  1095 			TBool rootAclFound = EFalse;
       
  1096 			//check if root root acl has changed
       
  1097 			uriPtr.Format(KNSmlDmRootUri);
       
  1098 			for(TInt i=0;i<iAclURIs.Count();i++)
       
  1099 				{
       
  1100 				if(uriPtr.Compare(*iAclURIs[i])==0)
       
  1101 					{
       
  1102 					//acl belonging to root
       
  1103 					rootAclFound = ETrue;
       
  1104 					aACL.InsertL(0,*iAcls[i]);
       
  1105 					ret = KErrNone;
       
  1106 					break;
       
  1107 					}
       
  1108 				}
       
  1109 			if(!rootAclFound)
       
  1110 				{
       
  1111 				//acl not found, return  default
       
  1112 				ret = KErrNone;
       
  1113 				aACL.InsertL(0,KNSmlDmAclDefaultRoot);
       
  1114 				}
       
  1115 			}
       
  1116 		}
       
  1117 	CleanupStack::PopAndDestroy(); //uri
       
  1118 
       
  1119 	_DBG_FILE("CNSmlDmDbHandler::GetAclL() : end");
       
  1120 	return ret;
       
  1121 	}
       
  1122 
       
  1123 
       
  1124 // ---------------------------------------------------------------------------
       
  1125 // TInt CNSmlDmDbHandler::WriteAclInfoToDbL()
       
  1126 // Updates the acl info to database
       
  1127 // ---------------------------------------------------------------------------
       
  1128 EXPORT_C TInt CNSmlDmDbHandler::WriteAclInfoToDbL()
       
  1129 	{
       
  1130 	_DBG_FILE("CNSmlDmDbHandler::WriteAclInfoToDbL() : begin");
       
  1131 
       
  1132 	TInt ret = KErrNone;
       
  1133 	if(!iAclUpToDate)
       
  1134 		{
       
  1135 		_DBG_FILE("CNSmlDmDbHandler::WriteAclInfoToDbL() : end (upToDate)");
       
  1136 		return ret;
       
  1137 		}
       
  1138 	if(iAclURIs.Count()!=iAcls.Count())
       
  1139 		{
       
  1140 		_DBG_FILE("CNSmlDmDbHandler::WriteAclInfoToDbL(): end (ACL Count");
       
  1141 		User::Leave(KErrGeneral);
       
  1142 		}
       
  1143 
       
  1144 
       
  1145 	_LIT(KNSmlDmSQLStatement, "select * from AclTable where AdapterId = %d");
       
  1146 	TInt pushed=0;
       
  1147 
       
  1148 	iDatabase.OpenL ( iFileStore, iFileStore->Root() );
       
  1149 
       
  1150 	HBufC8* aclURIs=KNullDesC8().AllocLC();
       
  1151 	pushed++;
       
  1152 
       
  1153 	TInt i;	
       
  1154 	//write aclURIs from array to HBufC*
       
  1155 	for(i=0;i<iAclURIs.Count();i++)
       
  1156 		{
       
  1157 		CleanupStack::Pop();
       
  1158 		aclURIs = aclURIs->ReAllocL( aclURIs->Length() +
       
  1159 			iAclURIs[i]->Length() + 1 );
       
  1160 		CleanupStack::PushL(aclURIs);
       
  1161 		aclURIs->Des().Append( *iAclURIs[i] );
       
  1162 		aclURIs->Des().Append( KNSmlDmLitMappingSeparator16 );
       
  1163 		}
       
  1164 
       
  1165 	HBufC8* acls=KNullDesC8().AllocLC();
       
  1166 	pushed++;
       
  1167 	//write acls from array to HBufC*
       
  1168 	for(i = 0;i<iAcls.Count();i++)
       
  1169 		{
       
  1170 		CleanupStack::Pop();
       
  1171 		acls = acls->ReAllocL( acls->Length() + iAcls[i]->Length() + 1 );
       
  1172 		CleanupStack::PushL(acls);
       
  1173 		acls->Des().Append( *iAcls[i] );
       
  1174 		acls->Des().Append(TChar(KNSmlDMAclUriSeparator));
       
  1175 		}
       
  1176 	
       
  1177 	HBufC* sql = HBufC::NewLC( KNSmlDmSQLStatement().Length()+10);
       
  1178 	pushed++;
       
  1179 	TPtr sqlPtr = sql->Des();
       
  1180 	sqlPtr.Format( KNSmlDmSQLStatement, KNSmlAclDbId );
       
  1181 
       
  1182 	ret = iView.Prepare(iDatabase,TDbQuery(sqlPtr,EDbCompareNormal));
       
  1183 
       
  1184 	CDbColSet* colSet = iView.ColSetL();
       
  1185 	CleanupStack::PushL(colSet);
       
  1186 	pushed++;
       
  1187 	User::LeaveIfError(iView.EvaluateAll());
       
  1188 
       
  1189 	if(ret==KErrNone)
       
  1190 		{
       
  1191 		iView.FirstL();
       
  1192 		if(iView.AtRow())
       
  1193 			{
       
  1194 			iView.GetL();
       
  1195 			iView.UpdateL();
       
  1196 			}
       
  1197 		else
       
  1198 			{
       
  1199 			iView.InsertL();
       
  1200 			iView.SetColL(colSet->ColNo(KNSmlDmAdapterId), KNSmlAclDbId );
       
  1201 			}
       
  1202 		}
       
  1203 
       
  1204 	RDbColWriteStream out[2];
       
  1205 	TDbColNo col[2];
       
  1206 	//write data to db
       
  1207 	for(i=0;i<2;i++)
       
  1208 		{
       
  1209 		switch(i)
       
  1210 			{
       
  1211 			case 0:
       
  1212 			col[i] = colSet->ColNo(KNSmlDmAclURI);
       
  1213 			out[i].OpenL(iView,col[i]);
       
  1214 			CleanupClosePushL(out[i]);
       
  1215 			out[i].WriteL(*aclURIs);
       
  1216 			break;
       
  1217 
       
  1218 			case 1:
       
  1219 			col[i] = colSet->ColNo(KNSmlDmAcl);
       
  1220 			out[i].OpenL(iView,col[i]);
       
  1221 			CleanupClosePushL(out[i]);
       
  1222 			out[i].WriteL(*acls);
       
  1223 			break;
       
  1224 			
       
  1225 			default:
       
  1226 			User::Panic(KSmlDmTreeDbHandlerPanic,KErrArgument);
       
  1227 			break;
       
  1228 			}
       
  1229 		out[i].CommitL();
       
  1230 		CleanupStack::PopAndDestroy(); //out[i]
       
  1231 		} //for
       
  1232 
       
  1233 	iView.PutL();
       
  1234 	iView.Close();
       
  1235 	iDatabase.Compact();
       
  1236 	iDatabase.Close();
       
  1237 	
       
  1238 	//store, colSet, adapters, versions, acls, aclURis
       
  1239 	CleanupStack::PopAndDestroy(pushed);
       
  1240 	
       
  1241 	_DBG_FILE("CNSmlDmDbHandler::WriteAclInfoToDbL() : end");
       
  1242 	return ret;
       
  1243 	}
       
  1244 
       
  1245 
       
  1246 // ---------------------------------------------------------------------------
       
  1247 // TInt CNSmlDmDbHandler::CheckAclL()
       
  1248 // Check if server has the acl rigths to current uri
       
  1249 // ---------------------------------------------------------------------------
       
  1250 EXPORT_C TBool CNSmlDmDbHandler::CheckAclL(const TDesC8& aURI,
       
  1251 	TNSmlDmCmdType aCmdType)
       
  1252 	{
       
  1253 	_DBG_FILE("CNSmlDmDbHandler::CheckAclL() : begin");
       
  1254 	if(!iServer)
       
  1255 		{
       
  1256 		return EFalse;
       
  1257 		}
       
  1258 
       
  1259 	GetAclDataFromDbL();
       
  1260 	
       
  1261 	HBufC8* uri;
       
  1262 	if(aURI.Length()==0)
       
  1263 		{
       
  1264 		uri = HBufC8::NewLC( 1 );
       
  1265 		uri->Des().Format(KNSmlDmRootUri);
       
  1266 		}
       
  1267 	else
       
  1268 		{
       
  1269 		uri = aURI.AllocLC();
       
  1270 		}
       
  1271 	TPtr8 uriPtr = uri->Des();
       
  1272 
       
  1273 	TBool found = EFalse;
       
  1274 	TBool ret = EFalse;
       
  1275 	//check if acl is set to this uri or any of it's parents
       
  1276 	while(uriPtr.Length()!=0 && !found)
       
  1277 		{
       
  1278 		for(TInt i=0;i<iAcls.Count();i++)
       
  1279 			{
       
  1280 			if(uriPtr.Compare(*iAclURIs[i])==0)
       
  1281 				{
       
  1282 				found = ETrue;
       
  1283 				break;
       
  1284 				}
       
  1285 			}
       
  1286 		if(found)
       
  1287 			{
       
  1288 			break;
       
  1289 			}
       
  1290 		uriPtr.Format(NSmlDmURI::RemoveLastSeg(uriPtr));
       
  1291 		}
       
  1292 
       
  1293 	//check if root acl has been changed
       
  1294 	if(!found)
       
  1295 		{
       
  1296 		uriPtr.Format(KNSmlDmRootUri);
       
  1297 		for(TInt i=0;i<iAcls.Count();i++)
       
  1298 			{
       
  1299 			if(uriPtr.Compare(*iAclURIs[i])==0)
       
  1300 				{
       
  1301 				found = ETrue;
       
  1302 				break;
       
  1303 				}
       
  1304 			}
       
  1305 		}
       
  1306 	
       
  1307 	if(found)
       
  1308 		{
       
  1309 		//acl is found from db
       
  1310 		CBufBase* acl;
       
  1311 		acl = CBufFlat::NewL(1);
       
  1312 		CleanupStack::PushL(acl);
       
  1313 
       
  1314 		GetAclL(uriPtr,*acl);
       
  1315 
       
  1316 		TPtr8 aclPtr = acl->Ptr(0);
       
  1317 		
       
  1318 		CNSmlDmACLParser* aclParser = CNSmlDmACLParser::NewLC();
       
  1319 		aclParser->ParseL(aclPtr);
       
  1320 		ret = aclParser->HasRights(*iServer,(TNSmlDmCmdType)aCmdType);
       
  1321 		CleanupStack::PopAndDestroy(2);//aclParser, acl
       
  1322 		} //end if(found)
       
  1323 	else
       
  1324 		{
       
  1325 		//acls not found, use default
       
  1326 		switch(aCmdType)
       
  1327 			{
       
  1328 			case EAclAdd:
       
  1329 				ret = ETrue;
       
  1330 				break;
       
  1331 
       
  1332 			case EAclGet:
       
  1333 				ret = ETrue;
       
  1334 				break;
       
  1335 
       
  1336 			case EAclReplace:
       
  1337 				ret = EFalse;
       
  1338 				break;
       
  1339 
       
  1340 			case EAclDelete:
       
  1341 				ret = EFalse;
       
  1342 				break;
       
  1343 
       
  1344 			case EAclExecute:
       
  1345 				ret = ETrue;
       
  1346 				break;
       
  1347 
       
  1348 			default:
       
  1349 				User::Panic(KSmlDmTreeDbHandlerPanic,KErrArgument);
       
  1350 				break;
       
  1351 
       
  1352 			}
       
  1353 		} //end else 
       
  1354 
       
  1355 
       
  1356 	CleanupStack::PopAndDestroy(); //uri
       
  1357 
       
  1358 	_DBG_FILE("CNSmlDmDbHandler::CheckAclL() : end");
       
  1359 	return ret;
       
  1360 	}
       
  1361 
       
  1362 
       
  1363 // ---------------------------------------------------------------------------
       
  1364 // TInt CNSmlDmDbHandler::DefaultACLsToServerL()
       
  1365 // Add default acl values to current server
       
  1366 // ---------------------------------------------------------------------------
       
  1367 EXPORT_C TInt CNSmlDmDbHandler::DefaultACLsToServerL( const TDesC8& aURI)
       
  1368 	{
       
  1369 	_DBG_FILE("CNSmlDmDbHandler::DefaultACLsToServerL() : begin");
       
  1370 	TPtr8 serverPtr = iServer->Des();
       
  1371 
       
  1372 	const TInt length = 5*serverPtr.Length()+KNSmlDmAclAddEqual().Length() +
       
  1373 		KNSmlDmAclGetEqual().Length() +
       
  1374 		KNSmlDmAclReplaceEqual().Length() +
       
  1375 		KNSmlDmAclDeleteEqual().Length() +
       
  1376 		KNSmlDmAclExecEqual().Length() + 4;
       
  1377 		
       
  1378 	HBufC8 *defaultACL = HBufC8::NewLC(length);
       
  1379 	TPtr8 defaultACLptr = defaultACL->Des();
       
  1380 	defaultACLptr.Format(KNSmlDmAclAddEqual);
       
  1381 	defaultACLptr.Append(serverPtr);
       
  1382 	defaultACLptr.Append(KNSmlDmAclSeparator);
       
  1383 	defaultACLptr.Append(KNSmlDmAclDeleteEqual);
       
  1384 	defaultACLptr.Append(serverPtr);
       
  1385 	defaultACLptr.Append(KNSmlDmAclSeparator);
       
  1386 	defaultACLptr.Append(KNSmlDmAclReplaceEqual);
       
  1387 	defaultACLptr.Append(serverPtr);
       
  1388 	defaultACLptr.Append(KNSmlDmAclSeparator);
       
  1389 	defaultACLptr.Append(KNSmlDmAclGetEqual);
       
  1390 	defaultACLptr.Append(serverPtr);
       
  1391 	defaultACLptr.Append(KNSmlDmAclSeparator);
       
  1392 	defaultACLptr.Append(KNSmlDmAclExecEqual);
       
  1393 	defaultACLptr.Append(serverPtr);
       
  1394 
       
  1395 	UpdateAclL(aURI,defaultACLptr);
       
  1396 	
       
  1397 	CleanupStack::PopAndDestroy(); //defaultACL
       
  1398 	_DBG_FILE("CNSmlDmDbHandler::DefaultACLsToServerL() : end");
       
  1399 	return KErrNone;
       
  1400 	}
       
  1401 
       
  1402 // ---------------------------------------------------------------------------
       
  1403 // CNSmlDmDbHandler::EraseServerIdL
       
  1404 // Removes ACL information for this server id
       
  1405 // ---------------------------------------------------------------------------
       
  1406 EXPORT_C TInt CNSmlDmDbHandler::EraseServerIdL(const TDesC8& aServerId)
       
  1407 	{
       
  1408 	GetAclDataFromDbL();
       
  1409 
       
  1410 	for(TInt i=0;i<iAcls.Count();i++)
       
  1411 		{
       
  1412 		CNSmlDmACLParser* acl = CNSmlDmACLParser::NewLC();
       
  1413 		acl->ParseL(*iAcls[i]);
       
  1414 		acl->RemoveAllReferences(aServerId);
       
  1415 		if(acl->ShouldDelete())
       
  1416 			{
       
  1417 			delete iAclURIs[i];
       
  1418 			iAclURIs.Remove(i);
       
  1419 			delete iAcls[i];
       
  1420 			iAcls.Remove(i);
       
  1421 			i--;
       
  1422 			}
       
  1423 		else
       
  1424 			{
       
  1425 			HBufC8* aclBuf=acl->GenerateL();
       
  1426 			delete iAcls[i];
       
  1427 			iAcls[i] = aclBuf;
       
  1428 			}
       
  1429 		CleanupStack::PopAndDestroy(); //acl		
       
  1430 		}
       
  1431 	WriteAclInfoToDbL();
       
  1432 
       
  1433 	return 0;
       
  1434 	}
       
  1435 
       
  1436 
       
  1437 // ---------------------------------------------------------------------------
       
  1438 // TInt CNSmlDmDbHandler::GetAclDataFromDbL(TUint32 aAdapterId)
       
  1439 // Gets the data from tthe db to membervariables
       
  1440 // ---------------------------------------------------------------------------
       
  1441 TInt CNSmlDmDbHandler::GetAclDataFromDbL()
       
  1442 	{
       
  1443 	_DBG_FILE("CNSmlDmDbHandler::GetAclDataFromDbL() : begin");
       
  1444 
       
  1445 	TInt ret=KErrNone;
       
  1446 	if(!iAclUpToDate)
       
  1447 		{
       
  1448 		_DBG_FILE("CNSmlDmDbHandler::GetAclDataFromDbL() : Get data from db");
       
  1449 		_LIT(KNSmlDmSQLStatement,
       
  1450 			"select * from AclTable where AdapterId = %d");
       
  1451 			
       
  1452 		TInt pushed=0;
       
  1453 
       
  1454 		//get data from database
       
  1455 		iAcls.ResetAndDestroy();
       
  1456 		iAclURIs.ResetAndDestroy();
       
  1457 
       
  1458 		iDatabase.OpenL ( iFileStore, iFileStore->Root() );
       
  1459 
       
  1460 		HBufC* sql = HBufC::NewLC( KNSmlDmSQLStatement().Length()+10);
       
  1461 		pushed++;
       
  1462 		TPtr sqlPtr = sql->Des();
       
  1463 		sqlPtr.Format( KNSmlDmSQLStatement, KNSmlAclDbId );
       
  1464 
       
  1465 		ret = iView.Prepare( iDatabase,TDbQuery(sqlPtr,EDbCompareNormal) );
       
  1466 		HBufC8* aclURIs=0;
       
  1467 		HBufC8* acls=0;
       
  1468 		
       
  1469 		if(ret==KErrNone)
       
  1470 			{
       
  1471 			ret = iView.EvaluateAll();
       
  1472 			//Get the structure of rowset
       
  1473 			CDbColSet* colSet = iView.ColSetL();
       
  1474 			CleanupStack::PushL(colSet);
       
  1475 			pushed++;
       
  1476 			
       
  1477 			if(iView.FirstL())
       
  1478 				{
       
  1479 				iView.GetL();
       
  1480 				}
       
  1481 			else
       
  1482 				{
       
  1483 				iView.Close();
       
  1484 				iDatabase.Close();
       
  1485 				iAclUpToDate = ETrue;
       
  1486 				CleanupStack::PopAndDestroy(pushed);
       
  1487 				return KErrNotFound;
       
  1488 				}
       
  1489 		
       
  1490 			RDbColReadStream in[2];
       
  1491 			TDbColNo col[2];
       
  1492 
       
  1493 			//read data from db
       
  1494 			for(TInt i=0;i<2;i++)
       
  1495 				{
       
  1496 				if(i==0)
       
  1497 					{
       
  1498 					col[i] = colSet->ColNo(KNSmlDmAcl);
       
  1499 					in[i].OpenL(iView,col[i]);
       
  1500 					CleanupClosePushL(in[i]);
       
  1501 					acls = HBufC8::NewLC(iView.ColLength(col[i]));
       
  1502 					TPtr8 aclPtr = acls->Des();
       
  1503 					in[i].ReadL(aclPtr,iView.ColLength(col[i]));
       
  1504 					CleanupStack::Pop(); //acls
       
  1505 					CleanupStack::PopAndDestroy(); //in[i]
       
  1506 					CleanupStack::PushL(acls); 
       
  1507 					pushed++;
       
  1508 					}
       
  1509 				else
       
  1510 					{
       
  1511 					col[i] = colSet->ColNo(KNSmlDmAclURI);
       
  1512 					in[i].OpenL(iView,col[i]);
       
  1513 					CleanupClosePushL(in[i]);
       
  1514 					aclURIs = HBufC8::NewLC(iView.ColLength(col[i]));
       
  1515 					TPtr8 uriPtr = aclURIs->Des();
       
  1516 					in[i].ReadL(uriPtr,iView.ColLength(col[i]));
       
  1517 					CleanupStack::Pop(); //aclURIs
       
  1518 					CleanupStack::PopAndDestroy(); //in[i]
       
  1519 					CleanupStack::PushL(aclURIs);
       
  1520 					pushed++;
       
  1521 					}
       
  1522 				} //for
       
  1523 			} //end if(ret==KErrNone)
       
  1524 
       
  1525 		//write data to arrays from HBufC*
       
  1526 		if(acls!=0)
       
  1527 			{
       
  1528 			TInt startIndex=0;
       
  1529 			for(TInt i=0;i<acls->Length();i++)
       
  1530 				{
       
  1531 				if(acls[0][i]==KNSmlDMAclUriSeparator)
       
  1532 					{
       
  1533 					HBufC8* tmpAcl = HBufC8::NewLC(i-startIndex);
       
  1534 					tmpAcl->Des() = acls->Mid(startIndex, i-startIndex);
       
  1535 					iAcls.AppendL(tmpAcl);
       
  1536 					CleanupStack::Pop ( tmpAcl );
       
  1537 					startIndex=i+1;
       
  1538 					}
       
  1539 				}
       
  1540 			} //end if(acls!=0)
       
  1541 
       
  1542 		//write data to arrays from HBufC*
       
  1543 		if(aclURIs!=0)
       
  1544 			{
       
  1545 			TInt startIndex=0;
       
  1546 			for(TInt i=0;i<aclURIs->Length();i++)
       
  1547 				{
       
  1548 				if(aclURIs[0][i]==KNSmlDMMappingSeparator)
       
  1549 					{
       
  1550 					HBufC8* tmpUri = HBufC8::NewLC(i-startIndex);
       
  1551 					tmpUri->Des() = aclURIs->Mid(startIndex, i-startIndex);
       
  1552 					iAclURIs.AppendL(tmpUri);
       
  1553 					CleanupStack::Pop ( tmpUri );
       
  1554 					startIndex=i+1;
       
  1555 					}
       
  1556 				}
       
  1557 			} //end if(aclUris!=0)
       
  1558 
       
  1559 
       
  1560 		//colset, store, sql, acls, aclUris
       
  1561 		CleanupStack::PopAndDestroy(pushed);
       
  1562 		
       
  1563 		iView.Close();
       
  1564 		iDatabase.Close();
       
  1565 		iAclUpToDate = ETrue;
       
  1566 		} //end if(aAdapterId!=iAdapterIdAcl)
       
  1567 	
       
  1568 	return ret;
       
  1569 	}
       
  1570 
       
  1571 
       
  1572 // ---------------------------------------------------------------------------
       
  1573 // CNSmlDmDbHandler::RenameAclL()
       
  1574 // Rename Acl uri and also the children
       
  1575 // ---------------------------------------------------------------------------
       
  1576 TInt CNSmlDmDbHandler::RenameAclL(const TDesC8& aURI, const TDesC8& aNewURI)
       
  1577 	{
       
  1578 	GetAclDataFromDbL();
       
  1579 	TInt ret = KErrNotFound;
       
  1580 
       
  1581 	TBool found = EFalse;
       
  1582 
       
  1583 	//rename mapping item if found
       
  1584 	for(TInt i=0;i<iAclURIs.Count();i++)
       
  1585 		{
       
  1586 		if( iAclURIs[i]->Find(aURI) == 0 )
       
  1587 			{
       
  1588 			found = ETrue;
       
  1589 			}
       
  1590 		if(iAclURIs[i]->Find(aNewURI)==0 )
       
  1591 			{
       
  1592 			return KErrAlreadyExists;
       
  1593 			}
       
  1594 		}
       
  1595 
       
  1596 	if(found)
       
  1597 		{
       
  1598 		for(TInt i=0;i<iAclURIs.Count();i++)
       
  1599 			{
       
  1600 			if( iAclURIs[i]->Find(aURI) == 0 )
       
  1601 				{
       
  1602 				HBufC8* newUri;
       
  1603 				if( iAclURIs[i]->Compare(aURI) == 0 )
       
  1604 					{
       
  1605 					newUri = aNewURI.AllocLC();
       
  1606 					}
       
  1607 				else
       
  1608 					{
       
  1609 					newUri = HBufC8::NewLC(aNewURI.Length() +
       
  1610 						(iAclURIs[i]->Length()-aURI.Length()));
       
  1611 						
       
  1612 					TPtr8 newUriPtr = newUri->Des();
       
  1613 					newUriPtr.Format(aNewURI);
       
  1614 					newUriPtr.Append(iAclURIs[i]->Mid(aURI.Length()));
       
  1615 					}
       
  1616 				HBufC8* acl = iAcls[i];
       
  1617 				delete iAclURIs[i];
       
  1618 				iAclURIs.Remove(i);
       
  1619 				iAcls.Remove(i);
       
  1620 
       
  1621 				iAclURIs.AppendL(newUri);
       
  1622 				CleanupStack::Pop(newUri);
       
  1623 				iAcls.AppendL(acl);
       
  1624 				iAclURIs.Compress();
       
  1625 				iAcls.Compress();
       
  1626 				i--;
       
  1627 				}
       
  1628 			}
       
  1629 		ret = KErrNone;
       
  1630 		}
       
  1631 	return ret;	
       
  1632 	}
       
  1633 
       
  1634 
       
  1635 // ===========================================================================
       
  1636 // CNSmlDmURIMapping
       
  1637 // ===========================================================================
       
  1638 
       
  1639 // ---------------------------------------------------------------------------
       
  1640 // CNSmlDmURIMapping::~CNSmlDmURIMapping()
       
  1641 // ---------------------------------------------------------------------------
       
  1642 CNSmlDmURIMapping::~CNSmlDmURIMapping()
       
  1643 	{
       
  1644 	delete iURI;
       
  1645 	delete iLUID;
       
  1646 	}
       
  1647 	
       
  1648 // ---------------------------------------------------------------------------
       
  1649 // CNSmlDmURIMapping* CNSmlDmURIMapping::NewLC()
       
  1650 // ---------------------------------------------------------------------------
       
  1651 CNSmlDmURIMapping* CNSmlDmURIMapping::NewLC()
       
  1652 	{
       
  1653 	CNSmlDmURIMapping* self = new (ELeave) CNSmlDmURIMapping();
       
  1654 
       
  1655 	CleanupStack::PushL( self );
       
  1656 	self->ConstructL();
       
  1657 
       
  1658 	return self;
       
  1659 	}
       
  1660 
       
  1661 // ---------------------------------------------------------------------------
       
  1662 // CNSmlDmURIMapping* CNSmlDmURIMapping::NewLC( const TDesC8& aURI,
       
  1663 //	const TDesC8& aLUID, TNSmlDmMapOperation aOperation )
       
  1664 // ---------------------------------------------------------------------------
       
  1665 CNSmlDmURIMapping* CNSmlDmURIMapping::NewLC( const TDesC8& aURI,
       
  1666 	const TDesC8& aLUID,
       
  1667 	TNSmlDmMapOperation aOperation )
       
  1668 	{
       
  1669 	CNSmlDmURIMapping* self = new (ELeave) CNSmlDmURIMapping();
       
  1670 
       
  1671 	CleanupStack::PushL( self );
       
  1672 	self->ConstructL ( aURI, aLUID, aOperation );
       
  1673 
       
  1674 	return self;
       
  1675 	}
       
  1676 	
       
  1677 // ---------------------------------------------------------------------------
       
  1678 // void CNSmlDmURIMapping::ConstructL()
       
  1679 // ---------------------------------------------------------------------------
       
  1680 void CNSmlDmURIMapping::ConstructL()
       
  1681 	{
       
  1682 	iURI = HBufC8::NewL(0);
       
  1683 	iLUID = HBufC8::NewL(0);
       
  1684 	iOperation = EMapNoAction;
       
  1685 	}
       
  1686 
       
  1687 // ---------------------------------------------------------------------------
       
  1688 // void CNSmlDmURIMapping::ConstructL( const TDesC8& aURI,
       
  1689 //	const TDesC8& aLUID, TNSmlDmMapOperation aOperation )
       
  1690 // ---------------------------------------------------------------------------
       
  1691 void CNSmlDmURIMapping::ConstructL( const TDesC8& aURI,
       
  1692 	const TDesC8& aLUID,
       
  1693 	TNSmlDmMapOperation aOperation )
       
  1694 	{
       
  1695 	iURI = aURI.AllocLC();
       
  1696 	iLUID = aLUID.AllocL();
       
  1697 	iOperation = aOperation;
       
  1698 	CleanupStack::Pop();
       
  1699 	}
       
  1700 
       
  1701 
       
  1702 
       
  1703 // ===========================================================================
       
  1704 // CNSmlDmAdapterElement
       
  1705 // ===========================================================================
       
  1706 
       
  1707 // ---------------------------------------------------------------------------
       
  1708 // CNSmlDmAdapterElement::CNSmlDmAdapterElement()
       
  1709 // ---------------------------------------------------------------------------
       
  1710 CNSmlDmAdapterElement::~CNSmlDmAdapterElement()
       
  1711 	{
       
  1712 	iMapArray.ResetAndDestroy();
       
  1713 	}
       
  1714 	
       
  1715 // ---------------------------------------------------------------------------
       
  1716 // CNSmlDmAdapterElement* CNSmlDmAdapterElement::NewLC( TUint32 aAdapterId )
       
  1717 // ---------------------------------------------------------------------------
       
  1718 CNSmlDmAdapterElement* CNSmlDmAdapterElement::NewLC ( TUint32 aAdapterId )
       
  1719 	{
       
  1720 	CNSmlDmAdapterElement* self = new (ELeave) CNSmlDmAdapterElement(aAdapterId);
       
  1721 	CleanupStack::PushL( self );
       
  1722 	return self;
       
  1723 	}
       
  1724 	
       
  1725 // ---------------------------------------------------------------------------
       
  1726 // CNSmlDmAdapterElement::CNSmlDmAdapterElement ( TUint32 aAdapterId )
       
  1727 // ---------------------------------------------------------------------------
       
  1728 CNSmlDmAdapterElement::CNSmlDmAdapterElement ( TUint32 aAdapterId )
       
  1729 	{
       
  1730 	iAdapterId = aAdapterId;
       
  1731 	}
       
  1732