/*
 *	Copyright (c) 1991 by the Massachusetts Institute of Technology,
 *	For copying and distribution information, see the file
 *	"mit-copyright.h".
 *  appleeventMgr.c - edward 1/11/94
 * 
 *  These are routines to deal with AppleEvents
 *
 * Revision 4.2a3 6/8/94 - edward
 * Included StandardFile
 *
 * Revision 4.2a3 6/9/94 - edward
 * ae_init now uses UPPs
 *
 * Revision 4.2a4 7/1/94 - edward
 * Porting to MW C/C++ 68K.  The headers are only included if we're not using MW
 * Added ae_init_upps and the global upps
 * Made ae_got_params static and prototyped it
 * Removed the #pragma(unused)'s and removed the variable names of the unused variables from
 *  the function definitions.  I left the names in the function prototypes.
 */

// 7/1/94 edward - If we're compiling with MW all of these headers are precompiled in TIFast*
#ifndef __MWERKS__

#include <Types.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Controls.h>
#include <Windows.h>
#include <Menus.h>
#include <TextEdit.h>
#include <Dialogs.h>
#include <Desk.h>
#include <Scrap.h>
#include <ToolUtils.h>
#include <Memory.h>
#include <SegLoad.h>
#include <Files.h>
#include <OSUtils.h>
#include <Traps.h>
#include <Lists.h>
#include <Errors.h>
#include <string.h>
#include <AppleEvents.h>
#include <StandardFile.h>
#include "pips.h"
#ifdef TE32K
#include "TE32K.h"
#endif
#include "documentRec.h"
#include "prototype.h"

#endif

#include "mit-copyright.h"

/* Universal Proc Ptrs */
AEEventHandlerUPP gAe_open_app_upp;
AEEventHandlerUPP gAe_open_doc_upp;
AEEventHandlerUPP gAe_print_doc_upp;
AEEventHandlerUPP gAe_open_quit_upp;
AEEventHandlerUPP gAe_show_by_node_id_upp;

static OSErr ae_got_params(AppleEvent *theAppleEvent);
pascal OSErr ae_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon);
pascal OSErr ae_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon);
pascal OSErr ae_print_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon);
pascal OSErr ae_quit_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon);
pascal OSErr ae_show_by_node_id(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon);

/*
 * ae_init_upps
 *
 * Initializes the routine descriptors for the AppleEvent handlers
 *
 */
 
void ae_init_upps(void)
{
	gAe_open_app_upp = NewAEEventHandlerProc(ae_open_app);
	gAe_open_doc_upp = NewAEEventHandlerProc(ae_open_doc);
	gAe_print_doc_upp = NewAEEventHandlerProc(ae_print_doc);
	gAe_open_quit_upp = NewAEEventHandlerProc(ae_quit_app);
	gAe_show_by_node_id_upp = NewAEEventHandlerProc(ae_show_by_node_id);
}

/*
 * ae_init
 *
 * Installs rountines to handle all of the AppleEvents
 */
 
void ae_init(void)
{
	OSErr	err;
	
	/* 4.2a3 - Handler procs updated to be UPPs */
	err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, gAe_open_app_upp, 0L, false);
	if (err == memFullErr)
		BigBadError(NO_MEMORY);

	err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, gAe_open_doc_upp, 0L, false);
	if (err == memFullErr)
		BigBadError(NO_MEMORY);

	err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, gAe_print_doc_upp, 0L, false);
	if (err == memFullErr)
		BigBadError(NO_MEMORY);

	err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, gAe_open_quit_upp, 0L, false);
	if (err == memFullErr)
		BigBadError(NO_MEMORY);

	err = AEInstallEventHandler('PIPS', 'SBNI', gAe_show_by_node_id_upp, 0L, false);
	if (err == memFullErr)
		BigBadError(NO_MEMORY);
		
	AESetInteractionAllowed(kAEInteractWithLocal);
}

static OSErr ae_got_params(AppleEvent *theAppleEvent)
{
	OSErr		err;
	DescType	returnedType;
	Size		actualSize;

	err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, &returnedType,
							NULL, 0, &actualSize);
	if (err == errAEDescNotFound)
		return noErr;
	else if (err == noErr)
		return errAEParamMissed;
	else
		return err;
}

// edward 7/1/94 - removed the reply and result variables from the handler function definitions because they
// are unused
pascal OSErr ae_open_app(AppleEvent *theAppleEvent, AppleEvent *, long)
{
	Str255		doit;
	OSErr		err;

	err = ae_got_params(theAppleEvent);
	
	if (err == noErr)
	{
		pref_get_string(SHOW_DISCLAIMER, doit);
		if (!strcmp(doit,"true"))
			err = dlog_disclaimer();		/* put-up disclaimer dialog box */
	}
	
	return err;
}

pascal OSErr ae_open_doc(AppleEvent *theAppleEvent, AppleEvent *, long)
{
	OSErr err;
	AEDescList docList;
	
	err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
	
	if (err == noErr)
	{
		err = ae_got_params(theAppleEvent);
		err = AEDisposeDesc(&docList);
	}
	return err;
}

pascal OSErr ae_print_doc(AppleEvent *theAppleEvent, AppleEvent *, long)
{
	OSErr err;
	AEDescList docList;
	
	err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
	
	if (err == noErr)
	{
		err = ae_got_params(theAppleEvent);
		err = AEDisposeDesc(&docList);
	}
	return err;
}

pascal OSErr ae_quit_app(AppleEvent *, AppleEvent *, long)
{
	Terminate();
	
	return noErr;
}

pascal OSErr ae_show_by_node_id(AppleEvent *theAppleEvent, AppleEvent *, long)
{
	OSErr		err;
	DescType	type;
	Str255		data;
	Size		actualSize;

	err = AEGetParamPtr(theAppleEvent, keyDirectObject, typeChar, &type, data, sizeof(data), &actualSize);
	if (err == noErr && type == typeChar)
		err = ae_got_params(theAppleEvent);
	if (err == noErr && type == typeChar)
		cmd_show(data);
	return err;
}