#import "MyObject.h"

@implementation MyObject

- init;
{
    NSMutableDictionary *defaults = [NSMutableDictionary dictionary];

    [super init];
    [defaults setObject:@"/etc/ppp/bin/pppd" forKey:@"Command"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
    return self;
}

- (void)dealloc;
{
    [filePath release];
    [fileList release];
    [super dealloc];
}

- (void)awakeFromNib;
{
    [[fileScroller window] setFrameAutosaveName:@"FileWindow"];
    [[command window] setFrameAutosaveName:@"PrefPanel"];

    [NSApp setDelegate:self];
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
        selector:@selector(appNotice:)
        name:NSWorkspaceWillLaunchApplicationNotification
        object:nil];

    [fileScroller setDelegate:self];
    [fileScroller setTarget:self];
    [fileScroller setAction:@selector(clickScroller:)];

    // if there isn't a fileList already, this will screw up
    filePath = [[[NSBundle mainBundle] pathForResource:@"fileList" ofType:@""] retain];
    NS_DURING
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        fileList = [[NSUnarchiver unarchiveObjectWithFile:filePath] retain];
    } else {
        fileList = [[NSMutableSet setWithCapacity:10] retain];
        [NSArchiver archiveRootObject:fileList toFile:filePath];
    }
    NS_HANDLER
    NSRunAlertPanel(0, @"Unable to open file name archive inside app wrapper", @"OK", 0, 0);
    NSLog(@"bit of a problem");
    [NSApp terminate:self];
    NS_ENDHANDLER

    [fileScroller loadColumnZero];
    
    [command setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"Command"]];
    [fileName selectText:self];
}

- (void)addFileName:sender;
{
    if ([[fileName stringValue] length] > 0)
	[fileList addObject:[fileName stringValue]];
    [fileScroller loadColumnZero];
    [NSArchiver archiveRootObject:fileList toFile:filePath];
    [fileName selectText:self];
}

- (void)delFileName:sender;
{
    [fileList removeObject:[fileName stringValue]];
    [fileScroller loadColumnZero];
    [NSArchiver archiveRootObject:fileList toFile:filePath];
    [fileName selectText:self];
}

- (void)cancelFileOp:sender;
{
    [fileName setStringValue:@""];
    [fileName selectText:self];
}

- (void)clickScroller:sender;
{
    if ([fileScroller selectedCell]) {
	[fileName setStringValue:[[fileScroller selectedCell] stringValue]];
    }
}

- (void)commandDefault:sender;
{
    [[NSUserDefaults standardUserDefaults] setObject:[command stringValue] forKey:@"Command"];
    [[sender window] orderOut:self];
}

- (void)selectCommand:sender;
{
    id openPanel = [NSOpenPanel openPanel];

    if ([openPanel runModal] == NSOKButton) {
	[command setStringValue:[openPanel filename]];
    }
}

- (void)selectApp:sender;
{
    id openPanel = [NSOpenPanel openPanel];

    [openPanel setCanChooseDirectories:YES];
    if ([openPanel runModal] == NSOKButton) {
	[fileName setStringValue:[openPanel filename]];
    }
}

- (void)infoPanel:sender;
{
    if (!infoPanel) {
	[NSBundle loadNibNamed:@"Info.nib" owner:self];
    }
    [infoPanel orderFront:self];
}

- (BOOL)applicationShouldTerminate:(id)sender;
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
        name:NSWorkspaceWillLaunchApplicationNotification
        object:nil];
    return YES;
}

- appNotice:(NSNotification *)notification;
{
    NSEnumerator *i = [fileList objectEnumerator];
    id string;

    NSLog(@"appNotice:%@", [[notification userInfo] objectForKey:@"NSApplicationName"]);
    while (string = [i nextObject]) {
//        NSRange range = [[[[notification object] mainMenu] title]
//            rangeOfString:string];
        NSRange range = [[[notification userInfo] objectForKey:@"NSApplicationName"]
            rangeOfString:string];
	if (range.length != 0) {
	    system([[[NSUserDefaults standardUserDefaults] objectForKey:@"Command"] cString]);
	}
    }
    return self;
}

- (void)browser:sender createRowsForColumn:(int)column inMatrix:(NSMatrix *)matrix;
{
    NSEnumerator *cellLoop, *fileLoop = [fileList objectEnumerator];
    NSString *string = nil;
    NSBrowserCell *theCell = nil;

    // Set matrix to have the right number of cells.
    [matrix renewRows:[fileList count] columns:1];

    // For each cell set its value, set whether it is a leaf or not
    cellLoop = [[matrix cells] objectEnumerator];
    while((string = [fileLoop nextObject])
          && (theCell = [cellLoop nextObject])) {
        [theCell setStringValue:string];
        [theCell setLeaf:YES];
    }
}

@end
