syncmlfw/common/alertqueue/src/NSmlAlertHandler.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Alert queue and handler
       
    15 *
       
    16 */
       
    17 
       
    18 #include <nsmldebug.h>
       
    19 
       
    20 #include "NSmlAlertQueue.h"
       
    21 #include "nsmlsosserverdefs.h"
       
    22 
       
    23 //Fix to Remove the Bad Compiler Warnings
       
    24 #ifndef __WINS__
       
    25 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    26 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    27 // DBG_ARGS8 macro in no-debug builds.
       
    28 #pragma diag_remark 174
       
    29 #endif
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // ---------------------------------------------------------
       
    34 // CNSmlAlertHandler::NewL(MNSmlMessageHandler* aMsgHandler)
       
    35 // Two phase constructor
       
    36 // ---------------------------------------------------------
       
    37 //
       
    38 CNSmlAlertHandler* CNSmlAlertHandler::NewL( MNSmlMessageHandler* aMsgHandler )
       
    39 	{
       
    40 	CNSmlAlertHandler* self = new (ELeave) CNSmlAlertHandler;
       
    41 	CleanupStack::PushL( self );
       
    42 	self->ConstructL( aMsgHandler );
       
    43 	CleanupStack::Pop();
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------
       
    48 // CNSmlAlertHandler::CNSmlAlertHandler()
       
    49 // Constructor
       
    50 // ---------------------------------------------------------
       
    51 CNSmlAlertHandler::CNSmlAlertHandler()
       
    52 : CActive(0), iNewEntry( EFalse )
       
    53 	{
       
    54 	CActiveScheduler::Add( this );
       
    55 	}
       
    56 
       
    57 // ---------------------------------------------------------
       
    58 // CNSmlAlertHandler::~CNSmlAlertHandler()
       
    59 // Destructor
       
    60 // ---------------------------------------------------------
       
    61 CNSmlAlertHandler::~CNSmlAlertHandler()
       
    62 	{
       
    63 
       
    64 	Cancel();
       
    65 	if( !iHistoryArray )
       
    66 	{
       
    67 	iHistoryArray->SetOwnerShip( ETrue );
       
    68 	delete iHistoryArray;
       
    69 	}
       
    70 	delete iAlertParser;			
       
    71 		
       
    72 	if ( ! iNewEntry )
       
    73 		{
       
    74 		delete iAlertInfo;
       
    75 		delete iHistoryInfo;
       
    76 		}
       
    77 	}
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 // CNSmlAlertHandler::DoCancel()
       
    81 // Method from base class
       
    82 // ---------------------------------------------------------
       
    83 void CNSmlAlertHandler::DoCancel()
       
    84 	{
       
    85 	
       
    86 	}
       
    87 
       
    88 // ---------------------------------------------------------
       
    89 // CNSmlAlertHandler::ProcessAlert()
       
    90 // Activates alert handler
       
    91 // ---------------------------------------------------------
       
    92 void CNSmlAlertHandler::ProcessAlert()
       
    93 	{
       
    94 	if ( IsActive() )
       
    95 		{
       
    96 		return;
       
    97 		}
       
    98 	SetActive();
       
    99 	TRequestStatus* pStatus = &iStatus;
       
   100 	User::RequestComplete(pStatus, KErrNone);
       
   101 	}
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // CNSmlAlertHandler::ConstructL( MNSmlMessageHandler* aMsgHandler )
       
   105 // Second phase constructor
       
   106 // ---------------------------------------------------------
       
   107 void CNSmlAlertHandler::ConstructL( MNSmlMessageHandler* aMsgHandler )
       
   108 	{
       
   109 	User::LeaveIfNull(aMsgHandler);
       
   110 	iMsgHandler = aMsgHandler;
       
   111 	
       
   112     iHistoryInfo = CSyncMLHistoryPushMsg::NewL();
       
   113 
       
   114 	iAlertInfo = new ( ELeave ) CSmlAlertInfo;
       
   115 
       
   116 	iHistoryArray = CNSmlHistoryArray::NewL();
       
   117 	iHistoryArray->SetOwnerShip( EFalse ); // move ownership to history array
       
   118 	}
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CNSmlAlertHandler::RunL()
       
   122 // Method from base class
       
   123 // ---------------------------------------------------------
       
   124 void CNSmlAlertHandler::RunL()
       
   125     {
       
   126     TRAP_IGNORE( DoRunL() );    
       
   127     };
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CNSmlAlertHandler::DoRunL()
       
   131 // Method from base class
       
   132 // ---------------------------------------------------------
       
   133 void CNSmlAlertHandler::DoRunL()
       
   134 	{
       
   135 
       
   136     // Delete member variables
       
   137     iHistoryArray->SetOwnerShip( ETrue );
       
   138 	delete iHistoryArray;
       
   139 	iHistoryArray = NULL;
       
   140 	delete iAlertParser;			
       
   141 	iAlertParser = NULL;
       
   142 
       
   143     // These are deleted by iHistoryArray
       
   144 	iAlertInfo = NULL;
       
   145 	iHistoryInfo = NULL;
       
   146 
       
   147     // Create member variables	
       
   148 	iHistoryInfo = CSyncMLHistoryPushMsg::NewL();
       
   149 	iAlertInfo = new ( ELeave ) CSmlAlertInfo;
       
   150 	iHistoryArray = CNSmlHistoryArray::NewL();
       
   151 	iHistoryArray->SetOwnerShip( EFalse ); // move ownership to history array
       
   152 	
       
   153 	
       
   154 	// Continue DoRunL method
       
   155 	TBool message( EFalse );
       
   156 	TBool quit( ETrue );
       
   157 	TBool doPop( EFalse );
       
   158 	
       
   159 	TPtrC8 pPackage;
       
   160 	TSmlUsageType type;
       
   161 	TSmlProtocolVersion version;
       
   162 	TSmlTransportId bearerType;
       
   163 	
       
   164 	//check message
       
   165 	iMsgHandler->CheckMessage( message, type, version, bearerType );
       
   166 	iAlertInfo->SetJobControl( CSmlAlertInfo::EDoNotCreateJob );
       
   167 	iAlertInfo->SetTransportId( bearerType );
       
   168 	#ifdef __NSML_DEBUG__
       
   169 		DBG_ARGS(_S("CNSmlAlertHandler::RunL(): TransportId : '%d'"), bearerType );
       
   170 	#endif // __NSML_DEBUG__
       
   171 
       
   172 	TInt err( KErrNone );
       
   173 	
       
   174 	if ( message )
       
   175 	    {
       
   176 	    		
       
   177     	iAlertParser = NSmlParserFactory::CreateAlertParserL( type, version, *iAlertInfo, *iHistoryInfo );
       
   178     	iAlertParser->CreateBufferL( iMsgHandler->MessageSize() );
       
   179     	iMsgHandler->AlertMessage( iAlertParser->Message() );
       
   180     					
       
   181     	TRAP(err, iAlertParser->ParseMessageL());
       
   182     	DBG_FILE_CODE(err, _S8("CNSmlAlertHandler::RunL() : Alert result"));
       
   183     	iAlertInfo->SetErrorCode( err );
       
   184     	
       
   185     	iNewEntry = ETrue;
       
   186     	
       
   187     	if ( err != KErrNone)
       
   188     		{
       
   189     		iNewEntry = EFalse;
       
   190     		iAlertInfo->SetJobControl( CSmlAlertInfo::EDoNotCreateJob );
       
   191     		
       
   192     		// Close local connection
       
   193     		if ( bearerType != KUidNSmlMediumTypeInternet.iUid )
       
   194     			{
       
   195     			iMsgHandler->DoDisconnect();	
       
   196     			}
       
   197     	   		
       
   198     		if ( err == KErrCorrupt )
       
   199     			{
       
   200     			return; 
       
   201     			}
       
   202     		}
       
   203     	else
       
   204     		{
       
   205     		CheckDigestL( iAlertInfo->Profile(), iHistoryInfo->MsgDigest() );
       
   206     					
       
   207     		CheckProtocolAndChangeL( version );
       
   208     		    					
       
   209     		if ( ( type == ESmlDataSync )  && ( version == ESmlVersion1_1_2 ) )
       
   210     			{
       
   211     			iAlertInfo->SetConfirmation( ETrue );
       
   212     			pPackage.Set( iAlertParser->DoMessageCopyLC() );
       
   213     			doPop = ETrue;
       
   214     			}
       
   215     		}
       
   216 
       
   217     	delete iAlertParser;
       
   218     	iAlertParser = NULL;
       
   219 	    }
       
   220 	    
       
   221 	iAlertInfo->SetConfirmation( FinalizeBeforeJobCreationL() );
       
   222 	iMsgHandler->CreateJobL( *iAlertInfo, quit, pPackage);                
       
   223 		
       
   224 	if ( doPop )
       
   225 		{
       
   226 		CleanupStack::PopAndDestroy(); // DoMessageCopyLC()
       
   227 		}
       
   228 	
       
   229 	
       
   230 	if (! quit)
       
   231 		{
       
   232 		ProcessAlert();
       
   233 		}
       
   234 	
       
   235 	}
       
   236 
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // CNSmlAlertHandler::FinalizeBeforeJobCreationL()
       
   240 // Connects to notifier plug in to show confirmation note and
       
   241 // waits for the response.
       
   242 // ---------------------------------------------------------
       
   243 TBool CNSmlAlertHandler::FinalizeBeforeJobCreationL()
       
   244 	{
       
   245 	
       
   246 	if ( iAlertInfo->Profile() < KMaxDataSyncID )		
       
   247 		{
       
   248 		CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   249 		
       
   250 		CNSmlDSProfile* profile = settings->ProfileL( iAlertInfo->Profile() );
       
   251 		
       
   252 		if ( !profile )
       
   253 			{
       
   254 			CleanupStack::PopAndDestroy(); //settings
       
   255 			return EFalse;
       
   256 			}
       
   257 			
       
   258 		CleanupStack::PushL( profile );
       
   259 		
       
   260 		iAlertInfo->SetTransportId( profile->IntValue( EDSProfileTransportId) );
       
   261 		iAlertInfo->SetConnectionId( profile->IntValue( EDSProfileTransportId) );
       
   262 		
       
   263 		CleanupStack::PopAndDestroy(2); //profile, settings
       
   264 		}
       
   265 	else
       
   266 		{
       
   267 		CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   268 		
       
   269 		CNSmlDMProfile* profile = settings->ProfileL( iAlertInfo->Profile() );
       
   270 		
       
   271 		if ( !profile )
       
   272 			{
       
   273 			CleanupStack::PopAndDestroy(); //settings
       
   274 			return EFalse;
       
   275 			}
       
   276 		
       
   277 		CleanupStack::PushL( profile );
       
   278         profile->SetIntValue( EDMProfileSessionId, iAlertInfo->SessionId() );
       
   279         profile->SaveL();
       
   280 
       
   281 		iAlertInfo->SetTransportId( profile->IntValue( EDMProfileTransportId) );
       
   282 		iAlertInfo->SetConnectionId( profile->IntValue( EDMProfileTransportId) );
       
   283 		
       
   284 		CleanupStack::PopAndDestroy(2); //profile, settings
       
   285 		}
       
   286 	
       
   287 	SaveAlertInfoL();
       
   288 	
       
   289 	if ( ! iAlertInfo->CreateSession() )
       
   290 		{
       
   291 		return ETrue;
       
   292 		}
       
   293 	
       
   294 	return ETrue;
       
   295 	}
       
   296 
       
   297 // ---------------------------------------------------------
       
   298 // CheckDigestL( const TInt aProfileId, const TDesC8& aDigest )
       
   299 // Checks whether alert has already been handled. Receive count
       
   300 // is updated if handled before.
       
   301 // ---------------------------------------------------------
       
   302 void CNSmlAlertHandler::CheckDigestL( const TInt aProfileId, const TDesC8& aDigest )
       
   303 	{
       
   304 	
       
   305 	if ( aProfileId < KMaxDataSyncID )		
       
   306 		{
       
   307 		CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   308 		
       
   309 		CNSmlDSProfile* profile = settings->ProfileL(aProfileId);
       
   310 		CleanupStack::PushL(profile);
       
   311 		
       
   312 		if ( ! profile->HasLogL() )
       
   313 			{
       
   314 			CleanupStack::PopAndDestroy(2); //profile, settings
       
   315 			return;
       
   316 			}
       
   317 		
       
   318 		RReadStream& readStream = profile->LogReadStreamL();
       
   319 		CleanupClosePushL( readStream );
       
   320 		
       
   321 		iHistoryArray->InternalizeL(readStream);
       
   322 		
       
   323 		CleanupStack::PopAndDestroy(); //readStream
       
   324 		
       
   325 		for (TInt i = 0; i < iHistoryArray->Count(); i++)
       
   326 			{
       
   327 			CSyncMLHistoryEntry& hEntry = iHistoryArray->Entry(i);
       
   328 			CSyncMLHistoryPushMsg * pushMsg = CSyncMLHistoryPushMsg::DynamicCast(&hEntry);
       
   329 			
       
   330 			if (pushMsg == NULL)
       
   331 				continue;
       
   332 			
       
   333 			if ( aDigest.Compare( pushMsg->MsgDigest() ) == 0)
       
   334 				{
       
   335 				pushMsg->IncReceivedCount();
       
   336 				iNewEntry = EFalse;
       
   337 				iAlertInfo->SetConfirmation( iNewEntry );
       
   338 				}
       
   339 			}
       
   340 	
       
   341 		CleanupStack::PopAndDestroy(2); //profile, settings
       
   342 		}
       
   343 	else
       
   344 		{
       
   345 		CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   346 		
       
   347 		CNSmlDMProfile* profile = settings->ProfileL(aProfileId);
       
   348 		CleanupStack::PushL(profile);
       
   349 		
       
   350 		if ( ! profile->HasLogL() )
       
   351 			{
       
   352 			CleanupStack::PopAndDestroy(2); //profile, settings
       
   353 			return;
       
   354 			}
       
   355 			
       
   356 		RReadStream& readStream = profile->LogReadStreamL();
       
   357 		CleanupClosePushL( readStream );
       
   358 
       
   359 		iHistoryArray->InternalizeL( readStream );
       
   360 		
       
   361 		CleanupStack::PopAndDestroy(); //readStream
       
   362 		
       
   363 		for (TInt i = 0; i < iHistoryArray->Count(); i++)
       
   364 			{
       
   365 			CSyncMLHistoryEntry& hEntry = iHistoryArray->Entry(i);
       
   366 			CSyncMLHistoryPushMsg * pushMsg = CSyncMLHistoryPushMsg::DynamicCast(&hEntry);
       
   367 			
       
   368 			if (pushMsg == NULL)
       
   369 				continue;
       
   370 			
       
   371 			if ( aDigest.Compare( pushMsg->MsgDigest() ) == 0)
       
   372 				{
       
   373 				pushMsg->IncReceivedCount();
       
   374 				iNewEntry = EFalse;
       
   375 				iAlertInfo->SetConfirmation( iNewEntry );
       
   376 				}
       
   377 			}
       
   378 		CleanupStack::PopAndDestroy(2); //profile, settings
       
   379 		}
       
   380 	}
       
   381 
       
   382 // ---------------------------------------------------------
       
   383 // CNSmlAlertHandler::SaveAlertInfoL( )
       
   384 // Saves the result to profile history log and adds new entry
       
   385 // ---------------------------------------------------------
       
   386 void CNSmlAlertHandler::SaveAlertInfoL( )
       
   387 	{
       
   388 	
       
   389 	TInt profileId( iAlertInfo->Profile() );
       
   390 	
       
   391 	if ( profileId == KNSmlNullId )
       
   392 		{
       
   393 		return;
       
   394 		}
       
   395 		
       
   396 	if ( profileId < KMaxDataSyncID )
       
   397 		{
       
   398 		CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   399 		
       
   400 		CNSmlDSProfile* profile = settings->ProfileL( profileId );
       
   401 		CleanupStack::PushL( profile );
       
   402 		RWriteStream& stream = profile->LogWriteStreamL();
       
   403 		CleanupClosePushL( stream );
       
   404 		
       
   405 		if ( iNewEntry )
       
   406 			{
       
   407 			RPointerArray<CSmlAlertInfo> alertInfoArray;
       
   408 			CleanupClosePushL( alertInfoArray );
       
   409 			
       
   410 			alertInfoArray.AppendL( iAlertInfo );
       
   411 			
       
   412 			iHistoryInfo->AddAlertsL( alertInfoArray );
       
   413 			iHistoryArray->AppendEntryL( iHistoryInfo );
       
   414 			CleanupStack::PopAndDestroy(); //alertInfoArray	
       
   415 			}
       
   416 			
       
   417 		iHistoryArray->ExternalizeL(stream);
       
   418 		
       
   419 		CleanupStack::PopAndDestroy(); //stream
       
   420 		
       
   421 		profile->WriteStreamCommitL();
       
   422 	
       
   423 		CleanupStack::PopAndDestroy(2); // profile, settings
       
   424 		}
       
   425 	else
       
   426 		{
       
   427 		CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   428 		
       
   429 		CNSmlDMProfile* profile = settings->ProfileL( profileId );
       
   430 		CleanupStack::PushL( profile );
       
   431 		
       
   432 		RWriteStream& stream = profile->LogWriteStreamL();
       
   433 		CleanupClosePushL( stream );
       
   434 		
       
   435 		if ( iNewEntry )
       
   436 			{
       
   437 			RPointerArray<CSmlAlertInfo> alertInfoArray;
       
   438 			CleanupClosePushL( alertInfoArray );
       
   439 			
       
   440 			alertInfoArray.AppendL( iAlertInfo );
       
   441 		
       
   442 			iHistoryInfo->AddAlertsL( alertInfoArray );
       
   443 			iHistoryArray->AppendEntryL( iHistoryInfo );
       
   444 			
       
   445 			CleanupStack::PopAndDestroy(); //alertInfoArray	
       
   446 			}
       
   447 			
       
   448 		iHistoryArray->ExternalizeL( stream );
       
   449 		CleanupStack::PopAndDestroy(); //stream
       
   450 		
       
   451 		profile->WriteStreamCommitL();
       
   452 		
       
   453 		CleanupStack::PopAndDestroy(2); // profile, settings
       
   454 		}
       
   455 	}
       
   456 
       
   457 
       
   458 // ---------------------------------------------------------
       
   459 // CheckProtocolAndChange(TSmlProtocolVersion& aVersion)
       
   460 // Checks if profile has different version and chages to alert's
       
   461 // version if different.
       
   462 // ---------------------------------------------------------
       
   463 void CNSmlAlertHandler::CheckProtocolAndChangeL( TSmlProtocolVersion& aVersion ) const
       
   464 	{
       
   465 	TInt profileId( iAlertInfo->Profile() );
       
   466 	
       
   467 	if ( profileId == KNSmlNullId )
       
   468 		{
       
   469 		return;
       
   470 		}
       
   471 		
       
   472 	if ( profileId < KMaxDataSyncID )
       
   473 		{
       
   474 		CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   475 		CNSmlDSProfile* profile = settings->ProfileL( profileId );
       
   476 		CleanupStack::PushL( profile );
       
   477 		
       
   478 		if ( profile->IntValue( EDSProfileProtocolVersion ) != aVersion )
       
   479 			{
       
   480 			profile->SetIntValue( EDSProfileProtocolVersion, (TInt) aVersion );
       
   481 			profile->SaveL();
       
   482 			}
       
   483 			
       
   484 		CleanupStack::PopAndDestroy(2); // profile, settings
       
   485 		}
       
   486 	}
       
   487 
       
   488 //End of File