Add startup dialog for when no file access has been granted.
[macgdbp.git] / Source / AppDelegate.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "AppDelegate.h"
18
19 #import <Sparkle/Sparkle.h>
20
21 #import "FileAccessController.h"
22 #import "PreferenceNames.h"
23
24 @implementation AppDelegate {
25 PreferencesController* _prefsController;
26 }
27
28 @synthesize debugger;
29 @synthesize breakpoint;
30 @synthesize loggingController = loggingController_;
31
32 /**
33 * Initialize method that is called before all other messages. This will set the default
34 * preference values.
35 */
36 + (void)load
37 {
38 @autoreleasepool {
39 NSDictionary* defaults = @{
40 kPrefPort : @9000,
41 kPrefInspectorWindowVisible : @YES,
42 kPrefPathReplacements : [NSMutableArray array],
43 kPrefFileAccessBookmarks : [NSMutableDictionary dictionary],
44 kPrefBreakOnFirstLine : @YES,
45 kPrefDebuggerAttached : @YES,
46 kPrefSelectedDebuggerSegment : @1,
47 };
48 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
49 }
50 }
51
52 + (AppDelegate*)instance
53 {
54 return (AppDelegate*)[NSApp delegate];
55 }
56
57 - (PreferencesController*)prefsController
58 {
59 if (!_prefsController)
60 _prefsController = [[PreferencesController alloc] init];
61 return _prefsController;
62 }
63
64 - (void)applicationDidFinishLaunching:(NSNotification*)notification
65 {
66 // Record whether this user ever used the beta VersionCast feed. In the
67 // future, we will use this bit to query for unstable releases after the user
68 // has upgraded to a stable version.
69 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
70
71 BOOL usesUnstable = [defaults boolForKey:kPrefUnstableVersionCast];
72 NSURL* feedURL = [[SUUpdater sharedUpdater] feedURL];
73 usesUnstable = usesUnstable ||
74 [[feedURL absoluteString] rangeOfString:@"?unstable"].location != NSNotFound;
75 [defaults setBool:usesUnstable forKey:kPrefUnstableVersionCast];
76
77 [FileAccessController maybeShowFileAccessDialog];
78
79 [self _activateSecureFileAccess];
80 }
81
82 - (void)applicationWillTerminate:(NSNotification*)notification
83 {
84 [[NSUserDefaults standardUserDefaults] setBool:self.debugger.connection.autoAttach
85 forKey:kPrefDebuggerAttached];
86 }
87
88 /**
89 * Shows the debugger window
90 */
91 - (IBAction)showDebuggerWindow:(id)sender
92 {
93 [[debugger window] makeKeyAndOrderFront:self];
94 [debugger.segmentControl setSelectedSegment:1];
95 }
96
97 /**
98 * Shows the breakpoints window
99 */
100 - (IBAction)showBreakpointWindow:(id)sender
101 {
102 [[debugger window] makeKeyAndOrderFront:sender];
103 [debugger.segmentControl setSelectedSegment:2];
104 }
105
106 /**
107 * Shows the preferences window. Lazily loads the PreferencesController.
108 */
109 - (IBAction)showPreferences:(id)sender
110 {
111 [self.prefsController showPreferencesWindow];
112 }
113
114 /**
115 * Opens the URL to the help page
116 */
117 - (IBAction)openHelpPage:(id)sender
118 {
119 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.bluestatic.org/software/macgdbp/help/"]];
120 }
121
122 /**
123 * Activates any secure file access bookmarks stored in preferences.
124 */
125 - (void)_activateSecureFileAccess
126 {
127 NSDictionary* prefs = [NSUserDefaults.standardUserDefaults objectForKey:kPrefFileAccessBookmarks];
128 NSMutableDictionary<NSString*, NSData*>* bookmarks = [NSMutableDictionary dictionaryWithDictionary:prefs];
129 for (NSString* path in bookmarks) {
130 NSURL* url = [NSURL URLWithString:path];
131
132 BOOL isStale;
133 NSError* error;
134 url = [NSURL URLByResolvingBookmarkData:bookmarks[path]
135 options:NSURLBookmarkResolutionWithSecurityScope
136 relativeToURL:nil
137 bookmarkDataIsStale:&isStale
138 error:&error];
139 if (error) {
140 NSLog(@"Failed to resolve secure bookmark for path %@: %@", path, error);
141 continue;
142 }
143 if (isStale) {
144 NSData* newBookmark = [PreferencesController secureBookmarkDataForURL:url];
145 bookmarks[url.absoluteString] = newBookmark;
146 [bookmarks removeObjectForKey:path];
147 }
148
149 if (![url startAccessingSecurityScopedResource]) {
150 NSLog(@"Failed to start accessing resource %@", path);
151 continue;
152 }
153 }
154
155 [NSUserDefaults.standardUserDefaults setObject:bookmarks forKey:kPrefFileAccessBookmarks];
156 }
157
158 @end