3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "PreferencesController.h"
19 @implementation PreferencesController
{
22 NSSize _generalSize
, _fileAccessSize
, _pathsSize
;
26 * Loads the NIB and shows the preferences
30 if (self = [super initWithWindowNibName
:@
"Preferences"])
32 _blankView
= [[NSView alloc
] init
];
39 _generalSize
= self.generalPreferencesView.frame.size
;
41 _fileAccessSize
= self.fileAccessPreferencesView.frame.size
;
43 NSUInteger i
= [self.toolbar.items indexOfObject
:self.fileAccessPreferencesItem
];
45 [self.toolbar removeItemAtIndex
:i
];
47 _pathsSize
= self.pathsPreferencesView.frame.size
;
51 * Shows the preferences controller window
53 - (void)showPreferencesWindow
55 NSWindow
* window
= self.window
; // Force the window to load.
56 [self showGeneral
:self];
58 [window makeKeyAndOrderFront
:self];
62 * Brings up a file picker to grant read-only file access to the selected path,
63 * which is then persisted across application restarts.
65 - (IBAction
)addFileAccess
:(id)sender
68 NSOpenPanel
* panel
= [NSOpenPanel openPanel
];
69 panel.canChooseDirectories
= YES
;
70 panel.canChooseFiles
= NO
;
71 if ([panel runModal
] != NSOKButton
)
74 NSURL
* url
= panel.URL
;
76 NSData
* secureBookmark
= [self.
class secureBookmarkDataForURL
:url
];
80 NSDictionaryControllerKeyValuePair
* pair
= [self.fileAccessController newObject
];
81 pair.key
= url.absoluteString
;
82 pair.value
= secureBookmark
;
83 [self.fileAccessController addObject
:pair
];
85 NSAssert(NO
, @
"not reached");
90 + (NSData
*)secureBookmarkDataForURL
:(NSURL
*)url
93 NSData
* secureBookmark
= [url bookmarkDataWithOptions
:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
94 includingResourceValuesForKeys
:nil
98 NSLog(@
"Error creating secure bookmark: %@", error
);
100 return secureBookmark
;
102 #endif // USE_APP_SANDBOX
104 #pragma mark Panel Switching
107 * Shows the general panel
109 - (IBAction
)showGeneral
:(id)sender
111 [self _switchToView
:self.generalPreferencesView resizeTo
:_generalSize forToolbarItem
:self.generalPreferencesItem
];
114 - (IBAction
)showFileAccess
:(id)sender
117 [self _switchToView
:self.fileAccessPreferencesView resizeTo
:_fileAccessSize forToolbarItem
:self.fileAccessPreferencesItem
];
119 NSAssert(NO
, @
"not reached");
124 * Shows the path replacement panel
126 - (IBAction
)showPaths
:(id)sender
128 [self _switchToView
:self.pathsPreferencesView resizeTo
:_pathsSize forToolbarItem
:self.pathsPreferencesItem
];
131 #pragma mark NSToolbar Delegate
134 * Returns the selection names
136 - (NSArray
*)toolbarSelectableItemIdentifiers
:(NSToolbar
*)toolbar
139 self.generalPreferencesItem.itemIdentifier
,
141 self.fileAccessPreferencesItem.itemIdentifier
,
143 self.pathsPreferencesItem.itemIdentifier
,
149 - (void)_switchToView
:(NSView
*)contentView
150 resizeTo
:(NSSize
)size
151 forToolbarItem
:(NSToolbarItem
*)item
{
152 if (self.window.contentView
== contentView
)
154 [self _resizeWindowToSize
:size
];
155 self.window.contentView
= contentView
;
156 self.toolbar.selectedItemIdentifier
= item.itemIdentifier
;
160 * Resizes the preferences window to be the size of the given preferences panel
162 - (void)_resizeWindowToSize
:(NSSize
)size
164 // Hide the current view when animating, to avoid weird artifacts.
165 self.window.contentView
= _blankView
;
167 NSWindowStyleMask styleMask
= self.window.styleMask
;
168 NSRect newFrame
= [NSWindow contentRectForFrameRect
:self.window.frame styleMask
:styleMask
];
170 CGFloat height
= size.height
+ 55;
171 newFrame.origin.y
+= newFrame.size.height
;
172 newFrame.origin.y
-= height
;
173 newFrame.size.height
= height
;
174 newFrame.size.width
= size.width
;
176 newFrame
= [NSWindow frameRectForContentRect
:newFrame styleMask
:styleMask
];
178 [[self window
] setFrame
:newFrame display
:YES animate
:YES
];