FileSettings.m 9.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
//
//  FileSettings.m
//  PythonLauncher
//
//  Created by Jack Jansen on Sun Jul 21 2002.
//  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
//

#import "FileSettings.h"

@implementation FileSettings
12 13 14 15 16

+ (id)getFactorySettingsForFileType: (NSString *)filetype
{
    static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
    FileSettings **curdefault;
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    if ([filetype isEqualToString: @"Python Script"]) {
        curdefault = &fsdefault_py;
    } else if ([filetype isEqualToString: @"Python GUI Script"]) {
        curdefault = &fsdefault_pyw;
    } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
        curdefault = &fsdefault_pyc;
    } else {
        NSLog(@"Funny File Type: %@\n", filetype);
        curdefault = &fsdefault_py;
        filetype = @"Python Script";
    }
    if (!*curdefault) {
        *curdefault = [[FileSettings new] initForFSDefaultFileType: filetype];
    }
    return *curdefault;
}

35 36 37 38
+ (id)getDefaultsForFileType: (NSString *)filetype
{
    static FileSettings *default_py, *default_pyw, *default_pyc;
    FileSettings **curdefault;
39

40 41 42 43 44 45 46 47 48 49 50 51
    if ([filetype isEqualToString: @"Python Script"]) {
        curdefault = &default_py;
    } else if ([filetype isEqualToString: @"Python GUI Script"]) {
        curdefault = &default_pyw;
    } else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
        curdefault = &default_pyc;
    } else {
        NSLog(@"Funny File Type: %@\n", filetype);
        curdefault = &default_py;
        filetype = @"Python Script";
    }
    if (!*curdefault) {
52
        *curdefault = [[FileSettings new] initForDefaultFileType: filetype];
53 54 55 56 57 58 59
    }
    return *curdefault;
}

+ (id)newSettingsForFileType: (NSString *)filetype
{
    FileSettings *cur;
60

61 62 63
    cur = [FileSettings new];
    [cur initForFileType: filetype];
    return [cur retain];
64 65 66 67
}

- (id)initWithFileSettings: (FileSettings *)source
{
68 69
    self = [super init];
    if (!self) return self;
70

71
    interpreter = [source->interpreter retain];
72
    honourhashbang = source->honourhashbang;
73 74 75 76 77 78 79
    debug = source->debug;
    verbose = source->verbose;
    inspect = source->inspect;
    optimize = source->optimize;
    nosite = source->nosite;
    tabs = source->tabs;
    others = [source->others retain];
80
    scriptargs = [source->scriptargs retain];
81
    with_terminal = source->with_terminal;
82 83
    prefskey = source->prefskey;
    if (prefskey) [prefskey retain];
84

85 86 87
    return self;
}

88
- (id)initForFileType: (NSString *)filetype
89
{
90
    FileSettings *defaults;
91

92 93 94 95 96 97 98 99 100 101 102 103
    defaults = [FileSettings getDefaultsForFileType: filetype];
    self = [self initWithFileSettings: defaults];
    origsource = [defaults retain];
    return self;
}

- (id)initForFSDefaultFileType: (NSString *)filetype
{
    int i;
    NSString *filename;
    NSDictionary *dict;
    static NSDictionary *factorySettings;
104

105 106
    self = [super init];
    if (!self) return self;
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    if (factorySettings == NULL) {
        NSBundle *bdl = [NSBundle mainBundle];
        NSString *path = [ bdl pathForResource: @"factorySettings"
                ofType: @"plist"];
        factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
            path] retain];
        if (factorySettings == NULL) {
            NSLog(@"Missing %@", path);
            return NULL;
        }
    }
    dict = [factorySettings objectForKey: filetype];
    if (dict == NULL) {
        NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
        interpreter = [@"no default found" retain];
        return NULL;
    }
    [self applyValuesFromDict: dict];
    interpreters = [dict objectForKey: @"interpreter_list"];
    interpreter = NULL;
    for (i=0; i < [interpreters count]; i++) {
        filename = [interpreters objectAtIndex: i];
        filename = [filename stringByExpandingTildeInPath];
        if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
            interpreter = [filename retain];
            break;
        }
    }
    if (interpreter == NULL)
        interpreter = [@"no default found" retain];
    origsource = NULL;
    return self;
}

- (void)applyUserDefaults: (NSString *)filetype
{
    NSUserDefaults *defaults;
    NSDictionary *dict;
146

147 148 149 150 151 152
    defaults = [NSUserDefaults standardUserDefaults];
    dict = [defaults dictionaryForKey: filetype];
    if (!dict)
        return;
    [self applyValuesFromDict: dict];
}
153

154 155 156
- (id)initForDefaultFileType: (NSString *)filetype
{
    FileSettings *fsdefaults;
157

158 159 160
    fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
    self = [self initWithFileSettings: fsdefaults];
    if (!self) return self;
161
    interpreters = [fsdefaults->interpreters retain];
162
    scriptargs = [@"" retain];
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    [self applyUserDefaults: filetype];
    prefskey = [filetype retain];
    return self;
}

- (void)reset
{
    if (origsource) {
        [self updateFromSource: origsource];
    } else {
        FileSettings *fsdefaults;
        fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
        [self updateFromSource: fsdefaults];
    }
177 178 179 180 181
}

