1 |
/* SmallIconsViewer.m |
2 |
* |
3 |
* Copyright (C) 2003 Free Software Foundation, Inc. |
4 |
* |
5 |
* Author: Enrico Sersale <enrico@imago.ro> |
6 |
* Date: August 2001 |
7 |
* |
8 |
* This file is part of the GNUstep GWorkspace application |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
*/ |
24 |
|
25 |
|
26 |
#include <Foundation/Foundation.h> |
27 |
#include <AppKit/AppKit.h> |
28 |
#ifdef GNUSTEP |
29 |
#include "GWLib.h" |
30 |
#include "GWProtocol.h" |
31 |
#include "GWFunctions.h" |
32 |
#include "GWNotifications.h" |
33 |
#else |
34 |
#include <GWorkspace/GWLib.h> |
35 |
#include <GWorkspace/GWProtocol.h> |
36 |
#include <GWorkspace/GWFunctions.h> |
37 |
#include <GWorkspace/GWNotifications.h> |
38 |
#endif |
39 |
#include "SmallIconsViewer.h" |
40 |
#include "Banner.h" |
41 |
#include "PathsPopUp.h" |
42 |
#include "SmallIconsPanel.h" |
43 |
#include "GNUstep.h" |
44 |
|
45 |
#define SETRECT(o, x, y, w, h) { \ |
46 |
NSRect rct = NSMakeRect(x, y, w, h); \ |
47 |
if (rct.size.width < 0) rct.size.width = 0; \ |
48 |
if (rct.size.height < 0) rct.size.height = 0; \ |
49 |
[o setFrame: rct]; \ |
50 |
} |
51 |
|
52 |
@implementation SmallIconsViewer |
53 |
|
54 |
- (void)dealloc |
55 |
{ |
56 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
57 |
TEST_RELEASE (rootPath); |
58 |
TEST_RELEASE (lastPath); |
59 |
TEST_RELEASE (currentPath); |
60 |
TEST_RELEASE (selectedPaths); |
61 |
TEST_RELEASE (savedSelection); |
62 |
TEST_RELEASE (watchedPaths); |
63 |
TEST_RELEASE (banner); |
64 |
TEST_RELEASE (panelScroll); |
65 |
TEST_RELEASE (panel); |
66 |
[super dealloc]; |
67 |
} |
68 |
|
69 |
- (id)init |
70 |
{ |
71 |
self = [super initWithFrame: NSZeroRect]; |
72 |
|
73 |
if (self) { |
74 |
#ifdef GNUSTEP |
75 |
Class gwclass = [[NSBundle mainBundle] principalClass]; |
76 |
#else |
77 |
Class gwclass = [[NSBundle mainBundle] classNamed: @"GWorkspace"]; |
78 |
#endif |
79 |
|
80 |
gworkspace = (id<GWProtocol>)[gwclass gworkspace]; |
81 |
rootPath = nil; |
82 |
lastPath = nil; |
83 |
currentPath = nil; |
84 |
selectedPaths = nil; |
85 |
watchedPaths = nil; |
86 |
panelScroll = nil; |
87 |
banner = nil; |
88 |
panel = nil; |
89 |
} |
90 |
|
91 |
return self; |
92 |
} |
93 |
|
94 |
// |
95 |
// NSCopying |
96 |
// |
97 |
- (id)copyWithZone:(NSZone *)zone |
98 |
{ |
99 |
SmallIconsViewer *vwr = [[SmallIconsViewer alloc] init]; |
100 |
return vwr; |
101 |
} |
102 |
|
103 |
// |
104 |
// ViewersProtocol |
105 |
// |
106 |
- (void)setRootPath:(NSString *)rpath |
107 |
viewedPath:(NSString *)vpath |
108 |
selection:(NSArray *)selection |
109 |
delegate:(id)adelegate |
110 |
viewApps:(BOOL)canview |
111 |
{ |
112 |
int colswidth, winwidth; |
113 |
|
114 |
fm = [NSFileManager defaultManager]; |
115 |
|
116 |
[self setDelegate: adelegate]; |
117 |
ASSIGN (currentPath, rpath); |
118 |
viewsapps = canview; |
119 |
|
120 |
colswidth = [delegate browserColumnsWidth]; |
121 |
resizeIncrement = colswidth; |
122 |
winwidth = [delegate getWindowFrameWidth]; |
123 |
columns = (int)winwidth / resizeIncrement; |
124 |
columnsWidth = (winwidth - 16) / columns; |
125 |
|
126 |
[self setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)]; |
127 |
|
128 |
if (banner != nil) { |
129 |
[banner removeFromSuperview]; |
130 |
RELEASE (banner); |
131 |
} |
132 |
|
133 |
banner = [[Banner alloc] init]; |
134 |
pathsPopUp = [banner pathsPopUp]; |
135 |
[pathsPopUp setTarget: self]; |
136 |
[pathsPopUp setAction: @selector(popUpAction:)]; |
137 |
[self addSubview: banner]; |
138 |
|
139 |
TEST_RELEASE (panel); |
140 |
|
141 |
if (panelScroll != nil) { |
142 |
[panelScroll removeFromSuperview]; |
143 |
RELEASE (panelScroll); |
144 |
} |
145 |
panelScroll = [NSScrollView new]; |
146 |
[panelScroll setBorderType: NSBezelBorder]; |
147 |
[panelScroll setHasHorizontalScroller: YES]; |
148 |
[panelScroll setHasVerticalScroller: YES]; |
149 |
[panelScroll setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)]; |
150 |
[self addSubview: panelScroll]; |
151 |
|
152 |
panel = [[SmallIconsPanel alloc] initAtPath: currentPath delegate: self]; |
153 |
[panelScroll setDocumentView: panel]; |
154 |
|
155 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
156 |
|
157 |
[[NSNotificationCenter defaultCenter] addObserver: self |
158 |
selector: @selector(fileSystemWillChange:) |
159 |
name: GWFileSystemWillChangeNotification |
160 |
object: nil]; |
161 |
|
162 |
[[NSNotificationCenter defaultCenter] addObserver: self |
163 |
selector: @selector(fileSystemDidChange:) |
164 |
name: GWFileSystemDidChangeNotification |
165 |
object: nil]; |
166 |
|
167 |
[[NSNotificationCenter defaultCenter] addObserver: self |
168 |
selector: @selector(sortTypeDidChange:) |
169 |
name: GWSortTypeDidChangeNotification |
170 |
object: nil]; |
171 |
|
172 |
if (watchedPaths != nil) { |
173 |
[self unsetWatchers]; |
174 |
DESTROY (watchedPaths); |
175 |
} |
176 |
|
177 |
[self setSelectedPaths: [NSArray arrayWithObject: currentPath]]; |
178 |
|
179 |
if (vpath) { |
180 |
[self setSelectedPaths: [NSArray arrayWithObject: vpath]]; |
181 |
[panel setPath: vpath]; |
182 |
[panel scrollFirstIconToVisible]; |
183 |
|
184 |
if (selection && [selection count]) { |
185 |
[panel selectIconsWithPaths: selection]; |
186 |
} |
187 |
[delegate addPathToHistory: [NSArray arrayWithObject: vpath]]; |
188 |
} else { |
189 |
[delegate addPathToHistory: [NSArray arrayWithObject: currentPath]]; |
190 |
} |
191 |
|
192 |
[self updateDiskInfo]; |
193 |
|
194 |
firstResize = YES; |
195 |
} |
196 |
|
197 |
- (NSString *)menuName |
198 |
{ |
199 |
return @"Small Icon"; |
200 |
} |
201 |
|
202 |
- (NSString *)shortCut |
203 |
{ |
204 |
return @"k"; |
205 |
} |
206 |
|
207 |
- (BOOL)usesShelf |
208 |
{ |
209 |
return NO; |
210 |
} |
211 |
|
212 |
- (NSSize)resizeIncrements |
213 |
{ |
214 |
return NSZeroSize; |
215 |
} |
216 |
|
217 |
- (NSImage *)miniicon |
218 |
{ |
219 |
NSBundle *bundle = [NSBundle bundleForClass: [self class]]; |
220 |
NSString *imgpath = [bundle pathForResource: @"miniwindow" ofType: @"tiff"]; |
221 |
NSImage *img = [[NSImage alloc] initWithContentsOfFile: imgpath]; |
222 |
return AUTORELEASE (img); |
223 |
} |
224 |
|
225 |
- (BOOL)hasPreferences |
226 |
{ |
227 |
return NO; |
228 |
} |
229 |
|
230 |
- (id)prefController |
231 |
{ |
232 |
return nil; |
233 |
} |
234 |
|
235 |
- (void)setSelectedPaths:(NSArray *)paths |
236 |
{ |
237 |
NSString *newPath; |
238 |
NSArray *components; |
239 |
NSMutableArray *wpaths; |
240 |
NSString *s; |
241 |
BOOL isDir; |
242 |
int i, j; |
243 |
|
244 |
if ([paths count] == 0) { |
245 |
return; |
246 |
} |
247 |
|
248 |
ASSIGN (selectedPaths, paths); |
249 |
[delegate setTheSelectedPaths: paths]; |
250 |
|
251 |
newPath = [paths objectAtIndex: 0]; |
252 |
[fm fileExistsAtPath: newPath isDirectory: &isDir]; |
253 |
if ((isDir == NO) || ([paths count] > 1)) { |
254 |
newPath = [newPath stringByDeletingLastPathComponent]; |
255 |
} else { |
256 |
if (([gworkspace isPakageAtPath: newPath]) && (viewsapps == NO)) { |
257 |
newPath = [newPath stringByDeletingLastPathComponent]; |
258 |
} |
259 |
} |
260 |
|
261 |
if (lastPath && [lastPath isEqual: newPath]) { |
262 |
return; |
263 |
} else { |
264 |
ASSIGN (lastPath, newPath); |
265 |
} |
266 |
|
267 |
ASSIGN (currentPath, newPath); |
268 |
|
269 |
[delegate setTitleAndPath: currentPath selectedPaths: selectedPaths]; |
270 |
|
271 |
components = [currentPath pathComponents]; |
272 |
[self makePopUp: components]; |
273 |
|
274 |
wpaths = [NSMutableArray arrayWithCapacity: 1]; |
275 |
s = [NSString string]; |
276 |
|
277 |
for (i = 0; i < [components count]; i++) { |
278 |
s = [s stringByAppendingPathComponent: [components objectAtIndex: i]]; |
279 |
[wpaths addObject: s]; |
280 |
} |
281 |
|
282 |
if (watchedPaths == nil) { |
283 |
watchedPaths = [wpaths mutableCopy]; |
284 |
[self setWatchers]; |
285 |
|
286 |
} else { |
287 |
int count = [wpaths count]; |
288 |
|
289 |
for (i = 0; i < [watchedPaths count]; i++) { |
290 |
NSString *s1, *s2; |
291 |
|
292 |
s1 = [watchedPaths objectAtIndex: i]; |
293 |
|
294 |
if (count > i) { |
295 |
s2 = [wpaths objectAtIndex: i]; |
296 |
} else { |
297 |
i = count; |
298 |
break; |
299 |
} |
300 |
|
301 |
if ([s1 isEqualToString: s2] == NO) { |
302 |
break; |
303 |
} |
304 |
} |
305 |
|
306 |
for (j = i; j < [watchedPaths count]; j++) { |
307 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: j]]; |
308 |
} |
309 |
|
310 |
for (j = i; j < [wpaths count]; j++) { |
311 |
[self setWatcherForPath: [wpaths objectAtIndex: j]]; |
312 |
} |
313 |
|
314 |
TEST_RELEASE (watchedPaths); |
315 |
watchedPaths = [wpaths mutableCopy]; |
316 |
} |
317 |
} |
318 |
|
319 |
- (void)setCurrentSelection:(NSArray *)paths |
320 |
{ |
321 |
NSString *path; |
322 |
NSArray *selection; |
323 |
|
324 |
if ([paths count] == 0) { |
325 |
return; |
326 |
} |
327 |
|
328 |
[self setSelectedPaths: paths]; |
329 |
|
330 |
path = [paths objectAtIndex: 0]; |
331 |
|
332 |
if ([paths count] > 1) { |
333 |
path = [path stringByDeletingLastPathComponent]; |
334 |
selection = [NSArray arrayWithArray: selectedPaths]; |
335 |
} else { |
336 |
BOOL isdir; |
337 |
|
338 |
[fm fileExistsAtPath: path isDirectory: &isdir]; |
339 |
|
340 |
if (isdir == NO) { |
341 |
path = [path stringByDeletingLastPathComponent]; |
342 |
selection = [NSArray arrayWithArray: selectedPaths]; |
343 |
} else { |
344 |
if ([gworkspace isPakageAtPath: path] && (viewsapps == NO)) { |
345 |
path = [path stringByDeletingLastPathComponent]; |
346 |
selection = [NSArray arrayWithArray: selectedPaths]; |
347 |
} else { |
348 |
selection = [NSArray array]; |
349 |
} |
350 |
} |
351 |
} |
352 |
|
353 |
[panel setPath: path]; |
354 |
[panel scrollFirstIconToVisible]; |
355 |
[panel selectIconsWithPaths: selection]; |
356 |
[panel setNeedsDisplay: YES]; |
357 |
[self updateDiskInfo]; |
358 |
} |
359 |
|
360 |
- (NSPoint)positionForSlidedImage |
361 |
{ |
362 |
return NSMakePoint(0, 0); |
363 |
} |
364 |
|
365 |
- (void)selectAll |
366 |
{ |
367 |
[panel selectAllIcons]; |
368 |
} |
369 |
|
370 |
- (NSArray *)selectedPaths |
371 |
{ |
372 |
return selectedPaths; |
373 |
} |
374 |
|
375 |
- (NSString *)currentViewedPath |
376 |
{ |
377 |
return [panel currentPath]; |
378 |
} |
379 |
|
380 |
- (NSPoint)locationOfIconForPath:(NSString *)path |
381 |
{ |
382 |
NSString *name = [path lastPathComponent]; |
383 |
NSPoint p = [panel locationOfIconWithName: name]; |
384 |
|
385 |
if ((p.x == 0) && (p.y == 0)) { |
386 |
return p; |
387 |
|
388 |
} else { |
389 |
NSView *sview = [[self window] contentView]; |
390 |
NSRect r = [self visibleRect]; |
391 |
NSPoint lim1 = r.origin; |
392 |
NSPoint lim2 = NSMakePoint(lim1.x + r.size.width, lim1.y + r.size.height); |
393 |
|
394 |
p = [sview convertPoint: p fromView: panel]; |
395 |
lim1 = [sview convertPoint: lim1 fromView: self]; |
396 |
lim2 = [sview convertPoint: lim2 fromView: self]; |
397 |
|
398 |
if (p.x < lim1.x) p.x = lim1.x; |
399 |
if (p.y < lim1.y) p.y = lim1.y; |
400 |
|
401 |
if (p.x >= lim2.x) p.x = lim2.x - 60; |
402 |
if (p.y >= lim2.y) p.y = lim2.y - 60; |
403 |
|
404 |
if (p.x == 0) p.x = 1; |
405 |
if (p.y == 0) p.y = 1; |
406 |
} |
407 |
|
408 |
return p; |
409 |
} |
410 |
|
411 |
- (void)unsetWatchers |
412 |
{ |
413 |
int i; |
414 |
|
415 |
[[NSNotificationCenter defaultCenter] removeObserver: self |
416 |
name: GWFileWatcherFileDidChangeNotification object: nil]; |
417 |
|
418 |
for (i = 0; i < [watchedPaths count]; i++) { |
419 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: i]]; |
420 |
} |
421 |
} |
422 |
|
423 |
- (void)setResizeIncrement:(int)increment |
424 |
{ |
425 |
resizeIncrement = increment; // CONTROLLARE !!!!!! |
426 |
} |
427 |
|
428 |
- (void)setAutoSynchronize:(BOOL)value |
429 |
{ |
430 |
autoSynchronize = value; |
431 |
} |
432 |
|
433 |
- (void)thumbnailsDidChangeInPaths:(NSArray *)paths |
434 |
{ |
435 |
if (paths == nil) { |
436 |
[panel makeFileIcons]; |
437 |
[panel resizeWithOldSuperviewSize: [panel frame].size]; |
438 |
} else { |
439 |
int i; |
440 |
|
441 |
for (i = 0; i < [paths count]; i++) { |
442 |
NSString *dir = [paths objectAtIndex: i]; |
443 |
|
444 |
if ([panel isOnBasePath: dir withFiles: nil]) { |
445 |
[panel reloadFromPath: dir]; |
446 |
[self setNeedsDisplay: YES]; |
447 |
} |
448 |
} |
449 |
} |
450 |
} |
451 |
|
452 |
- (id)viewerView |
453 |
{ |
454 |
return panel; |
455 |
} |
456 |
|
457 |
- (BOOL)viewsApps |
458 |
{ |
459 |
return viewsapps; |
460 |
} |
461 |
|
462 |
- (id)delegate |
463 |
{ |
464 |
return delegate; |
465 |
} |
466 |
|
467 |
- (void)setDelegate:(id)anObject |
468 |
{ |
469 |
delegate = anObject; |
470 |
} |
471 |
|
472 |
// |
473 |
// End of ViewersProtocol |
474 |
// |
475 |
|
476 |
- (void)validateCurrentPathAfterOperation:(NSDictionary *)opdict |
477 |
{ |
478 |
if ([currentPath isEqualToString: fixPath(@"/", 0)]) { |
479 |
return; |
480 |
} else { |
481 |
NSString *operation = [opdict objectForKey: @"operation"]; |
482 |
NSString *source = [opdict objectForKey: @"source"]; |
483 |
NSArray *files = [opdict objectForKey: @"files"]; |
484 |
int i; |
485 |
|
486 |
if (operation == NSWorkspaceMoveOperation |
487 |
|| operation == NSWorkspaceDestroyOperation |
488 |
|| operation == GWorkspaceRenameOperation |
489 |
|| operation == NSWorkspaceRecycleOperation |
490 |
|| operation == GWorkspaceRecycleOutOperation |
491 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
492 |
|
493 |
if (operation == GWorkspaceRenameOperation) { |
494 |
files = [NSArray arrayWithObject: [source lastPathComponent]]; |
495 |
source = [source stringByDeletingLastPathComponent]; |
496 |
} |
497 |
|
498 |
for (i = 0; i < [files count]; i++) { |
499 |
NSString *fpath = [source stringByAppendingPathComponent: [files objectAtIndex: i]]; |
500 |
|
501 |
if (subPathOfPath(fpath, currentPath) |
502 |
|| [fpath isEqualToString: currentPath]) { |
503 |
[self closeNicely]; |
504 |
break; |
505 |
} |
506 |
} |
507 |
} |
508 |
} |
509 |
} |
510 |
|
511 |
- (void)fileSystemWillChange:(NSNotification *)notification |
512 |
{ |
513 |
NSDictionary *dict = (NSDictionary *)[notification object]; |
514 |
NSString *operation = [dict objectForKey: @"operation"]; |
515 |
NSString *source = [dict objectForKey: @"source"]; |
516 |
NSString *destination = [dict objectForKey: @"destination"]; |
517 |
NSArray *files = [dict objectForKey: @"files"]; |
518 |
|
519 |
[self validateCurrentPathAfterOperation: dict]; |
520 |
|
521 |
[banner startIndicatorForOperation: operation]; |
522 |
|
523 |
if (operation == NSWorkspaceMoveOperation |
524 |
|| operation == NSWorkspaceCopyOperation |
525 |
|| operation == NSWorkspaceLinkOperation |
526 |
|| operation == NSWorkspaceDuplicateOperation |
527 |
|| operation == NSWorkspaceRecycleOperation |
528 |
|| operation == GWorkspaceRecycleOutOperation) { |
529 |
|
530 |
if ([panel isOnBasePath: destination withFiles: files]) { |
531 |
[self unsetWatchersFromPath: destination]; |
532 |
|
533 |
[panel extendSelectionWithDimmedFiles: files |
534 |
startingAtPath: destination]; |
535 |
} |
536 |
} |
537 |
|
538 |
if (operation == GWorkspaceCreateFileOperation |
539 |
|| operation == GWorkspaceCreateDirOperation) { |
540 |
if ([panel isOnBasePath: destination withFiles: nil]) { |
541 |
[self unsetWatchersFromPath: destination]; |
542 |
} |
543 |
} |
544 |
|
545 |
if (operation == GWorkspaceCreateFileOperation |
546 |
|| operation == GWorkspaceCreateDirOperation) { |
547 |
if ([panel isOnBasePath: destination withFiles: nil]) { |
548 |
[self unsetWatchersFromPath: destination]; |
549 |
} |
550 |
} |
551 |
|
552 |
if (operation == NSWorkspaceMoveOperation |
553 |
|| operation == NSWorkspaceDestroyOperation |
554 |
|| operation == NSWorkspaceRecycleOperation |
555 |
|| operation == GWorkspaceRecycleOutOperation |
556 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
557 |
|
558 |
if ([panel isOnBasePath: source withFiles: files]) { |
559 |
[self unsetWatchersFromPath: source]; |
560 |
|
561 |
[panel extendSelectionWithDimmedFiles: files |
562 |
startingAtPath: source]; |
563 |
} |
564 |
} |
565 |
} |
566 |
|
567 |
- (void)fileSystemDidChange:(NSNotification *)notification |
568 |
{ |
569 |
NSMutableDictionary *dict; |
570 |
NSString *operation, *source, *destination; |
571 |
NSArray *files; |
572 |
|
573 |
dict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
574 |
[dict addEntriesFromDictionary: (NSDictionary *)[notification object]]; |
575 |
|
576 |
operation = [dict objectForKey: @"operation"]; |
577 |
source = [dict objectForKey: @"source"]; |
578 |
destination = [dict objectForKey: @"destination"]; |
579 |
files = [dict objectForKey: @"files"]; |
580 |
|
581 |
[banner stopIndicatorForOperation: operation]; |
582 |
|
583 |
if (operation == NSWorkspaceMoveOperation |
584 |
|| operation == NSWorkspaceCopyOperation |
585 |
|| operation == NSWorkspaceLinkOperation |
586 |
|| operation == NSWorkspaceDuplicateOperation |
587 |
|| operation == NSWorkspaceRecycleOperation |
588 |
|| operation == GWorkspaceRecycleOutOperation) { |
589 |
|
590 |
if ([panel isOnBasePath: destination withFiles: files]) { |
591 |
[panel reloadFromPath: destination]; |
592 |
[self reSetWatchersFromPath: destination]; |
593 |
} |
594 |
} |
595 |
|
596 |
if (operation == NSWorkspaceMoveOperation |
597 |
|| operation == NSWorkspaceDestroyOperation |
598 |
|| operation == NSWorkspaceRecycleOperation |
599 |
|| operation == GWorkspaceRecycleOutOperation |
600 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
601 |
|
602 |
if ([panel isOnBasePath: source withFiles: files]) { |
603 |
[panel reloadFromPath: source]; |
604 |
[self reSetWatchersFromPath: source]; |
605 |
} |
606 |
} |
607 |
|
608 |
if (operation == GWorkspaceRenameOperation) { |
609 |
NSString *dest = [destination stringByDeletingLastPathComponent]; |
610 |
|
611 |
if ([panel isOnBasePath: dest withFiles: nil]) { |
612 |
[panel reloadFromPath: dest]; |
613 |
|
614 |
if ([[self window] isKeyWindow]) { |
615 |
[panel selectIconWithPath: destination]; |
616 |
} |
617 |
|
618 |
[self reSetWatchersFromPath: dest]; |
619 |
} |
620 |
} |
621 |
|
622 |
if (operation == GWorkspaceCreateFileOperation |
623 |
|| operation == GWorkspaceCreateDirOperation) { |
624 |
|
625 |
if ([panel isOnBasePath: destination withFiles: nil]) { |
626 |
[panel reloadFromPath: destination]; |
627 |
|
628 |
if ([[self window] isKeyWindow]) { |
629 |
NSString *fileName = [files objectAtIndex: 0]; |
630 |
NSString *filePath = [destination stringByAppendingPathComponent: fileName]; |
631 |
|
632 |
[self setCurrentSelection: [NSArray arrayWithObject: destination]]; |
633 |
|
634 |
[panel selectIconWithPath: filePath]; |
635 |
} |
636 |
|
637 |
[self reSetWatchersFromPath: destination]; |
638 |
} |
639 |
} |
640 |
|
641 |
[self updateDiskInfo]; |
642 |
[self setNeedsDisplay: YES]; |
643 |
} |
644 |
|
645 |
- (void)sortTypeDidChange:(NSNotification *)notification |
646 |
{ |
647 |
NSString *notifPath = [notification object]; |
648 |
|
649 |
if (notifPath != nil) { |
650 |
if ([currentPath isEqualToString: notifPath]) { |
651 |
[panel makeFileIcons]; |
652 |
[panel resizeWithOldSuperviewSize: [panel frame].size]; |
653 |
} |
654 |
} else { |
655 |
[panel makeFileIcons]; |
656 |
[panel resizeWithOldSuperviewSize: [panel frame].size]; |
657 |
} |
658 |
} |
659 |
|
660 |
- (void)watcherNotification:(NSNotification *)notification |
661 |
{ |
662 |
NSDictionary *notifdict = (NSDictionary *)[notification object]; |
663 |
NSString *path = [notifdict objectForKey: @"path"]; |
664 |
|
665 |
if ([watchedPaths containsObject: path] == NO) { |
666 |
return; |
667 |
|
668 |
} else { |
669 |
NSString *event = [notifdict objectForKey: @"event"]; |
670 |
|
671 |
if (event == GWWatchedDirectoryDeleted) { |
672 |
if ((subPathOfPath(path, currentPath)) || ([path isEqualToString: currentPath])) { |
673 |
[self closeNicely]; |
674 |
return; |
675 |
} else { |
676 |
NSString *s = [path stringByDeletingLastPathComponent]; |
677 |
|
678 |
[self unsetWatcherForPath: path]; |
679 |
|
680 |
if ([panel isOnBasePath: s withFiles: nil]) { |
681 |
[panel reloadFromPath: s]; |
682 |
[self setNeedsDisplay: YES]; |
683 |
} |
684 |
|
685 |
return; |
686 |
} |
687 |
} |
688 |
|
689 |
if (event == GWFileDeletedInWatchedDirectory) { |
690 |
if ([path isEqualToString: [panel currentPath]]) { |
691 |
[panel reloadFromPath: path]; |
692 |
[self setNeedsDisplay: YES]; |
693 |
return; |
694 |
} |
695 |
} |
696 |
|
697 |
if (event == GWFileCreatedInWatchedDirectory) { |
698 |
if ([path isEqualToString: [panel currentPath]]) { |
699 |
[panel addIconsWithNames: [notifdict objectForKey: @"files"] |
700 |
dimmed: NO]; |
701 |
} |
702 |
} |
703 |
} |
704 |
|
705 |
[self updateDiskInfo]; |
706 |
[self setNeedsDisplay: YES]; |
707 |
} |
708 |
|
709 |
- (void)setWatchers |
710 |
{ |
711 |
int i; |
712 |
|
713 |
for (i = 0; i < [watchedPaths count]; i++) { |
714 |
[self setWatcherForPath: [watchedPaths objectAtIndex: i]]; |
715 |
} |
716 |
|
717 |
[[NSNotificationCenter defaultCenter] addObserver: self |
718 |
selector: @selector(watcherNotification:) |
719 |
name: GWFileWatcherFileDidChangeNotification |
720 |
object: nil]; |
721 |
} |
722 |
|
723 |
- (void)setWatcherForPath:(NSString *)path |
724 |
{ |
725 |
[gworkspace addWatcherForPath: path]; |
726 |
} |
727 |
|
728 |
- (void)unsetWatcherForPath:(NSString *)path |
729 |
{ |
730 |
[gworkspace removeWatcherForPath: path]; |
731 |
} |
732 |
|
733 |
- (void)unsetWatchersFromPath:(NSString *)path |
734 |
{ |
735 |
unsigned index = [watchedPaths indexOfObject: path]; |
736 |
|
737 |
if (index != NSNotFound) { |
738 |
int i; |
739 |
|
740 |
for (i = index; i < [watchedPaths count]; i++) { |
741 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: i]]; |
742 |
} |
743 |
} |
744 |
} |
745 |
|
746 |
- (void)reSetWatchersFromPath:(NSString *)path |
747 |
{ |
748 |
unsigned index = [watchedPaths indexOfObject: path]; |
749 |
|
750 |
if (index != NSNotFound) { |
751 |
int i, count; |
752 |
BOOL isdir; |
753 |
|
754 |
count = [watchedPaths count]; |
755 |
|
756 |
for (i = index; i < count; i++) { |
757 |
NSString *wpath = [watchedPaths objectAtIndex: i]; |
758 |
|
759 |
if ([fm fileExistsAtPath: wpath isDirectory: &isdir] && isdir) { |
760 |
[self setWatcherForPath: wpath]; |
761 |
} else { |
762 |
[watchedPaths removeObjectAtIndex: i]; |
763 |
count--; |
764 |
i--; |
765 |
} |
766 |
} |
767 |
} |
768 |
} |
769 |
|
770 |
- (void)openCurrentSelection:(NSArray *)paths newViewer:(BOOL)newv |
771 |
{ |
772 |
if (newv == YES) { |
773 |
[gworkspace openSelectedPaths: paths newViewer: YES]; |
774 |
return; |
775 |
|
776 |
} else { |
777 |
NSMutableArray *allfiles = [NSMutableArray arrayWithCapacity: 1]; |
778 |
NSMutableArray *dirs = [NSMutableArray arrayWithCapacity: 1]; |
779 |
int count = [paths count]; |
780 |
int i; |
781 |
|
782 |
[self setSelectedPaths: paths]; |
783 |
|
784 |
[allfiles addObjectsFromArray: paths]; |
785 |
|
786 |
for (i = 0; i < count; i++) { |
787 |
NSString *fpath = [allfiles objectAtIndex: i]; |
788 |
NSString *defApp = nil; |
789 |
NSString *type = nil; |
790 |
|
791 |
[[NSWorkspace sharedWorkspace] getInfoForFile: fpath |
792 |
application: &defApp |
793 |
type: &type]; |
794 |
|
795 |
if (([type isEqualToString: NSDirectoryFileType]) |
796 |
|| ([type isEqualToString: NSFilesystemFileType])) { |
797 |
if ([gworkspace isPakageAtPath: fpath] == NO) { |
798 |
[dirs addObject: fpath]; |
799 |
[allfiles removeObject: fpath]; |
800 |
count--; |
801 |
i--; |
802 |
} |
803 |
} |
804 |
} |
805 |
|
806 |
if ([allfiles count]) { |
807 |
[gworkspace openSelectedPaths: allfiles newViewer: newv]; |
808 |
} |
809 |
|
810 |
if ([dirs count] == 1) { |
811 |
[self setSelectedPaths: dirs]; |
812 |
[panel setPath: [dirs objectAtIndex: 0]]; |
813 |
[panel scrollFirstIconToVisible]; |
814 |
[panel setNeedsDisplay: YES]; |
815 |
[delegate addPathToHistory: dirs]; |
816 |
} |
817 |
} |
818 |
} |
819 |
|
820 |
- (void)setSelectedIconsPaths:(NSArray *)paths |
821 |
{ |
822 |
if (([paths count] == 0) || ([paths isEqualToArray: selectedPaths])) { |
823 |
return; |
824 |
} |
825 |
|
826 |
ASSIGN (selectedPaths, paths); |
827 |
[delegate setTheSelectedPaths: selectedPaths]; |
828 |
} |
829 |
|
830 |
- (void)makePopUp:(NSArray *)pathComps |
831 |
{ |
832 |
NSArray *titles = [pathsPopUp itemTitles]; |
833 |
int i; |
834 |
|
835 |
if ((titles != nil) && ([titles count] != 0)) { |
836 |
if ([titles isEqualToArray: pathComps]) { |
837 |
return; |
838 |
} else { |
839 |
[pathsPopUp removeAllItems]; |
840 |
} |
841 |
} |
842 |
|
843 |
for (i = [pathComps count] -1; i >= 0; i--) { |
844 |
[pathsPopUp addItemWithTitle: [pathComps objectAtIndex: i]]; |
845 |
} |
846 |
} |
847 |
|
848 |
- (void)popUpAction:(id)sender |
849 |
{ |
850 |
NSArray *titles = [sender itemTitles]; |
851 |
NSString *title = [sender titleOfSelectedItem]; |
852 |
int index = [sender indexOfSelectedItem]; |
853 |
NSString *path = fixPath(@"/", 0); |
854 |
int i = [titles count] -1; |
855 |
|
856 |
while (1) { |
857 |
NSString *s = [titles objectAtIndex: i]; |
858 |
path = [path stringByAppendingPathComponent: s]; |
859 |
if ([s isEqualToString: title] && (i == index)) { |
860 |
break; |
861 |
} |
862 |
i--; |
863 |
} |
864 |
|
865 |
if ([currentPath isEqualToString: path] == NO) { |
866 |
BOOL newview = [pathsPopUp newViewer]; |
867 |
[self openCurrentSelection: [NSArray arrayWithObject: path] |
868 |
newViewer: newview]; |
869 |
if (newview) { |
870 |
[self makePopUp: [currentPath pathComponents]]; |
871 |
} |
872 |
} |
873 |
} |
874 |
|
875 |
- (void)updateDiskInfo |
876 |
{ |
877 |
NSDictionary *attributes; |
878 |
NSNumber *freeFs; |
879 |
NSString *infoString; |
880 |
|
881 |
attributes = [fm fileSystemAttributesAtPath: currentPath]; |
882 |
freeFs = [attributes objectForKey: NSFileSystemFreeSize]; |
883 |
|
884 |
if(freeFs == nil) { |
885 |
infoString = [NSString stringWithString: NSLocalizedString(@"unknown size", @"")]; |
886 |
} else { |
887 |
infoString = [NSString stringWithFormat: @"%@ %@", |
888 |
fileSizeDescription([freeFs unsignedLongLongValue]), |
889 |
NSLocalizedString(@"available", @"")]; |
890 |
} |
891 |
|
892 |
[banner updateInfo: infoString]; |
893 |
} |
894 |
|
895 |
- (void)closeNicely |
896 |
{ |
897 |
NSTimer *t; |
898 |
|
899 |
[self unsetWatchers]; |
900 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
901 |
|
902 |
t = [NSTimer timerWithTimeInterval: 1 target: self |
903 |
selector: @selector(close:) userInfo: nil repeats: NO]; |
904 |
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode]; |
905 |
} |
906 |
|
907 |
- (void)close:(id)sender |
908 |
{ |
909 |
[[self window] performClose: nil]; |
910 |
} |
911 |
|
912 |
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize |
913 |
{ |
914 |
NSRect r = [self frame]; |
915 |
float w = r.size.width; |
916 |
float h = r.size.height; |
917 |
int winwidth = [delegate getWindowFrameWidth]; |
918 |
|
919 |
SETRECT (banner, 0, h - 30, w, 30); |
920 |
[banner resizeWithOldSuperviewSize: [banner frame].size]; |
921 |
|
922 |
SETRECT (panelScroll, 0, 0, w, h - 30); |
923 |
|
924 |
if (firstResize) { |
925 |
NSArray *currSel = [panel currentSelection]; |
926 |
|
927 |
[panel scrollFirstIconToVisible]; |
928 |
|
929 |
if (currSel) { |
930 |
[panel scrollToVisibleIconsWithPaths: currSel]; |
931 |
} |
932 |
|
933 |
firstResize = NO; |
934 |
} |
935 |
|
936 |
columns = (int)winwidth / resizeIncrement; |
937 |
columnsWidth = (winwidth - 16) / columns; |
938 |
} |
939 |
|
940 |
@end |
941 |
|
942 |
// |
943 |
// SmallIconsPanel delegate methods |
944 |
// |
945 |
@implementation SmallIconsViewer (SmallIconsPanelDelegateMethods) |
946 |
|
947 |
- (void)setTheSelectedPaths:(id)paths |
948 |
{ |
949 |
[delegate addPathToHistory: paths]; |
950 |
[self setSelectedPaths: paths]; |
951 |
} |
952 |
|
953 |
- (void)setSelectedPathsFromIcons:(id)paths |
954 |
{ |
955 |
[self setSelectedIconsPaths: paths]; |
956 |
} |
957 |
|
958 |
- (void)openTheCurrentSelection:(id)paths newViewer:(BOOL)newv |
959 |
{ |
960 |
[self openCurrentSelection: paths newViewer: newv]; |
961 |
} |
962 |
|
963 |
- (int)iconCellsWidth |
964 |
{ |
965 |
return [delegate iconCellsWidth]; |
966 |
} |
967 |
|
968 |
@end |
969 |
|