Drag and Drop of files to Finder started

This commit is contained in:
michael starke
2013-08-16 01:54:14 +02:00
parent 5ce44e909c
commit dcef431c5c
3 changed files with 88 additions and 11 deletions

View File

@@ -9,6 +9,8 @@
#import "MPAttachmentTableDataSource.h"
#import "MPDocument.h"
#import "Kdb3Entry+KVOAdditions.h"
#import "Kdb4Entry+KVOAdditions.h"
@implementation MPAttachmentTableDataSource
@@ -56,5 +58,65 @@
}
return YES;
}
/*
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
NSString *extension;
if([rowIndexes count] != 1) {
return NO; // We only work with one file at a time
}
MPDocument *document = [[[tableView window] windowController] document];
id entry = document.selectedEntry;
NSUInteger row = [rowIndexes lastIndex];
if([entry isKindOfClass:[Kdb3Entry class]]) {
Kdb3Entry *entryV3 = (Kdb3Entry *)entry;
extension = [entryV3.binaryDesc pathExtension];
}
else if([entry isKindOfClass:[Kdb4Entry class]]) {
Kdb4Entry *entryV4 = (Kdb4Entry *)entry;
BinaryRef *binaryRef = entryV4.binaries[row];
extension = [binaryRef.key pathExtension];
}
NSString *uti = CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag( kUTTagClassFilenameExtension, (__bridge CFStringRef)(extension), NULL ));
[pboard setPropertyList:@[uti] forType:(NSString *)kPasteboardTypeFilePromiseContent];
[pboard setPropertyList:@[uti] forType:(NSString *)kPasteboardTypeFileURLPromise ];
return YES;
}
- (NSArray *)tableView:(NSTableView *)tableView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination forDraggedRowsWithIndexes:(NSIndexSet *)indexSet {
if([indexSet count] != 1) {
return nil; // We only work with one file at a time
}
if(![dropDestination isFileURL]) {
return nil;
}
NSUInteger row = [indexSet lastIndex];
NSData *fileData;
NSString *filename;
MPDocument *document = [[[tableView window] windowController] document];
id entry = document.selectedEntry;
if([entry isKindOfClass:[Kdb3Entry class]]) {
Kdb3Entry *entryV3 = (Kdb3Entry *)entry;
filename = entryV3.binaryDesc;
fileData = entryV3.binary;
}
else if([entry isKindOfClass:[Kdb4Entry class]]) {
Kdb4Entry *entryV4 = (Kdb4Entry *)entry;
BinaryRef *binaryRef = entryV4.binaries[row];
filename = binaryRef.key;
fileData = [document attachmentDataForItem:binaryRef];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *writeURL = [dropDestination URLByAppendingPathComponent:filename];
// Create unique filename if already present
[fileData writeToURL:writeURL atomically:YES];
});
return @[filename];
}
*/
@end

View File

@@ -119,12 +119,20 @@
}
- (void)saveAttachmentForItem:(id)item toLocation:(NSURL *)location {
NSData *fileData = [self attachmentDataForItem:item];
if(!fileData) {
return; // No data to save;
}
NSError *error = nil;
if( ![fileData writeToURL:location options:NSDataWritingAtomic error:&error] ) {
[NSApp presentError:error];
}
}
- (NSData *)attachmentDataForItem:(id)item {
if([item isKindOfClass:[Kdb3Entry class]]) {
Kdb3Entry *entry = (Kdb3Entry *)item;
NSError *error = nil;
if(! [entry.binary writeToURL:location options:NSDataWritingAtomic error:&error] ) {
[NSApp presentError:error];
}
return entry.binary;
}
else if([item isKindOfClass:[BinaryRef class]]) {
Binary *binary = [self findBinary:item];
@@ -137,16 +145,21 @@
else {
rawData = [NSMutableData mutableDataWithBase64DecodedData:[binary.data dataUsingEncoding:NSUTF8StringEncoding]];
}
NSError *error = nil;
if( ![rawData writeToURL:location options:NSDataWritingAtomic error:&error] ) {
[NSApp presentError:error];
}
return rawData;
}
}
else {
NSAssert(NO, @"Item is neither BinaryRef nor Kdb3Entry");
return;
return nil;
}
- (NSString *)attachmenFileNameForItem:(id)item {
if([item isKindOfClass:[Kdb3Entry class]]) {
Kdb3Entry *entry = (Kdb3Entry *)item;
return entry.binaryDesc;
}
else if([item isKindOfClass:[BinaryRef class]]) {
return ((BinaryRef *)item).key;
}
return nil;
}
- (NSUInteger)nextBinaryId {

View File

@@ -111,6 +111,8 @@ APPKIT_EXTERN NSString *const MPDocumnetDidChangeCurrentEntryNotification;
item can be either a BinaryRef or an Kdb3Entry.
*/
- (void)saveAttachmentForItem:(id)item toLocation:(NSURL *)location;
- (NSData *)attachmentDataForItem:(id)item;
- (NSString *)attachmenFileNameForItem:(id)item;
- (void)removeAttachment:(BinaryRef *)reference fromEntry:(KdbEntry *)anEntry;
- (void)removeAttachmentFromEntry:(KdbEntry *)anEntry;
- (NSUInteger)nextBinaryId;