//---------------------------------------------------------------------------
#include <vcl.h>
#include <dir.h>
#include <errno.h>
#include <stdio.h>

#include "FDLGSelDir.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDLGSelDir *DLGSelDir;
//---------------------------------------------------------------------------
AnsiString CurrentDirectory;
//---------------------------------------------------------------------------
bool dlg_bCreateDirectory(AnsiString _asBase)
{
    AnsiString NewDir = InputBox("Create directory:",
                                 "Enter the name of the directory you want to create:",
                                 "NewDir");

    NewDir = _asBase +"\\"+ NewDir;

    if (mkdir(NewDir.c_str())==0)
        return(true);
    else
    {
        switch(errno)
        {
            case EACCES :ShowMessage("Unable to create the directory:\r\n"
                                     "Permission denied.");
                        break;
            case ENOENT :ShowMessage("Unable to create the directory:\r\n"
                                     "No such directory.");
                        break;
            default:     ShowMessage("Unable to create the directory.");
        }
    }

    return(false);
}
//---------------------------------------------------------------------------
bool dlg_bGenericRename(AnsiString _asFilename,bool _bDirectory)
{
	AnsiString asText = _bDirectory?"directory" : "file";

    AnsiString asMsgBoxText = "Are you sure you want to rename the current " + asText + "?";
    if (Application->MessageBox(asMsgBoxText.c_str(),"Warning:",MB_YESNO) == IDYES)
    {
        AnsiString NewName = InputBox("Rename "+asText +":",
                                      "Enter the new name you want to give to the current " + asText + ":",
                                      _asFilename);

        if (_asFilename == NewName)
        	return(false);

        if (! rename(_asFilename.c_str(),NewName.c_str()) )
            return (true);
        else
        {
            switch(errno)
            {
                case EEXIST :ShowMessage("Unable to rename the " + asText + ":\r\n"
                                         "Another file uses the same name.");
            	break;

                case ENOENT :ShowMessage("Unable to rename the " + asText + "\r\n"
                                         "Unable to find source " + asText + " name.");
            	break;

                case ENOTSAM:ShowMessage("Unable to rename the " + asText + ":\r\n"
                                         "Source and destination names are in different folders.");
            	break;

                default:     ShowMessage("Unable to rename the " + asText + ".");

            }
        }
    }

    return(false);
}
//---------------------------------------------------------------------------
bool dlg_bRenameDirectory(AnsiString _asDirectory)
{
	return( dlg_bGenericRename(_asDirectory,true) );
}
//---------------------------------------------------------------------------
bool dlg_bRenameFile(AnsiString _asFilename)
{
	return( dlg_bGenericRename(_asFilename,false) );
}
//---------------------------------------------------------------------------
bool dlg_bRemoveDirectory(AnsiString _asDirectory)
{
    if (Application->MessageBox("Are you sure you want to remove the current directory?","Warning:",MB_YESNO) == IDYES)
    {
        if (rmdir( _asDirectory.c_str()) == 0)
        	return(true);
        else
        {
            switch(errno)
            {
                case EACCES :ShowMessage("Unable to remove the directory:\r\n"
                                         "Permission denied.");
            	break;

                case ENOENT :ShowMessage("Unable to remove the directory:\r\n"
                                         "Path not found");
                break;

                default:     ShowMessage("Unable to remove the directory.");
            }
        }
    }

    return(false);
}
//---------------------------------------------------------------------------
bool dlg_bSelectDirectory(AnsiString _oTitle,AnsiString& _roDirectory)
{
    Application->CreateForm(__classid(TDLGSelDir), &DLGSelDir);

    DLGSelDir->Caption = _oTitle;
    try
    {
    	DLGSelDir->dbox_Dir->Directory = _roDirectory;
    }
    catch (...)
    {
    }


    DLGSelDir->ShowModal();

    if (DLGSelDir->ModalResult == mrOk)
    {
        _roDirectory = DLGSelDir->dbox_Dir->Directory;
        delete DLGSelDir;
        return(true);
    }
    else
    {
        delete DLGSelDir;
        return(false);
    }
}
//---------------------------------------------------------------------------
__fastcall TDLGSelDir::TDLGSelDir(TComponent* Owner) : TForm(Owner)
{

}//---------------------------------------------------------------------------
void __fastcall TDLGSelDir::dbox_DirMouseDown(TObject *Sender,TMouseButton Button, TShiftState Shift, int X, int Y)
{
    tagPOINT Pos;

    Pos.x = X;
    Pos.y = Y;

    int ItemIndex = dbox_Dir->ItemAtPos(Pos,true);

    /*
    if (ItemIndex >= 0)
        CurrentDirectory = dbox_Dir->GetItemPath( ItemIndex );
    */

    if (ItemIndex >= 0)
        dbox_Dir->ItemIndex = ItemIndex;
}
//---------------------------------------------------------------------------
void __fastcall TDLGSelDir::Createdirectoryhere1Click(TObject *Sender)
{
    if (dlg_bCreateDirectory( dbox_Dir->GetItemPath( dbox_Dir->ItemIndex ) ))
    	dbox_Dir->Update();
}
//---------------------------------------------------------------------------
void __fastcall TDLGSelDir::Renamedirectory1Click(TObject *Sender)
{
	if (dlg_bRenameDirectory( dbox_Dir->GetItemPath( dbox_Dir->ItemIndex ) ))
    	dbox_Dir->Update();
}
//---------------------------------------------------------------------------
void __fastcall TDLGSelDir::Removedirectory1Click(TObject *Sender)
{
	if (dlg_bRemoveDirectory( dbox_Dir->GetItemPath( dbox_Dir->ItemIndex ) ))
    	dbox_Dir->Update();
}
//---------------------------------------------------------------------------

