2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-09-21 14:49:57 +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-09-21 14:49:57 +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-09-21 14:49:57 +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-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
#import "MPAnswersViewController.h"
|
|
|
|
#import "MPiOSAppDelegate.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
2014-09-27 05:27:05 +00:00
|
|
|
#import "MPOverlayViewController.h"
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@interface MPAnswersViewController()
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@property(nonatomic, strong) NSManagedObjectID *siteOID;
|
|
|
|
@property(nonatomic) BOOL multiple;
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
@end
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@implementation MPAnswersViewController
|
2014-09-22 02:45:21 +00:00
|
|
|
|
|
|
|
#pragma mark - Life
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
|
|
self.tableView.tableHeaderView = [UIView new];
|
|
|
|
self.tableView.tableFooterView = [UIView new];
|
2014-09-22 12:48:51 +00:00
|
|
|
self.view.backgroundColor = [UIColor clearColor];
|
2015-06-20 15:24:17 +00:00
|
|
|
|
|
|
|
[self.tableView automaticallyAdjustInsetsForKeyboard];
|
2014-09-22 12:48:51 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
2014-09-27 16:52:17 +00:00
|
|
|
PearlAddNotificationObserver( MPSignedOutNotification, nil, [NSOperationQueue mainQueue],
|
|
|
|
^(MPAnswersViewController *self, NSNotification *note) {
|
|
|
|
if (![note.userInfo[@"animated"] boolValue])
|
|
|
|
[UIView setAnimationsEnabled:NO];
|
|
|
|
[[MPOverlaySegue dismissViewController:self] perform];
|
|
|
|
[UIView setAnimationsEnabled:YES];
|
|
|
|
} );
|
2014-09-27 05:27:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 14:14:19 +00:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
|
|
[self.view.window endEditing:YES];
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
2015-06-20 15:24:17 +00:00
|
|
|
PearlRemoveNotificationObserversFrom( self.tableView );
|
2014-09-27 05:27:05 +00:00
|
|
|
PearlRemoveNotificationObservers();
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
- (UIStatusBarStyle)preferredStatusBarStyle {
|
|
|
|
|
|
|
|
return UIStatusBarStyleLightContent;
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
#pragma mark - State
|
|
|
|
|
|
|
|
- (void)setSite:(MPSiteEntity *)site {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
self.siteOID = site.permanentObjectID;
|
|
|
|
self.multiple = [site.questions count] > 0;
|
2014-09-22 02:45:21 +00:00
|
|
|
[self.tableView reloadData];
|
2014-09-23 02:32:31 +00:00
|
|
|
[self updateAnimated:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMultiple:(BOOL)multiple animated:(BOOL)animated {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
self.multiple = multiple;
|
2014-09-23 02:32:31 +00:00
|
|
|
[self updateAnimated:animated];
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (MPSiteEntity *)siteInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
return [MPSiteEntity existingObjectWithID:self.siteOID inContext:context];
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
if (section == 0)
|
|
|
|
return 3;
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.multiple)
|
2014-09-22 02:45:21 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-10-05 05:22:28 +00:00
|
|
|
return [[self siteInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]].questions count] + 1;
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
MPSiteEntity *site = [self siteInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]];
|
|
|
|
if (indexPath.section == 0) {
|
|
|
|
if (indexPath.item == 0) {
|
|
|
|
MPGlobalAnswersCell *cell = [MPGlobalAnswersCell dequeueCellFromTableView:tableView indexPath:indexPath];
|
|
|
|
[cell setSite:site];
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
if (indexPath.item == 1)
|
|
|
|
return [MPSendAnswersCell dequeueCellFromTableView:tableView indexPath:indexPath];
|
|
|
|
if (indexPath.item == 2) {
|
|
|
|
MPMultipleAnswersCell *cell = [MPMultipleAnswersCell dequeueCellFromTableView:tableView indexPath:indexPath];
|
2017-05-07 22:36:01 +00:00
|
|
|
cell.accessoryType = self.multiple? UITableViewCellAccessoryCheckmark: UITableViewCellAccessoryNone;
|
2014-09-22 02:45:21 +00:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
Throw( @"Unsupported row index: %@", indexPath );
|
|
|
|
}
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
MPAnswersQuestionCell *cell = [MPAnswersQuestionCell dequeueCellFromTableView:tableView indexPath:indexPath];
|
2014-09-22 02:45:21 +00:00
|
|
|
MPSiteQuestionEntity *question = nil;
|
|
|
|
if ([site.questions count] > indexPath.item)
|
|
|
|
question = site.questions[indexPath.item];
|
2014-10-14 01:56:46 +00:00
|
|
|
[cell setQuestion:question forSite:site inVC:self];
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
#pragma mark - UITableViewDelegate
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
if (indexPath.section == 0) {
|
|
|
|
if (indexPath.item == 0)
|
|
|
|
return 133;
|
|
|
|
return 44;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 130;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
MPSiteEntity *site = [self siteInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]];
|
|
|
|
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
|
2017-04-29 21:50:48 +00:00
|
|
|
if ([cell isKindOfClass:[MPGlobalAnswersCell class]])
|
|
|
|
[self copyAnswer:((MPGlobalAnswersCell *)cell).answerField.text];
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
else if ([cell isKindOfClass:[MPMultipleAnswersCell class]]) {
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.multiple)
|
2014-09-23 02:32:31 +00:00
|
|
|
[self setMultiple:YES animated:YES];
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
else if (self.multiple) {
|
2014-09-23 02:32:31 +00:00
|
|
|
if (![site.questions count])
|
|
|
|
[self setMultiple:NO animated:YES];
|
|
|
|
|
|
|
|
else
|
2014-09-22 03:48:49 +00:00
|
|
|
[PearlAlert showAlertWithTitle:@"Remove Site Questions?" message:
|
|
|
|
@"Do you want to remove the questions you have configured for this site?"
|
|
|
|
viewStyle:UIAlertViewStyleDefault initAlert:nil
|
|
|
|
tappedButtonBlock:^(UIAlertView *alert, NSInteger buttonIndex) {
|
|
|
|
if (buttonIndex == [alert cancelButtonIndex])
|
|
|
|
return;
|
|
|
|
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPSiteEntity *site_ = [self siteInContext:context];
|
2014-09-27 05:27:05 +00:00
|
|
|
NSOrderedSet *questions = [site_.questions copy];
|
|
|
|
for (MPSiteQuestionEntity *question in questions)
|
|
|
|
[context deleteObject:question];
|
2014-09-22 03:48:49 +00:00
|
|
|
[context saveToStore];
|
2014-09-23 02:32:31 +00:00
|
|
|
[self setMultiple:NO animated:YES];
|
2014-09-22 03:48:49 +00:00
|
|
|
}];
|
|
|
|
} cancelTitle:@"Cancel" otherTitles:@"Remove Questions", nil];
|
2014-09-23 02:32:31 +00:00
|
|
|
}
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
2017-04-29 21:50:48 +00:00
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
else if ([cell isKindOfClass:[MPSendAnswersCell class]]) {
|
|
|
|
NSString *body;
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.multiple) {
|
2016-02-21 02:56:04 +00:00
|
|
|
NSObject *answer = [site resolveSiteAnswerUsingKey:[MPiOSAppDelegate get].key];
|
2014-09-22 02:45:21 +00:00
|
|
|
body = strf( @"Master Password generated the following security answer for your site: %@\n\n"
|
|
|
|
@"%@\n"
|
|
|
|
@"\n\nYou should use this as the answer to each security question the site asks you.\n"
|
|
|
|
@"Do not share this answer with others!", site.name, answer );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NSMutableString *bodyBuilder = [NSMutableString string];
|
|
|
|
[bodyBuilder appendFormat:@"Master Password generated the following security answers for your site: %@\n\n", site.name];
|
|
|
|
for (MPSiteQuestionEntity *question in site.questions) {
|
2016-02-21 02:56:04 +00:00
|
|
|
NSObject *answer = [question resolveQuestionAnswerUsingKey:[MPiOSAppDelegate get].key];
|
2014-09-22 02:45:21 +00:00
|
|
|
[bodyBuilder appendFormat:@"For question: '%@', use answer: %@\n", question.keyword, answer];
|
|
|
|
}
|
|
|
|
[bodyBuilder appendFormat:@"\n\nUse the answer for the matching security question.\n"
|
|
|
|
@"Do not share this answer with others!"];
|
|
|
|
body = bodyBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
[PearlEMail sendEMailTo:nil fromVC:self subject:strf( @"Master Password security answers for %@", site.name ) body:body];
|
|
|
|
}
|
2017-04-29 21:50:48 +00:00
|
|
|
|
|
|
|
else if ([cell isKindOfClass:[MPAnswersQuestionCell class]])
|
|
|
|
[self copyAnswer:((MPAnswersQuestionCell *)cell).answerField.text];
|
2014-09-22 02:45:21 +00:00
|
|
|
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
2014-09-21 14:49:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 21:50:48 +00:00
|
|
|
- (void)copyAnswer:(NSString *)answer {
|
|
|
|
|
|
|
|
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
|
|
|
if ([pasteboard respondsToSelector:@selector( setItems:options: )]) {
|
|
|
|
[pasteboard setItems:@[ @{ UIPasteboardTypeAutomatic: answer } ]
|
|
|
|
options:@{
|
|
|
|
UIPasteboardOptionLocalOnly : @NO,
|
|
|
|
UIPasteboardOptionExpirationDate: [NSDate dateWithTimeIntervalSinceNow:3 * 60]
|
|
|
|
}];
|
|
|
|
[PearlOverlay showTemporaryOverlayWithTitle:strl( @"Answer Copied (3 min)" ) dismissAfter:2];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pasteboard.string = answer;
|
|
|
|
[PearlOverlay showTemporaryOverlayWithTitle:strl( @"Answer Copied" ) dismissAfter:2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 02:32:31 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)updateAnimated:(BOOL)animated {
|
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
UITableViewCell *multipleAnswersCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0]];
|
2017-05-07 22:36:01 +00:00
|
|
|
multipleAnswersCell.accessoryType = self.multiple? UITableViewCellAccessoryCheckmark: UITableViewCellAccessoryNone;
|
2014-09-23 02:32:31 +00:00
|
|
|
|
|
|
|
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
|
|
|
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
|
|
|
|
}];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:56:46 +00:00
|
|
|
- (void)didAddQuestion:(MPSiteQuestionEntity *)question toSite:(MPSiteEntity *)site {
|
|
|
|
|
|
|
|
NSUInteger newQuestionRow = [site.questions count];
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
[self.tableView beginUpdates];
|
|
|
|
[self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:newQuestionRow inSection:1] ]
|
|
|
|
withRowAnimation:UITableViewRowAnimationAutomatic];
|
|
|
|
[self.tableView endUpdates];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:49:57 +00:00
|
|
|
@end
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
@implementation MPGlobalAnswersCell
|
|
|
|
|
|
|
|
#pragma mark - State
|
|
|
|
|
|
|
|
- (void)setSite:(MPSiteEntity *)site {
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
self.titleLabel.text = strl( @"Answer for %@:", site.name );
|
2014-09-22 02:45:21 +00:00
|
|
|
self.answerField.text = @"...";
|
2016-02-21 02:56:04 +00:00
|
|
|
[site resolveSiteAnswerUsingKey:[MPiOSAppDelegate get].key result:^(NSString *result) {
|
2014-09-22 02:45:21 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.answerField.text = result;
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
@implementation MPSendAnswersCell
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
@implementation MPMultipleAnswersCell
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@interface MPAnswersQuestionCell()
|
|
|
|
|
|
|
|
@property(nonatomic, strong) NSManagedObjectID *siteOID;
|
|
|
|
@property(nonatomic, strong) NSManagedObjectID *questionOID;
|
|
|
|
@property(nonatomic, weak) MPAnswersViewController *answersVC;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPAnswersQuestionCell
|
2014-09-22 02:45:21 +00:00
|
|
|
|
|
|
|
#pragma mark - State
|
|
|
|
|
2014-10-14 01:56:46 +00:00
|
|
|
- (void)setQuestion:(MPSiteQuestionEntity *)question forSite:(MPSiteEntity *)site inVC:(MPAnswersViewController *)answersVC {
|
2014-09-22 02:45:21 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
self.siteOID = site.permanentObjectID;
|
|
|
|
self.questionOID = question.permanentObjectID;
|
|
|
|
self.answersVC = answersVC;
|
2014-09-22 02:45:21 +00:00
|
|
|
|
|
|
|
[self updateAnswerForQuestion:question ofSite:site];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITextFieldDelegate
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
|
|
|
|
|
|
[textField resignFirstResponder];
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)textFieldDidChange:(UITextField *)textField {
|
|
|
|
|
|
|
|
NSString *keyword = textField.text;
|
|
|
|
[MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-10-14 01:56:46 +00:00
|
|
|
BOOL didAddQuestionObject = NO;
|
2017-05-07 22:36:01 +00:00
|
|
|
MPSiteEntity *site = [MPSiteEntity existingObjectWithID:self.siteOID inContext:context];
|
|
|
|
MPSiteQuestionEntity *question = [MPSiteQuestionEntity existingObjectWithID:self.questionOID inContext:context];
|
2014-09-27 20:30:17 +00:00
|
|
|
if (!question) {
|
2014-10-14 01:56:46 +00:00
|
|
|
didAddQuestionObject = YES;
|
2014-09-22 02:45:21 +00:00
|
|
|
[site addQuestionsObject:question = [MPSiteQuestionEntity insertNewObjectInContext:context]];
|
2014-09-27 20:30:17 +00:00
|
|
|
question.site = site;
|
|
|
|
}
|
2014-09-22 02:45:21 +00:00
|
|
|
|
|
|
|
question.keyword = keyword;
|
|
|
|
|
|
|
|
if ([context saveToStore]) {
|
2017-05-07 22:36:01 +00:00
|
|
|
self.questionOID = question.permanentObjectID;
|
2014-09-22 02:45:21 +00:00
|
|
|
[self updateAnswerForQuestion:question ofSite:site];
|
2014-10-14 01:56:46 +00:00
|
|
|
|
|
|
|
if (didAddQuestionObject)
|
2017-05-07 22:36:01 +00:00
|
|
|
[self.answersVC didAddQuestion:question toSite:site];
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)updateAnswerForQuestion:(MPSiteQuestionEntity *)question ofSite:(MPSiteEntity *)site {
|
|
|
|
|
|
|
|
if (!question)
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.questionField.text = self.answerField.text = nil;
|
|
|
|
} );
|
|
|
|
|
|
|
|
else {
|
|
|
|
NSString *keyword = question.keyword;
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.answerField.text = @"...";
|
|
|
|
} );
|
2016-02-21 02:56:04 +00:00
|
|
|
[question resolveQuestionAnswerUsingKey:[MPiOSAppDelegate get].key result:^(NSString *result) {
|
2014-09-22 02:45:21 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.questionField.text = keyword;
|
|
|
|
self.answerField.text = result;
|
|
|
|
} );
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 14:49:57 +00:00
|
|
|
|
|
|
|
@end
|