/* 
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 <iac.h>
#include <menusharing.h>
#include "iowacore.h"
#include <applet.h>
#include <appletmain.h>
#include "iowapack.h"
#include "iowaobject.h"
#include "iowaiac.h"
#include "iowawires.h"


tyfilespec fslastfileopened = {0, 0, 0};


static void cleanObjectVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACcleanobject (param1);
	
	IACreturnboolean (returnedValue);
	} /*cleanObjectVerb*/


static void countObjectsVerb (void) {
	
	long returnedValue;
	
	returnedValue = IACcountobjects ();
	
	IACreturnlong (returnedValue);
	} /*countObjectsVerb*/


static void countSelectedObjectsVerb (void) {
	
	long returnedValue;
	
	returnedValue = IACcountselectedobjects ();
	
	IACreturnlong (returnedValue);
	} /*countSelectedObjectsVerb*/


static void deleteObjectVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACdeleteobject (param1);
	
	IACreturnboolean (returnedValue);
	} /*deleteObjectVerb*/


static void editSpecialAttributesVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACeditspecialattributes (param1);
	
	IACreturnboolean (returnedValue);
	} /*editSpecialAttributesVerb*/


static void getCardAttributesVerb (void) {
	
	Boolean returnedValue;
	
	returnedValue = IACgetcardattributes ();
	
	IACreturnboolean (returnedValue);
	} /*getCardAttributesVerb*/


static void getNthObjectVerb (void) {
	
	long param1;
	Handle returnedValue;
	
	if (!IACgetlongparam ('prm1', ¶m1))
		return;
	
	copyhandle (IACgetnthobject (param1), &returnedValue);
	
	IACreturntext (returnedValue);
	} /*getNthObjectVerb*/


static void getNthSelectedObjectVerb (void) {
	
	long param1;
	Handle returnedValue;
	
	if (!IACgetlongparam ('prm1', ¶m1))
		return;
	
	copyhandle (IACgetnthselectedobject (param1), &returnedValue);
	
	IACreturntext (returnedValue);
	} /*getNthSelectedObjectVerb*/


static void getObjectAttributesVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACgetobjectattributes (param1);
	
	IACreturnboolean (returnedValue);
	} /*getObjectAttributesVerb*/


static void getPackedCardVerb (void) {
	
	/*
	1.0b19 dmb: call app.packcallback instead of iowapack. The latter 
	doesn't handle the tablestoredinfrontier case. 
	*/
	
	Handle returnedValue;
	
	(*app.packcallback) (&returnedValue);
	
	IACreturnbinary (returnedValue, 'CARD');
	} /*getPackedCardVerb*/


static void getPackedObjectVerb (void) {
	
	Handle param1;
	Handle returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	IACpackobject (param1, &returnedValue);
	
	IACreturnbinary (returnedValue, 'CARD');
	} /*getPackedObjectVerb*/


static void getPackedTableVerb (void) {
	
	Handle returnedValue;
	
	copyhandle ((**iowadata).embeddedtable, &returnedValue);
	
	IACreturnbinary (returnedValue, 'CARD');
	} /*getPackedTableVerb*/


static void memAvailVerb (void) {
	
	long returnedValue;
	
	returnedValue = FreeMem ();
	
	IACreturnlong (returnedValue);
	} /*memAvailVerb*/


static void newObjectVerb (void) {
	
	long param1;
	Handle param2;
	Handle param3;
	Handle param4;
	Boolean returnedValue;
	
	if (!IACgetlongparam ('prm1', ¶m1))
		return;
	
	if (!IACgettextparam ('prm2', ¶m2))
		return;
	
	if (!IACgettextparam ('prm3', ¶m3))
		return;
	
	if (!IACgettextparam ('prm4', ¶m4))
		return;
	
	returnedValue = IACnewobject (param1, param2, param3, param4);
	
	IACreturnboolean (returnedValue);
	} /*newObjectVerb*/


