multimediacommsengine/mmcecli/src/mceevent.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "mceevent.h"
       
    22 #include "mcemanager.h"
       
    23 #include "mcerefer.h"
       
    24 #include "mcetransactiondatacontainer.h"
       
    25 #include "mceeventobserver.h"
       
    26 #include "mceitcsender.h"
       
    27 #include "mceeventreceiver.h"
       
    28 #include "mceevents.h"
       
    29 #include "mceserial.h"
       
    30 #include "mce.h"
       
    31 #include "mceclilogs.h"
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CMceEvent::ConstructL
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 void CMceEvent::ConstructL( HBufC8* aEventHeader, 
       
    40                             HBufC8* aRecipient,
       
    41 	                        HBufC8* aOriginator,
       
    42 	                        TUint32 aDialogType,
       
    43 	                        CDesC8Array* aParams )
       
    44     {
       
    45     MCECLI_DEBUG("CMceEvent::ConstructL, Entry");
       
    46     
       
    47     User::LeaveIfError( aEventHeader && aRecipient ? KErrNone : KErrArgument );
       
    48     
       
    49     HBufC8* originator = 0;
       
    50     if ( aOriginator )
       
    51         {
       
    52         originator = aOriginator;
       
    53         }
       
    54     else
       
    55         {
       
    56         originator = KNullDesC8().AllocLC();
       
    57         }
       
    58         
       
    59     CDesC8Array* params = 0;    
       
    60     if ( aParams )
       
    61         {
       
    62         params = aParams;
       
    63         }
       
    64     else
       
    65         {
       
    66         params = new (ELeave) CDesC8ArrayFlat( KMceArrayGranularity );  
       
    67         CleanupStack::PushL( params );  
       
    68         }
       
    69 	
       
    70 	if ( params->Count() == 0 )
       
    71 	    {
       
    72 	    params->AppendL( *aEventHeader );
       
    73         if ( iDialogId == KMceNotAssigned )
       
    74             {
       
    75             params->AppendL( *aRecipient );
       
    76             params->AppendL( *originator );
       
    77             }
       
    78         }
       
    79 	
       
    80     iId = iManager.RegisterEventL( this, *params, iDialogId, aDialogType );
       
    81 
       
    82     if ( !aParams )
       
    83         {
       
    84         CleanupStack::PopAndDestroy( params );
       
    85         }
       
    86         
       
    87     //get server    
       
    88     RMce& server = iManager.ServerSession();
       
    89     
       
    90     //create sender
       
    91     iSender = CMceItcSender::NewL( server  );
       
    92     //create receiver
       
    93     iReceiver = new (ELeave) CMceEventReceiver( server, *this );
       
    94     
       
    95     // Handle ownership change of input params when leave cannot occur anymore 
       
    96     
       
    97     if ( !aOriginator )
       
    98         {
       
    99         CleanupStack::Pop( originator );
       
   100         }     
       
   101     iOriginator = originator; 
       
   102     
       
   103     delete aParams;
       
   104     iEventHeader = aEventHeader;
       
   105     iRecipient = aRecipient;
       
   106     MCECLI_DEBUG("CMceEvent::ConstructL, Exit");
       
   107     }
       
   108 
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CMceEvent::~CMceEvent
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 CMceEvent::~CMceEvent()
       
   115     {
       
   116     MCECLI_DEBUG("CMceEvent::~CMceEvent, Entry");
       
   117     delete iReceiver;
       
   118     iReceiver = NULL;
       
   119     
       
   120 	iManager.UnregisterEvent( this );    
       
   121     
       
   122     delete iSender;    
       
   123     delete iEventHeader;
       
   124     delete iRecipient;
       
   125     delete iOriginator;
       
   126     MCECLI_DEBUG("CMceEvent::~CMceEvent, Exit");
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CMceEvent::State
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C CMceEvent::TState CMceEvent::State() const
       
   134     {
       
   135     return iState;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CMceEvent::Recipient
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C const TDesC8& CMceEvent::Recipient() const
       
   143     {
       
   144     return *iRecipient;
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CMceEvent::Originator
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C const TDesC8& CMceEvent::Originator() const
       
   152     {
       
   153     return *iOriginator;
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CMceEvent::EventHeader
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C const TDesC8& CMceEvent::EventHeader() const
       
   161     {
       
   162     return *iEventHeader;
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CMceEvent::TerminateL
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 EXPORT_C void CMceEvent::TerminateL( CDesC8Array* aHeaders,
       
   170             						 HBufC8* aContentType,
       
   171             						 HBufC8* aContent ) 
       
   172     {
       
   173     MCECLI_DEBUG("CMceEvent::TerminateL, Entry");
       
   174     TUint state = State();
       
   175     MCECLI_DEBUG_EVENTSTATE( "state", state );
       
   176     User::LeaveIfError( State() == EActive ? KErrNone : KErrNotReady );
       
   177     User::LeaveIfError( 
       
   178         ( aContentType && aContent ) || !( aContentType || aContent ) ? 
       
   179         KErrNone : KErrArgument );
       
   180     
       
   181     CMceRefer* refer = iManager.FindRefer( *this );
       
   182     if ( refer && !refer->IsInRefer() )
       
   183         {
       
   184         // Terminating not supported for event attached to out refer
       
   185         User::Leave( KErrNotSupported ); 
       
   186         }
       
   187     
       
   188 	
       
   189 	//This way both the terminating NOTIFY for CMceInEvent and 
       
   190 	//the un-SUBSCRIBE for CMceOutEvent can contain 
       
   191 	// At this moment, only MT terminate in Refer cases is able 
       
   192 	// to terminate not in MO
       
   193 	TMceIds ids;
       
   194 	PrepareForITC( ids );
       
   195 	ids.iState = State();
       
   196 	
       
   197 
       
   198 		
       
   199 	CMceMsgSIPEvent* notify = new (ELeave) CMceMsgSIPEvent( 
       
   200                                                 iId,
       
   201                                                 iDialogId,
       
   202                                                 EMceItcEventTypeEither,
       
   203                                                 CMceRefer::ENoSuppression, 
       
   204                                                 ( TUint32 ) KErrNotFound, 
       
   205                                                 aHeaders, 
       
   206                                                 aContentType );
       
   207 	CleanupStack::PushL( notify );
       
   208 
       
   209 	notify->OwnershipRollbackPushLC();
       
   210 	
       
   211 
       
   212 	iSender->SendL( ids, EMceItcTerminateEvent, *notify, aContent );
       
   213 
       
   214 	CleanupStack::Pop(); // notify->OwnershipRollbackPushLC()
       
   215     
       
   216     //cleanup    
       
   217     CleanupStack::PopAndDestroy( notify );
       
   218     //update the state
       
   219 	iState = static_cast<TState>( ids.iState );
       
   220 	
       
   221 	state = State();
       
   222     MCECLI_DEBUG_EVENTSTATE( "CMceEvent::TerminateL, after ITC, state", state );
       
   223     MCECLI_DEBUG("CMceEvent::TerminateL, Exit");
       
   224    
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CMceEvent::AssociatedSession
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C CMceSession* CMceEvent::AssociatedSession() const
       
   232     {
       
   233     return iManager.FindAssociatedSession( iDialogId );
       
   234     }
       
   235     
       
   236 // -----------------------------------------------------------------------------
       
   237 // CMceEvent::AssociatedEvents
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 EXPORT_C RPointerArray< CMceEvent >* CMceEvent::AssociatedEventsL() const
       
   241     {
       
   242     return NULL;
       
   243     }
       
   244     
       
   245 // -----------------------------------------------------------------------------
       
   246 // CMceEvent::AssociatedRefers
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 EXPORT_C RPointerArray< CMceRefer >* CMceEvent::AssociatedRefersL() const
       
   250     {
       
   251     return NULL;
       
   252     }
       
   253     
       
   254 // -----------------------------------------------------------------------------
       
   255 // CMceEvent::CMceEvent
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 CMceEvent::CMceEvent( CMceManager& aManager,                      
       
   259                       TUint32 aProfileId,
       
   260                       TUint32 aDialogId,
       
   261                       TUint32 aRefreshInterval )
       
   262     : iManager( aManager ), 
       
   263 	  iProfileId( aProfileId ),
       
   264       iId ( KMceNotAssigned ),
       
   265       iDialogId( aDialogId ),
       
   266       iRefreshInterval( aRefreshInterval )
       
   267     {
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CMceEvent::Id
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 TUint32 CMceEvent::Id() const
       
   275     {
       
   276     return iId;
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CMceEvent::DialogId
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 TUint32 CMceEvent::DialogId() const
       
   284     {
       
   285     return iDialogId;
       
   286     }
       
   287 
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CMceEvent::ITCSender
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 CMceItcSender& CMceEvent::ITCSender()
       
   294 	{
       
   295 	return *iSender;
       
   296 	}
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CMceEvent::Manager
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 CMceManager& CMceEvent::Manager()
       
   303     {
       
   304     return iManager;
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CMceEvent::ProfileId
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 TUint32 CMceEvent::ProfileId() const
       
   312 	{
       
   313 	return iProfileId;
       
   314 	}
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CMceEvent::PrepareForITC
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CMceEvent::PrepareForITC( TMceIds& aId )
       
   321     {
       
   322 	aId.iAppUID = Manager().AppUid().iUid;
       
   323     aId.iManagerType = KMceCSSIPEvent;
       
   324 	aId.iSessionID = Id();
       
   325 	aId.iProfileID = iProfileId;
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CMceEvent::EventReceivedL
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 void CMceEvent::EventReceivedL( TMceIds& aId  )
       
   333     {
       
   334     TMceEvent event( aId, NULL, NULL ); 
       
   335     EventReceivedL( event );
       
   336     }
       
   337 
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CMceEvent::EventReceivedL
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 void CMceEvent::EventReceivedL( TMceIds& aId, CMceMsgBase& aMessage )
       
   344     {
       
   345     TMceEvent event( aId, &aMessage, NULL ); 
       
   346     EventReceivedL( event );
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CMceEvent::EventReceivedL
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CMceEvent::EventReceivedL( TMceIds& aId, 
       
   354                                 CMceMsgBase& aMessage, 
       
   355                                 HBufC8* aContent )
       
   356     {
       
   357     TMceEvent event( aId, &aMessage, aContent ); 
       
   358     EventReceivedL( event );
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CMceEvent::ErrorOccuredL
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 void CMceEvent::ErrorOccuredL( TInt aError )
       
   366     {
       
   367     MCECLI_DEBUG("CMceEvent::ErrorOccuredL, Entry");
       
   368     MCECLI_DEBUG_DVALUE("error", aError );
       
   369     
       
   370     iState = ETerminated;    
       
   371     User::LeaveIfError( iManager.EventObserver() ? KErrNone : KErrArgument );
       
   372     
       
   373     CMceRefer* refer = iManager.FindRefer( *this );
       
   374 
       
   375     if ( refer )
       
   376         {
       
   377         refer->ErrorOccuredL( aError );
       
   378         }
       
   379     
       
   380     iManager.EventObserver()->Failed( *this, aError );
       
   381 	
       
   382 	TUint state = State();
       
   383     MCECLI_DEBUG_EVENTSTATE( "CMceEvent::ErrorOccuredL, before exit. state", state );
       
   384     MCECLI_DEBUG("CMceEvent::ErrorOccuredL, Exit");
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CMceEvent::EventReceivedL
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CMceEvent::EventReceivedL( TMceEvent& aEvent )
       
   392     {
       
   393     MCECLI_DEBUG("CMceEvent::EventReceivedL, Entry");
       
   394     TUint state = State();
       
   395     MCECLI_DEBUG_EVENTSTATE("this: state", state );
       
   396     MCECLI_DEBUG_DVALUE("this: id", Id() );
       
   397     
       
   398     User::LeaveIfError( aEvent.Id().iSessionID == Id() ? 
       
   399         KErrNone : KErrGeneral );
       
   400     User::LeaveIfError( aEvent.Id().IsSessionId() ? KErrNone : KErrGeneral );
       
   401         
       
   402     HandleEventL( aEvent );
       
   403 
       
   404 	state = State();
       
   405     MCECLI_DEBUG_EVENTSTATE("CMceEvent::EventReceivedL, before exit. state", state );
       
   406     MCECLI_DEBUG("CMceEvent::EventReceivedL, Exit");
       
   407     
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CMceEvent::HandleEventL
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 void CMceEvent::HandleEventL( TMceEvent& aEvent )
       
   415     {
       
   416     MCECLI_DEBUG("CMceEvent::EventReceivedL, Entry");
       
   417     
       
   418     // update state
       
   419     iState = static_cast<TState>( aEvent.Id().iState );
       
   420     MMceEventObserver* observer = iManager.EventObserver();
       
   421     TMceTransactionDataContainer* container = iManager.TransactionContainer();
       
   422 
       
   423     CMceMsgBase* msg = static_cast< CMceMsgBase* >( aEvent.Message() );
       
   424     // notify observer
       
   425     if ( aEvent.MessageType() == EMceItcMsgTypeSIPReply  
       
   426         && aEvent.Action() == EMceItcStateChanged )
       
   427         {
       
   428         MCECLI_DEBUG("CMceEvent::HandleEventL, session state changed" );
       
   429         MCECLI_DEBUG("CMceEvent::HandleEventL, caused by sip reply");
       
   430         
       
   431         CMceMsgSIPReply* replyMsg = static_cast< CMceMsgSIPReply* >( msg );
       
   432         
       
   433         CMceRefer* refer = iManager.FindRefer( *this );
       
   434     
       
   435         if ( replyMsg && container && 
       
   436             !( refer && !refer->IsInRefer() && 
       
   437             refer->State() == CMceRefer::EPending ) )
       
   438             {
       
   439             container->SetStatusCode( replyMsg->iCode );
       
   440             replyMsg->iCode = 0;
       
   441             container->SetReasonPhrase( replyMsg->Reason() );
       
   442             container->SetHeaders( replyMsg->Headers() );
       
   443             container->SetContentType( replyMsg->ContentType() );
       
   444             container->SetContent( aEvent.Content() );
       
   445             }
       
   446            
       
   447         if ( observer )
       
   448             {
       
   449             iManager.EventObserver()->EventStateChanged( *this, container );    
       
   450             }
       
   451         
       
   452         if ( refer && 
       
   453             !( iState == EActive && refer->State() == CMceRefer::EAccepted ) )
       
   454             {
       
   455             refer->EventStateChangedL( aEvent );
       
   456             }
       
   457         }
       
   458     else if ( aEvent.MessageType() == EMceItcMsgTypeEvent 
       
   459         && aEvent.Action() == EMceItcNotifyReceived ) 
       
   460         {
       
   461         MCECLI_DEBUG("CMceEvent::HandleEventL, notify received" );
       
   462         if ( observer )
       
   463             {
       
   464             CMceMsgSIPEvent* eventMsg =  static_cast<CMceMsgSIPEvent*>( msg );
       
   465             if ( container)
       
   466                 {
       
   467                 container->SetHeaders( eventMsg->SIPHeaders() );
       
   468                 container->SetContentType( eventMsg->ContentType() );
       
   469                 container->SetContent( aEvent.Content() );
       
   470                 }
       
   471             iManager.EventObserver()->NotifyReceived( *this, container );    
       
   472             }
       
   473         }
       
   474     else
       
   475         {
       
   476         MCECLI_DEBUG("CMceEvent::HandleEventL, not supported" );
       
   477         User::Leave( KErrNotSupported );
       
   478         }
       
   479         
       
   480     if ( container )
       
   481     	{
       
   482     	container->Clear();
       
   483     	}
       
   484     	
       
   485     if ( msg )
       
   486         {
       
   487         msg->Close();
       
   488         }
       
   489     delete aEvent.Content();
       
   490 
       
   491     MCECLI_DEBUG("CMceEvent::HandleEventL, Exit" );
       
   492     
       
   493     }