MYURLFormatter.m
changeset 27 256370e8935a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MYURLFormatter.m	Sun May 03 10:13:31 2009 -0700
     1.3 @@ -0,0 +1,114 @@
     1.4 +//
     1.5 +//  URLFormatter.m
     1.6 +//  Murky
     1.7 +//
     1.8 +//  Copyright 2008 Jens Alfke. All rights reserved.
     1.9 +//
    1.10 +
    1.11 +#import "MYURLFormatter.h"
    1.12 +
    1.13 +
    1.14 +@implementation MYURLFormatter
    1.15 +
    1.16 +@synthesize allowedSchemes=_allowedSchemes;
    1.17 +
    1.18 +
    1.19 +- (id) init
    1.20 +{
    1.21 +    self = [super init];
    1.22 +    if (self != nil) {
    1.23 +        _allowedSchemes = [[NSArray alloc] initWithObjects: @"http",@"https",@"file",@"ssh",nil];
    1.24 +    }
    1.25 +    return self;
    1.26 +}
    1.27 +
    1.28 +- (void) dealloc
    1.29 +{
    1.30 +    [_allowedSchemes release];
    1.31 +    [super dealloc];
    1.32 +}
    1.33 +
    1.34 +
    1.35 +- (NSString *)stringForObjectValue:(id)obj
    1.36 +{
    1.37 +    if( ! [obj isKindOfClass: [NSURL class]] )
    1.38 +        return @"";
    1.39 +    else if( [obj isFileURL] )
    1.40 +        return [obj path];
    1.41 +    else
    1.42 +        return [obj absoluteString];
    1.43 +}
    1.44 +
    1.45 +
    1.46 +- (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)outError
    1.47 +{
    1.48 +    *obj = nil;
    1.49 +    NSString *error = nil;
    1.50 +    if( str.length==0 ) {
    1.51 +    } else if( [str hasPrefix: @"/"] ) {
    1.52 +        *obj = [NSURL fileURLWithPath: str];
    1.53 +        if( ! *obj )
    1.54 +            error = @"Invalid filesystem path";
    1.55 +    } else {
    1.56 +        NSURL *url = [NSURL URLWithString: str];
    1.57 +        NSString *scheme = [url scheme];
    1.58 +        if( url && scheme == nil ) {
    1.59 +            if( [str rangeOfString: @"."].length > 0 ) {
    1.60 +                // Turn "foo.com/bar" into "http://foo.com/bar":
    1.61 +                str = [@"http://" stringByAppendingString: str];
    1.62 +                url = [NSURL URLWithString: str];
    1.63 +                scheme = [url scheme];
    1.64 +            } else
    1.65 +                url = nil;
    1.66 +        }
    1.67 +        if( ! url || ! [url path] || url.host.length==0 ) {
    1.68 +            error = @"Invalid URL";
    1.69 +        } else if( _allowedSchemes && ! [_allowedSchemes containsObject: scheme] ) {
    1.70 +            error = [@"URL protocol must be %@" stringByAppendingString:
    1.71 +                                    [_allowedSchemes componentsJoinedByString: @", "]];
    1.72 +        }
    1.73 +        *obj = url;
    1.74 +    }
    1.75 +    if( outError ) *outError = error;
    1.76 +    return (error==nil);
    1.77 +}
    1.78 +
    1.79 +
    1.80 ++ (void) beginFilePickerFor: (NSTextField*)field
    1.81 +{
    1.82 +    NSParameterAssert(field);
    1.83 +    NSOpenPanel *open = [NSOpenPanel openPanel];
    1.84 +    open.canChooseDirectories = YES;
    1.85 +    open.canChooseFiles = NO;
    1.86 +    open.requiredFileType = (id)kUTTypeDirectory;
    1.87 +    [open beginSheetForDirectory: nil
    1.88 +                            file: nil
    1.89 +                  modalForWindow: field.window
    1.90 +                   modalDelegate: self
    1.91 +                  didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
    1.92 +                     contextInfo: field];
    1.93 +}
    1.94 +
    1.95 ++ (void) beginNewFilePickerFor: (NSTextField*)field
    1.96 +{
    1.97 +    NSParameterAssert(field);
    1.98 +    NSSavePanel *save = [NSSavePanel savePanel];
    1.99 +    [save beginSheetForDirectory: nil
   1.100 +                            file: nil
   1.101 +                  modalForWindow: field.window
   1.102 +                   modalDelegate: self
   1.103 +                  didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
   1.104 +                     contextInfo: field];
   1.105 +}
   1.106 +
   1.107 ++ (void) _filePickerDidEnd: (NSSavePanel*)save returnCode: (int)returnCode context: (void*)context
   1.108 +{
   1.109 +    [save orderOut: self];
   1.110 +    if( returnCode == NSOKButton ) {
   1.111 +        NSTextField *field = context;
   1.112 +        field.objectValue = [NSURL fileURLWithPath: save.filename];
   1.113 +    }
   1.114 +}
   1.115 +
   1.116 +
   1.117 +@end