drag-and-drop - export urls to other applications.

This commit is contained in:
Kelvin Sherlock 2020-09-18 00:33:02 -04:00
parent bf619da42d
commit 7a085301cf

View File

@ -20,6 +20,7 @@
@implementation DiskImagesWindowController { @implementation DiskImagesWindowController {
BOOL _dirty; BOOL _dirty;
NSSet *_extensions; NSSet *_extensions;
NSTimer *_timer;
} }
@ -48,8 +49,9 @@
[super windowDidLoad]; [super windowDidLoad];
[_tableView registerForDraggedTypes: @[NSPasteboardTypeURL]]; [_tableView registerForDraggedTypes: @[NSPasteboardTypeFileURL]];
[_tableView setDraggingSourceOperationMask: NSDragOperationCopy forLocal: NO]; // enable drag/drop to othr apps.
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
} }
@ -81,11 +83,20 @@
[self addFile: url]; [self addFile: url];
} }
-(void)markDirty {
_dirty = YES;
if (_timer) [_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval: 5 * 60 repeats: NO block: ^(NSTimer *t) {
self->_timer = nil;
[self saveFile];
}];
}
-(void)willTerminate: (NSNotification *)notification { -(void)saveFile {
// if dirty, write data....
if (!_dirty) return; [_timer invalidate];
_timer = nil;
NSURL *sd = SupportDirectory(); NSURL *sd = SupportDirectory();
NSURL *url = [sd URLByAppendingPathComponent: @"RecentDiskImages.plist"]; NSURL *url = [sd URLByAppendingPathComponent: @"RecentDiskImages.plist"];
@ -93,11 +104,22 @@
if (_content && url) { if (_content && url) {
[_content writeToURL: url atomically: YES]; [_content writeToURL: url atomically: YES];
} }
_dirty = NO;
}
-(void)willTerminate: (NSNotification *)notification {
// if dirty, write data....
if (!_dirty) return;
[self saveFile];
} }
-(BOOL)addFile: (NSObject *)pathOrURL { -(BOOL)addFile: (NSObject *)pathOrURL {
NSString *path = nil; NSString *path = nil;
@ -120,7 +142,7 @@
if ([path compare: s] == NSOrderedSame) { if ([path compare: s] == NSOrderedSame) {
found = YES; found = YES;
[d setObject: [NSDate new] forKey: @"date"]; [d setObject: [NSDate new] forKey: @"date"];
_dirty = YES; // ? [self markDirty];
break; break;
} }
} }
@ -151,8 +173,7 @@
else else
[_content addObject: d]; [_content addObject: d];
} }
[self markDirty];
_dirty = YES;
return YES; return YES;
} }
@ -192,7 +213,7 @@
} else { } else {
[_content removeObject: item]; [_content removeObject: item];
} }
_dirty = YES; [self markDirty];
} }
} }
@ -215,31 +236,36 @@
#endif #endif
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard { -(id<NSPasteboardWriting>)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row {
// if ([rowIndexes count] > 1) return NO; // ?
id objects = [_arrayController arrangedObjects]; id objects = [_arrayController arrangedObjects];
[pboard declareTypes: @[NSPasteboardTypeURL] owner: nil];
// NSURLPboardType deprecated NSDictionary *d = [objects objectAtIndex: row];
[rowIndexes enumerateIndexesUsingBlock: ^(NSUInteger index, BOOL *stop) {
NSString *path = [d objectForKey: @"path"];
NSDictionary *d = [objects objectAtIndex: index];
NSString *path = [d objectForKey: @"path"]; NSURL *url = [NSURL fileURLWithPath: path];
return url;
#if 0
NSPasteboardItem *item = [NSPasteboardItem new];
[item setString: [url absoluteString] forType: NSPasteboardTypeFileURL]; // FileURL
[item setString: path forType: NSPasteboardTypeString]; // for Terminal.app
NSURL *url = [NSURL fileURLWithPath: path]; return item;
[url writeToPasteboard: pboard]; #endif
}];
// NSFilenamesPboardType -- old way of handling it ...
return YES;
} }
-(NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation { -(NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation {
if ([info draggingSource] == _tableView) return NSDragOperationNone;
// option key will ignore all filetype restrictions. // option key will ignore all filetype restrictions.
if ([NSEvent modifierFlags] & NSEventModifierFlagOption) return NSDragOperationCopy; if ([NSEvent modifierFlags] & NSEventModifierFlagOption) return NSDragOperationCopy;
// this only checks the first dragged item...
NSPasteboard * pb = [info draggingPasteboard]; NSPasteboard * pb = [info draggingPasteboard];
NSURL *url = [NSURL URLFromPasteboard: pb]; NSURL *url = [NSURL URLFromPasteboard: pb];
@ -253,13 +279,22 @@
-(BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation { -(BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation {
if ([info draggingSource] == _tableView) return NO;
NSPasteboard * pb = [info draggingPasteboard]; NSPasteboard * pb = [info draggingPasteboard];
NSURL *url = [NSURL URLFromPasteboard: pb];
if (!url) return NO;
return [self addFile: url]; BOOL ok = NO;
for (NSPasteboardItem *item in [pb pasteboardItems]) {
// need to convert from a string to a url back to a file in case it's a file id url?
NSString *s = [item stringForType: NSPasteboardTypeFileURL];
if (!s) continue;
NSURL *url = [NSURL URLWithString: s];
if (!url) continue;
ok |= [self addFile: url];
}
return ok;
} }
@end @end