author | Jens Alfke <jens@mooseyard.com> |
Wed Sep 02 08:41:25 2009 -0700 (2009-09-02) | |
changeset 35 | 5cab3034d3a1 |
permissions | -rw-r--r-- |
jens@20 | 1 |
// |
jens@20 | 2 |
// URLFormatter.m |
jens@20 | 3 |
// Murky |
jens@20 | 4 |
// |
jens@20 | 5 |
// Copyright 2008 Jens Alfke. All rights reserved. |
jens@20 | 6 |
// |
jens@20 | 7 |
|
jens@20 | 8 |
#import "MYURLFormatter.h" |
jens@20 | 9 |
|
jens@20 | 10 |
|
jens@20 | 11 |
@implementation MYURLFormatter |
jens@20 | 12 |
|
jens@20 | 13 |
@synthesize allowedSchemes=_allowedSchemes; |
jens@20 | 14 |
|
jens@20 | 15 |
|
jens@20 | 16 |
- (id) init |
jens@20 | 17 |
{ |
jens@20 | 18 |
self = [super init]; |
jens@20 | 19 |
if (self != nil) { |
jens@20 | 20 |
_allowedSchemes = [[NSArray alloc] initWithObjects: @"http",@"https",@"file",@"ssh",nil]; |
jens@20 | 21 |
} |
jens@20 | 22 |
return self; |
jens@20 | 23 |
} |
jens@20 | 24 |
|
jens@20 | 25 |
- (void) dealloc |
jens@20 | 26 |
{ |
jens@20 | 27 |
[_allowedSchemes release]; |
jens@20 | 28 |
[super dealloc]; |
jens@20 | 29 |
} |
jens@20 | 30 |
|
jens@20 | 31 |
|
jens@20 | 32 |
- (NSString *)stringForObjectValue:(id)obj |
jens@20 | 33 |
{ |
jens@20 | 34 |
if( ! [obj isKindOfClass: [NSURL class]] ) |
jens@20 | 35 |
return @""; |
jens@20 | 36 |
else if( [obj isFileURL] ) |
jens@20 | 37 |
return [obj path]; |
jens@20 | 38 |
else |
jens@20 | 39 |
return [obj absoluteString]; |
jens@20 | 40 |
} |
jens@20 | 41 |
|
jens@20 | 42 |
|
jens@20 | 43 |
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)outError |
jens@20 | 44 |
{ |
jens@20 | 45 |
*obj = nil; |
jens@20 | 46 |
NSString *error = nil; |
jens@20 | 47 |
if( str.length==0 ) { |
jens@20 | 48 |
} else if( [str hasPrefix: @"/"] ) { |
jens@20 | 49 |
*obj = [NSURL fileURLWithPath: str]; |
jens@20 | 50 |
if( ! *obj ) |
jens@20 | 51 |
error = @"Invalid filesystem path"; |
jens@20 | 52 |
} else { |
jens@20 | 53 |
NSURL *url = [NSURL URLWithString: str]; |
jens@20 | 54 |
NSString *scheme = [url scheme]; |
jens@20 | 55 |
if( url && scheme == nil ) { |
jens@20 | 56 |
if( [str rangeOfString: @"."].length > 0 ) { |
jens@20 | 57 |
// Turn "foo.com/bar" into "http://foo.com/bar": |
jens@20 | 58 |
str = [@"http://" stringByAppendingString: str]; |
jens@20 | 59 |
url = [NSURL URLWithString: str]; |
jens@20 | 60 |
scheme = [url scheme]; |
jens@20 | 61 |
} else |
jens@20 | 62 |
url = nil; |
jens@20 | 63 |
} |
jens@20 | 64 |
if( ! url || ! [url path] || url.host.length==0 ) { |
jens@20 | 65 |
error = @"Invalid URL"; |
jens@20 | 66 |
} else if( _allowedSchemes && ! [_allowedSchemes containsObject: scheme] ) { |
jens@20 | 67 |
error = [@"URL protocol must be %@" stringByAppendingString: |
jens@20 | 68 |
[_allowedSchemes componentsJoinedByString: @", "]]; |
jens@20 | 69 |
} |
jens@20 | 70 |
*obj = url; |
jens@20 | 71 |
} |
jens@20 | 72 |
if( outError ) *outError = error; |
jens@20 | 73 |
return (error==nil); |
jens@20 | 74 |
} |
jens@20 | 75 |
|
jens@20 | 76 |
|
jens@20 | 77 |
+ (void) beginFilePickerFor: (NSTextField*)field |
jens@20 | 78 |
{ |
jens@20 | 79 |
NSParameterAssert(field); |
jens@20 | 80 |
NSOpenPanel *open = [NSOpenPanel openPanel]; |
jens@20 | 81 |
open.canChooseDirectories = YES; |
jens@20 | 82 |
open.canChooseFiles = NO; |
jens@20 | 83 |
open.requiredFileType = (id)kUTTypeDirectory; |
jens@20 | 84 |
[open beginSheetForDirectory: nil |
jens@20 | 85 |
file: nil |
jens@20 | 86 |
modalForWindow: field.window |
jens@20 | 87 |
modalDelegate: self |
jens@20 | 88 |
didEndSelector: @selector(_filePickerDidEnd:returnCode:context:) |
jens@20 | 89 |
contextInfo: field]; |
jens@20 | 90 |
} |
jens@20 | 91 |
|
jens@20 | 92 |
+ (void) beginNewFilePickerFor: (NSTextField*)field |
jens@20 | 93 |
{ |
jens@20 | 94 |
NSParameterAssert(field); |
jens@20 | 95 |
NSSavePanel *save = [NSSavePanel savePanel]; |
jens@20 | 96 |
[save beginSheetForDirectory: nil |
jens@20 | 97 |
file: nil |
jens@20 | 98 |
modalForWindow: field.window |
jens@20 | 99 |
modalDelegate: self |
jens@20 | 100 |
didEndSelector: @selector(_filePickerDidEnd:returnCode:context:) |
jens@20 | 101 |
contextInfo: field]; |
jens@20 | 102 |
} |
jens@20 | 103 |
|
jens@20 | 104 |
+ (void) _filePickerDidEnd: (NSSavePanel*)save returnCode: (int)returnCode context: (void*)context |
jens@20 | 105 |
{ |
jens@20 | 106 |
[save orderOut: self]; |
jens@20 | 107 |
if( returnCode == NSOKButton ) { |
jens@20 | 108 |
NSTextField *field = context; |
jens@20 | 109 |
field.objectValue = [NSURL fileURLWithPath: save.filename]; |
jens@20 | 110 |
} |
jens@20 | 111 |
} |
jens@20 | 112 |
|
jens@20 | 113 |
|
jens@20 | 114 |
@end |