/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "iacinternal.h"


/*
8/24/92 DW: The routines in this file are usually only needed in double-
clickable applications.

7/19/94 dmb: Added IACwaitroutineUPP for Universal Headers / PowerPC 
compatibility
*/


OSErr IACdrivefilelist (tyFScallback handlespecroutine) {
	
	/*
	the opendoc and printdoc required events take a list of filespecs as a 
	parameter. we factor out the common code, and make it a little easier for an
	application to handle these events.
	
	you supply a callback routine that handles a single filespec, you could
	print it or open it, depending on which of the required events is being
	invoked.
	*/

	AEDesc desc;
	long ctfiles;
	DescType actualtype;
	long actualsize;
	AEKeyword actualkeyword;
	FSSpec fs;
	long i;
	OSErr ec;
						
	ec = AEGetKeyDesc (IACglobals.event, keyDirectObject, typeAEList, &desc);
	
	IACglobals.errorcode = ec;
	
	if (ec != noErr) 
		return (ec);
		
	ec = AECountItems (&desc, &ctfiles);
	
	IACglobals.errorcode = ec;
	
	if (ec != noErr) 
		return (ec);
				
	for (i = 1; i <= ctfiles; i ++) {
	
		ec = AEGetNthPtr (
			&desc, i, typeFSS, &actualkeyword, &actualtype, 
			
			(Ptr) &fs, sizeof (fs), &actualsize);
							
		IACglobals.errorcode = ec;
	
		if (ec != noErr) {
		
			AEDisposeDesc (&desc);
			
			return (ec);
			}
			
		if (!(*handlespecroutine) (&fs))
			return (-1);
		} /*for*/
		
	return (noErr);
	} /*IACdrivefilelist*/


static OSType IACgetprocesscreator (void) {
	
	/*
	get the 4-character creator identifier for the application we're running 
	inside of.
	*/
	
	ProcessSerialNumber psn;
	ProcessInfoRec info;
	
	GetCurrentProcess (&psn);
	
	info.processInfoLength = (long) sizeof (info);
	
	info.processName = nil;
	
	info.processAppSpec = nil;
	
	GetProcessInformation (&psn, &info);
	
	return (info.processSignature);
	} /*IACgetprocesscreator*/
	
	
static pascal OSErr CoerceTargetIDToType (DescType typecode, Ptr dataptr, Size datasize, DescType totype, long refcon, AEDesc *result) {
	
	#pragma unused (typecode, datasize, totype)
	
	TargetID target;
	ProcessSerialNumber psn;
	ProcessInfoRec info;
	Ptr addressoftype;
	OSErr ec;
	
	BlockMove (dataptr, &target, sizeof (target));
	
	if (target.location.locationKindSelector == ppcNoLocation) { /*local program*/
		
		ec = GetProcessSerialNumberFromPortName (&target.name, &psn);
		
		if (ec != noErr)
			return (ec);
		
		info.processInfoLength = (long) sizeof (info);
		
		info.processName = nil;
		
		info.processAppSpec = nil;
		
		ec = GetProcessInformation (&psn, &info);
		
		if (ec != noErr)
			return (ec);
			
		addressoftype = (Ptr) &info.processSignature;
		}
		
	else { /*not a local program*/
		
		if (target.name.portKindSelector == ppcByCreatorAndType)
			addressoftype = (Ptr) &target.name.u.port.portCreator;
		else
			addressoftype = ((Ptr) &target.name.u.portTypeStr) + 1; /*kloooge*/
		}
		
	(*result).descriptorType = typeType;
	
	return (PtrToHand (addressoftype, &(*result).dataHandle, 4));
	} /*CoerceTargetIDToType*/
	
	
static pascal OSErr CoercePSNToType (DescType typecode, Ptr dataptr, Size datasize, DescType totype, long refcon, AEDesc *result) {
	
	#pragma unused (typecode, datasize, totype, refcon)
	
	ProcessInfoRec info;
	OSErr ec;
	
	info.processInfoLength = (long) sizeof (info);
	
	info.processName = nil;
	
	info.processAppSpec = nil;
	
	ec = GetProcessInformation ((ProcessSerialNumber *) dataptr, &info);
	
	if (ec != noErr)
		return (ec);
		
	(*result).descriptorType = typeType;
	
	return (PtrToHand (&info.processSignature, &(*result).dataHandle, 4));
	} /*CoercePSNToType*/


Boolean IACinit (void) {
	
	/*
	returns true if the machine we're running on has Apple events, and if we are
	able to install our coercion handlers. it may do more in future versions.
	
	call this routine before using any of the other iac.c routines. disable your
	Apple event support if we return false.
	
	8/23/92 DW: only initialize once, no matter how many times we're called.
	*/
	
	static Boolean fl = false; 
	
	if (fl) 
		return (true);
		
	fl = true;
	
	if (!IAChaveappleevents ())
		return (false);
		
	if (!IACinstallcoercionhandler (typeProcessSerialNumber, typeType, (ProcPtr) &CoercePSNToType))
		return (false);
		
	if (!IACinstallcoercionhandler (typeTargetID, typeType, (ProcPtr) &CoerceTargetIDToType))
		return (false);
	
	IACwaitroutineUPP = NewAEIdleProc (IACwaitroutine);
	
	IACglobals.idprocess = IACgetprocesscreator ();

	IACglobals.waitroutine = nil;
	
	IACglobals.nextparamoptional = false;
		
	return (true);
	} /*IACinit*/
	
	
Boolean IAChaveappleevents (void) {

	/*
	return true if Apple Events are available.
	*/
	
	long gestaltAppleEventsPresent;
	
	if (Gestalt (gestaltAppleEventsAttr, &gestaltAppleEventsPresent) != noErr)
		return (false);
	
	return (gestaltAppleEventsPresent != 0);
	} /*IAChaveappleevents*/