Source

MYUtilities / MYURLFormatter.m

//
//  URLFormatter.m
//  MYUtilities
//
//  Copyright 2008 Jens Alfke. All rights reserved.
//

#import "MYURLFormatter.h"


@implementation MYURLFormatter

@synthesize allowedSchemes=_allowedSchemes;


- (id) init
{
    self = [super init];
    if (self != nil) {
        _allowedSchemes = [[NSArray alloc] initWithObjects: @"http",@"https",@"file",@"ssh",nil];
    }
    return self;
}

- (void) dealloc
{
    [_allowedSchemes release];
    [super dealloc];
}


- (NSString *)stringForObjectValue:(id)obj
{
    if( ! [obj isKindOfClass: [NSURL class]] )
        return @"";
    else if( [obj isFileURL] )
        return [obj path];
    else
        return [obj absoluteString];
}


- (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)outError
{
    *obj = nil;
    NSString *error = nil;
    if( str.length==0 ) {
    } else if( [str hasPrefix: @"/"] ) {
        *obj = [NSURL fileURLWithPath: str];
        if( ! *obj )
            error = @"Invalid filesystem path";
    } else {
        NSURL *url = [NSURL URLWithString: str];
        NSString *scheme = [url scheme];
        if( url && scheme == nil ) {
            if( [str rangeOfString: @"."].length > 0 ) {
                // Turn "foo.com/bar" into "http://foo.com/bar":
                str = [@"http://" stringByAppendingString: str];
                url = [NSURL URLWithString: str];
                scheme = [url scheme];
            } else
                url = nil;
        }
        if( ! url || ! [url path] || url.host.length==0 ) {
            error = @"Invalid URL";
        } else if( _allowedSchemes && ! [_allowedSchemes containsObject: scheme] ) {
            error = [@"URL protocol must be %@" stringByAppendingString:
                                    [_allowedSchemes componentsJoinedByString: @", "]];
        }
        *obj = url;
    }
    if( outError ) *outError = error;
    return (error==nil);
}


+ (void) beginFilePickerFor: (NSTextField*)field
{
    NSParameterAssert(field);
    NSOpenPanel *open = [NSOpenPanel openPanel];
    open.canChooseDirectories = YES;
    open.canChooseFiles = NO;
    open.requiredFileType = (id)kUTTypeDirectory;
    [open beginSheetForDirectory: nil
                            file: nil
                  modalForWindow: field.window
                   modalDelegate: self
                  didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
                     contextInfo: field];
}

+ (void) beginNewFilePickerFor: (NSTextField*)field
{
    NSParameterAssert(field);
    NSSavePanel *save = [NSSavePanel savePanel];
    [save beginSheetForDirectory: nil
                            file: nil
                  modalForWindow: field.window
                   modalDelegate: self
                  didEndSelector: @selector(_filePickerDidEnd:returnCode:context:)
                     contextInfo: field];
}

+ (void) _filePickerDidEnd: (NSSavePanel*)save returnCode: (int)returnCode context: (void*)context
{
    [save orderOut: self];
    if( returnCode == NSOKButton ) {
        NSTextField *field = context;
        field.objectValue = [NSURL fileURLWithPath: save.filename];
    }
}


@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.