1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
|
/* FSNodeRep.m
*
* Copyright (C) 2004-2016 Free Software Foundation, Inc.
*
* Author: Enrico Sersale <enrico@imago.ro>
* Riccardo Mottola <rm@gnu.org>
* Date: March 2004
*
* This file is part of the GNUstep FSNode framework
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#include <math.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepGUI/GSTheme.h>
#import "FSNodeRep.h"
#import "FSNFunctions.h"
#import "ExtendedInfo.h"
#import "config.h"
#ifdef HAVE_GETMNTINFO
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#ifdef __NetBSD__
#define statfs statvfs
#endif
#endif
#endif /* HAVE_SYSTYPES */
#else
#if defined(HAVE_GETMNTENT) && defined (MNT_DIR)
#if defined(HAVE_MNTENT_H)
#include <mntent.h>
#elif defined(HAVE_SYS_MNTENT_H)
#include <sys/mntent.h>
#else
#undef HAVE_GETMNTENT
#endif
#endif
#endif
#define LABEL_W_FACT (8.0)
#define FONT_H_FACT (1.5)
static FSNodeRep *shared = nil;
@interface FSNodeRep (PrivateMethods)
- (id)initSharedInstance;
- (void)loadExtendedInfoModules;
- (NSArray *)bundlesWithExtension:(NSString *)extension
inPath:(NSString *)path;
@end
@implementation FSNodeRep (PrivateMethods)
+ (void)initialize
{
static BOOL initialized = NO;
if (initialized == NO) {
if ([self class] == [FSNodeRep class]) {
[FSNodeRep sharedInstance];
}
initialized = YES;
}
}
/*
* Loads and caches named images
* Images coming from GSTheme need to be recached on a theme change
*/
- (void)cacheIcons
{
[multipleSelIcon release];
multipleSelIcon = [[NSImage imageNamed:NSImageNameMultipleDocuments] retain];
[trashIcon release];
trashIcon = [[NSImage imageNamed:NSImageNameTrashEmpty] retain];
[trashFullIcon retain];
trashFullIcon = [[NSImage imageNamed:NSImageNameTrashFull] retain];
}
- (id)initSharedInstance
{
self = [super init];
if (self) {
NSBundle *bundle = [NSBundle bundleForClass: [FSNodeRep class]];
NSString *imagepath;
BOOL isdir;
NSString *libraryDir;
NSNotificationCenter *nc;
fm = [NSFileManager defaultManager];
nc = [NSNotificationCenter defaultCenter];
labelWFactor = LABEL_W_FACT;
/* images coming form GSTheme */
[self cacheIcons];
/* images for which we provide our own resources */
imagepath = [bundle pathForImageResource: @"FolderOpen"];
openFolderIcon = [[NSImage alloc] initWithContentsOfFile: imagepath];
imagepath = [bundle pathForImageResource: @"HardDisk"];
hardDiskIcon = [[NSImage alloc] initWithContentsOfFile: imagepath];
imagepath = [bundle pathForImageResource: @"HardDiskOpen"];
openHardDiskIcon = [[NSImage alloc] initWithContentsOfFile: imagepath];
iconsCache = [NSMutableDictionary new];
rootPath = path_separator();
RETAIN (rootPath);
libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
if (([fm fileExistsAtPath: libraryDir isDirectory: &isdir] && isdir) == NO)
{
if ([fm createDirectoryAtPath: libraryDir attributes: nil] == NO)
{
NSLog(@"Unable to create the Library directory. Quitting now");
[NSApp terminate: self];
}
}
thumbnailDir = [libraryDir stringByAppendingPathComponent: @"Thumbnails"];
RETAIN (thumbnailDir);
if (([fm fileExistsAtPath: thumbnailDir isDirectory: &isdir] && isdir) == NO) {
if ([fm createDirectoryAtPath: thumbnailDir attributes: nil] == NO) {
NSLog(@"Unable to create the thumbnails directory. Quitting now");
[NSApp terminate: self];
}
}
defSortOrder = FSNInfoNameType;
hideSysFiles = NO;
usesThumbnails = NO;
lockedPaths = [NSMutableArray new];
hiddenPaths = [NSArray new];
volumes = [[NSMutableSet alloc] initWithCapacity: 1];
[self setVolumes:[[NSWorkspace sharedWorkspace] mountedRemovableMedia]];
reservedNames = [[NSMutableSet alloc] initWithCapacity: 1];
[self loadExtendedInfoModules];
/* we observe a theme change to re-cache icons */
[nc addObserver:self selector:@selector(themeDidActivate:) name:GSThemeDidActivateNotification object:nil];
}
return self;
}
- (void)loadExtendedInfoModules
{
NSString *bundlesDir;
NSMutableArray *bundlesPaths;
NSEnumerator *enumerator;
NSMutableArray *loaded;
NSUInteger i;
bundlesPaths = [NSMutableArray array];
enumerator = [NSSearchPathForDirectoriesInDomains
(NSLibraryDirectory, NSAllDomainsMask, YES) objectEnumerator];
while ((bundlesDir = [enumerator nextObject]) != nil)
{
bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
[bundlesPaths addObjectsFromArray:
[self bundlesWithExtension: @"extinfo" inPath: bundlesDir]];
}
loaded = [NSMutableArray array];
for (i = 0; i < [bundlesPaths count]; i++) {
NSString *bpath = [bundlesPaths objectAtIndex: i];
NSBundle *bundle = [NSBundle bundleWithPath: bpath];
if (bundle) {
Class principalClass = [bundle principalClass];
if ([principalClass conformsToProtocol: @protocol(ExtendedInfo)]) {
CREATE_AUTORELEASE_POOL (pool);
id module = [[principalClass alloc] init];
NSString *name = [module menuName];
BOOL exists = NO;
int j;
for (j = 0; j < [loaded count]; j++) {
if ([name isEqual: [[loaded objectAtIndex: j] menuName]]) {
NSLog(@"duplicate module \"%@\" at %@", name, bpath);
exists = YES;
break;
}
}
if (exists == NO) {
[loaded addObject: module];
}
RELEASE ((id)module);
RELEASE (pool);
}
}
}
ASSIGN (extInfoModules, loaded);
}
- (NSArray *)bundlesWithExtension:(NSString *)extension
inPath:(NSString *)path
{
NSMutableArray *bundleList = [NSMutableArray array];
NSEnumerator *enumerator;
NSString *dir;
BOOL isDir;
if ((([fm fileExistsAtPath: path isDirectory: &isDir]) && isDir) == NO) {
return nil;
}
enumerator = [[fm directoryContentsAtPath: path] objectEnumerator];
while ((dir = [enumerator nextObject])) {
if ([[dir pathExtension] isEqualToString: extension]) {
[bundleList addObject: [path stringByAppendingPathComponent: dir]];
}
}
return bundleList;
}
- (void)themeDidActivate:(id)sender
{
/* we clean the cache of theme-derived images */
[iconsCache removeAllObjects];
[self cacheIcons];
}
@end
@implementation FSNodeRep
- (void)dealloc
{
RELEASE (extInfoModules);
RELEASE (lockedPaths);
RELEASE (volumes);
RELEASE (reservedNames);
RELEASE (rootPath);
RELEASE (hiddenPaths);
RELEASE (iconsCache);
RELEASE (tumbsCache);
RELEASE (thumbnailDir);
RELEASE (multipleSelIcon);
RELEASE (openFolderIcon);
RELEASE (hardDiskIcon);
RELEASE (openHardDiskIcon);
RELEASE (trashIcon);
RELEASE (trashFullIcon);
[super dealloc];
}
+ (FSNodeRep *)sharedInstance
{
if (shared == nil)
{
shared = [[FSNodeRep alloc] initSharedInstance];
}
return shared;
}
- (NSArray *)directoryContentsAtPath:(NSString *)path
{
NSArray *fnames = [fm directoryContentsAtPath: path];
NSString *hdnFilePath = [path stringByAppendingPathComponent: @".hidden"];
NSArray *hiddenNames = nil;
if ([fm fileExistsAtPath: hdnFilePath])
hiddenNames = [[NSString stringWithContentsOfFile: hdnFilePath] componentsSeparatedByString: @"\n"];
if (hiddenNames || hideSysFiles || [hiddenPaths count])
{
NSMutableArray *filteredNames = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [fnames count]; i++)
{
NSString *fname = [fnames objectAtIndex: i];
NSString *fpath = [path stringByAppendingPathComponent: fname];
BOOL hidden = NO;
if ([fname hasPrefix: @"."] && hideSysFiles)
hidden = YES;
if (hiddenNames && [hiddenNames containsObject: fname])
hidden = YES;
if ([hiddenPaths containsObject: fpath])
hidden = YES;
if (hidden == NO)
{
[filteredNames addObject: fname];
}
}
return filteredNames;
}
return fnames;
}
- (int)labelMargin
{
return 4;
}
- (float)labelWFactor
{
return labelWFactor;
}
- (void)setLabelWFactor:(float)f
{
labelWFactor = f;
}
- (float)heightOfFont:(NSFont *)font
{
// return [font defaultLineHeightForFont];
return ([font pointSize] * FONT_H_FACT);
}
- (int)defaultIconBaseShift
{
return 12;
}
- (void)setDefaultSortOrder:(int)order
{
defSortOrder = order;
}
- (unsigned int)defaultSortOrder
{
return defSortOrder;
}
- (SEL)defaultCompareSelector
{
SEL compareSel;
switch(defSortOrder) {
case FSNInfoNameType:
compareSel = @selector(compareAccordingToName:);
break;
case FSNInfoKindType:
compareSel = @selector(compareAccordingToKind:);
break;
case FSNInfoDateType:
compareSel = @selector(compareAccordingToDate:);
break;
case FSNInfoSizeType:
compareSel = @selector(compareAccordingToSize:);
break;
case FSNInfoOwnerType:
compareSel = @selector(compareAccordingToOwner:);
break;
default:
compareSel = @selector(compareAccordingToName:);
break;
}
return compareSel;
}
- (unsigned int)sortOrderForDirectory:(NSString *)dirpath
{
if ([fm isWritableFileAtPath: dirpath]) {
NSString *dictPath = [dirpath stringByAppendingPathComponent: @".gwsort"];
if ([fm fileExistsAtPath: dictPath]) {
NSDictionary *sortDict = [NSDictionary dictionaryWithContentsOfFile: dictPath];
if (sortDict) {
return [[sortDict objectForKey: @"sort"] intValue];
}
}
}
return defSortOrder;
}
- (SEL)compareSelectorForDirectory:(NSString *)dirpath
{
int order = [self sortOrderForDirectory: dirpath];
SEL compareSel;
switch(order) {
case FSNInfoNameType:
compareSel = @selector(compareAccordingToName:);
break;
case FSNInfoKindType:
compareSel = @selector(compareAccordingToKind:);
break;
case FSNInfoDateType:
compareSel = @selector(compareAccordingToDate:);
break;
case FSNInfoSizeType:
compareSel = @selector(compareAccordingToSize:);
break;
case FSNInfoOwnerType:
compareSel = @selector(compareAccordingToOwner:);
break;
default:
compareSel = @selector(compareAccordingToName:);
break;
}
return compareSel;
}
- (void)setHideSysFiles:(BOOL)value
{
hideSysFiles = value;
}
- (BOOL)hideSysFiles
{
return hideSysFiles;
}
- (void)setHiddenPaths:(NSArray *)paths
{
ASSIGN (hiddenPaths, paths);
}
- (NSArray *)hiddenPaths
{
return hiddenPaths;
}
- (void)lockNode:(FSNode *)node
{
NSString *path = [node path];
if ([lockedPaths containsObject: path] == NO)
{
[lockedPaths addObject: path];
}
}
- (void)lockPath:(NSString *)path
{
if ([lockedPaths containsObject: path] == NO)
{
[lockedPaths addObject: path];
}
}
- (void)lockNodes:(NSArray *)nodes
{
NSUInteger i;
for (i = 0; i < [nodes count]; i++)
{
NSString *path = [[nodes objectAtIndex: i] path];
if ([lockedPaths containsObject: path] == NO)
{
[lockedPaths addObject: path];
}
}
}
- (void)lockPaths:(NSArray *)paths
{
NSUInteger i;
for (i = 0; i < [paths count]; i++)
{
NSString *path = [paths objectAtIndex: i];
if ([lockedPaths containsObject: path] == NO)
{
[lockedPaths addObject: path];
}
}
}
- (void)unlockNode:(FSNode *)node
{
NSString *path = [node path];
if ([lockedPaths containsObject: path])
{
[lockedPaths removeObject: path];
}
}
- (void)unlockPath:(NSString *)path
{
if ([lockedPaths containsObject: path])
{
[lockedPaths removeObject: path];
}
}
- (void)unlockNodes:(NSArray *)nodes
{
NSUInteger i;
for (i = 0; i < [nodes count]; i++)
{
NSString *path = [[nodes objectAtIndex: i] path];
if ([lockedPaths containsObject: path])
{
[lockedPaths removeObject: path];
}
}
}
- (void)unlockPaths:(NSArray *)paths
{
NSUInteger i;
for (i = 0; i < [paths count]; i++)
{
NSString *path = [paths objectAtIndex: i];
if ([lockedPaths containsObject: path])
{
[lockedPaths removeObject: path];
}
}
}
- (BOOL)isNodeLocked:(FSNode *)node
{
NSString *path = [node path];
NSUInteger i;
if ([lockedPaths containsObject: path])
return YES;
for (i = 0; i < [lockedPaths count]; i++)
{
NSString *lpath = [lockedPaths objectAtIndex: i];
if (isSubpathOfPath(lpath, path)) {
return YES;
}
}
return NO;
}
- (BOOL)isPathLocked:(NSString *)path
{
NSUInteger i;
if ([lockedPaths containsObject: path])
return YES;
for (i = 0; i < [lockedPaths count]; i++)
{
NSString *lpath = [lockedPaths objectAtIndex: i];
if (isSubpathOfPath(lpath, path))
return YES;
}
return NO;
}
- (void)setVolumes:(NSArray *)vls
{
[volumes removeAllObjects];
[volumes addObjectsFromArray: vls];
}
- (void)addVolumeAt:(NSString *)path
{
[volumes addObject: path];
}
- (void)removeVolumeAt:(NSString *)path
{
[volumes removeObject: path];
}
- (NSSet *)volumes
{
return volumes;
}
- (void)setReservedNames:(NSArray *)names
{
[reservedNames removeAllObjects];
[reservedNames addObjectsFromArray: names];
}
- (NSSet *)reservedNames
{
return reservedNames;
}
- (BOOL)isReservedName:(NSString *)name
{
return [reservedNames containsObject: name];
}
- (void)setUseThumbnails:(BOOL)value
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
usesThumbnails = value;
if (usesThumbnails) {
[self prepareThumbnailsCache];
}
[defaults setBool: usesThumbnails forKey: @"use_thumbnails"];
}
- (BOOL)usesThumbnails
{
return usesThumbnails;
}
- (void)thumbnailsDidChange:(NSDictionary *)info
{
NSArray *deleted = [info objectForKey: @"deleted"];
NSArray *created = [info objectForKey: @"created"];
NSUInteger i;
if (usesThumbnails == NO) {
return;
}
if ([deleted count]) {
for (i = 0; i < [deleted count]; i++) {
[tumbsCache removeObjectForKey: [deleted objectAtIndex: i]];
}
}
if ([created count]) {
NSString *dictName = @"thumbnails.plist";
NSString *dictPath = [thumbnailDir stringByAppendingPathComponent: dictName];
if ([fm fileExistsAtPath: dictPath]) {
NSDictionary *tdict = [NSDictionary dictionaryWithContentsOfFile: dictPath];
for (i = 0; i < [created count]; i++) {
NSString *key = [created objectAtIndex: i];
NSString *tumbname = [tdict objectForKey: key];
NSString *tumbpath = [thumbnailDir stringByAppendingPathComponent: tumbname];
if ([fm fileExistsAtPath: tumbpath]) {
NSImage *tumb = nil;
NS_DURING
{
tumb = [[NSImage alloc] initWithContentsOfFile: tumbpath];
if (tumb) {
[tumbsCache setObject: tumb forKey: key];
RELEASE (tumb);
}
}
NS_HANDLER
{
NSLog(@"BAD IMAGE '%@'", tumbpath);
}
NS_ENDHANDLER
}
}
}
}
}
- (NSArray *)availableExtendedInfoNames
{
NSMutableArray *names = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [extInfoModules count]; i++)
{
id module = [extInfoModules objectAtIndex: i];
[names addObject: [module menuName]];
}
return names;
}
- (NSDictionary *)extendedInfoOfType:(NSString *)type
forNode:(FSNode *)anode
{
NSUInteger i;
for (i = 0; i < [extInfoModules count]; i++)
{
id module = [extInfoModules objectAtIndex: i];
NSString *mname = [module menuName];
if ([mname isEqual: type])
{
return [module extendedInfoForNode: anode];
}
}
return nil;
}
@end
|