// TEIKIRDA.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <basched.h>
#include <eikenv.h>
#include <coecntrl.h>
#include <eikappui.h>
#include <eikdef.h>
#include <eikcmds.hrh>
#include <eikapp.h>
#include <eikdoc.h>
#include <eikirda.h>
#include <eikfbrow.h>
#include <eikdialg.hrh>

#include "teikirda.hrh"
#include <teikirda.rsg>
#include <eikon.rsg>

//
// class CTestContainer
//

class CTestContainer : public CCoeControl, public MCoeControlObserver
    {
public:
    void ConstructL();
    ~CTestContainer();
private: // framework
	virtual void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);
	virtual TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
    virtual void Draw(const TRect& aRect) const;
    virtual TInt CountComponentControls() const;
    virtual CCoeControl* ComponentControl(TInt aIndex) const;
private:
    };

void CTestContainer::ConstructL()
    {
    CreateWindowL();
    Window().SetShadowDisabled(ETrue);
    Window().SetBackgroundColor(KRgbGray);
    EnableDragEvents();
    SetExtentToWholeScreenL();
    ActivateL();
    }

CTestContainer::~CTestContainer()
    {
    }

TKeyResponse CTestContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    if (aType!=EEventKey)
        return(EKeyWasNotConsumed);
	for (TInt i=0; i<CountComponentControls();i++)
		{
		TKeyResponse key=ComponentControl(i)->OfferKeyEventL(aKeyEvent, aType);
		if (key==EKeyWasConsumed)
			return key;
		}
	return(EKeyWasNotConsumed);
    }


void CTestContainer::HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    }

TInt CTestContainer::CountComponentControls() const
    {
    return(0);
    }

CCoeControl* CTestContainer::ComponentControl(TInt /*aIndex*/) const
    {
	return NULL;
    }

void CTestContainer::Draw(const TRect& /*aRect*/) const
    {
    iEikonEnv->FillTexturedRect(Rect());
    }

//
// CButAppUi
//

class CButAppUi : public CEikAppUi
    {
public:
    ~CButAppUi();
    void ConstructL();
private: // framework
	virtual void HandleCommandL(TInt aCommand);
private:
	void CmdIrSend100KBufferL();
	void CmdIrReceiveDataL();
	void CmdIrSendFileL();
	void CmdIrReceiveFileL();
	void CmdIrSendFileIncDirL();
	void CmdIrReceiveFileRemSpecDirL();
	void CmdIrSendMultiFileL();
	void CmdIrReceiveMultiFileL();
private:
    CTestContainer* iContainer;
	CBufFlat* iBuf;
    };

CButAppUi::~CButAppUi()
    {
    delete iContainer;
	delete iBuf;
    }

void CButAppUi::ConstructL()
    {
	BaseConstructL();
    iContainer=new(ELeave) CTestContainer;
    iContainer->ConstructL();
    AddToStackL(iContainer);
    }

void CButAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
	case ETEikIrdaCmdIrSend100KBuffer:
		CmdIrSend100KBufferL();
		break;
	case ETEikIrdaCmdIrSendFile:
		CmdIrSendFileL();
		break;
	case ETEikIrdaCmdIrSendFileIncDir:
		CmdIrSendFileIncDirL();
		break;
	case ETEikIrdaCmdIrSendMultiFile:
		CmdIrSendMultiFileL();
		break;
	case ETEikIrdaCmdIrReceiveData:
		CmdIrReceiveDataL();
		break;
	case ETEikIrdaCmdIrReceiveFile:
		CmdIrReceiveFileL();
		break;
	case ETEikIrdaCmdIrReceiveFileRemSpecDir:
		CmdIrReceiveFileRemSpecDirL();
		break;
	case ETEikIrdaCmdIrReceiveMultiFile:
		CmdIrReceiveMultiFileL();
		break;
	case EEikCmdExit:
		CBaActiveScheduler::Exit();
	default:
		break;
		}
	}

void CButAppUi::CmdIrSend100KBufferL()
	{
	delete iBuf;
	iBuf=NULL;
	iBuf=CBufFlat::NewL(16);
	TUint size=0x19000;	// 100Kb
	iBuf->ResizeL(size); 
	TPtrC8 ptr=_L8("*");
	TPtrC8* temp=&ptr;
	for (TUint i=0; i<size; i++)
		iBuf->Write(i, *temp, 1);
	CEikIrDataSender* sender=CEikIrDataSender::NewL();
	sender->SendLD(iBuf);
	}

void CButAppUi::CmdIrReceiveDataL()
	{
	delete iBuf;
	iBuf=NULL;
	iBuf=CBufFlat::NewL(16);
	CEikIrDataReceiver* receiver=CEikIrDataReceiver::NewL();
	receiver->ReceiveLD(iBuf);
	}

void CButAppUi::CmdIrSendFileL()
	{
	TFullName path=_L("c:\\*");
	CDirContentsListBoxModel::TSortOrder order=CDirContentsListBoxModel::EOrderByName;
	CEikFileBrowserDialog* browserDialog=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	TParse parse;
	parse.Set(path, NULL, NULL);
	if (!(parse.NamePresent()))
		{
		iEikonEnv->InfoMsg(_L("You must specify a file to send"));
		return;
		}
	CEikIrFileSender* sender=CEikIrFileSender::NewL();
	sender->SendLD(path);
	}

