diff --git a/MacPass/KdbGroup+Undo.h b/MacPass/KdbGroup+Undo.h index 12dc2a73..0ba98ac9 100644 --- a/MacPass/KdbGroup+Undo.h +++ b/MacPass/KdbGroup+Undo.h @@ -17,10 +17,11 @@ APPKIT_EXTERN NSString *const MPGroupNameUndoableKey; - (NSString *)nameUndoable; - (void)setNameUndoable:(NSString *)newName; - - (void)addEntryUndoable:(KdbEntry *)entry; - (void)addGroupUndoable:(KdbGroup *)group; - (void)removeGroupUndoable:(KdbGroup *)group; - (void)removeEntryUndoable:(KdbEntry *)entry; +- (void)moveToGroupUndoable:(KdbGroup *)group; + @end diff --git a/MacPass/KdbGroup+Undo.m b/MacPass/KdbGroup+Undo.m index 1bcb5158..0e075604 100644 --- a/MacPass/KdbGroup+Undo.m +++ b/MacPass/KdbGroup+Undo.m @@ -58,4 +58,17 @@ NSString *const MPGroupNameUndoableKey = @"nameUndoable"; [[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_GROUP", @"Create Group Undo")]; [group.parent removeObjectFromGroupsAtIndex:index]; } + +- (void)moveToGroupUndoable:(KdbGroup *)group { + NSInteger index = [self.parent.groups indexOfObject:self]; + if(NSNotFound == index) { + return; // No object found + } + [[self undoManager] registerUndoWithTarget:self selector:@selector(moveToGroupUndoable:) object:self.parent]; + [[self undoManager] setActionName:NSLocalizedString(@"UNDO_MOVE_GROUP", @"Move Group Undo")]; + [self.parent removeObjectFromGroupsAtIndex:index]; + [group insertObject:self inGroupsAtIndex:[group.groups count]]; +} + + @end diff --git a/MacPass/MPOutlineDataSource.h b/MacPass/MPOutlineDataSource.h index 099a1a7c..6938abe4 100644 --- a/MacPass/MPOutlineDataSource.h +++ b/MacPass/MPOutlineDataSource.h @@ -8,7 +8,11 @@ #import -@interface MPOutlineDataSource : NSObject +APPKIT_EXTERN NSString *const MPPasteBoardType; +@class KdbGroup; +@interface MPOutlineDataSource : NSObject { + KdbGroup *_draggedItem; +} @end diff --git a/MacPass/MPOutlineDataSource.m b/MacPass/MPOutlineDataSource.m index 8a012972..374dc274 100644 --- a/MacPass/MPOutlineDataSource.m +++ b/MacPass/MPOutlineDataSource.m @@ -9,43 +9,45 @@ #import "MPOutlineDataSource.h" #import "MPDocument.h" #import "KdbLib.h" +#import "KdbGroup+Undo.h" + +NSString *const MPPasteBoardType = @"com.hicknhack.macpass.pasteboard"; @implementation MPOutlineDataSource -- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { - if(!item) { - return 1; + +- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard { + [_draggedItem release]; + _draggedItem = nil; + [pasteboard setString:@"Weee" forType:MPPasteBoardType]; + if([items count] == 1) { + _draggedItem = [[[items lastObject] representedObject] retain]; + return (nil != _draggedItem.parent); } - if( [item isKindOfClass:[KdbGroup class]]) { - KdbGroup *group = item; - return [[group groups] count]; - } - return 0; -} -- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { - if(!item) { - MPDocument *document = [[[outlineView window] windowController] document]; - return document.root; - } - if( [item isKindOfClass:[KdbGroup class]]) { - KdbGroup *group = item; - if( [[group groups] count] > index ) { - return [group groups][index]; - } - } - return nil; -} -- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { - if(!item) { - MPDocument *document = [[[outlineView window] windowController] document]; - return ([[document.root groups] count] > 0); - } - if([item isKindOfClass:[KdbGroup class]]) - { - KdbGroup *group = item; - return ([[group groups] count] > 0); - } - return NO; + return YES; } +- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id)info proposedItem:(id)item proposedChildIndex:(NSInteger)index { + if(_draggedItem) { + KdbGroup *target = [item representedObject]; + if( target == nil) { + return NSDragOperationNone; // Draggin over root + } + BOOL validParent = ( _draggedItem.parent == target && index != NSOutlineViewDropOnItemIndex); + if(validParent || _draggedItem.parent != target) { + return NSDragOperationMove; + info.animatesToDestination = YES; + } + } + return NSDragOperationNone; +} + +- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id)info item:(id)item childIndex:(NSInteger)index { + NSLog(@"Drag %@ to: %@ index: %ld", _draggedItem, [item representedObject], index); + KdbGroup *target = [item representedObject]; + BOOL accepted = (target != _draggedItem.parent); + info.animatesToDestination = YES; + [_draggedItem moveToGroupUndoable:target]; + return accepted; +} @end diff --git a/MacPass/MPOutlineViewController.m b/MacPass/MPOutlineViewController.m index f61ad866..1fabcfcd 100644 --- a/MacPass/MPOutlineViewController.m +++ b/MacPass/MPOutlineViewController.m @@ -13,6 +13,7 @@ #import "MPAppDelegate.h" #import "KdbLib.h" + @interface MPOutlineViewController () { BOOL _bindingEstablished; } @@ -42,8 +43,8 @@ if (self) { _treeController = [[NSTreeController alloc] init]; _bindingEstablished = NO; - self.outlineDelegate = [[[MPOutlineViewDelegate alloc] init] autorelease]; - self.datasource = [[[MPOutlineDataSource alloc] init] autorelease]; + _outlineDelegate = [[MPOutlineViewDelegate alloc] init]; + _datasource = [[MPOutlineDataSource alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didUpdateData:) @@ -71,11 +72,11 @@ } - (void)didLoadView { - //[self.outlineView setDataSource:self.datasource]; [self.outlineView setDelegate:self.outlineDelegate]; [self.outlineView setMenu:[self _contextMenu]]; [self.outlineView setAllowsEmptySelection:YES]; [self.outlineView setFloatsGroupRows:NO]; + [_outlineView registerForDraggedTypes:@[ MPPasteBoardType ]]; [self.outlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES]; } @@ -85,6 +86,7 @@ [_treeController setChildrenKeyPath:@"groups"]; [_treeController bind:NSContentBinding toObject:document withKeyPath:@"root" options:nil]; [_outlineView bind:NSContentBinding toObject:_treeController withKeyPath:@"arrangedObjects" options:nil]; + [_outlineView setDataSource:self.datasource]; _bindingEstablished = YES; } NSTreeNode *node = [_outlineView itemAtRow:0]; diff --git a/MacPass/MacPass-Info.plist b/MacPass/MacPass-Info.plist index 7c9a4689..27fd8355 100644 --- a/MacPass/MacPass-Info.plist +++ b/MacPass/MacPass-Info.plist @@ -48,7 +48,7 @@ CFBundleSignature ???? CFBundleVersion - A2F + A6E LSMinimumSystemVersion ${MACOSX_DEPLOYMENT_TARGET} NSHumanReadableCopyright