1//
2//  MyDocument.m
3//  PythonLauncher
4//
5//  Created by Jack Jansen on Fri Jul 19 2002.
6//  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7//
8
9#import "MyDocument.h"
10#import "MyAppDelegate.h"
11#import "doscript.h"
12
13@implementation MyDocument
14
15- (id)init
16{
17    self = [super init];
18    if (self) {
19
20        // Add your subclass-specific initialization here.
21        // If an error occurs here, send a [self dealloc] message and return nil.
22        script = [@"<no script>.py" retain];
23        filetype = [@"Python Script" retain];
24        settings = NULL;
25    }
26    return self;
27}
28
29- (NSString *)windowNibName
30{
31    // Override returning the nib file name of the document
32    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
33    return @"MyDocument";
34}
35
36- (void)close
37{
38    NSApplication *app = [NSApplication sharedApplication];
39    [super close];
40    if ([(MyAppDelegate*)[app delegate] shouldTerminate])
41        [app terminate: self];
42}
43
44- (void)load_defaults
45{
46    settings = [FileSettings newSettingsForFileType: filetype];
47}
48
49- (void)update_display
50{
51    [interpreter setStringValue: [settings interpreter]];
52    [honourhashbang setState: [settings honourhashbang]];
53    [debug setState: [settings debug]];
54    [verbose setState: [settings verbose]];
55    [inspect setState: [settings inspect]];
56    [optimize setState: [settings optimize]];
57    [nosite setState: [settings nosite]];
58    [tabs setState: [settings tabs]];
59    [others setStringValue: [settings others]];
60    [scriptargs setStringValue: [settings scriptargs]];
61    [with_terminal setState: [settings with_terminal]];
62
63    [commandline setStringValue: [settings commandLineForScript: script]];
64}
65
66- (void)update_settings
67{
68    [settings updateFromSource: self];
69}
70
71- (BOOL)run
72{
73    const char *cmdline;
74    int sts;
75
76     cmdline = [[settings commandLineForScript: script] UTF8String];
77   if ([settings with_terminal]) {
78        sts = doscript(cmdline);
79    } else {
80        sts = system(cmdline);
81    }
82    if (sts) {
83        NSLog(@"Exit status: %d\n", sts);
84        return NO;
85    }
86    return YES;
87}
88
89- (void)windowControllerDidLoadNib:(NSWindowController *) aController
90{
91    [super windowControllerDidLoadNib:aController];
92    // Add any code here that need to be executed once the windowController has loaded the document's window.
93    [self load_defaults];
94    [self update_display];
95}
96
97- (NSData *)dataRepresentationOfType:(NSString *)aType
98{
99    // Insert code here to write your document from the given data.  You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
100    return nil;
101}
102
103- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
104{
105    // Insert code here to read your document from the given data.  You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
106    BOOL show_ui;
107
108    // ask the app delegate whether we should show the UI or not.
109    show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
110    [script release];
111    script = [fileName retain];
112    [filetype release];
113    filetype = [type retain];
114    settings = [FileSettings newSettingsForFileType: filetype];
115    if (show_ui) {
116        [self update_display];
117        return YES;
118    } else {
119        [self run];
120	[self performSelector:@selector(close) withObject:nil afterDelay:0.0];
121        return YES;
122    }
123}
124
125- (IBAction)do_run:(id)sender
126{
127    [self update_settings];
128    [self update_display];
129    if ([self run])
130        [self close];
131}
132
133- (IBAction)do_cancel:(id)sender
134{
135    [self close];
136}
137
138
139- (IBAction)do_reset:(id)sender
140{
141    [settings reset];
142    [self update_display];
143}
144
145- (IBAction)do_apply:(id)sender
146{
147    [self update_settings];
148    [self update_display];
149}
150
151// FileSettingsSource protocol
152- (NSString *) interpreter { return [interpreter stringValue];};
153- (BOOL) honourhashbang { return [honourhashbang state];};
154- (BOOL) debug { return [debug state];};
155- (BOOL) verbose { return [verbose state];};
156- (BOOL) inspect { return [inspect state];};
157- (BOOL) optimize { return [optimize state];};
158- (BOOL) nosite { return [nosite state];};
159- (BOOL) tabs { return [tabs state];};
160- (NSString *) others { return [others stringValue];};
161- (NSString *) scriptargs { return [scriptargs stringValue];};
162- (BOOL) with_terminal { return [with_terminal state];};
163
164// Delegates
165- (void)controlTextDidChange:(NSNotification *)aNotification
166{
167    [self update_settings];
168    [self update_display];
169};
170
171@end
172