MYURLFormatter.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 28 09:39:31 2009 -0700 (2009-03-28)
changeset 21 88d7e2455a7f
permissions -rw-r--r--
(merge)
     1 //
     2 //  URLFormatter.m
     3 //  Murky
     4 //
     5 //  Copyright 2008 Jens Alfke. All rights reserved.
     6 //
     7 
     8 #import "MYURLFormatter.h"
     9 
    10 
    11 @implementation MYURLFormatter
    12 
    13 @synthesize allowedSchemes=_allowedSchemes;
    14 
    15 
    16 - (id) init
    17 {
    18     self = [super init];
    19     if (self != nil) {
    20         _allowedSchemes = [[NSArray alloc] initWithObjects: @"http",@"https",@"file",@"ssh",nil];
    21     }
    22     return self;
    23 }
    24 
    25 - (void) dealloc
    26 {
    27     [_allowedSchemes release];
    28     [super dealloc];
    29 }
    30 
    31 
    32 - (NSString *)stringForObjectValue:(id)obj
    33 {
    34     if( ! [obj isKindOfClass: [NSURL class]] )
    35         return @"";
    36     else if( [obj isFileURL] )
    37         return [obj path];
    38     else
    39         return [obj absoluteString];
    40 }
    41 
    42 
    43 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)outError
    44 {
    45     *obj = nil;
    46     NSString *error = nil;
    47     if( str.length==0 ) {
    48     } else if( [str hasPrefix: @"/"] ) {
    49         *obj = [NSURL fileURLWithPath: str];
    50         if( ! *obj )
    51             error = @"Invalid filesystem path";
    52     } else {
    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];
    61             } else
    62                 url = nil;
    63         }
    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: @", "]];
    69         }
    70         *obj = url;
    71     }
    72     if( outError ) *outError = error;
    73     return (error==nil);
    74 }
    75 
    76 
    77 + (void) beginFilePickerFor: (NSTextField*)field
    78 {
    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
    85                             file: nil
    86                   modalForWindow: field.window
    87                    modalDelegate: self
    88                   didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
    89                      contextInfo: field];
    90 }
    91 
    92 + (void) beginNewFilePickerFor: (NSTextField*)field
    93 {
    94     NSParameterAssert(field);
    95     NSSavePanel *save = [NSSavePanel savePanel];
    96     [save beginSheetForDirectory: nil
    97                             file: nil
    98                   modalForWindow: field.window
    99                    modalDelegate: self
   100                   didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
   101                      contextInfo: field];
   102 }
   103 
   104 + (void) _filePickerDidEnd: (NSSavePanel*)save returnCode: (int)returnCode context: (void*)context
   105 {
   106     [save orderOut: self];
   107     if( returnCode == NSOKButton ) {
   108         NSTextField *field = context;
   109         field.objectValue = [NSURL fileURLWithPath: save.filename];
   110     }
   111 }
   112 
   113 
   114 @end