2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-04-12 18:43:41 +00:00
|
|
|
//
|
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.
|
2014-04-12 18:43:41 +00:00
|
|
|
//
|
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.
|
2014-04-12 18:43:41 +00:00
|
|
|
//
|
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/>.
|
|
|
|
//==============================================================================
|
2014-04-12 18:43:41 +00:00
|
|
|
|
|
|
|
#import "MPEmergencyViewController.h"
|
|
|
|
#import "MPEntities.h"
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@interface MPEmergencyViewController()
|
|
|
|
|
|
|
|
@property(nonatomic, strong) MPKey *key;
|
|
|
|
@property(nonatomic, strong) NSOperationQueue *emergencyKeyQueue;
|
|
|
|
@property(nonatomic, strong) NSOperationQueue *emergencyPasswordQueue;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPEmergencyViewController
|
2014-04-12 18:43:41 +00:00
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.emergencyKeyQueue = [NSOperationQueue new] setMaxConcurrentOperationCount:1];
|
|
|
|
[self.emergencyPasswordQueue = [NSOperationQueue new] setMaxConcurrentOperationCount:1];
|
2014-04-13 17:04:18 +00:00
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
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];
|
2014-09-27 05:27:05 +00:00
|
|
|
PearlAddNotificationObserver( UIApplicationWillResignActiveNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPEmergencyViewController *self, NSNotification *note) {
|
|
|
|
[self performSegueWithIdentifier:@"unwind-popover" sender:self];
|
|
|
|
} );
|
2014-09-29 02:15:55 +00:00
|
|
|
|
|
|
|
[self.scrollView automaticallyAdjustInsetsForKeyboard];
|
2014-04-13 17:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidDisappear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewDidDisappear:animated];
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
PearlRemoveNotificationObservers();
|
2014-09-29 02:15:55 +00:00
|
|
|
PearlRemoveNotificationObserversFrom( self.scrollView );
|
2014-04-13 17:04:18 +00:00
|
|
|
[self reset];
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
- (UIStatusBarStyle)preferredStatusBarStyle {
|
|
|
|
|
|
|
|
return UIStatusBarStyleLightContent;
|
|
|
|
}
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
#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 {
|
2014-04-12 18:43:41 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
if (control == self.fullNameField || control == self.masterPasswordField)
|
2014-04-13 17:04:18 +00:00
|
|
|
[self updateKey];
|
|
|
|
else
|
|
|
|
[self updatePassword];
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:27:36 +00:00
|
|
|
- (IBAction)copyPassword:(id)sender {
|
|
|
|
|
|
|
|
NSString *sitePassword = [self.passwordButton titleForState:UIControlStateNormal];
|
2017-04-29 21:50:48 +00:00
|
|
|
if (![sitePassword length])
|
|
|
|
return;
|
|
|
|
|
|
|
|
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
|
|
|
if ([pasteboard respondsToSelector:@selector( setItems:options: )])
|
|
|
|
[pasteboard setItems:@[ @{ UIPasteboardTypeAutomatic: sitePassword } ]
|
|
|
|
options:@{
|
|
|
|
UIPasteboardOptionLocalOnly : @NO,
|
|
|
|
UIPasteboardOptionExpirationDate: [NSDate dateWithTimeIntervalSinceNow:3 * 60]
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
pasteboard.string = sitePassword;
|
|
|
|
|
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
|
|
|
self.tipContainer.visible = YES;
|
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
PearlMainQueueAfter( 3, ^{
|
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
|
|
|
self.tipContainer.visible = NO;
|
|
|
|
}];
|
|
|
|
} );
|
|
|
|
}];
|
2014-04-13 17:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)updateKey {
|
2014-04-12 18:43:41 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSString *fullName = self.fullNameField.text;
|
2014-04-13 17:04:18 +00:00
|
|
|
NSString *masterPassword = self.masterPasswordField.text;
|
2014-04-12 18:43:41 +00:00
|
|
|
|
2017-04-18 02:27:36 +00:00
|
|
|
[self.passwordButton setTitle:nil forState:UIControlStateNormal];
|
2014-04-13 17:04:18 +00:00
|
|
|
[self.activity startAnimating];
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.emergencyKeyQueue cancelAllOperations];
|
|
|
|
[self.emergencyKeyQueue addOperationWithBlock:^{
|
2015-02-28 15:01:41 +00:00
|
|
|
if ([masterPassword length] && [fullName length])
|
2017-05-07 22:36:01 +00:00
|
|
|
self.key = [[MPKey alloc] initForFullName:fullName withMasterPassword:masterPassword];
|
2014-04-13 17:04:18 +00:00
|
|
|
else
|
2017-05-07 22:36:01 +00:00
|
|
|
self.key = nil;
|
2014-04-13 17:04:18 +00:00
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
[self updatePassword];
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatePassword {
|
|
|
|
|
|
|
|
NSString *siteName = self.siteField.text;
|
2017-08-13 01:57:47 +00:00
|
|
|
MPResultType 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
|
|
|
|
2017-04-18 02:27:36 +00:00
|
|
|
[self.passwordButton setTitle:nil forState:UIControlStateNormal];
|
2014-04-13 17:04:18 +00:00
|
|
|
[self.activity startAnimating];
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.emergencyPasswordQueue cancelAllOperations];
|
|
|
|
[self.emergencyPasswordQueue addOperationWithBlock:^{
|
2014-04-13 17:04:18 +00:00
|
|
|
NSString *sitePassword = nil;
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.key && [siteName length])
|
2017-08-13 01:57:47 +00:00
|
|
|
sitePassword = [MPAlgorithmDefault mpwTemplateForSiteNamed:siteName ofType:siteType withCounter:siteCounter
|
|
|
|
usingKey:self.key];
|
2014-04-13 17:04:18 +00:00
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
[self.activity stopAnimating];
|
2017-04-18 02:27:36 +00:00
|
|
|
[self.passwordButton setTitle:sitePassword forState:UIControlStateNormal];
|
2014-04-13 17:04:18 +00:00
|
|
|
} );
|
|
|
|
}];
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (MPResultType)siteType {
|
2014-04-13 17:04:18 +00:00
|
|
|
|
|
|
|
switch (self.typeControl.selectedSegmentIndex) {
|
|
|
|
case 0:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplateMaximum;
|
2014-04-13 17:04:18 +00:00
|
|
|
case 1:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplateLong;
|
2014-04-13 17:04:18 +00:00
|
|
|
case 2:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplateMedium;
|
2014-04-13 17:04:18 +00:00
|
|
|
case 3:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplateBasic;
|
2014-04-13 17:04:18 +00:00
|
|
|
case 4:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplateShort;
|
2014-04-13 17:04:18 +00:00
|
|
|
case 5:
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPResultTypeTemplatePIN;
|
2014-04-13 17:04:18 +00:00
|
|
|
default:
|
2014-09-27 05:27:05 +00:00
|
|
|
Throw( @"Unsupported type index: %ld", (long)self.typeControl.selectedSegmentIndex );
|
2014-04-13 17:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reset {
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
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];
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|