/*
========================================================================
 Name        : AudioPlayerContainerView.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <barsread.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <akncontext.h>
#include <akntitle.h>
#include <aknnavide.h>
#include <aknnavi.h>
#include <AknVolumeControl.h>
#include <avkon.rsg>	
#include <eikbtgpc.h>
// ]]] end generated region [Generated System Includes]
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <AudioPlayer.rsg>
#include <f32file.h>
#include <s32file.h>
#include <coecntrl.h>
#include "AudioPlayer.pan"
#include "AudioPlayerAppUi.h"

// [[[ begin generated region: do not modify [Generated User Includes]
#include "AudioPlayer.hrh"
#include "AudioPlayerContainerView.h"
#include "AudioPlayerContainer.hrh"
#include "AudioPlayerContainer.h"
// ]]] end generated region [Generated User Includes]
#include <Akncommondialogs.h>
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]
_LIT(KTitle,"Dosya Secin");
_LIT(KPlay,"Caliyor");
_LIT(KPause,"Duraklatildi");
_LIT(KStop,"Durduruldu");
_LIT( KText, "Ses Oynatici");

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */


CAudioPlayerContainerView::CAudioPlayerContainerView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iNaviDecorator_ = NULL;
	iAudioPlayerContainer = NULL;
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CAudioPlayerContainerView::~CAudioPlayerContainerView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	if ( iNaviDecorator_ != NULL )
		{
		delete iNaviDecorator_;
		iNaviDecorator_ = NULL;
		}
	delete iAudioPlayerContainer;
	iAudioPlayerContainer = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CAudioPlayerContainerView
 */
CAudioPlayerContainerView* CAudioPlayerContainerView::NewL()
	{
	CAudioPlayerContainerView* self = CAudioPlayerContainerView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CAudioPlayerContainerView
 */
CAudioPlayerContainerView* CAudioPlayerContainerView::NewLC()
	{
	CAudioPlayerContainerView* self = new ( ELeave ) CAudioPlayerContainerView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CAudioPlayerContainerView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_AUDIO_PLAYER_CONTAINER_AUDIO_PLAYER_CONTAINER_VIEW );
	// ]]] end generated region [Generated Code]
	// add your own initialization code here
	}
	
/**
 * @return The UID for this view
 */
