Autotype for selected entry is working rudimentarily

This commit is contained in:
michael starke
2014-12-17 11:20:30 +01:00
parent 3eb98a516c
commit 692ade2110
12 changed files with 80 additions and 58 deletions

View File

@@ -29,7 +29,8 @@ typedef NS_ENUM(NSUInteger, MPActionType) {
MPActionImportXML, // Import form XML MPActionImportXML, // Import form XML
MPActionToggleQuicklook, MPActionToggleQuicklook,
MPActionShowHistory, // History anzeigen MPActionShowHistory, // History anzeigen
MPActionExitHistory // History ausblenden MPActionExitHistory, // History ausblenden
MPActionPerformAutotypeForSelectedEntry // Perform Autotype for selected Entry
}; };
/** /**
* Helper to retrieve commonly used actions * Helper to retrieve commonly used actions

View File

@@ -19,26 +19,27 @@
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
actionDict = @{ actionDict = @{
@(MPActionAddEntry): NSStringFromSelector(@selector(createEntry:)), @(MPActionAddEntry): NSStringFromSelector(@selector(createEntry:)),
@(MPActionAddGroup): NSStringFromSelector(@selector(createGroup:)), @(MPActionAddGroup): NSStringFromSelector(@selector(createGroup:)),
@(MPActionDuplicateEntry): NSStringFromSelector(@selector(duplicateEntry:)), @(MPActionDuplicateEntry): NSStringFromSelector(@selector(duplicateEntry:)),
@(MPActionDuplicateEntryWithOptions): NSStringFromSelector(@selector(duplicateEntryWithOptions:)), @(MPActionDuplicateEntryWithOptions): NSStringFromSelector(@selector(duplicateEntryWithOptions:)),
@(MPActionCopyPassword): NSStringFromSelector(@selector(copyPassword:)), @(MPActionCopyPassword): NSStringFromSelector(@selector(copyPassword:)),
@(MPActionCopyURL): NSStringFromSelector(@selector(copyURL:)), @(MPActionCopyURL): NSStringFromSelector(@selector(copyURL:)),
@(MPActionCopyUsername): NSStringFromSelector(@selector(copyUsername:)), @(MPActionCopyUsername): NSStringFromSelector(@selector(copyUsername:)),
@(MPActionDelete): NSStringFromSelector(@selector(delete:)), @(MPActionDelete): NSStringFromSelector(@selector(delete:)),
@(MPActionEditPassword): NSStringFromSelector(@selector(editPassword:)), @(MPActionEditPassword): NSStringFromSelector(@selector(editPassword:)),
@(MPActionOpenURL): NSStringFromSelector(@selector(openURL:)), @(MPActionOpenURL): NSStringFromSelector(@selector(openURL:)),
@(MPActionToggleInspector): NSStringFromSelector(@selector(toggleInspector:)), @(MPActionToggleInspector): NSStringFromSelector(@selector(toggleInspector:)),
@(MPActionLock): NSStringFromSelector(@selector(lock:)), @(MPActionLock): NSStringFromSelector(@selector(lock:)),
@(MPActionEmptyTrash): NSStringFromSelector(@selector(emptyTrash:)), @(MPActionEmptyTrash): NSStringFromSelector(@selector(emptyTrash:)),
@(MPActionDatabaseSettings): NSStringFromSelector(@selector(showDatabaseSettings:)), @(MPActionDatabaseSettings): NSStringFromSelector(@selector(showDatabaseSettings:)),
@(MPActionEditTemplateGroup): NSStringFromSelector(@selector(editTemplateGroup:)), @(MPActionEditTemplateGroup): NSStringFromSelector(@selector(editTemplateGroup:)),
@(MPActionExportXML): NSStringFromSelector(@selector(exportAsXML:)), @(MPActionExportXML): NSStringFromSelector(@selector(exportAsXML:)),
@(MPActionImportXML): NSStringFromSelector(@selector(importFromXML:)), @(MPActionImportXML): NSStringFromSelector(@selector(importFromXML:)),
@(MPActionToggleQuicklook): NSStringFromSelector(@selector(toggleQuicklookPreview:)), @(MPActionToggleQuicklook): NSStringFromSelector(@selector(toggleQuicklookPreview:)),
@(MPActionShowHistory): NSStringFromSelector(@selector(showHistory:)), @(MPActionShowHistory): NSStringFromSelector(@selector(showHistory:)),
@(MPActionExitHistory): NSStringFromSelector(@selector(exitHistory:)) @(MPActionExitHistory): NSStringFromSelector(@selector(exitHistory:)),
@(MPActionPerformAutotypeForSelectedEntry): NSStringFromSelector(@selector(performAutotypeForEntry:))
}; };
}); });
return actionDict; return actionDict;

View File

@@ -30,6 +30,10 @@
* Command with placeholders and references resolved * Command with placeholders and references resolved
*/ */
@property (nonatomic, readonly, copy) NSString *evaluatedCommand; @property (nonatomic, readonly, copy) NSString *evaluatedCommand;
/**
* @return YES if valid, NO otherwise
*/
@property (nonatomic, readonly, assign) BOOL isValid;
/** /**
* Designated initializer * Designated initializer
@@ -42,11 +46,5 @@
- (instancetype)initWithEntry:(KPKEntry *)entry andSequence:(NSString *)sequence; - (instancetype)initWithEntry:(KPKEntry *)entry andSequence:(NSString *)sequence;
- (instancetype)initWithDefaultSequenceForEntry:(KPKEntry *)entry; - (instancetype)initWithDefaultSequenceForEntry:(KPKEntry *)entry;
- (instancetype)initWithWindowAssociation:(KPKWindowAssociation *)association; - (instancetype)initWithWindowAssociation:(KPKWindowAssociation *)association;
/**
* Returns YES if the given sequence is valid (currentyl only bracke missmatch is regarded as invalid
*
* @return YES if valid, NO otherwise
*/
- (BOOL)isValid;
@end @end

