// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::RegisterCommandListL
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::RegisterCommandListL( 
                TMediatorCategory aCategory, 
                const RCommandList& aCommands,
                TSecureId aSecureId,
                MMediatorCommandObserver* aObserver )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::RegisterCommandListL"));
    // Check that domain exists --> if not add new
    CDomain* domain = iObjectHandler.FindDomain( aCategory.iDomain );
    if ( !domain )
        {
        domain = iObjectHandler.AddDomainL( aCategory.iDomain );
        }
    
    // Check that category exists --> if not add new
    TInt ignore = 0; // not used here    
    CCategory* category = domain->FindCategory( aCategory.iCategory, ignore );
    if ( !category )
        {
        category = domain->AddCategoryL( aCategory.iCategory );
        }
    
    // Loop through the commands and add them to list
    // Take the possible error to variable
    TInt error = KErrNone;
    TBool stop = EFalse;
    TInt index = 0;
    for ( index = 0; index < aCommands.Count() && !stop; index++ )
        {
        CCommand* newCommand = CCommand::NewL( aCommands[index] );
        CleanupStack::PushL( newCommand );
        newCommand->SetSecureId( aSecureId );   // For unregistering
        newCommand->SetObserver( aObserver );   // For getting the commands
        newCommand->SetCommitState( CItem::EAdded ); // For transaction handling
        TInt addError = category->AddCommand( newCommand );
        if ( addError )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::RegisterCommandListL: addError=%d\n"), addError ) );
            ERROR_TRACE(Print(_L("[Mediator] Failed to add command %d to category %d of domain %d\n"), newCommand->Id(), 
                                                                                                       aCategory.iCategory.iUid,
                                                                                                       aCategory.iDomain.iUid ) );
            // in case of error, delete event and take error
            CleanupStack::PopAndDestroy( newCommand );
            error = addError;
            stop = ETrue;
            }
        else
            {
            // Event has been added properly --> just pop
            CleanupStack::Pop( newCommand );
            }     
        newCommand = NULL;
        }  
    
    TRACE(Print(_L("[Mediator Server]\t Commands registered:\n")));
    TRACE(Print(_L("[Mediator Server]\t Success/count: %d/%d \tstatus: %d"), index, aCommands.Count(), error ));    
    
    // Check error if we need to do partial recovery
    if ( error != KErrNone ) 
        {
        // Remove the registered commands
        category->RollbackCommands();
        }
    else
        {
        // Complete command registration
        category->CommitCommands();
        
        // Use the object handler to notify command registration
        iObjectHandler.CommandsAdded( aCategory.iDomain, 
                                      aCategory.iCategory, 
                                      aCommands );
        }
    // In the end leave if error --> client gets error code
    User::LeaveIfError( error );
    }
// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::IssueCommandL
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::IssueCommandL( 
                                        TMediatorCategory aCategory, 
                                        MediatorService::TCommand aCommand,
                                        const TDesC8& aData,
                                        TCapabilitySet aCaps,
                                        MMediatorCommandResponseObserver* aObserver )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::IssueCommandL"));
    CCategory* category = iObjectHandler.CategoryL( aCategory );
    if ( category )
        {
        // Find the command from register list
        TInt ignore = 0;
        CCommand* commandPtr = category->FindCommand( aCommand.iCommandId, ignore );
        if ( !commandPtr )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: Command %d not found in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                                                   aCategory.iCategory.iUid,
                                                                                                                                                   aCategory.iDomain.iUid ) );
            
            User::Leave( KErrMediatorCommandNotFound );
            }     
        // Then check the capabilities && the version information
        // Capabilities are checked so that boolean ETrue is returned
        // when all parameter caps can be found from aCaps
        if ( !aCaps.HasCapabilities( commandPtr->Policy() ) )
            {
#ifdef _DEBUG
            for ( TInt index = 0; index < ECapability_Limit; index++ )
                {
                TCapabilitySet commandCaps = commandPtr->Policy();
                TBool command = commandCaps.HasCapability( (TCapability) index );
                TBool requestor = aCaps.HasCapability( (TCapability) index );
                if ( command && !requestor )
                    {
                    ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: capability %d missing\n"), index ));
                    ERROR_TRACE(Print(_L("[Mediator] Capability error when issuing command %d in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                                 aCategory.iCategory.iUid,
                                                                                                                                 aCategory.iDomain.iUid ) );
                    }
                }
#endif    
            User::Leave( KErrPermissionDenied );
            }
        // Check (major) version match
        if ( aCommand.iVersion.iMajor != commandPtr->Version().iMajor )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: registered=%d, issued=%d\n"), 
                                                                                        commandPtr->Version().iMajor,
                                                                                        aCommand.iVersion.iMajor ));
            ERROR_TRACE(Print(_L("[Mediator] Version error when issuing command %d in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                      aCategory.iCategory.iUid,
                                                                                                                      aCategory.iDomain.iUid ) );
            // There's a major version mismatch
            User::Leave( KErrMediatorVersionMismatch );
            }
        
        // If ok, issue to command to client
        // Make the new command, set initiator and responder
        // We don't need to command data for the pending list
        CCommand* newCommand = CCommand::NewL( aCommand );
        CleanupStack::PushL( newCommand );
        
        newCommand->SetResponseObserver( aObserver );
        newCommand->SetObserver( commandPtr->Observer() );
        newCommand->SetDomain( aCategory.iDomain );
        newCommand->SetCategory( aCategory.iCategory );
        newCommand->SetTimeout( commandPtr->Timeout() );
        
        // Start command timing, if it is not an infinite command
        if ( commandPtr->Timeout() != KMediatorTimeoutInfinite )
            {
            // Start timeout timer ( request callback here )
            newCommand->StartTimer( this );    
            }
                
        iCommandPendingList.AppendL( newCommand );
        
        CleanupStack::Pop( newCommand );
        
        // Now send the command to correct responder --> we have it pending
        commandPtr->Observer()->MediatorCommandL( aCategory.iDomain,
                                                  aCategory.iCategory,
                                                  aCommand.iCommandId,
                                                  aCommand.iVersion,
                                                  aData );
        }
    }