From 4f55d934517b5720cc1242b23c816b0a21f4fdc8 Mon Sep 17 00:00:00 2001 From: michael starke Date: Tue, 11 Feb 2014 20:58:27 +0100 Subject: [PATCH] Restrucuterde Autotype commands a bit --- MacPass/MPAutotypeCommand.h | 7 ++++++ MacPass/MPAutotypeCommand.m | 4 ++++ MacPass/MPAutotypeDaemon.m | 11 ++++------ MacPass/MPAutotypeKeyPress.h | 7 +++++- MacPass/MPAutotypeKeyPress.m | 16 +++++++++++++- MacPass/MPAutotypePaste.h | 5 +++++ MacPass/MPAutotypePaste.m | 32 +++++++++++++++------------- MacPass/MPDocument+Autotype.h | 2 +- MacPass/MPDocument+Autotype.m | 11 +++++----- MacPass/MPKeyMapper.h | 2 +- MacPass/MPUpdateSettingsController.m | 1 - 11 files changed, 66 insertions(+), 32 deletions(-) diff --git a/MacPass/MPAutotypeCommand.h b/MacPass/MPAutotypeCommand.h index db5fc5ca..36234fd9 100644 --- a/MacPass/MPAutotypeCommand.h +++ b/MacPass/MPAutotypeCommand.h @@ -45,4 +45,11 @@ extern NSString *const kMPAutptypeCommandEnter; */ - (void)execute; +/** + * Validates the command and returns the result + * + * @return YES if the command is valid and can be executed. NO otherwise + */ +- (BOOL)isValid; + @end diff --git a/MacPass/MPAutotypeCommand.m b/MacPass/MPAutotypeCommand.m index 04c9e11a..aa46adb5 100644 --- a/MacPass/MPAutotypeCommand.m +++ b/MacPass/MPAutotypeCommand.m @@ -118,4 +118,8 @@ NSString *const kMPAutotypeCommandF1 = @"{F1}"; - (void)execute { NSAssert(NO, @"Not Implemented"); } + +- (BOOL)isValid { + return NO; // No valid command +} @end diff --git a/MacPass/MPAutotypeDaemon.m b/MacPass/MPAutotypeDaemon.m index 00601ffa..4bf2aa84 100644 --- a/MacPass/MPAutotypeDaemon.m +++ b/MacPass/MPAutotypeDaemon.m @@ -25,12 +25,7 @@ NSString *const kMPApplciationNameKey = @"applicationName"; - (id)init { self = [super init]; if (self) { - /* - Test the system for enabled access for assistive devices. Otherwise we cannot work properly - - Use defaults to determine if global hotkey is enabled [self _registerHotKey]; - */ } return self; } @@ -49,7 +44,9 @@ NSString *const kMPApplciationNameKey = @"applicationName"; break; } } - + if(currentDocument.encrypted) { + return; // No need to search in closed documents + } /* Determine the window title of the current front most application Start searching the db for the best fit (based on title, then on window associations @@ -63,7 +60,7 @@ NSString *const kMPApplciationNameKey = @"applicationName"; Query the document to generate a autotype command list for the window title We do not care where this came form, just get the autotype commands */ - NSArray *autotypeCandidates = [[currentDocument buildContextsForWindowTitle:windowTitle] lastObject]; + NSArray *autotypeCandidates = [[currentDocument autotypContextsForWindowTitle:windowTitle] lastObject]; NSUInteger candiates = [autotypeCandidates count]; if(candiates == 0) { return; // No Entries found. diff --git a/MacPass/MPAutotypeKeyPress.h b/MacPass/MPAutotypeKeyPress.h index 2730f6b1..4cd99b0a 100644 --- a/MacPass/MPAutotypeKeyPress.h +++ b/MacPass/MPAutotypeKeyPress.h @@ -8,7 +8,12 @@ #import "MPAutotypeCommand.h" - +/** + * Autotype command to press a single key. Can be used with modifer keys as well + */ @interface MPAutotypeKeyPress : MPAutotypeCommand +@property (assign) CGEventFlags modifierMask; +@property (assign) CGKeyCode keyCode; + @end diff --git a/MacPass/MPAutotypeKeyPress.m b/MacPass/MPAutotypeKeyPress.m index 5818fe0d..5f0fe8fc 100644 --- a/MacPass/MPAutotypeKeyPress.m +++ b/MacPass/MPAutotypeKeyPress.m @@ -7,11 +7,25 @@ // #import "MPAutotypeKeyPress.h" +#import "MPKeyMapper.h" @implementation MPAutotypeKeyPress - (void)execute { - + if(![self isValid]) { + return; // no valid command. Stop. + } + CGKeyCode mappedKey = [self _transformKeyCode]; + [self sendPressKey:mappedKey modifierFlags:self.modifierMask]; +} + +- (BOOL)isValid { + return ([self _transformKeyCode] != kMPUnknownKeyCode); +} + +- (CGKeyCode)_transformKeyCode { + NSString *key = [MPKeyMapper stringForKey:self.keyCode]; + return [MPKeyMapper keyCodeForCharacter:key]; } @end diff --git a/MacPass/MPAutotypePaste.h b/MacPass/MPAutotypePaste.h index fced2bfb..81491d93 100644 --- a/MacPass/MPAutotypePaste.h +++ b/MacPass/MPAutotypePaste.h @@ -8,6 +8,11 @@ #import "MPAutotypeCommand.h" +/** + * Simple Paste action. Uses the Clipboard to copy and then paste contents in place + */ @interface MPAutotypePaste : MPAutotypeCommand +- (instancetype)initWithString:(NSString *)aString; + @end diff --git a/MacPass/MPAutotypePaste.m b/MacPass/MPAutotypePaste.m index 1b217336..66d0b916 100644 --- a/MacPass/MPAutotypePaste.m +++ b/MacPass/MPAutotypePaste.m @@ -13,29 +13,31 @@ @interface MPAutotypePaste () -@property (retain) NSString *commandString; +@property (strong) NSString *pasteData; @end @implementation MPAutotypePaste -/** - * Simple copy paste action - */ -- (void)executeWithEntry:(KPKEntry *)entry { - if([self.commandString length] > 0) { +- (instancetype)initWithString:(NSString *)aString { + self = [super init]; + if(self) { + self.pasteData = aString; + } + return self; +} + +- (void)execute { + if([self.pasteData length] > 0) { MPPasteBoardController *controller = [MPPasteBoardController defaultController]; - if([self.commandString isPlaceholder]) { - BOOL didReplace; - NSString *evaluatedPlaceholder = [self.commandString evaluatePlaceholderWithEntry:entry didReplace:&didReplace]; - [controller copyObjects:@[evaluatedPlaceholder]]; - } - else { - [controller copyObjects:@[self.commandString]]; - } - /* Find the correct key code! */ + [controller copyObjects:@[self.pasteData]]; [self sendPasteKeyCode]; } } +- (BOOL)isValid { + /* Pasting shoudl always be valid */ + return YES; +} + @end diff --git a/MacPass/MPDocument+Autotype.h b/MacPass/MPDocument+Autotype.h index eaef60a4..fe29ebf5 100644 --- a/MacPass/MPDocument+Autotype.h +++ b/MacPass/MPDocument+Autotype.h @@ -24,6 +24,6 @@ @interface MPDocument (Autotype) -- (NSArray *)buildContextsForWindowTitle:(NSString *)windowTitle; +- (NSArray *)autotypContextsForWindowTitle:(NSString *)windowTitle; @end diff --git a/MacPass/MPDocument+Autotype.m b/MacPass/MPDocument+Autotype.m index d71f2e24..d2fe9186 100644 --- a/MacPass/MPDocument+Autotype.m +++ b/MacPass/MPDocument+Autotype.m @@ -30,21 +30,22 @@ @implementation MPDocument (Autotype) -- (NSArray *)buildContextsForWindowTitle:(NSString *)windowTitle { +- (NSArray *)autotypContextsForWindowTitle:(NSString *)windowTitle { NSArray *autotypeEntries = [self.root autotypeableChildEntries]; NSMutableArray *contexts = [[NSMutableArray alloc] initWithCapacity:ceil([autotypeEntries count] / 4.0)]; for(KPKEntry *entry in autotypeEntries) { /* Test for title */ NSRange titleRange = [entry.title rangeOfString:windowTitle options:NSCaseInsensitiveSearch]; + MPAutotypeContext *context; if(titleRange.location != NSNotFound && titleRange.length != 0) { - MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultSequence]; - [contexts addObject:context]; - + context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultSequence]; } /* search in Autotype entries for match */ else { KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle]; - MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithWindowAssociation:association]; + context = [[MPAutotypeContext alloc] initWithWindowAssociation:association]; + } + if(context) { [contexts addObject:context]; } } diff --git a/MacPass/MPKeyMapper.h b/MacPass/MPKeyMapper.h index b7b825cc..045a2298 100644 --- a/MacPass/MPKeyMapper.h +++ b/MacPass/MPKeyMapper.h @@ -15,7 +15,7 @@ extern uint16_t const kMPUnknownKeyCode; /** * Retrieves the string representation with the current keyboard mapping for the keycode * - * @param keyCode The virutal keycode to be pressed + * @param keyCode The virtual keycode to be pressed * @return NSString containing the current mapping for the keyCode */ + (NSString *)stringForKey:(CGKeyCode)keyCode; diff --git a/MacPass/MPUpdateSettingsController.m b/MacPass/MPUpdateSettingsController.m index 7b5d979b..a72f0164 100644 --- a/MacPass/MPUpdateSettingsController.m +++ b/MacPass/MPUpdateSettingsController.m @@ -31,5 +31,4 @@ return NSLocalizedString(@"UPDATE_SETTINGS", @"Update Settings Label"); } - @end