modern Objective-C

This commit is contained in:
Michael Starke
2018-07-12 18:22:22 +02:00
parent a71cd85afc
commit 0c2d08208c
3 changed files with 47 additions and 50 deletions

View File

@@ -97,7 +97,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
[self clearRememberdKeyFiles:nil]; [self clearRememberdKeyFiles:nil];
} }
/* Inform anyone that might be interested that we can now no longer/ or can use keyfiles */ /* Inform anyone that might be interested that we can now no longer/ or can use keyfiles */
[[NSNotificationCenter defaultCenter] postNotificationName:MPDidChangeStoredKeyFilesSettings object:self]; [NSNotificationCenter.defaultCenter postNotificationName:MPDidChangeStoredKeyFilesSettings object:self];
} }
} }
@@ -138,16 +138,16 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
} }
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender { - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
return [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyOpenEmptyDatabaseOnLaunch]; return [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyOpenEmptyDatabaseOnLaunch];
} }
- (void)applicationWillFinishLaunching:(NSNotification *)notification { - (void)applicationWillFinishLaunching:(NSNotification *)notification {
_shouldOpenFile = NO; _shouldOpenFile = NO;
[[NSNotificationCenter defaultCenter] addObserver:self [NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(_applicationDidFinishRestoringWindows:) selector:@selector(_applicationDidFinishRestoringWindows:)
name:NSApplicationDidFinishRestoringWindowsNotification name:NSApplicationDidFinishRestoringWindowsNotification
object:nil]; object:nil];
} }
@@ -166,7 +166,9 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
_shouldOpenFile = YES; _shouldOpenFile = YES;
NSURL *fileURL = [NSURL fileURLWithPath:filename]; NSURL *fileURL = [NSURL fileURLWithPath:filename];
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:fileURL display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error){}]; [NSDocumentController.sharedDocumentController openDocumentWithContentsOfURL:fileURL
display:YES
completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error){}];
return YES; return YES;
} }
@@ -189,7 +191,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
#pragma mark NSMenuDelegate #pragma mark NSMenuDelegate
- (void)menuNeedsUpdate:(NSMenu *)menu { - (void)menuNeedsUpdate:(NSMenu *)menu {
if(menu == self.saveMenuItem.menu) { if(menu == self.saveMenuItem.menu) {
MPDocument *document = [NSDocumentController sharedDocumentController].currentDocument; MPDocument *document = NSDocumentController.sharedDocumentController.currentDocument;
BOOL displayDots = (document.fileURL == nil || !document.compositeKey.hasPasswordOrKeyFile); BOOL displayDots = (document.fileURL == nil || !document.compositeKey.hasPasswordOrKeyFile);
NSString *saveTitle = displayDots ? NSLocalizedString(@"SAVE_WITH_DOTS", "Save file menu item title when save will prompt for a location to save or ask for a password/key") : NSLocalizedString(@"SAVE", "Save file menu item title when save will just save the file"); NSString *saveTitle = displayDots ? NSLocalizedString(@"SAVE_WITH_DOTS", "Save file menu item title when save will prompt for a location to save or ask for a password/key") : NSLocalizedString(@"SAVE", "Save file menu item title when save will just save the file");
self.saveMenuItem.title = saveTitle; self.saveMenuItem.title = saveTitle;
@@ -216,7 +218,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
- (void)showPasswordCreator:(id)sender { - (void)showPasswordCreator:(id)sender {
if(!self.passwordCreatorWindow) { if(!self.passwordCreatorWindow) {
[[NSBundle mainBundle] loadNibNamed:@"PasswordCreatorWindow"owner:self topLevelObjects:nil]; [NSBundle.mainBundle loadNibNamed:@"PasswordCreatorWindow"owner:self topLevelObjects:nil];
} }
if(!self.passwordCreatorController) { if(!self.passwordCreatorController) {
self.passwordCreatorController = [[MPPasswordCreatorViewController alloc] init]; self.passwordCreatorController = [[MPPasswordCreatorViewController alloc] init];
@@ -228,16 +230,16 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
- (void)createNewDatabase:(id)sender { - (void)createNewDatabase:(id)sender {
[self.welcomeWindow orderOut:sender]; [self.welcomeWindow orderOut:sender];
[[NSDocumentController sharedDocumentController] newDocument:sender]; [NSDocumentController.sharedDocumentController newDocument:sender];
} }
- (void)openDatabase:(id)sender { - (void)openDatabase:(id)sender {
[self.welcomeWindow orderOut:sender]; [self.welcomeWindow orderOut:sender];
[[NSDocumentController sharedDocumentController] openDocument:sender]; [NSDocumentController.sharedDocumentController openDocument:sender];
} }
- (void)lockAllDocuments { - (void)lockAllDocuments {
for(NSDocument *document in ((NSDocumentController *)[NSDocumentController sharedDocumentController]).documents) { for(NSDocument *document in NSDocumentController.sharedDocumentController.documents) {
for(id windowController in [document.windowControllers reverseObjectEnumerator]) { for(id windowController in [document.windowControllers reverseObjectEnumerator]) {
if([windowController respondsToSelector:@selector(lock:)]) { if([windowController respondsToSelector:@selector(lock:)]) {
[windowController lock:self]; [windowController lock:self];
@@ -247,12 +249,12 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
} }
- (void)clearRememberdKeyFiles:(id)sender { - (void)clearRememberdKeyFiles:(id)sender {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kMPSettingsKeyRememeberdKeysForDatabases]; [NSUserDefaults.standardUserDefaults removeObjectForKey:kMPSettingsKeyRememeberdKeysForDatabases];
} }
- (void)showHelp:(id)sender { - (void)showHelp:(id)sender {
NSString *urlString = NSBundle.mainBundle.infoDictionary[MPBundleHelpURLKey]; NSString *urlString = NSBundle.mainBundle.infoDictionary[MPBundleHelpURLKey];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]]; [NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:urlString]];
} }
- (void)checkForUpdates:(id)sender { - (void)checkForUpdates:(id)sender {
@@ -270,17 +272,16 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
#pragma mark - #pragma mark -
#pragma mark Private Helper #pragma mark Private Helper
- (void)_applicationDidFinishRestoringWindows:(NSNotification *)notification { - (void)_applicationDidFinishRestoringWindows:(NSNotification *)notification {
NSDocumentController *documentController = [NSDocumentController sharedDocumentController]; NSArray *documents = NSDocumentController.sharedDocumentController.documents;
NSArray *documents = [documentController documents]; BOOL restoredWindows = documents.count > 0;
BOOL restoredWindows = [documents count] > 0;
for(NSDocument *document in documents) { for(NSDocument *document in documents) {
for(NSWindowController *windowController in [document windowControllers]) { for(NSWindowController *windowController in document.windowControllers) {
[windowController.window.contentView layout]; [windowController.window.contentView layout];
} }
} }
BOOL reopen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; BOOL reopen = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch];
BOOL showWelcomeScreen = !restoredWindows && !_shouldOpenFile; BOOL showWelcomeScreen = !restoredWindows && !_shouldOpenFile;
if(reopen && !restoredWindows && !_shouldOpenFile) { if(reopen && !restoredWindows && !_shouldOpenFile) {
showWelcomeScreen = ![self _reopenLastDocument]; showWelcomeScreen = ![self _reopenLastDocument];
@@ -298,45 +299,42 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
- (void)_loadWelcomeWindow { - (void)_loadWelcomeWindow {
if(!_welcomeWindow) { if(!_welcomeWindow) {
NSArray *topLevelObject; NSArray *topLevelObject;
[[NSBundle mainBundle] loadNibNamed:@"WelcomeWindow" owner:self topLevelObjects:&topLevelObject]; [NSBundle.mainBundle loadNibNamed:@"WelcomeWindow" owner:self topLevelObjects:&topLevelObject];
} }
} }
- (BOOL)_reopenLastDocument { - (BOOL)_reopenLastDocument {
NSDocumentController *documentController = [NSDocumentController sharedDocumentController]; if(NSDocumentController.sharedDocumentController.documents.count > 0) {
NSArray *documents = [documentController documents];
if([documents count] > 0) {
return YES; // The document is already open return YES; // The document is already open
} }
NSArray *recentDocuments = [[NSDocumentController sharedDocumentController] recentDocumentURLs]; NSArray *recentDocuments = NSDocumentController.sharedDocumentController.recentDocumentURLs;
NSURL *documentUrl = nil; NSURL *documentUrl = nil;
if([recentDocuments count] > 0) { if(recentDocuments.count > 0) {
documentUrl = recentDocuments[0]; documentUrl = recentDocuments.firstObject;
} }
else { else {
NSString *lastPath = [[NSUserDefaults standardUserDefaults] stringForKey:kMPSettingsKeyLastDatabasePath]; NSString *lastPath = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyLastDatabasePath];
documentUrl =[NSURL URLWithString:lastPath]; documentUrl =[NSURL URLWithString:lastPath];
} }
BOOL isFileURL = [documentUrl isFileURL]; BOOL isFileURL = documentUrl.fileURL;
if(isFileURL) { if(isFileURL) {
[documentController openDocumentWithContentsOfURL:documentUrl [NSDocumentController.sharedDocumentController openDocumentWithContentsOfURL:documentUrl
display:YES display:YES
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) { completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) {
if(error != nil){ if(error != nil){
NSAlert *alert = [[NSAlert alloc] init];
NSAlert *alert = [[NSAlert alloc] init]; alert.messageText = NSLocalizedString(@"FILE_OPEN_ERROR", "Error while reopening last known documents");
[alert setMessageText: NSLocalizedString(@"FILE_OPEN_ERROR", "Error while reopening last known documents")]; alert.informativeText = error.localizedDescription;
[alert setInformativeText: [error localizedDescription]]; alert.alertStyle = NSCriticalAlertStyle;
[alert setAlertStyle:NSCriticalAlertStyle ]; [alert runModal];
[alert runModal]; }
}
if(document == nil){
if(document == nil){ [self _showWelcomeWindow];
[self _showWelcomeWindow]; }
}
}];
}];
} }
return isFileURL; return isFileURL;
} }

View File

@@ -27,9 +27,8 @@
@interface MPTemporaryFileStorageCenter : NSObject @interface MPTemporaryFileStorageCenter : NSObject
+ (instancetype)defaultCenter; @property (class, strong, readonly) MPTemporaryFileStorageCenter *defaultCenter;
@property (readonly) BOOL hasPendingStorages;
- (BOOL)hasPendingStorages;
- (MPTemporaryFileStorage *)storageForBinary:(KPKBinary *)binary; - (MPTemporaryFileStorage *)storageForBinary:(KPKBinary *)binary;
- (void)registerStorage:(MPTemporaryFileStorage *)storage; - (void)registerStorage:(MPTemporaryFileStorage *)storage;

View File

@@ -49,7 +49,7 @@
} }
- (BOOL)hasPendingStorages { - (BOOL)hasPendingStorages {
return [_storages count] > 0; return _storages.count > 0;
} }
- (MPTemporaryFileStorage *)storageForBinary:(KPKBinary *)binary { - (MPTemporaryFileStorage *)storageForBinary:(KPKBinary *)binary {