blob: 7b28daae017782d0fd80bc8a306fc1ec66ceab9f [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
Jack Jansen2095c062002-11-25 13:11:06 +000012
13+ (id)getFactorySettingsForFileType: (NSString *)filetype
14{
15 static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
16 FileSettings **curdefault;
17
18 if ([filetype isEqualToString: @"Python Script"]) {
19 curdefault = &fsdefault_py;
20 } else if ([filetype isEqualToString: @"Python GUI Script"]) {
21 curdefault = &fsdefault_pyw;
22 } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
23 curdefault = &fsdefault_pyc;
24 } else {
25 NSLog(@"Funny File Type: %@\n", filetype);
26 curdefault = &fsdefault_py;
27 filetype = @"Python Script";
28 }
29 if (!*curdefault) {
30 *curdefault = [[FileSettings new] initForFSDefaultFileType: filetype];
31 }
32 return *curdefault;
33}
34
Jack Jansen3bbb6172002-07-29 21:36:35 +000035+ (id)getDefaultsForFileType: (NSString *)filetype
36{
37 static FileSettings *default_py, *default_pyw, *default_pyc;
38 FileSettings **curdefault;
39
40 if ([filetype isEqualToString: @"Python Script"]) {
41 curdefault = &default_py;
42 } else if ([filetype isEqualToString: @"Python GUI Script"]) {
43 curdefault = &default_pyw;
44 } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
45 curdefault = &default_pyc;
46 } else {
47 NSLog(@"Funny File Type: %@\n", filetype);
48 curdefault = &default_py;
49 filetype = @"Python Script";
50 }
51 if (!*curdefault) {
Jack Jansen2095c062002-11-25 13:11:06 +000052 *curdefault = [[FileSettings new] initForDefaultFileType: filetype];
Jack Jansen3bbb6172002-07-29 21:36:35 +000053 }
54 return *curdefault;
55}
56
57+ (id)newSettingsForFileType: (NSString *)filetype
58{
59 FileSettings *cur;
60
Jack Jansen2095c062002-11-25 13:11:06 +000061 cur = [FileSettings new];
62 [cur initForFileType: filetype];
63 return [cur retain];
Jack Jansen3bbb6172002-07-29 21:36:35 +000064}
65
66- (id)initWithFileSettings: (FileSettings *)source
67{
Jack Jansen2095c062002-11-25 13:11:06 +000068 self = [super init];
69 if (!self) return self;
70
Jack Jansen3bbb6172002-07-29 21:36:35 +000071 interpreter = [source->interpreter retain];
Jack Jansen3d3b7462003-02-17 15:40:00 +000072 honourhashbang = source->honourhashbang;
Jack Jansen3bbb6172002-07-29 21:36:35 +000073 debug = source->debug;
74 verbose = source->verbose;
75 inspect = source->inspect;
76 optimize = source->optimize;
77 nosite = source->nosite;
78 tabs = source->tabs;
79 others = [source->others retain];
80 with_terminal = source->with_terminal;
Jack Jansen2095c062002-11-25 13:11:06 +000081 prefskey = source->prefskey;
82 if (prefskey) [prefskey retain];
Jack Jansen3bbb6172002-07-29 21:36:35 +000083
Jack Jansen3bbb6172002-07-29 21:36:35 +000084 return self;
85}
86
Jack Jansen2095c062002-11-25 13:11:06 +000087- (id)initForFileType: (NSString *)filetype
Jack Jansen3bbb6172002-07-29 21:36:35 +000088{
Jack Jansen2095c062002-11-25 13:11:06 +000089 FileSettings *defaults;
90
91 defaults = [FileSettings getDefaultsForFileType: filetype];
92 self = [self initWithFileSettings: defaults];
93 origsource = [defaults retain];
94 return self;
95}
96
97//- (id)init
98//{
99// self = [self initForFileType: @"Python Script"];
100// return self;
101//}
102
103- (id)initForFSDefaultFileType: (NSString *)filetype
104{
105 int i;
106 NSString *filename;
107 NSDictionary *dict;
Jack Jansen2095c062002-11-25 13:11:06 +0000108 static NSDictionary *factorySettings;
109
110 self = [super init];
111 if (!self) return self;
112
113 if (factorySettings == NULL) {
114 NSBundle *bdl = [NSBundle mainBundle];
115 NSString *path = [ bdl pathForResource: @"factorySettings"
116 ofType: @"plist"];
117 factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
118 path] retain];
119 if (factorySettings == NULL) {
120 NSLog(@"Missing %@", path);
121 return NULL;
122 }
123 }
124 dict = [factorySettings objectForKey: filetype];
125 if (dict == NULL) {
126 NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
127 interpreter = [@"no default found" retain];
128 return NULL;
129 }
130 [self applyValuesFromDict: dict];
131 interpreters = [dict objectForKey: @"interpreter_list"];
132 interpreter = NULL;
133 for (i=0; i < [interpreters count]; i++) {
134 filename = [interpreters objectAtIndex: i];
135 filename = [filename stringByExpandingTildeInPath];
136 if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
137 interpreter = [filename retain];
138 break;
139 }
140 }
141 if (interpreter == NULL)
142 interpreter = [@"no default found" retain];
143 origsource = NULL;
144 return self;
145}
146
147- (void)applyUserDefaults: (NSString *)filetype
148{
149 NSUserDefaults *defaults;
150 NSDictionary *dict;
151
152 defaults = [NSUserDefaults standardUserDefaults];
153 dict = [defaults dictionaryForKey: filetype];
154 if (!dict)
155 return;
156 [self applyValuesFromDict: dict];
157}
158
159- (id)initForDefaultFileType: (NSString *)filetype
160{
161 FileSettings *fsdefaults;
162
163 fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
164 self = [self initWithFileSettings: fsdefaults];
165 if (!self) return self;
Jack Jansenf044e092002-12-26 22:10:53 +0000166 interpreters = [fsdefaults->interpreters retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000167 [self applyUserDefaults: filetype];
168 prefskey = [filetype retain];
169 return self;
170}
171
172- (void)reset
173{
174 if (origsource) {
175 [self updateFromSource: origsource];
176 } else {
177 FileSettings *fsdefaults;
178 fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
179 [self updateFromSource: fsdefaults];
180 }
Jack Jansen3bbb6172002-07-29 21:36:35 +0000181}
182
183- (void)updateFromSource: (id <FileSettingsSource>)source
184{
185 interpreter = [[source interpreter] retain];
Jack Jansen3d3b7462003-02-17 15:40:00 +0000186 honourhashbang = [source honourhashbang];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000187 debug = [source debug];
188 verbose = [source verbose];
189 inspect = [source inspect];
190 optimize = [source optimize];
191 nosite = [source nosite];
192 tabs = [source tabs];
193 others = [[source others] retain];
194 with_terminal = [source with_terminal];
Jack Jansen2095c062002-11-25 13:11:06 +0000195 // And if this is a user defaults object we also save the
196 // values
Jack Jansen3bbb6172002-07-29 21:36:35 +0000197 if (!origsource) {
198 NSUserDefaults *defaults;
199 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
200 interpreter, @"interpreter",
Jack Jansen3d3b7462003-02-17 15:40:00 +0000201 [NSNumber numberWithBool: honourhashbang], @"honourhashbang",
Jack Jansen3bbb6172002-07-29 21:36:35 +0000202 [NSNumber numberWithBool: debug], @"debug",
203 [NSNumber numberWithBool: verbose], @"verbose",
204 [NSNumber numberWithBool: inspect], @"inspect",
205 [NSNumber numberWithBool: optimize], @"optimize",
206 [NSNumber numberWithBool: nosite], @"nosite",
207 [NSNumber numberWithBool: nosite], @"nosite",
208 others, @"others",
209 [NSNumber numberWithBool: with_terminal], @"with_terminal",
210 nil];
211 defaults = [NSUserDefaults standardUserDefaults];
212 [defaults setObject: dict forKey: prefskey];
213 }
214}
215
Jack Jansen2095c062002-11-25 13:11:06 +0000216- (void)applyValuesFromDict: (NSDictionary *)dict
Jack Jansen3bbb6172002-07-29 21:36:35 +0000217{
Jack Jansen3bbb6172002-07-29 21:36:35 +0000218 id value;
219
Jack Jansen3bbb6172002-07-29 21:36:35 +0000220 value = [dict objectForKey: @"interpreter"];
221 if (value) interpreter = [value retain];
Jack Jansen3d3b7462003-02-17 15:40:00 +0000222 value = [dict objectForKey: @"honourhashbang"];
223 if (value) honourhashbang = [value boolValue];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000224 value = [dict objectForKey: @"debug"];
225 if (value) debug = [value boolValue];
226 value = [dict objectForKey: @"verbose"];
227 if (value) verbose = [value boolValue];
228 value = [dict objectForKey: @"inspect"];
229 if (value) inspect = [value boolValue];
230 value = [dict objectForKey: @"optimize"];
231 if (value) optimize = [value boolValue];
232 value = [dict objectForKey: @"nosite"];
233 if (value) nosite = [value boolValue];
234 value = [dict objectForKey: @"nosite"];
235 if (value) tabs = [value boolValue];
236 value = [dict objectForKey: @"others"];
237 if (value) others = [value retain];
238 value = [dict objectForKey: @"with_terminal"];
239 if (value) with_terminal = [value boolValue];
240}
241
242- (NSString *)commandLineForScript: (NSString *)script
243{
Jack Jansen3d3b7462003-02-17 15:40:00 +0000244 NSString *cur_interp = NULL;
245 char hashbangbuf[1024];
246 FILE *fp;
247 char *p;
248
249 if (honourhashbang &&
250 (fp=fopen([script cString], "r")) &&
251 fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
252 strncmp(hashbangbuf, "#!", 2) == 0 &&
253 (p=strchr(hashbangbuf, '\n'))) {
254 *p = '\0';
255 p = hashbangbuf + 2;
256 while (*p == ' ') p++;
257 cur_interp = [NSString stringWithCString: p];
258 }
259 if (!cur_interp)
260 cur_interp = interpreter;
261
Jack Jansen3bbb6172002-07-29 21:36:35 +0000262 return [NSString stringWithFormat:
263 @"\"%@\"%s%s%s%s%s%s %@ \"%@\" %s",
Jack Jansen3d3b7462003-02-17 15:40:00 +0000264 cur_interp,
Jack Jansen3bbb6172002-07-29 21:36:35 +0000265 debug?" -d":"",
266 verbose?" -v":"",
267 inspect?" -i":"",
268 optimize?" -O":"",
269 nosite?" -S":"",
270 tabs?" -t":"",
271 others,
272 script,
Jack Jansenf044e092002-12-26 22:10:53 +0000273 with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000274}
275
Jack Jansenf044e092002-12-26 22:10:53 +0000276- (NSArray *) interpreters { return interpreters;};
277
Jack Jansen3bbb6172002-07-29 21:36:35 +0000278// FileSettingsSource protocol
279- (NSString *) interpreter { return interpreter;};
Jack Jansen3d3b7462003-02-17 15:40:00 +0000280- (BOOL) honourhashbang { return honourhashbang; };
Jack Jansen3bbb6172002-07-29 21:36:35 +0000281- (BOOL) debug { return debug;};
282- (BOOL) verbose { return verbose;};
283- (BOOL) inspect { return inspect;};
284- (BOOL) optimize { return optimize;};
285- (BOOL) nosite { return nosite;};
286- (BOOL) tabs { return tabs;};
287- (NSString *) others { return others;};
288- (BOOL) with_terminal { return with_terminal;};
289
290@end