
/* Generated by Interface Builder */

#import "Main.h"
#import <appkit/Application.h>
#import <appkit/Speaker.h>
#import <appkit/Listener.h>
#import <appkit/Button.h>
#import <appkit/Panel.h>
#import <appkit/Matrix.h>
#import <sys/param.h>
#import <defaults.h>
#import <string.h>
#import <libc.h>

#define APPNAME "Launch"

@implementation Main

// The application speaker
id speaker=nil;

extern char loginScript[1024];
extern char logoutScript[1024];

// The auto-launching preferences.
int onauto=0;
char *autodefs[]={"ExecuteAndQuit", "Execute", "Ask", "Nothing", NULL};
int logout=0;
char *logoutdefs[]={"Execute", "Ask", "Nothing", NULL};
#define LO_EXECUTE 0
#define LO_ASK 1
#define LO_NOTHING 2

- appDidInit:sender;
{
	speaker = [NXApp appSpeaker];
	[self readPrefs];
	return self;
}


/* If the user logs out/powers off, we get one of these messages.*/
- app:sender powerOffIn:(int)ms andSave:(int)aFlag
{
	[self ask:logoutScript];
	return self;
}
- powerOff:(NXEvent *)theEvent
{
	[self ask:logoutScript];
	return self;
}
- testLogout:sender
{
	return [self ask:logoutScript];
}

- ask:(char *)script
{
	if (logout == LO_ASK)
	{
		int reply = NXRunAlertPanel("Launch", "Execute script\n%s?", "OK", "No", NULL, script);
		if (reply == NX_ALERTDEFAULT) [self execute:script];
	}
	else
	{
		if (logout == LO_EXECUTE)
			[self execute:script];
	}
	return self;
}


// Execute the script by forking and overlaying, so Launch stays around.
- execute:(char *)script
{
	int pid;
	pid = vfork();
	// Child executes script.
	if (pid==0) execl(script, ".Launch", 0);
	return self;
}

- executeLogin:sender
{
	return [self execute:loginScript];
}

- executeLogout:sender
{
	return [self execute:logoutScript];
}


// Send a message to Edit to open the file.
- open:(char *)script
{
	int msgDelivered, fileOpened; 
	port_t	editport;
	
	// Find Edit's port.
	editport = NXPortFromName("Edit", NULL);
	
	if (editport==PORT_NULL)
	{
		// An unlikely event, but you never know.
		NXRunAlertPanel(APPNAME, "Cannot get Edit's port.", "OK", NULL, NULL);
		[openItem setEnabled:NO];
	}
	else
	{
		// Edit is the receiving app.
		[speaker setSendPort:editport];
		msgDelivered = [speaker openFile:script ok:&fileOpened];
		if ((msgDelivered!=0) || (fileOpened==NO))
			NXRunAlertPanel(APPNAME, "Cannot open script %s", "OK", NULL, NULL, script);
	}
	return self;
}

- openLogin:sender
{
	return [self open:loginScript];
}

- openLogout:sender
{
	return [self open:logoutScript];
}

- readPrefs;
{
	char const *def;
	int i;
	
	def = NXGetDefaultValue(APPNAME, "OnAutoLaunch");
	for (i=0;autodefs[i]!=NULL;i++)
		if (strcmp(autodefs[i], def)==0) onauto = i;
	
	def = NXGetDefaultValue(APPNAME, "OnLogout");
	for (i=0;logoutdefs[i]!=NULL;i++)
		if (strcmp(logoutdefs[i], def)==0) logout = i;
	
	return self;
}

- writePrefs;
{
	NXWriteDefault(APPNAME, "OnAutoLaunch", autodefs[onauto]);
	NXWriteDefault(APPNAME, "OnLogout", logoutdefs[logout]);
	return self;
}

- showPrefs:sender
{
	[onautoSwitch selectCellWithTag:onauto];
	[logoutSwitch selectCellWithTag:logout];
	[prefsPanel makeKeyAndOrderFront:self];
	return self;
}

- okPrefs:sender
{
	onauto = [[onautoSwitch selectedCell] tag];
	logout = [[logoutSwitch selectedCell] tag];
	[self writePrefs];
	return self;
}
@end
