
#import "MyObject.h"
#import "StringList.h"

@implementation MyObject

-awakeFromNib;
{
    static NXDefaultsVector defaults = {
	{"Command", "/etc/ppp/bin/pppd"},
	{NULL}
    };

    NXRegisterDefaults("DialOnDemand", defaults);
    [[fileScroller window] setFrameAutosaveName:"FileWindow"];
    [[command window] setFrameAutosaveName:"PrefPanel"];

    [NXApp setDelegate:self];
    [[Application workspace] beginListeningForApplicationStatusChanges];

    [fileScroller setTarget:self];
    [fileScroller setAction:@selector(clickScroller:)];
    
    strcpy(filePath, [[NXBundle mainBundle] directory]);
    strcat(filePath, "/");
    strcat(filePath, "fileList");
    fileStream = NXOpenTypedStreamForFile(filePath, NX_READONLY);
    if (fileStream == NULL) { 
	fileStream = NXOpenTypedStreamForFile(filePath, NX_WRITEONLY);
	[fileList write:fileStream];
	NXCloseTypedStream(fileStream);
	fileStream = NXOpenTypedStreamForFile(filePath, NX_READONLY);
	if (fileStream == NULL) {
	    NXRunAlertPanel(NULL, "Can't create file list inside app wrapper",
		NULL, NULL, NULL);
	    [NXApp terminate:self];
	}
    }
    [fileList read:fileStream];
    [fileScroller loadColumnZero];
    NXCloseTypedStream(fileStream);
    
    [command setStringValue:NXGetDefaultValue("DialOnDemand", "Command")];
    [fileName selectText:self];
    return self;
}

- addFileName:sender;
{
    if (strlen([fileName stringValue]) > 0)
	[fileList addStringIfAbsent:[fileName stringValue]];
    [fileScroller loadColumnZero];
    fileStream = NXOpenTypedStreamForFile(filePath, NX_WRITEONLY);
    [fileList write:fileStream];
    NXCloseTypedStream(fileStream);
    [fileName selectText:self];
    return self;
}

- delFileName:sender;
{
    [fileList removeString:[fileName stringValue]];
    [fileScroller loadColumnZero];
    fileStream = NXOpenTypedStreamForFile(filePath, NX_WRITEONLY);
    [fileList write:fileStream];
    NXCloseTypedStream(fileStream);
    [fileName selectText:self];
    return self;
}

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

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

- commandDefault:sender;
{
    NXWriteDefault("DialOnDemand", "Command", [command stringValue]);
    return self;
}

- selectCommand:sender;
{
        id openPanel = [OpenPanel new];

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

    return self;
}

- selectApp:sender;
{
        id openPanel = [OpenPanel new];

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

    return self;
}

- infoPanel:sender;
{
    if (!infoPanel) {
	[NXApp loadNibSection:"Info.nib" owner:self];
    }
    [infoPanel orderFront:self];
    return self;
}

-appWillTerminate:sender;
{
    [[Application workspace] endListeningForApplicationStatusChanges];
    return (id)1;
}

-app:sender applicationWillLaunch:(const char *)appName;
{
    int i;
    
    for (i = 0; i < [fileList count]; i++) {
	if (strstr(appName, [fileList stringAt:i]) != NULL) {
	    system(NXGetDefaultValue("DialOnDemand", "Command"));
	}
    }
    return self;
}

@end
