Present buttons in notifications, reopen last document when none was open and user clicks on notification.

This commit is contained in:
Michael Starke
2018-09-27 13:49:24 +02:00
parent 1bed34d23f
commit 64d03ce56c
8 changed files with 67 additions and 62 deletions

View File

@@ -51,4 +51,6 @@ APPKIT_EXTERN NSString *const MPDidChangeStoredKeyFilesSettings;
- (void)lockAllDocuments;
- (void)showWelcomeWindow;
@end

View File

@@ -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

View File

@@ -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<NSString *,id> * _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

View File

@@ -27,4 +27,6 @@
- (IBAction)toggleAllowAllFiles:(id)sender;
- (IBAction)toggleShowHiddenFiles:(id)sender;
- (BOOL)reopenLastDocument;
@end

View File

@@ -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

View File

@@ -22,8 +22,9 @@
#import <Foundation/Foundation.h>
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 <NSUserNotificationCenterDelegate>

View File

@@ -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];
}

View File

@@ -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! ";