2
0
MasterPassword/platform-darwin/Source/iOS/MPEmergencyViewController.m

176 lines
5.3 KiB
Mathematica
Raw Normal View History

2017-04-05 20:56:22 +00:00
//==============================================================================
// This file is part of Master Password.
// Copyright (c) 2011-2017, Maarten Billemont.
//
2017-04-05 20:56:22 +00:00
// Master Password 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.
//
2017-04-05 20:56:22 +00:00
// Master Password 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.
//
2017-04-05 20:56:22 +00:00
// You can find a copy of the GNU General Public License in the
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
//==============================================================================
#import "MPEmergencyViewController.h"
#import "MPEntities.h"
2014-04-13 17:04:18 +00:00
@implementation MPEmergencyViewController {
MPKey *_key;
NSOperationQueue *_emergencyKeyQueue;
NSOperationQueue *_emergencyPasswordQueue;
}
- (void)viewDidLoad {
[super viewDidLoad];
2014-04-13 17:04:18 +00:00
[_emergencyKeyQueue = [NSOperationQueue new] setMaxConcurrentOperationCount:1];
[_emergencyPasswordQueue = [NSOperationQueue new] setMaxConcurrentOperationCount:1];
self.view.backgroundColor = [UIColor clearColor];
2014-04-13 17:04:18 +00:00
self.dialogView.layer.cornerRadius = 5;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self reset];
PearlAddNotificationObserver( UIApplicationWillResignActiveNotification, nil, [NSOperationQueue mainQueue],
^(MPEmergencyViewController *self, NSNotification *note) {
[self performSegueWithIdentifier:@"unwind-popover" sender:self];
} );
[self.scrollView automaticallyAdjustInsetsForKeyboard];
2014-04-13 17:04:18 +00:00
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
PearlRemoveNotificationObservers();
PearlRemoveNotificationObserversFrom( self.scrollView );
2014-04-13 17:04:18 +00:00
[self reset];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
2014-04-13 17:04:18 +00:00
#pragma mark - Actions
- (IBAction)controlChanged:(UIControl *)control {
if (control == self.fullNameField || control == self.masterPasswordField)
2014-04-13 17:04:18 +00:00
[self updateKey];
else
[self updatePassword];
}
- (IBAction)copyPassword:(id)sender {
NSString *sitePassword = [self.passwordButton titleForState:UIControlStateNormal];
if ([sitePassword length]) {
[UIPasteboard generalPasteboard].string = sitePassword;
[UIView animateWithDuration:0.3f animations:^{
2017-04-21 02:29:10 +00:00
self.tipContainer.visible = YES;
} completion:^(BOOL finished) {
2017-04-21 02:29:10 +00:00
PearlMainQueueAfter( 3, ^{
[UIView animateWithDuration:0.3f animations:^{
self.tipContainer.visible = NO;
}];
} );
}];
2014-04-13 17:04:18 +00:00
}
}
#pragma mark - Private
- (void)updateKey {
NSString *fullName = self.fullNameField.text;
2014-04-13 17:04:18 +00:00
NSString *masterPassword = self.masterPasswordField.text;
[self.passwordButton setTitle:nil forState:UIControlStateNormal];
2014-04-13 17:04:18 +00:00
[self.activity startAnimating];
[_emergencyKeyQueue cancelAllOperations];
[_emergencyKeyQueue addOperationWithBlock:^{
if ([masterPassword length] && [fullName length])
_key = [[MPKey alloc] initForFullName:fullName withMasterPassword:masterPassword];
2014-04-13 17:04:18 +00:00
else
_key = nil;
PearlMainQueue( ^{
[self updatePassword];
} );
}];
}
- (void)updatePassword {
NSString *siteName = self.siteField.text;
2014-09-21 14:39:09 +00:00
MPSiteType siteType = [self siteType];
2014-04-13 17:04:18 +00:00
NSUInteger siteCounter = (NSUInteger)self.counterStepper.value;
2014-04-25 02:00:38 +00:00
self.counterLabel.text = strf( @"%lu", (unsigned long)siteCounter );
2014-04-13 17:04:18 +00:00
[self.passwordButton setTitle:nil forState:UIControlStateNormal];
2014-04-13 17:04:18 +00:00
[self.activity startAnimating];
[_emergencyPasswordQueue cancelAllOperations];
[_emergencyPasswordQueue addOperationWithBlock:^{
NSString *sitePassword = nil;
if (_key && [siteName length])
sitePassword = [MPAlgorithmDefault generatePasswordForSiteNamed:siteName ofType:siteType withCounter:siteCounter usingKey:_key];
2014-04-13 17:04:18 +00:00
PearlMainQueue( ^{
[self.activity stopAnimating];
[self.passwordButton setTitle:sitePassword forState:UIControlStateNormal];
2014-04-13 17:04:18 +00:00
} );
}];
}
2014-09-21 14:39:09 +00:00
- (enum MPSiteType)siteType {
2014-04-13 17:04:18 +00:00
switch (self.typeControl.selectedSegmentIndex) {
case 0:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedMaximum;
2014-04-13 17:04:18 +00:00
case 1:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedLong;
2014-04-13 17:04:18 +00:00
case 2:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedMedium;
2014-04-13 17:04:18 +00:00
case 3:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedBasic;
2014-04-13 17:04:18 +00:00
case 4:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedShort;
2014-04-13 17:04:18 +00:00
case 5:
2014-09-21 14:39:09 +00:00
return MPSiteTypeGeneratedPIN;
2014-04-13 17:04:18 +00:00
default:
Throw( @"Unsupported type index: %ld", (long)self.typeControl.selectedSegmentIndex );
2014-04-13 17:04:18 +00:00
}
}
- (void)reset {
self.fullNameField.text = nil;
2014-04-13 17:04:18 +00:00
self.masterPasswordField.text = nil;
self.siteField.text = nil;
self.counterStepper.value = 1;
self.typeControl.selectedSegmentIndex = 1;
[self updateKey];
}
@end