Rudimentary auto type is working. Still some kinks on timing, etc.

This commit is contained in:
michael starke
2014-03-21 20:13:10 +01:00
parent 63f002aacd
commit 638f3ab9f9
7 changed files with 41 additions and 45 deletions

View File

@@ -20,11 +20,5 @@
@property (weak) IBOutlet NSButton *performAutotypeButton; @property (weak) IBOutlet NSButton *performAutotypeButton;
- (void)exectureAutotypeForEntry:(KPKEntry *)entry withWindowTitle:(NSString *)title; - (void)exectureAutotypeForEntry:(KPKEntry *)entry withWindowTitle:(NSString *)title;
/**
* Called by the selection window to start the autotype sequence
*
* @param sender sender of the action.
*/
- (IBAction)executeAutotypeWithSelectedMatch:(id)sender;
@end @end

View File

@@ -12,6 +12,7 @@
#import "MPDocument+Autotype.h" #import "MPDocument+Autotype.h"
#import "MPAutotypeCommand.h" #import "MPAutotypeCommand.h"
#import "MPAutotypeContext.h" #import "MPAutotypeContext.h"
#import "MPAutotypePaste.h"
#import "MPPasteBoardController.h" #import "MPPasteBoardController.h"
#import "MPSettingsHelper.h" #import "MPSettingsHelper.h"
@@ -28,6 +29,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
@interface MPAutotypeDaemon () @interface MPAutotypeDaemon ()
@property (nonatomic, assign) BOOL enabled; @property (nonatomic, assign) BOOL enabled;
@property (copy) NSString *lastFrontMostApplication;
@end @end
@@ -53,18 +55,15 @@ NSString *const kMPApplciationNameKey = @"applicationName";
- (void)setEnabled:(BOOL)enabled { - (void)setEnabled:(BOOL)enabled {
if(_enabled != enabled) { if(_enabled != enabled) {
_enabled = enabled; _enabled = enabled;
//self.enabled ? [self _registerHotKey] : [self _unregisterHotKey]; self.enabled ? [self _registerHotKey] : [self _unregisterHotKey];
} }
} }
- (void)exectureAutotypeForEntry:(KPKEntry *)entry withWindowTitle:(NSString *)title {
NSAssert(NO,@"Not Implemented");
}
- (void)executeAutotypeWithSelectedMatch:(id)sender { - (void)executeAutotypeWithSelectedMatch:(id)sender {
NSMenuItem *item = [self.matchSelectionButton selectedItem]; NSMenuItem *item = [self.matchSelectionButton selectedItem];
MPAutotypeContext *context = [item representedObject]; MPAutotypeContext *context = [item representedObject];
[self.matchSelectionWindow orderOut:self]; [self.matchSelectionWindow orderOut:self];
[self _performAutotypeForContext:context];
} }
- (void)_didPressHotKey { - (void)_didPressHotKey {
@@ -76,7 +75,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
break; break;
} }
} }
if(currentDocument.encrypted) { if(!currentDocument) {
return; // No need to search in closed documents return; // No need to search in closed documents
} }
/* /*
@@ -85,15 +84,15 @@ NSString *const kMPApplciationNameKey = @"applicationName";
*/ */
NSDictionary *frontApplicationInfoDict = [self _frontMostApplicationInfoDict]; NSDictionary *frontApplicationInfoDict = [self _frontMostApplicationInfoDict];
NSString *windowTitle = frontApplicationInfoDict[kMPWindowTitleKey]; NSString *windowTitle = frontApplicationInfoDict[kMPWindowTitleKey];
NSString *applicationName = frontApplicationInfoDict[kMPApplciationNameKey]; self.lastFrontMostApplication = frontApplicationInfoDict[kMPApplciationNameKey];
NSLog(@"Looking for entries matching window title:%@ of applciation: %@", windowTitle, applicationName); //NSLog(@"Looking for entries matching window title:%@ of applciation: %@", windowTitle, applicationName);
/* /*
Query the document to generate a autotype command list for the window title 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 We do not care where this came form, just get the autotype commands
*/ */
NSArray *autotypeCandidates = [currentDocument autotypContextsForWindowTitle:windowTitle]; NSArray *autotypeCandidates = [currentDocument autotypContextsForWindowTitle:windowTitle];
NSUInteger candiates = [autotypeCandidates count]; NSInteger candiates = [autotypeCandidates count];
if(candiates == 0) { if(candiates == 0) {
return; // No Entries found. return; // No Entries found.
} }
@@ -101,24 +100,23 @@ NSString *const kMPApplciationNameKey = @"applicationName";
[self _presentSelectionWindow:autotypeCandidates]; [self _presentSelectionWindow:autotypeCandidates];
return; // Nothing to do, we get called back by the window return; // Nothing to do, we get called back by the window
} }
/* Just in case it's not there anymore, order the app for the window we want to autotype back to the foreground! */ [self _performAutotypeForContext:autotypeCandidates[0]];
[self _orderApplicationToFront:applicationName]; }
/*
Implement!
*/
return;
KPKEntry *selectedEntry = currentDocument.selectedEntry; - (void)_performAutotypeForContext:(MPAutotypeContext *)context {
if(nil == currentDocument || nil == selectedEntry) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
return; // no open documents, no selected entry NSArray *commands = [MPAutotypeCommand commandsForContext:context];
} [MPAutotypeDaemon _orderApplicationToFront:self.lastFrontMostApplication];
usleep(1000*1000);
/* TODO: BOOL lastCommandWasPaste = NO;
Replace entry based palce holders for(MPAutotypeCommand *command in commands) {
Replace global placeholders if(lastCommandWasPaste) {
Translate to paste/copy commands usleep(500*1000);
Find correct key-codes for current keyboard layout to perform paste command }
*/ [command execute];
lastCommandWasPaste = [command isKindOfClass:[MPAutotypePaste class]];
}
});
} }
- (void)_registerHotKey { - (void)_registerHotKey {
@@ -161,8 +159,9 @@ NSString *const kMPApplciationNameKey = @"applicationName";
[associationMenu addItemWithTitle:NSLocalizedString(@"SELECT_AUTOTYPE_CANDIDATE", "") action:NULL keyEquivalent:@""]; [associationMenu addItemWithTitle:NSLocalizedString(@"SELECT_AUTOTYPE_CANDIDATE", "") action:NULL keyEquivalent:@""];
[associationMenu addItem:[NSMenuItem separatorItem]]; [associationMenu addItem:[NSMenuItem separatorItem]];
[associationMenu setAutoenablesItems:NO]; [associationMenu setAutoenablesItems:NO];
NSString *entryMask = NSLocalizedString(@"TITLE_%@_USERNAME_%@_PASSWORD_%@_AUTOTYPE_SEQUENCE_%@", "Mask to create autotype entries for selection by the user. %1 Title %2 Username %3 Password %4 Sequence");
for(MPAutotypeContext *context in candidates) { for(MPAutotypeContext *context in candidates) {
NSString *title = [[NSString alloc] initWithFormat:@"%@: %@", context.entry.title, context.command]; NSString *title = [[NSString alloc] initWithFormat:entryMask, context.entry.title, context.entry.username, context.entry.password, context.command];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:0 keyEquivalent:@""]; NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:0 keyEquivalent:@""];
[item setRepresentedObject:context]; [item setRepresentedObject:context];
[associationMenu addItem:item]; [associationMenu addItem:item];
@@ -172,11 +171,12 @@ NSString *const kMPApplciationNameKey = @"applicationName";
/* Setup Items in Popup */ /* Setup Items in Popup */
} }
- (void)_orderApplicationToFront:(NSString *)applicationName { + (void)_orderApplicationToFront:(NSString *)applicationName {
NSString *appleScript = [[NSString alloc] initWithFormat:@"activate application %@", applicationName]; NSLog(@"Moving %@ to the front.", applicationName);
NSString *appleScript = [[NSString alloc] initWithFormat:@"activate application \"%@\"", applicationName];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:appleScript]; NSAppleScript *script = [[NSAppleScript alloc] initWithSource:appleScript];
NSDictionary *error; NSDictionary *error;
[script executeAndReturnError:&error]; NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&error];
} }

