root/trunk/PreferenceController.m

Revision 99, 6.2 kB (checked in by bart, 1 year ago)

0.8.1

Line 
1 #import "PreferenceController.h"
2
3 NSString *SSHToolsPathString = @"SSH Tools Path";
4 NSString *SocketPathString = @"Authentication Socket Path";
5 NSString *DisplayString = @"Display";
6 NSString *AddKeysOnConnectionString = @"Add Keys On Connection";
7 NSString *AskForConfirmationString = @"Ask for Confirmation";
8 NSString *OnSleepString = @"On Sleep";
9 NSString *OnScreensaverString = @"On Screensaver";
10 NSString *FollowKeychainString = @"Follow Keychain";
11 NSString *MinutesOfSleepString = @"Minutes of Sleep";
12 NSString *ManageGlobalEnvironmentString = @"Manage Global Environment";
13 NSString *CheckForUpdatesOnStartupString = @"Check For Updates On Startup";
14 NSString *TunnelsString = @"Tunnels";
15 NSString *UseGlobalEnvironmentString = @"Use Global Environment ~/.MacOSX/environment.plist";
16 NSString *UseCustomSecuritySettingsString = @"Use Custom Security Settings";
17 NSString *CheckScreensaverIntervalString = @"Check Screensaver Interval";
18 NSString *KeyTimeoutString = @"Key Timeout";
19
20 PreferenceController *sharedPreferenceController = nil;
21
22 @implementation PreferenceController
23
24
25 + (PreferenceController *)sharedController
26 {
27         if(!sharedPreferenceController) {
28                 return [[PreferenceController alloc] init];
29         }
30        
31         return sharedPreferenceController;
32 }
33
34 + (void)openPreferencesWindow
35 {
36         PreferenceController *preferenceController = [PreferenceController sharedController];
37        
38         if(preferenceController)
39         {
40                 [NSApp activateIgnoringOtherApps:YES];
41                 [preferenceController showWindow];
42         }
43 }
44
45 #pragma mark -
46
47 - (id)init
48 {
49         if(self = [super init])
50         {
51                 sharedPreferenceController = self;
52
53                 blankView = [[NSView alloc] init];
54                
55                 [NSBundle loadNibNamed:@"Preferences" owner:sharedPreferenceController];
56         }
57        
58         return self;
59 }
60
61 - (void)dealloc
62 {
63         [blankView release];
64         [super dealloc];
65 }
66
67 - (void)awakeFromNib
68 {
69         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:)
70                                                 name:@"NSWindowWillCloseNotification" object:NSApp];
71
72         /* Set the required information for all preference sections. */
73         preferenceItems = [[NSDictionary dictionaryWithObjectsAndKeys:
74                         [NSArray arrayWithObjects:@"preference_general", generalController, local(@"General"), nil], @"General",
75                         [NSArray arrayWithObjects:@"preference_display", displayController, local(@"Display"), nil], @"Display",
76                         [NSArray arrayWithObjects:@"preference_environment", environmentController, local(@"Environment"), nil], @"Environment",
77                         [NSArray arrayWithObjects:@"preference_keys", keysController, local(@"SSH Keys"), nil], @"SSH Keys",
78                         [NSArray arrayWithObjects:@"preference_tunnels", tunnelsController, local(@"Tunnels"), nil], @"Tunnels",
79                         [NSArray arrayWithObjects:@"preference_security", securityController, local(@"Security"), nil], @"Security",
80                         nil]
81                 retain];
82
83         /* Define the precedence of the sections. */
84         preferenceItemsKeys = [[NSArray arrayWithObjects:@"General", @"Display", @"SSH Keys", @"Tunnels", @"Security", @"Environment", nil] retain];
85
86         toolbar = [[NSToolbar alloc] initWithIdentifier:@"preferenceToolbar"];
87         [toolbar setDelegate:self];
88         [toolbar setAllowsUserCustomization:NO];
89         [toolbar setAutosavesConfiguration:NO];
90         [window setToolbar:[toolbar autorelease]];
91        
92         [self switchToView:@"General"];
93
94         [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
95 }
96
97 - (void)showWindow
98 {
99         [window makeKeyAndOrderFront:self];
100 }
101
102 - (NSWindow *)window
103 {
104         return window;
105 }
106
107 - (void)resizeWindowToSize:(NSSize)size
108 {
109         NSRect frame, contentRect;
110         float toolbarHeight, newHeight;
111        
112         /* Determine the toolbar height. */
113         contentRect = [NSWindow contentRectForFrameRect:[window frame] styleMask:[window styleMask]];
114         toolbarHeight = (NSHeight(contentRect) - NSHeight([[window contentView] frame]));
115        
116         newHeight = size.height;
117
118         frame = [NSWindow contentRectForFrameRect:[window frame] styleMask:[window styleMask]];
119        
120         frame.origin.y += frame.size.height;
121         frame.origin.y -= newHeight + toolbarHeight;
122         frame.size.height = newHeight + toolbarHeight;
123         frame.size.width = 475;
124        
125         frame = [NSWindow frameRectForContentRect:frame styleMask:[window styleMask]];
126        
127         [window setFrame:frame display:YES animate:YES];
128 }
129
130 /* Switch to a preference section from a toolbar. Calls switchToView. */
131 - (void)switchToViewFromToolbar:(NSToolbarItem *)item
132 {
133         [self switchToView:[item itemIdentifier]];
134 }
135
136 /* Switch to a preference section. */
137 - (void)switchToView:(NSString *)identifier
138 {
139         NSArray *array;
140
141         if(((array = [preferenceItems objectForKey:identifier])) && ([array count] > 1))
142         {
143                 if(([array objectAtIndex:1]) && (currentController != [array objectAtIndex:1]))
144                 {
145                         [currentController closePreferences];
146
147                         currentController = [array objectAtIndex:1];
148
149                         [window setContentView:blankView];
150                         [window setTitle:[NSString stringWithFormat:@"%@ - %@", local(@"Preferences"), [array objectAtIndex:2]]];
151                         [self resizeWindowToSize:[currentController viewSize]];
152
153                         if ([toolbar respondsToSelector:@selector(setSelectedItemIdentifier:)])
154                                 [toolbar setSelectedItemIdentifier:identifier];
155                         [window setContentView:[currentController view]];
156
157                         [currentController loadPreferences];
158                 }
159         }
160 }
161
162 - (void)windowWillClose:(NSNotification *)notification
163 {
164         if([notification object] == window)
165         {
166                 [currentController closePreferences];
167         }
168 }
169
170 /* Toolbar Delegates. */
171
172 /* Initialize the items. */
173 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
174 {
175         NSArray *array;
176
177         if((array = [preferenceItems objectForKey:itemIdentifier]))
178         {
179                 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
180
181                 [item setLabel:[array objectAtIndex:2]];
182                 [item setPaletteLabel:[array objectAtIndex:2]];
183                 [item setImage:[NSImage imageNamed:[array objectAtIndex:0]]];
184                 [item setTarget:self];
185                 [item setAction:@selector(switchToViewFromToolbar:)];
186
187                 return [item autorelease];
188         }
189        
190         return nil;
191 }
192
193 /* Return the allowed item identifiers. */
194 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
195 {
196         return preferenceItemsKeys;
197 }
198
199 /* Return the default item identifiers. */
200 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
201 {
202         return preferenceItemsKeys;
203 }
204
205 /* Return the selectable item identifiers (>10.3). */
206 - (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
207 {
208         return preferenceItemsKeys;
209 }
210
211 @end
Note: See TracBrowser for help on using the browser.