Drag and Drop support for File attachments

This commit is contained in:
michael starke
2013-08-02 00:10:08 +02:00
parent 560d9ce941
commit 9d22494bf4
5 changed files with 90 additions and 0 deletions

View File

@@ -253,6 +253,7 @@
4CE39ABF16ECE34A000FE29D /* MPIconSelectViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE39ABE16ECE34A000FE29D /* MPIconSelectViewController.m */; };
4CE39AC116ECE359000FE29D /* IconSelection.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4CE39AC016ECE359000FE29D /* IconSelection.xib */; };
4CE39AC416ECE4F7000FE29D /* MPPopupImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE39AC316ECE4F7000FE29D /* MPPopupImageView.m */; };
4CE3E62617AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE3E62517AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.m */; };
4CE5B54B173AFBA700207B39 /* MPDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE5B549173AFBA700207B39 /* MPDocument.m */; };
4CE8246F16E2E93400573141 /* MPOverlayWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE8246E16E2E93400573141 /* MPOverlayWindowController.m */; };
4CE8247516E2F2B900573141 /* MPOverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CE8247416E2F2B900573141 /* MPOverlayView.m */; };
@@ -742,6 +743,8 @@
4CE39AC016ECE359000FE29D /* IconSelection.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = IconSelection.xib; sourceTree = "<group>"; };
4CE39AC216ECE4F7000FE29D /* MPPopupImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPPopupImageView.h; sourceTree = "<group>"; };
4CE39AC316ECE4F7000FE29D /* MPPopupImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPPopupImageView.m; sourceTree = "<group>"; };
4CE3E62417AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPAttachmentTableDataSource.h; sourceTree = "<group>"; };
4CE3E62517AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPAttachmentTableDataSource.m; sourceTree = "<group>"; };
4CE5B548173AFBA700207B39 /* MPDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDocument.h; sourceTree = "<group>"; };
4CE5B549173AFBA700207B39 /* MPDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDocument.m; sourceTree = "<group>"; };
4CE8246D16E2E93400573141 /* MPOverlayWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPOverlayWindowController.h; sourceTree = "<group>"; };
@@ -1156,6 +1159,8 @@
4C37A83F15B8B474005EF8EE /* MPOutlineDataSource.m */,
4C569D9F17652BFE00595B62 /* MPEntryTableDataSource.h */,
4C569DA017652BFE00595B62 /* MPEntryTableDataSource.m */,
4CE3E62417AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.h */,
4CE3E62517AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.m */,
6E719715172058BA00E4C5FC /* MPDatabaseVersion.h */,
4CE5B548173AFBA700207B39 /* MPDocument.h */,
4CE5B549173AFBA700207B39 /* MPDocument.m */,
@@ -2084,6 +2089,7 @@
4C6BEA3117A88E6C00CF69A8 /* MPStripLineBreaksTransformer.m in Sources */,
4C79E80317A9187400AC6CD2 /* KPKXmlHeaderWriter.m in Sources */,
4CFB5E9F17AAD20200D9B10C /* Kdb4Group+Undo.m in Sources */,
4CE3E62617AB0D2D00D9E4B4 /* MPAttachmentTableDataSource.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -0,0 +1,13 @@
//
// MPAttachmentTableDataSource.h
// MacPass
//
// Created by Michael Starke on 01.08.13.
// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MPAttachmentTableDataSource : NSObject <NSTableViewDataSource>
@end

View File

@@ -0,0 +1,60 @@
//
// MPAttachmentTableDataSource.m
// MacPass
//
// Created by Michael Starke on 01.08.13.
// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved.
//
#import "MPAttachmentTableDataSource.h"
#import "MPDocument.h"
@implementation MPAttachmentTableDataSource
- (NSDragOperation)tableView:(NSTableView *)tableView
validateDrop:(id<NSDraggingInfo>)info
proposedRow:(NSInteger)row
proposedDropOperation:(NSTableViewDropOperation)dropOperation {
NSPasteboard *draggingPasteBoard = [info draggingPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSURL class]];
NSArray *arrayOfURLs = [draggingPasteBoard readObjectsForClasses:classArray options:nil];
NSUInteger numberOfDirectories = 0;
for(NSURL *url in arrayOfURLs) {
if([url isFileURL] || [url isFileReferenceURL]) {
NSError *error = nil;
NSDictionary *resourceKeys = [url resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
if( [resourceKeys[ NSURLIsDirectoryKey ] boolValue] == YES ) {
numberOfDirectories++;
}
continue;
}
return NSDragOperationNone;
}
if(numberOfDirectories == [arrayOfURLs count]) {
return NSDragOperationNone;
}
row = [tableView numberOfRows];
return NSDragOperationCopy;
}
- (BOOL)tableView:(NSTableView *)tableView
acceptDrop:(id<NSDraggingInfo>)info
row:(NSInteger)row
dropOperation:(NSTableViewDropOperation)dropOperation {
MPDocument *document = [[[tableView window] windowController] document];
id entry = document.selectedEntry;
NSPasteboard *draggingPasteBoard = [info draggingPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSURL class]];
NSArray *arrayOfURLs = [draggingPasteBoard readObjectsForClasses:classArray options:nil];
for(NSURL *fileUrl in arrayOfURLs) {
[document addAttachment:fileUrl toEntry:entry];
}
return YES;
}
@end

View File

@@ -20,6 +20,10 @@
- (void)addAttachment:(NSURL *)location toEntry:(KdbEntry *)anEntry {
NSError *error = nil;
NSDictionary *resourceKeys = [location resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
if([resourceKeys[ NSURLIsDirectoryKey ] boolValue] == YES ) {
return; // We do not add whol directories
}
NSString *fileName = [location lastPathComponent];
if([anEntry isKindOfClass:[Kdb3Entry class]]) {
Kdb3Entry *entry = (Kdb3Entry *)anEntry;

View File

@@ -10,6 +10,7 @@
#import "MPAttachmentTableViewDelegate.h"
#import "MPCustomFieldTableViewDelegate.h"
#import "MPPasswordCreatorViewController.h"
#import "MPAttachmentTableDataSource.h"
#import "MPDocument.h"
#import "MPIconHelper.h"
@@ -34,6 +35,8 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
NSArrayController *_customFieldsController;
MPAttachmentTableViewDelegate *_attachmentTableDelegate;
MPCustomFieldTableViewDelegate *_customFieldTableDelegate;
MPAttachmentTableDataSource *_attachmentDataSource;
}
@property (nonatomic, assign) BOOL showPassword;
@@ -58,6 +61,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
_customFieldsController = [[NSArrayController alloc] init];
_attachmentTableDelegate = [[MPAttachmentTableViewDelegate alloc] init];
_customFieldTableDelegate = [[MPCustomFieldTableViewDelegate alloc] init];
_attachmentDataSource = [[MPAttachmentTableDataSource alloc] init];
_attachmentTableDelegate.viewController = self;
_customFieldTableDelegate.viewController = self;
_activeTab = MPEntryTabGeneral;
@@ -107,10 +111,13 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
[_attachmentTableView setBackgroundColor:[NSColor clearColor]];
[_attachmentTableView bind:NSContentBinding toObject:_attachmentsController withKeyPath:@"arrangedObjects" options:nil];
[_attachmentTableView setDelegate:_attachmentTableDelegate];
[_attachmentTableView setDataSource:_attachmentDataSource];
[_attachmentTableView registerForDraggedTypes:@[NSFilenamesPboardType]];
/* Set background to clearcolor so we can draw in the scrollview */
[_customFieldsTableView setBackgroundColor:[NSColor clearColor]];
[_customFieldsTableView bind:NSContentBinding toObject:_customFieldsController withKeyPath:@"arrangedObjects" options:nil];
[_customFieldsTableView setDelegate:_customFieldTableDelegate];
[self.passwordTextField bind:@"showPassword" toObject:self withKeyPath:@"showPassword" options:nil];
[self.togglePassword bind:NSValueBinding toObject:self withKeyPath:@"showPassword" options:nil];