View File

@@ -28,6 +28,7 @@
} }
- (void)execute { - (void)execute {
[NSThread isMainThread] ? NSLog(@"MainThread") : NSLog(@"NonMainThread");
if([self.pasteData length] > 0) { if([self.pasteData length] > 0) {
MPPasteBoardController *controller = [MPPasteBoardController defaultController]; MPPasteBoardController *controller = [MPPasteBoardController defaultController];
[controller copyObjects:@[self.pasteData]]; [controller copyObjects:@[self.pasteData]];

View File

@@ -41,7 +41,7 @@
[self.enableServerCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:serverKeyPath options:nil]; [self.enableServerCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:serverKeyPath options:nil];
[self.enableServerCheckbutton setEnabled:NO]; [self.enableServerCheckbutton setEnabled:NO];
[self.enableGlobalAutotypeCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:globalAutotypeKeyPath options:nil]; [self.enableGlobalAutotypeCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:globalAutotypeKeyPath options:nil];
[self.enableGlobalAutotypeCheckbutton setEnabled:NO]; //[self.enableGlobalAutotypeCheckbutton setEnabled:NO];
[self.enableQuicklookCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:quicklookKeyPath options:nil]; [self.enableQuicklookCheckbutton bind:NSValueBinding toObject:defaultsController withKeyPath:quicklookKeyPath options:nil];
} }

View File

@@ -73,6 +73,7 @@ NSString *const MPPasteBoardControllerDidClearClipboard = @"com.hicknhack.macpas
} }
- (void)copyObjects:(NSArray *)objects { - (void)copyObjects:(NSArray *)objects {
NSLog(@"ShoudlCopy %@", objects);
/* Should we save the old content ?*/ /* Should we save the old content ?*/
[[NSPasteboard generalPasteboard] clearContents]; [[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] writeObjects:objects]; [[NSPasteboard generalPasteboard] writeObjects:objects];

View File

@@ -1,10 +1,10 @@
;// //
// MPTemporaryFileStorage.m // MPTemporaryFileStorage.m
// MacPass // MacPass
// //
// Created by Michael Starke on 18/03/14. // Created by Michael Starke on 18/03/14.
// Copyright (c) 2014 HicknHack Software GmbH. All rights reserved. // Copyright (c) 2014 HicknHack Software GmbH. All rights reserved.
// //
#import "MPTemporaryFileStorage.h" #import "MPTemporaryFileStorage.h"
#import "MPTemporaryFileStorageCenter.h" #import "MPTemporaryFileStorageCenter.h"

Binary file not shown.