/*
 *  FileReq.c
 *  ASL interface
 */

#include "defs.h"

Prototype void do_aslload    (void);
Prototype int  do_aslsave    (void);
Prototype void do_aslinsfile (void);
Prototype void do_aslfont    (void);
Prototype void do_reqload    (void);
Prototype int  do_reqsave    (void);
Prototype void do_reqinsfile (void);
Prototype void do_reqfont    (void);
Prototype void fixfile	     (char *, char *);
Prototype void splitpath     (char *, char *, char *);
Prototype void fixdirname    (char *);

Prototype struct ReqToolsBase	 * ReqToolsBase;
Prototype struct rtFileRequester * RFReq;

struct rtFileRequester * RFReq;
struct ReqToolsBase    * ReqToolsBase;


/*
 *  The ASL interface uses the asl.library if it exists, else tries to
 *  use the reqtools, then the ARP interfafce.
 */

static char pattern[64] = "";
static char file[108];
static char dir[256];


void do_pattern (void)
{
    strncpy (pattern, av[1], 63);
    pattern[63] = 0;
}


void do_aslload (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
    BOOL ret;

    splitpath(Ep->Name, file, dir);
    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (FReq) {                /* If we have asl.library & requester */
	ret = AslRequestTags ((APTR)FReq,
		ASL_Hail,	"XDME Load New File",
		ASL_OKText,	"Load",
		ASL_Window,	Ep->Win,
		ASL_FuncFlags,	FILF_NEWIDCMP | FILF_PATGAD,
		ASL_Dir,	dir,
		ASL_File,	file,
		ASL_Pattern,	pattern,
		ASL_LeftEdge,	Ep->ASLleft + Ep->Win->LeftEdge,     /* use config coords */
		ASL_TopEdge,	Ep->ASLtop + Ep->Win->TopEdge,
		ASL_Width,	Ep->ASLwidth,
		ASL_Height,	Ep->ASLheight,
		TAG_DONE );

	Ep->ASLtop = FReq->rf_TopEdge - Ep->Win->TopEdge;      /* save coords */
	Ep->ASLleft = FReq->rf_LeftEdge - Ep->Win->LeftEdge;
	Ep->ASLwidth = FReq->rf_Width;
	Ep->ASLheight = FReq->rf_Height;

	if (ret) {
	    BPTR newlock;

	    if (newlock = Lock( FReq->rf_Dir, SHARED_LOCK)) {
		UnLock(CurrentDir(oldlock));
		Ep->dirlock = (long)newlock;

		strcpy (pattern, FReq->rf_Pat);

		/* fixfile(FReq->rf_File,FReq->rf_Dir); */
		av[0] = (ubyte *)"n";
		av[1] = (ubyte *) FReq->rf_File;

		do_edit();
	    }
	} else
	    CurrentDir(oldlock);
    } else {		  /* no asl.library? try reqtools */
	do_reqload();
    }
} /* do_aslload */


int do_aslsave (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
    BOOL ret;

    splitpath(Ep->Name, file, dir);
    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (FReq) {
loop:
	ret = AslRequestTags( (APTR)FReq,
		ASL_Hail,	"XDME Save File",
		ASL_OKText,	"Save",
		ASL_Window,	Ep->Win,
		ASL_FuncFlags,	FILF_NEWIDCMP | FILF_SAVE,
		ASL_Dir,	dir,
		ASL_File,	file,
		ASL_LeftEdge,	Ep->ASLleft + Ep->Win->LeftEdge,     /* use config coords */
		ASL_TopEdge,	Ep->ASLtop + Ep->Win->TopEdge,
		ASL_Width,	Ep->ASLwidth,
		ASL_Height,	Ep->ASLheight,
		TAG_DONE
	);

	Ep->ASLtop = FReq->rf_TopEdge - Ep->Win->TopEdge;      /* save coords */
	Ep->ASLleft = FReq->rf_LeftEdge - Ep->Win->LeftEdge;
	Ep->ASLwidth = FReq->rf_Width;
	Ep->ASLheight = FReq->rf_Height;

	if (ret) {
	    BPTR tmp_lock;

	    if (!stricmp (FReq->rf_File, "unnamed") ) {
		error ("arpsave:\nCannot save file as\n`unnamed'");
		*file = 0;
		goto loop;
	    }

	    CurrentDir(oldlock);
	    strcpy (pattern, FReq->rf_Pat);
	    fixfile( FReq->rf_File, FReq->rf_Dir);
	    av[1] = (ubyte *)FReq->rf_File;

	    if (tmp_lock = Lock (FReq->rf_File, ACCESS_READ)) {
		UnLock (tmp_lock);

		if (getyn ("XDME Warning",
			"The file\n`%s'\ndoes already\n"
			"exist. Do you want to overwrite it ?",
			"Yes|No", FReq->rf_File) )
		    return (do_saveas ());
	    }
	} else {
	    CurrentDir(oldlock);
	}
    } else {
	return (do_reqsave ());
    }

    return (FALSE);
} /* do_aslsave */


