/*
 *    Filename:	Passwd.m 
 *    Created :	Sat Jan  4 20:53:02 1992 
 *    Author  :	Vince DeMarco
 *		<vince@whatnxt.cuc.ab.ca>
 */


/* Generated by Interface Builder */

#import "Passwd.h"
#import <appkit/Application.h>
#import <appkit/Window.h>
#import <appkit/View.h>
#import <appkit/TextField.h>
#import <appkit/publicWraps.h>
#import <appkit/Menu.h>
#import <appkit/defaults.h>

#import <strings.h>
#import <sys/time.h>
#import <libc.h>

#import "passwd.h"

@implementation Passwd

static void LocalDateAndTime(char *buffer,int  maxBufferSize, const time_t *time) 
{
    const char *format;
    
    if ( (format = NXGetDefaultValue([NXApp appName], "NXDateAndTime")) == NULL){
        format = "%a %b %d %H:%M:%S %Z %Y";
    }
    strftime(buffer,maxBufferSize,format,localtime(time));
}

- init
{
    self = [super init];
    passwdEntry = getpwnam(NXUserName());
    nubmerOfattempts = 0;
    return self;
}

- free
{
    [window free];
    return [super free];
}

- passwdEntered:sender
{
    char *encoded = crypt((char *)[sender stringValue],(char *)passwdEntry->pw_passwd);
    [passwdText setStringValue:NULL];

    if (encoded){
	if (! strcmp(encoded,passwdEntry->pw_passwd)){
	    nubmerOfattempts = 0;
	    [self enableMainMenu];
	    [self orderWindowOut];
	    [NXApp hide:self];
	}else{
	    [self clearPasswdField];
	    [self incorrectPasswd];
	}
    }else{
	[self orderWindowOut];
    }
    return self;
}

- requestPasswd:sender
{
    if ([NXApp isHidden]){
	[NXApp unhide:self];
    }
    [self orderWindowFront];
    return self;
}

- incorrectPasswd
{
#define OFFSET 100.0
#define FRAMETIME 30
#define UPDATE   [window display]; \
		 [window flushWindow]; \
		 NXPing(); 
		     

    NXBeep();
    nubmerOfattempts++;
    [attemptsField setIntValue: nubmerOfattempts];

    [contentBox moveTo: xpos-OFFSET : ypos];
    UPDATE;
    [contentBox moveTo: xpos+OFFSET : ypos];
    UPDATE;
    [contentBox moveTo: xpos-OFFSET : ypos];
    UPDATE;
    [contentBox moveTo: xpos+OFFSET : ypos];
    UPDATE;
    [contentBox moveTo: xpos : ypos];
    UPDATE;

    return self;
}

- clearPasswdField
{
    [passwdText setStringValue:NULL];
    [passwdText selectText:self];
    return self;
}

- orderWindowFront
{
    char infoString[1024];
    char timebuffer[50];
    time_t timeVal = time(0);
    
    [attemptsField setIntValue: nubmerOfattempts];

    if (![window isVisible]){
	[self disableMainMenu];

	if (!window){
	    [self createWindow];	
	}

	LocalDateAndTime(timebuffer,50,&timeVal);
	sprintf(infoString,"Screen has been locked by %s\n on %s",passwdEntry->pw_gecos, timebuffer);
	[infoText setStringValue:(const char*)infoString];
	[self clearPasswdField];
	setWindowLevel([window windowNum],51);
	[window makeKeyAndOrderFront:self];
    }
    return self;
}

- orderWindowOut
{
    [window orderOut:self];
    return self;
}

- createWindow
{
    NXRect windowRect;
    NXRect contRect;
    NXSize screenSize;

    if (!contentBox){
	[NXApp loadNibSection:"Passwd.nib" owner:self withNames:NO fromZone:[self zone]];
    }

    [NXApp getScreenSize:&screenSize];
    [contentBox getFrame:&contRect];

    windowRect.origin.x    = windowRect.origin.y = 0;
    windowRect.size.width  = screenSize.width;
    windowRect.size.height = screenSize.height;
    xpos = (screenSize.width  - contRect.size.width ) / 2;
    ypos = (screenSize.height - contRect.size.height) / 2;

    window = [[Window allocFromZone:[self zone]]
	  initContent:&windowRect style:NX_PLAINSTYLE backing:NX_BUFFERED buttonMask:0 defer:NO];
    
    [contentBox removeFromSuperview];               
    [contentBox moveTo: xpos : ypos];
    [[window contentView] addSubview : contentBox];
    
    [window useOptimizedDrawing:YES];
    [window display];

    return self;
}

- disableMainMenu
{
    id matrix = [[NXApp mainMenu] itemList];
    [matrix setEnabled:NO];
    return self;
}

- enableMainMenu
{
    id matrix = [[NXApp mainMenu] itemList];
    [matrix setEnabled:YES];
    return self;
}


@end

