2
0

Fix deprecated NSAlert usage.

This commit is contained in:
Maarten Billemont 2016-04-23 12:22:15 -04:00
parent 15b14d67db
commit bbeb6bb948

View File

@ -23,14 +23,6 @@
#import "MPAppDelegate_Store.h" #import "MPAppDelegate_Store.h"
#import "MPAppDelegate_Key.h" #import "MPAppDelegate_Key.h"
#define MPAlertIncorrectMP @"MPAlertIncorrectMP"
#define MPAlertChangeMP @"MPAlertChangeMP"
#define MPAlertCreateSite @"MPAlertCreateSite"
#define MPAlertChangeType @"MPAlertChangeType"
#define MPAlertChangeLogin @"MPAlertChangeLogin"
#define MPAlertChangeContent @"MPAlertChangeContent"
#define MPAlertDeleteSite @"MPAlertDeleteSite"
@interface MPPasswordWindowController() @interface MPPasswordWindowController()
@property(nonatomic, strong) CAGradientLayer *siteGradient; @property(nonatomic, strong) CAGradientLayer *siteGradient;
@ -193,8 +185,7 @@
if (!success) if (!success)
[[NSAlert alertWithError:[NSError errorWithDomain:MPErrorDomain code:0 userInfo:@{ [[NSAlert alertWithError:[NSError errorWithDomain:MPErrorDomain code:0 userInfo:@{
NSLocalizedDescriptionKey : strf( @"Incorrect master password for user %@", userName ) NSLocalizedDescriptionKey : strf( @"Incorrect master password for user %@", userName )
}]] beginSheetModalForWindow:self.window modalDelegate:self }]] beginSheetModalForWindow:self.window completionHandler:nil];
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertIncorrectMP];
} ); } );
}]; }];
} }
@ -225,122 +216,6 @@
[self replaceFonts:rowView]; [self replaceFonts:rowView];
} }
#pragma mark - NSAlert
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if (contextInfo == MPAlertIncorrectMP)
return;
if (contextInfo == MPAlertChangeMP) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Reset" button.
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:context];
NSString *activeUserName = activeUser.name;
activeUser.keyID = nil;
[[MPMacAppDelegate get] forgetSavedKeyFor:activeUser];
[context saveToStore];
PearlMainQueue( ^{
NSAlert *alert_ = [NSAlert new];
alert_.messageText = @"Master Password Reset";
alert_.informativeText = strf( @"%@'s master password has been reset.\n\nYou can now set a new one by logging in.",
activeUserName );
[alert_ beginSheetModalForWindow:self.window modalDelegate:nil didEndSelector:NULL contextInfo:nil];
if ([MPMacAppDelegate get].key)
[[MPMacAppDelegate get] signOutAnimated:YES];
} );
}];
}
default:
break;
}
return;
}
if (contextInfo == MPAlertCreateSite) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Create" button.
[[MPMacAppDelegate get] addSiteNamed:[self.siteField stringValue] completion:
^(MPSiteEntity *site, NSManagedObjectContext *context) {
if (site)
PearlMainQueue( ^{ [self updateSites]; } );
}];
break;
}
default:
break;
}
}
if (contextInfo == MPAlertChangeType) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
MPSiteType type = (MPSiteType)[self.passwordTypesMatrix.selectedCell tag];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [[MPMacAppDelegate get] changeSite:[self.selectedSite entityInContext:context]
saveInContext:context toType:type];
if ([entity isKindOfClass:[MPStoredSiteEntity class]] && ![(MPStoredSiteEntity *)entity contentObject].length)
PearlMainQueue( ^{
[self changePassword:nil];
} );
}];
break;
}
default:
break;
}
}
if (contextInfo == MPAlertChangeLogin) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
NSString *loginName = [(NSSecureTextField *)alert.accessoryView stringValue];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [self.selectedSite entityInContext:context];
entity.loginName = loginName;
[context saveToStore];
}];
break;
}
default:
break;
}
}
if (contextInfo == MPAlertChangeContent) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
NSString *password = [(NSSecureTextField *)alert.accessoryView stringValue];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [self.selectedSite entityInContext:context];
[entity.algorithm savePassword:password toSite:entity usingKey:[MPMacAppDelegate get].key];
[context saveToStore];
}];
break;
}
default:
break;
}
}
if (contextInfo == MPAlertDeleteSite) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Delete" button.
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
[context deleteObject:[self.selectedSite entityInContext:context]];
[context saveToStore];
}];
break;
}
default:
break;
}
}
}
#pragma mark - State #pragma mark - State
- (void)insertObject:(MPSiteModel *)model inSitesAtIndex:(NSUInteger)index { - (void)insertObject:(MPSiteModel *)model inSitesAtIndex:(NSUInteger)index {
@ -373,8 +248,20 @@
[alert addButtonWithTitle:@"Cancel"]; [alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Delete Site?"]; [alert setMessageText:@"Delete Site?"];
[alert setInformativeText:strf( @"Do you want to delete the site named:\n\n%@", self.selectedSite.name )]; [alert setInformativeText:strf( @"Do you want to delete the site named:\n\n%@", self.selectedSite.name )];
[alert beginSheetModalForWindow:self.window modalDelegate:self [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertDeleteSite]; switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Delete" button.
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
[context deleteObject:[self.selectedSite entityInContext:context]];
[context saveToStore];
}];
break;
}
default:
break;
}
}];
} }
- (IBAction)changeLogin:(id)sender { - (IBAction)changeLogin:(id)sender {
@ -389,23 +276,62 @@
[loginField selectText:self]; [loginField selectText:self];
[alert setAccessoryView:loginField]; [alert setAccessoryView:loginField];
[alert layout]; [alert layout];
[alert beginSheetModalForWindow:self.window modalDelegate:self [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertChangeLogin]; switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
NSString *loginName = [(NSSecureTextField *)alert.accessoryView stringValue];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [self.selectedSite entityInContext:context];
entity.loginName = loginName;
[context saveToStore];
}];
break;
}
default:
break;
}
}];
} }
- (IBAction)resetMasterPassword:(id)sender { - (IBAction)resetMasterPassword:(id)sender {
MPUserEntity *activeUser = [MPMacAppDelegate get].activeUserForMainThread;
NSAlert *alert = [NSAlert new]; NSAlert *alert = [NSAlert new];
[alert addButtonWithTitle:@"Reset"]; [alert addButtonWithTitle:@"Reset"];
[alert addButtonWithTitle:@"Cancel"]; [alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Reset My Master Password"]; [alert setMessageText:@"Reset My Master Password"];
[alert setInformativeText:strf( @"This will allow you to change %@'s master password.\n\n" [alert setInformativeText:strf( @"This will allow you to change %@'s master password.\n\n"
@"WARNING: All your site passwords will change. Do this only if you've forgotten your " @"WARNING: All your site passwords will change. Do this only if you've forgotten your "
@"master password and are fully prepared to change all your sites' passwords to the new ones.", activeUser.name )]; @"master password and are fully prepared to change all your sites' passwords to the new ones.",
[alert beginSheetModalForWindow:self.window modalDelegate:self [MPMacAppDelegate get].activeUserForMainThread.name )];
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertChangeMP];
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Reset" button.
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:context];
NSString *activeUserName = activeUser.name;
activeUser.keyID = nil;
[[MPMacAppDelegate get] forgetSavedKeyFor:activeUser];
[context saveToStore];
PearlMainQueue( ^{
NSAlert *alert_ = [NSAlert new];
alert_.messageText = @"Master Password Reset";
alert_.informativeText = strf( @"%@'s master password has been reset.\n\nYou can now set a new one by logging in.",
activeUserName );
[alert_ beginSheetModalForWindow:self.window modalDelegate:nil didEndSelector:NULL contextInfo:nil];
if ([MPMacAppDelegate get].key)
[[MPMacAppDelegate get] signOutAnimated:YES];
} );
}];
}
default:
break;
}
}];
} }
- (IBAction)changePassword:(id)sender { - (IBAction)changePassword:(id)sender {
@ -420,8 +346,22 @@
[alert setInformativeText:strf( @"Enter the new password for: %@", self.selectedSite.name )]; [alert setInformativeText:strf( @"Enter the new password for: %@", self.selectedSite.name )];
[alert setAccessoryView:[[NSSecureTextField alloc] initWithFrame:NSMakeRect( 0, 0, 200, 22 )]]; [alert setAccessoryView:[[NSSecureTextField alloc] initWithFrame:NSMakeRect( 0, 0, 200, 22 )]];
[alert layout]; [alert layout];
[alert beginSheetModalForWindow:self.window modalDelegate:self [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertChangeContent]; switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
NSString *password = [(NSSecureTextField *)alert.accessoryView stringValue];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [self.selectedSite entityInContext:context];
[entity.algorithm savePassword:password toSite:entity usingKey:[MPMacAppDelegate get].key];
[context saveToStore];
}];
break;
}
default:
break;
}
}];
} }
- (IBAction)changeType:(id)sender { - (IBAction)changeType:(id)sender {
@ -450,8 +390,25 @@
[alert setMessageText:@"Change Password Type"]; [alert setMessageText:@"Change Password Type"];
[alert setAccessoryView:self.passwordTypesBox]; [alert setAccessoryView:self.passwordTypesBox];
[alert layout]; [alert layout];
[alert beginSheetModalForWindow:self.window modalDelegate:self [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertChangeType]; switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Save" button.
MPSiteType type = (MPSiteType)[self.passwordTypesMatrix.selectedCell tag];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPSiteEntity *entity = [[MPMacAppDelegate get] changeSite:[self.selectedSite entityInContext:context]
saveInContext:context toType:type];
if ([entity isKindOfClass:[MPStoredSiteEntity class]] && ![(MPStoredSiteEntity *)entity contentObject].length)
PearlMainQueue( ^{
[self changePassword:nil];
} );
}];
break;
}
default:
break;
}
}];
} }
#pragma mark - Private #pragma mark - Private
@ -618,8 +575,21 @@
[alert addButtonWithTitle:@"Cancel"]; [alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Create site?"]; [alert setMessageText:@"Create site?"];
[alert setInformativeText:strf( @"Do you want to create a new site named:\n\n%@", siteName )]; [alert setInformativeText:strf( @"Do you want to create a new site named:\n\n%@", siteName )];
[alert beginSheetModalForWindow:self.window modalDelegate:self [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
didEndSelector:@selector( alertDidEnd:returnCode:contextInfo: ) contextInfo:MPAlertCreateSite]; switch (returnCode) {
case NSAlertFirstButtonReturn: {
// "Create" button.
[[MPMacAppDelegate get] addSiteNamed:[self.siteField stringValue] completion:
^(MPSiteEntity *site, NSManagedObjectContext *context) {
if (site)
PearlMainQueue( ^{ [self updateSites]; } );
}];
break;
}
default:
break;
}
}];
} ); } );
} }