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
;
40 _fileAccessSize
= self.fileAccessPreferencesView.frame.size
;
41 _pathsSize
= self.pathsPreferencesView.frame.size
;
45 * Shows the preferences controller window
47 - (void)showPreferencesWindow
49 NSWindow
* window
= self.window
; // Force the window to load.
50 [self showGeneral
:self];
52 [window makeKeyAndOrderFront
:self];
56 * Brings up a file picker to grant read-only file access to the selected path,
57 * which is then persisted across application restarts.
59 - (IBAction
)addFileAccess
:(id)sender
61 NSOpenPanel
* panel
= [NSOpenPanel openPanel
];
62 panel.canChooseDirectories
= YES
;
63 panel.canChooseFiles
= NO
;
64 if ([panel runModal
] != NSOKButton
)
67 NSURL
* url
= panel.URL
;
69 NSData
* secureBookmark
= [self.
class secureBookmarkDataForURL
:url
];
73 NSDictionaryControllerKeyValuePair
* pair
= [self.fileAccessController newObject
];
74 pair.key
= url.absoluteString
;
75 pair.value
= secureBookmark
;
76 [self.fileAccessController addObject
:pair
];
79 + (NSData
*)secureBookmarkDataForURL
:(NSURL
*)url
82 NSData
* secureBookmark
= [url bookmarkDataWithOptions
:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
83 includingResourceValuesForKeys
:nil
87 NSLog(@
"Error creating secure bookmark: %@", error
);
89 return secureBookmark
;
92 #pragma mark Panel Switching
95 * Shows the general panel
97 - (IBAction
)showGeneral
:(id)sender
99 [self _switchToView
:self.generalPreferencesView resizeTo
:_generalSize forToolbarItem
:self.generalPreferencesItem
];
102 - (IBAction
)showFileAccess
:(id)sender
104 [self _switchToView
:self.fileAccessPreferencesView resizeTo
:_fileAccessSize forToolbarItem
:self.fileAccessPreferencesItem
];
108 * Shows the path replacement panel
110 - (IBAction
)showPaths
:(id)sender
112 [self _switchToView
:self.pathsPreferencesView resizeTo
:_pathsSize forToolbarItem
:self.pathsPreferencesItem
];
115 #pragma mark NSToolbar Delegate
118 * Returns the selection names
120 - (NSArray
*)toolbarSelectableItemIdentifiers
:(NSToolbar
*)toolbar
123 self.generalPreferencesItem.itemIdentifier
,
124 self.fileAccessPreferencesItem.itemIdentifier
,
125 self.pathsPreferencesItem.itemIdentifier
,
131 - (void)_switchToView
:(NSView
*)contentView
132 resizeTo
:(NSSize
)size
133 forToolbarItem
:(NSToolbarItem
*)item
{
134 if (self.window.contentView
== contentView
)
136 [self _resizeWindowToSize
:size
];
137 self.window.contentView
= contentView
;
138 self.toolbar.selectedItemIdentifier
= item.itemIdentifier
;
142 * Resizes the preferences window to be the size of the given preferences panel
144 - (void)_resizeWindowToSize
:(NSSize
)size
146 // Hide the current view when animating, to avoid weird artifacts.
147 self.window.contentView
= _blankView
;
149 NSWindowStyleMask styleMask
= self.window.styleMask
;
150 NSRect newFrame
= [NSWindow contentRectForFrameRect
:self.window.frame styleMask
:styleMask
];
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
;
158 newFrame
= [NSWindow frameRectForContentRect
:newFrame styleMask
:styleMask
];
160 [[self window
] setFrame
:newFrame display
:YES animate
:YES
];