コード例 #1
0
// -----------------------------------------------------------------------------
// 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 );
    }