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