/* MRBackup Error handling routines.
 * Filename:	Error.c
 * Date:		11/20/87
 * 
 * History:		(most recent change first)
 *
 * 11/20/87 -MRR- V1.4 This package was created, along with ErrorRequest.c,
 *				  to provide extended error handling features.
 */

#include "MRBackup.h"
#include "Gadget.h"

struct Requester RequesterStructure2;

/* Get an error handling option.  This is done by putting up a requester
 * and waiting for the user to select one of the option gadgets.
 * Called with:
 *		flags:	error recovery flags
 *			The flags are various ERR_ codes, OR'ed together.  For example,
 *			ERR_ABORT | ERR_RETRY_FILE | ERR_IGNORE
 *			would give the option of allowing the user to abort the
 *			operation, retry the current file or simply ignore the error.
 * Returns:
 *		error recovery code (ERR_ABORT, ERR_RETRY_FILE, etc.)
 */
GetErrOpt(flags)
	unsigned flags;
{
	ULONG class;
	unsigned enable;
	struct Gadget *gadget;
	struct IntuiMessage *msg;
	int status = ERR_ABORT;

	if (!flags) 	flags = ERR_ABORT;

	for (gadget = RequesterStructure2.ReqGadget; gadget;
		 gadget = gadget->NextGadget) {
		switch (gadget->GadgetID) {
		case ABORT:
			enable = flags & ERR_ABORT;
			break;
		case FILERETRY:
			enable = flags & ERR_RETRY_FILE;
			break;
		case DISKRESTART:
			enable = flags & ERR_RESTART_VOLUME;
			break;
		case FILESKIP:
			enable = flags & ERR_IGNORE;
			break;
		default:			/* Some other gadgetry; default to enabled. */
			enable = 1;
		}
		if (enable)
			gadget->Flags &= ~GADGDISABLED;
		else
			gadget->Flags |= GADGDISABLED;
	}

	RequesterStructure2.LeftEdge = 5;
	RequesterStructure2.TopEdge = 15;
	if (!Request(&RequesterStructure2, mainWindow)) {
		TypeAndSpeak("I could not put up my error requester.\n");
		TypeAndSpeak("I will have to abort this operation.\n");
		status = ERR_ABORT;
	}
	else {
		WindowToFront(mainWindow);
		for (class = 0; class != GADGETDOWN; ) { 
			Wait(1L << mainWindow->UserPort->mp_SigBit);
			msg = (struct IntuiMessage *) GetMsg(mainWindow->UserPort);
			if (msg) {
				class = msg->Class;
				gadget = (struct Gadget *) msg->IAddress;
				ReplyMsg(msg);
			}
		}
		switch (gadget->GadgetID) {
		case ABORT:
			status = ERR_ABORT;
			break;
		case FILERETRY:
			status = ERR_RETRY_FILE;
			break;
		case DISKRESTART:
			status = ERR_RESTART_VOLUME;
			break;
		case FILESKIP:
			status = ERR_IGNORE;
			break;
		default:
			TypeAndSpeak("I have a bug in my error requester!\n");
		}
	}
	WindowToBack(mainWindow);
	return status;
}
