blob: 3438870b7484c48218dcf5083b350ae93ed637fb [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;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020017
Jack Jansen2095c062002-11-25 13:11:06 +000018 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;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020039
Jack Jansen3bbb6172002-07-29 21:36:35 +000040 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;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020060
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;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020070
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];
Jack Jansend7cccdd2003-06-20 22:21:03 +000080 scriptargs = [source->scriptargs retain];
Jack Jansen3bbb6172002-07-29 21:36:35 +000081 with_terminal = source->with_terminal;
Jack Jansen2095c062002-11-25 13:11:06 +000082 prefskey = source->prefskey;
83 if (prefskey) [prefskey retain];
Ronald Oussoren4e327c92013-07-07 09:53:08 +020084
Jack Jansen3bbb6172002-07-29 21:36:35 +000085 return self;
86}
87
Jack Jansen2095c062002-11-25 13:11:06 +000088- (id)initForFileType: (NSString *)filetype
Jack Jansen3bbb6172002-07-29 21:36:35 +000089{
Jack Jansen2095c062002-11-25 13:11:06 +000090 FileSettings *defaults;
Ronald Oussoren4e327c92013-07-07 09:53:08 +020091
Jack Jansen2095c062002-11-25 13:11:06 +000092 defaults = [FileSettings getDefaultsForFileType: filetype];
93 self = [self initWithFileSettings: defaults];
94 origsource = [defaults retain];
95 return self;
96}
97
Jack Jansen2095c062002-11-25 13:11:06 +000098- (id)initForFSDefaultFileType: (NSString *)filetype
99{
100 int i;
101 NSString *filename;
102 NSDictionary *dict;
Jack Jansen2095c062002-11-25 13:11:06 +0000103 static NSDictionary *factorySettings;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200104
Jack Jansen2095c062002-11-25 13:11:06 +0000105 self = [super init];
106 if (!self) return self;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200107
Jack Jansen2095c062002-11-25 13:11:06 +0000108 if (factorySettings == NULL) {
109 NSBundle *bdl = [NSBundle mainBundle];
110 NSString *path = [ bdl pathForResource: @"factorySettings"
111 ofType: @"plist"];
112 factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
113 path] retain];
114 if (factorySettings == NULL) {
115 NSLog(@"Missing %@", path);
116 return NULL;
117 }
118 }
119 dict = [factorySettings objectForKey: filetype];
120 if (dict == NULL) {
121 NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
122 interpreter = [@"no default found" retain];
123 return NULL;
124 }
125 [self applyValuesFromDict: dict];
126 interpreters = [dict objectForKey: @"interpreter_list"];
127 interpreter = NULL;
128 for (i=0; i < [interpreters count]; i++) {
129 filename = [interpreters objectAtIndex: i];
130 filename = [filename stringByExpandingTildeInPath];
131 if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
132 interpreter = [filename retain];
133 break;
134 }
135 }
136 if (interpreter == NULL)
137 interpreter = [@"no default found" retain];
138 origsource = NULL;
139 return self;
140}
141
142- (void)applyUserDefaults: (NSString *)filetype
143{
144 NSUserDefaults *defaults;
145 NSDictionary *dict;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200146
Jack Jansen2095c062002-11-25 13:11:06 +0000147 defaults = [NSUserDefaults standardUserDefaults];
148 dict = [defaults dictionaryForKey: filetype];
149 if (!dict)
150 return;
151 [self applyValuesFromDict: dict];
152}
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200153
Jack Jansen2095c062002-11-25 13:11:06 +0000154- (id)initForDefaultFileType: (NSString *)filetype
155{
156 FileSettings *fsdefaults;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200157
Jack Jansen2095c062002-11-25 13:11:06 +0000158 fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
159 self = [self initWithFileSettings: fsdefaults];
160 if (!self) return self;
Jack Jansenf044e092002-12-26 22:10:53 +0000161 interpreters = [fsdefaults->interpreters retain];
Jack Jansend7cccdd2003-06-20 22:21:03 +0000162 scriptargs = [@"" retain];
Jack Jansen2095c062002-11-25 13:11:06 +0000163 [self applyUserDefaults: filetype];
164 prefskey = [filetype retain];
165 return self;
166}
167
168- (void)reset
169{
170 if (origsource) {
171 [self updateFromSource: origsource];
172 } else {
173 FileSettings *fsdefaults;
174 fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
175 [self updateFromSource: fsdefaults];
176 }
Jack Jansen3bbb6172002-07-29 21:36:35 +0000177}
178
179- (void)updateFromSource: (id <FileSettingsSource>)source
180{
181 interpreter = [[source interpreter] retain];
Jack Jansen3d3b7462003-02-17 15:40:00 +0000182 honourhashbang = [source honourhashbang];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000183 debug = [source debug];
184 verbose = [source verbose];
185 inspect = [source inspect];
186 optimize = [source optimize];
187 nosite = [source nosite];
188 tabs = [source tabs];
189 others = [[source others] retain];
Jack Jansend7cccdd2003-06-20 22:21:03 +0000190 scriptargs = [[source scriptargs] retain];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000191 with_terminal = [source with_terminal];
Jack Jansen2095c062002-11-25 13:11:06 +0000192 // And if this is a user defaults object we also save the
193 // values
Jack Jansen3bbb6172002-07-29 21:36:35 +0000194 if (!origsource) {
195 NSUserDefaults *defaults;
196 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
197 interpreter, @"interpreter",
Jack Jansen3d3b7462003-02-17 15:40:00 +0000198 [NSNumber numberWithBool: honourhashbang], @"honourhashbang",
Jack Jansen3bbb6172002-07-29 21:36:35 +0000199 [NSNumber numberWithBool: debug], @"debug",
200 [NSNumber numberWithBool: verbose], @"verbose",
201 [NSNumber numberWithBool: inspect], @"inspect",
202 [NSNumber numberWithBool: optimize], @"optimize",
203 [NSNumber numberWithBool: nosite], @"nosite",
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 [NSNumber numberWithBool: tabs], @"tabs",
Jack Jansen3bbb6172002-07-29 21:36:35 +0000205 others, @"others",
Jack Jansend7cccdd2003-06-20 22:21:03 +0000206 scriptargs, @"scriptargs",
Jack Jansen3bbb6172002-07-29 21:36:35 +0000207 [NSNumber numberWithBool: with_terminal], @"with_terminal",
208 nil];
209 defaults = [NSUserDefaults standardUserDefaults];
210 [defaults setObject: dict forKey: prefskey];
211 }
212}
213
Jack Jansen2095c062002-11-25 13:11:06 +0000214- (void)applyValuesFromDict: (NSDictionary *)dict
Jack Jansen3bbb6172002-07-29 21:36:35 +0000215{
Jack Jansen3bbb6172002-07-29 21:36:35 +0000216 id value;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200217
Jack Jansen3bbb6172002-07-29 21:36:35 +0000218 value = [dict objectForKey: @"interpreter"];
219 if (value) interpreter = [value retain];
Jack Jansen3d3b7462003-02-17 15:40:00 +0000220 value = [dict objectForKey: @"honourhashbang"];
221 if (value) honourhashbang = [value boolValue];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000222 value = [dict objectForKey: @"debug"];
223 if (value) debug = [value boolValue];
224 value = [dict objectForKey: @"verbose"];
225 if (value) verbose = [value boolValue];
226 value = [dict objectForKey: @"inspect"];
227 if (value) inspect = [value boolValue];
228 value = [dict objectForKey: @"optimize"];
229 if (value) optimize = [value boolValue];
230 value = [dict objectForKey: @"nosite"];
231 if (value) nosite = [value boolValue];
Thomas Wouters89f507f2006-12-13 04:49:30 +0000232 value = [dict objectForKey: @"tabs"];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000233 if (value) tabs = [value boolValue];
234 value = [dict objectForKey: @"others"];
235 if (value) others = [value retain];
Jack Jansend7cccdd2003-06-20 22:21:03 +0000236 value = [dict objectForKey: @"scriptargs"];
237 if (value) scriptargs = [value retain];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000238 value = [dict objectForKey: @"with_terminal"];
239 if (value) with_terminal = [value boolValue];
240}
241
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242- (NSString*)_replaceSingleQuotes: (NSString*)string
243{
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200244 /* Replace all single-quotes by '"'"', that way shellquoting will
245 * be correct when the result value is delimited using single quotes.
246 */
247 NSArray* components = [string componentsSeparatedByString:@"'"];
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000248
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200249 return [components componentsJoinedByString:@"'\"'\"'"];
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250}
251
Jack Jansen3bbb6172002-07-29 21:36:35 +0000252- (NSString *)commandLineForScript: (NSString *)script
253{
Jack Jansen3d3b7462003-02-17 15:40:00 +0000254 NSString *cur_interp = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255 NSString* script_dir = NULL;
Jack Jansen3d3b7462003-02-17 15:40:00 +0000256 char hashbangbuf[1024];
257 FILE *fp;
258 char *p;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259
260 script_dir = [script substringToIndex:
261 [script length]-[[script lastPathComponent] length]];
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200262
Jack Jansen3d3b7462003-02-17 15:40:00 +0000263 if (honourhashbang &&
Ronald Oussoren4a852692009-03-30 19:25:21 +0000264 (fp=fopen([script fileSystemRepresentation], "r")) &&
Jack Jansen3d3b7462003-02-17 15:40:00 +0000265 fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
266 strncmp(hashbangbuf, "#!", 2) == 0 &&
267 (p=strchr(hashbangbuf, '\n'))) {
268 *p = '\0';
269 p = hashbangbuf + 2;
270 while (*p == ' ') p++;
Ronald Oussoren4a852692009-03-30 19:25:21 +0000271 cur_interp = [NSString stringWithUTF8String: p];
Jack Jansen3d3b7462003-02-17 15:40:00 +0000272 }
273 if (!cur_interp)
274 cur_interp = interpreter;
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200275
Jack Jansen3bbb6172002-07-29 21:36:35 +0000276 return [NSString stringWithFormat:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277 @"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
278 [self _replaceSingleQuotes:script_dir],
279 [self _replaceSingleQuotes:cur_interp],
Jack Jansen3bbb6172002-07-29 21:36:35 +0000280 debug?" -d":"",
281 verbose?" -v":"",
282 inspect?" -i":"",
283 optimize?" -O":"",
284 nosite?" -S":"",
285 tabs?" -t":"",
286 others,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 [self _replaceSingleQuotes:script],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288 scriptargs ? scriptargs : @"",
Jack Jansenf044e092002-12-26 22:10:53 +0000289 with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
Jack Jansen3bbb6172002-07-29 21:36:35 +0000290}
291
Jack Jansenf044e092002-12-26 22:10:53 +0000292- (NSArray *) interpreters { return interpreters;};
293
Ronald Oussoren4e327c92013-07-07 09:53:08 +0200294// FileSettingsSource protocol
Jack Jansen3bbb6172002-07-29 21:36:35 +0000295- (NSString *) interpreter { return interpreter;};
Jack Jansen3d3b7462003-02-17 15:40:00 +0000296- (BOOL) honourhashbang { return honourhashbang; };
Jack Jansen3bbb6172002-07-29 21:36:35 +0000297- (BOOL) debug { return debug;};
298- (BOOL) verbose { return verbose;};
299- (BOOL) inspect { return inspect;};
300- (BOOL) optimize { return optimize;};
301- (BOOL) nosite { return nosite;};
302- (BOOL) tabs { return tabs;};
303- (NSString *) others { return others;};
Jack Jansend7cccdd2003-06-20 22:21:03 +0000304- (NSString *) scriptargs { return scriptargs;};
Jack Jansen3bbb6172002-07-29 21:36:35 +0000305- (BOOL) with_terminal { return with_terminal;};
306
307@end