blob: 2147004c61aa3a8a407a920c8f24e676f527f730 [file] [log] [blame]
Jack Jansen3bbb6172002-07-29 21:36:35 +00001//
2// FileSettings.m
3// PythonLauncher
4//
5// Created by Jack Jansen on Sun Jul 21 2002.
6// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7//
8
9#import "FileSettings.h"
10
11@implementation FileSettings
12+ (id)getDefaultsForFileType: (NSString *)filetype
13{
14 static FileSettings *default_py, *default_pyw, *default_pyc;
15 FileSettings **curdefault;
16
17 if ([filetype isEqualToString: @"Python Script"]) {
18 curdefault = &default_py;
19 } else if ([filetype isEqualToString: @"Python GUI Script"]) {
20 curdefault = &default_pyw;
21 } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
22 curdefault = &default_pyc;
23 } else {
24 NSLog(@"Funny File Type: %@\n", filetype);
25 curdefault = &default_py;
26 filetype = @"Python Script";
27 }
28 if (!*curdefault) {
29 *curdefault = [[FileSettings new] init];
30 [*curdefault factorySettingsForFileType: filetype];
31 [*curdefault updateFromUserDefaults: filetype];
32 }
33 return *curdefault;
34}
35
36+ (id)newSettingsForFileType: (NSString *)filetype
37{
38 FileSettings *cur;
39
40 cur = [[FileSettings new] init];
41 [cur initWithFileSettings: [FileSettings getDefaultsForFileType: filetype]];
42 return cur;
43}
44
45- (id)init
46{
47 self = [super init];
48 return [self factorySettingsForFileType: @"Python Script"];
49}
50
51- (id)factorySettingsForFileType: (NSString *)filetype
52{
53 debug = NO;
54 verbose = NO;
55 inspect = NO;
56 optimize = NO;
57 nosite = NO;
58 tabs = NO;
59 others = @"";
60 if ([filetype isEqualToString: @"Python Script"] ||
61 [filetype isEqualToString: @"Python Bytecode Document"]) {
Jack Jansenbe3e1f72002-08-01 21:14:06 +000062 interpreter = @"/Library/Frameworks/Python.framework/Versions/Current/bin/python";
Jack Jansen3bbb6172002-07-29 21:36:35 +000063 with_terminal = YES;
64 } else if ([filetype isEqualToString: @"Python GUI Script"]) {
Jack Jansenbe3e1f72002-08-01 21:14:06 +000065 interpreter = @"/Library/Frameworks/Python.framework/Versions/Current/Resources/Python.app/Contents/MacOS/python";
Jack Jansen3bbb6172002-07-29 21:36:35 +000066 with_terminal = NO;
67 } else {
68 NSLog(@"Funny File Type: %@\n", filetype);
69 }
70 origsource = NULL;
71 return self;
72}
73
74- (id)initWithFileSettings: (FileSettings *)source
75{
76 interpreter = [source->interpreter retain];
77 debug = source->debug;
78 verbose = source->verbose;
79 inspect = source->inspect;
80 optimize = source->optimize;
81 nosite = source->nosite;
82 tabs = source->tabs;
83 others = [source->others retain];
84 with_terminal = source->with_terminal;
85
86 origsource = [source retain];
87 return self;
88}
89
90- (void)saveDefaults
91{
92 [origsource updateFromSource: self];
93}
94
95- (void)updateFromSource: (id <FileSettingsSource>)source
96{
97 interpreter = [[source interpreter] retain];
98 debug = [source debug];
99 verbose = [source verbose];
100 inspect = [source inspect];
101 optimize = [source optimize];
102 nosite = [source nosite];
103 tabs = [source tabs];
104 others = [[source others] retain];
105 with_terminal = [source with_terminal];
106 if (!origsource) {
107 NSUserDefaults *defaults;
108 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
109 interpreter, @"interpreter",
110 [NSNumber numberWithBool: debug], @"debug",
111 [NSNumber numberWithBool: verbose], @"verbose",
112 [NSNumber numberWithBool: inspect], @"inspect",
113 [NSNumber numberWithBool: optimize], @"optimize",
114 [NSNumber numberWithBool: nosite], @"nosite",
115 [NSNumber numberWithBool: nosite], @"nosite",
116 others, @"others",
117 [NSNumber numberWithBool: with_terminal], @"with_terminal",
118 nil];
119 defaults = [NSUserDefaults standardUserDefaults];
120 [defaults setObject: dict forKey: prefskey];
121 }
122}
123
124- (void)updateFromUserDefaults: (NSString *)filetype
125{
126 NSUserDefaults *defaults;
127 NSDictionary *dict;
128 id value;
129
130 prefskey = [filetype retain];
131 defaults = [NSUserDefaults standardUserDefaults];
132 dict = [defaults dictionaryForKey: filetype];
133 if (!dict)
134 return;
135 value = [dict objectForKey: @"interpreter"];
136 if (value) interpreter = [value retain];
137 value = [dict objectForKey: @"debug"];
138 if (value) debug = [value boolValue];
139 value = [dict objectForKey: @"verbose"];
140 if (value) verbose = [value boolValue];
141 value = [dict objectForKey: @"inspect"];
142 if (value) inspect = [value boolValue];
143 value = [dict objectForKey: @"optimize"];
144 if (value) optimize = [value boolValue];
145 value = [dict objectForKey: @"nosite"];
146 if (value) nosite = [value boolValue];
147 value = [dict objectForKey: @"nosite"];
148 if (value) tabs = [value boolValue];
149 value = [dict objectForKey: @"others"];
150 if (value) others = [value retain];
151 value = [dict objectForKey: @"with_terminal"];
152 if (value) with_terminal = [value boolValue];
153}
154
155- (NSString *)commandLineForScript: (NSString *)script
156{
157 return [NSString stringWithFormat:
158 @"\"%@\"%s%s%s%s%s%s %@ \"%@\" %s",
159 interpreter,
160 debug?" -d":"",
161 verbose?" -v":"",
162 inspect?" -i":"",
163 optimize?" -O":"",
164 nosite?" -S":"",
165 tabs?" -t":"",
166 others,
167 script,
Jack Jansenb7276cd2002-07-31 13:15:59 +0000168 with_terminal? "&& exit" : " &"];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000169}
170
171// FileSettingsSource protocol
172- (NSString *) interpreter { return interpreter;};
173- (BOOL) debug { return debug;};
174- (BOOL) verbose { return verbose;};
175- (BOOL) inspect { return inspect;};
176- (BOOL) optimize { return optimize;};
177- (BOOL) nosite { return nosite;};
178- (BOOL) tabs { return tabs;};
179- (NSString *) others { return others;};
180- (BOOL) with_terminal { return with_terminal;};
181
182@end