TUid CAudioPlayerContainerView::Id() const
	{
	return TUid::Uid( EAudioPlayerContainerViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CAudioPlayerContainerView::HandleCommandL( TInt aCommand )
	{   
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case EAudioPlayerContainerViewSelectMenuItemCommand:
			commandHandled = HandleSelectMenuItemSelectedL( aCommand );
			break;
		case EAudioPlayerContainerViewPlayMenuItemCommand:
			commandHandled = HandlePlayMenuItemSelectedL( aCommand );
			break;
		case EAudioPlayerContainerViewPauseMenuItemCommand:
			commandHandled = HandlePauseMenuItemSelectedL( aCommand );
			break;
		case EAudioPlayerContainerViewStopMenuItemCommand:
			commandHandled = HandleStopMenuItemSelectedL( aCommand );
			break;
		case EAudioPlayerContainerViewEfektMenuItemCommand:
			commandHandled = HandleEfektMenuItemSelectedL( aCommand );
			break;
		case EAudioPlayerContainerViewDisableMenuItemCommand:
			commandHandled = HandleDisableMenuItemSelectedL( aCommand );
			break;
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
	
		if ( aCommand == EAknSoftkeyExit )
			{
			AppUi()->HandleCommandL( EEikCmdExit );
			}
	
		}
	// ]]] end generated region [Generated Code]
	
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CAudioPlayerContainerView::DoActivateL(
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
	CEikButtonGroupContainer* cba = AppUi()->Cba();
	if ( cba != NULL ) 
		{
		cba->MakeVisible( EFalse );
		}
	
	if ( iAudioPlayerContainer == NULL )
		{
		iAudioPlayerContainer = CAudioPlayerContainer::NewL( ClientRect(), NULL, this );
		iAudioPlayerContainer->SetMopParent( this );
		AppUi()->AddToStackL( *this, iAudioPlayerContainer );
		} 
	// ]]] end generated region [Generated Contents]
	
	}

/**
 */
void CAudioPlayerContainerView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	CEikButtonGroupContainer* cba = AppUi()->Cba();
	if ( cba != NULL ) 
		{
		cba->MakeVisible( ETrue );
		cba->DrawDeferred();
		}
	
	if ( iAudioPlayerContainer != NULL )
		{
		AppUi()->RemoveFromViewStack( *this, iAudioPlayerContainer );
		delete iAudioPlayerContainer;
		iAudioPlayerContainer = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

// [[[ begin generated function: do not modify
void CAudioPlayerContainerView::SetupStatusPaneL()
	{
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_AUDIO_PLAYER_CONTAINER_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	// setup the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
			StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context1 = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		TResourceReader reader;
		iCoeEnv->CreateResourceReaderLC( reader, R_AUDIO_PLAYER_CONTAINER_CONTEXT1_RESOURCE );
		context1->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	// set the navi pane content
	TUid naviPaneUid = TUid::Uid( EEikStatusPaneUidNavi );
	CEikStatusPaneBase::TPaneCapabilities subPaneNavi = 
		StatusPane()->PaneCapabilities( naviPaneUid );
	if ( subPaneNavi.IsPresent() && subPaneNavi.IsAppOwned() )
		{
		CAknNavigationControlContainer* naviPane = 
			static_cast< CAknNavigationControlContainer* >( 
				StatusPane()->ControlL( naviPaneUid ) );
		if ( iNaviDecorator_ != NULL )
			{
			delete iNaviDecorator_;
			iNaviDecorator_ = NULL;
			}
				
		iNaviDecorator_ = naviPane->CreateVolumeIndicatorL( R_AVKON_NAVI_PANE_VOLUME_INDICATOR ); 
		static_cast<CAknVolumeControl*>( iNaviDecorator_->DecoratedControl() )->SetValue( 5 );
				
		naviPane->PushL( *iNaviDecorator_ );
		}
				
	}
// ]]] end generated function

// [[[ begin generated function: do not modify
void CAudioPlayerContainerView::CleanupStatusPane()
	{
	// reset the navi pane 
	TUid naviPaneUid = TUid::Uid( EEikStatusPaneUidNavi );
	CEikStatusPaneBase::TPaneCapabilities subPaneNavi = 
		StatusPane()->PaneCapabilities( naviPaneUid );
	if ( subPaneNavi.IsPresent() && subPaneNavi.IsAppOwned() )
		{
		CAknNavigationControlContainer* naviPane = 
			static_cast< CAknNavigationControlContainer* >( 
				StatusPane()->ControlL( naviPaneUid ) );
		if ( iNaviDecorator_ != NULL )
			{
			naviPane->Pop( iNaviDecorator_ );
			delete iNaviDecorator_;
			iNaviDecorator_ = NULL;
			}
		}
	
	}
// ]]] end generated function

/** 
 * Handle status pane size change for this view (override)
 */
void CAudioPlayerContainerView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TInt result;
	TRAP( result, SetupStatusPaneL() ); 
	}
	
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandleSelectMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch (aCommand)
	{
	case EAudioPlayerContainerViewSelectMenuItemCommand:
	PlayFileL();
	
	break;
	}
	return ETrue;
	}

void CAudioPlayerContainerView::PlayFileL()
	{
	    
		TFileName file(KNullDesC);
	    iAudioEngine = CAudioPlayerEngine::NewL();
	    
	 
	    if (AknCommonDialogs::RunSelectDlgLD(file, R_FILE_SELECT_DIALOG, KTitle))
	    {
			iAudioEngine->PlayFileL(file);			
			iAudioPlayerContainer->UpdateLabelL(file);
	    }
	    
	    
	    
	    
	//	_LIT(KSoundFile, "C:\\Frank Sinatra - My Way.mp3");
	//	iAudioEngine->PlayFileL(KSoundFile);
	}
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandlePlayMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch(aCommand)
	{
	case EAudioPlayerContainerViewPlayMenuItemCommand:
	iAudioEngine->PlayL();
	iAudioPlayerContainer->UpdateNewLabelL(KPlay);
	
	break;
	}
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandlePauseMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch(aCommand)
	{
	case EAudioPlayerContainerViewPauseMenuItemCommand:
	iAudioEngine->PauseL();
	iAudioPlayerContainer->UpdateNewLabelL(KPause);
	
	break;
	}
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandleStopMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch(aCommand)
	{
	case EAudioPlayerContainerViewStopMenuItemCommand:
	iAudioEngine->StopFileL();
	iAudioPlayerContainer->UpdateNewLabelL(KStop);
	break;
	}
	return ETrue;
	}
			

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */

				

void CAudioPlayerContainerView::TogglePlayPause()
{
	iAudioEngine->PlayPauseL();
	
}
		
void CAudioPlayerContainerView::VolumeDown()
{
	if(iVolume>1)
		{
		--iVolume;
		static_cast<CAknVolumeControl*>( iNaviDecorator_->DecoratedControl() )->SetValue( iVolume );
		}
		else if ( !iMute )
        	{
            iMute = ETrue;
            iVolume = 0;
			
        	}
		
     	iAudioEngine->SetVolume(iVolume);
     	
		}


void CAudioPlayerContainerView::VolumeUp()
{
	if(iMute)
		{
		 iMute = EFalse;
		}
	if(iVolume<10)
		{
		++iVolume;
		static_cast<CAknVolumeControl*>( iNaviDecorator_->DecoratedControl() )->SetValue( iVolume );
		}
	iAudioEngine->SetVolume(iVolume);
}


void CAudioPlayerContainerView::EnableLoudness()
{
	
    
	iLoudnessEffect->EnableEffect();
	iBassEffect->EnableEffect();
	iStereoEffect->EnableEffect();
}

void CAudioPlayerContainerView::DisableLoudness()
	{
	iLoudnessEffect->DisableEffect();
	iBassEffect->DisableEffect();
	iStereoEffect->DisableEffect();
	}
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandleEfektMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch(aCommand)
	{
	case EAudioPlayerContainerViewEfektMenuItemCommand:
	EnableLoudness();
	break;
	}
	return ETrue;
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CAudioPlayerContainerView::HandleDisableMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	switch(aCommand)
	{
	case EAudioPlayerContainerViewDisableMenuItemCommand:
	DisableLoudness();
	break;
	}
	return ETrue;
	}
				