- (void)updateFromSource: (id <FileSettingsSource>)source
{
    interpreter = [[source interpreter] retain];
182
    honourhashbang = [source honourhashbang];
183 184 185 186 187 188 189
    debug = [source debug];
    verbose = [source verbose];
    inspect = [source inspect];
    optimize = [source optimize];
    nosite = [source nosite];
    tabs = [source tabs];
    others = [[source others] retain];
190
    scriptargs = [[source scriptargs] retain];
191
    with_terminal = [source with_terminal];
192 193
    // And if this is a user defaults object we also save the
    // values
194 195 196 197
    if (!origsource) {
        NSUserDefaults *defaults;
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            interpreter, @"interpreter",
198
            [NSNumber numberWithBool: honourhashbang], @"honourhashbang",
199 200 201 202 203
            [NSNumber numberWithBool: debug], @"debug",
            [NSNumber numberWithBool: verbose], @"verbose",
            [NSNumber numberWithBool: inspect], @"inspect",
            [NSNumber numberWithBool: optimize], @"optimize",
            [NSNumber numberWithBool: nosite], @"nosite",
204
            [NSNumber numberWithBool: tabs], @"tabs",
205
            others, @"others",
206
            scriptargs, @"scriptargs",
207 208 209 210 211 212 213
            [NSNumber numberWithBool: with_terminal], @"with_terminal",
            nil];
        defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject: dict forKey: prefskey];
    }
}

214
- (void)applyValuesFromDict: (NSDictionary *)dict
215 216
{
    id value;
217

218 219
    value = [dict objectForKey: @"interpreter"];
    if (value) interpreter = [value retain];
220 221
    value = [dict objectForKey: @"honourhashbang"];
    if (value) honourhashbang = [value boolValue];
222 223 224 225 226 227 228 229 230 231
    value = [dict objectForKey: @"debug"];
    if (value) debug = [value boolValue];
    value = [dict objectForKey: @"verbose"];
    if (value) verbose = [value boolValue];
    value = [dict objectForKey: @"inspect"];
    if (value) inspect = [value boolValue];
    value = [dict objectForKey: @"optimize"];
    if (value) optimize = [value boolValue];
    value = [dict objectForKey: @"nosite"];
    if (value) nosite = [value boolValue];
232
    value = [dict objectForKey: @"tabs"];
233 234 235
    if (value) tabs = [value boolValue];
    value = [dict objectForKey: @"others"];
    if (value) others = [value retain];
236 237
    value = [dict objectForKey: @"scriptargs"];
    if (value) scriptargs = [value retain];
238 239 240 241
    value = [dict objectForKey: @"with_terminal"];
    if (value) with_terminal = [value boolValue];
}

242 243
- (NSString*)_replaceSingleQuotes: (NSString*)string
{
244 245 246 247
    /* Replace all single-quotes by '"'"', that way shellquoting will
     * be correct when the result value is delimited  using single quotes.
     */
    NSArray* components = [string componentsSeparatedByString:@"'"];
248

249
    return [components componentsJoinedByString:@"'\"'\"'"];
250 251
}

252 253
- (NSString *)commandLineForScript: (NSString *)script
{
254
    NSString *cur_interp = NULL;
255
    NSString* script_dir = NULL;
256 257 258
    char hashbangbuf[1024];
    FILE *fp;
    char *p;
259 260 261

    script_dir = [script substringToIndex:
	    [script length]-[[script lastPathComponent] length]];
262

263
    if (honourhashbang &&
264
       (fp=fopen([script fileSystemRepresentation], "r")) &&
265 266 267 268 269 270
       fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
       strncmp(hashbangbuf, "#!", 2) == 0 &&
       (p=strchr(hashbangbuf, '\n'))) {
            *p = '\0';
            p = hashbangbuf + 2;
            while (*p == ' ') p++;
271
            cur_interp = [NSString stringWithUTF8String: p];
272 273 274
    }
    if (!cur_interp)
        cur_interp = interpreter;
275

276
    return [NSString stringWithFormat:
277 278 279
        @"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
    	[self _replaceSingleQuotes:script_dir],
        [self _replaceSingleQuotes:cur_interp],
280 281 282 283 284 285 286
        debug?" -d":"",
        verbose?" -v":"",
        inspect?" -i":"",
        optimize?" -O":"",
        nosite?" -S":"",
        tabs?" -t":"",
        others,
287
        [self _replaceSingleQuotes:script],
288
        scriptargs ? scriptargs : @"",
289
        with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
290 291
}

292 293
- (NSArray *) interpreters { return interpreters;};

294
// FileSettingsSource protocol
295
- (NSString *) interpreter { return interpreter;};
296
- (BOOL) honourhashbang { return honourhashbang; };
297 298 299 300 301 302 303
- (BOOL) debug { return debug;};
- (BOOL) verbose { return verbose;};
- (BOOL) inspect { return inspect;};
- (BOOL) optimize { return optimize;};
- (BOOL) nosite { return nosite;};
- (BOOL) tabs { return tabs;};
- (NSString *) others { return others;};
304
- (NSString *) scriptargs { return scriptargs;};
305 306 307
- (BOOL) with_terminal { return with_terminal;};

@end