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
MPActionToggleQuicklook,
MPActionShowHistory, // History anzeigen
MPActionExitHistory // History ausblenden
MPActionExitHistory, // History ausblenden
MPActionPerformAutotypeForSelectedEntry // Perform Autotype for selected Entry
};
/**
* Helper to retrieve commonly used actions

View File

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

View File

@@ -30,6 +30,10 @@
* Command with placeholders and references resolved
*/
@property (nonatomic, readonly, copy) NSString *evaluatedCommand;
/**
* @return YES if valid, NO otherwise
*/
@property (nonatomic, readonly, assign) BOOL isValid;
/**
* Designated initializer
@@ -42,11 +46,5 @@
- (instancetype)initWithEntry:(KPKEntry *)entry andSequence:(NSString *)sequence;
- (instancetype)initWithDefaultSequenceForEntry:(KPKEntry *)entry;
- (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

View File

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

View File

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

View File

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

View File

@@ -26,6 +26,7 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) {
BOOL const insertCopy = MPIsFlagSetInOptions(MPContextMenuCopy, flags);
BOOL const insertTrash = MPIsFlagSetInOptions(MPContextMenuTrash, flags);
BOOL const insertDuplicate = MPIsFlagSetInOptions(MPContextMenuDuplicate, flags);
BOOL const insertAutotype = MPIsFlagSetInOptions(MPContextMenuAutotype, flags);
NSMutableArray *items = [NSMutableArray arrayWithCapacity:10];
if(insertCreate) {
@@ -97,6 +98,14 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) {
[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;
}

View File

@@ -50,8 +50,10 @@
if(!windowTitle) {
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))];
MPAutotypeContext *context;
for(KPKEntry *entry in autotypeEntries) {
/* TODO:
@@ -68,7 +70,6 @@
if (titleRange.location == NSNotFound || titleRange.length == 0) {
titleRange = [entry.title rangeOfString:windowTitle options:NSCaseInsensitiveSearch];
}
MPAutotypeContext *context;
if(titleRange.location != NSNotFound && titleRange.length != 0) {
context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultKeystrokeSequence];
}
@@ -77,7 +78,14 @@
KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle];
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];
}
}

View File

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

View File

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

View File

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

Binary file not shown.