1 |
/* IconsViewer.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 "IconsViewer.h" |
40 |
#include "IconsPath.h" |
41 |
#include "PathIcon.h" |
42 |
#include "IconsPanel.h" |
43 |
#include "GWScrollView.h" |
44 |
#include "IconsViewerPref.h" |
45 |
#include "GNUstep.h" |
46 |
|
47 |
#define ICNWIDTH 64 |
48 |
|
49 |
@implementation IconsViewer |
50 |
|
51 |
- (void)dealloc |
52 |
{ |
53 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
54 |
TEST_RELEASE (rootPath); |
55 |
TEST_RELEASE (lastPath); |
56 |
TEST_RELEASE (selectedPaths); |
57 |
TEST_RELEASE (savedSelection); |
58 |
TEST_RELEASE (watchedPaths); |
59 |
TEST_RELEASE (iconsPath); |
60 |
TEST_RELEASE (pathsScroll); |
61 |
TEST_RELEASE (panelScroll); |
62 |
TEST_RELEASE (panel); |
63 |
TEST_RELEASE (prefs); |
64 |
[super dealloc]; |
65 |
} |
66 |
|
67 |
- (id)init |
68 |
{ |
69 |
self = [super initWithFrame: NSZeroRect]; |
70 |
|
71 |
if (self) { |
72 |
#ifdef GNUSTEP |
73 |
Class gwclass = [[NSBundle mainBundle] principalClass]; |
74 |
#else |
75 |
Class gwclass = [[NSBundle mainBundle] classNamed: @"GWorkspace"]; |
76 |
#endif |
77 |
|
78 |
gworkspace = (id<GWProtocol>)[gwclass gworkspace]; |
79 |
usesShelf = YES; |
80 |
rootPath = nil; |
81 |
lastPath = nil; |
82 |
selectedPaths = nil; |
83 |
watchedPaths = nil; |
84 |
iconsPath = nil; |
85 |
pathsScroll = nil; |
86 |
panelScroll = nil; |
87 |
panel = nil; |
88 |
prefs = nil; |
89 |
} |
90 |
|
91 |
return self; |
92 |
} |
93 |
|
94 |
// |
95 |
// NSCopying |
96 |
// |
97 |
- (id)copyWithZone:(NSZone *)zone |
98 |
{ |
99 |
IconsViewer *vwr = [[IconsViewer 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 |
[self checkUsesShelf]; |
116 |
[self setDelegate: adelegate]; |
117 |
ASSIGN (rootPath, rpath); |
118 |
viewsapps = canview; |
119 |
[self setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)]; |
120 |
|
121 |
colswidth = [delegate browserColumnsWidth]; |
122 |
resizeIncrement = colswidth; |
123 |
winwidth = [delegate getWindowFrameWidth]; |
124 |
columns = (int)winwidth / resizeIncrement; |
125 |
columnsWidth = (winwidth - 16) / columns; |
126 |
autoSynchronize = YES; |
127 |
|
128 |
TEST_RELEASE (iconsPath); |
129 |
|
130 |
if (pathsScroll != nil) { |
131 |
[pathsScroll removeFromSuperview]; |
132 |
RELEASE (pathsScroll); |
133 |
} |
134 |
|
135 |
pathsScroll = [[GWScrollView alloc] init]; |
136 |
[pathsScroll setBorderType: NSBezelBorder]; |
137 |
[pathsScroll setHasHorizontalScroller: YES]; |
138 |
[pathsScroll setHasVerticalScroller: NO]; |
139 |
[pathsScroll setLineScroll: columnsWidth]; |
140 |
[pathsScroll setAutoresizingMask: NSViewWidthSizable]; |
141 |
[pathsScroll setDelegate: self]; |
142 |
|
143 |
iconsPath = [[IconsPath alloc] initWithRootAtPath: rootPath |
144 |
columnsWidth: columnsWidth delegate: self]; |
145 |
|
146 |
[pathsScroll setDocumentView: iconsPath]; |
147 |
[self addSubview: pathsScroll]; |
148 |
|
149 |
TEST_RELEASE (panel); |
150 |
|
151 |
if (panelScroll != nil) { |
152 |
[panelScroll removeFromSuperview]; |
153 |
RELEASE (panelScroll); |
154 |
} |
155 |
|
156 |
panelScroll = [NSScrollView new]; |
157 |
[panelScroll setBorderType: NSBezelBorder]; |
158 |
[panelScroll setHasHorizontalScroller: YES]; |
159 |
[panelScroll setHasVerticalScroller: YES]; |
160 |
[panelScroll setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)]; |
161 |
[self addSubview: panelScroll]; |
162 |
|
163 |
panel = [[IconsPanel alloc] initAtPath: rootPath delegate: self]; |
164 |
[panelScroll setDocumentView: panel]; |
165 |
|
166 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
167 |
|
168 |
[[NSNotificationCenter defaultCenter] addObserver: self |
169 |
selector: @selector(fileSystemWillChange:) |
170 |
name: GWFileSystemWillChangeNotification |
171 |
object: nil]; |
172 |
|
173 |
[[NSNotificationCenter defaultCenter] addObserver: self |
174 |
selector: @selector(fileSystemDidChange:) |
175 |
name: GWFileSystemDidChangeNotification |
176 |
object: nil]; |
177 |
|
178 |
[[NSNotificationCenter defaultCenter] addObserver: self |
179 |
selector: @selector(sortTypeDidChange:) |
180 |
name: GWSortTypeDidChangeNotification |
181 |
object: nil]; |
182 |
|
183 |
if (watchedPaths != nil) { |
184 |
[self unsetWatchers]; |
185 |
DESTROY (watchedPaths); |
186 |
} |
187 |
|
188 |
[self setSelectedPaths: [NSArray arrayWithObject: rootPath]]; |
189 |
|
190 |
if (vpath) { |
191 |
[self setSelectedPaths: [NSArray arrayWithObject: vpath]]; |
192 |
[panel setPath: vpath]; |
193 |
[panel scrollFirstIconToVisible]; |
194 |
|
195 |
if (selection && [selection count]) { |
196 |
[panel selectIconsWithPaths: selection]; |
197 |
} |
198 |
[delegate addPathToHistory: [NSArray arrayWithObject: vpath]]; |
199 |
} else { |
200 |
[delegate addPathToHistory: [NSArray arrayWithObject: rootPath]]; |
201 |
} |
202 |
|
203 |
[delegate updateTheInfoString]; |
204 |
|
205 |
firstResize = YES; |
206 |
} |
207 |
|
208 |
- (NSString *)menuName |
209 |
{ |
210 |
return @"Icon"; |
211 |
} |
212 |
|
213 |
- (NSString *)shortCut |
214 |
{ |
215 |
return @"i"; |
216 |
} |
217 |
|
218 |
- (BOOL)usesShelf |
219 |
{ |
220 |
return usesShelf; |
221 |
} |
222 |
|
223 |
- (NSSize)resizeIncrements |
224 |
{ |
225 |
return NSZeroSize; |
226 |
} |
227 |
|
228 |
- (NSImage *)miniicon |
229 |
{ |
230 |
NSBundle *bundle = [NSBundle bundleForClass: [self class]]; |
231 |
NSString *imgpath = [bundle pathForResource: @"miniwindow" ofType: @"tiff"]; |
232 |
NSImage *img = [[NSImage alloc] initWithContentsOfFile: imgpath]; |
233 |
return AUTORELEASE (img); |
234 |
} |
235 |
|
236 |
- (BOOL)hasPreferences |
237 |
{ |
238 |
return YES; |
239 |
} |
240 |
|
241 |
- (id)prefController |
242 |
{ |
243 |
if (prefs == nil) { |
244 |
prefs = [[IconsViewerPref alloc] init]; |
245 |
} |
246 |
|
247 |
return prefs; |
248 |
} |
249 |
|
250 |
- (void)setSelectedPaths:(NSArray *)paths |
251 |
{ |
252 |
NSString *newPath; |
253 |
NSArray *components; |
254 |
NSMutableArray *wpaths; |
255 |
NSString *s; |
256 |
BOOL isDir; |
257 |
int i, j; |
258 |
|
259 |
if (([paths count] == 0) |
260 |
|| ([paths isEqualToArray: selectedPaths])) { |
261 |
return; |
262 |
} |
263 |
|
264 |
ASSIGN (selectedPaths, paths); |
265 |
[delegate setTheSelectedPaths: paths]; |
266 |
|
267 |
[self synchronize]; |
268 |
|
269 |
newPath = [paths objectAtIndex: 0]; |
270 |
[fm fileExistsAtPath: newPath isDirectory: &isDir]; |
271 |
if ((isDir == NO) || ([paths count] > 1)) { |
272 |
newPath = [newPath stringByDeletingLastPathComponent]; |
273 |
} else { |
274 |
if (([gworkspace isPakageAtPath: newPath]) && (viewsapps == NO)) { |
275 |
newPath = [newPath stringByDeletingLastPathComponent]; |
276 |
} |
277 |
} |
278 |
|
279 |
if (lastPath && [lastPath isEqual: newPath]) { |
280 |
return; |
281 |
} else { |
282 |
ASSIGN (lastPath, newPath); |
283 |
} |
284 |
|
285 |
components = [newPath pathComponents]; |
286 |
wpaths = [NSMutableArray arrayWithCapacity: 1]; |
287 |
s = [NSString string]; |
288 |
|
289 |
for (i = 0; i < [components count]; i++) { |
290 |
s = [s stringByAppendingPathComponent: [components objectAtIndex: i]]; |
291 |
[wpaths addObject: s]; |
292 |
} |
293 |
|
294 |
if (watchedPaths == nil) { |
295 |
watchedPaths = [wpaths mutableCopy]; |
296 |
[self setWatchers]; |
297 |
|
298 |
} else { |
299 |
int count = [wpaths count]; |
300 |
|
301 |
for (i = 0; i < [watchedPaths count]; i++) { |
302 |
NSString *s1, *s2; |
303 |
|
304 |
s1 = [watchedPaths objectAtIndex: i]; |
305 |
|
306 |
if (count > i) { |
307 |
s2 = [wpaths objectAtIndex: i]; |
308 |
} else { |
309 |
i = count; |
310 |
break; |
311 |
} |
312 |
|
313 |
if ([s1 isEqualToString: s2] == NO) { |
314 |
break; |
315 |
} |
316 |
} |
317 |
|
318 |
for (j = i; j < [watchedPaths count]; j++) { |
319 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: j]]; |
320 |
} |
321 |
|
322 |
for (j = i; j < [wpaths count]; j++) { |
323 |
[self setWatcherForPath: [wpaths objectAtIndex: j]]; |
324 |
} |
325 |
|
326 |
TEST_RELEASE (watchedPaths); |
327 |
watchedPaths = [wpaths mutableCopy]; |
328 |
} |
329 |
} |
330 |
|
331 |
- (void)setCurrentSelection:(NSArray *)paths |
332 |
{ |
333 |
NSString *path; |
334 |
NSArray *selection; |
335 |
|
336 |
if ([paths count] == 0) { |
337 |
return; |
338 |
} |
339 |
|
340 |
[self setSelectedPaths: paths]; |
341 |
|
342 |
path = [paths objectAtIndex: 0]; |
343 |
|
344 |
if ([paths count] > 1) { |
345 |
path = [path stringByDeletingLastPathComponent]; |
346 |
selection = [NSArray arrayWithArray: selectedPaths]; |
347 |
} else { |
348 |
BOOL isdir; |
349 |
|
350 |
[fm fileExistsAtPath: path isDirectory: &isdir]; |
351 |
|
352 |
if (isdir == NO) { |
353 |
path = [path stringByDeletingLastPathComponent]; |
354 |
selection = [NSArray arrayWithArray: selectedPaths]; |
355 |
} else { |
356 |
if ([gworkspace isPakageAtPath: path] && (viewsapps == NO)) { |
357 |
path = [path stringByDeletingLastPathComponent]; |
358 |
selection = [NSArray arrayWithArray: selectedPaths]; |
359 |
} else { |
360 |
selection = [NSArray array]; |
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
[panel setPath: path]; |
366 |
[panel scrollFirstIconToVisible]; |
367 |
[panel selectIconsWithPaths: selection]; |
368 |
[panel setNeedsDisplay: YES]; |
369 |
[delegate updateTheInfoString]; |
370 |
} |
371 |
|
372 |
- (NSPoint)positionForSlidedImage |
373 |
{ |
374 |
NSPoint p = [iconsPath positionForSlidedImage]; |
375 |
p.y += [pathsScroll frame].origin.y; |
376 |
return [[self window] convertBaseToScreen: p]; |
377 |
} |
378 |
|
379 |
- (void)selectAll |
380 |
{ |
381 |
[panel selectAllIcons]; |
382 |
} |
383 |
|
384 |
- (NSArray *)selectedPaths |
385 |
{ |
386 |
return selectedPaths; |
387 |
} |
388 |
|
389 |
- (NSString *)currentViewedPath |
390 |
{ |
391 |
return [panel currentPath]; |
392 |
} |
393 |
|
394 |
- (NSPoint)locationOfIconForPath:(NSString *)path |
395 |
{ |
396 |
if ([selectedPaths containsObject: path]) { |
397 |
NSPoint p = [iconsPath positionOfLastIcon]; |
398 |
|
399 |
if (p.x > ([self frame].size.width - ICNWIDTH)) { |
400 |
return NSZeroPoint; |
401 |
} |
402 |
|
403 |
p.y += [pathsScroll frame].origin.y; |
404 |
return [self convertPoint: p toView: nil]; |
405 |
} |
406 |
|
407 |
return NSZeroPoint; |
408 |
} |
409 |
|
410 |
- (void)unsetWatchers |
411 |
{ |
412 |
int i; |
413 |
|
414 |
[[NSNotificationCenter defaultCenter] removeObserver: self |
415 |
name: GWFileWatcherFileDidChangeNotification object: nil]; |
416 |
|
417 |
for (i = 0; i < [watchedPaths count]; i++) { |
418 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: i]]; |
419 |
} |
420 |
} |
421 |
|
422 |
- (void)setResizeIncrement:(int)increment |
423 |
{ |
424 |
int winwidth = [delegate getWindowFrameWidth]; |
425 |
|
426 |
resizeIncrement = [delegate browserColumnsWidth]; |
427 |
columns = (int)winwidth / resizeIncrement; |
428 |
columnsWidth = (winwidth - 16) / columns; |
429 |
|
430 |
[pathsScroll setLineScroll: columnsWidth]; |
431 |
[iconsPath setColumnWidth: columnsWidth]; |
432 |
} |
433 |
|
434 |
- (void)setAutoSynchronize:(BOOL)value |
435 |
{ |
436 |
autoSynchronize = value; |
437 |
} |
438 |
|
439 |
- (void)thumbnailsDidChangeInPaths:(NSArray *)paths |
440 |
{ |
441 |
if (paths == nil) { |
442 |
[panel makeFileIcons]; |
443 |
[panel resizeWithOldSuperviewSize: [panel frame].size]; |
444 |
[[iconsPath lastIcon] renewIcon]; |
445 |
} else { |
446 |
int i; |
447 |
|
448 |
for (i = 0; i < [paths count]; i++) { |
449 |
NSString *dir = [paths objectAtIndex: i]; |
450 |
|
451 |
if ([panel isOnBasePath: dir withFiles: nil]) { |
452 |
[panel reloadFromPath: dir]; |
453 |
[[iconsPath lastIcon] renewIcon]; |
454 |
[self setNeedsDisplay: YES]; |
455 |
} |
456 |
} |
457 |
} |
458 |
} |
459 |
|
460 |
- (id)viewerView |
461 |
{ |
462 |
return panel; |
463 |
} |
464 |
|
465 |
- (BOOL)viewsApps |
466 |
{ |
467 |
return viewsapps; |
468 |
} |
469 |
|
470 |
- (id)delegate |
471 |
{ |
472 |
return delegate; |
473 |
} |
474 |
|
475 |
- (void)setDelegate:(id)anObject |
476 |
{ |
477 |
delegate = anObject; |
478 |
} |
479 |
|
480 |
// |
481 |
// End of ViewersProtocol |
482 |
// |
483 |
|
484 |
- (void)checkUsesShelf |
485 |
{ |
486 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
487 |
usesShelf = ![defaults boolForKey: @"viewersDontUsesShelf"]; |
488 |
} |
489 |
|
490 |
- (void)validateRootPathAfterOperation:(NSDictionary *)opdict |
491 |
{ |
492 |
if ([rootPath isEqualToString: fixPath(@"/", 0)]) { |
493 |
return; |
494 |
} else { |
495 |
NSString *operation = [opdict objectForKey: @"operation"]; |
496 |
NSString *source = [opdict objectForKey: @"source"]; |
497 |
NSArray *files = [opdict objectForKey: @"files"]; |
498 |
int i; |
499 |
|
500 |
if (operation == NSWorkspaceMoveOperation |
501 |
|| operation == NSWorkspaceDestroyOperation |
502 |
|| operation == GWorkspaceRenameOperation |
503 |
|| operation == NSWorkspaceRecycleOperation |
504 |
|| operation == GWorkspaceRecycleOutOperation |
505 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
506 |
|
507 |
if (operation == GWorkspaceRenameOperation) { |
508 |
files = [NSArray arrayWithObject: [source lastPathComponent]]; |
509 |
source = [source stringByDeletingLastPathComponent]; |
510 |
} |
511 |
|
512 |
for (i = 0; i < [files count]; i++) { |
513 |
NSString *fpath = [source stringByAppendingPathComponent: [files objectAtIndex: i]]; |
514 |
|
515 |
if ((subPathOfPath(fpath, rootPath) == YES) |
516 |
|| ([fpath isEqualToString: rootPath] == YES)) { |
517 |
[self closeNicely]; |
518 |
break; |
519 |
} |
520 |
} |
521 |
} |
522 |
} |
523 |
} |
524 |
|
525 |
- (void)fileSystemWillChange:(NSNotification *)notification |
526 |
{ |
527 |
NSDictionary *dict = (NSDictionary *)[notification object]; |
528 |
NSString *operation = [dict objectForKey: @"operation"]; |
529 |
NSString *source = [dict objectForKey: @"source"]; |
530 |
NSString *destination = [dict objectForKey: @"destination"]; |
531 |
NSArray *files = [dict objectForKey: @"files"]; |
532 |
|
533 |
[self validateRootPathAfterOperation: dict]; |
534 |
|
535 |
[delegate startIndicatorForOperation: operation]; |
536 |
|
537 |
if (operation == NSWorkspaceMoveOperation |
538 |
|| operation == NSWorkspaceCopyOperation |
539 |
|| operation == NSWorkspaceLinkOperation |
540 |
|| operation == NSWorkspaceDuplicateOperation |
541 |
|| operation == NSWorkspaceRecycleOperation |
542 |
|| operation == GWorkspaceRecycleOutOperation) { |
543 |
|
544 |
if ([panel isOnBasePath: destination withFiles: files]) { |
545 |
[self unsetWatchersFromPath: destination]; |
546 |
|
547 |
[panel extendSelectionWithDimmedFiles: files |
548 |
startingAtPath: destination]; |
549 |
|
550 |
[iconsPath lockIconsFromPath: destination]; |
551 |
} |
552 |
} |
553 |
|
554 |
if (operation == GWorkspaceRenameOperation) { |
555 |
NSString *dest = [destination stringByDeletingLastPathComponent]; |
556 |
|
557 |
if ([panel isOnBasePath: dest withFiles: nil]) { |
558 |
[self unsetWatchersFromPath: dest]; |
559 |
} |
560 |
} |
561 |
|
562 |
if (operation == GWorkspaceCreateFileOperation |
563 |
|| operation == GWorkspaceCreateDirOperation) { |
564 |
if ([panel isOnBasePath: destination withFiles: nil]) { |
565 |
[self unsetWatchersFromPath: destination]; |
566 |
} |
567 |
} |
568 |
|
569 |
if (operation == NSWorkspaceMoveOperation |
570 |
|| operation == NSWorkspaceDestroyOperation |
571 |
|| operation == NSWorkspaceRecycleOperation |
572 |
|| operation == GWorkspaceRecycleOutOperation |
573 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
574 |
|
575 |
if ([panel isOnBasePath: source withFiles: files]) { |
576 |
[self unsetWatchersFromPath: source]; |
577 |
|
578 |
[panel extendSelectionWithDimmedFiles: files |
579 |
startingAtPath: source]; |
580 |
|
581 |
[iconsPath lockIconsFromPath: source]; |
582 |
} |
583 |
} |
584 |
} |
585 |
|
586 |
- (void)fileSystemDidChange:(NSNotification *)notification |
587 |
{ |
588 |
NSMutableDictionary *dict; |
589 |
NSString *operation, *source, *destination; |
590 |
NSArray *files; |
591 |
|
592 |
dict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
593 |
[dict addEntriesFromDictionary: (NSDictionary *)[notification object]]; |
594 |
|
595 |
operation = [dict objectForKey: @"operation"]; |
596 |
source = [dict objectForKey: @"source"]; |
597 |
destination = [dict objectForKey: @"destination"]; |
598 |
files = [dict objectForKey: @"files"]; |
599 |
|
600 |
[delegate stopIndicatorForOperation: operation]; |
601 |
|
602 |
if (operation == NSWorkspaceMoveOperation |
603 |
|| operation == NSWorkspaceCopyOperation |
604 |
|| operation == NSWorkspaceLinkOperation |
605 |
|| operation == NSWorkspaceDuplicateOperation |
606 |
|| operation == NSWorkspaceRecycleOperation |
607 |
|| operation == GWorkspaceRecycleOutOperation) { |
608 |
|
609 |
if ([panel isOnBasePath: destination withFiles: files]) { |
610 |
[iconsPath unlockIconsFromPath: destination]; |
611 |
[panel reloadFromPath: destination]; |
612 |
[self reSetWatchersFromPath: destination]; |
613 |
} |
614 |
} |
615 |
|
616 |
if (operation == NSWorkspaceMoveOperation |
617 |
|| operation == NSWorkspaceDestroyOperation |
618 |
|| operation == NSWorkspaceRecycleOperation |
619 |
|| operation == GWorkspaceRecycleOutOperation |
620 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
621 |
|
622 |
if ([panel isOnBasePath: source withFiles: files]) { |
623 |
[iconsPath unlockIconsFromPath: source]; |
624 |
[panel reloadFromPath: source]; |
625 |
[self reSetWatchersFromPath: source]; |
626 |
} |
627 |
} |
628 |
|
629 |
if (operation == GWorkspaceRenameOperation) { |
630 |
NSString *dest = [destination stringByDeletingLastPathComponent]; |
631 |
|
632 |
if ([panel isOnBasePath: dest withFiles: nil]) { |
633 |
[panel reloadFromPath: dest]; |
634 |
|
635 |
if ([[self window] isKeyWindow]) { |
636 |
[panel selectIconWithPath: destination]; |
637 |
} |
638 |
|
639 |
[self reSetWatchersFromPath: dest]; |
640 |
} |
641 |
} |
642 |
|
643 |
if (operation == GWorkspaceCreateFileOperation |
644 |
|| operation == GWorkspaceCreateDirOperation) { |
645 |
|
646 |
if ([panel isOnBasePath: destination withFiles: nil]) { |
647 |
[panel reloadFromPath: destination]; |
648 |
|
649 |
if ([[self window] isKeyWindow]) { |
650 |
NSString *fileName = [files objectAtIndex: 0]; |
651 |
NSString *filePath = [destination stringByAppendingPathComponent: fileName]; |
652 |
|
653 |
[self setCurrentSelection: [NSArray arrayWithObject: destination]]; |
654 |
|
655 |
[panel selectIconWithPath: filePath]; |
656 |
[self synchronize]; |
657 |
} |
658 |
|
659 |
[self reSetWatchersFromPath: destination]; |
660 |
} |
661 |
} |
662 |
|
663 |
[delegate updateTheInfoString]; |
664 |
[self setNeedsDisplay: YES]; |
665 |
} |
666 |
|
667 |
- (void)sortTypeDidChange:(NSNotification *)notification |
668 |
{ |
669 |
NSString *notifPath = [notification object]; |
670 |
|
671 |
if (notifPath != nil) { |
672 |
if ([[panel currentPath] isEqualToString: notifPath]) { |
673 |
[panel makeFileIcons]; |
674 |
} |
675 |
} else { |
676 |
[panel makeFileIcons]; |
677 |
} |
678 |
[panel resizeWithOldSuperviewSize: [panel frame].size]; |
679 |
} |
680 |
|
681 |
- (void)watcherNotification:(NSNotification *)notification |
682 |
{ |
683 |
NSDictionary *notifdict = (NSDictionary *)[notification object]; |
684 |
NSString *path = [notifdict objectForKey: @"path"]; |
685 |
|
686 |
if ([watchedPaths containsObject: path] == NO) { |
687 |
return; |
688 |
|
689 |
} else { |
690 |
NSString *event = [notifdict objectForKey: @"event"]; |
691 |
|
692 |
if (event == GWWatchedDirectoryDeleted) { |
693 |
if ((subPathOfPath(path, rootPath)) || ([path isEqualToString: rootPath])) { |
694 |
[self closeNicely]; |
695 |
return; |
696 |
} else { |
697 |
NSString *s = [path stringByDeletingLastPathComponent]; |
698 |
|
699 |
[self unsetWatcherForPath: path]; |
700 |
|
701 |
if ([panel isOnBasePath: s withFiles: nil]) { |
702 |
[panel reloadFromPath: s]; |
703 |
[self setNeedsDisplay: YES]; |
704 |
} |
705 |
|
706 |
return; |
707 |
} |
708 |
} |
709 |
|
710 |
if (event == GWFileDeletedInWatchedDirectory) { |
711 |
if ([path isEqualToString: [panel currentPath]]) { |
712 |
[panel reloadFromPath: path]; |
713 |
[self setNeedsDisplay: YES]; |
714 |
return; |
715 |
} |
716 |
} |
717 |
|
718 |
if (event == GWFileCreatedInWatchedDirectory) { |
719 |
if ([path isEqualToString: [panel currentPath]]) { |
720 |
[panel addIconsWithNames: [notifdict objectForKey: @"files"] |
721 |
dimmed: NO]; |
722 |
} |
723 |
} |
724 |
} |
725 |
|
726 |
[delegate updateTheInfoString]; |
727 |
[self setNeedsDisplay: YES]; |
728 |
} |
729 |
|
730 |
- (void)setWatchers |
731 |
{ |
732 |
int i; |
733 |
|
734 |
for (i = 0; i < [watchedPaths count]; i++) { |
735 |
[self setWatcherForPath: [watchedPaths objectAtIndex: i]]; |
736 |
} |
737 |
|
738 |
[[NSNotificationCenter defaultCenter] addObserver: self |
739 |
selector: @selector(watcherNotification:) |
740 |
name: GWFileWatcherFileDidChangeNotification |
741 |
object: nil]; |
742 |
} |
743 |
|
744 |
- (void)setWatcherForPath:(NSString *)path |
745 |
{ |
746 |
[gworkspace addWatcherForPath: path]; |
747 |
} |
748 |
|
749 |
- (void)unsetWatcherForPath:(NSString *)path |
750 |
{ |
751 |
[gworkspace removeWatcherForPath: path]; |
752 |
} |
753 |
|
754 |
- (void)unsetWatchersFromPath:(NSString *)path |
755 |
{ |
756 |
unsigned index = [watchedPaths indexOfObject: path]; |
757 |
|
758 |
if (index != NSNotFound) { |
759 |
int i; |
760 |
|
761 |
for (i = index; i < [watchedPaths count]; i++) { |
762 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: i]]; |
763 |
} |
764 |
} |
765 |
} |
766 |
|
767 |
- (void)reSetWatchersFromPath:(NSString *)path |
768 |
{ |
769 |
unsigned index = [watchedPaths indexOfObject: path]; |
770 |
|
771 |
if (index != NSNotFound) { |
772 |
int i, count; |
773 |
BOOL isdir; |
774 |
|
775 |
count = [watchedPaths count]; |
776 |
|
777 |
for (i = index; i < count; i++) { |
778 |
NSString *wpath = [watchedPaths objectAtIndex: i]; |
779 |
|
780 |
if ([fm fileExistsAtPath: wpath isDirectory: &isdir] && isdir) { |
781 |
[self setWatcherForPath: wpath]; |
782 |
} else { |
783 |
[watchedPaths removeObjectAtIndex: i]; |
784 |
count--; |
785 |
i--; |
786 |
} |
787 |
} |
788 |
} |
789 |
} |
790 |
|
791 |
- (void)closeNicely |
792 |
{ |
793 |
NSTimer *t; |
794 |
|
795 |
[self unsetWatchers]; |
796 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
797 |
|
798 |
t = [NSTimer timerWithTimeInterval: 2 target: self |
799 |
selector: @selector(close:) userInfo: nil repeats: NO]; |
800 |
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode]; |
801 |
} |
802 |
|
803 |
- (void)close:(id)sender |
804 |
{ |
805 |
[[self window] performClose: nil]; |
806 |
} |
807 |
|
808 |
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize |
809 |
{ |
810 |
NSRect r = [self frame]; |
811 |
int col = columns; |
812 |
int winwidth; |
813 |
|
814 |
[pathsScroll setFrame: NSMakeRect(0, r.size.height - 98, r.size.width, 98)]; |
815 |
[panelScroll setFrame: NSMakeRect(0, 0, r.size.width, r.size.height - 104)]; |
816 |
|
817 |
if (firstResize) { |
818 |
NSArray *currSel = [panel currentSelection]; |
819 |
|
820 |
[panel scrollFirstIconToVisible]; |
821 |
|
822 |
if (currSel) { |
823 |
[panel scrollToVisibleIconsWithPaths: currSel]; |
824 |
} |
825 |
|
826 |
firstResize = NO; |
827 |
} |
828 |
|
829 |
winwidth = [delegate getWindowFrameWidth]; |
830 |
columns = (int)winwidth / resizeIncrement; |
831 |
columnsWidth = (winwidth - 16) / columns; |
832 |
|
833 |
if (col != columns) { |
834 |
[pathsScroll setLineScroll: columnsWidth]; |
835 |
[iconsPath setColumnWidth: columnsWidth]; |
836 |
} |
837 |
|
838 |
[self synchronize]; |
839 |
} |
840 |
|
841 |
- (void)clickOnIcon:(PathIcon *)icon |
842 |
{ |
843 |
NSArray *ipaths = RETAIN ([icon paths]); |
844 |
NSString *type = [icon type]; |
845 |
BOOL chdir = NO; |
846 |
|
847 |
if (type && ((type == NSDirectoryFileType) || (type == NSFilesystemFileType))) { |
848 |
if (([icon isPakage] == NO) || ([icon isPakage] && viewsapps)) { |
849 |
chdir = YES; |
850 |
} |
851 |
} |
852 |
|
853 |
if (chdir && (icon == [iconsPath lastIcon])) { |
854 |
[self setSelectedPaths: ipaths]; |
855 |
[panel scrollToVisibleIconsWithPaths: ipaths]; |
856 |
[panel selectIconsWithPaths: ipaths]; |
857 |
|
858 |
} else { |
859 |
[self setCurrentSelection: ipaths]; |
860 |
|
861 |
if (chdir) { |
862 |
[panel scrollFirstIconToVisible]; |
863 |
} else { |
864 |
[panel scrollToVisibleIconsWithPaths: ipaths]; |
865 |
[panel selectIconsWithPaths: ipaths]; |
866 |
} |
867 |
} |
868 |
|
869 |
RELEASE (ipaths); |
870 |
} |
871 |
|
872 |
- (void)doubleClickOnIcon:(PathIcon *)icon newViewer:(BOOL)isnew |
873 |
{ |
874 |
[self clickOnIcon: icon]; |
875 |
[self openCurrentSelection: [icon paths] newViewer: isnew]; |
876 |
} |
877 |
|
878 |
- (void)synchronize |
879 |
{ |
880 |
NSClipView *clip; |
881 |
float x, y; |
882 |
int nicons; |
883 |
|
884 |
if (autoSynchronize == NO) { |
885 |
return; |
886 |
} |
887 |
|
888 |
[iconsPath setIconsForSelection: selectedPaths]; |
889 |
nicons = [iconsPath numberOfIcons]; |
890 |
clip = [pathsScroll contentView]; |
891 |
x = [clip bounds].origin.x; |
892 |
y = [clip bounds].origin.y; |
893 |
|
894 |
if (nicons > columns) { |
895 |
x += columnsWidth * (nicons - columns); |
896 |
[clip scrollToPoint: NSMakePoint(x, y)]; |
897 |
} |
898 |
} |
899 |
|
900 |
- (void)openCurrentSelection:(NSArray *)paths newViewer:(BOOL)newv |
901 |
{ |
902 |
[self setSelectedPaths: paths]; |
903 |
|
904 |
if (newv == YES) { |
905 |
[gworkspace openSelectedPaths: paths newViewer: YES]; |
906 |
return; |
907 |
|
908 |
} else { |
909 |
NSMutableArray *allfiles = [NSMutableArray arrayWithCapacity: 1]; |
910 |
NSMutableArray *dirs = [NSMutableArray arrayWithCapacity: 1]; |
911 |
int count = [paths count]; |
912 |
int i; |
913 |
|
914 |
[allfiles addObjectsFromArray: paths]; |
915 |
|
916 |
for (i = 0; i < count; i++) { |
917 |
NSString *fpath = [allfiles objectAtIndex: i]; |
918 |
NSString *defApp = nil; |
919 |
NSString *type = nil; |
920 |
|
921 |
[[NSWorkspace sharedWorkspace] getInfoForFile: fpath |
922 |
application: &defApp |
923 |
type: &type]; |
924 |
|
925 |
if (([type isEqualToString: NSDirectoryFileType]) |
926 |
|| ([type isEqualToString: NSFilesystemFileType])) { |
927 |
if ([gworkspace isPakageAtPath: fpath] == NO) { |
928 |
[dirs addObject: fpath]; |
929 |
[allfiles removeObject: fpath]; |
930 |
count--; |
931 |
i--; |
932 |
} |
933 |
} |
934 |
} |
935 |
|
936 |
if ([allfiles count]) { |
937 |
[gworkspace openSelectedPaths: allfiles newViewer: newv]; |
938 |
} |
939 |
|
940 |
if ([dirs count] == 1) { |
941 |
[self setSelectedPaths: dirs]; |
942 |
[panel setPath: [dirs objectAtIndex: 0]]; |
943 |
[panel scrollFirstIconToVisible]; |
944 |
[panel setNeedsDisplay: YES]; |
945 |
} |
946 |
} |
947 |
} |
948 |
|
949 |
// |
950 |
// scrollview delegate methods |
951 |
// |
952 |
- (void)gwscrollView:(GWScrollView *)sender |
953 |
scrollViewScrolled:(NSClipView *)clip |
954 |
hitPart:(NSScrollerPart)hitpart |
955 |
{ |
956 |
if (autoSynchronize == NO) { |
957 |
return; |
958 |
} else { |
959 |
int x = (int)[clip bounds].origin.x; |
960 |
int y = (int)[clip bounds].origin.y; |
961 |
int rem = x % (int)columnsWidth; |
962 |
|
963 |
if (rem != 0) { |
964 |
if (rem <= columnsWidth / 2) { |
965 |
x -= rem; |
966 |
} else { |
967 |
x += columnsWidth - rem; |
968 |
} |
969 |
|
970 |
[clip scrollToPoint: NSMakePoint(x, y)]; |
971 |
[iconsPath setNeedsDisplay: YES]; |
972 |
} |
973 |
} |
974 |
} |
975 |
|
976 |
@end |
977 |
|
978 |
// |
979 |
// IconsPanel delegate methods |
980 |
// |
981 |
@implementation IconsViewer (IconsPanelDelegateMethods) |
982 |
|
983 |
- (void)setTheSelectedPaths:(id)paths |
984 |
{ |
985 |
[delegate addPathToHistory: paths]; |
986 |
[self setSelectedPaths: paths]; |
987 |
} |
988 |
|
989 |
- (void)openTheCurrentSelection:(id)paths newViewer:(BOOL)newv |
990 |
{ |
991 |
[self openCurrentSelection: paths newViewer: newv]; |
992 |
} |
993 |
|
994 |
- (int)iconCellsWidth |
995 |
{ |
996 |
return [delegate iconCellsWidth]; |
997 |
} |
998 |
|
999 |
@end |
1000 |
|
1001 |
// |
1002 |
// IconsPath delegate methods |
1003 |
// |
1004 |
|
1005 |
@implementation IconsViewer (IconsPathDelegateMethods) |
1006 |
|
1007 |
- (void)clickedIcon:(id)anicon |
1008 |
{ |
1009 |
[self clickOnIcon: anicon]; |
1010 |
} |
1011 |
|
1012 |
- (void)doubleClickedIcon:(id)anicon newViewer:(BOOL)isnew |
1013 |
{ |
1014 |
[self doubleClickOnIcon: anicon newViewer: isnew]; |
1015 |
} |
1016 |
|
1017 |
@end |