void do_aslinsfile (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);
    BOOL ret;

    splitpath(Ep->Name, file, dir);
    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (FReq) {
	ret = AslRequestTags( (APTR)FReq,
		ASL_Hail,	"XDME Insert File",
		ASL_OKText,	"Insert",
		ASL_Window,	Ep->Win,
		ASL_FuncFlags,	FILF_NEWIDCMP | FILF_PATGAD,
		ASL_Dir,	dir,
		ASL_File,	file,
		ASL_Pattern,	pattern,
		ASL_LeftEdge,	Ep->ASLleft + Ep->Win->LeftEdge,     /* use config coords */
		ASL_TopEdge,	Ep->ASLtop + Ep->Win->TopEdge,
		ASL_Width,	Ep->ASLwidth,
		ASL_Height,	Ep->ASLheight,
		TAG_DONE );

	Ep->ASLtop = FReq->rf_TopEdge - Ep->Win->TopEdge;      /* save coords */
	Ep->ASLleft = FReq->rf_LeftEdge - Ep->Win->LeftEdge;
	Ep->ASLwidth = FReq->rf_Width;
	Ep->ASLheight = FReq->rf_Height;

	if (ret) {
	    CurrentDir(oldlock);

	    /* Fields are READ-ONLY */
	    strcpy (file, FReq->rf_File);
	    strcpy (dir, FReq->rf_Dir);
	    strcpy (pattern, FReq->rf_Pat);

	    fixfile (file, dir);

	    av[0] = (ubyte *)"i";
	    av[1] = (ubyte *)file;

	    do_edit();
	} else
	    CurrentDir(oldlock);
    } else {
	do_reqinsfile();
    }
} /* do_aslinsfile */


void do_aslfont (void)
{
    if (FReq) {
	struct FontRequester *fontreq;

#ifdef _DCC
	ULONG fwin = (ULONG)Ep->Win;  /* put ASL on correct screen */
#endif

	struct TagItem FontTags[] = {	/* DICE 2.06.37 allows us to initialize this here... */
#ifdef _DCC
	    ASL_Window,     fwin,
	    ASL_LeftEdge,   Ep->ASLleft + Ep->Win->LeftEdge,	 /* use config coords */
	    ASL_TopEdge,    Ep->ASLtop + Ep->Win->TopEdge,
#else
	    ASL_Window,     0L,
	    ASL_LeftEdge,   0L,
	    ASL_TopEdge,    0L,
#endif
	    ASL_Hail,	    (ULONG)"XDME fixed width font",
	    ASL_FuncFlags,  FILF_NEWIDCMP | FONF_FIXEDWIDTH,
	    /* Removed Min & MaxHeight since I cannot figure out what the
	       user may want */
	    TAG_DONE
	};

#ifndef _DCC
	FontTags[0].ti_Data = (ULONG)Ep->Win;
	FontTags[1].ti_Data = Ep->ASLleft + Ep->Win->LeftEdge;
	FontTags[2].ti_Data = Ep->ASLtop + Ep->Win->TopEdge;
#endif

	if (fontreq = (struct FontRequester *)
		      AllocAslRequest(ASL_FontRequest,FontTags) ) {

	    if (AslRequest(fontreq, NULL)) {

		/*  Since we have GetFont() already, just pass
		 *   the name and size from fontreq's TextAttr to it.
		 *
		 *  What follows mimics SETFONT (in cmnd3.c)
		 */

		FONT *font = (FONT *)GetFont( fontreq->fo_Attr.ta_Name,
					     (short) fontreq->fo_Attr.ta_YSize );
		ED *ep = Ep;
		if (font) {
		    text_sync();

		    if (ep->Font)
			CloseFont(ep->Font);

		    ep->Font = font;

		    SetFont(ep->Win->RPort, font);
		    SetRast(ep->Win->RPort, 0);

		    RefreshWindowFrame(ep->Win);

		    set_window_params();

		    text_redisplay();
		} else {
		    error ("aslfont:\nUnable to find font\n%s/%ld", fontreq->fo_Attr.ta_Name,
			   fontreq->fo_Attr.ta_YSize);
		}
	    }

	    FreeAslRequest(fontreq);
	}
    } else {
	do_reqfont();
    }
} /* do_aslfont */