static void renameObjectVerb (void) {
	
	Handle param1;
	Handle param2;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	if (!IACgettextparam ('prm2', ¶m2))
		return;
	
	returnedValue = IACrenameobject (param1, param2);
	
	IACreturnboolean (returnedValue);
	} /*renameObjectVerb*/


static void setCardAttributesVerb (void) {
	
	Boolean returnedValue;
	
	returnedValue = IACsetcardattributes ();
	
	IACreturnboolean (returnedValue);
	} /*setCardAttributesVerb*/


static void setObjectAttributesVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACsetobjectattributes (param1);
	
	IACreturnboolean (returnedValue);
	} /*setObjectAttributesVerb*/


static void updateObjectVerb (void) {
	
	Handle param1;
	Boolean returnedValue;
	
	if (!IACgettextparam ('prm1', ¶m1))
		return;
	
	returnedValue = IACupdateobject (param1);
	
	IACreturnboolean (returnedValue);
	} /*updateObjectVerb*/
	
	
static boolean runcardverb (void) {

	Handle hpackedcard;
	OSType binarytype;

	if (!IACgetbinaryparam (keyDirectObject, &hpackedcard, &binarytype))
		return (false);
		
	return (IACreturnboolean (appruncard (hpackedcard)));
	} /*runcardverb*/
	
	
static boolean editcardverb (void) {
	
	extern boolean flprocessingeditevent;
	extern boolean flappopenfileonidle;
	extern tyfilespec fsidleopen;
	
	tyfilespec fs;

	if (!IACgetfilespecparam (keyDirectObject, &fs))
		return (false);
	
	flprocessingeditevent = true; /*signal to unpacking code to edit the card, not run it*/
	
	flappopenfileonidle = true; /*signal to appletmain.c, appidle routine*/
	
	fsidleopen = fs; /*signal to appletmain.c, appidle routine*/
	
	IACreturnboolean (true);
	
	return (true);
	} /*editcardverb*/
	
	
boolean handlefastverb (void) {
	
	switch (IACgetverbtoken ()) {
		
		case 'test':
			IACreturnstring ("\pOh the buzzing of the bees.");
			
			break;
		
		} /*switch*/
	
	return (noErr);
	} /*handlefastverb*/


boolean handleverb (void) {

	switch (IACgetverbtoken ()) {
		
		case 'clob':
			cleanObjectVerb ();
			
			break;
		
		case 'ctob':
			countObjectsVerb ();
			
			break;
		
		case 'ctso':
			countSelectedObjectsVerb ();
			
			break;
		
		case 'deob':
			deleteObjectVerb ();
			
			break;
		
		case 'esat':
			editSpecialAttributesVerb ();
			
			break;
		
		case 'gacd':
			getCardAttributesVerb ();
			
			break;
		
		case 'gnob':
			getNthObjectVerb ();
			
			break;
		
		case 'gnso':
			getNthSelectedObjectVerb ();
			
			break;
		
		case 'gaob':
			getObjectAttributesVerb ();
			
			break;
		
		case 'gpcd':
			getPackedCardVerb ();
			
			break;
		
		case 'gpob':
			getPackedObjectVerb ();
			
			break;
		
		case 'gptb':
			getPackedTableVerb ();
			
			break;
		
		case 'mema':
			memAvailVerb ();
			
			break;
		
		case 'newo':
			newObjectVerb ();
			
			break;
		
		case 'rnob':
			renameObjectVerb ();
			
			break;
		
		case 'sacd':
			setCardAttributesVerb ();
			
			break;
		
		case 'saob':
			setObjectAttributesVerb ();
			
			break;
		
		case 'upob':
			updateObjectVerb ();
			
			break;
			
		case 'glof':
			IACreturnfilespec (&fslastfileopened);
			
			break;
			
		case 'edit':
			editcardverb ();
			
			break;
		
		} /*switch*/
	
	return (noErr);
	} /*handleverb*/