void CButAppUi::CmdIrReceiveFileL()
	{
	TFullName path=_L("c:\\*");
	CDirContentsListBoxModel::TSortOrder order=CDirContentsListBoxModel::EOrderByName;
	CEikFileBrowserDialog* browserDialog=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	TParse parse;
	parse.Set(path, NULL, NULL);
	if (parse.NamePresent())
		{
		iEikonEnv->InfoMsg(_L("You must specify only a directory to receive into"));
		return;
		}
	CEikIrFileReceiver* receiver=CEikIrFileReceiver::NewL();
	receiver->ReceiveLD(path);
	}

void CButAppUi::CmdIrSendFileIncDirL()
	{
	TFullName path=_L("c:\\*");
	CDirContentsListBoxModel::TSortOrder order=CDirContentsListBoxModel::EOrderByName;
	CEikFileBrowserDialog* browserDialog=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	TParse parse;
	parse.Set(path, NULL, NULL);
	if (!(parse.NamePresent()))
		{
		iEikonEnv->InfoMsg(_L("You must specify a file to send"));
		return;
		}
	CEikDialog* dialog=new(ELeave) CEikDialog;
	dialog->ConstructAutoDialogLC(EEikDialogFlagWait,R_EIK_BUTTONS_CANCEL_OK);
	dialog->SetTitleL(_L("Destination directory"));
	dialog->AddAutoTextEditorL(_L("Dest dir"),103,0,20,1,&path);
	if (!dialog->RunLD())
		return;
	CEikIrFileSender* sender=CEikIrFileSender::NewL();
	sender->SendLD(parse.FullName(), &path);
	}

void CButAppUi::CmdIrReceiveFileRemSpecDirL()
	{
	CEikIrFileReceiver* receiver=CEikIrFileReceiver::NewL();
	receiver->ReceiveLD();
	}

void CButAppUi::CmdIrSendMultiFileL()
	{
	TFullName path=_L("c:\\*");
	CDirContentsListBoxModel::TSortOrder order=CDirContentsListBoxModel::EOrderByName;
	CEikFileBrowserDialog* browserDialog=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	TParse parse;
	parse.Set(path, NULL, NULL);
	if (!(parse.NamePresent()))
		{
		iEikonEnv->InfoMsg(_L("You must specify a file to send"));
		return;
		}

	CDesCArraySeg* fileNameArray=new(ELeave) CDesCArraySeg(4);
	CleanupStack::PushL(fileNameArray);
	fileNameArray->InsertL(0,path);

	CEikFileBrowserDialog* browserDialog2=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog2->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	parse.Set(path, NULL, NULL);
	if (!(parse.NamePresent()))
		{
		iEikonEnv->InfoMsg(_L("You must specify a file to send"));
		return;
		}
	fileNameArray->InsertL(1,path);
	CEikIrMultipleFileSender* sender=CEikIrMultipleFileSender::NewL();
	CleanupStack::Pop(); // fileNameArray
	TInt editLength = parse.DriveAndPath().Length();
	sender->SendLD(fileNameArray,editLength);
	}

void CButAppUi::CmdIrReceiveMultiFileL()
	{
	TFullName path=_L("c:\\*");
	CDirContentsListBoxModel::TSortOrder order=CDirContentsListBoxModel::EOrderByName;
	CEikFileBrowserDialog* browserDialog=new(ELeave) CEikFileBrowserDialog(path,0,order);
	if (!(browserDialog->ExecuteLD(R_EIK_DIALOG_FILE_BROWSE)))
		return;	
	TParse parse;
	parse.Set(path, NULL, NULL);
	if (parse.NamePresent())
		{
		iEikonEnv->InfoMsg(_L("You must specify only a directory to receive into"));
		return;
		}
	CEikIrMultipleFileReceiver* receiver=CEikIrMultipleFileReceiver::NewL();
	receiver->ReceiveLD(path);
	}

//
// CSimpleDocument
//

class CSimpleDocument : public CEikDocument
	{
public:
	CSimpleDocument(CEikApplication& aApp): CEikDocument(aApp) { }
private: // from CApaDocument
	CEikAppUi* CreateAppUiL();
	};

CEikAppUi* CSimpleDocument::CreateAppUiL()
	{
    return(new(ELeave) CButAppUi);
	}

//
// CSimpleApplication
//

class CSimpleApplication : public CEikApplication
	{
private: // from CApaApplication
	CApaDocument* CreateDocumentL();
	TUid AppDllUid() const;
	};

CApaDocument* CSimpleApplication::CreateDocumentL()
	{
	return(new(ELeave) CSimpleDocument(*this));
	}

const TUid KUidSimpleApp={240};

TUid CSimpleApplication::AppDllUid() const
	{
	return(KUidSimpleApp);
	}

//
// EXPORTed functions
//

EXPORT_C CApaApplication* NewApplication()
	{
	return(new CSimpleApplication);
	}

GLDEF_C TInt E32Dll(TDllReason)
	{
	return(KErrNone);
	}
