/*
* Copyright (c) 2002-2008 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Class that defines logical OR operation in an XML tree.
*
*/



// INCLUDES
#include "cforoperation.h"
#include "cfbasicoptrace.h"

#include <gmxmlnode.h>

// CONSTANTS
_LIT( KScriptOrName, "or" );

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CCFOrOperation::CCFOrOperation
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCFOrOperation::CCFOrOperation( MCFOperationServices& aServices,
    CCFOperationNode* aParent )
    :   CCFOperationNode( aServices, aParent )
    {
    FUNC_LOG;
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCFOrOperation::ConstructL()
    {
    FUNC_LOG;
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::NewL
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCFOrOperation* CCFOrOperation::NewL( MCFOperationServices& aServices,
    CCFOperationNode* aParent )
    {
    FUNC_LOG;

    CCFOrOperation* self = NewLC( aServices, aParent );
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::NewLC
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CCFOrOperation* CCFOrOperation::NewLC( MCFOperationServices& aServices,
    CCFOperationNode* aParent )
    {
    FUNC_LOG;

    CCFOrOperation* self = new( ELeave ) CCFOrOperation( aServices, aParent );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::ParseL
// Construction with parsing from a DOM node.
// -----------------------------------------------------------------------------
//
CCFOrOperation* CCFOrOperation::ParseL( MCFOperationServices& aServices,
    CCFOperationNode* aParent,
    CMDXMLNode& aNode )
    {
    FUNC_LOG;

    if ( aNode.NodeName().CompareF( KScriptOrName ) != 0 )
        {
        return NULL; // Cannot create and operation from the given node.
        }

    CCFOrOperation* self = NewLC( aServices, aParent );     // CLEANUP<< self

    TBool unsupportedNode( EFalse );
    CMDXMLNode* child = aNode.FirstChild();
    while ( child )
        {
        if ( child->NodeType() == CMDXMLNode::EElementNode )
            {
            CCFOperationNode* op = self->iServices.ParseL( self, *child );
            if ( op )
                {
                self->iChildren.AppendL( op );
                }
            else
                {
                unsupportedNode = ETrue;
                break;
                }
            }
        else if ( child->NodeType() != CMDXMLNode::ECommentNode )
            {
            unsupportedNode = ETrue;
            break;
            }
        child = child->NextSibling();
        }

    CleanupStack::Pop( self );                              // CLEANUP<< self
    if ( unsupportedNode || !self->iChildren.Count() )
        {
        delete self; // Discard unsupported or operation.
        self = NULL;

        if ( unsupportedNode )
            {
            TPtrC nodeName( child->NodeName() );
            INFO_1( "CCFOrOperation::ParseL - Unsupported node [%S]",
                    &nodeName );
            }
        else
            {
            INFO( "CCFOrOperation::ParseL - No children" );
            }
        }

    CREATE_DOM_INFO( self, aNode );

    return self;
    }


// Destructor
CCFOrOperation::~CCFOrOperation()
    {
    FUNC_LOG;

    iChildren.ResetAndDestroy();
    }


// ---------------------------------------------------------------------------
// CCFOrOperation::ActivateL
// ---------------------------------------------------------------------------
//
void CCFOrOperation::ActivateL()
    {
    FUNC_LOG;

    for ( TInt i = 0; i < iChildren.Count(); ++i )
        {
        iChildren[ i ]->ActivateL();
        }
    }

// ---------------------------------------------------------------------------
// CCFOrOperation::Deactivate
// ---------------------------------------------------------------------------
//
void CCFOrOperation::Deactivate()
    {
    FUNC_LOG;

    for ( TInt i = 0; i < iChildren.Count(); ++i )
        {
        iChildren[ i ]->Deactivate();
        }
    }

// ---------------------------------------------------------------------------
// CCFOrOperation::CheckSecurity
// ---------------------------------------------------------------------------
//
TInt CCFOrOperation::CheckSecurity()
    {
    FUNC_LOG;

    for ( TInt i = 0; i < iChildren.Count(); ++i )
        {
        TInt err = iChildren[ i ]->CheckSecurity();
        if ( err != KErrNone )
            {
            return err;
            }
        }

    return KErrNone;
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::Evaluate
// -----------------------------------------------------------------------------
//
void CCFOrOperation::Evaluate()
    {
    FUNC_LOG;

    DOM_INFO( "CCFOrOperation::Evaluate" );

    TCFConditionValue newValue = Value();
    TBool branchUndefined = EFalse;
    for ( TInt i = 0; i < iChildren.Count(); ++i )
        {
        newValue = iChildren[ i ]->Value();
        if ( newValue == ECFConditionTrue )
            {
            break;
            }
        else if ( newValue == ECFConditionUndefined )
            {
            branchUndefined = ETrue;
            }
        }

    if ( branchUndefined && newValue == ECFConditionFalse )
        {
        newValue = ECFConditionUndefined;
        }

    if ( newValue != Value() )
        {
        INFO_1( "CCFOrOperation::Evaluate - Value changed to (-1=undefined, 0=false, 1=true): %d", newValue );

        iValue = newValue;
        if ( iParent )
            {
            iParent->Evaluate();
            }
        }
    else
        {
        INFO_1( "CCFOrOperation::Evaluate - Value still (-1=undefined, 0=false, 1=true): %d", newValue );
        }
    }

// -----------------------------------------------------------------------------
// CCFOrOperation::Cleanup
// -----------------------------------------------------------------------------
//
void CCFOrOperation::Cleanup()
    {
    FUNC_LOG;

    for ( TInt i = 0; i < iChildren.Count(); ++i )
        {
        iChildren[ i ]->Cleanup();
        }
    }