/*  this little kludge gets a DIR: string for the ASL requester
 */

void fixdirname (char * dir)
{
    if (!strlen(dir)) {                 /* if no DIR; KLUDGE ALERT! */
	if (NameFromLock((BPTR)Ep->dirlock, dir, 256)!=DOSTRUE) {
	    dir[0] = 0; /* assumes NameFromLock doesn't clean up on err(??) */
	}
    }
} /* fixdirname */


void fixfile (char * file, char * dir)
{
    char * ptr;
    short  len = strlen (dir);
    char   hasdev = 0;

    /*
     *	do we need to add a slash to the directory spec?
     */

    if (len && dir[len-1] != '/' && dir[len-1] != ':') {
	dir[len ++] = '/';
	dir[len] = 0;
    }

    /*
     *	Is file spec really a full path spec?
     */

    for (ptr = file; *ptr; ptr ++) {
	if (ptr[0] == ':')
	    hasdev = 1;
    }

    if (!hasdev) {
	movmem (file, file+len, strlen(file)+1);
	movmem (dir, file, len);
    }
} /* fixfile */


/*
 *  Search backwards for first ':' or '/' and split path there.
 *  This subroutine may appear to be coded incorrectly to a novice
 *  programmer.  It isn't [now].
 */

void splitpath (char * name, char * file, char * dir)
{
    short i;

    for (i = strlen(name); i >= 0; --i) {       /* was (incorrectly) "i > 0" */
	if (name[i] == ':' || name[i] == '/')
	    break;
    }

    i ++;

    strcpy (file, name + i);
    movmem (name, dir, i);

    dir[i] = 0;
} /* splitpath */


/*
 *  The REQ interface uses the reqtools.library if it exists, else tries to
 *  use the ARP interfafce.
 */

void do_reqload (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);

    splitpath(Ep->Name, file, dir);

    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (RFReq) {                /* If we have req.library & requester */
	rtChangeReqAttr(RFReq, RTFI_Dir, dir, TAG_DONE);

	if (rtFileRequest (RFReq, file, "XDME Load New File",
		RTFI_OkText,	"Load",
		RT_Window,	Ep->Win,
		RT_ReqPos,	REQPOS_TOPLEFTSCR,
		RT_LeftOffset,	Ep->ASLleft,
		RT_TopOffset,	Ep->ASLtop,
		RTFI_Height,	Ep->ASLheight,
		TAG_DONE )) {
	    BPTR newlock;

	    if (newlock = Lock( RFReq->Dir, SHARED_LOCK)) {
		UnLock(CurrentDir(oldlock));
		Ep->dirlock = (long)newlock;

		/* fixfile(file,RFReq->Dir); */
		av[0] = (ubyte *)"n";
		av[1] = (ubyte *) file;

		do_edit();

	    }
	} else
	    CurrentDir(oldlock);
    } else {		  /* no req.library? Gulp */
	error ("For filerequester,\nI need REQ- or ASL.LIBRARY !");
    }
} /* do_reqload */


