/*
* Copyright (c) 2009 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: Item action menu register array.
*
*/
#include <akncollection.h>
#include <aknitemactionmenu.h>
#include <eikmenub.h>

#include "aknitemactionmenuregisterarray.h"
#include "akntrace.h"

// ---------------------------------------------------------------------------
// CCAknItemActionMenuRegisterArray::NewL
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray* CAknItemActionMenuRegisterArray::NewL()
    {
    _AKNTRACE_FUNC_ENTER;
    CAknItemActionMenuRegisterArray* self =
        CAknItemActionMenuRegisterArray::NewLC();
    CleanupStack::Pop( self );
    _AKNTRACE_FUNC_EXIT;
    return self;
    }


// ---------------------------------------------------------------------------
// CCAknItemActionMenuRegisterArray::NewLC
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray* CAknItemActionMenuRegisterArray::NewLC()
    {
    _AKNTRACE_FUNC_ENTER;
    CAknItemActionMenuRegisterArray* self =
        new ( ELeave ) CAknItemActionMenuRegisterArray();
    CleanupStack::PushL( self );
    _AKNTRACE_FUNC_EXIT;
    return self;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::~CAknItemActionMenuRegisterArray
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::~CAknItemActionMenuRegisterArray()
    {
    _AKNTRACE_FUNC_ENTER;
    iRegisterArray.ResetAndDestroy();
    iRegisterArray.Close();
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::AddEntryL
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::AddEntryL(
        CEikMenuBar& aMenuBar, CAknItemActionMenu& aItemActionMenu )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* newEntry = CRegisterEntry::NewLC(
            aMenuBar, aItemActionMenu );
    iRegisterArray.AppendL( newEntry );
    CleanupStack::Pop( newEntry );
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::UnregisterMenuBar
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::UnregisterMenuBar(
        CEikMenuBar& aMenuBar )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* entry( NULL );
    for ( TInt i = 0; i < iRegisterArray.Count(); i++ )
        {
        entry = iRegisterArray[ i ];
        if ( entry->MenuBar() == &aMenuBar )
            {
            entry->RemoveMenuBar();
            // Remove entry if there are no more collections
            if ( entry->ItemActionMenu()->CollectionCount() )
                {
                entry->ItemActionMenu()->SetMenuBar( NULL, NULL );
                }
            else
                {
                RemoveEntry( i );
                }
            break;
            }
        }
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::UnregisterItemActionMenu
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::UnregisterItemActionMenu(
        CAknItemActionMenu& aItemActionMenu )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* entry( NULL );

    for ( TInt i = 0; i < iRegisterArray.Count(); i++ )
        {
        entry = iRegisterArray[ i ];
        if ( entry->ItemActionMenu() == &aItemActionMenu )
            {
            // Remove entry only if menu bar is already removed
            if ( !entry->MenuBar() )
                {
                RemoveEntry( i );
                }
            break;
            }
        }
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::RegisterCollectionObserverL
// ---------------------------------------------------------------------------
//
TBool CAknItemActionMenuRegisterArray::RegisterCollectionObserverL(
        CEikMenuBar& aMenuBar, MAknCollectionObserver& aObserver )
    {
    _AKNTRACE_FUNC_ENTER;
    TBool menuBarFound( EFalse );
    CRegisterEntry* entry( NULL );
    for ( TInt i = 0; i < iRegisterArray.Count(); i++ )
        {
        entry = iRegisterArray[ i ];
        if ( entry->MenuBar() == &aMenuBar )
            {
            if ( entry->ItemActionMenu() )
                {
                entry->ItemActionMenu()->RegisterCollectionObserverL(
                        aObserver );
                }
            menuBarFound = ETrue;
            break;
            }
        }
    _AKNTRACE_FUNC_EXIT;
    return menuBarFound;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::UnregisterCollectionObserver
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::UnregisterCollectionObserver(
        MAknCollectionObserver& aObserver )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* entry( NULL );

    for ( TInt i = 0; i < iRegisterArray.Count(); i++ )
        {
        entry = iRegisterArray[ i ];
        if ( entry->ItemActionMenu() )
            {
            entry->ItemActionMenu()->RemoveCollectionObserver( aObserver );
            }
        }
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CAknItemActionMenuRegisterArray
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::CAknItemActionMenuRegisterArray()
    {
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::ConstructL
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::ConstructL()
    {
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::RemoveEntry
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::RemoveEntry( TInt aIndex )
    {
    _AKNTRACE_FUNC_ENTER;
    if ( aIndex >= 0 && aIndex < iRegisterArray.Count() )
        {
        CRegisterEntry* entry = iRegisterArray[ aIndex ];
        delete entry;
        entry = NULL;
        iRegisterArray.Remove( aIndex );
        }
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CCAknItemActionMenuRegisterArray::CRegisterEntry::NewL
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::CRegisterEntry*
    CAknItemActionMenuRegisterArray::CRegisterEntry::NewL(
            CEikMenuBar& aMenuBar, CAknItemActionMenu& aItemActionMenu )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* self = CRegisterEntry::NewLC( aMenuBar, aItemActionMenu );
    CleanupStack::Pop( self );
    _AKNTRACE_FUNC_EXIT;
    return self;
    }


// ---------------------------------------------------------------------------
// CCAknItemActionMenuRegisterArray::CRegisterEntry::NewLC
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::CRegisterEntry*
    CAknItemActionMenuRegisterArray::CRegisterEntry::NewLC(
            CEikMenuBar& aMenuBar, CAknItemActionMenu& aItemActionMenu )
    {
    _AKNTRACE_FUNC_ENTER;
    CRegisterEntry* self =
        new ( ELeave ) CRegisterEntry( aMenuBar, aItemActionMenu );
    CleanupStack::PushL( self );
    _AKNTRACE_FUNC_EXIT;
    return self;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::~CRegisterEntry
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::CRegisterEntry::~CRegisterEntry()
    {
    _AKNTRACE_FUNC_ENTER;
    delete iActionMenu;
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::RemoveMenuBar
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::CRegisterEntry::RemoveMenuBar()
    {
    _AKNTRACE_FUNC_ENTER;
    iMenuBar = NULL;
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::MenuBar
// ---------------------------------------------------------------------------
//
CEikMenuBar* CAknItemActionMenuRegisterArray::CRegisterEntry::MenuBar()
    {
    _AKNTRACE_FUNC_ENTER;
    _AKNTRACE_FUNC_EXIT;
    return iMenuBar;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::RemoveItemActionMenu
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::CRegisterEntry::RemoveItemActionMenu()
    {
    _AKNTRACE_FUNC_ENTER;
    delete iActionMenu;
    iActionMenu = NULL;
    _AKNTRACE_FUNC_EXIT;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::ItemActionMenu
// ---------------------------------------------------------------------------
//
CAknItemActionMenu*
    CAknItemActionMenuRegisterArray::CRegisterEntry::ItemActionMenu()
    {
    _AKNTRACE_FUNC_ENTER;
    _AKNTRACE_FUNC_EXIT;
    return iActionMenu;
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::CRegisterEntry
// ---------------------------------------------------------------------------
//
CAknItemActionMenuRegisterArray::CRegisterEntry::CRegisterEntry(
        CEikMenuBar& aMenuBar, CAknItemActionMenu& aItemActionMenu )
    : iMenuBar( &aMenuBar ),
    iActionMenu( &aItemActionMenu )
    {
    }


// ---------------------------------------------------------------------------
// CAknItemActionMenuRegisterArray::CRegisterEntry::ConstructL
// ---------------------------------------------------------------------------
//
void CAknItemActionMenuRegisterArray::CRegisterEntry::ConstructL()
    {
    }

// End of File
