Changes to config-file structure.
5 // Copyright 2008 Jens Alfke. All rights reserved.
8 #import "MYURLFormatter.h"
11 @implementation MYURLFormatter
13 @synthesize allowedSchemes=_allowedSchemes;
20 _allowedSchemes = [[NSArray alloc] initWithObjects: @"http",@"https",@"file",@"ssh",nil];
27 [_allowedSchemes release];
32 - (NSString *)stringForObjectValue:(id)obj
34 if( ! [obj isKindOfClass: [NSURL class]] )
36 else if( [obj isFileURL] )
39 return [obj absoluteString];
43 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)outError
46 NSString *error = nil;
48 } else if( [str hasPrefix: @"/"] ) {
49 *obj = [NSURL fileURLWithPath: str];
51 error = @"Invalid filesystem path";
53 NSURL *url = [NSURL URLWithString: str];
54 NSString *scheme = [url scheme];
55 if( url && scheme == nil ) {
56 if( [str rangeOfString: @"."].length > 0 ) {
57 // Turn "foo.com/bar" into "http://foo.com/bar":
58 str = [@"http://" stringByAppendingString: str];
59 url = [NSURL URLWithString: str];
60 scheme = [url scheme];
64 if( ! url || ! [url path] || url.host.length==0 ) {
65 error = @"Invalid URL";
66 } else if( _allowedSchemes && ! [_allowedSchemes containsObject: scheme] ) {
67 error = [@"URL protocol must be %@" stringByAppendingString:
68 [_allowedSchemes componentsJoinedByString: @", "]];
72 if( outError ) *outError = error;
77 + (void) beginFilePickerFor: (NSTextField*)field
79 NSParameterAssert(field);
80 NSOpenPanel *open = [NSOpenPanel openPanel];
81 open.canChooseDirectories = YES;
82 open.canChooseFiles = NO;
83 open.requiredFileType = (id)kUTTypeDirectory;
84 [open beginSheetForDirectory: nil
86 modalForWindow: field.window
88 didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
92 + (void) beginNewFilePickerFor: (NSTextField*)field
94 NSParameterAssert(field);
95 NSSavePanel *save = [NSSavePanel savePanel];
96 [save beginSheetForDirectory: nil
98 modalForWindow: field.window
100 didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
104 + (void) _filePickerDidEnd: (NSSavePanel*)save returnCode: (int)returnCode context: (void*)context
106 [save orderOut: self];
107 if( returnCode == NSOKButton ) {
108 NSTextField *field = context;
109 field.objectValue = [NSURL fileURLWithPath: save.filename];