int do_reqsave (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);

    splitpath(Ep->Name, file, dir);
    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (FReq) {
	rtChangeReqAttr(RFReq, RTFI_Dir, dir, TAG_DONE);

loop:
	if(rtFileRequest (RFReq, file, "XDME Save File",
		RTFI_OkText,	"Save",
		RT_Window,	Ep->Win,
		RTFI_Flags,	FREQF_SAVE,
		RT_ReqPos,	REQPOS_TOPLEFTSCR,
		RT_LeftOffset,	Ep->ASLleft,
		RT_TopOffset,	Ep->ASLtop,
		RTFI_Height,	Ep->ASLheight,
		TAG_DONE )) {
	    BPTR tmp_lock;

	    if (!(stricmp (file, "unnamed")) ) {
		error ("arpsave:\nCannot save file as\n`unnamed'");
		goto loop;
	    }

	    CurrentDir(oldlock);
	    fixfile( file, RFReq->Dir);
	    av[1] = (ubyte *)file;

	    if (tmp_lock = Lock (file, ACCESS_READ)) {
		UnLock (tmp_lock);

		if (getyn ("XDME Warning",
			"The file\n`%s'\ndoes already\n"
			"exist. Do you want to overwrite it ?",
			"Yes|No", file) )
		    return (do_saveas ());
	    }
	} else {
	    CurrentDir(oldlock);
	}
    } else {
	error ("For filerequester,\nI need REQ- or ASL.LIBRARY !");
    }

    return (FALSE);
} /* do_reqsave */


void do_reqinsfile (void)
{
    BPTR oldlock = CurrentDir((BPTR)Ep->dirlock);

    splitpath(Ep->Name, file, dir);
    if (!stricmp (file, "unnamed"))
	*file = 0;

    fixdirname(dir);

    if (FReq) {
	rtChangeReqAttr(RFReq, RTFI_Dir, dir, TAG_DONE);
	if(rtFileRequest (RFReq, file, "XDME Insert File",
		RTFI_OkText,	"Load",
		RT_ReqPos,	REQPOS_TOPLEFTSCR,
		RT_Window,	Ep->Win,
		RT_LeftOffset,	Ep->ASLleft,
		RT_TopOffset,	Ep->ASLtop,
		RTFI_Height,	Ep->ASLheight,
		TAG_DONE )) {

	    CurrentDir(oldlock);

	    fixfile( file, RFReq->Dir);

	    av[0] = (ubyte *)"i";
	    av[1] = (ubyte *)file;

	    do_edit();
	} else
	    CurrentDir(oldlock);
    } else {
	error ("For filerequester,\nI need REQ- or ASL.LIBRARY !");
    }
} /* do_reqinsfile */


void do_reqfont (void)
{
    if (ReqToolsBase) {
	struct rtFontRequester *fontreq;

#ifdef _DCC
	struct Window *fwin = Ep->Win;	/* put ASL on correct screen */
#endif

	struct TagItem FontTags[] = {	/* DICE 2.06.37 allows us to initialize this here... */
#ifdef _DCC
	    RT_Window,	    fwin,
	    RTFO_MinHeight, 6,
	    RTFO_MaxHeight, 24,
#else
	    RT_Window,	    0,
	    RTFO_MinHeight, 0,
	    RTFO_MaxHeight, 0,
#endif
	    RTFO_Flags,     FREQF_FIXEDWIDTH, /* | FILF_NEWIDCMP */
	    TAG_DONE };

#ifndef _DCC
	    FontTags[0].ti_Data = (ULONG)Ep->Win;
	    FontTags[1].ti_Data = 6;
	    FontTags[2].ti_Data = 24;
#endif

	if (fontreq = rtAllocRequestA (RT_FONTREQ, NULL)) {
	    if (rtFontRequestA (fontreq, "XDME fixed width font", FontTags)) {

		/*  Since we have GetFont() already, just pass
		 *   the name and size from fontreq's TextAttr to it.
		 *
		 *  What follows mimics SETFONT (in cmnd3.c)
		 */

		FONT *font = (FONT *)GetFont( fontreq->Attr.ta_Name,
					     (short) fontreq->Attr.ta_YSize );
		ED *ep = Ep;
		if (font) {
		    text_sync();
		    if (ep->Font)
			CloseFont(ep->Font);
		    ep->Font = font;
		    SetFont(ep->Win->RPort, font);
		    SetRast(ep->Win->RPort, 0);
		    RefreshWindowFrame(ep->Win);
		    set_window_params();
		    text_redisplay();
		} else {
		    error ("aslfont:\nUnable to find font\n%s/%ld", fontreq->Attr.ta_Name,
			   fontreq->Attr.ta_YSize);
		}
	    }
	    rtFreeRequest (fontreq);
	}
    } else {
	error ("Need ASL.LIBRARY or\nREQTOOLS.LIBRARY for\nfont requester !");
    }
} /* do_reqfont */


/* EOF */
