mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 14:02:28 +00:00
180 lines
6.0 KiB
Objective-C
180 lines
6.0 KiB
Objective-C
//
|
|
// MPPasswordInputController.m
|
|
// MacPass
|
|
//
|
|
// Created by Michael Starke on 17.02.13.
|
|
// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
//
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
//
|
|
|
|
#import "MPPasswordInputController.h"
|
|
#import "MPAppDelegate.h"
|
|
#import "MPDocumentWindowController.h"
|
|
#import "MPDocument.h"
|
|
#import "MPSettingsHelper.h"
|
|
#import "MPKeyfilePathControlDelegate.h"
|
|
|
|
#import "HNHUi/HNHUi.h"
|
|
|
|
#import "NSError+Messages.h"
|
|
|
|
@interface MPPasswordInputController ()
|
|
|
|
@property (weak) IBOutlet HNHUIRoundedSecureTextField *passwordTextField;
|
|
@property (weak) IBOutlet NSPathControl *keyPathControl;
|
|
@property (weak) IBOutlet NSImageView *errorImageView;
|
|
@property (weak) IBOutlet NSTextField *errorInfoTextField;
|
|
@property (weak) IBOutlet NSButton *togglePasswordButton;
|
|
@property (weak) IBOutlet NSButton *enablePasswordCheckBox;
|
|
@property (weak) IBOutlet NSButton *unlockButton;
|
|
@property (weak) IBOutlet NSButton *cancelButton;
|
|
|
|
@property (copy) NSString *message;
|
|
@property (copy) NSString *cancelLabel;
|
|
|
|
@property (assign) BOOL showPassword;
|
|
@property (nonatomic, assign) BOOL enablePassword;
|
|
@property (copy) passwordInputCompletionBlock completionHandler;
|
|
@end
|
|
|
|
@implementation MPPasswordInputController
|
|
|
|
- (NSString *)nibName {
|
|
return @"PasswordInputView";
|
|
}
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
|
if(self) {
|
|
_enablePassword = YES;
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_selectKeyURL) name:MPDidChangeStoredKeyFilesSettings object:nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
}
|
|
|
|
- (void)viewDidLoad {
|
|
self.errorImageView.image = [NSImage imageNamed:NSImageNameCaution];
|
|
[self.passwordTextField bind:NSStringFromSelector(@selector(showPassword)) toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
|
|
[self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
|
|
[self.enablePasswordCheckBox bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(enablePassword)) options:nil];
|
|
[self.togglePasswordButton bind:NSEnabledBinding toObject:self withKeyPath:NSStringFromSelector(@selector(enablePassword)) options:nil];
|
|
[self.passwordTextField bind:NSEnabledBinding toObject:self withKeyPath:NSStringFromSelector(@selector(enablePassword)) options:nil];
|
|
|
|
[self.passwordTextField setNextKeyView:self.keyPathControl];
|
|
[self _reset];
|
|
}
|
|
|
|
- (NSResponder *)reconmendedFirstResponder {
|
|
return self.passwordTextField;
|
|
}
|
|
|
|
- (void)requestPasswordWithMessage:(NSString *)message cancelLabel:(NSString *)cancelLabel completionHandler:(passwordInputCompletionBlock)completionHandler {
|
|
self.completionHandler = completionHandler;
|
|
self.message = message;
|
|
self.cancelLabel = cancelLabel;
|
|
[self _reset];
|
|
}
|
|
|
|
- (void)requestPasswordWithCompletionHandler:(passwordInputCompletionBlock)completionHandler {
|
|
[self requestPasswordWithMessage:nil cancelLabel:nil completionHandler:completionHandler];
|
|
}
|
|
|
|
#pragma mark Properties
|
|
- (void)setEnablePassword:(BOOL)enablePassword {
|
|
if(_enablePassword != enablePassword) {
|
|
_enablePassword = enablePassword;
|
|
if(!_enablePassword) {
|
|
self.passwordTextField.stringValue = @"";
|
|
}
|
|
}
|
|
if(_enablePassword) {
|
|
self.passwordTextField.placeholderString = NSLocalizedString(@"PASSWORD_INPUT_ENTER_PASSWORD", "Placeholder in the unlock-password input field if password is enabled");
|
|
}
|
|
else {
|
|
self.passwordTextField.placeholderString = NSLocalizedString(@"PASSWORD_INPUT_NO_PASSWORD", "Placeholder in the unlock-password input field if password is disabled");
|
|
}
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark Private
|
|
- (IBAction)_submit:(id)sender {
|
|
|
|
if(!self.completionHandler) {
|
|
return;
|
|
}
|
|
|
|
/* No password is different than an empty password */
|
|
NSError *error = nil;
|
|
NSString *password = self.enablePassword ? self.passwordTextField.stringValue : nil;
|
|
|
|
BOOL cancel = (sender == self.cancelButton);
|
|
BOOL result = self.completionHandler(password, self.keyPathControl.URL, cancel, &error);
|
|
if(cancel || result) {
|
|
return;
|
|
}
|
|
[self _showError:error];
|
|
/* do not shake if we are a sheet */
|
|
if(!self.view.window.isSheet) {
|
|
[self.view.window shakeWindow:nil];
|
|
}
|
|
}
|
|
|
|
- (IBAction)resetKeyFile:(id)sender {
|
|
/* If the reset was triggered by ourselves we want to preselect the keyfile */
|
|
if(sender == self) {
|
|
[self _selectKeyURL];
|
|
}
|
|
else {
|
|
self.keyPathControl.URL = nil;
|
|
}
|
|
}
|
|
|
|
- (void)_reset {
|
|
self.showPassword = NO;
|
|
self.enablePassword = YES;
|
|
self.passwordTextField.stringValue = @"";
|
|
self.errorInfoTextField.hidden = (nil == self.message);
|
|
if(self.message) {
|
|
self.errorInfoTextField.stringValue = self.message;
|
|
}
|
|
self.errorImageView.hidden = (nil == self.message);;
|
|
self.cancelButton.hidden = (nil == self.cancelLabel);
|
|
if(self.cancelLabel) {
|
|
self.cancelButton.stringValue = self.cancelLabel;
|
|
}
|
|
[self resetKeyFile:self];
|
|
}
|
|
|
|
- (void)_selectKeyURL {
|
|
MPDocument *document = self.windowController.document;
|
|
self.keyPathControl.URL = document.suggestedKeyURL;
|
|
}
|
|
|
|
- (void)_showError:(NSError *)error {
|
|
if(error) {
|
|
self.errorInfoTextField.stringValue = error.descriptionForErrorCode;
|
|
}
|
|
self.errorImageView.hidden = NO;
|
|
self.errorInfoTextField.hidden = NO;
|
|
}
|
|
|
|
@end
|