//
//	MiscInfoController.m -- a class to handle the guts under an Info menu
//		Written by Don Yacktman (c) 1994 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/misckit.h>

#define mailMessage [strings valueForStringKey:"MailMessage"]
#define authorEmail [strings valueForStringKey:"AuthorEMail"]
#define authorName [strings valueForStringKey:"AuthorName"]
#define _clobber [strings valueForStringKey:"Clobber"]
#define _warning [strings valueForStringKey:"Warning"]
#define _abort [strings valueForStringKey:"Abort"]
#define _OK [strings valueForStringKey:"OK"]
#define _OKForever [strings valueForStringKey:"OKForever"]

@implementation MiscInfoController

- strings
{
	// attempt to get the string table if it isn't already set up.
	// Try (1) .lproj directories or (2) app delegate.
	if (!strings) {
		// .lproj?
	}
	if (!strings) {
		// NXApp?
		if ([NXApp respondsTo:@selector(strings)])
			strings = [NXApp strings];
	}
	if (!strings) {
		// [NXApp delegate]?
		if ([[NXApp delegate] respondsTo:@selector(strings)])
			strings = [[NXApp delegate] strings];
	}
	return strings;
}

- info
{
	if (!info) { // create the object if needed
		info = [[MiscInfo alloc] init];
		[info setController:self];
	}
	return info;
}

- orderForm		// return the orderFormPanel, load it if needed.
{
	if (!orderForm) { // create the object if needed
		orderForm = [[MiscOrderForm alloc] init];
		[orderForm setController:self];
	}
	return orderForm;
}

- registration		// return the registerPanel, load it if needed.
{
	if (!registration) { // create the object if needed
		registration = [[MiscRegistration alloc] init];
		[registration setController:self];
	}
	return registration;
}

- info:sender
{ // forward the message on to the MiscInfo object.
	[[self info] info:sender];
	return self;
}

- orderForm:sender
{ // forward the message on to the MiscInfo object.
	[[self orderForm] orderForm:sender];
	return self;
}

- registration:sender
{ // forward the message on to the MiscInfo object.
	[[self registration] registration:sender];
	return self;
}

- releaseNotes:sender	// make the README panel be up there.
{
	[[[NXHelpPanel new]
			showFile:"ReleaseNotes.rtfd" atMarker:NULL]
			makeKeyAndOrderFront:self];
	return self;
}

- license:sender	// make the license panel be up there.
{
	[[[NXHelpPanel new]
			showFile:"License.rtfd" atMarker:NULL]
			makeKeyAndOrderFront:self];
	return self;
}

// call() macro sends a message from the app's Speaker to Mail.app to
// perform the various Mail.app functions.
#define call(a,b) [s performRemoteMethod:a with:b length:strlen(b)+1]
- suggestion:sender	// This is pretty much lifted from Opener.
{
	id body = [[MiscString alloc] init];
	id subject = [[MiscString alloc] init];
    id s = [NXApp appSpeaker];
	int x = 1; int doit = NO;
	id tmpstr = [MiscString newWithString:
			NXGetDefaultValue([NXApp appName], "MiscMailClobber")];

	[self strings]; // make sure we have a string table
	// Determine if we clobber, abort, or skip warning panel
	if ([tmpstr emptyString]) doit = YES;
	else if ([tmpstr cmp:"OK"]) doit = YES;
	if (doit) x = NXRunAlertPanel(_warning, _clobber, _OK, _OKForever, _abort);
	if (x == 0) NXWriteDefault ([NXApp appName], "MiscMailClobber", "OK");
	if (x == -1) return self; // abort

	// prepare the message to send
    [subject setFromFormat:"Comments and suggestions for ``%s'' ",
			[NXApp appName]];
	[subject catStrings:[info versionString], " (",
			[registration serialNumber], ")", NULL];
	[body setFromFormat:mailMessage, authorName, [NXApp appName]];
    [body catStrings:[NXApp userRealName], "\n", NULL];

	// launch mail, bring up a compose window if needed, and prepare
	// the message for the user.  They decide whether or not to deliver
	// it, and so on.
    NXPortFromName("Mail", NULL); // make sure app is launched
    [s setSendPort:NXPortFromName("MailSendDemo", NULL)];
    call("setTo:", authorEmail); // will bring up a compose window if needed
    call("setSubject:", [subject stringValue]);
    call("setBody:", [body stringValue]);
    [subject free];
    return self;
}

@end
