syncmlfw/common/sosserver/src/nsmlsosthread.cpp
changeset 0 b497e44ab2fc
child 24 13d7c31c74e0
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Symbian OS Server source.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32math.h>
       
    19 #include <DevManInternalCRKeys.h>
       
    20 #include <centralrepository.h>
       
    21 #include <nsmldebug.h>
       
    22 
       
    23 #include "nsmlsosthread.h"
       
    24 #include <DataSyncInternalPSKeys.h>
       
    25 
       
    26 #include <PolicyEngineClient.h>
       
    27 #include <centralrepository.h>
       
    28 #include "PMUtilInternalCRKeys.h"
       
    29 #include <featmgr.h>
       
    30 // --------------------------------------------------------------------------
       
    31 // EXPORT_C TInt ThreadFunction( TAny* )
       
    32 // --------------------------------------------------------------------------
       
    33 //
       
    34 TInt ThreadFunction( TAny* aStarted )
       
    35 	{
       
    36 	#ifdef __CLIENT_API_MT_
       
    37 	 __UHEAP_MARK;
       
    38 	#endif
       
    39 	
       
    40 	CNSmlThreadParams* params = (CNSmlThreadParams*) aStarted;
       
    41 	
       
    42 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    43 	_DBG_FILE("CNSmlSOSServer : Session thread started");	
       
    44 	
       
    45 	CActiveScheduler* scheduler = new CActiveScheduler;
       
    46 	__ASSERT_ALWAYS( scheduler, User::Panic( KSOSServerThread, EMainSchedulerError ) );
       
    47     	
       
    48 	CActiveScheduler::Install( scheduler );
       
    49 	
       
    50 	TInt endStatus(KErrNone);
       
    51 	CNSmlThreadEngine * threadEngine = 0;
       
    52 	
       
    53 	params->iCurrentJob.iSilentJob = EFalse;
       
    54 
       
    55 	TRAPD(err,
       
    56 		threadEngine = CNSmlThreadEngine::NewL( *params );
       
    57 	
       
    58 	if ( params->iSyncInit == EServerAlerted && params->iCurrentJob.iPending )
       
    59 		{
       
    60 		if ( threadEngine->SilentModeL( params ))
       
    61 			{
       
    62 			//in silent mode DM client UI is not opened
       
    63 			params->iCurrentJob.iPending = EFalse;
       
    64 		    params->iCurrentJob.iSilentJob = ETrue;
       
    65 			threadEngine->SaveSessionInfoL();
       
    66 			threadEngine->ForcedCertificateCheckL( ETrue );
       
    67 			threadEngine->StartJobSessionL();		
       
    68 			}
       
    69 		else
       
    70 			{
       
    71 			//if not a silent mode behave as in a normal DM session
       
    72 			threadEngine->ForcedCertificateCheckL( EFalse );
       
    73 			threadEngine->VerifyJobFromNotifierL();		
       
    74 			}
       
    75 		}
       
    76 	else
       
    77 		{
       
    78 		//when sync session is not alerted behave as in a normal DM session
       
    79 		threadEngine->ForcedCertificateCheckL( EFalse );
       
    80 		threadEngine->StartJobSessionL();		
       
    81 		}
       
    82 	);
       
    83 	
       
    84 	endStatus = err;
       
    85     
       
    86     if ( err != KErrNone )    
       
    87  		{
       
    88 		if ( params->iCurrentJob.TransportId() != KUidNSmlMediumTypeInternet.iUid )
       
    89 			{
       
    90 			// Disconnect if local connection
       
    91 			TRAP_IGNORE( params->iSOSHandler->DoDisconnectL() );		
       
    92 			}		
       
    93 		}	
       
    94 	
       
    95 	// start scheduler after succesfull initialization 
       
    96 	if ( threadEngine )
       
    97 	    {
       
    98 	    if ( threadEngine->IsActive() )
       
    99 	        {
       
   100 	        CActiveScheduler::Start();                
       
   101 	        }	    
       
   102 	    }	
       
   103 	
       
   104 	delete scheduler;
       
   105 	
       
   106 	if ( endStatus == KErrNone )
       
   107 		{
       
   108 		endStatus = threadEngine->FinishedStatus();	
       
   109 		}
       
   110 	
       
   111 	if ( threadEngine )	
       
   112 	    {
       
   113 	    delete threadEngine;        
       
   114 	    }
       
   115 	
       
   116 	_DBG_FILE("SosServer Job Thread ended");
       
   117 	delete cleanup;
       
   118 	
       
   119 	#ifdef __CLIENT_API_MT_
       
   120 	__UHEAP_MARKEND;
       
   121 	#endif
       
   122 	
       
   123     return endStatus;
       
   124 	}
       
   125 
       
   126 // --------------------------------------------------------------------------
       
   127 // CNSmlThreadEngine::NewL( CNSmlThreadParams& aParams )
       
   128 // Two phase Constructor
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 CNSmlThreadEngine* CNSmlThreadEngine::NewL( CNSmlThreadParams& aParams )
       
   132 	{
       
   133 	CNSmlThreadEngine* self = new (ELeave) CNSmlThreadEngine( aParams );
       
   134 	CleanupStack::PushL( self );
       
   135 	self->ConstructL();
       
   136 	CleanupStack::Pop();
       
   137 	return self;
       
   138 	}
       
   139 
       
   140 // --------------------------------------------------------------------------
       
   141 // CNSmlThreadEngine::ConstructL()
       
   142 // 2nd Constructor
       
   143 // --------------------------------------------------------------------------
       
   144 //
       
   145 void CNSmlThreadEngine::ConstructL()
       
   146 	{
       
   147 		FeatureManager::InitializeLibL();
       
   148 	iThreadParams.iThreadEngine = this;
       
   149 	iContentArray = new(ELeave) CArrayFixFlat<TNSmlContentSpecificSyncType>(1);
       
   150 	
       
   151 	if ( iThreadParams.iCSArray )
       
   152 		{
       
   153 		for (TInt i = 0; i < iThreadParams.iCSArray->Count(); i++)
       
   154 			{
       
   155 			TNSmlContentSpecificSyncType type;
       
   156 			type.iTaskId = iThreadParams.iCSArray->At( i ).iTaskId;
       
   157 			type.iType = iThreadParams.iCSArray->At( i ).iType;
       
   158 			iContentArray->AppendL( type );
       
   159 			}
       
   160 		}		
       
   161 	}
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CNSmlThreadEngine( TNSmlThreadParams& aParams ) 
       
   165 // Constructor
       
   166 // --------------------------------------------------------------------------
       
   167 //
       
   168 CNSmlThreadEngine::CNSmlThreadEngine( CNSmlThreadParams& aParams ) 
       
   169 : CActive(EPriorityLow), iThreadParams(aParams)
       
   170 	{
       
   171 	CActiveScheduler::Add(this);
       
   172 	}
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CNSmlThreadEngine::~CNSmlThreadEngine()
       
   176 // Destructor
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 CNSmlThreadEngine::~CNSmlThreadEngine()
       
   180 	{
       
   181 	delete iContentArray;
       
   182 	delete iNotifierObserver;
       
   183 	Cancel();
       
   184 	RThread().Close();
       
   185 	delete iDSAgent;
       
   186 	delete iDMAgent;
       
   187 	delete iCancelTimeout;
       
   188 	FeatureManager::UnInitializeLib();
       
   189 	}
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CNSmlThreadEngine::SilentModeL()
       
   193 // Check is current serverid mentioned to work in silent mode (TARM feature)
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 TBool CNSmlThreadEngine::SilentModeL( const CNSmlThreadParams* aParams )
       
   197     {   
       
   198     _DBG_FILE("CNSmlThreadEngine::SilentModeL() : start");	
       
   199     TInt retVal = ETrue;
       
   200     
       
   201     if ( aParams->iCurrentJob.UsageType() == ESmlDataSync )
       
   202         {
       
   203         return EFalse;               
       
   204         }
       
   205  if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   206  {
       
   207 	//get DM profile -> get profile serverid
       
   208 	CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   209     CNSmlDMProfile* prof = settings->ProfileL( 
       
   210             aParams->iCurrentJob.ProfileId() );
       
   211     CleanupStack::PushL( prof );
       
   212 
       
   213 	//connect to policy engine
       
   214 	RPolicyEngine policyEngine;
       
   215 	User::LeaveIfError( policyEngine.Connect() );
       
   216 	CleanupClosePushL( policyEngine );
       
   217 		
       
   218 	RPolicyManagement policyManagement;
       
   219 	User::LeaveIfError( policyManagement.Open( policyEngine ) );
       
   220 
       
   221 	//check is the specific server id allowed to work in silent mode
       
   222 	TInt err = policyManagement.IsAllowedServerId( 
       
   223 	        prof->StrValue( EDMProfileServerId ) );
       
   224 			
       
   225 	policyManagement.Close();
       
   226 
       
   227 	CleanupStack::PopAndDestroy( 3, settings );
       
   228 	
       
   229 	if ( err == KErrNone )
       
   230 	    {
       
   231 	    retVal = ETrue;    
       
   232 	    }
       
   233 	else
       
   234 	    {
       
   235 	    retVal = EFalse;    
       
   236 	    }	
       
   237 }
       
   238 else
       
   239 {
       
   240 	//if policy management is not installed....
       
   241 	retVal = EFalse;
       
   242 }
       
   243 
       
   244     if( retVal )
       
   245         {
       
   246         _DBG_FILE("CNSmlThreadEngine::SilentModeL() : end, return ETrue");
       
   247         }
       
   248     else
       
   249         {
       
   250         _DBG_FILE("CNSmlThreadEngine::SilentModeL() : end, return EFalse");
       
   251         }
       
   252 
       
   253     return retVal;
       
   254     }
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CNSmlThreadEngine::SaveSessionInfo()
       
   258 // Save session info which is needed later in DM agent to launch notifier
       
   259 // --------------------------------------------------------------------------
       
   260 //
       
   261 void CNSmlThreadEngine::SaveSessionInfoL()
       
   262     {    
       
   263  if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   264  {
       
   265 	CRepository * rep = CRepository::NewLC( 
       
   266 	                    KCRUidPolicyManagementUtilInternalKeys );
       
   267 
       
   268 	TSyncMLAppLaunchNotifParams params;	
       
   269 	
       
   270 	if ( iThreadParams.iCurrentJob.UsageType() == ESmlDataSync )	
       
   271 	    {
       
   272 	    params.iSessionType = ESyncMLSyncSession;    
       
   273 	    }
       
   274 	else
       
   275 	    {
       
   276 	    params.iSessionType = ESyncMLMgmtSession;    
       
   277 	    }
       
   278    
       
   279 	params.iJobId = iThreadParams.iCurrentJob.JobId();
       
   280 	params.iProfileId = iThreadParams.iCurrentJob.ProfileId(); 
       
   281 
       
   282     TPckg<TSyncMLAppLaunchNotifParams> data( params );
       
   283    
       
   284 	TInt err = rep->Set( KSyncMLSessionParamsKey, data );
       
   285 	
       
   286 	if ( err == KErrNotFound )
       
   287 	    {
       
   288 		User::LeaveIfError( rep->Create( KSyncMLSessionParamsKey, data ) );
       
   289     	}
       
   290 	else
       
   291 	    {
       
   292         _DBG_FILE("CNSmlThreadEngine::SaveSessionInfoL TInt err = \
       
   293                     rep->Set( KParamsKey, data );");
       
   294 	    User::LeaveIfError( err );
       
   295 	    }
       
   296 
       
   297 	CleanupStack::PopAndDestroy( rep );
       
   298 }
       
   299     }
       
   300 
       
   301 // --------------------------------------------------------------------------
       
   302 // CNSmlThreadEngine::ForcedCertificateCheckL( TBool aForcedState )
       
   303 // Launch job session in thread
       
   304 // --------------------------------------------------------------------------
       
   305 //
       
   306 void CNSmlThreadEngine::ForcedCertificateCheckL( TBool aForcedState )
       
   307     {
       
   308     if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   309     {	
       
   310 	CRepository * rep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys );
       
   311 
       
   312 	TInt err = rep->Set( KSyncMLForcedCertificateCheckKey, aForcedState );
       
   313 	
       
   314 	if ( err == KErrNotFound )
       
   315 	    {
       
   316 		User::LeaveIfError( rep->Create( KSyncMLForcedCertificateCheckKey, aForcedState ));
       
   317     	}
       
   318 
       
   319 	CleanupStack::PopAndDestroy( rep );
       
   320 		}
       
   321     }
       
   322 
       
   323 // --------------------------------------------------------------------------
       
   324 // CNSmlThreadEngine::StartJobSessionL()
       
   325 // Launch job session in thread
       
   326 // --------------------------------------------------------------------------
       
   327 //
       
   328 void CNSmlThreadEngine::StartJobSessionL()
       
   329 	{
       
   330 	_DBG_FILE("CNSmlThreadStart::StartJobSession() : Begin");	
       
   331 		
       
   332 	if ( !IsActive() )
       
   333 		{
       
   334 		SetActive();
       
   335 		}
       
   336 		
       
   337 	TInt status( KErrNone );			
       
   338 	if ( iThreadParams.iCurrentJob.UsageType() == ESmlDevMan )
       
   339 		{
       
   340 		TRAP( status, StartDMSessionL() );
       
   341 		}
       
   342 	else
       
   343 		{
       
   344 		TRAP( status, StartDSSessionL() );	
       
   345 		}
       
   346 	
       
   347 	if ( status != KErrNone )
       
   348 	    {
       
   349 	   	TRequestStatus* sStatus = &iStatus;
       
   350 		User::RequestComplete( sStatus, status );    
       
   351 	    }
       
   352 	_DBG_FILE("CNSmlThreadStart::StartJobSession() : End");	
       
   353 	}
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // CNSmlThreadEngine::CancelJob()
       
   357 // Cancel current running job
       
   358 // --------------------------------------------------------------------------
       
   359 //
       
   360 void CNSmlThreadEngine::CancelJob()
       
   361 	{
       
   362 	if ( iCancelTimeout )
       
   363 	    {
       
   364 		iCancelTimeout->SetJobCancelled( iThreadParams.iCurrentJob.UsageType() );
       
   365 	    }	
       
   366 	}
       
   367 
       
   368 // --------------------------------------------------------------------------
       
   369 // CNSmlThreadEngine::StartDMSessionL()
       
   370 // Start DM job
       
   371 // --------------------------------------------------------------------------
       
   372 //	
       
   373 void CNSmlThreadEngine::StartDMSessionL()
       
   374 	{
       
   375 	TInt result = iSessionLib.Load( KSmlDMAgentLoaderLibName );
       
   376     if ( result != KErrNone )
       
   377     	{
       
   378 	    _DBG_FILE("CNSmlSOSHandler::StartDMSyncL(): loading agent failed !");
       
   379     	User::Leave( result );
       
   380     	}
       
   381     TNSmlCreateDMAgentFunc createDMAgentL = (TNSmlCreateDMAgentFunc)iSessionLib.Lookup(1);
       
   382     iDMAgent =  createDMAgentL(iThreadParams.iSOSHandler);    
       
   383     
       
   384     iCancelTimeout = new (ELeave) CNSmlCancelTimeOut( *iDMAgent, *iDSAgent );
       
   385     
       
   386 	iDMAgent->Configure(iStatus, iThreadParams.iCurrentJob.ProfileId(), iThreadParams.iCurrentJob.TransportId(), (TNSmlSyncInitiation)iThreadParams.iSyncInit,
       
   387 			iThreadParams.iCurrentJob.GetJobIapId());
       
   388 	}
       
   389 
       
   390 // --------------------------------------------------------------------------
       
   391 // CNSmlThreadEngine::StartDSSessionL()
       
   392 // Start DS job
       
   393 // --------------------------------------------------------------------------
       
   394 //		
       
   395 void CNSmlThreadEngine::StartDSSessionL()
       
   396 	{
       
   397 	
       
   398 	TInt result = iSessionLib.Load( KSmlDSAgentLoaderLibName );
       
   399 	if ( result != KErrNone )
       
   400 		{
       
   401 	    _DBG_FILE("CNSmlSOSHandler::StartDSSyncL(): loading agent failed!");
       
   402 		User::Leave( result );
       
   403 		}
       
   404 	
       
   405 	TNSmlCreateDSAgentFunc createDSAgentL = (TNSmlCreateDSAgentFunc)iSessionLib.Lookup(1);
       
   406 	iDSAgent = createDSAgentL( iThreadParams.iSOSHandler );
       
   407 	
       
   408 	iCancelTimeout = new (ELeave) CNSmlCancelTimeOut( *iDMAgent, *iDSAgent );
       
   409 	
       
   410 	// Select correct sync method and start sync
       
   411 	TNSmlSyncInitiation syncInit = (TNSmlSyncInitiation)iThreadParams.iSyncInit;
       
   412 	
       
   413     switch ( iThreadParams.iCurrentJob.JobType() )
       
   414         {
       
   415         case EDSJobProfile:
       
   416             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), KNSmlNullId, NULL, syncInit );
       
   417             break;           
       
   418         case EDSJobProfileST:
       
   419             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), KNSmlNullId, iContentArray, syncInit ); // + syncType
       
   420             break;            
       
   421         case EDSJobTasks:
       
   422             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), KNSmlNullId, iContentArray, syncInit ); // + tasks
       
   423             break;
       
   424         case EDSJobTasksST:
       
   425             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), KNSmlNullId, iContentArray, syncInit ); // + tasks & syncType
       
   426             break;
       
   427         case EDSJobTransport:
       
   428             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), iThreadParams.iCurrentJob.TransportId(), NULL, syncInit ); // + transport
       
   429             break;
       
   430         case EDSJobTransportST:
       
   431             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), iThreadParams.iCurrentJob.TransportId(), iContentArray, syncInit ); // + transport & syncType
       
   432             break;
       
   433         case EDSJobTaskTrans:
       
   434             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), iThreadParams.iCurrentJob.TransportId(), iContentArray, syncInit ); // + tasks & transport
       
   435             break;
       
   436         case EDSJobTaskTransST:
       
   437             iDSAgent->Synchronise( iStatus, iThreadParams.iCurrentJob.ProfileId(), iThreadParams.iCurrentJob.TransportId(), iContentArray, syncInit, iThreadParams.iCurrentJob.iSessionId, iThreadParams.iCurrentJob.iPackage ); // + tasks & transport & syncType & server alert
       
   438             break;
       
   439         default:
       
   440             break;
       
   441         }
       
   442 	}
       
   443 
       
   444 // --------------------------------------------------------------------------
       
   445 // CNSmlThreadEngine::VerifyJobFromNotifierL()
       
   446 // Launches notifier and waits for 
       
   447 // --------------------------------------------------------------------------
       
   448 //
       
   449 TInt CNSmlThreadEngine::VerifyJobFromNotifierL()
       
   450 	{
       
   451 	_DBG_FILE("CNSmlThreadEngine::VerifyJobFromNotifierL() : Begin");
       
   452 	iNotifierObserver = new (ELeave) CNSmlNotifierObserver(iStatus, iThreadParams);
       
   453 	SetActive();
       
   454 	
       
   455 	TSyncMLAppLaunchNotifParams params;
       
   456 	params.iSessionType = ( iThreadParams.iCurrentJob.UsageType() == ESmlDataSync ) ? ESyncMLSyncSession : ESyncMLMgmtSession;
       
   457 	params.iJobId = iThreadParams.iCurrentJob.JobId();
       
   458 	params.iProfileId = iThreadParams.iCurrentJob.ProfileId();
       
   459     params.iUimode = iThreadParams.iCurrentJob.iUimode;	
       
   460 	iNotifierObserver->ConnectToNotifierL( params );
       
   461 	_DBG_FILE("CNSmlThreadEngine::VerifyJobFromNotifierL() : End");
       
   462     return KErrNone;
       
   463 	}
       
   464 
       
   465 // --------------------------------------------------------------------------
       
   466 // CNSmlThreadEngine::DMAgentL()
       
   467 // Returns dm agent if it is created, otherwise leaves with KErrNotFound.
       
   468 // --------------------------------------------------------------------------
       
   469 //
       
   470 CNSmlDMAgent* CNSmlThreadEngine::DMAgentL() const
       
   471 	{
       
   472 	if ( !iDMAgent )
       
   473 		{
       
   474 		User::Leave( KErrNotFound );
       
   475 		}
       
   476 	
       
   477 	return iDMAgent;
       
   478 	}
       
   479 
       
   480 // --------------------------------------------------------------------------
       
   481 // CNSmlThreadEngine::DSAgentL()
       
   482 // Returns ds agent if it is created, otherwise leaves with KErrNotFound.
       
   483 // --------------------------------------------------------------------------
       
   484 //
       
   485 CNSmlDSAgent* CNSmlThreadEngine::DSAgentL() const
       
   486 	{
       
   487 	if ( !iDSAgent )
       
   488 		{
       
   489 		User::Leave( KErrNotFound );
       
   490 		}
       
   491 	
       
   492 	return iDSAgent;
       
   493 	}
       
   494 
       
   495 // --------------------------------------------------------------------------
       
   496 // CNSmlThreadEngine::DoCancel()
       
   497 // From base class
       
   498 // --------------------------------------------------------------------------
       
   499 //
       
   500 void CNSmlThreadEngine::DoCancel()
       
   501 	{
       
   502 	_DBG_FILE("CNSmlThreadStart::DoCancel() : Begin");	
       
   503 	_DBG_FILE("CNSmlThreadStart::DoCancel() : End");	
       
   504 	}
       
   505 
       
   506 // --------------------------------------------------------------------------
       
   507 // CNSmlThreadEngine::RunL()
       
   508 // From base class : 
       
   509 // --------------------------------------------------------------------------
       
   510 //
       
   511 void CNSmlThreadEngine::RunL()
       
   512 	{
       
   513 	_DBG_FILE("CNSmlThreadEngine::RunL() : Begin");
       
   514 	TBool resume( EFalse );
       
   515 	TRAP_IGNORE( resume = DoRunL() );
       
   516 	
       
   517 	if ( resume )
       
   518 	    {
       
   519 	    return;
       
   520 	    }
       
   521 	_DBG_FILE("CNSmlThreadEngine::RunL() : End");	
       
   522 	CActiveScheduler::Stop();	
       
   523 	}
       
   524 
       
   525 // --------------------------------------------------------------------------
       
   526 // CNSmlThreadEngine::DoRunL()
       
   527 // Method to perform RunL, that can be trapped.
       
   528 // --------------------------------------------------------------------------
       
   529 //
       
   530 TBool CNSmlThreadEngine::DoRunL()
       
   531     {
       
   532     _DBG_FILE("CNSmlThreadEngine::DoRunL() : Begin");
       
   533     
       
   534     if ( iThreadParams.iCurrentJob.iPending != EFalse )	
       
   535 		{
       
   536 		iThreadParams.iCurrentJob.iPending = EFalse;
       
   537 		if ( iStatus.Int() == KErrNone )
       
   538 			{
       
   539 			StartJobSessionL();
       
   540 			return ETrue;
       
   541 			}
       
   542 		else
       
   543 			{
       
   544 			if ( iThreadParams.iCurrentJob.TransportId() != KUidNSmlMediumTypeInternet.iUid )
       
   545 				{
       
   546 				// Disconnect local connection.
       
   547 				TRAP_IGNORE( iThreadParams.iSOSHandler->DoDisconnectL() );				
       
   548 				}
       
   549 			
       
   550 			WriteHistoryLogL( iStatus.Int() );
       
   551 			}
       
   552 		}
       
   553 	_DBG_FILE("CNSmlThreadEngine::DoRunL() : Begin");    
       
   554 	return EFalse;	
       
   555     }
       
   556 
       
   557 // --------------------------------------------------------------------------
       
   558 // CNSmlThreadEngine::WriteHistoryLogL( TInt aErrorCode )
       
   559 // Writes history log when notifier start 
       
   560 // --------------------------------------------------------------------------
       
   561 //
       
   562 void CNSmlThreadEngine::WriteHistoryLogL( TInt aErrorCode )
       
   563 	{
       
   564     RArray<TInt> tasks;
       
   565    	       	
       
   566     if ( iThreadParams.iCurrentJob.UsageType() == ESmlDataSync )
       
   567     	{
       
   568     	iThreadParams.iCurrentJob.TaskIds( tasks );	
       
   569     	}       	
       
   570    	CleanupClosePushL(tasks);
       
   571    	
       
   572     CSyncMLHistoryJob* historyLog = CSyncMLHistoryJob::NewL( iThreadParams.iCurrentJob.ProfileId(), tasks );
       
   573     CleanupStack::PushL( historyLog );             
       
   574     
       
   575 	TTime time;
       
   576 	time.UniversalTime();
       
   577 	
       
   578 	historyLog->SetResult( time, aErrorCode );
       
   579 
       
   580 	if ( iThreadParams.iCurrentJob.UsageType() == ESmlDataSync )
       
   581 		{
       
   582 		CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   583 		CNSmlDSProfile* prof = settings->ProfileL( iThreadParams.iCurrentJob.ProfileId() );
       
   584 		CleanupStack::PushL( prof );
       
   585 		CNSmlHistoryArray* array = CNSmlHistoryArray::NewL();
       
   586 		CleanupStack::PushL( array );
       
   587 		array->SetOwnerShip( ETrue );
       
   588 	
       
   589 		RReadStream& readStream = prof->LogReadStreamL();
       
   590 		CleanupClosePushL( readStream );
       
   591 		
       
   592 		if ( readStream.Source()->SizeL() > 0 )
       
   593 			{
       
   594 			array->InternalizeL( readStream );
       
   595 			}
       
   596 			
       
   597 		CleanupStack::PopAndDestroy(); // readStream
       
   598 	
       
   599 		array->AppendEntryL( historyLog );
       
   600 		RWriteStream& writeStream = prof->LogWriteStreamL();
       
   601 		CleanupClosePushL( writeStream );
       
   602 		array->ExternalizeL( writeStream );
       
   603 		CleanupStack::PopAndDestroy(); // writeStream
       
   604 		prof->WriteStreamCommitL();
       
   605 	
       
   606 		CleanupStack::PopAndDestroy(array);
       
   607 		CleanupStack::PopAndDestroy(prof);
       
   608 		CleanupStack::PopAndDestroy(settings);
       
   609 		}
       
   610 	else	// DevMan
       
   611 		{
       
   612 		CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   613 		CNSmlDMProfile* prof = settings->ProfileL( iThreadParams.iCurrentJob.ProfileId() );
       
   614 		CleanupStack::PushL( prof );
       
   615 		CNSmlHistoryArray* array = CNSmlHistoryArray::NewL();
       
   616 		CleanupStack::PushL( array );
       
   617 		array->SetOwnerShip( ETrue );
       
   618 	
       
   619 		RReadStream& readStream = prof->LogReadStreamL();
       
   620 		CleanupClosePushL( readStream );
       
   621 		
       
   622 		if ( readStream.Source()->SizeL() > 0 )
       
   623 			{
       
   624 			array->InternalizeL( readStream );
       
   625 			}
       
   626 			
       
   627 		CleanupStack::PopAndDestroy(); // readStream
       
   628 	
       
   629 		array->AppendEntryL( historyLog );
       
   630 		RWriteStream& writeStream = prof->LogWriteStreamL();
       
   631 		CleanupClosePushL( writeStream );
       
   632 		array->ExternalizeL( writeStream );
       
   633 		CleanupStack::PopAndDestroy(); // writeStream
       
   634 		prof->WriteStreamCommitL();
       
   635 	
       
   636 		CleanupStack::PopAndDestroy(array);
       
   637 		CleanupStack::PopAndDestroy(prof);
       
   638 		CleanupStack::PopAndDestroy(settings); 				
       
   639 		}
       
   640 		
       
   641 	CleanupStack::Pop(historyLog);
       
   642 	CleanupStack::PopAndDestroy(&tasks);					
       
   643 	}
       
   644 // --------------------------------------------------------------------------
       
   645 // TNSmlThreadLauncher::RunJobSession( CNSmlThreadParams& aParams, CNSmlThreadObserver& aObserver )
       
   646 // Launches a new thread for job session
       
   647 // --------------------------------------------------------------------------
       
   648 //
       
   649 TInt TNSmlThreadLauncher::RunJobSession( CNSmlThreadParams& aParams, CNSmlThreadObserver& aObserver )
       
   650 	{
       
   651 	_DBG_FILE("TNSmlThreadLauncher::RunJobSession() : Begin");	
       
   652 	RThread thread;
       
   653 	
       
   654 	TName threadName ( KNSmlJobThread );
       
   655     threadName.Append ( _L("_") );
       
   656     threadName.AppendNum ( Math::Random(), EHex );
       
   657 	
       
   658 	TInt status = thread.Create(threadName, ThreadFunction, KNSmlSOSServerStackSize, NULL, &aParams, EOwnerProcess);
       
   659 	
       
   660 	if (status != KErrNone)
       
   661 		{
       
   662 		thread.Close();
       
   663 		return status;
       
   664 		}
       
   665 		
       
   666 	thread.SetPriority( EPriorityNormal ); // set priority
       
   667 	thread.Resume();
       
   668 	
       
   669 	//Request notification when DM session thread ends.
       
   670 	aObserver.LogonToThread( thread );
       
   671 
       
   672 	thread.Close(); 
       
   673 	_DBG_FILE("TNSmlThreadLauncher::RunJobSession() : End");	
       
   674 	return status;
       
   675 	}
       
   676 
       
   677 
       
   678 // --------------------------------------------------------------------------
       
   679 // CNSmlThreadObserver::CNSmlThreadObserver()
       
   680 // Constructor
       
   681 // --------------------------------------------------------------------------
       
   682 //
       
   683 CNSmlThreadObserver::CNSmlThreadObserver(TRequestStatus& aStatus)
       
   684 :CActive(0), iSosStatus(aStatus)
       
   685 	{
       
   686 	CActiveScheduler::Add(this);
       
   687 	}
       
   688 
       
   689 // --------------------------------------------------------------------------
       
   690 // CNSmlThreadObserver::~CNSmlThreadObserver()
       
   691 // Constructor
       
   692 // --------------------------------------------------------------------------
       
   693 //
       
   694 CNSmlThreadObserver::~CNSmlThreadObserver()
       
   695 	{
       
   696 	Cancel();
       
   697 	}
       
   698 
       
   699 // --------------------------------------------------------------------------
       
   700 // CNSmlThreadObserver::LogonToThread( RThread& aThread )
       
   701 // Logs on to given thread. Wakes the server scheduler
       
   702 // --------------------------------------------------------------------------
       
   703 //	
       
   704 void CNSmlThreadObserver::LogonToThread( RThread& aThread )
       
   705 	{
       
   706 	_DBG_FILE("CNSmlThreadObserver::LogonToThread() : Begin");	
       
   707 	if ( !IsActive() )
       
   708 		{
       
   709 		SetActive();
       
   710 		}
       
   711 		
       
   712 	aThread.Logon( iStatus );
       
   713 	_DBG_FILE("CNSmlThreadObserver::LogonToThread() : End");	
       
   714 	}
       
   715 
       
   716 // --------------------------------------------------------------------------
       
   717 // CNSmlThreadObserver::DoCancel()
       
   718 // From base class
       
   719 // --------------------------------------------------------------------------
       
   720 //
       
   721 void CNSmlThreadObserver::DoCancel()
       
   722 	{
       
   723 	
       
   724 	}
       
   725 
       
   726 // --------------------------------------------------------------------------
       
   727 // CNSmlThreadObserver::RunL()
       
   728 // Logs on to given thread. Called when thread finishes.
       
   729 // --------------------------------------------------------------------------
       
   730 //
       
   731 void CNSmlThreadObserver::RunL()
       
   732 	{
       
   733 	DBG_FILE_CODE(iStatus.Int(), _S8("CNSmlThreadObserver::RunL() : Begin"));	
       
   734 	
       
   735 	TInt err;
       
   736 	TInt dsStatus = KErrNotFound;
       
   737 	
       
   738 	err = RProperty::Set( KPSUidDataSynchronizationInternalKeys, KDataSyncStatus, dsStatus );
       
   739 	
       
   740 	if(err == KErrNone) 
       
   741 	{
       
   742 		if(dsStatus != EDataSyncNotRunning )	
       
   743 		{
       
   744 			RProperty::Set( KPSUidDataSynchronizationInternalKeys, KDataSyncStatus, EDataSyncNotRunning );	
       
   745 		}
       
   746 	
       
   747 	}
       
   748 	//Notify thread completion status code to caller.
       
   749 	TRequestStatus* sStatus = &iSosStatus;
       
   750 	User::RequestComplete( sStatus, iStatus.Int() );
       
   751 	
       
   752 	_DBG_FILE("CNSmlThreadObserver::LogonToThread() : End");	
       
   753 	}
       
   754 
       
   755 // --------------------------------------------------------------------------
       
   756 // CNSmlNotifierObserver::CNSmlNotifierObserver(TRequestStatus& aStatus, CNSmlThreadEngine* aEngine, CNSmlThreadParams& aParams)
       
   757 // Constructor
       
   758 // --------------------------------------------------------------------------
       
   759 //	
       
   760 CNSmlNotifierObserver::CNSmlNotifierObserver(TRequestStatus& aStatus, CNSmlThreadParams& aParams)
       
   761 : CActive(0), iCallerStatus(aStatus), iThreadParams(aParams)
       
   762 	{
       
   763 	CActiveScheduler::Add(this);
       
   764 	}
       
   765 
       
   766 // --------------------------------------------------------------------------
       
   767 // CNSmlNotifierObserver::~CNSmlNotifierObserver()
       
   768 // Destructor
       
   769 // --------------------------------------------------------------------------
       
   770 //	
       
   771 CNSmlNotifierObserver::~CNSmlNotifierObserver()
       
   772 	{
       
   773 	
       
   774 	// StartNotifier called to avoid Notifier server panic, if 
       
   775 	// notifier does not exist anymore.
       
   776 	TBuf8<1> dummy;	
       
   777 	iNotifier.StartNotifier(KNullUid, dummy, dummy); // KNullUid should do also..
       
   778 	
       
   779 	iNotifier.CancelNotifier( KUidNotifier );
       
   780 	iNotifier.Close();
       
   781 	Cancel();
       
   782 	}
       
   783 
       
   784 // --------------------------------------------------------------------------
       
   785 // CNSmlNotifierObserver::ConnectToNotifierL( const TSyncMLAppLaunchNotifParams& aParam )
       
   786 // Starts connection with notifier
       
   787 // --------------------------------------------------------------------------
       
   788 //		
       
   789 void CNSmlNotifierObserver::ConnectToNotifierL( const TSyncMLAppLaunchNotifParams& aParam )
       
   790 	{
       
   791 	_DBG_FILE( "CNSmlNotifierObserver::ConnectToNotifierL:Begin" );
       
   792 	iTimeOut = EFalse;
       
   793 	
       
   794 	if ( !IsActive() )
       
   795 		{
       
   796 		SetActive();
       
   797 		}
       
   798 		
       
   799 	   
       
   800     TSyncMLAppLaunchNotifParamsPckg data( aParam );
       
   801     
       
   802     if ( !KNSmlWaitNotifierForEver )
       
   803         {
       
   804         iNotifierTimeOut.LaunchNotifierTimer( this );
       
   805         }
       
   806         
       
   807     TInt err = iNotifier.Connect();
       
   808     if ( err == KErrNone )
       
   809         {
       
   810         iNotifier.StartNotifierAndGetResponse( iStatus, KUidNotifier, data, iResBuf );                
       
   811         }
       
   812     else
       
   813         {
       
   814         // Stop job. Error connecting to notifier.
       
   815         TRequestStatus* sStatus = &iStatus;
       
   816 		User::RequestComplete( sStatus, err );            
       
   817         }
       
   818      _DBG_FILE("CNSmlNotifierObserver::ConnectToNotifierL:End");   
       
   819 	}
       
   820 
       
   821 // --------------------------------------------------------------------------
       
   822 // CNSmlNotifierObserver::NotifierTimeOut()
       
   823 // Starts notifier time out timer
       
   824 // --------------------------------------------------------------------------
       
   825 //
       
   826 void CNSmlNotifierObserver::NotifierTimeOut()
       
   827 	{
       
   828 	iTimeOut = ETrue;
       
   829 	
       
   830 	// StartNotifier called to avoid Notifier server panic, if 
       
   831 	// notifier does not exist anymore.
       
   832 	TBuf8<1> dummy;
       
   833 	iNotifier.StartNotifier(KNullUid, dummy, dummy); // KNullUid should do also..
       
   834 	iNotifier.CancelNotifier( KUidNotifier );
       
   835 	}
       
   836 
       
   837 // --------------------------------------------------------------------------
       
   838 // CNSmlNotifierObserver::DoCancel()
       
   839 // From base class
       
   840 // --------------------------------------------------------------------------
       
   841 //	
       
   842 void CNSmlNotifierObserver::DoCancel()
       
   843 	{
       
   844 	}
       
   845 
       
   846 // --------------------------------------------------------------------------
       
   847 // CNSmlNotifierObserver::RunL()
       
   848 // From base class
       
   849 // --------------------------------------------------------------------------
       
   850 //	
       
   851 void CNSmlNotifierObserver::RunL()
       
   852 	{
       
   853 	    
       
   854     TInt ret = iStatus.Int();
       
   855     if ( ret == KErrCancel && iTimeOut )
       
   856     	{
       
   857     	
       
   858     	}
       
   859     else
       
   860     if ( ret == KErrNone )
       
   861     	{	   
       
   862         
       
   863 	  	TInt sid = iResBuf().iSecureId.iUid; // read secure id from notifier.
       
   864 	   	
       
   865 	    // Check the response and error code. If there is a fail, dump the job.        
       
   866 	    // Also compare sid to creator id saved for current job to secure that listener owns the job.
       
   867 	    if ( ret != KErrNone || sid != iThreadParams.iCurrentJob.CreatorId() )
       
   868 	        {
       
   869 	        if ( ret == KErrNone && sid != iThreadParams.iCurrentJob.CreatorId() )
       
   870 	        	{
       
   871 #ifndef __WINS__	        	
       
   872 	        	ret = KErrPermissionDenied;
       
   873 #endif	        	
       
   874 	        	}
       
   875 	        }
       
   876 	    	
       
   877     	}
       
   878     
       
   879 	TRequestStatus* sStatus = &iCallerStatus;
       
   880 	User::RequestComplete( sStatus, ret );
       
   881 	}
       
   882 
       
   883 // --------------------------------------------------------------------------
       
   884 // CNSmlNotifierTimeOut::~CNSmlNotifierTimeOut()
       
   885 // Constructor
       
   886 // --------------------------------------------------------------------------
       
   887 //
       
   888 CNSmlNotifierTimeOut::CNSmlNotifierTimeOut()
       
   889 :CActive(0)
       
   890 	{
       
   891 	CActiveScheduler::Add(this);
       
   892 	}
       
   893 
       
   894 // --------------------------------------------------------------------------
       
   895 // CNSmlNotifierTimeOut::~CNSmlNotifierTimeOut()
       
   896 // Destructor
       
   897 // --------------------------------------------------------------------------
       
   898 //
       
   899 CNSmlNotifierTimeOut::~CNSmlNotifierTimeOut()
       
   900 	{
       
   901 	Cancel();
       
   902 	iTimeOutTimer.Close();
       
   903 	}
       
   904 
       
   905 // --------------------------------------------------------------------------
       
   906 // CNSmlNotifierTimeOut::LaunchNotifierTimer( CNSmlNotifierObserver*  aObserver )
       
   907 // Starts notifier time out timer
       
   908 // --------------------------------------------------------------------------
       
   909 //
       
   910 void CNSmlNotifierTimeOut::LaunchNotifierTimer( CNSmlNotifierObserver*  aObserver )
       
   911 	{
       
   912 	
       
   913 	if ( !IsActive() )
       
   914 		{
       
   915 		SetActive();
       
   916 		}
       
   917 		
       
   918 	iTimeOutTimer.CreateLocal();
       
   919 	iTimeOutTimer.After( iStatus, KNotifierTimeout );
       
   920 	iObserver = aObserver;
       
   921 	}
       
   922 	
       
   923 // --------------------------------------------------------------------------
       
   924 // CNSmlNotifierTimeOut::DoCancel()
       
   925 // From base class 
       
   926 // --------------------------------------------------------------------------
       
   927 //
       
   928 void CNSmlNotifierTimeOut::DoCancel()
       
   929 	{
       
   930 	iTimeOutTimer.Cancel();
       
   931 	}
       
   932 
       
   933 // --------------------------------------------------------------------------
       
   934 // CNSmlNotifierTimeOut::RunL()
       
   935 // From base class 
       
   936 // --------------------------------------------------------------------------
       
   937 //
       
   938 void CNSmlNotifierTimeOut::RunL()
       
   939 	{
       
   940 	if ( iStatus.Int() != KErrCancel )
       
   941 		{
       
   942 		iObserver->NotifierTimeOut();	
       
   943 		}
       
   944 	}
       
   945 
       
   946 // --------------------------------------------------------------------------
       
   947 // CNSmlCancelTimeOut::CNSmlCancelTimeOut()
       
   948 // Constructor
       
   949 // --------------------------------------------------------------------------
       
   950 //
       
   951 CNSmlCancelTimeOut::CNSmlCancelTimeOut( CNSmlDMAgent& aDMAgent, CNSmlDSAgent& aDSAgent )
       
   952 : CActive(0), iDMAgent(aDMAgent), iDSAgent(aDSAgent)
       
   953 	{
       
   954 	CActiveScheduler::Add(this);
       
   955 	iTimeOutTimer.CreateLocal();
       
   956 	iDoCancel = EFalse;
       
   957 	if ( !IsActive() )
       
   958 		{
       
   959 		SetActive();
       
   960 		}
       
   961 		
       
   962 	iTimeOutTimer.After( iStatus, 10000 );	
       
   963 	}
       
   964 
       
   965 // --------------------------------------------------------------------------
       
   966 // CNSmlCancelTimeOut::~CNSmlCancelTimeOut()
       
   967 // Destructor
       
   968 // --------------------------------------------------------------------------
       
   969 //	
       
   970 CNSmlCancelTimeOut::~CNSmlCancelTimeOut()
       
   971 	{
       
   972 	Cancel();
       
   973 	iTimeOutTimer.Close();	
       
   974 	}
       
   975 
       
   976 // --------------------------------------------------------------------------
       
   977 // CNSmlCancelTimeOut::SetJobCancelled()
       
   978 // Flags job to be cancelled
       
   979 // --------------------------------------------------------------------------
       
   980 //			
       
   981 void CNSmlCancelTimeOut::SetJobCancelled( TSmlUsageType aUsageType )
       
   982 	{
       
   983 	iDoCancel = ETrue;
       
   984 	iUsageType = aUsageType;	
       
   985 	}
       
   986 
       
   987 // --------------------------------------------------------------------------
       
   988 // CNSmlCancelTimeOut::DoCancel()
       
   989 // From base class 
       
   990 // --------------------------------------------------------------------------
       
   991 //	
       
   992 void CNSmlCancelTimeOut::DoCancel()
       
   993 	{
       
   994 	iTimeOutTimer.Cancel();	
       
   995 	}
       
   996 
       
   997 // --------------------------------------------------------------------------
       
   998 // CNSmlCancelTimeOut::RunL()
       
   999 // From base class 
       
  1000 // --------------------------------------------------------------------------
       
  1001 //
       
  1002 void CNSmlCancelTimeOut::RunL()
       
  1003 	{
       
  1004 	if ( iDoCancel )
       
  1005 		{
       
  1006 		if ( iUsageType == ESmlDevMan )
       
  1007 			{
       
  1008 			iDMAgent.Cancel();					
       
  1009 			}
       
  1010 		else
       
  1011 			{
       
  1012 			iDSAgent.Cancel();					
       
  1013 			}
       
  1014 		}
       
  1015 	else
       
  1016 		{
       
  1017 		if ( !IsActive() )
       
  1018 			{
       
  1019 			SetActive();
       
  1020 			}
       
  1021 		iTimeOutTimer.After( iStatus, 10000 );		
       
  1022 		}
       
  1023 	}
       
  1024 
       
  1025 // --------------------------------------------------------------------------
       
  1026 // CNSmlThreadParams::~CNSmlThreadParams()
       
  1027 // Destructor
       
  1028 // --------------------------------------------------------------------------
       
  1029 //
       
  1030 CNSmlThreadParams::~CNSmlThreadParams()
       
  1031 	{
       
  1032 	delete iCSArray;
       
  1033 	iCSArray = 0;
       
  1034 	}