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