applayerpluginsandutils/uripermissionservices/server/src/urilistinterface.cpp
changeset 0 b16258d2340f
child 18 5f1cd966e0d9
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <ineturilistdef.h>
       
    17 #include <tldlistdef.h>
       
    18 #include "urilistinterface.h"
       
    19 #include "sqldbaccessor.h"
       
    20 #include "ineturiimpl.h"
       
    21 #include "urilistwritestream.h"
       
    22 #include "urilist.h"
       
    23 #include "urilistinitializer.h"
       
    24 #include "tldlistinitializer.h"
       
    25 #include "uriqueryfilter.h"
       
    26 #include "tldproperties.h"
       
    27 #include <delimitedpath8.h>
       
    28 #include "tldlist.h"
       
    29 #include <swi/sisregistrysession.h>
       
    30 #include <swi/sisregistryentry.h>
       
    31 #include <bautils.h> 
       
    32 #include <uriutils.h>
       
    33 
       
    34 const TInt KMaxLength = 1024;
       
    35 const TInt pkgId(0x2000A471);
       
    36 _LIT(KRomDirectory, "z");
       
    37 _LIT(KUriListFile, ":\\private\\20009D70\\ineturilist.xml");
       
    38 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    39 _LIT(KPolicyFile, ":\\private\\20009D70\\tldpolicy.xml");
       
    40 #endif	//SYMBIAN_ENABLE_SPLIT_HEADERS
       
    41 
       
    42 CUriListInterface::CUriListInterface ()
       
    43 	{
       
    44 		
       
    45 	}
       
    46 
       
    47 CUriListInterface::~CUriListInterface ()
       
    48 	{
       
    49 	if ( iDbAccessor )
       
    50 		{
       
    51 		iDbAccessor->Release ();	
       
    52 		}
       
    53 	iStringPool.Close ();
       
    54 	}
       
    55 
       
    56 CUriListInterface* CUriListInterface::NewL ()
       
    57 	{
       
    58 	CUriListInterface* self = new ( ELeave ) CUriListInterface ();
       
    59 	CleanupStack::PushL ( self );
       
    60 	self->ConstructL ();
       
    61 	CleanupStack::Pop ( self );
       
    62 	
       
    63 	return self;
       
    64 	}
       
    65 
       
    66 	
       
    67 void CUriListInterface::ConstructL ()
       
    68 	{
       
    69 	iStringPool.OpenL ( URILIST::Table );
       
    70 	iStringPool.OpenL ( TLDLIST::Table );
       
    71 	iDbAccessor = CSqlDbAccessor::NewL ( iStringPool );
       
    72 	InitializeDatabaseL ();
       
    73 	}
       
    74 
       
    75 /**
       
    76 Add a new URI and its associated properties into the storage. 
       
    77 
       
    78 @param aUriStream A stream to read the CInetUriImpl object.
       
    79 @param aUriId URI Id value that will be returned to the caller
       
    80 @param aPropId Property Id value that will be returned to the caller
       
    81 
       
    82 */
       
    83 void CUriListInterface::AddUriL ( RReadStream& aUriStream, TInt& aUriId, TInt& aPropId )
       
    84 	{	
       
    85 	CInetUriImpl* inetUri = CInetUriImpl::NewL ();
       
    86 	CleanupStack::PushL ( inetUri );
       
    87 	inetUri->UnpackL ( aUriStream );
       
    88 	InsertL ( *inetUri );
       
    89 	aUriId = inetUri->UriId ();
       
    90 	aPropId = inetUri->Properties().PropId ();
       
    91 	CleanupStack::PopAndDestroy ( inetUri );	
       
    92 	}
       
    93 
       
    94 /**
       
    95 Removes the properties from the URIPropertiesTbl. The function removes the URI from the 
       
    96 URITbl table if it is not having a dependency. See the table structure
       
    97 
       
    98 @param aUriId URI Id value 
       
    99 @param aPropId Property Id value
       
   100 */
       
   101 void CUriListInterface::RemoveUriL ( TInt aUriId, TInt aPropId )
       
   102 	{
       
   103 	iDbAccessor->BeginTransactionL ();
       
   104 	// This is not to delete the object rather to do a rollback operation incase of a failure
       
   105 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::RollbackTrans, iDbAccessor ) );
       
   106 	
       
   107 	// Delete from properties table
       
   108 	DeleteL ( KUriPropsTblName(), URILIST::EPropId, aPropId );
       
   109 
       
   110 	// The URI also need to be removed if it is not marked for another service type.
       
   111 	// Check for dependency. 
       
   112 	if ( CheckDependencyL( aUriId ) )
       
   113 		{
       
   114 		// No dependency. The URI also need to be removed			
       
   115 		DeleteL ( KUriTblName(), URILIST::EId, aUriId );
       
   116 		}	
       
   117 	
       
   118 	// Delete the same from view
       
   119 	DeleteL ( KViewName(), URILIST::EPropId, aPropId );
       
   120 	iDbAccessor->CommitTransactionL ();
       
   121 	CleanupStack::Pop (); // iDbAccessor
       
   122 	}
       
   123 
       
   124 /**
       
   125 Updates the listtype and favouritename. Only, these 2 properties can be modified.
       
   126 
       
   127 @param aUriPropsId Property ID which uniquely identifies the row in the URIPropertiesTbl
       
   128 @param aListType New Listtype 
       
   129 @param aFavouriteName New favourite name for the URI
       
   130 */
       
   131 void CUriListInterface::UpdateUriL ( TInt aUriPropsId, InetUriList::TListType aListType, const TDesC8& aFavouriteName )
       
   132 	{	
       
   133 	iDbAccessor->BeginTransactionL ();
       
   134 	// This is not to delete the object rather to do a rollback operation incase of a failure
       
   135 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::RollbackTrans, iDbAccessor ) );
       
   136 
       
   137 	// Only update List Type & Favourite Name. Other properties are not allowed to update.
       
   138 	if ( UpdateL ( KUriPropsTblName(), aUriPropsId, aListType, aFavouriteName ) == 0 )
       
   139 		User::Leave ( InetUriList::KErrUriNotFound );
       
   140 
       
   141 	User::LeaveIfError ( UpdateL ( KViewName(), aUriPropsId, aListType, aFavouriteName ) );
       
   142 	
       
   143 	iDbAccessor->CommitTransactionL ();
       
   144 	CleanupStack::Pop (); // iDbAccessor	
       
   145 	}
       
   146 
       
   147 /**
       
   148 Inserts the information into URITbl, URIPropertiesTbl & the URI view. Checks whether
       
   149 the URI is already present in the storage and applies the blacklist/whitelist rule
       
   150 before insertion. 
       
   151 
       
   152 @param aInetUri URI information object. On return the new property id and URI id will be set.
       
   153 */
       
   154 void CUriListInterface::InsertL ( CInetUriImpl& aInetUri )
       
   155 	{
       
   156 	TInt result = KErrNone;
       
   157 	const TUriC8& uri ( aInetUri.Uri().Uri() );
       
   158 	MDBTransaction* dbTrans = DoSearchExactUriLC ( uri, aInetUri.Properties().ServiceType() );
       
   159 	if ( dbTrans->Next() )
       
   160 		{		
       
   161 		// URI already exists for the requested service type. 
       
   162 		result = InetUriList::KErrUriAlreadyExists;
       
   163 		}	
       
   164 	User::LeaveIfError ( result );
       
   165 	CleanupStack::PopAndDestroy (); // dbTrans
       
   166 	
       
   167 	// Now the URI can be added.
       
   168 	iDbAccessor->BeginTransactionL ();
       
   169 	// This is not to delete the object rather to do a rollback operation incase of a failure
       
   170 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::RollbackTrans, iDbAccessor ) );
       
   171 
       
   172 	TInt uriId = 0;
       
   173 	if ( InsertIntoUriTblL ( aInetUri.Uri().Uri(), uriId ) == 0 )
       
   174 		User::Leave ( InetUriList::KErrUriAddFailed );
       
   175 		
       
   176 	aInetUri.SetUriId ( uriId );
       
   177 	
       
   178 	// Now add the properties	
       
   179 	TInt propId = 0;
       
   180 	if ( InsertIntoUriPropsTblL ( aInetUri.UriId(), aInetUri.Properties(), propId ) == 0 )
       
   181 		User::Leave ( InetUriList::KErrUriAddFailed );	
       
   182 	aInetUri.Properties().SetPropId( propId );
       
   183 	User::LeaveIfError ( InsertIntoViewL ( aInetUri ) );
       
   184 	iDbAccessor->CommitTransactionL ();			
       
   185 	CleanupStack::Pop (); // iDbAccessor	
       
   186 	}
       
   187 
       
   188 /**
       
   189 Inserts the information into TLDTbl. Checks whether
       
   190 the URI is already present in the storage and applies the blacklist/whitelist rule
       
   191 before insertion. 
       
   192 
       
   193 @param aTldPolicydata Tld information object.
       
   194 */
       
   195 void CUriListInterface::InsertTldPolicyDataL ( CTldProperties& aTldPolicydata )
       
   196 	{
       
   197 	//First check whether such row is available
       
   198 	//if yes dont insert.
       
   199 	// Now the TLD Policy data can be added.
       
   200 	iDbAccessor->BeginTransactionL ();
       
   201 	// This is not to delete the object rather to do a rollback operation incase of a failure
       
   202 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::RollbackTrans, iDbAccessor ) );
       
   203 	//Inserts unique row 
       
   204 	if( !IsDuplicateEntryL( aTldPolicydata )   )
       
   205 		{
       
   206 		InsertIntoTldTblL ( aTldPolicydata );
       
   207 		}
       
   208 
       
   209 	iDbAccessor->CommitTransactionL ();			
       
   210 	CleanupStack::Pop (); // iDbAccessor	
       
   211 	}
       
   212 	
       
   213 /**
       
   214 Inserts the data into the URITbl. 
       
   215 
       
   216 @param aUri URI object
       
   217 @param [out] aUriId The URI will be filled upon successful insertion. This ID
       
   218 	uniquelly indentifies the URI.
       
   219 
       
   220 @return Returns the number of rows inserted. Normally 1. Otherwise returns with
       
   221 		system-wide error code.
       
   222 */
       
   223 TInt CUriListInterface::InsertIntoUriTblL ( const TUriC8& aUri, TInt& aUriId )
       
   224 	{
       
   225 	const TDesC8& id ( iStringPool.String (URILIST::EId,URILIST::Table).DesC() );
       
   226 	aUriId = GetMaxIdValueL ( KUriTblName(), id ) + 1;
       
   227 	_LIT8 ( KUriTblInsertStmt, "INSERT INTO %S VALUES (:V1, :V2, :V3, :V4, :V5, :V6, :V7, :V8)" );
       
   228 	
       
   229 	RBuf8 sqlStmt;
       
   230 	sqlStmt.CreateL ( KMaxDbStmtLen );
       
   231 	CleanupClosePushL ( sqlStmt );
       
   232 	sqlStmt.Format ( KUriTblInsertStmt(), &(KUriTblName()) );
       
   233 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( sqlStmt );
       
   234 	CleanupStack::PopAndDestroy (); // sqlStmt
       
   235 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   236 	// We are having 8 parameters for this query. First bind the query with parameter position 0 
       
   237 	// and so on
       
   238 	dbTrans->BindIntL ( URILIST::EId, aUriId ); 
       
   239 	dbTrans->BindTextL ( URILIST::EScheme, aUri.IsPresent(EUriScheme) ? aUri.Extract ( EUriScheme ) : KNullDesC8() );
       
   240 	dbTrans->BindTextL ( URILIST::EUserInfo, aUri.IsPresent(EUriUserinfo) ? aUri.Extract ( EUriUserinfo ) : KNullDesC8() );
       
   241 	dbTrans->BindTextL ( URILIST::EHost, aUri.IsPresent(EUriHost) ? aUri.Extract ( EUriHost ) : KNullDesC8() ); 
       
   242 	dbTrans->BindTextL ( URILIST::EPort, aUri.IsPresent(EUriPort) ? aUri.Extract ( EUriPort ) : KNullDesC8() );
       
   243 	dbTrans->BindTextL ( URILIST::EPath, aUri.IsPresent(EUriPath) ? aUri.Extract ( EUriPath ) : KNullDesC8() ); 
       
   244 	dbTrans->BindTextL ( URILIST::EQuery, aUri.IsPresent(EUriQuery) ? aUri.Extract ( EUriQuery ) : KNullDesC8() );  
       
   245 	dbTrans->BindTextL ( URILIST::EFragments, aUri.IsPresent(EUriFragment) ? aUri.Extract ( EUriFragment ) : KNullDesC8() );  				
       
   246 
       
   247 	TInt result = dbTrans->ExecuteL ();
       
   248 	CleanupStack::PopAndDestroy (); //dbTrans
       
   249 	return result;
       
   250 	}
       
   251 
       
   252 /**
       
   253 Inserts the data into the TLDTbl. 
       
   254 
       
   255 @return Returns the number of rows inserted. Normally 1. Otherwise returns with
       
   256 		system-wide error code.
       
   257 */
       
   258 TInt CUriListInterface::InsertIntoTldTblL ( const CTldProperties& aTldProps )
       
   259 	{
       
   260 	_LIT8 ( KTldTblInsertStmt, "INSERT INTO %S VALUES ( :V1, :V2, :V3 )" );
       
   261 	RBuf8 sqlStmt;
       
   262 	sqlStmt.CreateL ( KMaxDbStmtLen );
       
   263 	CleanupClosePushL ( sqlStmt );
       
   264 	sqlStmt.Format ( KTldTblInsertStmt(), &(KTldTblName()) );
       
   265 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( sqlStmt );
       
   266 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   267 	dbTrans->BindTextL ( TLDLIST::ETLDName, aTldProps.TldName() );
       
   268 	TInt lt = static_cast<TInt> ( aTldProps.ListType() ) ;
       
   269 	dbTrans->BindIntL ( TLDLIST::EListType, lt );
       
   270 	dbTrans->BindTextL ( TLDLIST::ECharacterSet, aTldProps.CharacterSet() );
       
   271 	TInt result = dbTrans->ExecuteL ();
       
   272 	CleanupStack::PopAndDestroy (2); // sqlStmt, dbTrans
       
   273 	return result;
       
   274 	}
       
   275 	
       
   276 /**
       
   277 Checks whether the duplicate entry in the TLDTbl. 
       
   278 
       
   279 @return ETrue if already such row exist else retrns EFalse
       
   280 */
       
   281 TBool CUriListInterface::IsDuplicateEntryL ( const CTldProperties& aTldProps )
       
   282 	{
       
   283 	_LIT8 ( KTldTblSearchStmt, "SELECT * FROM %S WHERE %S=:V1 and %S=:V2" );
       
   284 	const TDesC8& KTldName (iStringPool.String (TLDLIST::ETLDName,TLDLIST::Table).DesC());
       
   285 	const TDesC8& KTldListType (iStringPool.String (TLDLIST::EListType,TLDLIST::Table).DesC());
       
   286 	RBuf8 sqlStmt;
       
   287 	sqlStmt.CreateL ( KMaxDbStmtLen );
       
   288 	CleanupClosePushL ( sqlStmt );
       
   289 	sqlStmt.Format ( KTldTblSearchStmt(), &(KTldTblName()), &KTldName, &KTldListType  );
       
   290 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( sqlStmt );
       
   291 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   292 	dbTrans->BindTextL ( TLDLIST::ETLDName, aTldProps.TldName() );
       
   293 	TInt lt = static_cast<TInt> ( aTldProps.ListType() ) ;
       
   294 	dbTrans->BindIntL ( TLDLIST::EListType, lt );
       
   295 	TBool result( dbTrans->Next () );
       
   296 	CleanupStack::PopAndDestroy (2); // sqlStmt, dbTrans
       
   297 	return result;
       
   298 	}
       
   299 
       
   300 /**
       
   301 Inserts the data into the URIPropertiesTbl. 
       
   302 
       
   303 @param aUri URI object
       
   304 @param [out] aUriId The URI will be filled upon successful insertion. This ID
       
   305 	uniquelly indentifies the URI.
       
   306 
       
   307 @return Returns the number of rows inserted. Normally 1. Otherwise returns with
       
   308 		system-wide error code.
       
   309 */
       
   310 TInt CUriListInterface::InsertIntoUriPropsTblL ( TInt aUriId, const CInetUriProperties& aUriProps, TInt& aPropId )
       
   311 	{
       
   312 	const TInt KMaxLen = 128;
       
   313 	const TDesC8& propId ( iStringPool.String (URILIST::EPropId,URILIST::Table).DesC() );
       
   314 	aPropId = GetMaxIdValueL ( KUriPropsTblName(), propId ) + 1;
       
   315 	_LIT8 ( KInsertStmt, "INSERT INTO %S VALUES (%d, %d, %d, %d, %d, :V1)");
       
   316 	RBuf8 queryBuf;
       
   317 	CleanupClosePushL (queryBuf);
       
   318 	queryBuf.CreateL ( KMaxLen );
       
   319 	queryBuf.AppendFormat ( KInsertStmt, &(KUriPropsTblName()), aUriId, aPropId, aUriProps.ServiceType(), aUriProps.ListType(), aUriProps.Permission() );
       
   320 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   321 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   322 	dbTrans->BindTextL ( 0, aUriProps.FavouriteName () ); // Bind with parameter index 0
       
   323 	TInt result = dbTrans->ExecuteL ();
       
   324 	CleanupStack::PopAndDestroy (2); // queryBuf, dbTrans
       
   325 	
       
   326 	return result;
       
   327 	}
       
   328 
       
   329 /**
       
   330 Insert the data into the view. The view insertion will happen via a trigger. 
       
   331 The SQLite views are read-only and cannot do any INSERT/DELETE/UPDATE operation directly.
       
   332 
       
   333 @param aInetUri The URI information
       
   334 
       
   335 @return KErrNone for success else return any of the system wide error code.
       
   336 
       
   337 Note: The view insertion execution will not give the number of rows actualy 
       
   338 inserted. It returns KErrNone
       
   339 */
       
   340 TInt CUriListInterface::InsertIntoViewL ( const CInetUriImpl& aInetUri )
       
   341 	{
       
   342 	_LIT8 ( KUriViewInsertStmt, "INSERT INTO %S \
       
   343 								VALUES (:V1, :V2, :V3, :V4, :V5, :V6, :V7, :V8, \
       
   344 								:V9, :V10, :V11, :V12, :V13)" );
       
   345 	
       
   346 	RBuf8 sqlStmt;
       
   347 	sqlStmt.CreateL ( KMaxDbStmtLen );
       
   348 	CleanupClosePushL ( sqlStmt );
       
   349 	sqlStmt.Format ( KUriViewInsertStmt(), &(KViewName()));
       
   350 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( sqlStmt );
       
   351 	CleanupStack::PopAndDestroy (); //sqlStmt
       
   352 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   353 	// We are having 8 parameters for this query. First bind the query with parameter position 0 
       
   354 	// and so on
       
   355 	dbTrans->BindIntL ( URILIST::EId, aInetUri.UriId() ); 
       
   356 	TUriC8 uri ( aInetUri.Uri().Uri() );
       
   357 	dbTrans->BindTextL ( URILIST::EScheme, uri.IsPresent(EUriScheme) ? uri.Extract ( EUriScheme ) : KNullDesC8() );
       
   358 	dbTrans->BindTextL ( URILIST::EUserInfo, uri.IsPresent(EUriUserinfo) ? uri.Extract ( EUriUserinfo ) : KNullDesC8() );
       
   359 	dbTrans->BindTextL ( URILIST::EHost, uri.IsPresent(EUriHost) ? uri.Extract ( EUriHost ) : KNullDesC8() ); 
       
   360 	dbTrans->BindTextL ( URILIST::EPort, uri.IsPresent(EUriPort) ? uri.Extract ( EUriPort ) : KNullDesC8() );
       
   361 	dbTrans->BindTextL ( URILIST::EPath, uri.IsPresent(EUriPath) ? uri.Extract ( EUriPath ) : KNullDesC8() ); 
       
   362 	dbTrans->BindTextL ( URILIST::EQuery, uri.IsPresent(EUriQuery) ? uri.Extract ( EUriQuery ) : KNullDesC8() );  
       
   363 	dbTrans->BindTextL ( URILIST::EFragments, uri.IsPresent(EUriFragment) ? uri.Extract ( EUriFragment ) : KNullDesC8() );  
       
   364 
       
   365 	CInetUriProperties& properties = aInetUri.Properties ();
       
   366 	dbTrans->BindIntL ( URILIST::EPropId - 1, properties.PropId() );
       
   367 	dbTrans->BindIntL ( URILIST::EServiceType - 1, properties.ServiceType() );
       
   368 	dbTrans->BindIntL ( URILIST::EListType - 1, properties.ListType() );
       
   369 	dbTrans->BindIntL ( URILIST::EPermission - 1, properties.Permission() );
       
   370 	dbTrans->BindTextL ( URILIST::EFavouriteName-1, properties.FavouriteName() );
       
   371 
       
   372 	TInt result = dbTrans->ExecuteL ();
       
   373 	CleanupStack::PopAndDestroy (); //dbTrans	
       
   374 	return result;	
       
   375 	}
       
   376 
       
   377 
       
   378 /**
       
   379 Updates the URI properties
       
   380 
       
   381 @param aSchemaName Schema name - Could be table or view name
       
   382 @param aPropId Property ID 
       
   383 @param aListType new List type
       
   384 @param aFavouritename new favourite name.
       
   385 */
       
   386 TInt CUriListInterface::UpdateL ( const TDesC8& aSchemaName, TInt aPropId, InetUriList::TListType aListType, const TDesC8& aFavouriteName )
       
   387 	{
       
   388 	const TDesC8& propId ( iStringPool.String (URILIST::EPropId,URILIST::Table).DesC() );
       
   389 	const TDesC8& listType (iStringPool.String (URILIST::EListType,URILIST::Table).DesC());
       
   390 	const TDesC8& favouriteName (iStringPool.String (URILIST::EFavouriteName,URILIST::Table).DesC());
       
   391 	
       
   392 	// Only update List Type & Favourite Name. Other properties are not allowed to update.
       
   393 	_LIT8 ( KUpdateStmt, "UPDATE %S SET %S=%d, %S=:V1 where %S=%d" );
       
   394 	const TInt KMaxLen = 128;
       
   395 	RBuf8 queryBuf;
       
   396 	CleanupClosePushL ( queryBuf );
       
   397 	queryBuf.CreateL ( KMaxLen );
       
   398 	queryBuf.AppendFormat ( KUpdateStmt(), &aSchemaName, &listType, aListType, &favouriteName, &propId, aPropId );
       
   399 	
       
   400 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   401 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   402 	dbTrans->BindTextL ( 0, aFavouriteName );
       
   403 	TInt result = dbTrans->ExecuteL ();
       
   404 	CleanupStack::PopAndDestroy (2); // dbTrans, queryBuf
       
   405 	return result;
       
   406 	}
       
   407 
       
   408 /**
       
   409 Counts the number of URIs present in the storage for a given service type & list type
       
   410 */
       
   411 TInt CUriListInterface::CountUriL ( InetUriList::TServiceType aServiceType, InetUriList::TListType aListType )
       
   412 	{
       
   413 
       
   414 	TQueryArgs args ( aServiceType, aListType );
       
   415 	CUriQueryFilter* queryFilter = QueryL ( args );
       
   416 	CleanupStack::PushL ( queryFilter );
       
   417 	TInt count = queryFilter->MatchRecordCountL();
       
   418 	CleanupStack::PopAndDestroy (); // queryFilter
       
   419 	return count;
       
   420 	}
       
   421 
       
   422 /**
       
   423 Returns the List type associated with the URI and a servicetype
       
   424 */
       
   425 InetUriList::TListType CUriListInterface::GetListTypeL ( const TDesC8& aUri, InetUriList::TServiceType aSt )
       
   426 	{
       
   427 	TUriParser8 uriParser;
       
   428 	User::LeaveIfError ( uriParser.Parse ( aUri ) );
       
   429 	
       
   430 	MDBTransaction* dbTrans = DoSearchExactUriLC ( uriParser, aSt );
       
   431 	
       
   432 	if ( !dbTrans->Next() )
       
   433 		{		
       
   434 		User::LeaveIfError ( InetUriList::KErrUriNotFound );
       
   435 		}	
       
   436 	InetUriList::TListType lt = static_cast < InetUriList::TListType > ( dbTrans->ColumnIntL ( URILIST::EListType - 1 ) );
       
   437 	CleanupStack::PopAndDestroy (); // dbTrans
       
   438 	return lt;
       
   439 	}
       
   440 
       
   441 /**
       
   442 Delete the information from the given schema
       
   443 
       
   444 @param aSchemaName Table or view name
       
   445 @param aFieldPos The actual string index in the string table
       
   446 @param aid ID
       
   447 
       
   448 @return 
       
   449 */
       
   450 void CUriListInterface::DeleteL ( const TDesC8& aSchemaName, TInt aFieldPos, TInt aId )
       
   451 	{
       
   452 	const TInt KMaxLen = 64;
       
   453 	_LIT8 ( KDeleteStmt, "DELETE FROM %S where %S=%d" );
       
   454 	
       
   455 	const TDesC8& fieldName ( iStringPool.String (aFieldPos,URILIST::Table).DesC() );
       
   456 	
       
   457 	RBuf8 queryBuf;
       
   458 	CleanupClosePushL ( queryBuf );
       
   459 	queryBuf.CreateL ( KMaxLen );
       
   460 	queryBuf.AppendFormat ( KDeleteStmt(), &aSchemaName, &fieldName, aId );
       
   461 
       
   462 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   463 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   464 	dbTrans->ExecuteL ();
       
   465 	CleanupStack::PopAndDestroy ( 2 ); // queryBuf, dbTrans	
       
   466 	}
       
   467 
       
   468 /**
       
   469 The URI can be added for different servicetypes. Check if it is exist in the table.
       
   470 
       
   471 */
       
   472 TBool CUriListInterface::CheckDependencyL ( TInt aId )
       
   473 	{
       
   474 	const TDesC8& id ( iStringPool.String (URILIST::EId,URILIST::Table).DesC() );	
       
   475 	const TInt KMaxLen = 64;
       
   476 	_LIT8 ( KQueryStmt, "SELECT * FROM %S where %S=%d" );
       
   477 	RBuf8 queryBuf;
       
   478 	CleanupClosePushL ( queryBuf );
       
   479 	queryBuf.CreateL ( KMaxLen );
       
   480 	queryBuf.AppendFormat ( KQueryStmt(), &(KUriTblName()), &id, aId );
       
   481 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   482 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   483 	TBool result = dbTrans->Next();
       
   484 	CleanupStack::PopAndDestroy (2); // queryBuf, dbTrans
       
   485 	return result;
       
   486 	}
       
   487 
       
   488 
       
   489 /**
       
   490 Get the Maximum Id value from the table.
       
   491 
       
   492 */
       
   493 TInt CUriListInterface::GetMaxIdValueL ( const TDesC8& aTblName, const TDesC8& aFieldName )
       
   494 	{
       
   495 	const TInt KMaxLen = 64;
       
   496 	_LIT8 ( KMaxValueQuery, "SELECT MAX(%S) from %S" );
       
   497 	RBuf8 queryBuf;
       
   498 	CleanupClosePushL ( queryBuf );
       
   499 	queryBuf.CreateL ( KMaxLen );
       
   500 	queryBuf.AppendFormat ( KMaxValueQuery, &aFieldName, &aTblName );
       
   501 	TInt result = iDbAccessor->ExecuteScalarQueryL ( queryBuf );
       
   502 	CleanupStack::PopAndDestroy (); //queryBuf 
       
   503 	return result;
       
   504 	}
       
   505 
       
   506 /**
       
   507 Destroys the transaction object
       
   508 */
       
   509 void CUriListInterface::DestroyTransObj ( TAny* aPtr )
       
   510 	{
       
   511 	MDBTransaction* trans =	(MDBTransaction*)aPtr;
       
   512 	trans->Release();
       
   513 	}
       
   514 /**
       
   515 Rollback the transaction when a leave happens in insert/delete/update operation
       
   516 */
       
   517 void CUriListInterface::RollbackTrans ( TAny* aPtr )
       
   518 	{
       
   519 	MDBAccessor* accessor = (MDBAccessor*)aPtr;
       
   520 	accessor->RollbackTransaction();
       
   521 	}
       
   522 
       
   523 /**
       
   524 Query for the URI based on service type and listtype. The servicetype and listtype 
       
   525 will be extracted from aQueryArgs
       
   526 */
       
   527 CUriQueryFilter* CUriListInterface::QueryL ( const TQueryArgs& aQueryArgs )
       
   528 	{
       
   529 	const TDesC8& serviceType (iStringPool.String (URILIST::EServiceType,URILIST::Table).DesC());
       
   530 	const TDesC8& listType (iStringPool.String (URILIST::EListType,URILIST::Table).DesC());
       
   531 
       
   532 	RBuf8 queryBuf; 
       
   533 	InitViewQueryLC (queryBuf);
       
   534 	
       
   535 	TInt st = aQueryArgs.Get ( TQueryArgs::EServiceType );
       
   536 	TInt lt = aQueryArgs.Get ( TQueryArgs::EListType );
       
   537 
       
   538 	if ( lt == KErrNotFound )
       
   539 		{
       
   540 		_LIT8 (KFmtStr, "%S=%d" );
       
   541 		queryBuf.AppendFormat ( KFmtStr, &serviceType, st );
       
   542 
       
   543 		}
       
   544 	else
       
   545 		{
       
   546 		_LIT8 (KFmtStr, "%S=%d and %S=%d" );
       
   547 		queryBuf.AppendFormat ( KFmtStr, &serviceType, st, &listType, lt );
       
   548 		}
       
   549 
       
   550 	
       
   551 	// Execute the query
       
   552 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   553 	CleanupStack::PopAndDestroy (); //queryBuf
       
   554 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   555 	CUriQueryFilter* queryFilter = CUriQueryFilter::NewL ( dbTrans );
       
   556 	CleanupStack::Pop (); // dbTrans
       
   557 	
       
   558 	return queryFilter;	// return the queryFilter
       
   559 	}
       
   560 
       
   561 /**
       
   562 Fetches the length of the policy data if exists. 
       
   563 */
       
   564 TInt CUriListInterface::PolicyDataLengthL ( const TDesC8& aUri, const TPolicyQueryArgs& aQueryArgs )
       
   565 	{
       
   566 	CUriQueryFilter* queryFilter = QueryWithTldL ( aUri, aQueryArgs );
       
   567 	CleanupStack::PushL(queryFilter);
       
   568 	MDBTransaction& dbTrans = queryFilter->DBTransaction();
       
   569 	TInt resultsLength(0);
       
   570 	if ( dbTrans.Next() && queryFilter->MatchRecordL() )
       
   571 		{
       
   572 		resultsLength = dbTrans.ColumnTextL ( TLDLIST::ECharacterSet ).Length();
       
   573 		}
       
   574 	else
       
   575 		{
       
   576 		//Requested Policydata Not available for this TLD
       
   577 		CleanupStack::PopAndDestroy(queryFilter);
       
   578 		User::Leave(InetUriList::KErrInvalidTLD);
       
   579 		}
       
   580 	CleanupStack::PopAndDestroy(queryFilter);
       
   581 	return resultsLength;
       
   582 	}
       
   583 
       
   584 /**
       
   585 Fetches the host type of given uri. 
       
   586 */
       
   587 InetUriList::TListType CUriListInterface::GetHostTypeL (const TDesC8& aUri )
       
   588 	{
       
   589 	TBool blackListed( EFalse );
       
   590 	TInt err(KErrNone);
       
   591 	TRAP(err, blackListed = IsBlackListedUriL(aUri) );
       
   592 	if( err == InetUriList::KErrInvalidTLD )
       
   593 		{
       
   594 		//No Blacklisted policy data avaialble
       
   595 		TBool whiteListed( EFalse );
       
   596 		whiteListed = IsWhiteListedUriL(aUri);
       
   597 		blackListed = whiteListed? EFalse : ETrue;
       
   598 		}
       
   599 	//Bundle output Args with "blackListed" flag
       
   600 	return blackListed ? InetUriList::EBlackList : InetUriList::EWhiteList ;
       
   601 	}
       
   602 
       
   603 HBufC8* CUriListInterface::QueryTldInfoL(const TDesC8& aUri, const TPolicyQueryArgs& aQueryArgs )
       
   604 	{
       
   605 	HBufC8* charSet(NULL);
       
   606 	CUriQueryFilter* queryFilter = QueryWithTldL ( aUri, aQueryArgs );
       
   607 	CleanupStack::PushL(queryFilter);
       
   608 	MDBTransaction& dbTrans = queryFilter->DBTransaction();
       
   609 	if ( dbTrans.Next() && queryFilter->MatchRecordL() )
       
   610 		{
       
   611 		charSet = dbTrans.ColumnTextL ( TLDLIST::ECharacterSet ).AllocL();
       
   612 		}
       
   613 	else
       
   614 		{
       
   615 		//Requested Policydata Not available for this TLD
       
   616 		CleanupStack::PopAndDestroy(queryFilter);
       
   617 		User::Leave(InetUriList::KErrInvalidTLD);
       
   618 		}
       
   619 	CleanupStack::PopAndDestroy(queryFilter);
       
   620 	return charSet;
       
   621 	}
       
   622 	
       
   623 /**
       
   624 Query for the Tld based on listtype. The listtype will be extracted from aQueryArgs
       
   625 */
       
   626 CUriQueryFilter* CUriListInterface::QueryWithTldL (const TDesC8& aUri,  const TPolicyQueryArgs& aQueryArgs )
       
   627 	{
       
   628 	//Etract Tld
       
   629 	HBufC8* tld = ExtractTldL(aUri);
       
   630 	CleanupStack::PushL(tld); 
       
   631 	//execute Query Tld
       
   632 	CUriQueryFilter* queryFilter = DoQueryWithTldL ( tld->Des(), aQueryArgs );
       
   633 	CleanupStack::PopAndDestroy(); //tld
       
   634 	return queryFilter;
       
   635 	}
       
   636 
       
   637 /**
       
   638 Query for the Tld based on listtype. The listtype will be extracted from aQueryArgs
       
   639 */
       
   640 CUriQueryFilter* CUriListInterface::DoQueryWithTldL ( const TDesC8& aUri, const TPolicyQueryArgs& aQueryArgs )
       
   641 	{
       
   642 	InetUriList::TTLDQueryType matchType(InetUriList::EPolicyListType);
       
   643 	#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
   644 	if ( aQueryArgs.Get ( TPolicyQueryArgs::ETldQueryType ) != KErrNotFound)
       
   645 		{
       
   646 		matchType = static_cast < InetUriList::TTLDQueryType > ( aQueryArgs.Get ( TPolicyQueryArgs::ETldQueryType ) );	
       
   647 		}
       
   648 	#else
       
   649 	if ( aQueryArgs.IsSet ( TPolicyQueryArgs::ETldQueryType ) )
       
   650 		{
       
   651 		matchType = static_cast < InetUriList::TTLDQueryType > ( aQueryArgs.Get ( TPolicyQueryArgs::ETldQueryType ) );	
       
   652 		}
       
   653 	#endif	//SYMBIAN_ENABLE_SPLIT_HEADERS
       
   654 	__ASSERT_ALWAYS( ( matchType == InetUriList::EPolicyListType || matchType == InetUriList::EPolicyCharSet ), User::Invariant() );
       
   655 	
       
   656 	const TDesC8& tldName (iStringPool.String (TLDLIST::ETLDName,TLDLIST::Table).DesC());
       
   657 	const TDesC8& listType (iStringPool.String (TLDLIST::EListType,TLDLIST::Table).DesC());
       
   658 	
       
   659 	RBuf8 queryBuf; 
       
   660 	InitTldQueryLC (queryBuf);
       
   661 	//Do not change the order of this query. Index is created on tldname and list type
       
   662 	_LIT8(KSql, "( %S=:V1 and  %S=:V2 ) or ( %S=:V3 and %S=:V2 )" );
       
   663 	queryBuf.AppendFormat( KSql, &tldName, &listType, &tldName, &listType );
       
   664 
       
   665 	//Prepare the query
       
   666 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   667 	CleanupStack::PopAndDestroy (); //queryBuf
       
   668 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   669 	TInt lt = aQueryArgs.Get ( TPolicyQueryArgs::ETldListType );
       
   670 	RBuf8 upperCaseUri;
       
   671 	upperCaseUri.Create(aUri);
       
   672 	upperCaseUri.UpperCase();
       
   673 	dbTrans->BindTextL(0, aUri);
       
   674 	dbTrans->BindIntL(1, lt);
       
   675 	dbTrans->BindTextL(2, upperCaseUri );
       
   676 	
       
   677 	// Execute the query
       
   678 	CUriQueryFilter* queryFilter = CTldPolicyDataFilter::NewL ( dbTrans );
       
   679 	upperCaseUri.Close();
       
   680 	CleanupStack::Pop (); // dbTrans
       
   681 	return queryFilter;	// return the queryFilter
       
   682 	}
       
   683 	
       
   684 /**
       
   685 Query the list with the URI and other specified parameters in the aQueryArgs
       
   686 */
       
   687 CUriQueryFilter* CUriListInterface::QueryWithUriL ( const TDesC8& aUri, const TQueryArgs& aQueryArgs )
       
   688 	{
       
   689 	TUriParser8 uriParser;
       
   690 	User::LeaveIfError ( uriParser.Parse ( aUri ) );
       
   691 
       
   692 	MDBTransaction* dbTrans = NULL;
       
   693 	CUriQueryFilter* queryFilter = NULL;
       
   694 
       
   695 	TInt serviceType = aQueryArgs.Get ( TQueryArgs::EServiceType );
       
   696 	TInt listType = aQueryArgs.Get ( TQueryArgs::EListType );	
       
   697 	InetUriList::TURIMatch matchType = 	InetUriList::EExact;
       
   698 	#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
   699 	if ( aQueryArgs.Get ( TQueryArgs::EURIMatch ) != KErrNotFound)
       
   700 		matchType = static_cast < InetUriList::TURIMatch > ( aQueryArgs.Get ( TQueryArgs::EURIMatch ) );
       
   701 	#else
       
   702 	if ( aQueryArgs.IsSet ( TQueryArgs::EURIMatch ) )
       
   703 		matchType = static_cast < InetUriList::TURIMatch > ( aQueryArgs.Get ( TQueryArgs::EURIMatch ) );
       
   704 	#endif	//SYMBIAN_ENABLE_SPLIT_HEADERS
       
   705 	switch ( matchType )
       
   706 		{
       
   707 		case InetUriList::EExact:
       
   708 		dbTrans = DoSearchExactUriLC ( uriParser, serviceType, listType );
       
   709 		queryFilter = CUriQueryFilter::NewL ( dbTrans );
       
   710 		break;	
       
   711 
       
   712 		case InetUriList::EDomain:
       
   713 		dbTrans = DoSearchUriDomainLC ( uriParser, serviceType, listType );
       
   714 		queryFilter = CUriDomainFilter::NewL ( dbTrans, uriParser.Extract( EUriHost ) );
       
   715 		break;
       
   716 
       
   717 		case InetUriList::EExactPath:
       
   718 		dbTrans = DoSearchUriPathLC (uriParser, matchType, serviceType, listType );
       
   719 		queryFilter = CUriQueryFilter::NewL ( dbTrans );
       
   720 		break;
       
   721 
       
   722 		case InetUriList::EPartialPrefixPath:
       
   723 		case InetUriList::EPartialSuffixPath:
       
   724 		dbTrans = DoSearchUriPathLC (uriParser, matchType, serviceType, listType );
       
   725 		queryFilter = CUriPathFilter::NewL ( dbTrans, matchType, uriParser.Extract ( EUriPath ) );
       
   726 		break;
       
   727 		default:
       
   728 		User::Invariant ();			
       
   729 		}
       
   730 	CleanupStack::Pop (); // dbTrans
       
   731 	return queryFilter;
       
   732 	}
       
   733 
       
   734 /**
       
   735 Do a exact match in the list for the given uri and servicetype. All URI components
       
   736 will be matched
       
   737 */
       
   738 MDBTransaction* CUriListInterface::DoSearchExactUriLC ( const TUriC8& aUri, TInt aServiceType, TInt aListType /* = KErrNotFound */ )
       
   739 	{
       
   740 	const TDesC8& serviceType (iStringPool.String (URILIST::EServiceType,URILIST::Table).DesC());
       
   741 	const TDesC8& listType (iStringPool.String (URILIST::EListType,URILIST::Table).DesC());
       
   742 
       
   743 	RBuf8 queryBuf;
       
   744 	InitViewQueryLC(queryBuf);
       
   745 	
       
   746 	AppendFieldName ( queryBuf, URILIST::EScheme, aUri, EUriScheme );
       
   747 	AppendFieldName ( queryBuf, URILIST::EUserInfo, aUri, EUriUserinfo );
       
   748 	AppendFieldName ( queryBuf, URILIST::EHost, aUri, EUriHost );	
       
   749 	AppendFieldName ( queryBuf, URILIST::EPort, aUri, EUriPort );	
       
   750 	AppendFieldName ( queryBuf, URILIST::EPath, aUri, EUriPath );	
       
   751 	AppendFieldName ( queryBuf, URILIST::EQuery, aUri, EUriQuery );	
       
   752 	AppendFieldName ( queryBuf, URILIST::EFragments, aUri, EUriFragment );	
       
   753 	if ( aListType == KErrNotFound )	
       
   754 		{
       
   755 		_LIT8 ( KStr, "%S=%d" );
       
   756 		queryBuf.AppendFormat( KStr(), &serviceType, aServiceType );
       
   757 		}		
       
   758 	else
       
   759 		{
       
   760 		_LIT8 ( KStrWithListType, "%S=%d and %S=%d" );
       
   761 		queryBuf.AppendFormat( KStrWithListType(), &serviceType, aServiceType, &listType, aListType );			
       
   762 		}
       
   763 	TInt bindPos = 0;
       
   764 
       
   765 	// Prepare the transaction.
       
   766 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   767 	CleanupStack::PopAndDestroy ();  // queryBuf
       
   768 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   769 	
       
   770 	if ( aUri.IsPresent(EUriScheme) && aUri.Extract(EUriScheme).Length() )
       
   771 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriScheme) );
       
   772 	if ( aUri.IsPresent(EUriUserinfo) && aUri.Extract(EUriUserinfo).Length() )
       
   773 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriUserinfo) );
       
   774 	if ( aUri.IsPresent(EUriHost) && aUri.Extract(EUriHost).Length() )
       
   775 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriHost) );
       
   776 	if ( aUri.IsPresent(EUriPort) && aUri.Extract(EUriPort).Length() )
       
   777 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriPort) );
       
   778 	if ( aUri.IsPresent(EUriPath) && aUri.Extract(EUriPath).Length() )
       
   779 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriPath) );
       
   780 	if ( aUri.IsPresent(EUriQuery) && aUri.Extract(EUriQuery).Length() )
       
   781 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriQuery) );
       
   782 	if ( aUri.IsPresent(EUriFragment) && aUri.Extract(EUriFragment).Length() )
       
   783 		dbTrans->BindTextL ( bindPos++, aUri.Extract(EUriFragment) );
       
   784 	
       
   785 	return dbTrans;			
       
   786 	}
       
   787 
       
   788 /**
       
   789 Do a path match. Path match can be EExactPath, EPartialSuffixPath or EPartialPrefixPath.
       
   790 Only path and scheme components of the URI is considered for this query 
       
   791 
       
   792 Actual filtering of the query results will be done CUriPathFilter class while sending the results.
       
   793 */
       
   794 MDBTransaction* CUriListInterface::DoSearchUriPathLC ( const TUriC8& aUri, InetUriList::TURIMatch aMatchType, TInt aServiceType, TInt aListType /* = KErrNotFound */ )
       
   795 	{
       
   796 	const TDesC8& serviceType (iStringPool.String (URILIST::EServiceType,URILIST::Table).DesC());
       
   797 	const TDesC8& listType (iStringPool.String (URILIST::EListType,URILIST::Table).DesC());
       
   798 	const TDesC8& path ( iStringPool.String (URILIST::EPath,URILIST::Table).DesC() );
       
   799 	const TDesC8& scheme ( iStringPool.String (URILIST::EScheme,URILIST::Table).DesC() );
       
   800 
       
   801 	RBuf8 queryBuf;
       
   802 	InitViewQueryLC(queryBuf);
       
   803 	
       
   804 	if ( aMatchType == InetUriList::EExactPath && aListType == KErrNotFound )	
       
   805 		{
       
   806 		// exact path without listtype
       
   807 		_LIT8 ( KFmtStr, "%S=? and %S=%d and %S=?" );
       
   808 		queryBuf.AppendFormat ( KFmtStr(), &scheme, &serviceType, aServiceType, &path );
       
   809 		}
       
   810 	else if ( aMatchType == InetUriList::EExactPath )
       
   811 		{
       
   812 		// exact path with listtype			
       
   813 		_LIT8 ( KFmtStr, "%S=? and %S=%d and %S=%d and %S=?" );
       
   814 		queryBuf.AppendFormat ( KFmtStr(), &scheme, &serviceType, aServiceType, &listType, aListType, &path );		
       
   815 		}
       
   816 	else if ( aListType == KErrNotFound )
       
   817 		{
       
   818 		// partial path withoutlisttype
       
   819 		_LIT8 ( KFmtStr, "%S=? and %S=%d" );	
       
   820 		queryBuf.AppendFormat ( KFmtStr(), &scheme, &serviceType, aServiceType );
       
   821 		}
       
   822 	else
       
   823 		{
       
   824 		// partial path with listtype
       
   825 		_LIT8 ( KFmtStr, "%S=? and %S=%d and %S=%d" );	
       
   826 		queryBuf.AppendFormat ( KFmtStr(), &scheme, &serviceType, aServiceType, &listType, aListType );			
       
   827 		}
       
   828 	
       
   829 	// Prepare the transaction.
       
   830 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   831 	CleanupStack::PopAndDestroy ();  // queryBuf
       
   832 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   833 	dbTrans->BindTextL ( 0, aUri.Extract ( EUriScheme ) ); // Bind the scheme
       
   834 	if ( aMatchType == InetUriList::EExactPath )
       
   835 		dbTrans->BindTextL ( 1, aUri.Extract ( EUriPath ) ); // Bind the path
       
   836 	return dbTrans;
       
   837 	}
       
   838 
       
   839 /**
       
   840 Do a domain search. Actual domain query ersults will be done in the CUriDomainFilter class
       
   841 */
       
   842 MDBTransaction* CUriListInterface::DoSearchUriDomainLC ( const TUriC8& aUri, TInt aServiceType, TInt aListType /* = KErrNotFound */ )
       
   843 	{
       
   844 	const TDesC8& serviceType (iStringPool.String (URILIST::EServiceType,URILIST::Table).DesC());
       
   845 	const TDesC8& listType (iStringPool.String (URILIST::EListType,URILIST::Table).DesC());
       
   846 	const TDesC8& scheme ( iStringPool.String (URILIST::EScheme,URILIST::Table).DesC() );
       
   847 	RBuf8 queryBuf;
       
   848 	InitViewQueryLC(queryBuf);
       
   849 	
       
   850 	if ( aListType == KErrNotFound )	
       
   851 		{
       
   852 		_LIT8 ( KStr, "%S=? and %S=%d" );
       
   853 		queryBuf.AppendFormat( KStr(), &scheme, &serviceType, aServiceType );
       
   854 		}		
       
   855 	else
       
   856 		{
       
   857 		_LIT8 ( KStrWithListType, "%S=? and %S=%d and %S=%d" );
       
   858 		queryBuf.AppendFormat( KStrWithListType(), &scheme, &serviceType, aServiceType, &listType, aListType );			
       
   859 		}
       
   860 	// Prepare the transaction.
       
   861 	MDBTransaction* dbTrans = iDbAccessor->PrepareTransactionL ( queryBuf );
       
   862 	CleanupStack::PopAndDestroy ();  // queryBuf
       
   863 	CleanupStack::PushL ( TCleanupItem ( CUriListInterface::DestroyTransObj, dbTrans ) );
       
   864 	dbTrans->BindTextL ( 0, aUri.Extract ( EUriScheme ) ); // Bind the scheme
       
   865 
       
   866 	return dbTrans;				
       
   867 	}
       
   868 
       
   869 
       
   870 void CUriListInterface::InitViewQueryLC (  RBuf8& aBuf )
       
   871 	{
       
   872 	_LIT8 ( KSelectStmt, "SELECT * FROM %S where ");
       
   873 	CleanupClosePushL ( aBuf );
       
   874 	aBuf.CreateL ( KMaxDbStmtLen );
       
   875 	aBuf.AppendFormat ( KSelectStmt(), &(KViewName()) );
       
   876 	}
       
   877 	
       
   878 void CUriListInterface::InitTldQueryLC (  RBuf8& aBuf )
       
   879 	{
       
   880 	_LIT8 ( KSelectStmt, "SELECT * FROM %S where ");
       
   881 	CleanupClosePushL ( aBuf );
       
   882 	aBuf.CreateL ( KMaxDbStmtLen );
       
   883 	aBuf.AppendFormat ( KSelectStmt(), &(KTldTblName()) );
       
   884 	}
       
   885 
       
   886 void CUriListInterface::AppendFieldName ( RBuf8& aBuf, TInt aTableIndex, const TUriC8& aUri, TUriComponent aComponent )
       
   887 	{
       
   888 	_LIT8 ( KNormalStr, "%S=? and " );
       
   889 	_LIT8 ( KIsNullStr, "%S ISNULL and " );
       
   890 	
       
   891 	const TPtrC8 value ( aUri.IsPresent ( aComponent ) ? aUri.Extract ( aComponent ) : KNullDesC8() );
       
   892 	if ( value.Length() > 0 )
       
   893 		{
       
   894 		aBuf.AppendFormat ( KNormalStr(), &(GetFieldName(aTableIndex)) );
       
   895 		}
       
   896 	else
       
   897 		{
       
   898 		aBuf.AppendFormat ( KIsNullStr(), &(GetFieldName(aTableIndex)) );
       
   899 		}		
       
   900 	}
       
   901 
       
   902 const TDesC8& CUriListInterface::GetFieldName ( TInt aTableIndex )
       
   903 	{
       
   904 	return iStringPool.String ( aTableIndex,URILIST::Table ).DesC();	
       
   905 	}
       
   906 
       
   907 void CUriListInterface::BindTextL ( MDBTransaction& aDbTrans, TInt aBindPos, const TUriC8& aUri, TUriComponent aComponent )
       
   908 	{
       
   909 	TPtrC8 valuePtr ( KNullDesC8() );
       
   910 	if ( aUri.IsPresent (aComponent) )
       
   911 		valuePtr.Set( aUri.Extract ( aComponent ) );
       
   912 	
       
   913 	aDbTrans.BindTextL ( aBindPos, (valuePtr.Length() == 0) ? KNullDesC8() : valuePtr );
       
   914 	}
       
   915 
       
   916 /**
       
   917 Initializes the database(if it is empty) from the pre-configured XML file. 
       
   918 
       
   919 */
       
   920 void CUriListInterface::InitializeDatabaseL ()
       
   921 	{
       
   922 	const TDesC8& id ( iStringPool.String (URILIST::EId,URILIST::Table).DesC() );
       
   923 	if ( GetMaxIdValueL ( KViewName(), id ) != 0 )
       
   924 		{
       
   925 		// Database already created and thre are some values already added.
       
   926 		//Update with Policyfile installed, after rebooting the server.
       
   927 		UpgradePolicyDatabaseL();
       
   928 		return;
       
   929 		}
       
   930 	// Otherwise go and read the XML file, if exists and populate the data fields
       
   931 	TBuf<KMaxLength> uriListFilePath;
       
   932 	uriListFilePath.FillZ();
       
   933 	//if uriListFilePath is Null then SIS file is not installed
       
   934 	GetInstalledDirectoryL( TUid::Uid(pkgId), KUriListFile(), uriListFilePath );
       
   935 	CUriListInitializer* listInitializer = CUriListInitializer::NewL ( *this );
       
   936 	CleanupStack::PushL ( listInitializer );
       
   937 	TInt err( KErrNone );
       
   938 	if(uriListFilePath.CompareF(KNullDesC) != 0)
       
   939 		{
       
   940 		//File ineturilist.xml exist in external path.
       
   941 		TRAP ( err, listInitializer->ParseDocumentL ( uriListFilePath ) );	
       
   942 		}
       
   943 	CleanupStack::PopAndDestroy (); // listInitializer
       
   944 	if ( err != KErrNotFound && err != KErrPathNotFound ) 
       
   945 		{
       
   946 		// File found and some other error. leave
       
   947 		User::LeaveIfError ( err );
       
   948 		}
       
   949 	//initialise Policy data
       
   950 	InitializePolicyDatabaseL();
       
   951 	}
       
   952 
       
   953 
       
   954 /**
       
   955 Initializes the database to default policy data if it is available in ROM.
       
   956 If a valid upgraded policy file exist then, database will be updated to later version.
       
   957 */
       
   958 void CUriListInterface::InitializePolicyDatabaseL ()
       
   959 	{
       
   960 	//Install the file with ROM Policy data if ROM file exists	
       
   961 	_LIT( KDefaultFilePath, "z:\\private\\20009d70\\tldpolicy.xml");
       
   962 	if( FileExist( KDefaultFilePath() ) )
       
   963 		{
       
   964 		CTldListInitializer* tldlistInitializer = CTldListInitializer::NewL ( *this );
       
   965 		CleanupStack::PushL ( tldlistInitializer );
       
   966 		TRAPD ( policyfileerr, tldlistInitializer->ParseDocumentL ( KDefaultFilePath() ) );
       
   967 		CleanupStack::PopAndDestroy ();	//tldlistInitializer
       
   968 		if ( policyfileerr != KErrNotFound && policyfileerr != KErrPathNotFound ) // File found and some other error. leave
       
   969 			{
       
   970 			User::LeaveIfError ( policyfileerr );	
       
   971 			}
       
   972 		}
       
   973 
       
   974 	UpgradePolicyDatabaseL();
       
   975 	}
       
   976 
       
   977 /**
       
   978 Database will be updated with the Policy file installed.
       
   979 */
       
   980 void CUriListInterface::UpgradePolicyDatabaseL()
       
   981 	{
       
   982 	//Upgrade DB if Policy file is installed
       
   983 	//Get the Installed file path
       
   984 	_LIT(KTldPolicyFile, "c:\\private\\20009d70\\tldpolicy.xml");
       
   985 	if(!FileExist( KTldPolicyFile() ))
       
   986 		{
       
   987 		return;	
       
   988 		}
       
   989 	//Upgrade DB
       
   990 	CTldListInitializer* tldListUpgrader = CTldListInitializer::NewL ( *this );
       
   991 	CleanupStack::PushL ( tldListUpgrader );	
       
   992 	TRAPD ( fileerr, tldListUpgrader->ParseDocumentL ( KTldPolicyFile() ) );
       
   993 	CleanupStack::PopAndDestroy ();//tldListUpgrader
       
   994 	if ( fileerr != KErrNotFound && fileerr != KErrPathNotFound ) // File found and some other error. leave
       
   995 		{
       
   996 		User::LeaveIfError ( fileerr );	
       
   997 		}
       
   998 	}
       
   999 
       
  1000 /**
       
  1001 Get the external file path installed out of ROM.
       
  1002 out parameter -- aFilePath
       
  1003 */
       
  1004 void CUriListInterface::GetInstalledDirectoryL(const TUid aUid, const TDesC& aFile, TDes& aFilePath )
       
  1005 	{
       
  1006 	__ASSERT_DEBUG( aFile.CompareF( KNullDesC ) !=0, User::Invariant( ) );
       
  1007 	Swi::RSisRegistrySession session;
       
  1008 	if(session.Connect( ) != KErrNone)
       
  1009 		{
       
  1010 		//Sis Not Installed
       
  1011 		session.Close();
       
  1012 		return;
       
  1013 		}
       
  1014 	CleanupClosePushL( session );
       
  1015 	TBool installed = session.IsInstalledL( aUid );
       
  1016 	Swi::RSisRegistryEntry  registryEntry;
       
  1017     TInt error = registryEntry.Open( session, aUid );
       
  1018     CleanupClosePushL( registryEntry );
       
  1019     TBool available = EFalse;
       
  1020    	if ( error == KErrNone )
       
  1021 	   	{
       
  1022 		RPointerArray<HBufC> files;
       
  1023 		// get list of files
       
  1024 		TRAPD( err, registryEntry.FilesL( files ) );
       
  1025 		if (err != KErrNone)
       
  1026 			{
       
  1027 			files.ResetAndDestroy();
       
  1028 			CleanupStack::PopAndDestroy(2);	// &registryEntry, &session
       
  1029 			User::Leave(err);
       
  1030 			}
       
  1031 		TInt filesCount = files.Count();
       
  1032 		for (TInt index = 0; index < filesCount; index++)
       
  1033 			{
       
  1034 			TBuf<KMaxLength> fileNameBuf(KNullDesC);
       
  1035 			fileNameBuf.Copy(files[index]->Des());
       
  1036 			TInt pos = fileNameBuf.FindF(aFile);
       
  1037 			if( pos >= 0 && !available && fileNameBuf.Left(pos).CompareF(KRomDirectory)!=0 )
       
  1038 				{
       
  1039 				aFilePath.Copy(fileNameBuf);
       
  1040 				available = ETrue;
       
  1041 				break;
       
  1042 				}
       
  1043 			}
       
  1044 		files.ResetAndDestroy();
       
  1045 		}
       
  1046 	CleanupStack::PopAndDestroy(2);	// &registryEntry, &session	
       
  1047 	}
       
  1048 
       
  1049 /**
       
  1050 Checks if the specified file exists.
       
  1051 
       
  1052 @param aFileName File to check
       
  1053 @return ETrue if the file exists, otherwise EFalse
       
  1054 */
       
  1055 TBool CUriListInterface::FileExist(const TDesC& aFilePath)
       
  1056 	{
       
  1057 	RFs iFs;
       
  1058 	TBool result( EFalse );
       
  1059 	TInt error = iFs.Connect();
       
  1060 	if( !error )
       
  1061 		{
       
  1062 		result = BaflUtils::FileExists( iFs, aFilePath );
       
  1063 		}
       
  1064 	iFs.Close();
       
  1065 	return result;
       
  1066 	}
       
  1067 
       
  1068 /**
       
  1069 return ETrue if Uri is BlackListed, else return EFalse
       
  1070 Leaves with KErrNotSupported if No Policydata is available
       
  1071 */
       
  1072 TBool CUriListInterface::IsBlackListedUriL( const TDesC8& aUri )
       
  1073 	{
       
  1074 	TBool blackListed(EFalse);
       
  1075 	TPolicyQueryArgs blackListArgs ( InetUriList::EBlackList, InetUriList::EPolicyCharSet );
       
  1076 	CUriQueryFilter* queryFilter = QueryWithTldL ( aUri, blackListArgs );
       
  1077 	CleanupStack::PushL(queryFilter);
       
  1078 	MDBTransaction& dbTrans = queryFilter->DBTransaction();
       
  1079 	if ( dbTrans.Next() && queryFilter->MatchRecordL() )
       
  1080 		{
       
  1081 		const TDesC8& charSet = dbTrans.ColumnTextL ( TLDLIST::ECharacterSet );
       
  1082 		//Policy file contains empty body for Requested white/Black listed data
       
  1083 		__ASSERT_ALWAYS( charSet.Length() > 0, User::Invariant() );
       
  1084 		RArray<TChar> policyCharList;
       
  1085 		CleanupClosePushL(policyCharList);
       
  1086 		//Tokenize the results
       
  1087 		TokenizeStringL( charSet, policyCharList);
       
  1088 		//Get the host and check whether any of these char is in PolicyCharList
       
  1089 		HBufC8* host = ExtractHostL(aUri);
       
  1090 		CleanupStack::PushL(host);
       
  1091 		blackListed = IsBlackListedHost( host->Des(), policyCharList ); 
       
  1092 		CleanupStack::PopAndDestroy(2);	//host, PolicyCharList
       
  1093 		}
       
  1094 	else
       
  1095 		{
       
  1096 		//Requested Policydata Not available for this TLD
       
  1097 		CleanupStack::PopAndDestroy(queryFilter);
       
  1098 		User::Leave(InetUriList::KErrInvalidTLD);
       
  1099 		}
       
  1100 	CleanupStack::PopAndDestroy(queryFilter);
       
  1101 	return blackListed;
       
  1102 	}
       
  1103 
       
  1104 /**
       
  1105 return ETrue if Uri is WhiteListed, else return EFalse
       
  1106 Leaves with KErrNotSupported if No Policydata is available
       
  1107 */
       
  1108 TBool CUriListInterface::IsWhiteListedUriL( const TDesC8& aUri )
       
  1109 	{
       
  1110 	TBool whiteListed (EFalse);
       
  1111 	TPolicyQueryArgs whiteListArgs ( InetUriList::EWhiteList, InetUriList::EPolicyCharSet );
       
  1112 	CUriQueryFilter* queryFilter = QueryWithTldL ( aUri, whiteListArgs );
       
  1113 	CleanupStack::PushL(queryFilter);
       
  1114 	MDBTransaction& dbTrans = queryFilter->DBTransaction();
       
  1115 	if ( dbTrans.Next() && queryFilter->MatchRecordL() )
       
  1116 		{
       
  1117 		const TDesC8& charSet = dbTrans.ColumnTextL ( TLDLIST::ECharacterSet );
       
  1118 		//Policy file contains empty body for Requested white/Black listed data
       
  1119 		__ASSERT_ALWAYS( charSet.Length() > 0, User::Invariant() );
       
  1120 		RArray<TChar> policyCharList;
       
  1121 		CleanupClosePushL(policyCharList);
       
  1122 		//Tokenize the results
       
  1123 		TokenizeStringL(charSet, policyCharList);
       
  1124 		//Get the host and check whether any of these char is in PolicyCharList
       
  1125 		HBufC8* host = ExtractHostL(aUri);
       
  1126 		CleanupStack::PushL(host);
       
  1127 		whiteListed = IsWhiteListedHostL( host->Des(), policyCharList ); 
       
  1128 		CleanupStack::PopAndDestroy(2);	// PolicyCharList,host
       
  1129 		}
       
  1130 	else
       
  1131 		{
       
  1132 		//Requested Policydata Not available for this TLD
       
  1133 		CleanupStack::PopAndDestroy(queryFilter);
       
  1134 		User::Leave(InetUriList::KErrInvalidTLD);
       
  1135 		}
       
  1136 	CleanupStack::PopAndDestroy(queryFilter);
       
  1137 	return whiteListed;
       
  1138 	}
       
  1139 
       
  1140 /**
       
  1141 returns ETrue if aHost is white listed else returns EFalse
       
  1142 */
       
  1143 TBool CUriListInterface::IsWhiteListedHostL( const TDesC8& aHost, RArray<TChar>& aPolicyList )
       
  1144 	{
       
  1145 	//Every character in the host must be in the list
       
  1146 	RArray<TChar> hostCharList;
       
  1147 	CleanupClosePushL(hostCharList);
       
  1148 	StringToCharListL(aHost, hostCharList);
       
  1149 	TBool whiteList(ETrue);
       
  1150 	for(TInt index(0); index < hostCharList.Count(); index++ )
       
  1151 		{
       
  1152 		if(!IsNextCharInList(hostCharList[index], aPolicyList))	
       
  1153 			{
       
  1154 			whiteList = EFalse;
       
  1155 			break;
       
  1156 			}
       
  1157 		}
       
  1158 	CleanupStack::PopAndDestroy();//hostCharList
       
  1159 	return whiteList;
       
  1160 	}
       
  1161 
       
  1162 /**
       
  1163 Returns ETrue if aChar is in aPolicyList else returns EFalse
       
  1164 */
       
  1165 TBool CUriListInterface::IsNextCharInList(TChar aChar, RArray<TChar>& aPolicyList)
       
  1166 	{
       
  1167 	TBool found(EFalse);
       
  1168 	for (TInt index(0); index < aPolicyList.Count(); index++)
       
  1169 		{
       
  1170 		if( aPolicyList[index] == aChar )	
       
  1171 			{
       
  1172 			found =  ETrue;	
       
  1173 			break;
       
  1174 			}
       
  1175 		}
       
  1176 	return found;
       
  1177 	}
       
  1178 
       
  1179 /**
       
  1180 Tokenizes the policydata into characters list
       
  1181 */
       
  1182 void CUriListInterface::TokenizeStringL(const TDesC8& aString, RArray<TChar>& aList, TChar aSeparator)
       
  1183 	{
       
  1184 	TLex8 lexer(aString);
       
  1185 	while(!lexer.Eos())
       
  1186 		{
       
  1187 		TChar charcter = lexer.Get();
       
  1188 		if ( charcter != aSeparator && charcter != '\n' &&  charcter != '\t' )
       
  1189 			{
       
  1190 			aList.AppendL(charcter);	
       
  1191 			}
       
  1192 		}
       
  1193 	}
       
  1194 
       
  1195 /**
       
  1196 Tokenizes the Host into characters list
       
  1197 */
       
  1198 void CUriListInterface::StringToCharListL(const TDesC8& aString, RArray<TChar>& aList, TChar aSeparator)
       
  1199 	{
       
  1200 	//Here sepearator is '.'
       
  1201 	TokenizeStringL( aString, aList, aSeparator );
       
  1202 	}
       
  1203 
       
  1204 /**
       
  1205 returns ETrue if aHost is Black listed else returns EFalse
       
  1206 */
       
  1207 TBool CUriListInterface::IsBlackListedHost( const TDesC8& aHost, RArray<TChar>& aList )
       
  1208 	{
       
  1209 	TBool result( EFalse );
       
  1210 	for(TInt index(0); index < aList.Count(); index++ )	
       
  1211 		{
       
  1212 		if ( aHost.Locate(aList[index]) >= 0	)
       
  1213 			{
       
  1214 			result = ETrue;
       
  1215 			break;			
       
  1216 			}
       
  1217 		}
       
  1218 	return result;
       
  1219 	}
       
  1220 
       
  1221 /**
       
  1222 Extracts the TLD from the given uri.
       
  1223 */
       
  1224 HBufC8* CUriListInterface::ExtractTldL( const TDesC8& aUri )
       
  1225 	{
       
  1226 	HBufC8* tld = ExtractHostL( aUri );
       
  1227 	CleanupStack::PushL( tld );
       
  1228 	TChar dot('.');
       
  1229 	TInt pos = tld->LocateReverse( dot );
       
  1230 	__ASSERT_DEBUG( ( pos >= 0 ) , User::Invariant() );
       
  1231 	HBufC8* tldPtr = tld->Mid( pos + 1 ).AllocL(); //Excluding dot '.'
       
  1232 	CleanupStack::PopAndDestroy( tld );
       
  1233 	return tldPtr;
       
  1234 	}
       
  1235 	
       
  1236 /**
       
  1237 Extracts the Host from the given uri.
       
  1238 */
       
  1239 HBufC8* CUriListInterface::ExtractHostL( const TDesC8& aUri )
       
  1240 	{
       
  1241 	TUriParser8 tldUri1;
       
  1242 	RBuf8 tld;
       
  1243 	RBuf8 customisedUri;
       
  1244 	HBufC8* tldPtr;
       
  1245 	User::LeaveIfError ( tldUri1.Parse ( aUri ) );
       
  1246 	TBool isSchemePresent = tldUri1.IsPresent( EUriScheme );
       
  1247 	if (!isSchemePresent)
       
  1248 	{
       
  1249         _LIT8(KHttp, "http://");
       
  1250 	    RBuf8 urirbuf;
       
  1251 	    urirbuf.CleanupClosePushL();
       
  1252 	    urirbuf.CreateL(KHttp,aUri.Length()+7);
       
  1253 	    urirbuf.Append(aUri);
       
  1254 	    customisedUri.Create( DoNormalisationLC ( urirbuf ) );
       
  1255 	    TUriParser8 tldUri2;
       
  1256 	    User::LeaveIfError ( tldUri2.Parse ( customisedUri ) );
       
  1257 	    __ASSERT_ALWAYS(tldUri2.IsPresent( EUriHost ), User::Invariant());
       
  1258 	    tld.Create( tldUri2.Extract(EUriHost) );
       
  1259 	    tldPtr = tld.AllocL();
       
  1260 	    CleanupStack::PopAndDestroy(2); //calls aRBuf.Close()and DoNormalisationLC pop
       
  1261 	    
       
  1262 	 }
       
  1263 	else 
       
  1264 	{   
       
  1265         customisedUri.Create( DoNormalisationLC ( aUri ) );
       
  1266         User::LeaveIfError ( tldUri1.Parse ( customisedUri ) );
       
  1267         __ASSERT_ALWAYS(tldUri1.IsPresent( EUriHost ), User::Invariant());
       
  1268         tld.Create( tldUri1.Extract(EUriHost) );
       
  1269         tldPtr = tld.AllocL();
       
  1270         CleanupStack::PopAndDestroy ();//objects added in DoNormalisationLC
       
  1271     }
       
  1272 	 tld.Close();
       
  1273 	 customisedUri.Close();
       
  1274 	 return tldPtr;
       
  1275 	}	
       
  1276 
       
  1277 /**
       
  1278 Does the syntax based normalisation. If the MUriCustomiser is set then it customises the URI, 
       
  1279 that is, do the protocol/scheme-based normalisation on the URI. The customisation algorithm needs 
       
  1280 to be implemented by the application. 
       
  1281 */
       
  1282 const TDesC8& CUriListInterface::DoNormalisationLC ( const TDesC8& aUri )
       
  1283 	{
       
  1284 	// Do a syntax based normalisation
       
  1285 	TUriParser8 uriParser;
       
  1286 	User::LeaveIfError ( uriParser.Parse ( aUri ) );
       
  1287 	CUri8* normalisedUri = UriUtils::NormaliseUriL ( uriParser );	
       
  1288 	CleanupStack::PushL ( normalisedUri );
       
  1289 	return 	normalisedUri->Uri().UriDes();	
       
  1290 	}
       
  1291