// -----------------------------------------------------------------------------
// CMceEventSubscribeTerminatingState::HandleReceiveRequestL
// Ápply concrete state.
// Be noticed that it will be also update, unsubscribe
// -----------------------------------------------------------------------------
//
void CMceEventSubscribeTerminatingState::HandleReceiveRequestL( 
												CMceComEvent& aEvent )
	{
	MCEEVENTS_DEBUG("CMceEventSubscribeTerminatingState::HandleReceiveRequestL, Entry");
	User::LeaveIfError( aEvent.SIPEvent().Dialog() ?
        KErrNone : KMceEventStateError );
	
	CSIPServerTransaction& serverTran = aEvent.SIPEvent().Request();
	CSIPServerTransaction* serverTranCheck = &serverTran;
    User::LeaveIfError( serverTranCheck ? KErrNone : KMceEventStateError );
    User::LeaveIfError( serverTran.RequestElements() ? KErrNone : KErrCorrupt );	    		 
	//Received Notify  form Mt
	//Receiving Notify and Send 200
	//Check the Notify Subscription state so knows which state should go
	//Observer inform the notify received, and Established
	const CSIPMessageElements& msgElem = 
	    serverTran.RequestElements()->MessageElements();
    
    CSIPResponseElements* response = 
        MCESIPEventHelper::NotifyRequestReceivedLC( aEvent, msgElem );
    if ( response->StatusCode() == KMceSipOK )
    	{
        CDesC8Array* headers = MceSip::UserHeadersToTextArrayL( 
                                    serverTran, 
                                    aEvent.SIPEvent().Dialog()->Dialog() );
        CleanupStack::PushL( headers );
        HBufC8* contenttype = NULL;
        if( msgElem.ContentType() )
    	    {
    	    contenttype= msgElem.ContentType()->ToTextValueL();
    	    }
        CleanupStack::PushL( contenttype );
        HBufC8* content = msgElem.Content().AllocLC();
    	aEvent.SIPEvent().NotifyReceivedL( headers,
    			    					   contenttype,
    									   content );
        
        CleanupStack::Pop( content );
        CleanupStack::Pop( contenttype );
        CleanupStack::Pop( headers );
        
        serverTran.SendResponseL( response );
        CleanupStack::Pop( response );

        aEvent.EventContext().SetCurrentStateL( 
                                            aEvent, 
                                            KMceTerminatedEventStateIndex );
        aEvent.SIPEvent().ClientStateChangedL(
                                            CMceEvent::ETerminated,
                                            EFalse );                                        
      	}
    else
      	{
        //when the notify request is bad, state doesnt change
        serverTran.SendResponseL( response );
        CleanupStack::Pop( response );
        }

   	MCEEVENTS_DEBUG("CMceEventSubscribeTerminatingState::HandleReceiveRequestL, Exit");
	}
// -----------------------------------------------------------------------------
// CMceEventReferMtEstablishedState::HandleReceiveRequestL
// Ápply concrete state.
// Be noticed that it will be also Refer/Bye or Refer/Invite(update)
// -----------------------------------------------------------------------------
//
void CMceEventReferMtEstablishedState::HandleReceiveRequestL( 
														CMceComEvent& aEvent )
	{
	MCEEVENTS_DEBUG("CMceEventReferMtEstablishedState::HandleReceiveRequestL, Entry");
	User::LeaveIfError( aEvent.SIPEvent().Dialog() ?
	    KErrNone : KMceEventStateError );
        
	User::LeaveIfError( aEvent.SIPEvent().Dialog()->Type() 
        == SIPStrings::StringF( SipStrConsts::ENotify ) ?
        KErrNone : KMceEventStateError );
    
    CSIPServerTransaction& serverTran = aEvent.SIPEvent().Request();
    CSIPServerTransaction* serverTranCheck = &serverTran;
	User::LeaveIfError( serverTranCheck ? KErrNone : KMceEventStateError );
	
	//Received Subscribe from Mo there is no action in the previous action
	User::LeaveIfError( aEvent.PreviousAction() == KErrNotFound ?
	    KErrNone : KMceEventStateError );
	
    User::LeaveIfError( CSIPDialog::EConfirmed ==
        aEvent.SIPEvent().Dialog()->Dialog().State() ?
        KErrNone : KMceEventStateError );
    
    User::LeaveIfError( serverTran.RequestElements() ? KErrNone : KErrCorrupt) ;
					
	const CSIPMessageElements& subscribeMsg = 
	    serverTran.RequestElements()->MessageElements();
			
	TBool terminated = EFalse;
    CSIPResponseElements* response = 
        MCESIPEventHelper::SubscribeRequestReceivedLC( aEvent, subscribeMsg );
    
    if ( response->StatusCode() == KMceSipOK )
		{

		MCESIPEventHelper::HandleExpireHeaderL(
        								subscribeMsg,
        								terminated ); 
   
        aEvent.SIPEvent().Request().SendResponseL( response );
        
        CleanupStack::Pop( response );
        //then send Notify

		CSIPNotifyDialogAssoc* dialogAssoc = 
    			static_cast <CSIPNotifyDialogAssoc* >
    			(aEvent.SIPEvent().Dialog()); 

		CSIPMessageElements* notifyMsg = 
	        MCESIPEventHelper::CreateReferNotifyMessageElementsLC( aEvent );
		
		CSIPSubscriptionStateHeader& subscrState = 
		    dialogAssoc->SubscriptionState();
		
		TInt nextState = KErrNotFound;		
        if ( terminated )
            {
			subscrState.SetSubStateValueL( KStateTerminated );
			RStringF expires = SIPStrings::StringF( SipStrConsts::EExpires );
            subscrState.DeleteParam( expires );
            nextState = KMceTerminatingEventStateIndex;
            }
        else
            {
            //when subscription state is active
            subscrState.SetSubStateValueL( KStateActive );
			subscrState.SetExpiresParameterL(aEvent.RefreshInterval());
            nextState = KMceEstablishedEventStateIndex;
			}
        
        CSIPClientTransaction* tx = dialogAssoc->SendNotifyL( notifyMsg );
		CleanupStack::Pop( notifyMsg );
	    CleanupStack::PushL( tx );
		aEvent.SIPEvent().SetPendingTransactionL( tx );
		CleanupStack::Pop( tx );	
		
        aEvent.EventContext().SetCurrentStateL( aEvent, nextState );
        }
	else
	  	{
	  	// bad request
	    aEvent.SIPEvent().Request().SendResponseL( response );
	    CleanupStack::Pop( response );
	    }

	MCEEVENTS_DEBUG("CMceEventReferMtEstablishedState::HandleReceiveRequestL, Exit");
	}