Add file manager support for ‘new file’

This commit is contained in:
Allan Odgaard
2013-02-15 15:27:57 +01:00
parent 873b9e8ced
commit 52ba0cb5c2
2 changed files with 38 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ PUBLIC extern NSString* const OakFileManagerPathKey;
PUBLIC @interface OakFileManager : NSObject
+ (OakFileManager*)sharedInstance;
- (NSURL*)createUntitledDirectoryAtURL:(NSURL*)anURL window:(NSWindow*)window;
- (BOOL)createFileAtURL:(NSURL*)anURL window:(NSWindow*)window;
- (NSURL*)createDuplicateOfURL:(NSURL*)srcURL window:(NSWindow*)window;
- (void)createSymbolicLinkAtURL:(NSURL*)anURL withDestinationURL:(NSURL*)dstURL window:(NSWindow*)window;
- (BOOL)renameItemAtURL:(NSURL*)srcURL toURL:(NSURL*)dstURL window:(NSWindow*)window;

View File

@@ -73,6 +73,33 @@ NSString* const OakFileManagerPathKey = @"directory";
}
}
- (BOOL)doCreateFile:(NSURL*)fileURL window:(NSWindow*)window
{
int fd = open([[fileURL path] fileSystemRepresentation], O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
if(fd != -1)
{
close(fd);
[[[window undoManager] prepareWithInvocationTarget:self] doRemoveFile:fileURL window:window];
[self postDidChangeContentsOfDirectory:[[fileURL path] stringByDeletingLastPathComponent]];
}
return fd != -1;
}
- (void)doRemoveFile:(NSURL*)fileURL window:(NSWindow*)window
{
NSError* error;
[[NSNotificationCenter defaultCenter] postNotificationName:OakFileManagerWillDeleteItemAtPath object:self userInfo:@{ OakFileManagerPathKey : [[fileURL filePathURL] path] }];
if([[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error])
{
[[[window undoManager] prepareWithInvocationTarget:self] doCreateFile:fileURL window:window];
[self postDidChangeContentsOfDirectory:[[fileURL path] stringByDeletingLastPathComponent]];
}
else
{
[window presentError:error];
}
}
- (BOOL)doCreateCopy:(NSURL*)dstURL ofURL:(NSURL*)srcURL window:(NSWindow*)window
{
BOOL res;
@@ -229,6 +256,16 @@ NSString* const OakFileManagerPathKey = @"directory";
return nil;
}
- (BOOL)createFileAtURL:(NSURL*)anURL window:(NSWindow*)window
{
if([self doCreateFile:anURL window:window])
{
[[window undoManager] setActionName:@"New Document"];
return YES;
}
return NO;
}
- (NSURL*)createDuplicateOfURL:(NSURL*)srcURL window:(NSWindow*)window
{
NSURL* dst = [NSURL fileURLWithPath:[NSString stringWithCxxString:path::unique([[srcURL path] fileSystemRepresentation], " copy")]];