Modernize the source view classes.
[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 static NSString* const kAppcastUnstable = @"appcast-unstable.xml";
25
26 @implementation AppDelegate {
27 PreferencesController* _prefsController;
28 }
29
30 @synthesize debugger;
31 @synthesize breakpoint;
32 @synthesize loggingController = loggingController_;
33
34 /**
35 * Initialize method that is called before all other messages. This will set the default
36 * preference values.
37 */
38 + (void)load
39 {
40 @autoreleasepool {
41 NSDictionary* defaults = @{
42 kPrefPort : @9000,
43 kPrefInspectorWindowVisible : @YES,
44 kPrefPathReplacements : [NSMutableArray array],
45 #if USE_APP_SANDBOX
46 kPrefFileAccessBookmarks : [NSMutableDictionary dictionary],
47 #endif
48 kPrefBreakOnFirstLine : @YES,
49 kPrefDebuggerAttached : @YES,
50 kPrefSelectedDebuggerSegment : @1,
51 };
52 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
53 }
54 }
55
56 + (AppDelegate*)instance
57 {
58 return (AppDelegate*)[NSApp delegate];
59 }
60
61 - (PreferencesController*)prefsController
62 {
63 if (!_prefsController)
64 _prefsController = [[PreferencesController alloc] init];
65 return _prefsController;
66 }
67
68 - (void)applicationDidFinishLaunching:(NSNotification*)notification
69 {
70 [[SUUpdater sharedUpdater] setDelegate:self];
71
72 #if USE_APP_SANDBOX
73 [FileAccessController maybeShowFileAccessDialog];
74
75 [self _activateSecureFileAccess];
76 #endif // USE_APP_SANDBOX
77 }
78
79 - (void)applicationWillTerminate:(NSNotification*)notification
80 {
81 [[NSUserDefaults standardUserDefaults] setBool:self.debugger.connection.autoAttach
82 forKey:kPrefDebuggerAttached];
83 }
84
85 /**
86 * Shows the debugger window
87 */
88 - (IBAction)showDebuggerWindow:(id)sender
89 {
90 [[debugger window] makeKeyAndOrderFront:self];
91 [debugger.segmentControl setSelectedSegment:1];
92 }
93
94 /**
95 * Shows the breakpoints window
96 */
97 - (IBAction)showBreakpointWindow:(id)sender
98 {
99 [[debugger window] makeKeyAndOrderFront:sender];
100 [debugger.segmentControl setSelectedSegment:2];
101 }
102
103 /**
104 * Shows the preferences window. Lazily loads the PreferencesController.
105 */
106 - (IBAction)showPreferences:(id)sender
107 {
108 [self.prefsController showPreferencesWindow];
109 }
110
111 /**
112 * Opens the URL to the help page
113 */
114 - (IBAction)openHelpPage:(id)sender
115 {
116 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.bluestatic.org/software/macgdbp/help/"]];
117 }
118
119 #if USE_APP_SANDBOX
120 /**
121 * Activates any secure file access bookmarks stored in preferences.
122 */
123 - (void)_activateSecureFileAccess
124 {
125 NSDictionary* prefs = [NSUserDefaults.standardUserDefaults objectForKey:kPrefFileAccessBookmarks];
126 NSMutableDictionary<NSString*, NSData*>* bookmarks = [NSMutableDictionary dictionaryWithDictionary:prefs];
127 for (NSString* path in bookmarks) {
128 NSURL* url = [NSURL URLWithString:path];
129
130 BOOL isStale;
131 NSError* error;
132 url = [NSURL URLByResolvingBookmarkData:bookmarks[path]
133 options:NSURLBookmarkResolutionWithSecurityScope
134 relativeToURL:nil
135 bookmarkDataIsStale:&isStale
136 error:&error];
137 if (error) {
138 NSLog(@"Failed to resolve secure bookmark for path %@: %@", path, error);
139 continue;
140 }
141 if (isStale) {
142 NSData* newBookmark = [PreferencesController secureBookmarkDataForURL:url];
143 bookmarks[url.absoluteString] = newBookmark;
144 [bookmarks removeObjectForKey:path];
145 }
146
147 if (![url startAccessingSecurityScopedResource]) {
148 NSLog(@"Failed to start accessing resource %@", path);
149 continue;
150 }
151 }
152
153 [NSUserDefaults.standardUserDefaults setObject:bookmarks forKey:kPrefFileAccessBookmarks];
154 }
155 #endif // USE_APP_SANDBOX
156
157 ////////////////////////////////////////////////////////////////////////////////
158 #pragma mark SUUpdater Delegate
159
160 - (nullable NSString*)feedURLStringForUpdater:(SUUpdater*)updater
161 {
162 // Record whether this user ever used the beta appcast feed.
163 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
164 NSURL* feedURL = [NSURL URLWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"SUFeedURL"]];
165
166 BOOL usesUnstable = [defaults boolForKey:kPrefUnstableVersionCast] ||
167 [[feedURL absoluteString] hasSuffix:kAppcastUnstable];
168 [defaults setBool:usesUnstable forKey:kPrefUnstableVersionCast];
169
170 if (!usesUnstable)
171 return nil;
172
173 feedURL = [[feedURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:kAppcastUnstable];
174 return [feedURL absoluteString];
175 }
176
177 @end