Turn off the App Sandbox for now.
[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 #if USE_APP_SANDBOX
44 kPrefFileAccessBookmarks : [NSMutableDictionary dictionary],
45 #endif
46 kPrefBreakOnFirstLine : @YES,
47 kPrefDebuggerAttached : @YES,
48 kPrefSelectedDebuggerSegment : @1,
49 };
50 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
51 }
52 }
53
54 + (AppDelegate*)instance
55 {
56 return (AppDelegate*)[NSApp delegate];
57 }
58
59 - (PreferencesController*)prefsController
60 {
61 if (!_prefsController)
62 _prefsController = [[PreferencesController alloc] init];
63 return _prefsController;
64 }
65
66 - (void)applicationDidFinishLaunching:(NSNotification*)notification
67 {
68 // Record whether this user ever used the beta VersionCast feed. In the
69 // future, we will use this bit to query for unstable releases after the user
70 // has upgraded to a stable version.
71 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
72
73 BOOL usesUnstable = [defaults boolForKey:kPrefUnstableVersionCast];
74 NSURL* feedURL = [[SUUpdater sharedUpdater] feedURL];
75 usesUnstable = usesUnstable ||
76 [[feedURL absoluteString] rangeOfString:@"?unstable"].location != NSNotFound;
77 [defaults setBool:usesUnstable forKey:kPrefUnstableVersionCast];
78
79 #if USE_APP_SANDBOX
80 [FileAccessController maybeShowFileAccessDialog];
81
82 [self _activateSecureFileAccess];
83 #endif // USE_APP_SANDBOX
84 }
85
86 - (void)applicationWillTerminate:(NSNotification*)notification
87 {
88 [[NSUserDefaults standardUserDefaults] setBool:self.debugger.connection.autoAttach
89 forKey:kPrefDebuggerAttached];
90 }
91
92 /**
93 * Shows the debugger window
94 */
95 - (IBAction)showDebuggerWindow:(id)sender
96 {
97 [[debugger window] makeKeyAndOrderFront:self];
98 [debugger.segmentControl setSelectedSegment:1];
99 }
100
101 /**
102 * Shows the breakpoints window
103 */
104 - (IBAction)showBreakpointWindow:(id)sender
105 {
106 [[debugger window] makeKeyAndOrderFront:sender];
107 [debugger.segmentControl setSelectedSegment:2];
108 }
109
110 /**
111 * Shows the preferences window. Lazily loads the PreferencesController.
112 */
113 - (IBAction)showPreferences:(id)sender
114 {
115 [self.prefsController showPreferencesWindow];
116 }
117
118 /**
119 * Opens the URL to the help page
120 */
121 - (IBAction)openHelpPage:(id)sender
122 {
123 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.bluestatic.org/software/macgdbp/help/"]];
124 }
125
126 #if USE_APP_SANDBOX
127 /**
128 * Activates any secure file access bookmarks stored in preferences.
129 */
130 - (void)_activateSecureFileAccess
131 {
132 NSDictionary* prefs = [NSUserDefaults.standardUserDefaults objectForKey:kPrefFileAccessBookmarks];
133 NSMutableDictionary<NSString*, NSData*>* bookmarks = [NSMutableDictionary dictionaryWithDictionary:prefs];
134 for (NSString* path in bookmarks) {
135 NSURL* url = [NSURL URLWithString:path];
136
137 BOOL isStale;
138 NSError* error;
139 url = [NSURL URLByResolvingBookmarkData:bookmarks[path]
140 options:NSURLBookmarkResolutionWithSecurityScope
141 relativeToURL:nil
142 bookmarkDataIsStale:&isStale
143 error:&error];
144 if (error) {
145 NSLog(@"Failed to resolve secure bookmark for path %@: %@", path, error);
146 continue;
147 }
148 if (isStale) {
149 NSData* newBookmark = [PreferencesController secureBookmarkDataForURL:url];
150 bookmarks[url.absoluteString] = newBookmark;
151 [bookmarks removeObjectForKey:path];
152 }
153
154 if (![url startAccessingSecurityScopedResource]) {
155 NSLog(@"Failed to start accessing resource %@", path);
156 continue;
157 }
158 }
159
160 [NSUserDefaults.standardUserDefaults setObject:bookmarks forKey:kPrefFileAccessBookmarks];
161 }
162 #endif // USE_APP_SANDBOX
163
164 @end