root/Footagehead/trunk/FHApplicationController.m

Revision 5274, 9.1 kB (checked in by morris, 7 months ago)

Add downloads folder

  • Property svn:keywords set to author date id revision
Line 
1 /* $Id$ */
2
3 /*
4  *  Copyright (c) 2003-2007 Axel Andersson
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "FHApplicationController.h"
30 #import "FHBrowserController.h"
31 #import "FHHandler.h"
32 #import "FHFileHandler.h"
33 #import "FHImageView.h"
34 #import "FHSettings.h"
35
36 @interface FHApplicationController(Private)
37
38 - (void)_buildGoMenu;
39 - (void)_reloadVolumesInGoMenu;
40 - (void)_addGoMenuItemWithPath:(NSString *)path keyEquivalent:(NSString *)keyEquivalent;
41 - (void)_reloadPathsInGoMenuForHandler:(FHHandler *)handler;
42 - (void)_updateViewMenu;
43
44 @end
45
46
47 @implementation FHApplicationController(Private)
48
49 - (void)_buildGoMenu {
50         _initialMenuItems = [_goMenu numberOfItems];
51        
52         [self _reloadVolumesInGoMenu];
53
54         [self _addGoMenuItemWithPath:@"~" keyEquivalent:@"H"];
55         [self _addGoMenuItemWithPath:@"~/Desktop" keyEquivalent:@"d"];
56         [self _addGoMenuItemWithPath:@"~/Downloads" keyEquivalent:@"D"];
57         [self _addGoMenuItemWithPath:@"~/Pictures" keyEquivalent:@"P"];
58        
59         [_goMenu addItem:[NSMenuItem separatorItem]];
60         _menuItems++;
61 }
62
63
64
65 - (void)_reloadVolumesInGoMenu {
66         NSEnumerator    *enumerator;
67         NSMenuItem              *item;
68         NSString                *volume, *path;
69         NSImage                 *icon;
70         NSUInteger              i = _initialMenuItems;
71        
72         if((NSUInteger) [_goMenu numberOfItems] > _initialMenuItems) {
73                 while(![[_goMenu itemAtIndex:_initialMenuItems] isSeparatorItem]) {
74                         [_goMenu removeItemAtIndex:_initialMenuItems];
75                         _menuItems--;
76                 }
77         }
78        
79         enumerator = [[[NSFileManager defaultManager] directoryContentsAtPath:@"/Volumes/"] objectEnumerator];
80        
81         while((volume = [enumerator nextObject])) {
82                 if([volume hasPrefix:@"."])
83                         continue;
84                
85                 path = [NSSWF:@"/Volumes/%@", volume];
86                 icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
87                 [icon setSize:NSMakeSize(16.0, 16.0)];
88
89                 item = [NSMenuItem itemWithTitle:[[NSFileManager defaultManager] displayNameAtPath:path]
90                                                                   action:@selector(go:)];
91                 [item setImage:icon];
92                 [item setRepresentedObject:[WIURL fileURLWithPath:path]];
93                 [_goMenu insertItem:item atIndex:i];
94                 _menuItems++;
95                 i++;
96         }
97        
98         if(i == (NSUInteger) [_goMenu numberOfItems] || ![[_goMenu itemAtIndex:i] isSeparatorItem]) {
99                 [_goMenu insertItem:[NSMenuItem separatorItem] atIndex:i];
100                 _menuItems++;
101         }
102 }
103
104
105
106 - (void)_addGoMenuItemWithPath:(NSString *)path keyEquivalent:(NSString *)keyEquivalent {
107         NSMenuItem      *item;
108         NSImage         *icon;
109        
110         path = [path stringByStandardizingPath];
111         icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
112         [icon setSize:NSMakeSize(16.0, 16.0)];
113
114         item = [NSMenuItem itemWithTitle:[[NSFileManager defaultManager] displayNameAtPath:path]
115                                                           action:@selector(go:)
116                                            keyEquivalent:keyEquivalent];
117
118         [item setImage:icon];
119         [item setRepresentedObject:[WIURL fileURLWithPath:path]];
120         [_goMenu addItem:item];
121
122         _menuItems++;
123 }
124
125
126
127 - (void)_reloadPathsInGoMenuForHandler:(FHHandler *)handler {
128         NSArray                 *stringComponents, *urlComponents;
129         NSMenuItem              *item;
130         NSImage                 *icon;
131         NSString                *name;
132         WIURL                   *url;
133         NSUInteger              i, count, items;
134        
135         stringComponents        = [handler stringComponents];
136         urlComponents           = [handler URLComponents];
137         count                           = [stringComponents count];
138         items                           = [_goMenu numberOfItems];
139        
140         for(i = 0; i < count; i++) {
141                 if(_menuItems + i + _initialMenuItems < items) {
142                         name = [[_goMenu itemAtIndex:i + _menuItems + _initialMenuItems] title];
143                        
144                         if([name isEqualToString:[stringComponents objectAtIndex:i]])
145                                 continue;
146                        
147                         [_goMenu removeItemAtIndex:_menuItems + i + _initialMenuItems];
148                         items--;
149                 }
150                
151                 url = [urlComponents objectAtIndex:i];
152
153                 item = [NSMenuItem itemWithTitle:[stringComponents objectAtIndex:i]
154                                                                   action:@selector(go:)];
155                 [item setRepresentedObject:url];
156                
157                 icon = [handler iconForURL:url];
158                
159                 if(icon) {
160                         [icon setSize:NSMakeSize(16.0, 16.0)];
161                         [item setImage:icon];
162                 }
163                        
164                 [_goMenu insertItem:item atIndex:_menuItems + i + _initialMenuItems];
165                 items++;
166         }
167        
168         while(items > _menuItems + count + _initialMenuItems) {
169                 [_goMenu removeItemAtIndex:_menuItems + count + _initialMenuItems];
170                
171                 items--;
172         }
173        
174         if(count == 0) {
175                 i = _initialMenuItems + _menuItems - 1;
176                
177                 if([[_goMenu itemAtIndex:i] isSeparatorItem]) {
178                         [_goMenu removeItemAtIndex:i];
179                        
180                         _menuItems--;
181                 }
182         } else {
183                 i = items - count - 1;
184                
185                 if(![[_goMenu itemAtIndex:i] isSeparatorItem]) {
186                         [_goMenu insertItem:[NSMenuItem separatorItem] atIndex:i + 1];
187                        
188                         _menuItems++;
189                 }
190         }
191 }
192
193
194
195 - (void)_updateViewMenu {
196         NSEnumerator    *enumerator;
197         NSMenuItem              *item;
198         FHImageScaling  scaling;
199         int                             spread;
200        
201         scaling = [FHSettings intForKey:FHImageScalingMethod];
202         enumerator = [[[_viewMenu itemArray] subarrayWithRange:NSMakeRange(0, 6)] objectEnumerator];
203        
204         while((item = [enumerator nextObject]))
205                 [item setState:((FHImageScaling) [item tag] == scaling) ? NSOnState : NSOffState];
206
207         spread = [FHSettings intForKey:FHSpreadMode];
208         enumerator = [[[_viewMenu itemArray] subarrayWithRange:NSMakeRange(7, 3)] objectEnumerator];
209        
210         while((item = [enumerator nextObject]))
211                 [item setState:([item tag] == spread) ? NSOnState : NSOffState];
212        
213         [_spreadRightToLeftMenuItem setState:[FHSettings boolForKey:FHSpreadRightToLeft] ? NSOnState : NSOffState];
214 }
215
216 @end
217
218
219 @implementation FHApplicationController
220
221 - (void)dealloc {
222         [_browserController release];
223        
224         [super dealloc];
225 }
226
227
228
229 - (void)awakeFromNib {
230         _openLastURL = YES;
231        
232         [self _buildGoMenu];
233         [self _updateViewMenu];
234
235         [[NSNotificationCenter defaultCenter]
236                 addObserver:self
237                    selector:@selector(windowControllerDidLoadHandler:)
238                            name:FHBrowserControllerDidLoadHandler];
239
240         [[NSNotificationCenter defaultCenter]
241                 addObserver:self
242                    selector:@selector(windowControllerChangedScalingMode:)
243                            name:FHWindowControllerChangedScalingMode];
244        
245         [[NSNotificationCenter defaultCenter]
246                 addObserver:self
247                    selector:@selector(windowControllerChangedSpreadMode:)
248                            name:FHWindowControllerChangedSpreadMode];
249        
250         [[[NSWorkspace sharedWorkspace] notificationCenter]
251                 addObserver:self
252                    selector:@selector(workspaceDidChangeMounts:)
253                            name:NSWorkspaceDidMountNotification];
254
255         [[[NSWorkspace sharedWorkspace] notificationCenter]
256                 addObserver:self
257                    selector:@selector(workspaceDidChangeMounts:)
258                            name:NSWorkspaceDidUnmountNotification];
259        
260         _browserController = [[FHBrowserController alloc] init];
261 }
262
263
264
265 #pragma mark -
266
267 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
268         WIURL           *url;
269        
270         if((GetCurrentKeyModifiers() & optionKey) != 0)
271                 [FHSettings setObject:[[WIURL fileURLWithPath:NSHomeDirectory()] string] forKey:FHOpenURL];
272                
273         if(_openLastURL) {
274                 url = [WIURL URLWithString:[FHSettings objectForKey:FHOpenURL]];
275                
276                 if([url isFileURL] && ![FHFileHandler handlesURL:url isPrimary:YES])
277                         url = [WIURL fileURLWithPath:NSHomeDirectory()];
278
279                 [_browserController loadURL:url];
280         }
281        
282         [_browserController showWindow:self];
283 }
284
285
286
287 - (void)applicationWillTerminate:(NSNotification *)notification {
288         WIURL   *url;
289        
290         url = [_browserController URL];
291        
292         if(url)
293                 [FHSettings setObject:[url string] forKey:FHOpenURL];
294 }
295
296
297
298 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
299         return YES;
300 }
301
302
303
304 - (BOOL)application:(NSApplication *)application openFile:(NSString *)path {
305         [_browserController loadURL:[WIURL fileURLWithPath:path]];
306        
307         _openLastURL = NO;
308
309         return YES;
310 }
311
312
313
314 - (void)workspaceDidChangeMounts:(NSNotification *)notification {
315         [self _reloadVolumesInGoMenu];
316 }
317
318
319
320 - (void)windowControllerDidLoadHandler:(NSNotification *)notification {
321         [self _reloadPathsInGoMenuForHandler:[notification object]];
322 }
323
324
325
326 - (void)windowControllerChangedScalingMode:(NSNotification *)notification {
327         [self _updateViewMenu];
328 }
329
330
331
332 - (void)windowControllerChangedSpreadMode:(NSNotification *)notification {
333         [self _updateViewMenu];
334 }
335
336
337
338 - (void)menuNeedsUpdate:(NSMenu *)menu {
339         if(menu == _viewMenu) {
340                 [_toggleStatusBarMenuItem setTitle:![FHSettings boolForKey:FHShowStatusBar]
341                         ? NSLS(@"Show Status Bar", @"Menu item title")
342                         : NSLS(@"Hide Status Bar", @"Menu item title")];
343         }
344 }
345
346
347 #pragma mark -
348
349 - (FHBrowserController *)browserController {
350         return _browserController;
351 }
352
353 @end
Note: See TracBrowser for help on using the browser.