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
{
24 * Loads the NIB and shows the preferences
28 if (self = [super initWithWindowNibName
:@
"Preferences"])
30 _blankView
= [[NSView alloc
] init
];
36 * Shows the preferences controller window
38 - (void)showPreferencesWindow
40 NSWindow
* window
= self.window
; // Force the window to load.
41 [self showGeneral
:self];
43 [window makeKeyAndOrderFront
:self];
47 * Brings up a file picker to grant read-only file access to the selected path,
48 * which is then persisted across application restarts.
50 - (IBAction
)addFileAccess
:(id)sender
52 NSOpenPanel
* panel
= [NSOpenPanel openPanel
];
53 panel.canChooseDirectories
= YES
;
54 panel.canChooseFiles
= NO
;
55 if ([panel runModal
] != NSOKButton
)
58 NSURL
* url
= panel.URL
;
61 NSData
* secureBookmark
= [url bookmarkDataWithOptions
:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
62 includingResourceValuesForKeys
:nil
66 NSLog(@
"Error creating secure bookmark: %@", error
);
70 NSDictionaryControllerKeyValuePair
* pair
= [self.fileAccessController newObject
];
71 pair.key
= url.absoluteString
;
72 pair.value
= secureBookmark
;
73 [self.fileAccessController addObject
:pair
];
76 #pragma mark Panel Switching
79 * Shows the general panel
81 - (IBAction
)showGeneral
:(id)sender
83 [self _switchToView
:self.generalPreferencesView forToolbarItem
:self.generalPreferencesItem
];
86 - (IBAction
)showFileAccess
:(id)sender
88 [self _switchToView
:self.fileAccessPreferencesView forToolbarItem
:self.fileAccessPreferencesItem
];
92 * Shows the path replacement panel
94 - (IBAction
)showPaths
:(id)sender
96 [self _switchToView
:self.pathsPreferencesView forToolbarItem
:self.pathsPreferencesItem
];
99 #pragma mark NSToolbar Delegate
102 * Returns the selection names
104 - (NSArray
*)toolbarSelectableItemIdentifiers
:(NSToolbar
*)toolbar
107 self.generalPreferencesItem.itemIdentifier
,
108 self.fileAccessPreferencesItem.itemIdentifier
,
109 self.pathsPreferencesItem.itemIdentifier
,
115 - (void)_switchToView
:(NSView
*)contentView forToolbarItem
:(NSToolbarItem
*)item
{
116 if (self.window.contentView
== contentView
)
118 [self _resizeWindowToSize
:contentView.frame.size
];
119 self.window.contentView
= contentView
;
120 self.toolbar.selectedItemIdentifier
= item.itemIdentifier
;
124 * Resizes the preferences window to be the size of the given preferences panel
126 - (void)_resizeWindowToSize
:(NSSize
)size
128 // Hide the current view when animating, to avoid weird artifacts.
129 self.window.contentView
= _blankView
;
131 NSWindowStyleMask styleMask
= self.window.styleMask
;
132 NSRect newFrame
= [NSWindow contentRectForFrameRect
:self.window.frame styleMask
:styleMask
];
134 CGFloat height
= size.height
+ 55;
135 newFrame.origin.y
+= newFrame.size.height
;
136 newFrame.origin.y
-= height
;
137 newFrame.size.height
= height
;
138 newFrame.size.width
= size.width
;
140 newFrame
= [NSWindow frameRectForContentRect
:newFrame styleMask
:styleMask
];
142 [[self window
] setFrame
:newFrame display
:YES animate
:YES
];