From dcef431c5c44cac7eb38a1bddd8fc46fddb29f1c Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 16 Aug 2013 01:54:14 +0200 Subject: [PATCH] Drag and Drop of files to Finder started --- MacPass/MPAttachmentTableDataSource.m | 62 +++++++++++++++++++++++++++ MacPass/MPDocument+Attachments.m | 35 ++++++++++----- MacPass/MPDocument.h | 2 + 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/MacPass/MPAttachmentTableDataSource.m b/MacPass/MPAttachmentTableDataSource.m index e27b5aa2..55a2ae79 100644 --- a/MacPass/MPAttachmentTableDataSource.m +++ b/MacPass/MPAttachmentTableDataSource.m @@ -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 diff --git a/MacPass/MPDocument+Attachments.m b/MacPass/MPDocument+Attachments.m index 35700980..b7d4b2eb 100644 --- a/MacPass/MPDocument+Attachments.m +++ b/MacPass/MPDocument+Attachments.m @@ -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 { diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index ba7f8893..6c540e27 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -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;