/* Program: MOTD - 'Motto of the day' window
 *
 * By: Christopher Lane (lane@sumex-aim.stanford.edu)
 *     Symbolic Systems Resources Group
 *     Knowledge Systems Laboratory
 *     Stanford University
 *
 * Contributors:
 *
 *    Izumi Ohzawa (izumi@violet.berkeley.edu)
 *    Group in Neurobiology/School of Optometry
 *    University of California, Berkeley
 *
 *    Steve Hayman (sahayman@cs.indiana.edu)
 *    Computer Science Department Workstation Manager
 *
 * Date:  5 November 1992
 *
 * Copyright: 1989, 1990, 1991 & 1992 by The Leland Stanford Junior University.
 * This program may be distributed without restriction for non-commercial use.
 */

#import "MOTD.h"
#import "DefaultsTable.h"

#define MAILSND "Mail"
#define NEWMAILSND "NewMail"
#define DEFAULTSFILE "Defaults"

#define MAILMESSAGE NXLocalizedString("You have mail.", NULL, "User has unread mail")
#define NEWMAILMESSAGE NXLocalizedString("You have new mail.", NULL, "User has new mail")
#define MAILTITLE NXLocalizedString("Mail Alert", NULL, "Mail alert panel title")

#define QUITBUTTONTITLE NXLocalizedString("Quit", NULL, "User wants to quit and proceed with login")
#define HOLDBUTTONTITLE NXLocalizedString("Hold (%3d)", NULL, "User wants to hold (and format for seconds)")
#define CANCELBUTTONTITLE NXLocalizedString("Cancel Login", NULL, "User wants to quit and not login")

#define SECOND ((double) 1.0)

extern void timer(DPSTimedEntry, double, id);
	
@implementation MOTD

+ new
{
	char pathnamebuf[MAXPATHLEN];

	self = [super new];

	bundle = [NXBundle bundleForClass:[self class]];
	
	if ([bundle getPath:pathnamebuf forResource:DEFAULTSFILE ofType:"strings"])
		[[[DefaultsTable alloc] initFromFile:pathnamebuf] registerDefaults:[self appName]];
	
	file = getDefault("File");
	
	wait = getIntDefault("Wait");

	return self;
}

- appDidInit:sender
{
	[[self appIcon] orderOut:self];

	if (loginHook) (void) [self activateSelf:YES];

	if (!do_motd) wait = getIntDefault("MailWait"); /* short timeout */  

	timedEntry = DPSAddTimedEntry(SECOND, (DPSTimedEntryProc) &timer, self, NX_BASETHRESHOLD);

	return self;
}

- windowWillClose:sender { return [self terminate:sender]; }

- free
{
	if (timedEntry != NULL) DPSRemoveTimedEntry(timedEntry);
    
	return [super free];
}

- setMailPanel:anObject
{
	char pathnamebuf[MAXPATHLEN];

	[(mailPanel = anObject) setTitle:strcat(strcpy(pathnamebuf, MAILDIR), name)];

	return self;
}

- setMailText:anObject
{
	const char *value;
	char pathnamebuf[MAXPATHLEN];

	mailText = anObject;
	
	if (maillevel != NONE) {
		[mailText setStringValue:(maillevel == NEW) ? NEWMAILMESSAGE : MAILMESSAGE];
		[[mailPanel display] orderFront:nil];
		if ((value = getSystemDefault("SystemAlert")) != NULL && strcasecmp(value, "Panel") != EQ) {
			if ([bundle getPath:pathnamebuf forResource:(maillevel == NEW) ? NEWMAILSND : MAILSND ofType:"snd"])
				 SNDPlaySoundfile(pathnamebuf, 0);
			}
		}

	return self;
}

- setMailTitle:anObject { [(mailTitle = anObject) setStringValue:MAILTITLE]; return self; }

- txt
{
	NXStream *stream;

	if ((stream = NXMapFile(file, NX_READONLY)) == NULL) return nil;
	
	[docView readText:stream];

	NXCloseMemory(stream, NX_FREEBUFFER);

	return self;
}

- rtf
{
	NXStream *stream;

	if ((stream = NXMapFile(file, NX_READONLY)) == NULL) return nil;
	
	[docView readRichText:stream];

	NXCloseMemory(stream, NX_FREEBUFFER);

	return self;
}

- rtfd
{
	if([docView openRTFDFrom:file] != NX_RTFDErrorNone) return nil;
	
	return self;
}

- setWindow:anObject
{
	window = anObject;

	if (do_motd) [[window setTitle:file] makeKeyAndOrderFront:self];

	return self;
}

- setDocView:anObject
{
	const char *type;
	SEL method, action = @selector(txt);
 
	docView = anObject;
	
	if (!do_motd) return self;	/* Don't do it unless we have a MOTD window */

	/* Can't use [workspace getInfoForFile:file application:&application type:&type]) in LoginHook */

	if ((type = rindex(file, '.')) != NULL) {
		if ((method = sel_getUid(++type)) != 0 && [self respondsTo:method]) action = method;
		}
		
	if([self perform:action] == nil) [self terminate:self];

	return self;
}

- quitButton { return quitButton; }
- setQuitButton:anObject { [(quitButton = anObject) setTitle:QUITBUTTONTITLE]; return self; }

- holdButton { return holdButton; }

- setHoldButton:anObject
{
	[[(holdButton = anObject) setTitle:HOLDBUTTONTITLE] setState:getBoolDefault("Hold")];
	
	return self;
}

- cancelButton { return cancelButton; }
- setCancelButton:anObject { [(cancelButton = anObject) setTitle:CANCELBUTTONTITLE]; return self; }

- (const char *) userName { return name; }

- setUserName:(const char *) pw_name { name = NXCopyStringBuffer(pw_name); return self; }

- (int) UID { return uid; }
- setUID:(int) pw_uid { uid = pw_uid; return self; }

- (BOOL) do_Motd { return do_motd; }
- setDo_Motd:(BOOL) aBoolean { do_motd = aBoolean; return self; }

- (MailLevel) mailLevel { return maillevel; }
- setMailLevel:(MailLevel) aMailLevel { maillevel = aMailLevel; return self; }

- (const char *) file { return file; }
- setFile:(const char *) aString { file = aString; return self; }

- (BOOL) loginHook { return loginHook; }
- setLoginHook:(BOOL) aBoolean { loginHook = aBoolean; return self; }

- (int) wait { return wait; }
- setWait:(int) anInteger { wait = anInteger; return self; }

- cancelLogin:sender
{
	[self free];

	exit(EXIT_FAILURE);
	
	return self; /* NOT REACHED */
}

@end