View File

@@ -20,7 +20,7 @@
@property (weak) IBOutlet NSPopUpButton *matchSelectionButton; @property (weak) IBOutlet NSPopUpButton *matchSelectionButton;
@property (readonly, strong) DDHotKey *registredHotKey; @property (readonly, strong) DDHotKey *registredHotKey;
- (void)performAutotypeForEntry:(KPKEntry *)entryOrNil; - (void)performAutotypeForEntry:(KPKEntry *)entry;
- (IBAction)performAutotypeWithSelectedMatch:(id)sender; - (IBAction)performAutotypeWithSelectedMatch:(id)sender;
- (IBAction)cancelAutotypeSelection:(id)sender; - (IBAction)cancelAutotypeSelection:(id)sender;

View File

@@ -34,8 +34,9 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
@property (nonatomic, assign) BOOL enabled; @property (nonatomic, assign) BOOL enabled;
@property (nonatomic, copy) NSData *hotKeyData; @property (nonatomic, copy) NSData *hotKeyData;
@property (strong) DDHotKey *registredHotKey; @property (strong) DDHotKey *registredHotKey;
@property (assign) pid_t targetPID; @property (assign) pid_t targetPID; // The pid of the process we want to sent commands to
@property (copy) NSString *targetWindowTitle; @property (copy) NSString *targetWindowTitle; // The title of the window that we are targeting
@property (strong) NSRunningApplication *previousApplication; // The application that was active before we got invoked
@end @end
@@ -43,7 +44,6 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
#pragma mark - #pragma mark -
#pragma mark Lifecylce #pragma mark Lifecylce
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
@@ -76,7 +76,6 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
#pragma mark - #pragma mark -
#pragma mark Properties #pragma mark Properties
- (void)setEnabled:(BOOL)enabled { - (void)setEnabled:(BOOL)enabled {
if(_enabled != enabled) { if(_enabled != enabled) {
_enabled = enabled; _enabled = enabled;
@@ -94,7 +93,18 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
} }
} }
- (void)executeAutotypeForEntry:(KPKEntry *)entry { #pragma mark -
#pragma mark Autotype Invocation
- (void)performAutotypeForEntry:(KPKEntry *)entry {
if(entry) {
[self _updateTargeInformationForApplication:self.previousApplication];
[self _performAutotypeForEntry:entry];
}
}
- (void)_didPressHotKey {
[self _updateTargetInfoForFrontMostApplication];
[self _performAutotypeForEntry:nil];
} }
#pragma mark - #pragma mark -
@@ -114,14 +124,9 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
} }
#pragma mark - #pragma mark -
#pragma mark Hotkey evaluation #pragma mark Autotype Execution
- (void)_didPressHotKey { - (void)_performAutotypeForEntry:(KPKEntry *)entryOrNil {
[self _updateTargetInfoForFrontMostApplication];
[self performAutotypeForEntry:nil];
}
- (void)performAutotypeForEntry:(KPKEntry *)entryOrNil {
NSInteger pid = [[NSProcessInfo processInfo] processIdentifier]; NSInteger pid = [[NSProcessInfo processInfo] processIdentifier];
if(self.targetPID == pid) { if(self.targetPID == pid) {
return; // We do not perform Autotype on ourselves return; // We do not perform Autotype on ourselves
@@ -200,7 +205,6 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
#pragma mark - #pragma mark -
#pragma mark Hotkey Registration #pragma mark Hotkey Registration
- (void)_registerHotKey { - (void)_registerHotKey {
__weak MPAutotypeDaemon *welf = self; __weak MPAutotypeDaemon *welf = self;
DDHotKeyTask aTask = ^(NSEvent *event) { DDHotKeyTask aTask = ^(NSEvent *event) {
@@ -272,23 +276,21 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
#pragma mark - #pragma mark -
#pragma mark MPDocument Notifications #pragma mark MPDocument Notifications
- (void)_didUnlockDatabase:(NSNotification *)notification { - (void)_didUnlockDatabase:(NSNotification *)notification {
/* Remove ourselves and call again to search matches */ /* Remove ourselves and call again to search matches */
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
[self performAutotypeForEntry:nil]; [self _performAutotypeForEntry:nil];
} }
#pragma mark - #pragma mark -
#pragma mark NSApplication Notifications #pragma mark NSApplication Notifications
- (void)_didDeactivateApplication:(NSNotification *)notification { - (void)_didDeactivateApplication:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo; NSDictionary *userInfo = notification.userInfo;
[self _updateTargeInformationForApplication:userInfo[NSWorkspaceApplicationKey]]; self.previousApplication = userInfo[NSWorkspaceApplicationKey];
} }
#pragma mark - #pragma mark -
#pragma mark Application information #pragma mark Application information
- (BOOL)_orderApplicationToFront:(pid_t)processIdentifier { - (BOOL)_orderApplicationToFront:(pid_t)processIdentifier {
NSRunningApplication *runingApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:processIdentifier]; NSRunningApplication *runingApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:processIdentifier];
NSRunningApplication *frontApplication = [[NSWorkspace sharedWorkspace] frontmostApplication]; NSRunningApplication *frontApplication = [[NSWorkspace sharedWorkspace] frontmostApplication];

View File

@@ -9,14 +9,15 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
typedef NS_OPTIONS(NSUInteger, MPContextMenuItemsFlags) { typedef NS_OPTIONS(NSUInteger, MPContextMenuItemsFlags) {
MPContextMenuCreate = 1 << 0, MPContextMenuCreate = 1 << 0,
MPContextMenuDelete = 1 << 1, MPContextMenuDelete = 1 << 1,
MPContextMenuCopy = 1 << 2, MPContextMenuCopy = 1 << 2,
MPContextMenuTrash = 1 << 3, MPContextMenuTrash = 1 << 3,
MPContextMenuDuplicate = 1 << 4, MPContextMenuDuplicate = 1 << 4,
MPContextMenuMinimal = MPContextMenuCreate | MPContextMenuDelete, MPContextMenuAutotype = 1 << 5,
MPContextMenuFull = MPContextMenuMinimal | MPContextMenuCopy | MPContextMenuDuplicate, MPContextMenuMinimal = MPContextMenuCreate | MPContextMenuDelete,
MPContextMenuExtended = MPContextMenuFull | MPContextMenuTrash MPContextMenuFull = MPContextMenuMinimal | MPContextMenuCopy | MPContextMenuDuplicate | MPContextMenuAutotype,
MPContextMenuExtended = MPContextMenuFull | MPContextMenuTrash
}; };
@interface MPContextMenuHelper : NSTableCellView @interface MPContextMenuHelper : NSTableCellView

View File

@@ -26,6 +26,7 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) {
BOOL const insertCopy = MPIsFlagSetInOptions(MPContextMenuCopy, flags); BOOL const insertCopy = MPIsFlagSetInOptions(MPContextMenuCopy, flags);
BOOL const insertTrash = MPIsFlagSetInOptions(MPContextMenuTrash, flags); BOOL const insertTrash = MPIsFlagSetInOptions(MPContextMenuTrash, flags);
BOOL const insertDuplicate = MPIsFlagSetInOptions(MPContextMenuDuplicate, flags); BOOL const insertDuplicate = MPIsFlagSetInOptions(MPContextMenuDuplicate, flags);
BOOL const insertAutotype = MPIsFlagSetInOptions(MPContextMenuAutotype, flags);
NSMutableArray *items = [NSMutableArray arrayWithCapacity:10]; NSMutableArray *items = [NSMutableArray arrayWithCapacity:10];
if(insertCreate) { if(insertCreate) {
@@ -97,6 +98,14 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) {
[items addObjectsFromArray:@[ copyUsername, copyPassword, urlItem]]; [items addObjectsFromArray:@[ copyUsername, copyPassword, urlItem]];
} }
if(insertAutotype) {
MPContextmenuHelperBeginSection(items);
NSMenuItem *performAutotype = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"PERFORM_AUTOTYPE_FOR_ENTRY", @"")
action:[MPActionHelper actionOfType:MPActionPerformAutotypeForSelectedEntry]
keyEquivalent:@"a"];
[performAutotype setKeyEquivalentModifierMask:[performAutotype keyEquivalentModifierMask] | NSControlKeyMask];
[items addObject:performAutotype];
}
return items; return items;
} }

View File

@@ -50,8 +50,10 @@
if(!windowTitle) { if(!windowTitle) {
return nil; return nil;
} }
NSArray *autotypeEntries = entry ? [[NSArray alloc] initWithObjects:entry, nil] : [self.root autotypeableChildEntries]; BOOL usePreferredEntry = (nil != entry);
NSArray *autotypeEntries = usePreferredEntry ? [[NSArray alloc] initWithObjects:entry, nil] : [self.root autotypeableChildEntries];
NSMutableArray *contexts = [[NSMutableArray alloc] initWithCapacity:MAX(1,ceil([autotypeEntries count] / 4.0))]; NSMutableArray *contexts = [[NSMutableArray alloc] initWithCapacity:MAX(1,ceil([autotypeEntries count] / 4.0))];
MPAutotypeContext *context;
for(KPKEntry *entry in autotypeEntries) { for(KPKEntry *entry in autotypeEntries) {
/* TODO: /* TODO:
@@ -68,7 +70,6 @@
if (titleRange.location == NSNotFound || titleRange.length == 0) { if (titleRange.location == NSNotFound || titleRange.length == 0) {
titleRange = [entry.title rangeOfString:windowTitle options:NSCaseInsensitiveSearch]; titleRange = [entry.title rangeOfString:windowTitle options:NSCaseInsensitiveSearch];
} }
MPAutotypeContext *context;
if(titleRange.location != NSNotFound && titleRange.length != 0) { if(titleRange.location != NSNotFound && titleRange.length != 0) {
context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultKeystrokeSequence]; context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultKeystrokeSequence];
} }
@@ -77,7 +78,14 @@
KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle]; KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle];
context = [[MPAutotypeContext alloc] initWithWindowAssociation:association]; context = [[MPAutotypeContext alloc] initWithWindowAssociation:association];
} }
if([context isValid]) { if(context.isValid) {
[contexts addObject:context];
}
}
/* Fall back to preferred Entry if no match was found */
if(contexts.count == 0 && usePreferredEntry) {
context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultKeystrokeSequence];
if(context.isValid) {
[contexts addObject:context]; [contexts addObject:context];
} }
} }

View File

@@ -713,6 +713,9 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
case MPActionOpenURL: case MPActionOpenURL:
valid &= (nil != targetEntry ) && ([targetEntry.url length] > 0); valid &= (nil != targetEntry ) && ([targetEntry.url length] > 0);
break; break;
case MPActionPerformAutotypeForSelectedEntry:
valid &= (nil != targetEntry);
break;
default: default:
break; break;
} }

