From 64d03ce56c9d55cd0eda0cde0227b8e1a9490725 Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Thu, 27 Sep 2018 13:49:24 +0200 Subject: [PATCH] Present buttons in notifications, reopen last document when none was open and user clicks on notification. --- MacPass/MPAppDelegate.h | 2 + MacPass/MPAppDelegate.m | 56 ++++------------------ MacPass/MPAutotypeDaemon.m | 8 ++-- MacPass/MPDocumentController.h | 2 + MacPass/MPDocumentController.m | 36 ++++++++++++++ MacPass/MPUserNotificationCenterDelegate.h | 5 +- MacPass/MPUserNotificationCenterDelegate.m | 18 +++---- MacPass/de.lproj/Localizable.strings | 2 +- 8 files changed, 67 insertions(+), 62 deletions(-) diff --git a/MacPass/MPAppDelegate.h b/MacPass/MPAppDelegate.h index 47b68197..b5f17a00 100644 --- a/MacPass/MPAppDelegate.h +++ b/MacPass/MPAppDelegate.h @@ -51,4 +51,6 @@ APPKIT_EXTERN NSString *const MPDidChangeStoredKeyFilesSettings; - (void)lockAllDocuments; +- (void)showWelcomeWindow; + @end diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index 9b9d329f..9662ea8c 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -125,13 +125,13 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag { if(!flag) { - BOOL reopen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; + BOOL reopen = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; BOOL showWelcomeScreen = YES; if(reopen) { - showWelcomeScreen = ![self _reopenLastDocument]; + showWelcomeScreen = ![((MPDocumentController *)NSDocumentController.sharedDocumentController) reopenLastDocument]; } if(showWelcomeScreen) { - [self _showWelcomeWindow]; + [self showWelcomeWindow]; } } return YES; @@ -248,6 +248,11 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi } } +- (void)showWelcomeWindow { + [self _loadWelcomeWindow]; + [self.welcomeWindow makeKeyAndOrderFront:nil]; +} + - (void)clearRememberdKeyFiles:(id)sender { [NSUserDefaults.standardUserDefaults removeObjectForKey:kMPSettingsKeyRememeberdKeysForDatabases]; } @@ -284,18 +289,13 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi BOOL reopen = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; BOOL showWelcomeScreen = !restoredWindows && !_shouldOpenFile; if(reopen && !restoredWindows && !_shouldOpenFile) { - showWelcomeScreen = ![self _reopenLastDocument]; + showWelcomeScreen = ![((MPDocumentController *)NSDocumentController.sharedDocumentController) reopenLastDocument]; } if(showWelcomeScreen) { - [self _showWelcomeWindow]; + [self showWelcomeWindow]; } } -- (void)_showWelcomeWindow { - [self _loadWelcomeWindow]; - [self.welcomeWindow makeKeyAndOrderFront:nil]; -} - - (void)_loadWelcomeWindow { if(!_welcomeWindow) { NSArray *topLevelObject; @@ -303,40 +303,4 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi } } -- (BOOL)_reopenLastDocument { - if(NSDocumentController.sharedDocumentController.documents.count > 0) { - return YES; // The document is already open - } - NSArray *recentDocuments = NSDocumentController.sharedDocumentController.recentDocumentURLs; - NSURL *documentUrl = nil; - if(recentDocuments.count > 0) { - documentUrl = recentDocuments.firstObject; - } - else { - NSString *lastPath = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyLastDatabasePath]; - documentUrl =[NSURL URLWithString:lastPath]; - } - BOOL isFileURL = documentUrl.fileURL; - if(isFileURL) { - [NSDocumentController.sharedDocumentController openDocumentWithContentsOfURL:documentUrl - display:YES - completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) { - - if(error != nil){ - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = NSLocalizedString(@"FILE_OPEN_ERROR", "Error while reopening last known documents"); - alert.informativeText = error.localizedDescription; - alert.alertStyle = NSCriticalAlertStyle; - [alert runModal]; - } - - if(document == nil){ - [self _showWelcomeWindow]; - } - - }]; - } - return isFileURL; -} - @end diff --git a/MacPass/MPAutotypeDaemon.m b/MacPass/MPAutotypeDaemon.m index 87b63548..43313337 100644 --- a/MacPass/MPAutotypeDaemon.m +++ b/MacPass/MPAutotypeDaemon.m @@ -193,10 +193,12 @@ static MPAutotypeDaemon *_sharedInstance; notification.title = NSApp.applicationName; notification.informativeText = NSLocalizedString(@"AUTOTYPE_OVERLAY_NO_DOCUMENTS", "Notification: Autotype failed, no documents are open"); notification.actionButtonTitle = NSLocalizedString(@"OPEN_DOCUMENT", "Action button in Notification to open a document"); - //notification.showsButtons = YES; + notification.userInfo = @{ MPUserNotificationTypeKey: MPUserNotificationTypeAutotypeOpenDocumentRequest }; + notification.showsButtons = YES; [NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification]; return; } + NSPredicate *filterPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) { MPDocument *document = evaluatedObject; return !document.encrypted;}]; @@ -219,7 +221,7 @@ static MPAutotypeDaemon *_sharedInstance; if(!entryOrNil) { NSUserNotification *notification = [[NSUserNotification alloc] init]; notification.title = NSApp.applicationName; - notification.userInfo = @{ kMPUserNotificationInfoKeyNotificationType: kMPUserNotificationTypeAutotype }; + notification.userInfo = @{ MPUserNotificationTypeKey: MPUserNotificationTypeAutotypeFeedback }; if(context) { notification.informativeText = [NSString stringWithFormat:NSLocalizedString(@"AUTOTYPE_OVERLAY_SINGLE_MATCH_FOR_%@", "Notification: Autotype found a single match for %@ (string placeholder)."), self.targetWindowTitle]; } @@ -320,8 +322,6 @@ static MPAutotypeDaemon *_sharedInstance; } - (void)_presentSelectionWindow:(NSArray *)candidates { - - if(!self.matchSelectionWindow) { self.matchSelectionWindow = [[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) styleMask:NSWindowStyleMaskNonactivatingPanel|NSWindowStyleMaskTitled diff --git a/MacPass/MPDocumentController.h b/MacPass/MPDocumentController.h index baed2568..58c391d7 100644 --- a/MacPass/MPDocumentController.h +++ b/MacPass/MPDocumentController.h @@ -27,4 +27,6 @@ - (IBAction)toggleAllowAllFiles:(id)sender; - (IBAction)toggleShowHiddenFiles:(id)sender; +- (BOOL)reopenLastDocument; + @end diff --git a/MacPass/MPDocumentController.m b/MacPass/MPDocumentController.m index a1b38618..7144c203 100644 --- a/MacPass/MPDocumentController.m +++ b/MacPass/MPDocumentController.m @@ -23,6 +23,8 @@ #import "MPDocumentController.h" #import "MPConstants.h" +#import "MPSettingsHelper.h" +#import "MPAppDelegate.h" #import "HNHUi/HNHUi.h" @@ -76,4 +78,38 @@ return [super typeForContentsOfURL:url error:outError]; } +- (BOOL)reopenLastDocument { + if(self.documents.count > 0) { + return YES; // The document is already open + } + NSURL *documentUrl = nil; + if(self.recentDocumentURLs.count > 0) { + documentUrl = self.recentDocumentURLs.firstObject; + } + else { + NSString *lastPath = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyLastDatabasePath]; + documentUrl =[NSURL URLWithString:lastPath]; + } + BOOL isFileURL = documentUrl.fileURL; + if(isFileURL) { + [self openDocumentWithContentsOfURL:documentUrl + display:YES + completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) { + + if(error != nil){ + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = NSLocalizedString(@"FILE_OPEN_ERROR", "Error while reopening last known documents"); + alert.informativeText = error.localizedDescription; + alert.alertStyle = NSCriticalAlertStyle; + [alert runModal]; + } + + if(document == nil){ + [(MPAppDelegate *)NSApp.delegate showWelcomeWindow]; + } + }]; + } + return isFileURL; +} + @end diff --git a/MacPass/MPUserNotificationCenterDelegate.h b/MacPass/MPUserNotificationCenterDelegate.h index 45aa60a1..b3506d51 100644 --- a/MacPass/MPUserNotificationCenterDelegate.h +++ b/MacPass/MPUserNotificationCenterDelegate.h @@ -22,8 +22,9 @@ #import -FOUNDATION_EXTERN NSString *const kMPUserNotificationInfoKeyNotificationType; -FOUNDATION_EXTERN NSString *const kMPUserNotificationTypeAutotype; +FOUNDATION_EXTERN NSString *const MPUserNotificationTypeKey; +FOUNDATION_EXTERN NSString *const MPUserNotificationTypeAutotypeFeedback; +FOUNDATION_EXTERN NSString *const MPUserNotificationTypeAutotypeOpenDocumentRequest; @interface MPUserNotificationCenterDelegate : NSObject diff --git a/MacPass/MPUserNotificationCenterDelegate.m b/MacPass/MPUserNotificationCenterDelegate.m index 492b205c..bb7ba24c 100644 --- a/MacPass/MPUserNotificationCenterDelegate.m +++ b/MacPass/MPUserNotificationCenterDelegate.m @@ -21,9 +21,11 @@ // #import "MPUserNotificationCenterDelegate.h" +#import "MPDocumentController.h" -NSString *const kMPUserNotificationInfoKeyNotificationType = @"kMPUserNotificationInfoKeyNotificationType"; -NSString *const kMPUserNotificationTypeAutotype = @"kMPUserNotificationTypeAutotype"; +NSString *const MPUserNotificationTypeKey = @"MPUserNotificationTypeKey"; +NSString *const MPUserNotificationTypeAutotypeFeedback = @"MPUserNotificationTypeAutotypeFeedback"; +NSString *const MPUserNotificationTypeAutotypeOpenDocumentRequest = @"MPUserNotificationTypeAutotypeOpenDocumentRequest"; @implementation MPUserNotificationCenterDelegate @@ -36,16 +38,14 @@ NSString *const kMPUserNotificationTypeAutotype = @"kMPUserNotificationTypeAutot } - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification { - NSLog(@"%@", notification); + NSDictionary *userInfo = notification.userInfo; + if([userInfo[MPUserNotificationTypeKey] isEqualToString:MPUserNotificationTypeAutotypeOpenDocumentRequest]) { + [((MPDocumentController*)NSDocumentController.sharedDocumentController) reopenLastDocument]; + } } - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification { - NSDictionary *userInfo = notification.userInfo; - if(!userInfo) { - return NO; - } - NSString *value = userInfo[kMPUserNotificationInfoKeyNotificationType]; - return [value isEqualToString:kMPUserNotificationTypeAutotype]; + return [notification.userInfo[MPUserNotificationTypeKey] isEqualToString:MPUserNotificationTypeAutotypeFeedback]; } diff --git a/MacPass/de.lproj/Localizable.strings b/MacPass/de.lproj/Localizable.strings index 42beaec7..f22c9b6e 100644 --- a/MacPass/de.lproj/Localizable.strings +++ b/MacPass/de.lproj/Localizable.strings @@ -44,7 +44,7 @@ "ALERT_MERGE_CONTINUE" = "Daten synchronisieren."; /* Informative text displayed when merging KDB files */ -"ALERT_MERGE_KDB_FILE_INFO_TEXT" = "KDB Datenbanken enthalten nicht alle notwendigen Informationen für eine eindeutige Synchronisatione. Die Synchronisation der Daten von Einträgen erfolgt korrekt, jedoch können Gruppen nur anhand des Namens zugeordnet werden, was möglicherweise zu unerwarteten Ergebnissen führen kann. Soll mit der Synchronisation fortgefahren werden?"; +"ALERT_MERGE_KDB_FILE_INFO_TEXT" = "KDB Datenbanken enthalten nicht alle notwendigen Informationen für eine eindeutige Synchronisation. Einzelne Einträgen werden korrekt synchronisiert. Gruppen werden jedoch nur durch den Namens zugeordnet werden, was zu unerwarteten Ergebnissen führen kann. Etwa können lokal bewegte Einträge wieder in alte Gruppen geschoben werden, unbenannte Gruppen können leer werden und die Einträge unter einer Gruppe mit dem vorherigen Namen wieder auftauchen. Weiterhin können gelöschte Einträge wieder auftauchen, da keine Informationen über diese gespeichert wird. Soll mit der Synchronisation fortgefahren werden?"; /* Alert message warning user about KDB file merge */ "ALERT_MERGE_KDB_FILE_MESSAGE" = "Es werden KDB Datenbanken synchronisiert! ";