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