// DBGPREF.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eikenv.h>
#include <eikdialg.h>
#include <eikappui.h>
#include <eikapp.h>
#include <eikdoc.h>
#include <eikdebug.h>
#include <eikchkbx.h>
#include <eikdialg.hrh>
#include <eikcmds.hrh>
#include <eikon.rsg>

#include "dbgpref.hrh"
#include <dbgpref.rsg>

//
// class CDebugKeysDlg
//

class CDebugKeysDlg : public CEikDialog
    {
public:
    CDebugKeysDlg();
	~CDebugKeysDlg() { User::Exit(KErrNone); }
private: // from CEikDialog
    TBool OkToExitL(TInt aKeycode);
	void PreLayoutDynInitL();
    };

CDebugKeysDlg::CDebugKeysDlg()
	{
	}

void CDebugKeysDlg::PreLayoutDynInitL()
	{
	CEikDebugPreferences* debugPrefs=CEikDebugPreferences::NewLC();
	debugPrefs->RestoreL();
	TBool keysOn=debugPrefs->KeysOn();
	CleanupStack::PopAndDestroy();	// debugPrefs
	
	SetCheckBoxState(EAppCheckBoxId, keysOn ? CEikButtonBase::ESet: CEikButtonBase::EClear);
	}

TBool CDebugKeysDlg::OkToExitL(TInt /*aKeycode*/)
    {
	TBool keysOn;

	switch(CheckBoxState(EAppCheckBoxId))
		{
	case CEikButtonBase::ESet:
		keysOn=ETrue;
		break;
	case CEikButtonBase::EClear:
	default:
		keysOn=EFalse;
		break;
		};

	CEikDebugPreferences* debugPrefs=CEikDebugPreferences::NewLC();
	debugPrefs->SetKeysOn(keysOn);
	debugPrefs->StoreL();
	CleanupStack::PopAndDestroy();	// debugPrefs

    return(ETrue);
    }

//
// class CDebugKeysAppUi
//

class CDebugKeysAppUi : public CEikAppUi
    {
public:
    void ConstructL();
    ~CDebugKeysAppUi();
private: // framework
    void HandleCommandL(TInt aCommand);
    };

void CDebugKeysAppUi::ConstructL()
    {
    BaseConstructL();
#if defined(_DEBUG)
	iEikonEnv->InfoMsg(R_DBGPREF_ALL_ON);
#else
	CEikDialog* dialog=new(ELeave) CDebugKeysDlg();
	dialog->ExecuteLD(R_DEBUG_KEYS_DIALOG);
#endif
    }

CDebugKeysAppUi::~CDebugKeysAppUi()
    {
    }

void CDebugKeysAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
    case EEikCmdExit:
        Exit();
        break;
        }
    }

//
// CSimpleDocument
//

class CSimpleDocument : public CEikDocument
	{
public:
	CSimpleDocument(CEikApplication& aApp): CEikDocument(aApp) { }
private: // from CApaDocument
	CEikAppUi* CreateAppUiL();
	};

CEikAppUi* CSimpleDocument::CreateAppUiL()
	{
    return(new(ELeave) CDebugKeysAppUi);
	}

//
// CSimpleApplication
//

class CSimpleApplication : public CEikApplication
	{
private: // from CApaApplication
	CApaDocument* CreateDocumentL();
	TUid AppDllUid() const;
	};

const TUid KUidSimpleApp={350};

TUid CSimpleApplication::AppDllUid() const
	{
	return(KUidSimpleApp);
	}

CApaDocument* CSimpleApplication::CreateDocumentL()
	{
	return(new(ELeave) CSimpleDocument(*this));
	}

//
// EXPORTed functions
//

EXPORT_C CApaApplication* NewApplication()
	{
	return(new CSimpleApplication);
	}

GLDEF_C TInt E32Dll(TDllReason)
	{
	return(KErrNone);
	}