View File

@@ -373,7 +373,7 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
- (void)performAutotypeForEntry:(id)sender { - (void)performAutotypeForEntry:(id)sender {
id<MPTargetNodeResolving> entryResolver = [NSApp targetForAction:@selector(currentTargetEntry)]; id<MPTargetNodeResolving> entryResolver = [NSApp targetForAction:@selector(currentTargetEntry)];
KPKEntry *targetEntry = [entryResolver currentTargetEntry]; KPKEntry *targetEntry = [entryResolver currentTargetEntry];
MPAutotypeDaemon *autotyped = ((MPAppDelegate *)[NSApplication sharedApplication]).autotypeDaemon; MPAutotypeDaemon *autotyped = ((MPAppDelegate *)[NSApplication sharedApplication].delegate).autotypeDaemon;
[autotyped performAutotypeForEntry:targetEntry]; [autotyped performAutotypeForEntry:targetEntry];
} }

View File

@@ -143,7 +143,6 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
selector:@selector(_didBecomFirstResponder:) selector:@selector(_didBecomFirstResponder:)
name:MPDidActivateViewNotification name:MPDidActivateViewNotification
object:_entryTable]; object:_entryTable];
/* Filter bar notifications */
[self _setupEntryMenu]; [self _setupEntryMenu];
NSTableColumn *parentColumn = [self.entryTable tableColumns][0]; NSTableColumn *parentColumn = [self.entryTable tableColumns][0];

Binary file not shown.