Tweak invalid filename handling. Use it for manual exports also. Allow drag exports...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 7 Mar 2013 13:23:29 +0000 (14:23 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 7 Mar 2013 13:23:29 +0000 (14:23 +0100)
NJMappingsController.m
NSString+FixFilename.h
NSString+FixFilename.m

index 45085b5..ea8137f 100644 (file)
     NSSavePanel *panel = [NSSavePanel savePanel];
     panel.allowedFileTypes = @[ @"enjoyable" ];
     NJMapping *mapping = _currentMapping;
     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) {
     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;
         [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;
     }
     } else {
         return NO;
     }
index 0850e74..b3e318f 100644 (file)
@@ -8,12 +8,21 @@
 
 #import <Foundation/Foundation.h>
 
 
 #import <Foundation/Foundation.h>
 
+@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
 @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
 
 @end
index fc8ba57..06a837c 100644 (file)
@@ -8,19 +8,28 @@
 
 #import "NSString+FixFilename.h"
 
 
 #import "NSString+FixFilename.h"
 
+@implementation NSCharacterSet (FixFilename)
+
++ (NSCharacterSet *)invalidPathComponentCharacterSet {
+    return [NSCharacterSet characterSetWithCharactersInString:@"\"\\/:*?<>|"];
+}
+
+@end
+
 @implementation NSString (FixFilename)
 
 - (NSString *)stringByFixingPathComponent {
 @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];
     NSArray *parts = [self componentsSeparatedByCharactersInSet:invalid];
-    NSString *newName = [parts componentsJoinedByString:@""];
-    if (!newName.length)
+    NSString *name = [parts componentsJoinedByString:@"_"];
+    name = [name stringByTrimmingCharactersInSet:whitespace];
+    if (!name.length)
         return @"_";
         return @"_";
-    if ([newName characterAtIndex:0] == '.')
-        newName = [@"_" stringByAppendingString:newName];
-    return newName;
+    unichar first = [name characterAtIndex:0];
+    if (first == '.' || first == '-')
+        name = [@"_" stringByAppendingString:name];
+    return name;
 }
 
 @end
 }
 
 @end