From 87590130c813d3f885b9e4e38a478b9b9ca0ecaf Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 14 Dec 2019 15:24:44 -0500 Subject: [PATCH] Show the FileAccessController when reading a source file fails. Also only show the startup prompt once. --- Source/BSSourceView.h | 3 +++ Source/BSSourceView.mm | 3 +++ Source/DebuggerController.m | 8 ++++++++ Source/FileAccessController.h | 1 + Source/FileAccessController.m | 19 ++++++++++++++----- Source/PreferenceNames.h | 3 +++ Source/PreferenceNames.m | 2 ++ 7 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Source/BSSourceView.h b/Source/BSSourceView.h index a9359da..2bfb18d 100644 --- a/Source/BSSourceView.h +++ b/Source/BSSourceView.h @@ -64,4 +64,7 @@ // Notifies the delegate that the gutter was clicked at a certain line. - (void)gutterClickedAtLine:(NSUInteger)line forFile:(NSString*)file; +// An error occurrerd while attempting to read or highlight the file. +- (void)error:(NSError*)error whileHighlightingFile:(NSString*)file; + @end diff --git a/Source/BSSourceView.mm b/Source/BSSourceView.mm index e7bf17b..5914803 100644 --- a/Source/BSSourceView.mm +++ b/Source/BSSourceView.mm @@ -259,6 +259,9 @@ error:&error]; if (error) { NSLog(@"Error reading file at %@: %@", filePath, error); + if ([delegate_ respondsToSelector:@selector(error:whileHighlightingFile:)]) { + [delegate_ error:error whileHighlightingFile:filePath]; + } return; } [textView_ setString:contents]; diff --git a/Source/DebuggerController.m b/Source/DebuggerController.m index f7d85b4..0a2cedb 100644 --- a/Source/DebuggerController.m +++ b/Source/DebuggerController.m @@ -23,6 +23,7 @@ #import "DebuggerBackEnd.h" #import "DebuggerModel.h" #import "EvalController.h" +#import "FileAccessController.h" #import "PreferenceNames.h" #import "NSXMLElementAdditions.h" #import "StackFrame.h" @@ -407,4 +408,11 @@ [_sourceViewer setMarkers:[manager breakpointsForFile:file]]; } +- (void)error:(NSError*)error whileHighlightingFile:(NSString*)file +{ + if (error.code == NSFileReadNoPermissionError) { + [FileAccessController showFileAccessDialog]; + } +} + @end diff --git a/Source/FileAccessController.h b/Source/FileAccessController.h index 67cceab..744795a 100644 --- a/Source/FileAccessController.h +++ b/Source/FileAccessController.h @@ -18,6 +18,7 @@ @interface FileAccessController : NSWindowController + (void)maybeShowFileAccessDialog; ++ (void)showFileAccessDialog; - (IBAction)openFileAccess:(id)sender; @end diff --git a/Source/FileAccessController.m b/Source/FileAccessController.m index 871cfdd..9ec95c6 100644 --- a/Source/FileAccessController.m +++ b/Source/FileAccessController.m @@ -27,14 +27,23 @@ + (void)maybeShowFileAccessDialog { - NSDictionary* fileAccesses = [[NSUserDefaults standardUserDefaults] objectForKey:kPrefFileAccessBookmarks]; - if ([fileAccesses count] == 0) { - FileAccessController* controller = [[FileAccessController alloc] init]; - [controller.window center]; - [controller showWindow:self]; + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + NSDictionary* fileAccesses = [defaults objectForKey:kPrefFileAccessBookmarks]; + // TODO: Re-prompt after some amount of time. + if ([fileAccesses count] == 0 && + ![defaults objectForKey:kPrefFileAccessStartupShowDate]) { + [defaults setObject:[NSDate date] forKey:kPrefFileAccessStartupShowDate]; + [self showFileAccessDialog]; } } ++ (void)showFileAccessDialog +{ + FileAccessController* controller = [[FileAccessController alloc] init]; + [controller.window center]; + [controller showWindow:self]; +} + - (instancetype)init { if ((self = [self initWithWindowNibName:@"FileAccess"])) { diff --git a/Source/PreferenceNames.h b/Source/PreferenceNames.h index fa6b777..c236181 100644 --- a/Source/PreferenceNames.h +++ b/Source/PreferenceNames.h @@ -28,6 +28,9 @@ extern NSString* const kPrefPathReplacements; // NSMutableDictionary of NSString paths to NSData file access bookmarks. extern NSString* const kPrefFileAccessBookmarks; +// NSDate of last showing the FileAccessController on startup. +extern NSString* const kPrefFileAccessStartupShowDate; + // NSNumber bool for whether to stop the debugger on the first line of the // program. extern NSString* const kPrefBreakOnFirstLine; diff --git a/Source/PreferenceNames.m b/Source/PreferenceNames.m index 49da56b..e2958b5 100644 --- a/Source/PreferenceNames.m +++ b/Source/PreferenceNames.m @@ -24,6 +24,8 @@ NSString* const kPrefPathReplacements = @"PathReplacements"; NSString* const kPrefFileAccessBookmarks = @"FileAccessBookmarks"; +NSString* const kPrefFileAccessStartupShowDate = @"FileAccessStartupShowDate"; + NSString* const kPrefBreakOnFirstLine = @"BreakOnFirstLine"; NSString* const kPrefDebuggerAttached = @"DebuggerAttached"; -- 2.22.5