Bump project version to 212.1.
[macgdbp.git] / Source / PreferencesController.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 "PreferencesController.h"
18
19 @implementation PreferencesController {
20 NSView* _blankView;
21
22 NSSize _generalSize, _fileAccessSize, _pathsSize;
23 }
24
25 /**
26 * Loads the NIB and shows the preferences
27 */
28 - (id)init
29 {
30 if (self = [super initWithWindowNibName:@"Preferences"])
31 {
32 _blankView = [[NSView alloc] init];
33 }
34 return self;
35 }
36
37 - (void)awakeFromNib
38 {
39 _generalSize = self.generalPreferencesView.frame.size;
40 #if USE_APP_SANDBOX
41 _fileAccessSize = self.fileAccessPreferencesView.frame.size;
42 #else
43 NSUInteger i = [self.toolbar.items indexOfObject:self.fileAccessPreferencesItem];
44 if (i != NSNotFound)
45 [self.toolbar removeItemAtIndex:i];
46 #endif
47 _pathsSize = self.pathsPreferencesView.frame.size;
48 }
49
50 /**
51 * Shows the preferences controller window
52 */
53 - (void)showPreferencesWindow
54 {
55 NSWindow* window = self.window; // Force the window to load.
56 [self showGeneral:self];
57 [window center];
58 [window makeKeyAndOrderFront:self];
59 }
60
61 /**
62 * Brings up a file picker to grant read-only file access to the selected path,
63 * which is then persisted across application restarts.
64 */
65 - (IBAction)addFileAccess:(id)sender
66 {
67 #if USE_APP_SANDBOX
68 NSOpenPanel* panel = [NSOpenPanel openPanel];
69 panel.canChooseDirectories = YES;
70 panel.canChooseFiles = NO;
71 if ([panel runModal] != NSOKButton)
72 return;
73
74 NSURL* url = panel.URL;
75
76 NSData* secureBookmark = [self.class secureBookmarkDataForURL:url];
77 if (!secureBookmark)
78 return;
79
80 NSDictionaryControllerKeyValuePair* pair = [self.fileAccessController newObject];
81 pair.key = url.absoluteString;
82 pair.value = secureBookmark;
83 [self.fileAccessController addObject:pair];
84 #else
85 NSAssert(NO, @"not reached");
86 #endif
87 }
88
89 #if USE_APP_SANDBOX
90 + (NSData*)secureBookmarkDataForURL:(NSURL*)url
91 {
92 NSError* error;
93 NSData* secureBookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
94 includingResourceValuesForKeys:nil
95 relativeToURL:nil
96 error:&error];
97 if (error) {
98 NSLog(@"Error creating secure bookmark: %@", error);
99 }
100 return secureBookmark;
101 }
102 #endif // USE_APP_SANDBOX
103
104 #pragma mark Panel Switching
105
106 /**
107 * Shows the general panel
108 */
109 - (IBAction)showGeneral:(id)sender
110 {
111 [self _switchToView:self.generalPreferencesView resizeTo:_generalSize forToolbarItem:self.generalPreferencesItem];
112 }
113
114 - (IBAction)showFileAccess:(id)sender
115 {
116 #if USE_APP_SANDBOX
117 [self _switchToView:self.fileAccessPreferencesView resizeTo:_fileAccessSize forToolbarItem:self.fileAccessPreferencesItem];
118 #else
119 NSAssert(NO, @"not reached");
120 #endif
121 }
122
123 /**
124 * Shows the path replacement panel
125 */
126 - (IBAction)showPaths:(id)sender
127 {
128 [self _switchToView:self.pathsPreferencesView resizeTo:_pathsSize forToolbarItem:self.pathsPreferencesItem];
129 }
130
131 #pragma mark NSToolbar Delegate
132
133 /**
134 * Returns the selection names
135 */
136 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)toolbar
137 {
138 return @[
139 self.generalPreferencesItem.itemIdentifier,
140 #if USE_APP_SANDBOX
141 self.fileAccessPreferencesItem.itemIdentifier,
142 #endif
143 self.pathsPreferencesItem.itemIdentifier,
144 ];
145 }
146
147 #pragma mark Private
148
149 - (void)_switchToView:(NSView*)contentView
150 resizeTo:(NSSize)size
151 forToolbarItem:(NSToolbarItem*)item {
152 if (self.window.contentView == contentView)
153 return;
154 [self _resizeWindowToSize:size];
155 self.window.contentView = contentView;
156 self.toolbar.selectedItemIdentifier = item.itemIdentifier;
157 }
158
159 /**
160 * Resizes the preferences window to be the size of the given preferences panel
161 */
162 - (void)_resizeWindowToSize:(NSSize)size
163 {
164 // Hide the current view when animating, to avoid weird artifacts.
165 self.window.contentView = _blankView;
166 NSPoint origin = self.window.frame.origin;
167 NSRect newFrame = [self.window frameRectForContentRect:NSMakeRect(origin.x, origin.y, size.width, size.height)];
168 [[self window] setFrame:newFrame display:YES animate:YES];
169 }
170
171 @end