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