From 56d825ba259066d847a9fc3f9c8c0c0a362a1507 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 7 Mar 2013 14:23:29 +0100 Subject: [PATCH] Tweak invalid filename handling. Use it for manual exports also. Allow drag exports of default mapping. --- NJMappingsController.m | 6 +++++- NSString+FixFilename.h | 15 ++++++++++++--- NSString+FixFilename.m | 25 +++++++++++++++++-------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/NJMappingsController.m b/NJMappingsController.m index 45085b5..ea8137f 100644 --- a/NJMappingsController.m +++ b/NJMappingsController.m @@ -299,7 +299,7 @@ NSSavePanel *panel = [NSSavePanel savePanel]; panel.allowedFileTypes = @[ @"enjoyable" ]; NJMapping *mapping = _currentMapping; - panel.nameFieldStringValue = mapping.name; + panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent]; NSWindow *window = NSApplication.sharedApplication.keyWindow; [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) { @@ -424,6 +424,10 @@ writeRowsWithIndexes:(NSIndexSet *)rowIndexes [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW]; [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType]; return YES; + } else if (rowIndexes.count == 1 && rowIndexes.firstIndex == 0) { + [pboard declareTypes:@[NSFilesPromisePboardType] owner:nil]; + [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType]; + return YES; } else { return NO; } diff --git a/NSString+FixFilename.h b/NSString+FixFilename.h index 0850e74..b3e318f 100644 --- a/NSString+FixFilename.h +++ b/NSString+FixFilename.h @@ -8,12 +8,21 @@ #import +@interface NSCharacterSet (FixFilename) + ++ (NSCharacterSet *)invalidPathComponentCharacterSet; + // A character set containing the characters that are invalid to + // use in path components on common filesystems. + +@end + @interface NSString (FixFilename) - (NSString *)stringByFixingPathComponent; // Does various operations to make this string suitable for use as - // a single path component of a normal filename. Removes / and : - // characters and prepends a _ to avoid leading .s or an empty - // name. + // a single path component of a normal filename. Removes + // characters that are invalid. Strips whitespace from the + // beginning and end. If the first character is a . or a -, a _ is + // added to the front. @end diff --git a/NSString+FixFilename.m b/NSString+FixFilename.m index fc8ba57..06a837c 100644 --- a/NSString+FixFilename.m +++ b/NSString+FixFilename.m @@ -8,19 +8,28 @@ #import "NSString+FixFilename.h" +@implementation NSCharacterSet (FixFilename) + ++ (NSCharacterSet *)invalidPathComponentCharacterSet { + return [NSCharacterSet characterSetWithCharactersInString:@"\"\\/:*?<>|"]; +} + +@end + @implementation NSString (FixFilename) - (NSString *)stringByFixingPathComponent { - static NSCharacterSet *invalid; - if (!invalid) - invalid = [NSCharacterSet characterSetWithCharactersInString:@"/:"]; + NSCharacterSet *invalid = NSCharacterSet.invalidPathComponentCharacterSet; + NSCharacterSet *whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet; NSArray *parts = [self componentsSeparatedByCharactersInSet:invalid]; - NSString *newName = [parts componentsJoinedByString:@""]; - if (!newName.length) + NSString *name = [parts componentsJoinedByString:@"_"]; + name = [name stringByTrimmingCharactersInSet:whitespace]; + if (!name.length) return @"_"; - if ([newName characterAtIndex:0] == '.') - newName = [@"_" stringByAppendingString:newName]; - return newName; + unichar first = [name characterAtIndex:0]; + if (first == '.' || first == '-') + name = [@"_" stringByAppendingString:name]; + return name; } @end -- 2.30.2