1 |
/* GWLib.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 |
#include <Foundation/Foundation.h> |
26 |
#include <AppKit/AppKit.h> |
27 |
#include <math.h> |
28 |
#include "GWLib.h" |
29 |
#include "GWFunctions.h" |
30 |
#include "GWNotifications.h" |
31 |
#include "FSWatcher.h" |
32 |
#include "GWProtocol.h" |
33 |
#include "GNUstep.h" |
34 |
#ifndef GNUSTEP |
35 |
#include "OSXCompatibility.h" |
36 |
#endif |
37 |
|
38 |
#define CHECKGW \ |
39 |
if (gwapp == nil) \ |
40 |
gwapp = (id <GWProtocol>)[[GWLib class] gworkspaceApplication]; \ |
41 |
if (gwapp == nil) return |
42 |
|
43 |
#define CHECKGW_RET(x) \ |
44 |
if (gwapp == nil) \ |
45 |
gwapp = (id <GWProtocol>)[[GWLib class] gworkspaceApplication]; \ |
46 |
if (gwapp == nil) return x |
47 |
|
48 |
#ifndef CACHED_MAX |
49 |
#define CACHED_MAX 20 |
50 |
#endif |
51 |
|
52 |
#ifndef byname |
53 |
#define byname 0 |
54 |
#define bykind 1 |
55 |
#define bydate 2 |
56 |
#define bysize 3 |
57 |
#define byowner 4 |
58 |
#endif |
59 |
|
60 |
id instance = nil; |
61 |
static id gwapp = nil; |
62 |
static NSString *gwName = @"GWorkspace"; |
63 |
|
64 |
@interface GWLib (PrivateMethods) |
65 |
|
66 |
+ (GWLib *)instance; |
67 |
|
68 |
- (NSArray *)sortedDirectoryContentsAtPath:(NSString *)path; |
69 |
|
70 |
- (NSArray *)checkHiddenFiles:(NSArray *)files atPath:(NSString *)path; |
71 |
|
72 |
- (NSMutableDictionary *)cachedRepresentationForPath:(NSString *)path; |
73 |
|
74 |
- (void)addCachedRepresentation:(NSDictionary *)contentsDict |
75 |
ofDirectory:(NSString *)path; |
76 |
|
77 |
- (void)removeCachedRepresentationForPath:(NSString *)path; |
78 |
|
79 |
- (void)removeOlderCache; |
80 |
|
81 |
- (void)clearCache; |
82 |
|
83 |
- (void)setCachedMax:(int)cmax; |
84 |
|
85 |
- (void)addWatcherForPath:(NSString *)path; |
86 |
|
87 |
- (void)removeWatcherForPath:(NSString *)path; |
88 |
|
89 |
- (void)watcherTimeOut:(id)sender; |
90 |
|
91 |
- (void)removeWatcher:(FSWatcher *)awatcher; |
92 |
|
93 |
- (FSWatcher *)watcherForPath:(NSString *)path; |
94 |
|
95 |
- (NSTimer *)timerForPath:(NSString *)path; |
96 |
|
97 |
- (void)watcherNotification:(NSNotification *)notification; |
98 |
|
99 |
- (void)lockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path; |
100 |
|
101 |
- (void)unLockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path; |
102 |
|
103 |
- (BOOL)isLockedPath:(NSString *)path; |
104 |
|
105 |
- (BOOL)existsAndIsDirectoryFileAtPath:(NSString *)path; |
106 |
|
107 |
- (NSString *)typeOfFileAt:(NSString *)path; |
108 |
|
109 |
- (BOOL)isWritableFileAtPath:(NSString *)path; |
110 |
|
111 |
- (BOOL)isPakageAtPath:(NSString *)path; |
112 |
|
113 |
- (int)sortTypeForDirectoryAtPath:(NSString *)path; |
114 |
|
115 |
- (void)setSortType:(int)type forDirectoryAtPath:(NSString *)path; |
116 |
|
117 |
- (void)setDefSortType:(int)type; |
118 |
|
119 |
- (int)defSortType; |
120 |
|
121 |
- (void)setHideSysFiles:(BOOL)value; |
122 |
|
123 |
- (BOOL)hideSysFiles; |
124 |
|
125 |
- (void)setHideDotFiles:(NSNotification *)notif; |
126 |
|
127 |
- (NSImage *)iconForFile:(NSString *)fullPath ofType:(NSString *)type; |
128 |
|
129 |
- (NSImage *)smallIconForFile:(NSString*)aPath; |
130 |
|
131 |
- (NSImage *)smallIconForFiles:(NSArray*)pathArray; |
132 |
|
133 |
- (NSImage *)smallHighlightIcon; |
134 |
|
135 |
- (NSImage *)thumbnailForPath:(NSString *)path; |
136 |
|
137 |
- (void)prepareThumbnailsCache; |
138 |
|
139 |
- (void)thumbnailsDidChange:(NSNotification *)notif; |
140 |
|
141 |
- (void)setUseThumbnails:(BOOL)value; |
142 |
|
143 |
- (NSArray *)imageExtensions; |
144 |
|
145 |
@end |
146 |
|
147 |
@implementation GWLib (PrivateMethods) |
148 |
|
149 |
+ (GWLib *)instance |
150 |
{ |
151 |
if (instance == nil) { |
152 |
instance = [[GWLib alloc] init]; |
153 |
} |
154 |
return instance; |
155 |
} |
156 |
|
157 |
- (void)dealloc |
158 |
{ |
159 |
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self]; |
160 |
[nc removeObserver: self]; |
161 |
|
162 |
RELEASE (cachedContents); |
163 |
RELEASE (watchers); |
164 |
RELEASE (watchTimers); |
165 |
RELEASE (watchedPaths); |
166 |
RELEASE (lockedPaths); |
167 |
RELEASE (tumbsCache); |
168 |
RELEASE (thumbnailDir); |
169 |
|
170 |
[super dealloc]; |
171 |
} |
172 |
|
173 |
- (id)init |
174 |
{ |
175 |
self = [super init]; |
176 |
|
177 |
if (self) { |
178 |
BOOL isdir; |
179 |
|
180 |
fm = [NSFileManager defaultManager]; |
181 |
ws = [NSWorkspace sharedWorkspace]; |
182 |
nc = [NSNotificationCenter defaultCenter]; |
183 |
|
184 |
cachedContents = [NSMutableDictionary new]; |
185 |
cachedMax = CACHED_MAX; |
186 |
defSortType = byname; |
187 |
hideSysFiles = NO; |
188 |
|
189 |
watchers = [NSMutableArray new]; |
190 |
watchTimers = [NSMutableArray new]; |
191 |
watchedPaths = [NSMutableArray new]; |
192 |
|
193 |
lockedPaths = [NSMutableArray new]; |
194 |
|
195 |
tumbsCache = [NSMutableDictionary new]; |
196 |
thumbnailDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; |
197 |
thumbnailDir = [thumbnailDir stringByAppendingPathComponent: @"Thumbnails"]; |
198 |
RETAIN (thumbnailDir); |
199 |
if (([fm fileExistsAtPath: thumbnailDir isDirectory: &isdir] && isdir) == NO) { |
200 |
[fm createDirectoryAtPath: thumbnailDir attributes: nil]; |
201 |
} |
202 |
usesThumbnails = NO; |
203 |
|
204 |
[nc addObserver: self |
205 |
selector: @selector(watcherNotification:) |
206 |
name: GWFileWatcherFileDidChangeNotification |
207 |
object: nil]; |
208 |
|
209 |
[[NSDistributedNotificationCenter defaultCenter] addObserver: self |
210 |
selector: @selector(setHideDotFiles:) |
211 |
name: GSHideDotFilesDidChangeNotification |
212 |
object: nil]; |
213 |
|
214 |
[[NSDistributedNotificationCenter defaultCenter] addObserver: self |
215 |
selector: @selector(thumbnailsDidChange:) |
216 |
name: GWThumbnailsDidChangeNotification |
217 |
object: nil]; |
218 |
} |
219 |
|
220 |
return self; |
221 |
} |
222 |
|
223 |
- (NSArray *)sortedDirectoryContentsAtPath:(NSString *)path |
224 |
{ |
225 |
NSMutableDictionary *contentsDict = [self cachedRepresentationForPath: path]; |
226 |
|
227 |
if (contentsDict) { |
228 |
return [contentsDict objectForKey: @"files"]; |
229 |
|
230 |
} else { |
231 |
NSArray *files = [fm directoryContentsAtPath: path]; |
232 |
int stype = [self sortTypeForDirectoryAtPath: path]; |
233 |
int count = [files count]; |
234 |
NSMutableArray *paths = [NSMutableArray arrayWithCapacity: count]; |
235 |
NSMutableArray *sortfiles = [NSMutableArray arrayWithCapacity: count]; |
236 |
NSArray *sortPaths = nil; |
237 |
NSDictionary *attributes = nil; |
238 |
NSDate *date = nil; |
239 |
SEL appendPathCompSel = @selector(stringByAppendingPathComponent:); |
240 |
IMP appendPathComp = [[NSString class] instanceMethodForSelector: appendPathCompSel]; |
241 |
SEL lastPathCompSel = @selector(lastPathComponent); |
242 |
IMP lastPathComp = [[NSString class] instanceMethodForSelector: lastPathCompSel]; |
243 |
int i; |
244 |
|
245 |
for (i = 0; i < count; i++) { |
246 |
NSString *s = (*appendPathComp)(path, appendPathCompSel, [files objectAtIndex: i]); |
247 |
[paths addObject: s]; |
248 |
} |
249 |
|
250 |
sortPaths = [paths sortedArrayUsingFunction: (int (*)(id, id, void*))comparePaths |
251 |
context: (void *)stype]; |
252 |
|
253 |
for (i = 0; i < count; i++) { |
254 |
NSString *s = (*lastPathComp)([sortPaths objectAtIndex: i], lastPathCompSel); |
255 |
[sortfiles addObject: s]; |
256 |
} |
257 |
|
258 |
contentsDict = [NSMutableDictionary dictionary]; |
259 |
[contentsDict setObject: [NSDate date] forKey: @"datestamp"]; |
260 |
attributes = [fm fileAttributesAtPath: path traverseLink: YES]; |
261 |
date = [attributes fileModificationDate]; |
262 |
[contentsDict setObject: date forKey: @"moddate"]; |
263 |
[contentsDict setObject: sortfiles forKey: @"files"]; |
264 |
|
265 |
if ([cachedContents count] >= cachedMax) { |
266 |
[self removeOlderCache]; |
267 |
} |
268 |
|
269 |
[self addCachedRepresentation: contentsDict ofDirectory: path]; |
270 |
|
271 |
return sortfiles; |
272 |
} |
273 |
|
274 |
return nil; |
275 |
} |
276 |
|
277 |
- (NSArray *)checkHiddenFiles:(NSArray *)files atPath:(NSString *)path |
278 |
{ |
279 |
NSArray *checkedFiles; |
280 |
NSArray *hiddenFiles; |
281 |
NSString *h; |
282 |
|
283 |
h = [path stringByAppendingPathComponent: @".hidden"]; |
284 |
if ([fm fileExistsAtPath: h]) { |
285 |
h = [NSString stringWithContentsOfFile: h]; |
286 |
hiddenFiles = [h componentsSeparatedByString: @"\n"]; |
287 |
} else { |
288 |
hiddenFiles = nil; |
289 |
} |
290 |
|
291 |
if (hiddenFiles != nil || hideSysFiles) { |
292 |
NSMutableArray *mutableFiles = AUTORELEASE ([files mutableCopy]); |
293 |
|
294 |
if (hiddenFiles != nil) { |
295 |
[mutableFiles removeObjectsInArray: hiddenFiles]; |
296 |
} |
297 |
|
298 |
if (hideSysFiles) { |
299 |
int j = [mutableFiles count] - 1; |
300 |
|
301 |
while (j >= 0) { |
302 |
NSString *file = (NSString *)[mutableFiles objectAtIndex: j]; |
303 |
|
304 |
if ([file hasPrefix: @"."]) { |
305 |
[mutableFiles removeObjectAtIndex: j]; |
306 |
} |
307 |
j--; |
308 |
} |
309 |
} |
310 |
|
311 |
checkedFiles = mutableFiles; |
312 |
|
313 |
} else { |
314 |
checkedFiles = files; |
315 |
} |
316 |
|
317 |
return checkedFiles; |
318 |
} |
319 |
|
320 |
- (NSMutableDictionary *)cachedRepresentationForPath:(NSString *)path |
321 |
{ |
322 |
NSMutableDictionary *contents = [cachedContents objectForKey: path]; |
323 |
|
324 |
if (contents) { |
325 |
NSDate *modDate = [contents objectForKey: @"moddate"]; |
326 |
NSDictionary *attributes = [fm fileAttributesAtPath: path |
327 |
traverseLink: YES]; |
328 |
NSDate *date = [attributes fileModificationDate]; |
329 |
|
330 |
if ([modDate isEqualToDate: date]) { |
331 |
return contents; |
332 |
} else { |
333 |
[cachedContents removeObjectForKey: path]; |
334 |
} |
335 |
} |
336 |
|
337 |
return nil; |
338 |
} |
339 |
|
340 |
- (void)addCachedRepresentation:(NSDictionary *)contentsDict |
341 |
ofDirectory:(NSString *)path |
342 |
{ |
343 |
[cachedContents setObject: contentsDict forKey: path]; |
344 |
|
345 |
if ([watchedPaths containsObject: path] == NO) { |
346 |
[watchedPaths addObject: path]; |
347 |
[self addWatcherForPath: path]; |
348 |
} |
349 |
} |
350 |
|
351 |
- (void)removeCachedRepresentationForPath:(NSString *)path |
352 |
{ |
353 |
[cachedContents removeObjectForKey: path]; |
354 |
|
355 |
if ([watchedPaths containsObject: path]) { |
356 |
[watchedPaths removeObject: path]; |
357 |
[self removeWatcherForPath: path]; |
358 |
} |
359 |
} |
360 |
|
361 |
- (void)removeOlderCache |
362 |
{ |
363 |
NSArray *keys = [cachedContents allKeys]; |
364 |
NSDate *date = [NSDate date]; |
365 |
NSString *removeKey = nil; |
366 |
int i; |
367 |
|
368 |
if ([keys count]) { |
369 |
for (i = 0; i < [keys count]; i++) { |
370 |
NSString *key = [keys objectAtIndex: i]; |
371 |
NSDate *stamp = [[cachedContents objectForKey: key] objectForKey: @"datestamp"]; |
372 |
NSDate *d = [date earlierDate: stamp]; |
373 |
|
374 |
if ([date isEqualToDate: d] == NO) { |
375 |
date = d; |
376 |
removeKey = key; |
377 |
} |
378 |
} |
379 |
|
380 |
if (removeKey == nil) { |
381 |
removeKey = [keys objectAtIndex: 0]; |
382 |
} |
383 |
|
384 |
[cachedContents removeObjectForKey: removeKey]; |
385 |
|
386 |
if ([watchedPaths containsObject: removeKey]) { |
387 |
[watchedPaths removeObject: removeKey]; |
388 |
[self removeWatcherForPath: removeKey]; |
389 |
} |
390 |
} |
391 |
} |
392 |
|
393 |
- (void)clearCache |
394 |
{ |
395 |
NSArray *keys = [cachedContents allKeys]; |
396 |
int i; |
397 |
|
398 |
for (i = 0; i < [keys count]; i++) { |
399 |
[self removeWatcherForPath: [keys objectAtIndex: i]]; |
400 |
} |
401 |
|
402 |
DESTROY (cachedContents); |
403 |
cachedContents = [NSMutableDictionary new]; |
404 |
} |
405 |
|
406 |
- (void)setCachedMax:(int)cmax |
407 |
{ |
408 |
cachedMax = cmax; |
409 |
} |
410 |
|
411 |
- (void)addWatcherForPath:(NSString *)path |
412 |
{ |
413 |
FSWatcher *watcher = [self watcherForPath: path]; |
414 |
|
415 |
if ((watcher != nil) && ([watcher isOld] == NO)) { |
416 |
[watcher addListener]; |
417 |
return; |
418 |
} else { |
419 |
BOOL isdir; |
420 |
|
421 |
if ([fm fileExistsAtPath: path isDirectory: &isdir] && isdir) { |
422 |
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 |
423 |
target: self selector: @selector(watcherTimeOut:) |
424 |
userInfo: path repeats: YES]; |
425 |
[watchTimers addObject: timer]; |
426 |
|
427 |
watcher = [[FSWatcher alloc] initForWatchAtPath: path]; |
428 |
[watchers addObject: watcher]; |
429 |
RELEASE (watcher); |
430 |
} |
431 |
} |
432 |
} |
433 |
|
434 |
- (void)removeWatcherForPath:(NSString *)path |
435 |
{ |
436 |
FSWatcher *watcher = [self watcherForPath: path]; |
437 |
|
438 |
if ((watcher != nil) && ([watcher isOld] == NO)) { |
439 |
[watcher removeListener]; |
440 |
} |
441 |
} |
442 |
|
443 |
- (void)watcherTimeOut:(id)sender |
444 |
{ |
445 |
NSString *watchedPath = (NSString *)[sender userInfo]; |
446 |
|
447 |
if (watchedPath != nil) { |
448 |
FSWatcher *watcher = [self watcherForPath: watchedPath]; |
449 |
|
450 |
if (watcher != nil) { |
451 |
if ([watcher isOld]) { |
452 |
[self removeWatcher: watcher]; |
453 |
} else { |
454 |
[watcher watchFile]; |
455 |
} |
456 |
} |
457 |
} |
458 |
} |
459 |
|
460 |
- (void)removeWatcher:(FSWatcher *)awatcher |
461 |
{ |
462 |
NSString *watchedPath = [awatcher watchedPath]; |
463 |
NSTimer *timer = [self timerForPath: watchedPath]; |
464 |
|
465 |
if (timer && [timer isValid]) { |
466 |
[timer invalidate]; |
467 |
[watchTimers removeObject: timer]; |
468 |
} |
469 |
|
470 |
[watchers removeObject: awatcher]; |
471 |
} |
472 |
|
473 |
- (FSWatcher *)watcherForPath:(NSString *)path |
474 |
{ |
475 |
int i; |
476 |
|
477 |
for (i = 0; i < [watchers count]; i++) { |
478 |
FSWatcher *watcher = [watchers objectAtIndex: i]; |
479 |
if ([watcher isWathcingPath: path]) { |
480 |
return watcher; |
481 |
} |
482 |
} |
483 |
|
484 |
return nil; |
485 |
} |
486 |
|
487 |
- (NSTimer *)timerForPath:(NSString *)path |
488 |
{ |
489 |
int i; |
490 |
|
491 |
for (i = 0; i < [watchTimers count]; i++) { |
492 |
NSTimer *t = [watchTimers objectAtIndex: i]; |
493 |
|
494 |
if (([t isValid]) && ([(NSString *)[t userInfo] isEqual: path])) { |
495 |
return t; |
496 |
} |
497 |
} |
498 |
|
499 |
return nil; |
500 |
} |
501 |
|
502 |
- (void)watcherNotification:(NSNotification *)notification |
503 |
{ |
504 |
NSDictionary *notifdict = (NSDictionary *)[notification object]; |
505 |
NSString *path = [notifdict objectForKey: @"path"]; |
506 |
|
507 |
if ([self cachedRepresentationForPath: path]) { |
508 |
[self removeCachedRepresentationForPath: path]; |
509 |
} |
510 |
} |
511 |
|
512 |
- (void)lockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path |
513 |
{ |
514 |
int i; |
515 |
|
516 |
for (i = 0; i < [files count]; i++) { |
517 |
NSString *file = [files objectAtIndex: i]; |
518 |
NSString *fpath = [path stringByAppendingPathComponent: file]; |
519 |
|
520 |
if ([lockedPaths containsObject: fpath] == NO) { |
521 |
[lockedPaths addObject: fpath]; |
522 |
} |
523 |
} |
524 |
} |
525 |
|
526 |
- (void)unLockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path |
527 |
{ |
528 |
int i; |
529 |
|
530 |
for (i = 0; i < [files count]; i++) { |
531 |
NSString *file = [files objectAtIndex: i]; |
532 |
NSString *fpath = [path stringByAppendingPathComponent: file]; |
533 |
|
534 |
if ([lockedPaths containsObject: fpath]) { |
535 |
[lockedPaths removeObject: fpath]; |
536 |
} |
537 |
} |
538 |
} |
539 |
|
540 |
- (BOOL)isLockedPath:(NSString *)path |
541 |
{ |
542 |
int i; |
543 |
|
544 |
if ([lockedPaths containsObject: path]) { |
545 |
return YES; |
546 |
} |
547 |
|
548 |
for (i = 0; i < [lockedPaths count]; i++) { |
549 |
NSString *lpath = [lockedPaths objectAtIndex: i]; |
550 |
|
551 |
if (subPathOfPath(lpath, path)) { |
552 |
return YES; |
553 |
} |
554 |
} |
555 |
|
556 |
return NO; |
557 |
} |
558 |
|
559 |
- (BOOL)existsAndIsDirectoryFileAtPath:(NSString *)path |
560 |
{ |
561 |
BOOL isDir; |
562 |
return ([fm fileExistsAtPath: path isDirectory: &isDir] && isDir); |
563 |
} |
564 |
|
565 |
- (NSString *)typeOfFileAt:(NSString *)path |
566 |
{ |
567 |
NSString *defApp, *type; |
568 |
[ws getInfoForFile: path application: &defApp type: &type]; |
569 |
return type; |
570 |
} |
571 |
|
572 |
- (BOOL)isWritableFileAtPath:(NSString *)path |
573 |
{ |
574 |
return [fm isWritableFileAtPath: path]; |
575 |
} |
576 |
|
577 |
- (BOOL)isPakageAtPath:(NSString *)path |
578 |
{ |
579 |
NSString *defApp, *type; |
580 |
BOOL isdir; |
581 |
|
582 |
[ws getInfoForFile: path application: &defApp type: &type]; |
583 |
|
584 |
if (type == NSApplicationFileType) { |
585 |
return YES; |
586 |
} else if (type == NSPlainFileType) { |
587 |
if ((([fm fileExistsAtPath: path isDirectory: &isdir]) && isdir)) { |
588 |
return YES; |
589 |
} |
590 |
} |
591 |
|
592 |
return NO; |
593 |
} |
594 |
|
595 |
- (int)sortTypeForDirectoryAtPath:(NSString *)path |
596 |
{ |
597 |
if ([fm isWritableFileAtPath: path]) { |
598 |
NSString *dictPath = [path stringByAppendingPathComponent: @".gwsort"]; |
599 |
|
600 |
if ([fm fileExistsAtPath: dictPath]) { |
601 |
NSDictionary *sortDict = [NSDictionary dictionaryWithContentsOfFile: dictPath]; |
602 |
|
603 |
if (sortDict) { |
604 |
return [[sortDict objectForKey: @"sort"] intValue]; |
605 |
} |
606 |
} |
607 |
} |
608 |
|
609 |
return defSortType; |
610 |
} |
611 |
|
612 |
- (void)setSortType:(int)type forDirectoryAtPath:(NSString *)path |
613 |
{ |
614 |
if ([fm isWritableFileAtPath: path]) { |
615 |
NSString *sortstr = [NSString stringWithFormat: @"%i", type]; |
616 |
NSDictionary *dict = [NSDictionary dictionaryWithObject: sortstr |
617 |
forKey: @"sort"]; |
618 |
[dict writeToFile: [path stringByAppendingPathComponent: @".gwsort"] |
619 |
atomically: YES]; |
620 |
} |
621 |
|
622 |
[self removeCachedRepresentationForPath: path]; |
623 |
|
624 |
[[NSNotificationCenter defaultCenter] |
625 |
postNotificationName: GWSortTypeDidChangeNotification |
626 |
object: (id)path]; |
627 |
} |
628 |
|
629 |
- (void)setDefSortType:(int)type |
630 |
{ |
631 |
if (defSortType == type) { |
632 |
return; |
633 |
} else { |
634 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
635 |
defSortType = type; |
636 |
[defaults setObject: [NSString stringWithFormat: @"%i", defSortType] |
637 |
forKey: @"defaultsorttype"]; |
638 |
[defaults synchronize]; |
639 |
|
640 |
[[NSNotificationCenter defaultCenter] |
641 |
postNotificationName: GWSortTypeDidChangeNotification |
642 |
object: nil]; |
643 |
} |
644 |
} |
645 |
|
646 |
- (int)defSortType |
647 |
{ |
648 |
return defSortType; |
649 |
} |
650 |
|
651 |
- (void)setHideSysFiles:(BOOL)value |
652 |
{ |
653 |
if (hideSysFiles != value) { |
654 |
[self clearCache]; |
655 |
|
656 |
hideSysFiles = value; |
657 |
} |
658 |
} |
659 |
|
660 |
- (BOOL)hideSysFiles |
661 |
{ |
662 |
return hideSysFiles; |
663 |
} |
664 |
|
665 |
- (void)setHideDotFiles:(NSNotification *)notif |
666 |
{ |
667 |
NSString *hideStr = (NSString *)[notif object]; |
668 |
BOOL hideDot = (BOOL)[hideStr intValue]; |
669 |
|
670 |
if (hideSysFiles != hideDot) { |
671 |
[self clearCache]; |
672 |
|
673 |
hideSysFiles = hideDot; |
674 |
|
675 |
[[NSNotificationCenter defaultCenter] |
676 |
postNotificationName: GWSortTypeDidChangeNotification |
677 |
object: nil]; |
678 |
} |
679 |
} |
680 |
|
681 |
- (NSImage *)iconForFile:(NSString *)fullPath ofType:(NSString *)type |
682 |
{ |
683 |
NSImage *icon; |
684 |
NSSize size; |
685 |
|
686 |
if (usesThumbnails) { |
687 |
icon = [self thumbnailForPath: fullPath]; |
688 |
|
689 |
if (icon) { |
690 |
return icon; |
691 |
} |
692 |
} |
693 |
|
694 |
icon = [ws iconForFile: fullPath]; |
695 |
size = [icon size]; |
696 |
|
697 |
if ((size.width > ICNMAX) || (size.height > ICNMAX)) { |
698 |
NSSize newsize; |
699 |
|
700 |
if (size.width >= size.height) { |
701 |
newsize.width = ICNMAX; |
702 |
newsize.height = floor(ICNMAX * size.height / size.width + 0.5); |
703 |
} else { |
704 |
newsize.height = ICNMAX; |
705 |
newsize.width = floor(ICNMAX * size.width / size.height + 0.5); |
706 |
} |
707 |
|
708 |
[icon setScalesWhenResized: YES]; |
709 |
[icon setSize: newsize]; |
710 |
} |
711 |
|
712 |
return icon; |
713 |
} |
714 |
|
715 |
- (NSImage *)smallIconForFile:(NSString*)aPath |
716 |
{ |
717 |
NSImage *icon = [[self iconForFile: aPath ofType: nil] copy]; |
718 |
NSSize size = [icon size]; |
719 |
[icon setScalesWhenResized: YES]; |
720 |
[icon setSize: NSMakeSize(size.width / 2, size.height / 2)]; |
721 |
|
722 |
return AUTORELEASE (icon); |
723 |
} |
724 |
|
725 |
- (NSImage *)smallIconForFiles:(NSArray*)pathArray |
726 |
{ |
727 |
NSImage *icon = [NSImage imageNamed: @"MultipleSelection.tiff"]; |
728 |
NSSize size = [icon size]; |
729 |
[icon setScalesWhenResized: YES]; |
730 |
[icon setSize: NSMakeSize(size.width / 2, size.height / 2)]; |
731 |
|
732 |
return icon; |
733 |
} |
734 |
|
735 |
- (NSImage *)smallHighlightIcon |
736 |
{ |
737 |
return [NSImage imageNamed: @"SmallCellHighlightSmall.tiff"]; |
738 |
} |
739 |
|
740 |
- (NSImage *)thumbnailForPath:(NSString *)path |
741 |
{ |
742 |
if (usesThumbnails == NO) { |
743 |
return nil; |
744 |
} else { |
745 |
return [tumbsCache objectForKey: path]; |
746 |
} |
747 |
|
748 |
return nil; |
749 |
} |
750 |
|
751 |
- (void)prepareThumbnailsCache |
752 |
{ |
753 |
NSString *dictName = @"thumbnails.plist"; |
754 |
NSString *dictPath = [thumbnailDir stringByAppendingPathComponent: dictName]; |
755 |
NSDictionary *tdict; |
756 |
|
757 |
TEST_RELEASE (tumbsCache); |
758 |
tumbsCache = [NSMutableDictionary new]; |
759 |
|
760 |
tdict = [NSDictionary dictionaryWithContentsOfFile: dictPath]; |
761 |
|
762 |
if (tdict) { |
763 |
NSArray *keys = [tdict allKeys]; |
764 |
int i; |
765 |
|
766 |
for (i = 0; i < [keys count]; i++) { |
767 |
NSString *key = [keys objectAtIndex: i]; |
768 |
NSString *tumbname = [tdict objectForKey: key]; |
769 |
NSString *tumbpath = [thumbnailDir stringByAppendingPathComponent: tumbname]; |
770 |
|
771 |
if ([fm fileExistsAtPath: tumbpath]) { |
772 |
NSImage *tumb = [[NSImage alloc] initWithContentsOfFile: tumbpath]; |
773 |
|
774 |
if (tumb) { |
775 |
[tumbsCache setObject: tumb forKey: key]; |
776 |
RELEASE (tumb); |
777 |
} |
778 |
} |
779 |
} |
780 |
} |
781 |
} |
782 |
|
783 |
- (void)thumbnailsDidChange:(NSNotification *)notif |
784 |
{ |
785 |
NSDictionary *info = [notif userInfo]; |
786 |
NSArray *deleted = [info objectForKey: @"deleted"]; |
787 |
NSArray *created = [info objectForKey: @"created"]; |
788 |
int i; |
789 |
|
790 |
if (usesThumbnails == NO) { |
791 |
return; |
792 |
} |
793 |
|
794 |
if ([deleted count]) { |
795 |
for (i = 0; i < [deleted count]; i++) { |
796 |
NSString *path = [deleted objectAtIndex: i]; |
797 |
|
798 |
[tumbsCache removeObjectForKey: path]; |
799 |
} |
800 |
} |
801 |
|
802 |
if ([created count]) { |
803 |
NSString *dictName = @"thumbnails.plist"; |
804 |
NSString *dictPath = [thumbnailDir stringByAppendingPathComponent: dictName]; |
805 |
NSDictionary *tdict = [NSDictionary dictionaryWithContentsOfFile: dictPath]; |
806 |
|
807 |
for (i = 0; i < [created count]; i++) { |
808 |
NSString *key = [created objectAtIndex: i]; |
809 |
NSString *tumbname = [tdict objectForKey: key]; |
810 |
NSString *tumbpath = [thumbnailDir stringByAppendingPathComponent: tumbname]; |
811 |
|
812 |
if ([fm fileExistsAtPath: tumbpath]) { |
813 |
NSImage *tumb = [[NSImage alloc] initWithContentsOfFile: tumbpath]; |
814 |
|
815 |
if (tumb) { |
816 |
[tumbsCache setObject: tumb forKey: key]; |
817 |
RELEASE (tumb); |
818 |
} |
819 |
} |
820 |
} |
821 |
} |
822 |
} |
823 |
|
824 |
- (void)setUseThumbnails:(BOOL)value |
825 |
{ |
826 |
if (usesThumbnails == value) { |
827 |
return; |
828 |
} |
829 |
|
830 |
usesThumbnails = value; |
831 |
if (usesThumbnails) { |
832 |
[self prepareThumbnailsCache]; |
833 |
} |
834 |
} |
835 |
|
836 |
- (NSArray *)imageExtensions |
837 |
{ |
838 |
return [NSArray arrayWithObjects: @"tiff", @"tif", @"TIFF", @"TIF", |
839 |
@"png", @"PNG", @"jpeg", @"jpg", |
840 |
@"JPEG", @"JPG", @"gif", @"GIF", |
841 |
@"xpm", nil]; |
842 |
} |
843 |
|
844 |
@end |
845 |
|
846 |
|
847 |
@implementation GWLib |
848 |
|
849 |
+ (NSArray *)sortedDirectoryContentsAtPath:(NSString *)path |
850 |
{ |
851 |
return [[self instance] sortedDirectoryContentsAtPath: path]; |
852 |
} |
853 |
|
854 |
+ (NSArray *)checkHiddenFiles:(NSArray *)files atPath:(NSString *)path |
855 |
{ |
856 |
return [[self instance] checkHiddenFiles: files atPath: path]; |
857 |
} |
858 |
|
859 |
+ (void)setCachedMax:(int)cmax |
860 |
{ |
861 |
[[self instance] setCachedMax: cmax]; |
862 |
} |
863 |
|
864 |
+ (void)addWatcherForPath:(NSString *)path |
865 |
{ |
866 |
[[self instance] addWatcherForPath: path]; |
867 |
} |
868 |
|
869 |
+ (void)removeWatcherForPath:(NSString *)path |
870 |
{ |
871 |
[[self instance] removeWatcherForPath: path]; |
872 |
} |
873 |
|
874 |
+ (void)lockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path |
875 |
{ |
876 |
[[self instance] lockFiles: files inDirectoryAtPath: path]; |
877 |
} |
878 |
|
879 |
+ (void)unLockFiles:(NSArray *)files inDirectoryAtPath:(NSString *)path |
880 |
{ |
881 |
[[self instance] unLockFiles: files inDirectoryAtPath: path]; |
882 |
} |
883 |
|
884 |
+ (BOOL)isLockedPath:(NSString *)path |
885 |
{ |
886 |
return [[self instance] isLockedPath: path]; |
887 |
} |
888 |
|
889 |
+ (BOOL)existsAndIsDirectoryFileAtPath:(NSString *)path |
890 |
{ |
891 |
return [[self instance] existsAndIsDirectoryFileAtPath: path]; |
892 |
} |
893 |
|
894 |
+ (NSString *)typeOfFileAt:(NSString *)path |
895 |
{ |
896 |
return [[self instance] typeOfFileAt: path]; |
897 |
} |
898 |
|
899 |
+ (BOOL)isPakageAtPath:(NSString *)path |
900 |
{ |
901 |
return [[self instance] isPakageAtPath: path]; |
902 |
} |
903 |
|
904 |
+ (int)sortTypeForDirectoryAtPath:(NSString *)path |
905 |
{ |
906 |
return [[self instance] sortTypeForDirectoryAtPath: path]; |
907 |
} |
908 |
|
909 |
+ (void)setSortType:(int)type forDirectoryAtPath:(NSString *)path |
910 |
{ |
911 |
[[self instance] setSortType: type forDirectoryAtPath: path]; |
912 |
} |
913 |
|
914 |
+ (void)setDefSortType:(int)type |
915 |
{ |
916 |
[[self instance] setDefSortType: type]; |
917 |
} |
918 |
|
919 |
+ (int)defSortType |
920 |
{ |
921 |
return [[self instance] defSortType]; |
922 |
} |
923 |
|
924 |
+ (void)setHideSysFiles:(BOOL)value |
925 |
{ |
926 |
[[self instance] setHideSysFiles: value]; |
927 |
} |
928 |
|
929 |
+ (BOOL)hideSysFiles |
930 |
{ |
931 |
return [[self instance] hideSysFiles]; |
932 |
} |
933 |
|
934 |
+ (NSImage *)iconForFile:(NSString *)fullPath ofType:(NSString *)type |
935 |
{ |
936 |
return [[self instance] iconForFile: fullPath ofType: type]; |
937 |
} |
938 |
|
939 |
+ (NSImage *)smallIconForFile:(NSString*)aPath |
940 |
{ |
941 |
return [[self instance] smallIconForFile: aPath]; |
942 |
} |
943 |
|
944 |
+ (NSImage *)smallIconForFiles:(NSArray*)pathArray |
945 |
{ |
946 |
return [[self instance] smallIconForFiles: pathArray]; |
947 |
} |
948 |
|
949 |
+ (NSImage *)smallHighlightIcon |
950 |
{ |
951 |
return [[self instance] smallHighlightIcon]; |
952 |
} |
953 |
|
954 |
+ (void)setUseThumbnails:(BOOL)value |
955 |
{ |
956 |
[[self instance] setUseThumbnails: value]; |
957 |
} |
958 |
|
959 |
+ (NSArray *)imageExtensions |
960 |
{ |
961 |
return [[self instance] imageExtensions]; |
962 |
} |
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
+ (id)gworkspaceApplication |
979 |
{ |
980 |
if (gwapp == nil) { |
981 |
NSString *host; |
982 |
NSString *port; |
983 |
NSDate *when = nil; |
984 |
BOOL done = NO; |
985 |
|
986 |
while (done == NO) { |
987 |
host = [[NSUserDefaults standardUserDefaults] stringForKey: @"NSHost"]; |
988 |
|
989 |
if (host == nil) { |
990 |
host = @""; |
991 |
} else { |
992 |
NSHost *h = [NSHost hostWithName: host]; |
993 |
|
994 |
if ([h isEqual: [NSHost currentHost]]) { |
995 |
host = @""; |
996 |
} |
997 |
} |
998 |
|
999 |
port = gwName; |
1000 |
|
1001 |
NS_DURING |
1002 |
{ |
1003 |
gwapp = (id <GWProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName: port host: host]; |
1004 |
RETAIN (gwapp); |
1005 |
} |
1006 |
NS_HANDLER |
1007 |
{ |
1008 |
gwapp = nil; |
1009 |
} |
1010 |
NS_ENDHANDLER |
1011 |
|
1012 |
if (gwapp) { |
1013 |
done = YES; |
1014 |
} |
1015 |
|
1016 |
if (gwapp == nil) { |
1017 |
[[NSWorkspace sharedWorkspace] launchApplication: gwName]; |
1018 |
|
1019 |
if (when == nil) { |
1020 |
when = [[NSDate alloc] init]; |
1021 |
done = NO; |
1022 |
} else if ([when timeIntervalSinceNow] > 5.0) { |
1023 |
int result; |
1024 |
|
1025 |
DESTROY (when); |
1026 |
result = NSRunAlertPanel(gwName, |
1027 |
@"Application seems to have hung", |
1028 |
@"Continue", @"Terminate", @"Wait"); |
1029 |
|
1030 |
if (result == NSAlertDefaultReturn) { |
1031 |
done = YES; |
1032 |
} else if (result == NSAlertOtherReturn) { |
1033 |
done = NO; |
1034 |
} else { |
1035 |
done = YES; |
1036 |
} |
1037 |
} |
1038 |
|
1039 |
if (done == NO) { |
1040 |
NSDate *limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 0.5]; |
1041 |
[[NSRunLoop currentRunLoop] runUntilDate: limit]; |
1042 |
RELEASE(limit); |
1043 |
} |
1044 |
} |
1045 |
} |
1046 |
|
1047 |
TEST_RELEASE (when); |
1048 |
} |
1049 |
|
1050 |
return gwapp; |
1051 |
} |
1052 |
|
1053 |
+ (BOOL)selectFile:(NSString *)fullPath |
1054 |
inFileViewerRootedAtPath:(NSString *)rootFullpath |
1055 |
{ |
1056 |
CHECKGW_RET(NO); |
1057 |
return [gwapp selectFile: fullPath inFileViewerRootedAtPath: rootFullpath]; |
1058 |
return NO; |
1059 |
} |
1060 |
|
1061 |
+ (oneway void)rootViewerSelectFiles:(NSArray *)paths |
1062 |
{ |
1063 |
CHECKGW; |
1064 |
[gwapp rootViewerSelectFiles: paths]; |
1065 |
} |
1066 |
|
1067 |
+ (oneway void)openSelectedPaths:(NSArray *)paths |
1068 |
{ |
1069 |
CHECKGW; |
1070 |
[gwapp openSelectedPaths: paths]; |
1071 |
} |
1072 |
|
1073 |
+ (oneway void)performFileOperationWithDictionary:(NSDictionary *)dict |
1074 |
{ |
1075 |
CHECKGW; |
1076 |
[gwapp performFileOperationWithDictionary: dict]; |
1077 |
} |
1078 |
|
1079 |
+ (oneway void)performServiceWithName:(NSString *)sname |
1080 |
pasteboard:(NSPasteboard *)pboard |
1081 |
{ |
1082 |
NSPerformService(sname, pboard); |
1083 |
} |
1084 |
|
1085 |
+ (NSString *)trashPath |
1086 |
{ |
1087 |
CHECKGW_RET(nil); |
1088 |
return [gwapp trashPath]; |
1089 |
return nil; |
1090 |
} |
1091 |
|
1092 |
@end |