/* requester.c */

/*
 * $Author: Espie $
 * $Date: 91/05/06 23:37:25 $
 * $Revision: 1.9 $
 * $Log:	requester.c,v $
 * Revision 1.9  91/05/06  23:37:25  Espie
 * New calling sequence, returns a WBArg, much better !!
 * 
 * Revision 1.8  91/05/05  04:02:18  Espie
 * Suppress .info files now that we support workbench.
 * 
 * Revision 1.7  91/05/03  02:48:24  Espie
 * *** empty log message ***
 * 
 * Revision 1.6  91/05/02  23:31:40  Espie
 * Got rid of the 2.0 file requester for the time being...
 * 
 * Revision 1.5  91/05/02  11:21:13  Espie
 * Small cosmetic changes. Test if we really have a requester,
 * so that we don't add the gadget for nothing.
 * (NB: interface should exit if no requester and no song loaded :-).
 * 
 * Revision 1.4  91/05/02  01:31:53  Espie
 * New interface to the event handler... still needs
 * a separate task. The kludge start_ticking()/stop_ticking()
 * isn't enough...
 * 
 * Revision 1.3  91/04/30  16:52:41  Espie
 * Added music:.
 * 
 * Revision 1.2  91/04/30  00:35:08  Espie
 * Stable version III.
 * 
 * Revision 1.1  91/04/29  23:54:50  Espie
 * Initial revision
 * 
 *
 */

/* this file will not compile under 1.3 currently,
 * just get rid of the excess luggage...
 */

#define SONG_DIR "Music:"
#include <exec/types.h>
#include <libraries/asl.h>
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/asl.h>
#include <proto/arp.h>
#include <string.h>
#include <stdio.h>
#include <custom/cleanup.h>
#include "proto.h"
 
/* try to get a file requester *somewhere*
 */

/* WE TAKE ADVANTAGE OF THE FACT THAT THE TWO FILEREQUESTER
 * STRUCTURES ARE VERY SIMILAR. THIS MIGHT NOT BE A GOOD IDEA...
 */

/* novice mistake: taking the file name on the directory...
 * It is much more consistent to get a lock on that directory,
 * and return the result as a WBArg, even if it means some
 * juggling.
 *
 * It is this module responsibility to unlock everything it
 * might lock.
 */
  
LOCAL struct FileRequester *request;
LOCAL int req_type;
LOCAL BPTR last_lock;


LOCAL BOOL get_requester(void)
	{
		req_type = requester_type();
		switch(req_type)
			{
			case ARP:
				request = ArpAllocFreq();
				return request != 0;
				
			case OS_20:
				request = AllocFileRequest();
				return request != 0;
			default:
				return NULL;
			}
	}

/* this manual check can weed out .info files */

BOOL is_info(char *name)
	{
	int l;
		l = strlen(name);
		if (l < 5)
			return FALSE;
		if (name[l-5] != '.' || name[l-4] != 'i' || name[l-3] != 'n'
			|| name[l-2] != 'f' || name[l-1] != 'o')
			return FALSE;
		else
			return TRUE;
	}

LOCAL ULONG newdo(ULONG flags, void *object)
	{
		if (flags & RFF_DOMSGFUNC)
			{
				message_interface((struct IntuiMessage *)object);
				clear_abort();
			}
		if (flags & RFF_DOWILDFUNC)
			{
			struct AnchorPath *ap;
				ap = (struct AnchorPath *)object;
			return is_info(ap->ap_Info.fib_FileName);
			}
		return 0;
	}
	
void cleanlock(void)
	{
		if (last_lock)
			{
				UnLock(last_lock);
				last_lock = NULL;
			}
	}
				
BOOL init_requester(struct Window *w)
	{
		if (get_requester())
			{
				strcpy(request->rf_Dir, SONG_DIR);
				last_lock = NULL;
				ToClean0(cleanlock);
				request->rf_Window = w;
				request->rf_Function = &newdo;
				request->rf_FuncFlags |= RFF_DOMSGFUNC | RFF_DOWILDFUNC;
				return TRUE;
			}
		else
			return FALSE;	
	}	
				
BOOL query_name(char *banner, struct WBArg *result)
	{
	char *ok;
		if (req_type == NO_REQ)
			return NULL;
		request->rf_Hail = banner;
		switch(req_type)
			{
			case ARP:
				ok = FileRequest(request);
				break;
			case OS_20:
				ok = RequestFile(request);
			}	
		if (!ok)
			return FALSE;
		else
			{
				cleanlock();
				last_lock = Lock(request->rf_Dir, ACCESS_READ);
				if (last_lock)
					{
						result->wa_Lock = last_lock;
						result->wa_Name = request->rf_File;
						return TRUE;
					}
				else
					return FALSE;
			}
	}
