syncmlfw/dm/provisioningadapter/src/NSmlDmProvisioningAdapter.cpp
changeset 0 b497e44ab2fc
child 24 bf47f3b79154
child 34 696f5dd11939
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Main class for handling provisioning Provisioning message
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <badesca.h>
       
    22 #include <commdb.h>
       
    23 #include <cdbcols.h>			 // CommsDB columname defs
       
    24 #include <stringresourcereader.h>
       
    25 #include <barsread.h>
       
    26 #include <NSmlDMProvisioningAdapter.rsg>
       
    27 #include <f32file.h>
       
    28 #include <bautils.h>
       
    29 #include <ApUtils.h>
       
    30 #include <utf.h>
       
    31 #include <featmgr.h>
       
    32 #include <barsc.h> 
       
    33 
       
    34 #include <nsmldebug.h>
       
    35 #include <CWPCharacteristic.h>
       
    36 #include <CWPParameter.h>
       
    37 #include <CWPEngine.h>
       
    38 #include <NSmlPrivateAPI.h>
       
    39 #include "NSmlDmProvisioningAdapter.h"
       
    40 #include "NSmlTransportHandler.h"
       
    41 
       
    42 #include <data_caging_path_literals.hrh>
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CNSmlDmProvisioningAdapter::CNSmlDmProvisioningAdapter
       
    48 // C++ default constructor can NOT contain any code, that
       
    49 // might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 CNSmlDmProvisioningAdapter::CNSmlDmProvisioningAdapter()
       
    52 	:CWPAdapter(), iAuthSecretLimitIndicator(0)
       
    53 	{
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CNSmlDmProvisioningAdapter::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 void CNSmlDmProvisioningAdapter::ConstructL()
       
    61 	{
       
    62 	iSession.OpenL();
       
    63 	FeatureManager::InitializeLibL();
       
    64 	}
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CNSmlDmProvisioningAdapter::NewL
       
    68 // Two-phased constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 CNSmlDmProvisioningAdapter* CNSmlDmProvisioningAdapter::NewL()
       
    71 	{
       
    72 	_DBG_FILE("CNSmlDmProvisioningAdapter::NewL(): begin");
       
    73 	CNSmlDmProvisioningAdapter* self = new(ELeave) CNSmlDmProvisioningAdapter; 
       
    74 	CleanupStack::PushL( self );
       
    75 	self->ConstructL();
       
    76 	CleanupStack::Pop( self );
       
    77 	_DBG_FILE("CNSmlDmProvisioningAdapter::NewL(): end");
       
    78 	return self;
       
    79 	}
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // Destructor
       
    83 // -----------------------------------------------------------------------------
       
    84 CNSmlDmProvisioningAdapter::~CNSmlDmProvisioningAdapter()
       
    85 	{
       
    86 	iProfiles.ResetAndDestroy();
       
    87 	iProfiles.Close();
       
    88 	
       
    89 	delete iTitle;
       
    90 	iSession.Close();
       
    91 	FeatureManager::UnInitializeLib();
       
    92 	}
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // Destructor
       
    96 // -----------------------------------------------------------------------------
       
    97 CNSmlDmProfileElement::~CNSmlDmProfileElement()
       
    98 	{
       
    99 	iVisitParameter = 0;
       
   100 	
       
   101 	delete iServerNonce;
       
   102 	delete iHostAddress;
       
   103 	delete iPort;
       
   104 	delete iHTTPUserName;
       
   105 	delete iHTTPPassword;
       
   106 	
       
   107 	delete iDisplayName;
       
   108 	delete iServerId;
       
   109 	delete iUsername;
       
   110 	delete iPassword;
       
   111 	delete iServerPassword;
       
   112 	}
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CNSmlDmProvisioningAdapter::CanSetAsDefault
       
   116 // -----------------------------------------------------------------------------
       
   117 TBool CNSmlDmProvisioningAdapter::CanSetAsDefault(TInt /*aItem*/) const
       
   118 	{
       
   119 	return EFalse;
       
   120 	}
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CNSmlDmProvisioningAdapter::SetAsDefaultL
       
   124 // -----------------------------------------------------------------------------
       
   125 void CNSmlDmProvisioningAdapter::SetAsDefaultL(TInt /*aItem*/ )
       
   126 	{
       
   127 	}
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CNSmlDmProvisioningAdapter::ItemCount
       
   131 // -----------------------------------------------------------------------------
       
   132 TInt CNSmlDmProvisioningAdapter::ItemCount() const
       
   133 	{
       
   134 	return iProfiles.Count();
       
   135 	}
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CNSmlDmProvisioningAdapter::SummaryTitle
       
   139 // -----------------------------------------------------------------------------
       
   140 const TDesC16& CNSmlDmProvisioningAdapter::SummaryTitle(TInt /*aIndex*/) const
       
   141 	{
       
   142 	return *iTitle;
       
   143 	}
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CNSmlDmProvisioningAdapter::SummaryText
       
   147 // -----------------------------------------------------------------------------
       
   148 const TDesC16& CNSmlDmProvisioningAdapter::SummaryText(TInt aIndex) const
       
   149 	{
       
   150 	return *iProfiles[aIndex]->iDisplayName;
       
   151 	}
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CNSmlDmProvisioningAdapter::SaveL
       
   155 // -----------------------------------------------------------------------------
       
   156 void CNSmlDmProvisioningAdapter::SaveL(TInt aItem)
       
   157 	{
       
   158 		_DBG_FILE("CNSmlDmProvisioningAdapter::SaveL(): begin");
       
   159 	TInt serverID = 0;
       
   160 	TInt hostAddress = 0;
       
   161 	TInt username = 0;
       
   162 
       
   163 	if(iProfiles[aItem]->iServerId && iProfiles[aItem]->iHostAddress && 
       
   164 	iProfiles[aItem]->iUsername)
       
   165 	{
       
   166 		TPtrC8 serverPtr = iProfiles[aItem]->iServerId->Des();
       
   167 		TPtrC hostAddressPtr = iProfiles[aItem]->iHostAddress->Des();
       
   168 		TPtrC8 usernamePtr = iProfiles[aItem]->iUsername->Des();		
       
   169 		serverID = serverPtr.Compare(KNSmlDMProvisioningNoVal);
       
   170 		hostAddress = hostAddressPtr.Compare(KNSmlDMProvisioningNoVal16);
       
   171 		username = usernamePtr.Compare(KNSmlDMProvisioningNoVal);
       
   172 	}
       
   173 	else
       
   174 	{
       
   175 		User::Leave(KErrGeneral);
       
   176 	}
       
   177 
       
   178 	if( ( serverID == 0 ) || ( hostAddress == 0 ) || ( username == 0 ))
       
   179 	{
       
   180 		User::Leave(KErrGeneral);
       
   181 	}
       
   182 	if (iAuthSecretLimitIndicator)
       
   183 	{
       
   184 		iAuthSecretLimitIndicator = 0;
       
   185 		User::Leave(KErrOverflow);
       
   186 	}
       
   187 
       
   188 	TPckgBuf<TUint32> uid;
       
   189 	
       
   190 	RSyncMLDevManProfile profile,ProfileToSearch;
       
   191 	RArray<TSmlProfileId> arr;
       
   192   iSession.ListProfilesL( arr, ESmlDevMan );
       
   193   TBool ret = EFalse;
       
   194   TInt ProfileId = KErrNotFound;
       
   195   CleanupClosePushL(arr);   	
       
   196 	for ( TInt index = 0; index < arr.Count(); index++ )
       
   197 		{
       
   198 	    TRAPD( error, ProfileToSearch.OpenL(iSession, arr[index], ESmlOpenRead ) );
       
   199 	    if ( error == KErrNone )
       
   200 	        {	            	            	        
       
   201     	        if ( ( iProfiles[aItem]->iServerId->Des()).Compare(ProfileToSearch.ServerId() ) == 0 )
       
   202     	            {
       
   203     	            
       
   204                     ret = ETrue;                    
       
   205                     ProfileId = (TInt)arr[index];
       
   206                     ProfileToSearch.Close();
       
   207                     break;
       
   208                     }	            		    
       
   209 	        }
       
   210 	      ProfileToSearch.Close();  
       
   211 		}
       
   212     CleanupStack::PopAndDestroy( &arr );
       
   213 	
       
   214 	if( ret )
       
   215 		{
       
   216 		profile.OpenL(iSession, ProfileId , ESmlOpenReadWrite );	
       
   217 		TInt isprofilelocked=profile.ProfileLocked(EFalse, EFalse);
       
   218 		if (isprofilelocked == 1)
       
   219 		 {
       
   220 		  
       
   221       profile.Close(); 
       
   222       User::Leave(KErrAccessDenied); 	 	
       
   223 		 }
       
   224 		
       
   225 		}
       
   226 	else
       
   227 		{
       
   228 	profile.CreateL( iSession );
       
   229 		}
       
   230 	CleanupClosePushL( profile );
       
   231 	// ui needs this to be set 0
       
   232 	profile.SetCreatorId(0);
       
   233 	
       
   234 	profile.SetSanUserInteractionL( iProfiles[aItem]->iServerAlertedAction );
       
   235 	
       
   236 	if ( iProfiles[aItem]->iDisplayName )
       
   237 	    {	    
       
   238 	    profile.SetDisplayNameL( iProfiles[aItem]->iDisplayName->Des() );    
       
   239 	    }
       
   240 	
       
   241 	if ( iProfiles[aItem]->iServerId )
       
   242 	    {	    
       
   243 	    profile.SetServerIdL( iProfiles[aItem]->iServerId->Des() );    
       
   244 	    }
       
   245 	
       
   246     if ( iProfiles[aItem]->iServerPassword )
       
   247 	    {	    
       
   248 	    profile.SetServerPasswordL( iProfiles[aItem]->iServerPassword->Des() );    
       
   249 	    }
       
   250 		        
       
   251 	if ( iProfiles[aItem]->iUsername )   
       
   252 	    {	    
       
   253 	    profile.SetUserNameL( iProfiles[aItem]->iUsername->Des() );    
       
   254 	    }
       
   255 	
       
   256 	if ( iProfiles[aItem]->iPassword )
       
   257 	    {	    
       
   258 	    profile.SetPasswordL( iProfiles[aItem]->iPassword->Des() );    
       
   259 	    }
       
   260 		    
       
   261 	// creates profile -> must be done before opening the connection
       
   262 	profile.UpdateL();
       
   263 	
       
   264 	RSyncMLConnection connection;
       
   265 	connection.OpenL( profile, KUidNSmlMediumTypeInternet.iUid );
       
   266 	CleanupClosePushL( connection );
       
   267 		
       
   268 	if( iProfiles[aItem]->iVisitParameter && iProfiles[aItem]->iVisitParameter->Data().Length() == uid.MaxLength() )
       
   269 		{
       
   270 		uid.Copy( iProfiles[aItem]->iVisitParameter->Data() );
       
   271 
       
   272 		CCommsDatabase* commDb = CCommsDatabase::NewL();
       
   273 		CleanupStack::PushL( commDb );
       
   274 		CApUtils* aputils = CApUtils::NewLC( *commDb );
       
   275 
       
   276 		TUint apId = 0;
       
   277 		TRAPD( ERROR, apId = aputils->IapIdFromWapIdL( uid() ) );
       
   278 		if( ERROR != KErrNone )
       
   279 			{
       
   280 			apId = GetDefaultIAPL();
       
   281 			}
       
   282 				
       
   283 		HBufC8* iapBuf = HBufC8::NewLC( 8 );
       
   284 		TPtr8 ptrBuf = iapBuf->Des();
       
   285 		ptrBuf.Num( apId );
       
   286 
       
   287 		connection.SetPropertyL( KNSmlIAPId, *iapBuf );
       
   288 		
       
   289 		CleanupStack::PopAndDestroy( 3 ); //commdb, aputils, iapBuf		
       
   290 		}
       
   291 		
       
   292 	if( iProfiles[aItem]->iHostAddress )
       
   293 		{
       
   294 		// see if address contains also port
       
   295 		TBool portFound = EFalse;
       
   296 		
       
   297 		TInt startPos(0);
       
   298 		if(iProfiles[aItem]->iHostAddress->Find(KNSmlDMProvisioningHTTP)==0)
       
   299 		    {
       
   300 		    startPos=KNSmlDMProvisioningHTTP().Length();		    		    
       
   301 		    }
       
   302 		else if(iProfiles[aItem]->iHostAddress->Find(KNSmlDMProvisioningHTTPS)==0)
       
   303 		    {
       
   304 		    startPos=KNSmlDMProvisioningHTTPS().Length();		    		    
       
   305 		    }
       
   306 		TPtrC uriPtr = iProfiles[aItem]->iHostAddress->Mid(startPos);
       
   307 		
       
   308 		if(uriPtr.Locate(KNSmlDMColon)!=KErrNotFound)
       
   309 		    {
       
   310 			portFound = ETrue;
       
   311 		    }
       
   312 	
       
   313 		if( portFound == EFalse )
       
   314 			{
       
   315 			HBufC *uri = 0;
       
   316 			// port not found from URI -> see if it is given separately				
       
   317 			if( iProfiles[aItem]->iPort )
       
   318 				{
       
   319 				// parse address and port into URI				
       
   320 				if( CombineURILC( iProfiles[aItem]->iHostAddress->Des(),
       
   321 							  iProfiles[aItem]->iPort->Des(), uri ) == KErrNone )
       
   322 					{
       
   323 					if(iProfiles[aItem]->iHostAddress)
       
   324 					{
       
   325 						delete iProfiles[aItem]->iHostAddress;
       
   326 						iProfiles[aItem]->iHostAddress = NULL;
       
   327 					}
       
   328 
       
   329 					iProfiles[aItem]->iHostAddress = uri->Des().AllocL();
       
   330 					}
       
   331 				CleanupStack::PopAndDestroy(); // uri		  
       
   332 				}
       
   333 			else
       
   334 				{
       
   335 				// use default port
       
   336 				if( CombineURILC( iProfiles[aItem]->iHostAddress->Des(),
       
   337 							  KNSmlDmDefaultPort(), uri ) == KErrNone )
       
   338 					{
       
   339 					if(iProfiles[aItem]->iHostAddress)
       
   340 					{
       
   341 						delete iProfiles[aItem]->iHostAddress;
       
   342 						iProfiles[aItem]->iHostAddress = NULL;
       
   343 					}
       
   344 
       
   345 					iProfiles[aItem]->iHostAddress = uri->Des().AllocL();
       
   346 					}
       
   347 				CleanupStack::PopAndDestroy(); // uri					
       
   348 				}
       
   349 			}
       
   350 		
       
   351 
       
   352 		connection.SetServerURIL( ConvertTo8LC( iProfiles[aItem]->iHostAddress->Des() ) );
       
   353 		CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   354 		}
       
   355 			
       
   356 	// set authtype, HTTPUserName, HTTPPassword
       
   357 	if( iProfiles[aItem]->iHTTPUserName ||
       
   358 	    iProfiles[aItem]->iHTTPPassword )
       
   359 		{
       
   360 		connection.SetPropertyL( KNSmlHTTPAuth, KNSmlTrueVal );
       
   361 		
       
   362 		if( iProfiles[aItem]->iHTTPUserName )
       
   363 			{
       
   364 			connection.SetPropertyL( KNSmlHTTPUsername, iProfiles[aItem]->iHTTPUserName->Des() );
       
   365 			}
       
   366 		if( iProfiles[aItem]->iHTTPPassword )
       
   367 			{
       
   368 			connection.SetPropertyL( KNSmlHTTPPassword, iProfiles[aItem]->iHTTPPassword->Des() );
       
   369 			}
       
   370 		}
       
   371 	else
       
   372 		{
       
   373 		connection.SetPropertyL( KNSmlHTTPAuth, KNSmlFalseVal );
       
   374 		}
       
   375 		
       
   376 	connection.UpdateL();
       
   377 	CleanupStack::PopAndDestroy(); //connection
       
   378 
       
   379 	profile.UpdateL();
       
   380 
       
   381 	if( iProfiles[aItem]->iServerNonce )
       
   382 		{
       
   383 		RNSmlPrivateAPI privateApi;
       
   384 		iProfiles[aItem]->iAuthInfo.iProfileId = profile.Identifier();		
       
   385  		iProfiles[aItem]->iAuthInfo.iServerNonce = ConvertTo8LC( iProfiles[aItem]->iServerNonce->Des() ).AllocL();
       
   386  		CleanupStack::PopAndDestroy(); //ConvertTo8LC 
       
   387  		iProfiles[aItem]->iAuthInfo.iClientNonce = KNullDesC8().AllocL();
       
   388  		
       
   389  		privateApi.OpenL();
       
   390  		CleanupClosePushL( privateApi );
       
   391  		TInt err( KErrNone );
       
   392  		TRAP(err, privateApi.SetDMAuthInfoL( iProfiles[aItem]->iAuthInfo ));
       
   393  		CleanupStack::PopAndDestroy(); // privateApi
       
   394 		}
       
   395 
       
   396 	iProfiles[aItem]->iProfileId = profile.Identifier(); 
       
   397 	if(iInitSession&(0x01<<(aItem+1)))
       
   398 	    {
       
   399     	_DBG_FILE("CNSmlDmProvisioningAdapter::SaveL(): init session");
       
   400 	    TInt serverIdLen = profile.ServerId().Length();
       
   401 	    TInt staticPartLen = KNSmlDmStaticAlertMessagePart().Length();
       
   402 	    HBufC8* alertMessage = HBufC8::NewLC(staticPartLen+3+serverIdLen);
       
   403 	    TPtr8 alertPtr = alertMessage->Des();
       
   404 	    TPckgBuf<TUint8> sessionIdLo(1);
       
   405 	    TPckgBuf<TUint8> sessionIdHi(0);
       
   406 	    TPckgBuf<TUint8> serverIdLenBuf(serverIdLen);
       
   407 	    alertPtr.Append(KNSmlDmStaticAlertMessagePart);
       
   408 	    alertPtr.Append(sessionIdHi);
       
   409 	    alertPtr.Append(sessionIdLo);
       
   410 	    alertPtr.Append(serverIdLenBuf);
       
   411 	    alertPtr.Append(profile.ServerId());
       
   412 		DBG_ARGS8( alertMessage->Ptr());
       
   413 	    
       
   414 		RNSmlPrivateAPI privateApi;
       
   415 		privateApi.OpenL();
       
   416 		CleanupClosePushL(privateApi);
       
   417 		if(!FeatureManager::FeatureSupported( KFeatureIdSyncMlDm112  ))
       
   418 		{
       
   419 		privateApi.SendL( alertPtr, ESmlDevMan, ESmlVersion1_2 );	
       
   420 		}
       
   421 		else
       
   422 		{
       
   423 		privateApi.SendL( alertPtr, ESmlDevMan, ESmlVersion1_1_2 );	
       
   424 		}
       
   425 	    CleanupStack::PopAndDestroy(); //privateApi
       
   426 	    
       
   427 	    CleanupStack::PopAndDestroy(alertMessage);	    
       
   428 	    }
       
   429 
       
   430 	CleanupStack::PopAndDestroy( &profile );
       
   431 
       
   432 	_DBG_FILE("CNSmlDmProvisioningAdapter::SaveL(): end");
       
   433 	}
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CNSmlDmProvisioningAdapter::Reserved_1
       
   437 // -----------------------------------------------------------------------------
       
   438 TInt CNSmlDmProvisioningAdapter::Reserved_1()
       
   439 	{
       
   440 	return KErrNotSupported;
       
   441 	}
       
   442 
       
   443 // -----------------------------------------------------------------------------
       
   444 // CNSmlDmProvisioningAdapter::Reserved_2
       
   445 // -----------------------------------------------------------------------------
       
   446 TInt CNSmlDmProvisioningAdapter::Reserved_2()
       
   447 	{
       
   448 	return KErrNotSupported;
       
   449 	}
       
   450 
       
   451 // -----------------------------------------------------------------------------
       
   452 // CNSmlDmProvisioningAdapter::VisitL
       
   453 // -----------------------------------------------------------------------------
       
   454 void CNSmlDmProvisioningAdapter::VisitL( CWPCharacteristic& aCharacteristic )
       
   455 	{
       
   456 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPCharacteristic): begin");
       
   457 	if(aCharacteristic.Type() == KWPApplication)
       
   458 		{
       
   459 		iState = CNSmlDmProvisioningAdapter::EStateApplication;
       
   460 		}
       
   461 	else if(aCharacteristic.Type() == KWPAppAuth)
       
   462 		{
       
   463 		iAuthLevel = CNSmlDmProvisioningAdapter::ENone;
       
   464 		if(iState != CNSmlDmProvisioningAdapter::EStateDmSettings)
       
   465 			{
       
   466 			iState = CNSmlDmProvisioningAdapter::EStateNull;
       
   467 			}
       
   468 		}
       
   469 	else if(aCharacteristic.Type() == KWPAppAddr || aCharacteristic.Type() == KWPResource || aCharacteristic.Type() == KWPPort)
       
   470 		{
       
   471 		if(iState != CNSmlDmProvisioningAdapter::EStateDmSettings)
       
   472 			{
       
   473 			iState = CNSmlDmProvisioningAdapter::EStateNull;
       
   474 			}
       
   475 		}
       
   476 	else
       
   477 		{
       
   478 		iState = CNSmlDmProvisioningAdapter::EStateNull;
       
   479 		}
       
   480 
       
   481 	if(iState!=CNSmlDmProvisioningAdapter::EStateNull)
       
   482 		{
       
   483 		aCharacteristic.AcceptL( *this );
       
   484 		}
       
   485 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPCharacteristic): end");
       
   486 	}
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CNSmlDmProvisioningAdapter::VisitL
       
   490 // -----------------------------------------------------------------------------
       
   491 void CNSmlDmProvisioningAdapter::VisitL( CWPParameter& aParameter)
       
   492 	{
       
   493 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): begin");
       
   494 	
       
   495 	if( iState == CNSmlDmProvisioningAdapter::EStateApplication )
       
   496 		{
       
   497 		if( aParameter.ID() == EWPParameterAppID && aParameter.Value().Compare( KNSmlDMProvisioningDMAppIdVal ) == 0 )
       
   498 			{
       
   499 			iState = CNSmlDmProvisioningAdapter::EStateDmSettings;
       
   500 	
       
   501 			GetTitleL();
       
   502 
       
   503 			CNSmlDmProfileElement* dmProfileElement = new(ELeave) CNSmlDmProfileElement;
       
   504 			CleanupStack::PushL( dmProfileElement );
       
   505 			
       
   506 			dmProfileElement->iDisplayName = KNSmlDMProvisioningDefName().AllocL();			
       
   507 			dmProfileElement->iServerAlertedAction = ESmlConfirmSync;			
       
   508 			
       
   509 			dmProfileElement->iServerNonce = 0;
       
   510 			dmProfileElement->iVisitParameter = 0;			
       
   511 
       
   512 	 		dmProfileElement->iAuthInfo.iAuthPref = ENoAuth;
       
   513 
       
   514 			iProfiles.AppendL( dmProfileElement );
       
   515 			CleanupStack::Pop( dmProfileElement );
       
   516 			}
       
   517 		else
       
   518 			{
       
   519 			iState = CNSmlDmProvisioningAdapter::EStateNull;
       
   520 			}
       
   521 		}
       
   522 	else if( iState == CNSmlDmProvisioningAdapter::EStateDmSettings )
       
   523 		{
       
   524 		switch ( aParameter.ID() )
       
   525 			{
       
   526 			case EWPParameterName:
       
   527 			case EWPParameterName1: {
       
   528 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): Name");
       
   529 				DBG_ARGS( aParameter.Value().Ptr() );
       
   530 				
       
   531 				if ( iProfiles[iProfiles.Count()-1]->iDisplayName )
       
   532 				    {
       
   533 				    delete iProfiles[iProfiles.Count()-1]->iDisplayName;
       
   534 				    iProfiles[iProfiles.Count()-1]->iDisplayName = NULL;				        
       
   535 				    }
       
   536 				    
       
   537 				if(aParameter.Value().Length()>KNSmlDmMaxProfileNameLength)
       
   538 					{
       
   539 						TPtrC ptr2 = aParameter.Value().Left(KNSmlDmMaxProfileNameLength);
       
   540 						iProfiles[iProfiles.Count()-1]->iDisplayName = ptr2.AllocL();	
       
   541 					}
       
   542 				else
       
   543 				iProfiles[iProfiles.Count()-1]->iDisplayName = aParameter.Value().AllocL();								
       
   544 				/* parameter is NULL set to default value*/	    		
       
   545 	    		TInt num = iProfiles[iProfiles.Count()-1]->iDisplayName->Length();
       
   546 	    			
       
   547 				/*check for white spaces, If all characters are white spaces set to default*/
       
   548 				TPtr temp = iProfiles[iProfiles.Count()-1]->iDisplayName->Des();
       
   549 				TInt i;
       
   550 				for(i=0; i<num; i++)
       
   551 	    			{			
       
   552 	    				if(temp[i] != ' ')
       
   553 	    				break;
       
   554 	    			}
       
   555 			    TInt flag =0;					
       
   556 	    			
       
   557     			if(i == num || num == 0 )
       
   558     			  flag =1;
       
   559     			
       
   560     			if(flag)
       
   561     			{
       
   562     				if ( iProfiles[iProfiles.Count()-1]->iDisplayName )
       
   563 			    	{
       
   564 			    	delete iProfiles[iProfiles.Count()-1]->iDisplayName;
       
   565 			    	iProfiles[iProfiles.Count()-1]->iDisplayName = NULL;
       
   566 			    	}
       
   567 			    	iProfiles[iProfiles.Count()-1]->iDisplayName = KNSmlDMProvisioningDefName().AllocL();
       
   568     			}
       
   569 				break;
       
   570 				}
       
   571 			case EWPParameterToProxy:	
       
   572 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): ToProxy");
       
   573 			case EWPParameterToNapID:
       
   574 			case EWPParameterToNapID1:
       
   575 				break;
       
   576 
       
   577 			case EWPParameterPortNbr:
       
   578 			case EWPParameterPortNbr1:
       
   579 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): PortNbr");
       
   580 				DBG_ARGS( aParameter.Value().Ptr() );
       
   581 				iProfiles[iProfiles.Count()-1]->iPort = aParameter.Value().AllocL();
       
   582 				break;
       
   583 
       
   584 			case EWPParameterAddr:
       
   585 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): Addr");
       
   586 				DBG_ARGS( aParameter.Value().Ptr() );
       
   587 				if(aParameter.Value().Length()<=KNSmlMaxURLLength)
       
   588 					{
       
   589 					iProfiles[iProfiles.Count()-1]->iHostAddress = aParameter.Value().AllocL();
       
   590 					}
       
   591 				break;
       
   592 
       
   593 			case EWPParameterAAuthLevel:
       
   594 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): AuthLevel");
       
   595 
       
   596 				if( aParameter.Value().Compare( KNSmlDMProvisioningServerAuth ) == 0 )
       
   597 					{
       
   598 					iAuthLevel = CNSmlDmProvisioningAdapter::EServer;
       
   599 					}
       
   600 				else if( aParameter.Value().Compare( KNSmlDMProvisioningClientAuth ) == 0 )
       
   601 					{
       
   602 					iAuthLevel = CNSmlDmProvisioningAdapter::EClient;
       
   603 					}
       
   604 				break;
       
   605 
       
   606 			case EWPParameterAAuthName:
       
   607 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): AuthName");
       
   608 				
       
   609 				if(aParameter.Value().Length()<=KNSmlDmMaxUsernameLength)
       
   610 				{
       
   611 				if(iAuthLevel == CNSmlDmProvisioningAdapter::EClient)
       
   612 					{
       
   613 					if ( iProfiles[iProfiles.Count()-1]->iServerId == NULL )
       
   614 					    {					    
       
   615 					    iProfiles[iProfiles.Count()-1]->iServerId = ConvertTo8LC( aParameter.Value() ).AllocL();
       
   616 						CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   617 					    }
       
   618                     else if ( ( iProfiles[iProfiles.Count()-1]->iServerId->Compare( KNullDesC8() ) ) == 0 )
       
   619 						{
       
   620 						delete iProfiles[iProfiles.Count()-1]->iServerId;
       
   621 						iProfiles[iProfiles.Count()-1]->iServerId = NULL;
       
   622 						
       
   623 						iProfiles[iProfiles.Count()-1]->iServerId = ConvertTo8LC( aParameter.Value() ).AllocL();
       
   624 						CleanupStack::PopAndDestroy(); // ConvertTo8LC					    
       
   625 						}										
       
   626 					}
       
   627 					
       
   628 				else if( iAuthLevel == CNSmlDmProvisioningAdapter::EServer )
       
   629 					{
       
   630 					iProfiles[iProfiles.Count()-1]->iUsername = ConvertTo8LC( aParameter.Value() ).AllocL();					
       
   631 					CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   632 
       
   633 					if( iProfiles[iProfiles.Count()-1]->iAuthInfo.iAuthPref == ENoAuth )
       
   634 						{
       
   635 						iProfiles[iProfiles.Count()-1]->iAuthInfo.iAuthPref = EBasic;
       
   636 						}
       
   637 					}
       
   638 				else if (iAuthLevel == CNSmlDmProvisioningAdapter::EHttp)
       
   639                     {
       
   640 			        iProfiles[iProfiles.Count()-1]->iHTTPUserName = ConvertTo8LC( aParameter.Value() ).AllocL();
       
   641 			        CleanupStack::PopAndDestroy(); //ConvertTo8LC
       
   642 			        }
       
   643 				}
       
   644 				break;
       
   645 
       
   646 			case EWPParameterAAuthSecret:
       
   647 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): AuthSecret");
       
   648 				if(aParameter.Value().Length()<=KNSmlDmMaxPasswordLength)
       
   649 				{
       
   650 				if( iAuthLevel == CNSmlDmProvisioningAdapter::EClient )
       
   651 					{
       
   652 					iProfiles[iProfiles.Count()-1]->iServerPassword = ConvertTo8LC( aParameter.Value() ).AllocL();					
       
   653 					CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   654 					}
       
   655 				else if ( iAuthLevel == CNSmlDmProvisioningAdapter::EServer )
       
   656 					{
       
   657 					iProfiles[iProfiles.Count()-1]->iPassword = ConvertTo8LC( aParameter.Value() ).AllocL();				
       
   658 					CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   659 					}
       
   660 				else if (iAuthLevel == CNSmlDmProvisioningAdapter::EHttp)
       
   661                     {
       
   662 			        iProfiles[iProfiles.Count()-1]->iHTTPPassword = ConvertTo8LC( aParameter.Value() ).AllocL();
       
   663 			        CleanupStack::PopAndDestroy(); //ConvertTo8LC
       
   664 			        }
       
   665 				}
       
   666 				else
       
   667 				iAuthSecretLimitIndicator = 1;
       
   668 				break;
       
   669 
       
   670 			case EWPParameterAAuthData:
       
   671 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): AuthData");
       
   672 				if( iAuthLevel == CNSmlDmProvisioningAdapter::EServer )
       
   673 					{
       
   674 					iProfiles[iProfiles.Count()-1]->iAuthInfo.iAuthPref = EMD5;
       
   675 					iProfiles[iProfiles.Count()-1]->iServerNonce = aParameter.Value().AllocL();					
       
   676 					}
       
   677 				break;
       
   678 
       
   679 			case EWPParameterAAuthType:
       
   680 				_DBG_FILE("CNSmlDsProvisioningAdapter::VisitL(CWPParameter): AuthType");
       
   681 				DBG_ARGS( aParameter.Value().Ptr() );
       
   682 				if (( aParameter.Value().Compare( KNSmlDMProvisioningHTTPBasic ) == 0 ) ||
       
   683 					( aParameter.Value().Compare( KNSmlDMProvisioningHTTPDigest ) == 0 ))
       
   684 					{
       
   685 					_DBG_FILE("CNSmlDsProvisioningAdapter::VisitL(CWPParameter): HTTP");
       
   686 					iAuthLevel = CNSmlDmProvisioningAdapter::EHttp;
       
   687 					} 
       
   688 				break;
       
   689 				
       
   690 			case EWPParameterProviderID:
       
   691 				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): ProviderId");
       
   692 				if(aParameter.Value().Length()<=KNSmlDmServerIdMaxLength)
       
   693 				{
       
   694 				iProfiles[iProfiles.Count()-1]->iServerId = ConvertTo8LC( aParameter.Value() ).AllocL();				
       
   695 				CleanupStack::PopAndDestroy(); // ConvertTo8LC
       
   696 				}
       
   697 				break;
       
   698 				
       
   699 			case EWPNamedParameter:
       
   700    				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): Named parameter");
       
   701 			    if(aParameter.Name().Compare(KNSmlDMProvisioningINIT)==0)
       
   702 			        {
       
   703     				_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL(CWPParameter): Named parameter (INIT)");
       
   704     				iInitSession= iInitSession|(0x01<<iProfiles.Count());
       
   705 			        }
       
   706 			    break;
       
   707 			default:
       
   708 				break;
       
   709 			}
       
   710 		}
       
   711 	
       
   712 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitL( CWPParameter ): end");
       
   713 	}
       
   714 
       
   715 // -----------------------------------------------------------------------------
       
   716 // CNSmlDmProvisioningAdapter::VisitLinkL
       
   717 // -----------------------------------------------------------------------------
       
   718 void CNSmlDmProvisioningAdapter::VisitLinkL( CWPCharacteristic& aParameter)
       
   719 	{
       
   720 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitLinkL(): begin");
       
   721 	
       
   722 	if( ( ( aParameter.Type()==KWPNapDef ) || ( aParameter.Type()==KWPPxLogical ) ) &&
       
   723 		( iState == CNSmlDmProvisioningAdapter::EStateDmSettings ) )
       
   724 		{
       
   725 		if( iProfiles.Count() > 0 )
       
   726 			{
       
   727 			iProfiles[iProfiles.Count()-1]->iVisitParameter = &aParameter;
       
   728 			}
       
   729 		}
       
   730 	_DBG_FILE("CNSmlDmProvisioningAdapter::VisitLinkL(): end");
       
   731 	}
       
   732 
       
   733 
       
   734 // -----------------------------------------------------------------------------
       
   735 // CNSmlDmProvisioningAdapter::ContextExtension
       
   736 // -----------------------------------------------------------------------------
       
   737 TInt CNSmlDmProvisioningAdapter::ContextExtension( MWPContextExtension*& aContextExtension )
       
   738 	{
       
   739 	_DBG_FILE("CNSmlDmProvisioningAdapter::ContextExtension(): begin");
       
   740 	aContextExtension = this;
       
   741 	_DBG_FILE("CNSmlDmProvisioningAdapter::ContextExtension(): end");
       
   742 	return KErrNone;
       
   743 	}
       
   744 
       
   745 // -----------------------------------------------------------------------------
       
   746 // CNSmlDmProvisioningAdapter::SaveDataL
       
   747 // -----------------------------------------------------------------------------
       
   748 const TDesC8& CNSmlDmProvisioningAdapter::SaveDataL( TInt aIndex ) const
       
   749 	{
       
   750 	return iProfiles[aIndex]->iProfileId;
       
   751 	}
       
   752 
       
   753 
       
   754 // -----------------------------------------------------------------------------
       
   755 // CNSmlDmProvisioningAdapter::DeleteL
       
   756 // -----------------------------------------------------------------------------
       
   757 void CNSmlDmProvisioningAdapter::DeleteL( const TDesC8& aSaveData)
       
   758 	{
       
   759 	_DBG_FILE("CNSmlDmProvisioningAdapter::DeleteL(): begin");
       
   760 	TPckgBuf<TInt> uid;
       
   761 	uid.Copy( aSaveData );
       
   762 	iSession.DeleteProfileL( uid() );
       
   763 	_DBG_FILE("CNSmlDmProvisioningAdapter::DeleteL(): end");
       
   764 	}
       
   765 
       
   766 // -----------------------------------------------------------------------------
       
   767 // CNSmlDmProvisioningAdapter::Uid
       
   768 // -----------------------------------------------------------------------------
       
   769 TUint32 CNSmlDmProvisioningAdapter::Uid() const
       
   770 	{
       
   771 	_DBG_FILE("CNSmlDmProvisioningAdapter::Uid(): begin");
       
   772 	return KNSmlDmProvisioningAdapterImplUid;
       
   773 	}
       
   774 
       
   775 // -----------------------------------------------------------------------------
       
   776 // CNSmlDmProvisioningAdapter::DetailsL( )
       
   777 // -----------------------------------------------------------------------------
       
   778 TInt CNSmlDmProvisioningAdapter::DetailsL( TInt /*aItem*/, MWPPairVisitor& /*aVisitor*/ )
       
   779 	{
       
   780 	return KErrNotSupported;
       
   781 	}
       
   782 
       
   783 // -------------------------------------------------------------------------------------
       
   784 // CNSmlDmProvisioningAdapter::GetDefaultIAPL()
       
   785 // Get the default NAPId 
       
   786 // -------------------------------------------------------------------------------------
       
   787 TInt CNSmlDmProvisioningAdapter::GetDefaultIAPL()
       
   788 	{
       
   789 	TInt iapId = KErrNotFound; 
       
   790 
       
   791 	CCommsDatabase* database = CCommsDatabase::NewL();
       
   792 	CleanupStack::PushL( database );
       
   793 
       
   794 	CCommsDbTableView* tableView = database->OpenTableLC( TPtrC( IAP ) );
       
   795 
       
   796 	TInt errorCode = tableView->GotoFirstRecord();
       
   797 		
       
   798 	if ( errorCode == KErrNone ) 
       
   799 		{
       
   800 		TUint32	value;
       
   801 		tableView->ReadUintL( TPtrC( COMMDB_ID ), value );
       
   802 		iapId = value;
       
   803 		}
       
   804 
       
   805 	CleanupStack::PopAndDestroy( 2 ); // database, tableView
       
   806 
       
   807 	return iapId;
       
   808 	}
       
   809 
       
   810 // ---------------------------------------------------------
       
   811 // CNSmlDmProvisioningAdapter::GetTitleL
       
   812 // ---------------------------------------------------------
       
   813 void CNSmlDmProvisioningAdapter::GetTitleL()
       
   814 	{
       
   815 	if( iTitle == 0 )
       
   816 		{
       
   817 		RFs	fs;
       
   818 		User::LeaveIfError( fs.Connect() );
       
   819 		CleanupClosePushL( fs );
       
   820 
       
   821 		TFileName fileName;
       
   822 		TParse parse;
       
   823 		parse.Set( KNSmlDmPovisioningDirAndResource, &KDC_RESOURCE_FILES_DIR, NULL );
       
   824 		fileName = parse.FullName();
       
   825 
       
   826 		RResourceFile resourceFile; 
       
   827 		BaflUtils::NearestLanguageFile( fs, fileName );
       
   828 		resourceFile.OpenL( fs, fileName );
       
   829 		CleanupClosePushL( resourceFile );
       
   830 
       
   831 		HBufC8* dataBuffer = resourceFile.AllocReadLC( R_SYNCMLDM_PROVISIONING_TITLE );
       
   832 
       
   833 		TResourceReader reader; 
       
   834 		reader.SetBuffer( dataBuffer ); 
       
   835 		iTitle = reader.ReadHBufC16L(); 
       
   836 		CleanupStack::PopAndDestroy( 3 ); //fs, resourcefile, databuffer
       
   837 		}
       
   838 	}
       
   839 
       
   840 // ------------------------------------------------------------------------------------------------
       
   841 // CNSmlDmProvisioningAdapter::CombineURILC()
       
   842 // Combines address and port to URI 
       
   843 // ------------------------------------------------------------------------------------------------
       
   844 TInt CNSmlDmProvisioningAdapter::CombineURILC( const TDesC& aAddr, const TDesC& aPort, HBufC*& aRealURI )
       
   845 	{
       
   846 	TInt offset = 0;
       
   847 	TInt i = 0;
       
   848 	if( aAddr.Find( KNSmlDMProvisioningHTTP ) == 0 )
       
   849 		{
       
   850 		offset = KNSmlDMProvisioningHTTP().Length();
       
   851 		}
       
   852 	else if( aAddr.Find( KNSmlDMProvisioningHTTPS ) == 0 )
       
   853 		{
       
   854 		offset = KNSmlDMProvisioningHTTPS().Length();
       
   855 		}
       
   856 		
       
   857 	// after skipping double slashes seek next single slash
       
   858 	for( i = offset; i < aAddr.Length(); i++ )
       
   859 	    {
       
   860 	    if ( aAddr[i] == KNSmlDMUriSeparator )
       
   861 	        {
       
   862 	        break;
       
   863 	        }
       
   864 	    }
       
   865 	
       
   866 	aRealURI = HBufC::NewLC( aAddr.Length() + aPort.Length() + 1 );
       
   867 	TPtr uriPtr = aRealURI->Des();
       
   868 
       
   869 	uriPtr.Copy( aAddr.Ptr(), i );
       
   870 	uriPtr.Append( KNSmlDMColon );
       
   871 	uriPtr.Append( aPort );
       
   872 	uriPtr.Append( aAddr.Right( aAddr.Length() - i ) );
       
   873 		
       
   874 	return KErrNone;
       
   875 	}
       
   876 // ---------------------------------------------------------
       
   877 //		CNSmlDmProvisioningAdapter::ConvertTo8LC()
       
   878 //		Converts string value to 8-bit
       
   879 // ---------------------------------------------------------
       
   880 TDesC8& CNSmlDmProvisioningAdapter::ConvertTo8LC( const TDesC& aSource )
       
   881 	{
       
   882 	HBufC8* buf = HBufC8::NewLC( aSource.Length()*2 );
       
   883 	TPtr8 bufPtr = buf->Des();
       
   884 	CnvUtfConverter::ConvertFromUnicodeToUtf8( bufPtr, aSource );
       
   885 
       
   886     return *buf;
       
   887 	}
       
   888 	
       
   889 // ---------------------------------------------------------
       
   890 //		CNSmlDmProvisioningAdapter::ConvertTo8L()
       
   891 //		Converts string value to 8-bit
       
   892 // ---------------------------------------------------------
       
   893 TDesC8& CNSmlDmProvisioningAdapter::ConvertTo8L( const TDesC& aSource )
       
   894 	{
       
   895 	HBufC8* buf = HBufC8::NewL( aSource.Length()*2 );
       
   896 	TPtr8 bufPtr = buf->Des();
       
   897 	CnvUtfConverter::ConvertFromUnicodeToUtf8( bufPtr, aSource );
       
   898 
       
   899     return *buf;
       
   900 	}
       
   901 
       
   902 
       
   903 //  End of File