From 55d56956764159f763f01b88c36429905d7b2f84 Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 8 Aug 2014 18:34:38 +0200 Subject: [PATCH] Hotkey is stored in defaults --- MacPass/DDHotKey+Keydata.h | 2 +- MacPass/DDHotKey+Keydata.m | 27 ++++++++++++++++++---- MacPass/MPAutotypeDaemon.h | 3 ++- MacPass/MPAutotypeDaemon.m | 46 ++++++++++++++++++++++++++++++-------- MacPass/MPSettingsHelper.h | 2 +- MacPass/MPSettingsHelper.m | 2 +- 6 files changed, 65 insertions(+), 17 deletions(-) diff --git a/MacPass/DDHotKey+Keydata.h b/MacPass/DDHotKey+Keydata.h index a5510a7d..f2c707ad 100644 --- a/MacPass/DDHotKey+Keydata.h +++ b/MacPass/DDHotKey+Keydata.h @@ -11,6 +11,6 @@ @interface DDHotKey (Keydata) - (NSData *)keyData; +- (instancetype)initWithKeyData:(NSData *)data taks:(DDHotKeyTask)task; - (instancetype)initWithKeyData:(NSData *)data; - @end diff --git a/MacPass/DDHotKey+Keydata.m b/MacPass/DDHotKey+Keydata.m index db9497b5..e45c9991 100644 --- a/MacPass/DDHotKey+Keydata.m +++ b/MacPass/DDHotKey+Keydata.m @@ -11,10 +11,19 @@ @implementation DDHotKey (Keydata) - (instancetype)initWithKeyData:(NSData *)data { - NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; - unsigned short keyCode = [unarchiver decodeIntForKey:NSStringFromSelector(@selector(keyCode))]; - NSUInteger modiferFlags = [unarchiver decodeIntegerForKey:NSStringFromSelector(@selector(modifierFlags))]; - self = [DDHotKey hotKeyWithKeyCode:keyCode modifierFlags:modiferFlags task:nil]; + self = [self initWithKeyData:data taks:nil]; + return self; +} + +- (instancetype)initWithKeyData:(NSData *)data taks:(DDHotKeyTask)task{ + NSUInteger modifierFlags; + unsigned short keyCode; + if([self _getKeyCode:&keyCode modifierFlags:&modifierFlags fromData:data]) { + self = [DDHotKey hotKeyWithKeyCode:keyCode modifierFlags:modifierFlags task:task]; + } + else { + self = nil; + } return self; } @@ -27,4 +36,14 @@ return [data copy]; } + +- (BOOL)_getKeyCode:(unsigned short *)keyCode modifierFlags:(NSUInteger *)modifierFlags fromData:(NSData *)data { + if(keyCode == NULL || modifierFlags == NULL || data == nil) { + return NO; + } + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; + *keyCode = [unarchiver decodeIntForKey:NSStringFromSelector(@selector(keyCode))]; + *modifierFlags = [unarchiver decodeIntegerForKey:NSStringFromSelector(@selector(modifierFlags))]; + return YES; +} @end diff --git a/MacPass/MPAutotypeDaemon.h b/MacPass/MPAutotypeDaemon.h index 8db6ed04..afa33d79 100644 --- a/MacPass/MPAutotypeDaemon.h +++ b/MacPass/MPAutotypeDaemon.h @@ -8,7 +8,7 @@ #import -@class KPKEntry; +@class DDHotKey; /** * The autotype daemon is repsonsible for registering the globa hotkey and to perform any autotype actions @@ -17,6 +17,7 @@ @property (strong) IBOutlet NSWindow *matchSelectionWindow; @property (weak) IBOutlet NSPopUpButton *matchSelectionButton; +@property (readonly, strong) DDHotKey *registredHotKey; - (IBAction)executeAutotypeWithSelectedMatch:(id)sender; - (IBAction)cancelAutotypeSelection:(id)sender; diff --git a/MacPass/MPAutotypeDaemon.m b/MacPass/MPAutotypeDaemon.m index 4c8bd0ac..616da205 100644 --- a/MacPass/MPAutotypeDaemon.m +++ b/MacPass/MPAutotypeDaemon.m @@ -21,6 +21,7 @@ #import "KPKEntry.h" #import "DDHotKeyCenter.h" +#import "DDHotKey+Keydata.h" #import NSString *const kMPWindowTitleKey = @"windowTitle"; @@ -28,12 +29,14 @@ NSString *const kMPApplciationNameKey = @"applicationName"; /* Enable to activate autotype + */ #define MP_AUTOTYPE -*/ @interface MPAutotypeDaemon () @property (nonatomic, assign) BOOL enabled; +@property (nonatomic, copy) NSData *hotKeyData; +@property (strong) DDHotKey *registredHotKey; @property (copy) NSString *targetApplicationName; @property (copy) NSString *targetWindowTitle; @@ -52,12 +55,18 @@ NSString *const kMPApplciationNameKey = @"applicationName"; toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEnableGlobalAutotype] options:nil]; + + [self bind:NSStringFromSelector(@selector(hotKeyData)) + toObject:[NSUserDefaultsController sharedUserDefaultsController] + withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyGlobalAutotypeKeyDataKey] + options:nil]; } return self; } - (void)dealloc { [self unbind:NSStringFromSelector(@selector(enabled))]; + [self unbind:NSStringFromSelector(@selector(hotKeyData))]; } #pragma mark - @@ -72,6 +81,21 @@ NSString *const kMPApplciationNameKey = @"applicationName"; } } +- (void)setHotKeyData:(NSData *)hotKeyData { + if(![_hotKeyData isEqualToData:hotKeyData]) { +#ifdef MP_AUTOTYPE + [self _unregisterHotKey]; +#endif + _hotKeyData = [hotKeyData copy]; +#ifdef MP_AUTOTYPE + if(self.enabled) { + [self _registerHotKey]; + } +#endif + } +} + + #pragma mark - #pragma mark Actions @@ -100,12 +124,12 @@ NSString *const kMPApplciationNameKey = @"applicationName"; if(useCurrentWindowAndApplication) { [self _updateTargetApplicationAndWindow]; } - + MPDocument *document = [self _findAutotypeDocument]; if(!document) { return; // nothing to do } - + MPAutotypeContext *context = [self _autotypeContextInDocument:document forWindowTitle:self.targetWindowTitle]; [self _performAutotypeForContext:context]; } @@ -167,15 +191,19 @@ NSString *const kMPApplciationNameKey = @"applicationName"; #pragma mark Hotkey Registration - (void)_registerHotKey { - [[DDHotKeyCenter sharedHotKeyCenter] registerHotKeyWithKeyCode:kVK_ANSI_M - modifierFlags:(NSCommandKeyMask | NSAlternateKeyMask ) - target:self - action:@selector(_didPressHotKey) - object:nil]; + if(nil == self.hotKeyData) { + return; // No hotkey data defined, so nothign to do + } + __weak MPAutotypeDaemon *welf = self; + DDHotKey *storedHotkey = [[DDHotKey alloc] initWithKeyData:self.hotKeyData taks:^(NSEvent *event) { + [welf _didPressHotKey]; + }]; + self.registredHotKey = [[DDHotKeyCenter sharedHotKeyCenter] registerHotKey:storedHotkey]; } - (void)_unregisterHotKey { - [[DDHotKeyCenter sharedHotKeyCenter] unregisterHotKeysWithTarget:self action:@selector(_didPressHotKey)]; + [[DDHotKeyCenter sharedHotKeyCenter] unregisterHotKey:self.registredHotKey]; + self.registredHotKey = nil; } - (NSDictionary *)_frontMostApplicationInfoDict { diff --git a/MacPass/MPSettingsHelper.h b/MacPass/MPSettingsHelper.h index d5c7e481..6c650029 100644 --- a/MacPass/MPSettingsHelper.h +++ b/MacPass/MPSettingsHelper.h @@ -49,7 +49,7 @@ APPKIT_EXTERN NSString *const kMPSettingsKeyRememberKeyFilesForDatabases; /* Autotype */ APPKIT_EXTERN NSString *const kMPSettingsKeySendCommandForControlKey; APPKIT_EXTERN NSString *const kMPSettingsKeyEnableGlobalAutotype; -APPKIT_EXTERN NSString *const kMPSettingsKeyGlobalAutotypeKeyKey; +APPKIT_EXTERN NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey; APPKIT_EXTERN NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown; /* Search */ diff --git a/MacPass/MPSettingsHelper.m b/MacPass/MPSettingsHelper.m index 6228d8e7..0e4a48be 100644 --- a/MacPass/MPSettingsHelper.m +++ b/MacPass/MPSettingsHelper.m @@ -35,7 +35,7 @@ NSString *const kMPSettingsKeyRememberKeyFilesForDatabases = @"Rememb NSString *const kMPSettingsKeySendCommandForControlKey = @"SendCommandKeyForControlKey"; NSString *const kMPSettingsKeyEnableGlobalAutotype = @"EnableGlobalAutotype"; -NSString *const kMPSettingsKeyGlobalAutotypeKeyKey = @"GlobalAutotypeKey"; +NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey = @"GlobalAutotypeKeyDataKey"; NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown = @"DocumentsAutotypeFixNoteWasShown"; NSString *const kMPSettingsKeyEntrySearchFilterMode = @"EntrySearchFilterMode";